Projet

Général

Profil

Paste
Télécharger (18,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / tests / link.crud_browser.test @ 39a181a4

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
  /**
29
   * Get Info.
30
   */
31
  public static function getInfo() {
32
    return array(
33
      'name' => 'Link CRUD - browser test',
34
      'description' => 'Tests the field CRUD (create, read, update, delete) API 2.',
35
      'group' => 'Link',
36
    );
37
  }
38

    
39
  /**
40
   * Setup.
41
   */
42
  public function setUp() {
43
    parent::setUp('field_ui', 'link');
44
  }
45

    
46
  /**
47
   * Creates a link field for the "page" type and creates a page with a link.
48
   */
49
  public function testLinkCreate() {
50
    // libxml_use_internal_errors(true);
51
    $this->web_user = $this->drupalCreateUser(array(
52
      'administer content types',
53
      'administer fields',
54
      'administer nodes',
55
      'administer filters',
56
      'access content',
57
      'create page content',
58
      'access administration pages',
59
    ));
60
    $this->drupalLogin($this->web_user);
61

    
62
    // Create field.
63
    $name = strtolower($this->randomName());
64
    $edit = array(
65
      'fields[_add_new_field][label]' => $name,
66
      'fields[_add_new_field][field_name]' => $name,
67
      'fields[_add_new_field][type]' => 'link_field',
68
      'fields[_add_new_field][widget_type]' => 'link_field',
69
    );
70
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
71
    $this->drupalPost(NULL, array(), t('Save field settings'));
72
    $this->drupalPost(NULL, array(), t('Save settings'));
73

    
74
    // Is field created?
75
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
76
    node_types_rebuild();
77
    menu_rebuild();
78

    
79
    $permission = 'create page content';
80
    $this->checkPermissions(array($permission), TRUE);
81

    
82
    // Create page form
83
    // $this->drupalGet('node/add');.
84
    $this->drupalGet('node/add/page');
85
    $field_name = 'field_' . $name;
86
    $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
87
    $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
88

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

    
142
    foreach ($input_test_cases as $input) {
143
      $this->drupalLogin($this->web_user);
144
      $this->drupalGet('node/add/page');
145

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

    
166
      // Change to Anonymous user.
167
      $this->drupalLogout();
168

    
169
      $this->drupalGet($url);
170
      // debug($this);
171
      // If simpletest starts using something to override the error system, this
172
      // will flag us and let us know it's broken.
173
      $this->assertFalse(libxml_use_internal_errors(TRUE));
174
      if (isset($input['expected_href'])) {
175
        $path = '//a[@href="' . $input['expected_href'] . '" and text()="' . $input['label'] . '"]';
176
      }
177
      else {
178
        $path = '//a[@href="' . $input['href'] . '" and text()="' . $input['label'] . '"]';
179
      }
180
      $elements = $this->xpath($path);
181
      libxml_use_internal_errors(FALSE);
182
      $this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
183
    }
184
    // libxml_use_internal_errors(FALSE);
185
  }
186

    
187
  /**
188
   * Static Link Create.
189
   *
190
   * Testing that if you use <strong> in a static title for your link, that the
191
   * title actually displays <strong>.
192
   */
193
  public function testStaticLinkCreate() {
194

    
195
    $this->web_user = $this->drupalCreateUser(array(
196
      'administer content types',
197
      'administer fields',
198
      'access content',
199
      'create page content',
200
    ));
201

    
202
    $this->drupalLogin($this->web_user);
203

    
204
    // Create field.
205
    $name = strtolower($this->randomName());
206
    $field_name = 'field_' . $name;
207
    $edit = array(
208
      'fields[_add_new_field][label]' => $name,
209
      'fields[_add_new_field][field_name]' => $name,
210
      'fields[_add_new_field][type]' => 'link_field',
211
      'fields[_add_new_field][widget_type]' => 'link_field',
212
    );
213
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
214
    $this->drupalPost(NULL, array(), t('Save field settings'));
215
    $this->drupalPost(NULL, array(
216
      'instance[settings][title]' => 'value',
217
      'instance[settings][title_value]' => '<strong>' . $name . '</strong>',
218
    ), t('Save settings'));
219

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

    
223
    // Create page form.
224
    $this->drupalGet('node/add/page');
225
    $this->assertField($field_name . '[und][0][url]', 'URL found');
226

    
227
    $input = array(
228
      'href' => 'http://example.com/' . $this->randomName(),
229
    );
230

    
231
    $edit = array(
232
      'title' => $name,
233
      $field_name . '[und][0][url]' => $input['href'],
234
    );
235
    $this->drupalPost(NULL, $edit, t('Save'));
236

    
237
    $url = $this->getUrl();
238

    
239
    // Change to anonymous user.
240
    $this->drupalLogout();
241
    $this->drupalGet($url);
242

    
243
    $this->assertRaw(l('<strong>' . $name . '</strong>', $input['href'], array('html' => TRUE)));
244
  }
245

    
246
  /**
247
   * CRUD Title Only Title No Link.
248
   *
249
   * Testing that if you have the title but no url, the title is not sanitized
250
   * twice.
251
   *
252
   * @codingStandardsIgnoreStart
253
   */
254
  public function testCRUDTitleOnlyTitleNoLink() {
255
    // @codingStandardsIgnoreEnd
256
    $this->web_user = $this->drupalCreateUser(array(
257
      'administer content types',
258
      'administer fields',
259
      'access content',
260
      'create page content',
261
    ));
262
    $this->drupalLogin($this->web_user);
263

    
264
    // Create field.
265
    $name = strtolower($this->randomName());
266
    $field_name = 'field_' . $name;
267
    $edit = array(
268
      'fields[_add_new_field][label]' => $name,
269
      'fields[_add_new_field][field_name]' => $name,
270
      'fields[_add_new_field][type]' => 'link_field',
271
      'fields[_add_new_field][widget_type]' => 'link_field',
272
    );
273
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
274
    $this->drupalPost(NULL, array(), t('Save field settings'));
275
    $this->drupalPost(NULL, array(
276
      'instance[settings][url]' => 1,
277
    ), t('Save settings'));
278

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

    
282
    // Create page form.
283
    $this->drupalGet('node/add/page');
284
    $this->assertField($field_name . '[und][0][url]', 'URL found');
285

    
286
    $input = array(
287
      'title' => 'This & That',
288
      'href' => '',
289
    );
290

    
291
    $edit = array(
292
      'title' => $name,
293
      $field_name . '[und][0][title]' => $input['title'],
294
      $field_name . '[und][0][url]' => $input['href'],
295
    );
296
    $this->drupalPost(NULL, $edit, t('Save'));
297

    
298
    $url = $this->getUrl();
299

    
300
    // Change to anonymous user.
301
    $this->drupalLogout();
302
    $this->drupalGet($url);
303

    
304
    $this->assertRaw('This &amp; That');
305
  }
306

    
307
  /**
308
   * CRUD Create Field Defaults.
309
   *
310
   * If we're creating a new field and just hit 'save' on the default options,
311
   * we want to make sure they are set to the expected results.
312
   *
313
   * @codingStandardsIgnoreStart
314
   */
315
  public function testCRUDCreateFieldDefaults() {
316
    // @codingStandardsIgnoreEnd
317

    
318
    $this->web_user = $this->drupalCreateUser(array(
319
      'administer content types',
320
      'administer fields',
321
      'access content',
322
      'create page content',
323
    ));
324

    
325
    $this->drupalLogin($this->web_user);
326

    
327
    // Create field.
328
    $name = strtolower($this->randomName());
329
    $edit = array(
330
      'fields[_add_new_field][label]' => $name,
331
      'fields[_add_new_field][field_name]' => $name,
332
      'fields[_add_new_field][type]' => 'link_field',
333
      'fields[_add_new_field][widget_type]' => 'link_field',
334
    );
335
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
336
    $this->drupalPost(NULL, array(), t('Save field settings'));
337
    $this->drupalPost(NULL, array(), t('Save settings'));
338

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

    
344
    _field_info_collate_fields(TRUE);
345
    $instances = field_info_instances('node', 'page');
346

    
347
    $instance = $instances['field_' . $name];
348
    $this->assertFalse($instance['required'], 'Make sure field is not required.');
349
    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
350
    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
351
    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
352
    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
353
    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
354
    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
355
    $this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.');
356
    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
357
  }
358

    
359
  /**
360
   * CRUD Create Field With Class.
361
   *
362
   * If we're creating a new field and just hit 'save' on the default options,
363
   * we want to make sure they are set to the expected results.
364
   *
365
   * @codingStandardsIgnoreStart
366
   */
367
  public function testCRUDCreateFieldWithClass() {
368
    // @codingStandardsIgnoreEnd
369
    $this->web_user = $this->drupalCreateUser(array(
370
      'administer content types',
371
      'administer fields',
372
      'access content',
373
      'create page content',
374
    ));
375
    $this->drupalLogin($this->web_user);
376

    
377
    // Create field.
378
    $name = strtolower($this->randomName());
379
    $edit = array(
380
      'fields[_add_new_field][label]' => $name,
381
      'fields[_add_new_field][field_name]' => $name,
382
      'fields[_add_new_field][type]' => 'link_field',
383
      'fields[_add_new_field][widget_type]' => 'link_field',
384
    );
385
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
386

    
387
    $this->drupalPost(NULL, array(), t('Save field settings'));
388
    $link_class_name = 'basic-link-' . strtolower($this->randomName());
389
    $edit = array(
390
      'instance[settings][attributes][class]' => $link_class_name,
391
    );
392
    $this->drupalPost(NULL, $edit, t('Save settings'));
393

    
394
    // Is field created?
395
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
396
    node_types_rebuild();
397
    menu_rebuild();
398

    
399
    _field_info_collate_fields(TRUE);
400
    $instances = field_info_instances('node', 'page');
401

    
402
    $instance = $instances['field_' . $name];
403
    $this->assertFalse($instance['required'], 'Make sure field is not required.');
404
    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
405
    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
406
    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
407
    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
408
    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
409
    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
410
    $this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'One class should be set.');
411
    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
412

    
413
    // Now, let's create a node with this field and make sure the link shows up:
414
    // create page form.
415
    $field_name = 'field_' . $name;
416
    $this->drupalGet('node/add/page');
417
    $this->assertField($field_name . '[und][0][url]', 'URL found');
418

    
419
    $input = array(
420
      'title' => 'This & That',
421
      'href' => 'http://www.example.com/',
422
    );
423

    
424
    $edit = array(
425
      'title' => $field_name,
426
      $field_name . '[und][0][title]' => $input['title'],
427
      $field_name . '[und][0][url]' => $input['href'],
428
    );
429
    $this->drupalPost(NULL, $edit, t('Save'));
430

    
431
    $url = $this->getUrl();
432

    
433
    // Change to anonymous user.
434
    $this->drupalLogout();
435
    $this->drupalGet($url);
436

    
437
    $this->assertRaw('This &amp; That');
438
    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Class $link_class_name exists on page.");
439
  }
440

    
441
  /**
442
   * CRUD Create Field With Two Classes.
443
   *
444
   * If we're creating a new field and just hit 'save' on the default options,
445
   * we want to make sure they are set to the expected results.
446
   *
447
   * @codingStandardsIgnoreStart
448
   */
449
  public function testCRUDCreateFieldWithTwoClasses() {
450
    // @codingStandardsIgnoreEnd
451
    $this->web_user = $this->drupalCreateUser(array(
452
      'administer content types',
453
      'administer fields',
454
      'access content',
455
      'create page content',
456
    ));
457
    $this->drupalLogin($this->web_user);
458

    
459
    // Create field.
460
    $name = strtolower($this->randomName());
461
    $edit = array(
462
      'fields[_add_new_field][label]' => $name,
463
      'fields[_add_new_field][field_name]' => $name,
464
      'fields[_add_new_field][type]' => 'link_field',
465
      'fields[_add_new_field][widget_type]' => 'link_field',
466
    );
467
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
468

    
469
    $this->drupalPost(NULL, array(), t('Save field settings'));
470
    $link_class_name = 'basic-link ' . strtoupper($this->randomName());
471
    $edit = array(
472
      'instance[settings][attributes][class]' => $link_class_name,
473
    );
474
    $this->drupalPost(NULL, $edit, t('Save settings'));
475

    
476
    // Is field created?
477
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
478
    node_types_rebuild();
479
    menu_rebuild();
480

    
481
    _field_info_collate_fields(TRUE);
482
    $instances = field_info_instances('node', 'page');
483

    
484
    $instance = $instances['field_' . $name];
485
    $this->assertFalse($instance['required'], 'Make sure field is not required.');
486
    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
487
    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
488
    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
489
    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
490
    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
491
    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
492
    $this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'Two classes should be set.');
493
    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
494

    
495
    // Now, let's create a node with this field and make sure the link shows up:
496
    // create page form.
497
    $field_name = 'field_' . $name;
498
    $this->drupalGet('node/add/page');
499
    $this->assertField($field_name . '[und][0][url]', 'URL found');
500

    
501
    $input = array(
502
      'title' => 'This & That',
503
      'href' => 'http://www.example.com/',
504
    );
505

    
506
    $edit = array(
507
      'title' => $field_name,
508
      $field_name . '[und][0][title]' => $input['title'],
509
      $field_name . '[und][0][url]' => $input['href'],
510
    );
511
    $this->drupalPost(NULL, $edit, t('Save'));
512

    
513
    $url = $this->getUrl();
514

    
515
    // Change to anonymous user.
516
    $this->drupalLogout();
517
    $this->drupalGet($url);
518

    
519
    $this->assertRaw('This &amp; That');
520
    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Classes $link_class_name exist on page.");
521
  }
522

    
523
}