Projet

Général

Profil

Paste
Télécharger (17,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / tests / link.crud_browser.test @ 7fe061e8

1
<?php
2

    
3
/**
4
 * @file
5
 * Testing CRUD API in the browser.
6
 */
7

    
8
/**
9
 * Testing that users can not input bad URLs or labels
10
 */
11
class LinkUITest extends DrupalWebTestcase {
12

    
13
  /**
14
   * Link supposed to be good
15
   */
16
  const LINK_INPUT_TYPE_GOOD = 0;
17

    
18
  /**
19
   * Link supposed to have a bad title
20
   */
21
  const LINK_INPUT_TYPE_BAD_TITLE = 1;
22

    
23
  /**
24
   * Link supposed to have a bad URL
25
   */
26
  const LINK_INPUT_TYPE_BAD_URL = 2;
27

    
28
  public static function getInfo() {
29
    return array(
30
      'name' => 'Link CRUD - browser test',
31
      'description' => 'Tests the field CRUD (create, read, update, delete) API 2.',
32
      'group' => 'Link',
33
    );
34
  }
35

    
36
  function setUp() {
37
    parent::setUp('field_ui', 'link');
38
  }
39

    
40
  /**
41
   * Creates a link field for the "page" type and creates a page with a link.
42
   */
43
  function testLinkCreate() {
44
    //libxml_use_internal_errors(true);
45
    $this->web_user = $this->drupalCreateUser(array(
46
      'administer content types',
47
      'administer nodes',
48
      'administer filters',
49
      'access content',
50
      'create page content',
51
      'access administration pages'
52
    ));
53
    $this->drupalLogin($this->web_user);
54

    
55
    // create field
56
    $name = strtolower($this->randomName());
57
    $edit = array(
58
      'fields[_add_new_field][label]' => $name,
59
      'fields[_add_new_field][field_name]' => $name,
60
      'fields[_add_new_field][type]' => 'link_field',
61
      'fields[_add_new_field][widget_type]' => 'link_field',
62
    );
63
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
64
    $this->drupalPost(NULL, array(), t('Save field settings'));
65
    $this->drupalPost(NULL, array(), t('Save settings'));
66

    
67
    // Is field created?
68
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
69
    node_types_rebuild();
70
    menu_rebuild();
71

    
72
    $permission = 'create page content';
73
    $this->checkPermissions(array($permission), TRUE);
74

    
75
    // create page form
76
    //$this->drupalGet('node/add');
77
    $this->drupalGet('node/add/page');
78
    $field_name = 'field_' . $name;
79
    $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
80
    $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
81

    
82
    $input_test_cases = array(
83
      array(
84
        'href' => 'http://example.com/' . $this->randomName(),
85
        'label' => $this->randomName(),
86
        'msg' => 'Link found',
87
        'type' => self::LINK_INPUT_TYPE_GOOD
88
      ),
89
      array(
90
        'href' => 'http://example.com/' . $this->randomName(),
91
        'label' => $this->randomName() . '<script>alert("hi");</script>',
92
        'msg' => 'js label',
93
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
94
      ),
95
      array(
96
        'href' => 'http://example.com/' . $this->randomName(),
97
        'label' => $this->randomName() . '<script src="http://devil.site.com"></script>',
98
        'msg' => 'js label',
99
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
100
      ),
101
      array(
102
        'href' => 'http://example.com/' . $this->randomName(),
103
        'label' => $this->randomName() . '" onmouseover="alert(\'hi\')',
104
        'msg' => 'js label',
105
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
106
      ),
107
      array(
108
        'href' => 'http://example.com/' . $this->randomName(),
109
        'label' => $this->randomName() . '\' onmouseover="alert(\'hi\')',
110
        'msg' => 'js label',
111
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
112
      ),
113
      array(
114
        'href' => 'javascript:alert("http://example.com/' . $this->randomName() . '")',
115
        'label' => $this->randomName(),
116
        'msg' => 'js url',
117
        'type' => self::LINK_INPUT_TYPE_BAD_URL
118
      ),
119
      array(
120
        'href' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
121
        'label' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
122
        'msg' => 'Url with . in querystring',
123
        'type' => self::LINK_INPUT_TYPE_GOOD,
124
      ),
125
    );
126
    $test_case = array(
127
      'href' => 'www.example.com/' . $this->randomName(),
128
      'label' => $this->randomName(),
129
      'msg' => 'Link found',
130
      'type' => self::LINK_INPUT_TYPE_GOOD,
131
    );
132
    $test_case['expected_href'] = 'http://' . $test_case['href'];
133
    $input_test_cases[] = $test_case;
134

    
135
    foreach ($input_test_cases as $input) {
136
      $this->drupalLogin($this->web_user);
137
      $this->drupalGet('node/add/page');
138

    
139
      $edit = array(
140
        'title' => $input['label'],
141
        $field_name . '[und][0][title]' => $input['label'],
142
        $field_name . '[und][0][url]' => $input['href'],
143
      );
144
      $this->drupalPost(NULL, $edit, t('Save'));
145
      if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
146
        $this->assertRaw(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => trim($input['href']))), 'Not a valid URL: ' . $input['href']);
147
        continue;
148
      }
149
      else {
150
        $this->assertRaw(' ' . t('has been created.',
151
                           array('@type' => 'Basic Page', '%title' => $edit['title'])),
152
                         'Page created: ' . $input['href']);
153
      }
154
      $url = $this->getUrl();
155

    
156
      // change to Anonymous user.
157
      $this->drupalLogout();
158

    
159
      $this->drupalGet($url);
160
      //debug($this);
161
      // If simpletest starts using something to override the error system, this will flag
162
      // us and let us know it's broken.
163
      $this->assertFalse(libxml_use_internal_errors(TRUE));
164
      if (isset($input['expected_href'])) {
165
        $path = '//a[@href="' . $input['expected_href'] . '" and text()="' . $input['label'] . '"]';
166
      }
167
      else {
168
        $path = '//a[@href="' . $input['href'] . '" and text()="' . $input['label'] . '"]';
169
      }
170
      $elements = $this->xpath($path);
171
      libxml_use_internal_errors(FALSE);
172
      $this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
173
    }
174
    //libxml_use_internal_errors(FALSE);
175
  }
176

    
177
  /**
178
   * Testing that if you use <strong> in a static title for your link, that the
179
   * title actually displays <strong>.
180
   */
181
  function testStaticLinkCreate() {
182
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
183
    $this->drupalLogin($this->web_user);
184

    
185
    // create field
186
    $name = strtolower($this->randomName());
187
    $field_name = 'field_' . $name;
188
    $edit = array(
189
      'fields[_add_new_field][label]' => $name,
190
      'fields[_add_new_field][field_name]' => $name,
191
      'fields[_add_new_field][type]' => 'link_field',
192
      'fields[_add_new_field][widget_type]' => 'link_field',
193
    );
194
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
195
    $this->drupalPost(NULL, array(), t('Save field settings'));
196
    $this->drupalPost(NULL, array(
197
      'instance[settings][title]' => 'value',
198
      'instance[settings][title_value]' => '<strong>' . $name . '</strong>'), t('Save settings'));
199

    
200
    // Is field created?
201
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
202

    
203
    // create page form
204
    $this->drupalGet('node/add/page');
205
    $this->assertField($field_name . '[und][0][url]', 'URL found');
206

    
207
    $input = array(
208
      'href' => 'http://example.com/' . $this->randomName()
209
    );
210

    
211
    $edit = array(
212
      'title' => $name,
213
      $field_name . '[und][0][url]' => $input['href'],
214
    );
215
    $this->drupalPost(NULL, $edit, t('Save'));
216

    
217
    $url = $this->getUrl();
218

    
219
    // change to anonymous user
220
    $this->drupalLogout();
221
    $this->drupalGet($url);
222

    
223
    $this->assertRaw(l('<strong>' . $name . '</strong>', $input['href'], array('html' => TRUE)));
224
  }
225
  
226
  /**
227
   * Testing that if you have the title but no url, the title is not sanitized twice.
228
   */
229
  function testCRUDTitleOnlyTitleNoLink() {
230
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
231
    $this->drupalLogin($this->web_user);
232

    
233
    // create field
234
    $name = strtolower($this->randomName());
235
    $field_name = 'field_' . $name;
236
    $edit = array(
237
      'fields[_add_new_field][label]' => $name,
238
      'fields[_add_new_field][field_name]' => $name,
239
      'fields[_add_new_field][type]' => 'link_field',
240
      'fields[_add_new_field][widget_type]' => 'link_field',
241
    );
242
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
243
    $this->drupalPost(NULL, array(), t('Save field settings'));
244
    $this->drupalPost(NULL, array(
245
      'instance[settings][url]' => 1,
246
    ), t('Save settings'));
247

    
248
    // Is field created?
249
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
250
    
251
    // create page form
252
    $this->drupalGet('node/add/page');
253
    $this->assertField($field_name . '[und][0][url]', 'URL found');
254

    
255
    $input = array(
256
      'title' => 'This & That',
257
      'href' => '',
258
    );
259

    
260
    $edit = array(
261
      'title' => $name,
262
      $field_name . '[und][0][title]' => $input['title'],
263
      $field_name . '[und][0][url]' => $input['href'],
264
    );
265
    $this->drupalPost(NULL, $edit, t('Save'));
266

    
267
    $url = $this->getUrl();
268
    
269
    // change to anonymous user
270
    $this->drupalLogout();
271
    $this->drupalGet($url);
272

    
273
    $this->assertRaw('This &amp; That');
274
  }
275

    
276
  /**
277
   * If we're creating a new field and just hit 'save' on the default options, we want to make
278
   * sure they are set to the expected results.
279
   */
280
  function testCRUDCreateFieldDefaults() {
281
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
282
    $this->drupalLogin($this->web_user);
283

    
284
    // create field
285
    $name = strtolower($this->randomName());
286
    $edit = array(
287
      'fields[_add_new_field][label]' => $name,
288
      'fields[_add_new_field][field_name]' => $name,
289
      'fields[_add_new_field][type]' => 'link_field',
290
      'fields[_add_new_field][widget_type]' => 'link_field',
291
    );
292
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
293
    $this->drupalPost(NULL, array(), t('Save field settings'));
294
    $this->drupalPost(NULL, array(), t('Save settings'));
295

    
296
    // Is field created?
297
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
298
    node_types_rebuild();
299
    menu_rebuild();
300

    
301
    _field_info_collate_fields(TRUE);
302
    $instances = field_info_instances('node', 'page');
303

    
304
    $instance = $instances['field_' . $name];
305
    $this->assertFalse($instance['required'], 'Make sure field is not required.');
306
    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
307
    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
308
    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
309
    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
310
    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
311
    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
312
    $this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.');
313
    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
314
  }
315
  
316
  /**
317
   * If we're creating a new field and just hit 'save' on the default options, we want to make
318
   * sure they are set to the expected results.
319
   */
320
  function testCRUDCreateFieldWithClass() {
321
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
322
    $this->drupalLogin($this->web_user);
323

    
324
    // create field
325
    $name = strtolower($this->randomName());
326
    $edit = array(
327
      'fields[_add_new_field][label]' => $name,
328
      'fields[_add_new_field][field_name]' => $name,
329
      'fields[_add_new_field][type]' => 'link_field',
330
      'fields[_add_new_field][widget_type]' => 'link_field',
331
    );
332
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
333

    
334
    $this->drupalPost(NULL, array(), t('Save field settings'));
335
    $link_class_name = 'basic-link-' . strtolower($this->randomName());
336
    $edit = array(
337
      'instance[settings][attributes][class]' => $link_class_name,
338
    );
339
    $this->drupalPost(NULL, $edit, t('Save settings'));
340

    
341
    // Is field created?
342
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
343
    node_types_rebuild();
344
    menu_rebuild();
345

    
346
    _field_info_collate_fields(TRUE);
347
    $instances = field_info_instances('node', 'page');
348

    
349
    $instance = $instances['field_' . $name];
350
    $this->assertFalse($instance['required'], 'Make sure field is not required.');
351
    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
352
    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
353
    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
354
    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
355
    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
356
    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
357
    $this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'One class should be set.');
358
    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
359
    
360
    // Now, let's create a node with this field and make sure the link shows up:
361
    // create page form
362
    $field_name = 'field_' . $name;
363
    $this->drupalGet('node/add/page');
364
    $this->assertField($field_name . '[und][0][url]', 'URL found');
365

    
366
    $input = array(
367
      'title' => 'This & That',
368
      'href' => 'http://www.example.com/',
369
    );
370

    
371
    $edit = array(
372
      'title' => $field_name,
373
      $field_name . '[und][0][title]' => $input['title'],
374
      $field_name . '[und][0][url]' => $input['href'],
375
    );
376
    $this->drupalPost(NULL, $edit, t('Save'));
377

    
378
    $url = $this->getUrl();
379
    
380
    // change to anonymous user
381
    $this->drupalLogout();
382
    $this->drupalGet($url);
383

    
384
    $this->assertRaw('This &amp; That');
385
    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Class $link_class_name exists on page.");
386
  }
387

    
388
/**
389
   * If we're creating a new field and just hit 'save' on the default options, we want to make
390
   * sure they are set to the expected results.
391
   */
392
  function testCRUDCreateFieldWithTwoClasses() {
393
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
394
    $this->drupalLogin($this->web_user);
395

    
396
    // create field
397
    $name = strtolower($this->randomName());
398
    $edit = array(
399
      'fields[_add_new_field][label]' => $name,
400
      'fields[_add_new_field][field_name]' => $name,
401
      'fields[_add_new_field][type]' => 'link_field',
402
      'fields[_add_new_field][widget_type]' => 'link_field',
403
    );
404
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
405

    
406
    $this->drupalPost(NULL, array(), t('Save field settings'));
407
    $link_class_name = 'basic-link ' . strtoupper($this->randomName());
408
    $edit = array(
409
      'instance[settings][attributes][class]' => $link_class_name,
410
    );
411
    $this->drupalPost(NULL, $edit, t('Save settings'));
412

    
413
    // Is field created?
414
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
415
    node_types_rebuild();
416
    menu_rebuild();
417

    
418
    _field_info_collate_fields(TRUE);
419
    $instances = field_info_instances('node', 'page');
420

    
421
    $instance = $instances['field_' . $name];
422
    $this->assertFalse($instance['required'], 'Make sure field is not required.');
423
    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
424
    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
425
    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
426
    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
427
    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
428
    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
429
    $this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'Two classes should be set.');
430
    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
431
    
432
    // Now, let's create a node with this field and make sure the link shows up:
433
    // create page form
434
    $field_name = 'field_' . $name;
435
    $this->drupalGet('node/add/page');
436
    $this->assertField($field_name . '[und][0][url]', 'URL found');
437

    
438
    $input = array(
439
      'title' => 'This & That',
440
      'href' => 'http://www.example.com/',
441
    );
442

    
443
    $edit = array(
444
      'title' => $field_name,
445
      $field_name . '[und][0][title]' => $input['title'],
446
      $field_name . '[und][0][url]' => $input['href'],
447
    );
448
    $this->drupalPost(NULL, $edit, t('Save'));
449

    
450
    $url = $this->getUrl();
451
    
452
    // change to anonymous user
453
    $this->drupalLogout();
454
    $this->drupalGet($url);
455

    
456
    $this->assertRaw('This &amp; That');
457
    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Classes $link_class_name exist on page.");
458
  }
459
}