text.test 12.8 KB
Newer Older
Dries's avatar
   
Dries committed
1
2
3
4
5
<?php
// $Id$

class TextFieldTestCase extends DrupalWebTestCase {
  protected $instance;
6
7
  protected $admin_user;
  protected $web_user;
Dries's avatar
   
Dries committed
8

9
  public static function getInfo() {
Dries's avatar
   
Dries committed
10
    return array(
11
12
13
      'name'  => 'Text Field',
      'description'  => "Test the creation of text fields.",
      'group' => 'Field'
Dries's avatar
   
Dries committed
14
15
16
17
    );
  }

  function setUp() {
18
    parent::setUp('field_test');
19

20
21
22
    $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
23
24
  }

25
26
27
28
29
30
31
32
  // Test fields.

  /**
   * Test text field validation.
   */
  function testTextFieldValidation() {
    // Create a field with settings to validate.
    $max_length = 3;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    $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'],
      'bundle' => FIELD_TEST_BUNDLE,
      'widget' => array(
        'type' => 'text_textfield',
      ),
      'display' => array(
        'full' => array(
          'type' => 'text_default',
        ),
      ),
    );
    field_create_instance($this->instance);
54
55
    // Test valid and invalid values with field_attach_validate().
    $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);
56
    $langcode = FIELD_LANGUAGE_NONE;
57
    for ($i = 0; $i <= $max_length + 2; $i++) {
58
      $entity->{$this->field['field_name']}[$langcode][0]['value'] = str_repeat('x', $i);
59
60
61
62
63
64
65
66
67
68
      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");
      }
    }
  }

69
70
71
72
73
74
75
  /**
   * Test widgets.
   */
  function testTextfieldWidgets() {
    $this->_testTextfieldWidgets('text', 'text_textfield');
    $this->_testTextfieldWidgets('text_long', 'text_textarea');
  }
Dries's avatar
   
Dries committed
76
77

  /**
78
   * Helper function for testTextfieldWidgets().
Dries's avatar
   
Dries committed
79
   */
80
81
82
  function _testTextfieldWidgets($field_type, $widget_type) {
    // Setup a field and instance
    $entity_type = 'test_entity';
83
    $this->field_name = drupal_strtolower($this->randomName());
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    $this->field = array('field_name' => $this->field_name, 'type' => $field_type);
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
      'bundle' => FIELD_TEST_BUNDLE,
      'label' => $this->randomName() . '_label',
      'settings' => array(
        'text_processing' => TRUE,
      ),
      'widget' => array(
        'type' => $widget_type,
      )
    );
    field_create_instance($this->instance);
98
    $langcode = FIELD_LANGUAGE_NONE;
Dries's avatar
   
Dries committed
99

100
101
    // Display creation form.
    $this->drupalGet('test-entity/add/test-bundle');
102
103
    $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'));
104
105
106
107

    // Submit with some value.
    $value = $this->randomName();
    $edit = array(
108
      "{$this->field_name}[$langcode][0][value]" => $value,
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    );
    $this->drupalPost(NULL, $edit, t('Save'));
    preg_match('|test-entity/(\d+)/edit|', $this->url, $match);
    $id = $match[1];
    $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));

    // Display the object.
    $entity = field_test_entity_load($id);
    $entity->content = field_attach_view($entity_type, $entity);
    $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
128
129
130
  }

  /**
131
   * Helper function for testTextfieldWidgetsFormatted().
Dries's avatar
   
Dries committed
132
   */
133
134
135
  function _testTextfieldWidgetsFormatted($field_type, $widget_type) {
    // Setup a field and instance
    $entity_type = 'test_entity';
136
    $this->field_name = drupal_strtolower($this->randomName());
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    $this->field = array('field_name' => $this->field_name, 'type' => $field_type);
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
      'bundle' => FIELD_TEST_BUNDLE,
      'label' => $this->randomName() . '_label',
      'settings' => array(
        'text_processing' => TRUE,
      ),
      'widget' => array(
        'type' => $widget_type,
      )
    );
    field_create_instance($this->instance);
151
    $langcode = FIELD_LANGUAGE_NONE;
152

153
154
155
156
157
158
159
160
161
162
163
    // Delete all text formats besides the plain text fallback format.
    $this->drupalLogin($this->admin_user);
    foreach (filter_formats() as $format) {
      if ($format->format != filter_fallback_format()) {
        $this->drupalPost('admin/config/content/formats/' . $format->format . '/delete', array(), t('Delete'));
      }
    }
    $this->drupalLogin($this->web_user);

    // Display the creation form. Since the user only has access to one format,
    // no format selector will be displayed.
164
    $this->drupalGet('test-entity/add/test-bundle');
165
    $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', t('Widget is displayed'));
166
    $this->assertNoFieldByName("{$this->field_name}[$langcode][0][value_format]", '', t('Format selector is not displayed'));
167
168

    // Submit with data that should be filtered.
169
    $value = '<em>' . $this->randomName() . '</em>';
170
    $edit = array(
171
      "{$this->field_name}[$langcode][0][value]" => $value,
172
173
174
175
176
177
178
179
180
181
    );
    $this->drupalPost(NULL, $edit, t('Save'));
    preg_match('|test-entity/(\d+)/edit|', $this->url, $match);
    $id = $match[1];
    $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));

    // Display the object.
    $entity = field_test_entity_load($id);
    $entity->content = field_attach_view($entity_type, $entity);
    $this->content = drupal_render($entity->content);
182
183
184
185
186
187
188
189
    $this->assertNoRaw($value, t('HTML tags are not displayed.'));
    $this->assertRaw(check_plain($value), t('Escaped HTML is displayed correctly.'));

    // Create a new text format that does not escape HTML, and grant the user
    // access to it.
    $this->drupalLogin($this->admin_user);
    $edit = array('name' => $this->randomName());
    $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
190
    filter_formats_reset();
191
192
193
194
    $this->checkPermissions(array(), TRUE);
    $format_id = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $edit['name']))->fetchField();
    $permission = filter_permission_name(filter_format_load($format_id));
    $rid = max(array_keys($this->web_user->roles));
195
    user_role_grant_permissions($rid, array($permission), TRUE);
196
    $this->drupalLogin($this->web_user);
197
198
199
200

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

204
    // Edit and change the text format to the new one that was created.
205
    $edit = array(
206
      "{$this->field_name}[$langcode][0][value_format]" => $format_id,
207
208
209
210
211
212
213
214
215
216
    );
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), t('Entity was updated'));

    // Display the object.
    $entity = field_test_entity_load($id);
    $entity->content = field_attach_view($entity_type, $entity);
    $this->content = drupal_render($entity->content);
    $this->assertRaw($value, t('Value is displayed unfiltered'));
  }
Dries's avatar
   
Dries committed
217
}
218
219
220
221

class TextSummaryTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
222
223
224
      'name' => 'Text summary',
      'description' => 'Test text_summary() with different strings and lengths.',
      'group' => 'Field',
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
    );
  }

  /**
   * 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>",
308
309
310
311
312
313
      "",
      "<p />",
      "<p />",
      "<p />",
      "<p />",
      "<p />",
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
      "<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++) {
      $this->callTextSummary($text, $expected[$i],    NULL, $i);
      $this->callTextSummary($text, $expected_lb[$i], 1,    $i);
      $this->callTextSummary($text, $expected_lb[$i], 2,    $i);
    }
  }

  /**
   * 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);
    $this->assertIdentical($summary, $expected, t('Generated summary "@summary" matches expected "@expected".', array('@summary' => $summary, '@expected' => $expected)));
  }
}