text.test 18.4 KB
Newer Older
Dries's avatar
 
Dries committed
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Tests for text.module.
 */

8
use Drupal\field\FieldValidationException;
9
use Drupal\simpletest\WebTestBase;
10

11
class TextFieldTestCase extends WebTestBase {
Dries's avatar
 
Dries committed
12
  protected $instance;
13 14
  protected $admin_user;
  protected $web_user;
Dries's avatar
 
Dries committed
15

16
  public static function getInfo() {
Dries's avatar
 
Dries committed
17
    return array(
18
      'name'  => 'Text field',
19
      'description'  => "Test the creation of text fields.",
20
      'group' => 'Field types'
Dries's avatar
 
Dries committed
21 22 23 24
    );
  }

  function setUp() {
25
    parent::setUp('field_test');
26

27 28 29
    $this->admin_user = $this->drupalCreateUser(array('administer filters'));
    $this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
    $this->drupalLogin($this->web_user);
Dries's avatar
 
Dries committed
30 31
  }

32 33 34 35 36 37 38 39
  // Test fields.

  /**
   * Test text field validation.
   */
  function testTextFieldValidation() {
    // Create a field with settings to validate.
    $max_length = 3;
40 41 42 43 44 45 46 47 48 49
    $this->field = array(
      'field_name' => drupal_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'],
50
      'entity_type' => 'test_entity',
51
      'bundle' => 'test_bundle',
52 53 54 55
      'widget' => array(
        'type' => 'text_textfield',
      ),
      'display' => array(
56
        'default' => array(
57 58 59 60 61
          'type' => 'text_default',
        ),
      ),
    );
    field_create_instance($this->instance);
62
    // Test valid and invalid values with field_attach_validate().
63
    $entity = field_test_create_stub_entity();
64
    $langcode = LANGUAGE_NOT_SPECIFIED;
65
    for ($i = 0; $i <= $max_length + 2; $i++) {
66
      $entity->{$this->field['field_name']}[$langcode][0]['value'] = str_repeat('x', $i);
67 68 69 70 71 72 73 74 75 76
      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");
      }
    }
  }

77 78 79 80 81 82 83
  /**
   * Test widgets.
   */
  function testTextfieldWidgets() {
    $this->_testTextfieldWidgets('text', 'text_textfield');
    $this->_testTextfieldWidgets('text_long', 'text_textarea');
  }
Dries's avatar
 
Dries committed
84 85

  /**
86
   * Helper function for testTextfieldWidgets().
Dries's avatar
 
Dries committed
87
   */
88 89 90
  function _testTextfieldWidgets($field_type, $widget_type) {
    // Setup a field and instance
    $entity_type = 'test_entity';
91
    $this->field_name = drupal_strtolower($this->randomName());
92 93 94 95
    $this->field = array('field_name' => $this->field_name, 'type' => $field_type);
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
96
      'entity_type' => 'test_entity',
97
      'bundle' => 'test_bundle',
98 99 100 101 102 103
      'label' => $this->randomName() . '_label',
      'settings' => array(
        'text_processing' => TRUE,
      ),
      'widget' => array(
        'type' => $widget_type,
104 105 106 107 108 109
      ),
      'display' => array(
        'full' => array(
          'type' => 'text_default',
        ),
      ),
110 111
    );
    field_create_instance($this->instance);
112
    $langcode = LANGUAGE_NOT_SPECIFIED;
Dries's avatar
 
Dries committed
113

114
    // Display creation form.
115
    $this->drupalGet('test-entity/add/test_bundle');
116 117
    $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', t('Widget is displayed'));
    $this->assertNoFieldByName("{$this->field_name}[$langcode][0][format]", '1', t('Format selector is not displayed'));
118 119 120 121

    // Submit with some value.
    $value = $this->randomName();
    $edit = array(
122
      "{$this->field_name}[$langcode][0][value]" => $value,
123 124
    );
    $this->drupalPost(NULL, $edit, t('Save'));
125
    preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
126
    $id = $match[1];
127
    $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
128

129
    // Display the entity.
130
    $entity = field_test_entity_test_load($id);
131
    $entity->content = field_attach_view($entity_type, $entity, 'full');
132 133 134 135 136 137 138 139 140 141
    $this->content = drupal_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');
Dries's avatar
 
Dries committed
142 143 144
  }

  /**
145
   * Helper function for testTextfieldWidgetsFormatted().
Dries's avatar
 
Dries committed
146
   */
147 148 149
  function _testTextfieldWidgetsFormatted($field_type, $widget_type) {
    // Setup a field and instance
    $entity_type = 'test_entity';
150
    $this->field_name = drupal_strtolower($this->randomName());
151 152 153 154
    $this->field = array('field_name' => $this->field_name, 'type' => $field_type);
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
155
      'entity_type' => 'test_entity',
156
      'bundle' => 'test_bundle',
157 158 159 160 161 162
      'label' => $this->randomName() . '_label',
      'settings' => array(
        'text_processing' => TRUE,
      ),
      'widget' => array(
        'type' => $widget_type,
163 164 165 166 167 168
      ),
      'display' => array(
        'full' => array(
          'type' => 'text_default',
        ),
      ),
169 170
    );
    field_create_instance($this->instance);
171
    $langcode = LANGUAGE_NOT_SPECIFIED;
172

173
    // Disable all text formats besides the plain text fallback format.
174 175 176
    $this->drupalLogin($this->admin_user);
    foreach (filter_formats() as $format) {
      if ($format->format != filter_fallback_format()) {
177
        $this->drupalPost('admin/config/content/formats/' . $format->format . '/disable', array(), t('Disable'));
178 179 180 181 182 183
      }
    }
    $this->drupalLogin($this->web_user);

    // Display the creation form. Since the user only has access to one format,
    // no format selector will be displayed.
184
    $this->drupalGet('test-entity/add/test_bundle');
185 186
    $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', t('Widget is displayed'));
    $this->assertNoFieldByName("{$this->field_name}[$langcode][0][format]", '', t('Format selector is not displayed'));
187 188

    // Submit with data that should be filtered.
189
    $value = '<em>' . $this->randomName() . '</em>';
190
    $edit = array(
191
      "{$this->field_name}[$langcode][0][value]" => $value,
192 193
    );
    $this->drupalPost(NULL, $edit, t('Save'));
194
    preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
195
    $id = $match[1];
196
    $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
197

198
    // Display the entity.
199
    $entity = field_test_entity_test_load($id);
200
    $entity->content = field_attach_view($entity_type, $entity, 'full');
201
    $this->content = drupal_render($entity->content);
202 203
    $this->assertNoRaw($value, t('HTML tags are not displayed.'));
    $this->assertRaw(check_plain($value), t('Escaped HTML is displayed correctly.'));
204 205 206 207

    // Create a new text format that does not escape HTML, and grant the user
    // access to it.
    $this->drupalLogin($this->admin_user);
208 209 210 211
    $edit = array(
      'format' => drupal_strtolower($this->randomName()),
      'name' => $this->randomName(),
    );
212
    $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
213
    filter_formats_reset();
214
    $this->checkPermissions(array(), TRUE);
215 216 217
    $format = filter_format_load($edit['format']);
    $format_id = $format->format;
    $permission = filter_permission_name($format);
218 219 220
    $roles = $this->web_user->roles;
    unset($roles[DRUPAL_AUTHENTICATED_RID]);
    $rid = key($roles);
221
    user_role_grant_permissions($rid, array($permission));
222
    $this->drupalLogin($this->web_user);
223 224 225

    // Display edition form.
    // We should now have a 'text format' selector.
226
    $this->drupalGet('test-entity/manage/' . $id . '/edit');
227 228
    $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", NULL, t('Widget is displayed'));
    $this->assertFieldByName("{$this->field_name}[$langcode][0][format]", NULL, t('Format selector is displayed'));
229

230
    // Edit and change the text format to the new one that was created.
231
    $edit = array(
232
      "{$this->field_name}[$langcode][0][format]" => $format_id,
233 234
    );
    $this->drupalPost(NULL, $edit, t('Save'));
235
    $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), t('Entity was updated'));
236

237
    // Display the entity.
238
    $entity = field_test_entity_test_load($id);
239
    $entity->content = field_attach_view($entity_type, $entity, 'full');
240
    $this->content = drupal_render($entity->content);
241
    $this->assertRaw($value, t('Value is displayed unfiltered'));
242
  }
Dries's avatar
 
Dries committed
243
}
244

245
class TextSummaryTestCase extends WebTestBase {
246 247
  protected $profile = 'standard';

248 249
  public static function getInfo() {
    return array(
250 251
      'name' => 'Text summary',
      'description' => 'Test text_summary() with different strings and lengths.',
252
      'group' => 'Field types',
253 254 255
    );
  }

256 257 258 259 260
  function setUp() {
    parent::setUp();
    $this->article_creator = $this->drupalCreateUser(array('create article content', 'edit own article content'));
  }

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  /**
   * 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->callTextSummary($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. ' . // 125
            'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ' . // 108
            'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ' . // 103
            'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; // 110
    $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.';
    // First three sentences add up to: 336, so add one for space and then 3 to get half-way into next word.
    $this->callTextSummary($text, $expected, NULL, 340);
  }

  /**
   * Test various summary length edge cases.
   */
  function testLength() {
    // This string tests a number of edge cases.
    $text = "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>";

    // The summaries we expect text_summary() to return when $size is the index
    // of each array item.
    // Using no text format:
    $expected = array(
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
      "<",
      "<p",
      "<p>",
      "<p>\n",
      "<p>\nH",
      "<p>\nHi",
      "<p>\nHi\n",
      "<p>\nHi\n<",
      "<p>\nHi\n</",
      "<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>",
    );

    // And using a text format WITH the line-break and htmlcorrector filters.
    $expected_lb = array(
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
341
      "",
342 343 344 345 346
      "<p></p>",
      "<p></p>",
      "<p></p>",
      "<p></p>",
      "<p></p>",
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
      "<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>",
    );

    // Test text_summary() for different sizes.
    for ($i = 0; $i <= 37; $i++) {
382
      $this->callTextSummary($text, $expected[$i],    NULL, $i);
383 384
      $this->callTextSummary($text, $expected_lb[$i], 'plain_text', $i);
      $this->callTextSummary($text, $expected_lb[$i], 'filtered_html', $i);
385 386 387 388 389 390 391 392
    }
  }

  /**
   * Calls text_summary() and asserts that the expected teaser is returned.
   */
  function callTextSummary($text, $expected, $format = NULL, $size = NULL) {
    $summary = text_summary($text, $format, $size);
393
    $this->assertIdentical($summary, $expected, t('Generated summary "@summary" matches expected "@expected".', array('@summary' => $summary, '@expected' => $expected)));
394
  }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

  /**
   * Test sending only summary.
   */
  function testOnlyTextSummary() {
    // Login as article creator.
    $this->drupalLogin($this->article_creator);
    // Create article with summary but empty body.
    $summary = $this->randomName();
    $edit = array(
      "title" => $this->randomName(),
      "body[und][0][summary]" => $summary,
    );
    $this->drupalPost('node/add/article', $edit, t('Save'));
    $node = $this->drupalGetNodeByTitle($edit['title']);

    $this->assertIdentical($node->body['und'][0]['summary'], $summary, t('Article with with summary and no body has been submitted.'));
  }
413
}
414

415
class TextTranslationTestCase extends WebTestBase {
416 417
  protected $profile = 'standard';

418 419 420 421 422 423 424 425 426 427 428
  public static function getInfo() {
    return array(
      'name' => 'Text translation',
      'description' => 'Check if the text field is correctly prepared for translation.',
      'group' => 'Field types',
    );
  }

  function setUp() {
    parent::setUp('locale', 'translation');

429 430 431 432 433 434 435 436 437
    $full_html_format = filter_format_load('full_html');
    $this->format = $full_html_format->format;
    $this->admin = $this->drupalCreateUser(array(
      'administer languages',
      'administer content types',
      'access administration pages',
      'bypass node access',
      filter_permission_name($full_html_format),
    ));
438 439 440 441 442 443 444 445
    $this->translator = $this->drupalCreateUser(array('create article content', 'edit own article content', 'translate content'));

    // Enable an additional language.
    $this->drupalLogin($this->admin);
    $edit = array('langcode' => 'fr');
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));

    // Set "Article" content type to use multilingual support with translation.
446
    $edit = array('node_type_language' => TRANSLATION_ENABLED);
447
    $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
448
    $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Article')), t('Article content type has been updated.'));
449 450
  }

451 452 453 454 455 456 457 458 459 460 461 462
  /**
   * Test that a plaintext textfield widget is correctly populated.
   */
  function testTextField() {
    // Disable text processing for body.
    $edit = array('instance[settings][text_processing]' => 0);
    $this->drupalPost('admin/structure/types/manage/article/fields/body', $edit, t('Save settings'));

    // Login as translator.
    $this->drupalLogin($this->translator);

    // Create content.
463
    $langcode = LANGUAGE_NOT_SPECIFIED;
464 465
    $body = $this->randomName();
    $edit = array(
466 467
      'title' => $this->randomName(),
      'langcode' => 'en',
468 469 470 471 472 473 474 475
      "body[$langcode][0][value]" => $body,
    );

    // Translate the article in french.
    $this->drupalPost('node/add/article', $edit, t('Save'));
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->drupalGet("node/$node->nid/translate");
    $this->clickLink(t('add translation'));
476
    $this->assertFieldByXPath("//textarea[@name='body[$langcode][0][value]']", $body, t('The textfield widget is populated.'));
477 478
  }

479 480 481 482
  /**
   * Check that user that does not have access the field format cannot see the
   * source value when creating a translation.
   */
483
  function testTextFieldFormatted() {
484 485 486 487
    // Make node body multiple.
    $edit = array('field[cardinality]' => -1);
    $this->drupalPost('admin/structure/types/manage/article/fields/body', $edit, t('Save settings'));
    $this->drupalGet('node/add/article');
488
    $this->assertFieldByXPath("//input[@name='body_add_more']", t('Add another item'), t('Body field cardinality set to multiple.'));
489 490 491 492 493 494 495

    $body = array(
      $this->randomName(),
      $this->randomName(),
    );

    // Create an article with the first body input format set to "Full HTML".
496
    $title = $this->randomName();
497
    $edit = array(
498
      'title' => $title,
499
      'langcode' => 'en',
500 501 502 503 504
    );
    $this->drupalPost('node/add/article', $edit, t('Save'));

    // Populate the body field: the first item gets the "Full HTML" input
    // format, the second one "Filtered HTML".
505
    $formats = array('full_html', 'filtered_html');
506
    $langcode = LANGUAGE_NOT_SPECIFIED;
507 508 509
    foreach ($body as $delta => $value) {
      $edit = array(
        "body[$langcode][$delta][value]" => $value,
510
        "body[$langcode][$delta][format]" => array_shift($formats),
511 512
      );
      $this->drupalPost('node/1/edit', $edit, t('Save'));
513
      $this->assertText($body[$delta], t('The body field with delta @delta has been saved.', array('@delta' => $delta)));
514 515 516 517 518 519
    }

    // Login as translator.
    $this->drupalLogin($this->translator);

    // Translate the article in french.
520 521
    $node = $this->drupalGetNodeByTitle($title);
    $this->drupalGet("node/$node->nid/translate");
522
    $this->clickLink(t('add translation'));
523 524
    $this->assertNoText($body[0], t('The body field with delta @delta is hidden.', array('@delta' => 0)));
    $this->assertText($body[1], t('The body field with delta @delta is shown.', array('@delta' => 1)));
525 526
  }
}