Projet

Général

Profil

Paste
Télécharger (19,9 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Basic simpletests to test options on link module.
6
 */
7

    
8
/**
9
 * Attribute Crud Test.
10
 */
11
class LinkAttributeCrudTest extends DrupalWebTestCase {
12

    
13
  private $zebra;
14

    
15
  protected $permissions = array(
16
    'access content',
17
    'administer content types',
18
    'administer nodes',
19
    'administer filters',
20
    'access comments',
21
    'post comments',
22
    'skip comment approval',
23
    'access administration pages',
24
  );
25

    
26
  /**
27
   * Get Info.
28
   */
29
  public static function getInfo() {
30
    return array(
31
      'name' => 'Link Attribute Tests',
32
      'description' => 'Tests the field attributes, making sure they appear in various displayed situations.',
33
      'group' => 'Link',
34
    );
35
  }
36

    
37
  /**
38
   * Setup.
39
   */
40
  public function setup() {
41
    parent::setup('field_ui', 'link');
42
    $this->zebra = 0;
43
    // Create and login user.
44
    $this->web_user = $this->drupalCreateUser(array(
45
      'administer content types',
46
      'administer fields',
47
    ));
48
    $this->drupalLogin($this->web_user);
49
  }
50

    
51
  /**
52
   * Create Link.
53
   */
54
  protected function createLink($url, $title, $attributes = array()) {
55
    return array(
56
      'url' => $url,
57
      'title' => $title,
58
      'attributes' => $attributes,
59
    );
60
  }
61

    
62
  /**
63
   * Assert Link On Node.
64
   */
65
  protected function assertLinkOnNode($field_name, $link_value, $message = '', $group = 'Other') {
66
    $this->zebra++;
67
    $zebra_string = ($this->zebra % 2 == 0) ? 'even' : 'odd';
68
    $cssFieldLocator = 'field-' . str_replace('_', '-', $field_name);
69
    $this->assertPattern('@<div class="field field-type-link ' . $cssFieldLocator . '".*<div class="field-item ' . $zebra_string . '">\s*' . $link_value . '\s*</div>@is',
70
      $message,
71
      $group);
72
  }
73

    
74
  /**
75
   * Test Basic.
76
   *
77
   * A simple test that just creates a new node type, adds a link field to it,
78
   * creates a new node of that type, and makes sure that the node is being
79
   * displayed.
80
   */
81
  public function testBasic() {
82
    $content_type_friendly = $this->randomName(20);
83
    $content_type_machine = strtolower($this->randomName(10));
84
    $title = $this->randomName(20);
85

    
86
    $this->drupalGet('admin/structure/types');
87

    
88
    // Create the content type.
89
    $this->clickLink(t('Add content type'));
90

    
91
    $edit = array(
92
      'name' => $content_type_friendly,
93
      'type' => $content_type_machine,
94
    );
95
    $this->drupalPost(NULL, $edit, t('Save and add fields'));
96
    $this->assertText(t('The content type @name has been added.', array('@name' => $content_type_friendly)));
97

    
98
    // Now add a singleton field.
99
    $single_field_name_friendly = $this->randomName(20);
100
    $single_field_name_machine = strtolower($this->randomName(10));
101

    
102
    $edit = array(
103
      'fields[_add_new_field][label]' => $single_field_name_friendly,
104
      'fields[_add_new_field][field_name]' => $single_field_name_machine,
105
      'fields[_add_new_field][type]' => 'link_field',
106
      'fields[_add_new_field][widget_type]' => 'link_field',
107

    
108
    );
109
    $this->drupalPost(NULL, $edit, t('Save'));
110

    
111
    // We'll go with the default settings for this run-through.
112
    $this->drupalPost(NULL, array(), t('Save field settings'));
113

    
114
    // Using all the default settings, so press the button.
115
    $this->drupalPost(NULL, array(), t('Save settings'));
116
    $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
117

    
118
    // Somehow clicking "save" isn't enough, and we have to do a
119
    // node_types_rebuild().
120
    node_types_rebuild();
121
    menu_rebuild();
122
    $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
123
    $this->assertTrue($type_exists, 'The new content type has been created in the database.');
124

    
125
    $permission = 'create ' . $content_type_machine . ' content';
126

    
127
    // Reset the permissions cache.
128
    $this->checkPermissions(array($permission), TRUE);
129

    
130
    // Now that we have a new content type, create a user that has privileges
131
    // on the content type.
132
    $permissions = array_merge($this->permissions, array($permission));
133
    $this->web_user = $this->drupalCreateUser($permissions);
134
    $this->drupalLogin($this->web_user);
135

    
136
    // Go to page.
137
    $this->drupalGet('node/add/' . $content_type_machine);
138

    
139
    // Add a node.
140
    $edit = array(
141
      'title' => $title,
142
      'field_' . $single_field_name_machine . '[und][0][title]' => 'Link',
143
      'field_' . $single_field_name_machine . '[und][0][url]' => 'http://www.drupal.org/',
144
    );
145

    
146
    $this->drupalPost(NULL, $edit, t('Save'));
147
    $this->assertText(t('@content_type_friendly @title has been created', array(
148
      '@content_type_friendly' => $content_type_friendly,
149
      '@title' => $title,
150
    )));
151

    
152
    $this->drupalGet('node/add/' . $content_type_machine);
153

    
154
    // Create a node:
155
    $edit = array(
156
      'title' => $title,
157
      'field_' . $single_field_name_machine . '[und][0][url]' => 'http://www.example.com/',
158
      'field_' . $single_field_name_machine . '[und][0][title]' => 'Display',
159
    );
160

    
161
    // Now we can fill in the second item in the multivalue field and save.
162
    $this->drupalPost(NULL, $edit, t('Save'));
163
    $this->assertText(t('@content_type_friendly @title has been created', array(
164
      '@content_type_friendly' => $content_type_friendly,
165
      '@title' => $title,
166
    )));
167

    
168
    $this->assertText('Display');
169
    $this->assertLinkByHref('http://www.example.com');
170
  }
171

    
172
  /**
173
   * Create Simple Link Field.
174
   */
175
  protected function createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine) {
176
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/fields');
177
    $edit = array(
178
      'fields[_add_new_field][label]' => $single_field_name_friendly,
179
      'fields[_add_new_field][field_name]' => $single_field_name_machine,
180
      'fields[_add_new_field][type]' => 'link_field',
181
      'fields[_add_new_field][widget_type]' => 'link_field',
182
    );
183
    $this->drupalPost(NULL, $edit, t('Save'));
184

    
185
    // We'll go with the default settings for this run-through.
186
    $this->drupalPost(NULL, array(), t('Save field settings'));
187

    
188
    // Using all the default settings, so press the button.
189
    $this->drupalPost(NULL, array(), t('Save settings'));
190
    $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
191

    
192
    // Somehow clicking "save" isn't enough, and we have to do a
193
    // node_types_rebuild().
194
    node_types_rebuild();
195
    menu_rebuild();
196
    $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
197
    $this->assertTrue($type_exists, 'The new content type has been created in the database.');
198
  }
199

    
200
  /**
201
   * Create Node Type User.
202
   */
203
  protected function createNodeTypeUser($content_type_machine) {
204
    $permission = 'create ' . $content_type_machine . ' content';
205
    // Reset the permissions cache.
206
    $this->checkPermissions(array($permission), TRUE);
207

    
208
    // Now that we have a new content type, create a user that has privileges
209
    // on the content type.
210
    $permissions = array_merge($this->permissions, array($permission));
211
    $this->web_user = $this->drupalCreateUser($permissions);
212
    $this->drupalLogin($this->web_user);
213
  }
214

    
215
  /**
216
   * Create Node For Testing.
217
   */
218
  protected function createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $title, $url, $node_title = '') {
219
    $this->drupalGet('node/add/' . $content_type_machine);
220

    
221
    if (!$node_title) {
222
      $node_title = $this->randomName(20);
223
    }
224
    $edit = array(
225
      'title' => $node_title,
226
    );
227
    if ($url) {
228
      $edit['field_' . $single_field_name_machine . '[und][0][url]'] = $url;
229
    }
230
    if ($title) {
231
      $edit['field_' . $single_field_name_machine . '[und][0][title]'] = $title;
232
    }
233

    
234
    $this->drupalPost(NULL, $edit, t('Save'));
235
    $this->assertText(t('@content_type_friendly @title has been created', array(
236
      '@content_type_friendly' => $content_type_friendly,
237
      '@title' => $node_title,
238
    )));
239

    
240
  }
241

    
242
  /**
243
   * Test the link_plain formatter and it's output.
244
   */
245
  public function testFormatterPlain() {
246
    $content_type_friendly = $this->randomName(20);
247
    $content_type_machine = strtolower($this->randomName(10));
248

    
249
    $this->drupalCreateContentType(array(
250
      'type' => $content_type_machine,
251
      'name' => $content_type_friendly,
252
    ));
253

    
254
    // Now add a singleton field.
255
    $single_field_name_friendly = $this->randomName(20);
256
    $single_field_name_machine = strtolower($this->randomName(10));
257
    // $single_field_name = 'field_'. $single_field_name_machine;.
258
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
259

    
260
    // Okay, now we want to make sure this display is changed:
261
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
262
    $edit = array(
263
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
264
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_plain',
265
    );
266
    $this->drupalPost(NULL, $edit, t('Save'));
267

    
268
    $this->createNodeTypeUser($content_type_machine);
269

    
270
    $link_tests = array(
271
      'plain' => array(
272
        'text' => 'Display',
273
        'url' => 'http://www.example.com/',
274
      ),
275
      'query' => array(
276
        'text' => 'Display',
277
        'url' => 'http://www.example.com/?q=test',
278
      ),
279
      'fragment' => array(
280
        'text' => 'Display',
281
        'url' => 'http://www.example.com/#test',
282
      ),
283
    );
284

    
285
    foreach ($link_tests as $link_test) {
286
      $link_text = $link_test['text'];
287
      $link_url = $link_test['url'];
288
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
289

    
290
      $this->assertText($link_url);
291
      $this->assertNoText($link_text);
292
      $this->assertNoLinkByHref($link_url);
293
    }
294
  }
295

    
296
  /**
297
   * Formatter Host.
298
   */
299
  public function testFormatterHost() {
300
    $content_type_friendly = $this->randomName(20);
301
    $content_type_machine = strtolower($this->randomName(10));
302

    
303
    $this->drupalCreateContentType(array(
304
      'type' => $content_type_machine,
305
      'name' => $content_type_friendly,
306
    ));
307

    
308
    // Now add a singleton field.
309
    $single_field_name_friendly = $this->randomName(20);
310
    $single_field_name_machine = strtolower($this->randomName(10));
311
    // $single_field_name = 'field_'. $single_field_name_machine;.
312
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
313

    
314
    // Okay, now we want to make sure this display is changed:
315
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
316
    $edit = array(
317
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
318
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_host',
319
    );
320
    $this->drupalPost(NULL, $edit, t('Save'));
321

    
322
    $this->createNodeTypeUser($content_type_machine);
323

    
324
    $link_text = 'Display';
325
    $link_url = 'http://www.example.com/';
326
    $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
327

    
328
    $this->assertText('www.example.com');
329
    $this->assertNoText($link_text);
330
    $this->assertNoLinkByHref($link_url);
331
  }
332

    
333
  /**
334
   * Formatter URL.
335
   *
336
   * @codingStandardsIgnoreStart
337
   */
338
  public function testFormatterURL() {
339
    // @codingStandardsIgnoreEnd
340
    $content_type_friendly = $this->randomName(20);
341
    $content_type_machine = strtolower($this->randomName(10));
342

    
343
    $this->drupalCreateContentType(array(
344
      'type' => $content_type_machine,
345
      'name' => $content_type_friendly,
346
    ));
347

    
348
    // Now add a singleton field.
349
    $single_field_name_friendly = $this->randomName(20);
350
    $single_field_name_machine = strtolower($this->randomName(10));
351
    // $single_field_name = 'field_'. $single_field_name_machine;.
352
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
353

    
354
    // Okay, now we want to make sure this display is changed:
355
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
356
    $edit = array(
357
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
358
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_url',
359
    );
360
    $this->drupalPost(NULL, $edit, t('Save'));
361

    
362
    $this->createNodeTypeUser($content_type_machine);
363

    
364
    $link_tests = array(
365
      'plain' => array(
366
        'text' => 'Display',
367
        'url' => 'http://www.example.com/',
368
      ),
369
      'query' => array(
370
        'text' => 'Display',
371
        'url' => 'http://www.example.com/?q=test',
372
      ),
373
      'fragment' => array(
374
        'text' => 'Display',
375
        'url' => 'http://www.example.com/#test',
376
      ),
377
    );
378

    
379
    foreach ($link_tests as $link_test) {
380
      $link_text = $link_test['text'];
381
      $link_url = $link_test['url'];
382
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
383

    
384
      $this->assertNoText($link_text);
385
      $this->assertLinkByHref($link_url);
386
    }
387
  }
388

    
389
  /**
390
   * Formatter Short.
391
   */
392
  public function testFormatterShort() {
393
    $content_type_friendly = $this->randomName(20);
394
    $content_type_machine = strtolower($this->randomName(10));
395

    
396
    $this->drupalCreateContentType(array(
397
      'type' => $content_type_machine,
398
      'name' => $content_type_friendly,
399
    ));
400

    
401
    // Now add a singleton field.
402
    $single_field_name_friendly = $this->randomName(20);
403
    $single_field_name_machine = strtolower($this->randomName(10));
404
    // $single_field_name = 'field_'. $single_field_name_machine;.
405
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
406

    
407
    // Okay, now we want to make sure this display is changed:
408
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
409
    $edit = array(
410
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
411
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_short',
412
    );
413
    $this->drupalPost(NULL, $edit, t('Save'));
414

    
415
    $this->createNodeTypeUser($content_type_machine);
416

    
417
    $link_tests = array(
418
      'plain' => array(
419
        'text' => 'Display',
420
        'url' => 'http://www.example.com/',
421
      ),
422
      'query' => array(
423
        'text' => 'Display',
424
        'url' => 'http://www.example.com/?q=test',
425
      ),
426
      'fragment' => array(
427
        'text' => 'Display',
428
        'url' => 'http://www.example.com/#test',
429
      ),
430
    );
431

    
432
    foreach ($link_tests as $link_test) {
433
      $link_text = $link_test['text'];
434
      $link_url = $link_test['url'];
435
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
436

    
437
      $this->assertText('Link');
438
      $this->assertNoText($link_text);
439
      $this->assertLinkByHref($link_url);
440
    }
441
  }
442

    
443
  /**
444
   * Formatter Label.
445
   */
446
  public function testFormatterLabel() {
447
    $content_type_friendly = $this->randomName(20);
448
    $content_type_machine = strtolower($this->randomName(10));
449

    
450
    $this->drupalCreateContentType(array(
451
      'type' => $content_type_machine,
452
      'name' => $content_type_friendly,
453
    ));
454

    
455
    // Now add a singleton field.
456
    $single_field_name_friendly = $this->randomName(20);
457
    $single_field_name_machine = strtolower($this->randomName(10));
458
    // $single_field_name = 'field_'. $single_field_name_machine;.
459
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
460

    
461
    // Okay, now we want to make sure this display is changed:
462
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
463
    $edit = array(
464
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
465
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_label',
466
    );
467
    $this->drupalPost(NULL, $edit, t('Save'));
468

    
469
    $this->createNodeTypeUser($content_type_machine);
470

    
471
    $link_tests = array(
472
      'plain' => array(
473
        'text' => 'Display',
474
        'url' => 'http://www.example.com/',
475
      ),
476
      'query' => array(
477
        'text' => 'Display',
478
        'url' => 'http://www.example.com/?q=test',
479
      ),
480
      'fragment' => array(
481
        'text' => 'Display',
482
        'url' => 'http://www.example.com/#test',
483
      ),
484
    );
485

    
486
    foreach ($link_tests as $link_test) {
487
      $link_text = $link_test['text'];
488
      $link_url = $link_test['url'];
489
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
490

    
491
      $this->assertNoText($link_text);
492
      $this->assertText($single_field_name_friendly);
493
      $this->assertLinkByHref($link_url);
494
    }
495
  }
496

    
497
  /**
498
   * Formatter Separate.
499
   */
500
  public function testFormatterSeparate() {
501
    $content_type_friendly = $this->randomName(20);
502
    $content_type_machine = strtolower($this->randomName(10));
503

    
504
    $this->drupalCreateContentType(array(
505
      'type' => $content_type_machine,
506
      'name' => $content_type_friendly,
507
    ));
508

    
509
    // Now add a singleton field.
510
    $single_field_name_friendly = $this->randomName(20);
511
    $single_field_name_machine = strtolower($this->randomName(10));
512
    // $single_field_name = 'field_'. $single_field_name_machine;.
513
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
514

    
515
    // Okay, now we want to make sure this display is changed:
516
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
517
    $edit = array(
518
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
519
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_separate',
520
    );
521
    $this->drupalPost(NULL, $edit, t('Save'));
522

    
523
    $this->createNodeTypeUser($content_type_machine);
524

    
525
    $plain_url = 'http://www.example.com/';
526
    $link_tests = array(
527
      'plain' => array(
528
        'text' => $this->randomName(20),
529
        'url' => $plain_url,
530
      ),
531
      'query' => array(
532
        'text' => $this->randomName(20),
533
        'url' => $plain_url . '?q=test',
534
      ),
535
      'fragment' => array(
536
        'text' => $this->randomName(20),
537
        'url' => $plain_url . '#test',
538
      ),
539
    );
540

    
541
    foreach ($link_tests as $link_test) {
542
      $link_text = $link_test['text'];
543
      $link_url = $link_test['url'];
544
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
545

    
546
      $this->assertText($link_text);
547
      $this->assertLink($plain_url);
548
      $this->assertLinkByHref($link_url);
549
    }
550
  }
551

    
552
  /**
553
   * Formatter Plain Title.
554
   */
555
  public function testFormatterPlainTitle() {
556
    $content_type_friendly = $this->randomName(20);
557
    $content_type_machine = strtolower($this->randomName(10));
558

    
559
    $this->drupalCreateContentType(array(
560
      'type' => $content_type_machine,
561
      'name' => $content_type_friendly,
562
    ));
563

    
564
    // Now add a singleton field.
565
    $single_field_name_friendly = $this->randomName(20);
566
    $single_field_name_machine = strtolower($this->randomName(10));
567
    // $single_field_name = 'field_'. $single_field_name_machine;.
568
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
569

    
570
    // Okay, now we want to make sure this display is changed:
571
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
572
    $edit = array(
573
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
574
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_title_plain',
575
    );
576
    $this->drupalPost(NULL, $edit, t('Save'));
577

    
578
    $this->createNodeTypeUser($content_type_machine);
579

    
580
    $link_text = 'Display';
581
    $link_url = 'http://www.example.com/';
582
    $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
583

    
584
    $this->assertText($link_text);
585
    $this->assertNoText($link_url);
586
    $this->assertNoLinkByHref($link_url);
587
  }
588

    
589
}