Projet

Général

Profil

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

root / drupal7 / sites / all / modules / link / tests / link.crud_browser.test @ 8e7483ab

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
   * {@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 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' => 'js 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 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' => 'js 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 url',
126
        'type' => self::LINK_INPUT_TYPE_BAD_URL,
127
      ),
128
      array(
129
        'href' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
130
        'label' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
131
        'msg' => 'Url with . in querystring',
132
        'type' => self::LINK_INPUT_TYPE_GOOD,
133
      ),
134
    );
135
    $test_case = array(
136
      'href' => 'www.example.com/' . $this->randomName(),
137
      'label' => $this->randomName(),
138
      'msg' => 'Link found',
139
      'type' => self::LINK_INPUT_TYPE_GOOD,
140
    );
141
    $test_case['expected_href'] = 'http://' . $test_case['href'];
142
    $input_test_cases[] = $test_case;
143

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

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

    
168
      // Change to Anonymous user.
169
      $this->drupalLogout();
170

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
296
    $url = $this->getUrl();
297

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

    
302
    $this->assertRaw('This &amp; That');
303
  }
304

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

    
319
    $this->drupalLogin($this->web_user);
320

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

    
333
    // Is field created?
334
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
335
    node_types_rebuild();
336
    menu_rebuild();
337

    
338
    _field_info_collate_fields(TRUE);
339
    $instances = field_info_instances('node', 'page');
340

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

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

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

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

    
385
    // Is field created?
386
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
387
    node_types_rebuild();
388
    menu_rebuild();
389

    
390
    _field_info_collate_fields(TRUE);
391
    $instances = field_info_instances('node', 'page');
392

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

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

    
410
    $input = array(
411
      'title' => 'This & That',
412
      'href' => 'http://www.example.com/',
413
    );
414

    
415
    $edit = array(
416
      'title' => $field_name,
417
      $field_name . '[und][0][title]' => $input['title'],
418
      $field_name . '[und][0][url]' => $input['href'],
419
    );
420
    $this->drupalPost(NULL, $edit, t('Save'));
421

    
422
    $url = $this->getUrl();
423

    
424
    // Change to anonymous user.
425
    $this->drupalLogout();
426
    $this->drupalGet($url);
427

    
428
    $this->assertRaw('This &amp; That');
429
    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Class $link_class_name exists on page.");
430
  }
431

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

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

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

    
464
    // Is field created?
465
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
466
    node_types_rebuild();
467
    menu_rebuild();
468

    
469
    _field_info_collate_fields(TRUE);
470
    $instances = field_info_instances('node', 'page');
471

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

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

    
489
    $input = array(
490
      'title' => 'This & That',
491
      'href' => 'http://www.example.com/',
492
    );
493

    
494
    $edit = array(
495
      'title' => $field_name,
496
      $field_name . '[und][0][title]' => $input['title'],
497
      $field_name . '[und][0][url]' => $input['href'],
498
    );
499
    $this->drupalPost(NULL, $edit, t('Save'));
500

    
501
    $url = $this->getUrl();
502

    
503
    // Change to anonymous user.
504
    $this->drupalLogout();
505
    $this->drupalGet($url);
506

    
507
    $this->assertRaw('This &amp; That');
508
    $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Classes $link_class_name exist on page.");
509
  }
510

    
511
}