- <?php
- * @file
- * Tests for text.module.
- */
-
- class TextFieldTestCase extends BackdropWebTestCase {
- protected $instance;
- protected $admin_user;
- protected $web_user;
-
-
- * @var array
- */
- protected $field;
-
-
- * @var string
- */
- protected $field_name;
-
- function setUp() {
- parent::setUp('field_test');
-
- $this->admin_user = $this->backdropCreateUser(array('administer filters'));
- $this->web_user = $this->backdropCreateUser(array('access field_test content', 'administer field_test content'));
- $this->backdropLogin($this->web_user);
- }
-
-
-
-
- * Test text field validation.
- */
- function testTextFieldValidation() {
-
- $max_length = 3;
- $this->field = array(
- 'field_name' => backdrop_strtolower($this->randomName()),
- 'type' => 'text',
- 'settings' => array(
- 'max_length' => $max_length,
- )
- );
- field_create_field($this->field);
- $this->instance = array(
- 'field_name' => $this->field['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'widget' => array(
- 'type' => 'text_textfield',
- ),
- 'display' => array(
- 'default' => array(
- 'type' => 'text_default',
- ),
- ),
- );
- field_create_instance($this->instance);
-
- $entity = field_test_create_entity();
- $langcode = LANGUAGE_NONE;
- for ($i = 0; $i <= $max_length + 2; $i++) {
- $entity->{$this->field['field_name']}[$langcode][0]['value'] = str_repeat('x', $i);
- try {
- field_attach_validate('test_entity', $entity);
- $this->assertTrue($i <= $max_length, "Length $i does not cause validation error when max_length is $max_length");
- }
- catch (FieldValidationException $e) {
- $this->assertTrue($i > $max_length, "Length $i causes validation error when max_length is $max_length");
- }
- }
- }
-
-
- * Test widgets.
- */
- function testTextfieldWidgets() {
- $this->_testTextfieldWidgets('text', 'text_textfield');
- $this->_testTextfieldWidgets('text_long', 'text_textarea');
- }
-
-
- * Helper function for testTextfieldWidgets().
- */
- function _testTextfieldWidgets($field_type, $widget_type) {
-
- $entity_type = 'test_entity';
- $field_name = backdrop_strtolower($this->randomName());
- $this->field = array(
- 'field_name' => $field_name,
- 'type' => $field_type,
- );
- field_create_field($this->field);
- $this->instance = array(
- 'field_name' => $field_name,
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'label' => $this->randomName() . '_label',
- 'settings' => array(
- 'text_processing' => TRUE,
- ),
- 'widget' => array(
- 'type' => $widget_type,
- ),
- 'display' => array(
- 'full' => array(
- 'type' => 'text_default',
- ),
- ),
- );
- field_create_instance($this->instance);
- $langcode = LANGUAGE_NONE;
-
-
- $this->backdropGet('test-entity/add/test-bundle');
- $this->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget is displayed');
- $this->assertNoFieldByName("{$field_name}[$langcode][0][format]", '1', 'Format selector is not displayed');
-
-
- $value = $this->randomName();
- $edit = array(
- "{$field_name}[$langcode][0][value]" => $value,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
- $id = $match[1];
- $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
-
-
- $entity = field_test_entity_test_load($id);
- $entity->content = field_attach_view($entity_type, $entity, 'full');
- $this->content = backdrop_render($entity->content);
- $this->assertText($value, 'Filtered tags are not displayed');
- }
-
-
- * Test widgets + 'formatted_text' setting.
- */
- function testTextfieldWidgetsFormatted() {
- $this->_testTextfieldWidgetsFormatted('text', 'text_textfield');
- $this->_testTextfieldWidgetsFormatted('text_long', 'text_textarea');
- }
-
-
- * Helper function for testTextfieldWidgetsFormatted().
- */
- function _testTextfieldWidgetsFormatted($field_type, $widget_type) {
-
- $entity_type = 'test_entity';
- $this->field_name = backdrop_strtolower($this->randomName());
- $this->field = array('field_name' => $this->field_name, 'type' => $field_type);
- field_create_field($this->field);
- $this->instance = array(
- 'field_name' => $this->field_name,
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'label' => $this->randomName() . '_label',
- 'settings' => array(
- 'text_processing' => TRUE,
- ),
- 'widget' => array(
- 'type' => $widget_type,
- ),
- 'display' => array(
- 'full' => array(
- 'type' => 'text_default',
- ),
- ),
- );
- field_create_instance($this->instance);
- $langcode = LANGUAGE_NONE;
-
-
- $this->backdropLogin($this->admin_user);
- foreach (filter_formats() as $format) {
- if ($format->format != filter_fallback_format()) {
- $this->backdropPost('admin/config/content/formats/' . $format->format . '/disable', array(), t('Disable'));
- }
- }
- $this->backdropLogin($this->web_user);
-
-
-
- $this->backdropGet('test-entity/add/test-bundle');
- $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget is displayed');
- $this->assertNoFieldByName("{$this->field_name}[$langcode][0][format]", '', 'Format selector is not displayed');
-
-
- $value = '<em>' . $this->randomName() . '</em>';
- $edit = array(
- "{$this->field_name}[$langcode][0][value]" => $value,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
- $id = $match[1];
- $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
-
-
- $entity = field_test_entity_test_load($id);
- $entity->content = field_attach_view($entity_type, $entity, 'full');
- $this->content = backdrop_render($entity->content);
- $this->assertNoRaw($value, 'HTML tags are not displayed.');
- $this->assertRaw(check_plain($value), 'Escaped HTML is displayed correctly.');
-
-
-
- $this->backdropLogin($this->admin_user);
- $edit = array(
- 'format' => backdrop_strtolower($this->randomName()),
- 'name' => $this->randomName(),
- );
- $this->backdropPost('admin/config/content/formats/add', $edit, t('Save configuration'));
- filter_formats_reset();
- $this->checkPermissions(array(), TRUE);
- $format = filter_format_load($edit['format']);
- $format_id = $format->format;
- $permission = filter_permission_name($format);
- $web_user_roles = array_diff($this->web_user->roles, array(BACKDROP_AUTHENTICATED_ROLE));
- $web_user_role = reset($web_user_roles);
- user_role_grant_permissions($web_user_role, array($permission));
- $this->backdropLogin($this->web_user);
-
-
-
- $this->backdropGet('test-entity/manage/' . $id . '/edit');
- $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", NULL, 'Widget is displayed');
- $this->assertFieldByName("{$this->field_name}[$langcode][0][format]", NULL, 'Format selector is displayed');
-
-
- $edit = array(
- "{$this->field_name}[$langcode][0][format]" => $format_id,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated');
-
-
- $entity = field_test_entity_test_load($id);
- $entity->content = field_attach_view($entity_type, $entity, 'full');
- $this->content = backdrop_render($entity->content);
- $this->assertRaw($value, 'Value is displayed unfiltered');
- }
-
-
- * Test widgets for fields with selected allowed formats.
- */
- public function testTextfieldWidgetsAllowedFormats() {
-
- $format1 = array(
- 'format' => backdrop_strtolower($this->randomName()),
- 'name' => $this->randomName(),
- 'weight' => -3,
- );
- $format1 = (object) $format1;
- filter_format_save($format1);
-
-
- $format2 = array(
- 'format' => backdrop_strtolower($this->randomName()),
- 'name' => $this->randomName(),
- 'weight' => -2,
- 'filters' => array(
- 'filter_html' => array(
- 'status' => 1,
- 'settings' => array(
- 'allowed_html' => '<strong>',
- ),
- ),
- ),
- );
- $format2 = (object) $format2;
- filter_format_save($format2);;
-
-
- $format3 = array(
- 'weight' => -1,
- 'format' => backdrop_strtolower($this->randomName()),
- 'name' => $this->randomName(),
- );
- $format3 = (object) $format3;
- filter_format_save($format3);
-
-
- $web_user_roles = array_diff($this->web_user->roles, array(BACKDROP_AUTHENTICATED_ROLE));
- $web_user_role = reset($web_user_roles);
- user_role_grant_permissions($web_user_role, array(
- filter_permission_name($format1),
- filter_permission_name($format2),
- filter_permission_name($format3),
- ));
-
-
- $this->field_name = backdrop_strtolower($this->randomName());
- $this->field = array('field_name' => $this->field_name, 'type' => 'text_long');
- field_create_field($this->field);
- $this->instance = array(
- 'field_name' => $this->field_name,
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'label' => $this->randomName() . '_label',
- 'settings' => array(
- 'text_processing' => TRUE,
- 'allowed_formats' => array(),
- ),
- 'widget' => array(
- 'type' => 'text_textarea',
- ),
- 'display' => array(
- 'full' => array(
- 'type' => 'text_default',
- ),
- ),
- );
- $this->instance = field_create_instance($this->instance);
-
-
- $this->backdropLogin($this->web_user);
- $this->backdropGet('test-entity/add/test-bundle');
- $this->assertFieldByName("{$this->field_name}[und][0][value]");
- $this->assertRaw('<option value="' . $format1->format . '"');
- $this->assertRaw('<option value="' . $format2->format . '"');
- $this->assertRaw('<option value="' . $format3->format . '"');
-
- $filtered_markup = '<div><strong><span>Hello World</span></strong></div>';
- $edit = array(
- "{$this->field_name}[und][0][value]" => $filtered_markup,
- );
- $this->backdropPost(NULL, $edit, 'Save');
- preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
- $id = $match[1];
- $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
-
-
- $entity = field_test_entity_test_load($id);
- $entity->content = field_attach_view($entity->entityType(), $entity, 'full');
- $this->content = backdrop_render($entity->content);
- $this->assertRaw('<div><strong><span>', 'Value is displayed unfiltered');
-
-
- $this->instance['settings']['allowed_formats'] = array($format1->format);
- field_update_instance($this->instance);
-
-
-
- $this->backdropGet('test-entity/add/test-bundle');
- $this->assertFieldByName("{$this->field_name}[und][0][value]");
- $this->assertNoFieldByName("{$this->field_name}[und][0][format]");
-
-
- $entity = field_test_entity_test_load($id);
- $entity->content = field_attach_view($entity->entityType(), $entity, 'full');
- $this->content = backdrop_render($entity->content);
- $this->assertRaw('<div><strong><span>', 'Value is displayed unfiltered');
-
-
- $this->instance['settings']['allowed_formats'] = array(
- $format1->format,
- $format2->format
- );
- field_update_instance($this->instance);
-
- $this->backdropGet('test-entity/add/test-bundle');
-
- $this->assertFieldByName("{$this->field_name}[und][0][value]", NULL);
- $this->assertRaw('<option value="' . $format1->format . '"');
- $this->assertRaw('<option value="' . $format2->format . '"');
- $this->assertNoRaw('<option value="' . $format3->format . '"');
-
-
- $this->instance['settings']['allowed_formats'] = array();
- field_update_instance($this->instance);
-
- $this->backdropGet('test-entity/add/test-bundle');
-
- $this->assertFieldByName("{$this->field_name}[und][0][value]", NULL);
- $this->assertRaw('<option value="' . $format1->format . '"');
- $this->assertRaw('<option value="' . $format2->format . '"');
- $this->assertRaw('<option value="' . $format3->format . '"');
- }
-
- }
-
- class TextSummaryTestCase extends BackdropWebTestCase {
-
-
- * @var User
- */
- protected $post_creator;
-
- function setUp() {
- parent::setUp();
- $this->post_creator = $this->backdropCreateUser(array('create post content', 'edit own post content'));
- }
-
-
- * Tests an edge case where the first sentence is a question and
- * subsequent sentences are not. This edge case is documented at
- * http://drupal.org/node/180425.
- */
- function testFirstSentenceQuestion() {
- $text = 'A question? A sentence. Another sentence.';
- $expected = 'A question? A sentence.';
- $this->assertTextSummary($text, $expected, NULL, 30);
- }
-
-
- * Test summary with long example.
- */
- function testLongSentence() {
- $text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' .
- 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ' .
- 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ' .
- 'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
- $expected = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' .
- 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ' .
- 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.';
-
- $this->assertTextSummary($text, $expected, NULL, 340);
- }
-
-
- * Test various summary length edge cases.
- */
- function testLength() {
-
- $text = "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>";
-
-
-
-
- $expected = array(
- "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
- "",
- "<p></p>",
- "<p></p>",
- "<p>\n</p>",
- "<p>\nH</p>",
- "<p>\nHi</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
- "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
- "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
- );
-
-
- $expected_lb = array(
- "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
- "",
- "<p></p>",
- "<p></p>",
- "<p></p>",
- "<p></p>",
- "<p></p>",
- "<p>\nHi</p>",
- "<p>\nHi</p>",
- "<p>\nHi</p>",
- "<p>\nHi</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>",
- "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
- "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
- "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
- );
-
-
- for ($i = 0; $i <= 37; $i++) {
- $this->assertTextSummary($text, $expected[$i], NULL, $i);
- $this->assertTextSummary($text, $expected_lb[$i], 'filtered_html', $i);
- }
- }
-
-
- * Calls text_summary() and asserts that the expected teaser is returned.
- */
- function assertTextSummary($text, $expected, $format = NULL, $size = NULL) {
- $summary = text_summary($text, $format, $size);
- $this->assertIdentical($summary, $expected, format_string('Generated @format summary "@summary" matches expected "@expected".', array('@format' => $format, '@summary' => str_replace("\n", '\n', $summary), '@expected' => str_replace("\n", '\n', $expected))));
- }
-
-
- * Test sending only summary.
- */
- function testOnlyTextSummary() {
-
- $this->backdropLogin($this->post_creator);
-
- $summary = $this->randomName();
- $edit = array(
- "title" => $this->randomName(),
- "body[und][0][summary]" => $summary,
- );
- $this->backdropPost('node/add/post', $edit, t('Save'));
- $node = $this->backdropGetNodeByTitle($edit['title']);
-
- $this->assertIdentical($node->body['und'][0]['summary'], $summary, 'Post with with summary and no body has been submitted.');
- }
- }
-
- class TextTranslationTestCase extends BackdropWebTestCase {
-
-
- * @var object
- */
- protected $format;
-
-
- * @var User
- */
- protected $admin;
-
-
- * @var User
- */
- protected $translator;
-
- function setUp() {
- parent::setUp('locale', 'translation');
-
- $full_html_format = filter_format_load('full_html');
- $this->format = $full_html_format->format;
- $this->admin = $this->backdropCreateUser(array(
- 'administer languages',
- 'administer content types',
- 'access administration pages',
- 'bypass node access',
- 'administer fields',
- filter_permission_name($full_html_format),
- ));
- $this->translator = $this->backdropCreateUser(array('create post content', 'edit own post content', 'translate content'));
-
-
- $this->backdropLogin($this->admin);
- $edit = array('langcode' => 'fr');
- $this->backdropPost('admin/config/regional/language/add', $edit, t('Add language'));
-
-
- $edit = array('language' => TRANSLATION_ENABLED);
- $this->backdropPost('admin/structure/types/manage/post', $edit, t('Save content type'));
- $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Post')), 'Post content type has been updated.');
- }
-
-
- * Test that a plaintext textfield widget is correctly populated.
- */
- function testTextField() {
-
- $edit = array('instance[settings][text_processing]' => 0);
- $this->backdropPost('admin/structure/types/manage/post/fields/body', $edit, t('Save settings'));
-
-
- $this->backdropLogin($this->translator);
-
-
- $langcode = LANGUAGE_NONE;
- $body = $this->randomName();
- $edit = array(
- 'title' => $this->randomName(),
- 'langcode' => 'en',
- "body[$langcode][0][value]" => $body,
- );
-
-
- $this->backdropPost('node/add/post', $edit, t('Save'));
- $node = $this->backdropGetNodeByTitle($edit['title']);
- $this->backdropGet("node/$node->nid/translate");
- $this->clickLink(t('Add translation'));
- $this->assertFieldByXPath("//textarea[@name='body[$langcode][0][value]']", $body, 'The textfield widget is populated.');
- }
-
-
- * Check that user that does not have access the field format cannot see the
- * source value when creating a translation.
- */
- function testTextFieldFormatted() {
-
- $edit = array('field[cardinality]' => -1);
- $this->backdropPost('admin/structure/types/manage/post/fields/body', $edit, t('Save settings'));
- $this->backdropGet('node/add/post');
- $this->assertFieldByXPath("//input[@name='body_add_more']", t('Add another'), 'Body field cardinality set to multiple.');
-
- $body = array(
- $this->randomName(),
- $this->randomName(),
- );
-
-
-
- $title = $this->randomName();
- $edit = array(
- 'title' => $title,
- 'langcode' => 'en',
- );
- $this->backdropPost('node/add/post', $edit, t('Save'));
- $node = $this->backdropGetNodeByTitle($title);
-
-
-
- $formats = array('full_html', 'filtered_html');
- $langcode = LANGUAGE_NONE;
- foreach ($body as $delta => $value) {
- $edit = array(
- "body[$langcode][$delta][value]" => $value,
- "body[$langcode][$delta][format]" => array_shift($formats),
- );
- $this->backdropPost("node/$node->nid/edit", $edit, t('Save'));
- $this->assertText($body[$delta], format_string('The body field with delta @delta has been saved.', array('@delta' => $delta)));
- }
-
-
- $this->backdropLogin($this->translator);
-
-
- $this->backdropGet("node/$node->nid/translate");
- $this->clickLink(t('Add translation'));
- $this->assertNoText($body[0], format_string('The body field with delta @delta is hidden.', array('@delta' => 0)));
- $this->assertText($body[1], format_string('The body field with delta @delta is shown.', array('@delta' => 1)));
- }
- }