Projet

Général

Profil

Paste
Télécharger (20,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / tests / LinkFieldCrudTest.test @ bad4e148

1
<?php
2

    
3
/**
4
 * @file
5
 * Testing that users can not input bad URLs or labels.
6
 */
7

    
8
/**
9
 * Testing that users can not input bad URLs or labels.
10
 */
11
class LinkFieldCrudTest extends LinkBaseTestClass {
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
   * {@inheritdoc}
41
   */
42
  public function setUp(array $modules = array()) {
43
    $modules[] = 'field_ui';
44
    $modules[] = 'link';
45
    parent::setUp($modules);
46
  }
47

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

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

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

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

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

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

    
150
    foreach ($input_test_cases as $input) {
151
      $this->drupalLogin($this->web_user);
152
      $this->drupalGet('node/add/page');
153

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

    
174
      // Change to Anonymous user.
175
      $this->drupalLogout();
176

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

    
193
  /**
194
   * Static Link Create.
195
   *
196
   * Testing that if you use <strong> in a static title for your link, that the
197
   * title actually displays <strong>.
198
   */
199
  public function testStaticLinkCreate() {
200
    $this->web_user = $this->drupalCreateUser(array(
201
      'administer content types',
202
      'administer fields',
203
      'access content',
204
      'create page content',
205
    ));
206

    
207
    $this->drupalLogin($this->web_user);
208

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

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

    
228
    // Create page form.
229
    $this->drupalGet('node/add/page');
230
    $this->assertField($field_name . '[und][0][url]', 'URL found');
231

    
232
    $input = array(
233
      'href' => 'http://example.com/' . $this->randomName(),
234
    );
235

    
236
    $edit = array(
237
      'title' => $name,
238
      $field_name . '[und][0][url]' => $input['href'],
239
    );
240
    $this->drupalPost(NULL, $edit, t('Save'));
241

    
242
    $url = $this->getUrl();
243

    
244
    // Change to anonymous user.
245
    $this->drupalLogout();
246
    $this->drupalGet($url);
247

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

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

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

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

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

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

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

    
300
    $url = $this->getUrl();
301

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

    
306
    $this->assertRaw('This &amp; That');
307
  }
308

    
309
  /**
310
   * CRUD Create Field Defaults.
311
   *
312
   * If we're creating a new field and just hit 'save' on the default options,
313
   * we want to make sure they are set to the expected results.
314
   */
315
  public function testCrudCreateFieldDefaults() {
316
    $this->web_user = $this->drupalCreateUser(array(
317
      'administer content types',
318
      'administer fields',
319
      'access content',
320
      'create page content',
321
    ));
322

    
323
    $this->drupalLogin($this->web_user);
324

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

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

    
342
    _field_info_collate_fields(TRUE);
343
    $instances = field_info_instances('node', 'page');
344

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

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

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

    
382
    $this->drupalPost(NULL, array(), t('Save field settings'));
383
    $link_class_name = 'basic-link-' . strtolower($this->randomName());
384
    $edit = array(
385
      'instance[settings][attributes][class]' => $link_class_name,
386
    );
387
    $this->drupalPost(NULL, $edit, t('Save settings'));
388

    
389
    // Is field created?
390
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
391
    node_types_rebuild();
392
    menu_rebuild();
393

    
394
    _field_info_collate_fields(TRUE);
395
    $instances = field_info_instances('node', 'page');
396

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

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

    
414
    $input = array(
415
      'title' => 'This & That',
416
      'href' => 'http://www.example.com/',
417
    );
418

    
419
    $edit = array(
420
      'title' => $field_name,
421
      $field_name . '[und][0][title]' => $input['title'],
422
      $field_name . '[und][0][url]' => $input['href'],
423
    );
424
    $this->drupalPost(NULL, $edit, t('Save'));
425

    
426
    $url = $this->getUrl();
427

    
428
    // Change to anonymous user.
429
    $this->drupalLogout();
430
    $this->drupalGet($url);
431

    
432
    $this->assertRaw('This &amp; That');
433
    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Class $link_class_name exists on page.");
434
  }
435

    
436
  /**
437
   * CRUD Create Field With Two Classes.
438
   *
439
   * If we're creating a new field and just hit 'save' on the default options,
440
   * we want to make sure they are set to the expected results.
441
   */
442
  public function testCrudCreateFieldWithTwoClasses() {
443
    $this->web_user = $this->drupalCreateUser(array(
444
      'administer content types',
445
      'administer fields',
446
      'access content',
447
      'create page content',
448
    ));
449
    $this->drupalLogin($this->web_user);
450

    
451
    // Create field.
452
    $name = strtolower($this->randomName());
453
    $edit = array(
454
      'fields[_add_new_field][label]' => $name,
455
      'fields[_add_new_field][field_name]' => $name,
456
      'fields[_add_new_field][type]' => 'link_field',
457
      'fields[_add_new_field][widget_type]' => 'link_field',
458
    );
459
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
460

    
461
    $this->drupalPost(NULL, array(), t('Save field settings'));
462
    $link_class_name = 'basic-link ' . strtoupper($this->randomName());
463
    $edit = array(
464
      'instance[settings][attributes][class]' => $link_class_name,
465
    );
466
    $this->drupalPost(NULL, $edit, t('Save settings'));
467

    
468
    // Is field created?
469
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
470
    node_types_rebuild();
471
    menu_rebuild();
472

    
473
    _field_info_collate_fields(TRUE);
474
    $instances = field_info_instances('node', 'page');
475

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

    
487
    // Now, let's create a node with this field and make sure the link shows up:
488
    // create page form.
489
    $field_name = 'field_' . $name;
490
    $this->drupalGet('node/add/page');
491
    $this->assertField($field_name . '[und][0][url]', 'URL found');
492

    
493
    $input = array(
494
      'title' => 'This & That',
495
      'href' => 'http://www.example.com/',
496
    );
497

    
498
    $edit = array(
499
      'title' => $field_name,
500
      $field_name . '[und][0][title]' => $input['title'],
501
      $field_name . '[und][0][url]' => $input['href'],
502
    );
503
    $this->drupalPost(NULL, $edit, t('Save'));
504

    
505
    $url = $this->getUrl();
506

    
507
    // Change to anonymous user.
508
    $this->drupalLogout();
509
    $this->drupalGet($url);
510

    
511
    $this->assertRaw('This &amp; That');
512
    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Classes $link_class_name exist on page.");
513
  }
514

    
515
  /**
516
   * Tests that link titles properly preserve the query params.
517
   */
518
  public function testLinkTitlePreservesQueryParams() {
519
    // Create field.
520
    $field_name = $this->createLinkField();
521

    
522
    // Create page form.
523
    $this->drupalGet('node/add/page');
524
    $this->assertField($field_name . '[und][0][title]', 'Title found');
525
    $this->assertField($field_name . '[und][0][url]', 'URL found');
526

    
527
    $link_title_tests = array(
528
      array(
529
        'href' => 'http://example.com/' . $this->randomName(),
530
      ),
531
      array(
532
        'href' => 'http://example.com/' . $this->randomName() . '?property=value',
533
      ),
534
      array(
535
        'href' => 'http://example.com/' . $this->randomName() . '#position',
536
      ),
537
      array(
538
        'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
539
      ),
540
      array(
541
        'href' => 'http://example.com/' . $this->randomName() . '?property=value2#lower',
542
      ),
543
    );
544

    
545
    foreach ($link_title_tests as &$input) {
546
      $this->drupalGet('node/add/page');
547
      $this->assertResponse(200);
548

    
549
      // Intentionally set empty title, to force using the url as the title.
550
      $edit = array(
551
        'title' => 'Some node title',
552
        $field_name . '[und][0][title]' => '',
553
        $field_name . '[und][0][url]' => $input['href'],
554
      );
555
      $this->drupalPost(NULL, $edit, t('Save'));
556
      // Confirm the node saved correctly.
557
      $this->assertResponse(200);
558
      $this->assertText($edit['title']);
559
      $input['url'] = $this->getUrl();
560
    }
561

    
562
    // Change to anonymous user.
563
    $this->drupalLogout();
564

    
565
    // Loop over the nodes, confirm each URL is rendered correctly.
566
    foreach ($link_title_tests as $input) {
567
      $this->drupalGet($input['url']);
568
      $this->assertRaw(l($input['href'], $input['href']), "Test that the link title has been set to " . $input['href']);
569
    }
570
  }
571

    
572
}