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 @ 8e7483ab

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
   * {@inheritdoc}
39
   */
40
  public function setUp(array $modules = array()) {
41
    $modules[] = 'field_ui';
42
    $modules[] = 'link';
43
    parent::setUp($modules);
44

    
45
    $this->zebra = 0;
46

    
47
    // Create and login user.
48
    $this->web_user = $this->drupalCreateUser(array(
49
      'administer content types',
50
      'administer fields',
51
    ));
52
    $this->drupalLogin($this->web_user);
53
  }
54

    
55
  /**
56
   * Create Link.
57
   */
58
  protected function createLink($url, $title, $attributes = array()) {
59
    return array(
60
      'url' => $url,
61
      'title' => $title,
62
      'attributes' => $attributes,
63
    );
64
  }
65

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

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

    
90
    $this->drupalGet('admin/structure/types');
91

    
92
    // Create the content type.
93
    $this->clickLink(t('Add content type'));
94

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

    
102
    // Now add a singleton field.
103
    $single_field_name_friendly = $this->randomName(20);
104
    $single_field_name_machine = strtolower($this->randomName(10));
105

    
106
    $edit = array(
107
      'fields[_add_new_field][label]' => $single_field_name_friendly,
108
      'fields[_add_new_field][field_name]' => $single_field_name_machine,
109
      'fields[_add_new_field][type]' => 'link_field',
110
      'fields[_add_new_field][widget_type]' => 'link_field',
111

    
112
    );
113
    $this->drupalPost(NULL, $edit, t('Save'));
114

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

    
118
    // Using all the default settings, so press the button.
119
    $this->drupalPost(NULL, array(), t('Save settings'));
120
    $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
121

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

    
129
    $permission = 'create ' . $content_type_machine . ' content';
130

    
131
    // Reset the permissions cache.
132
    $this->checkPermissions(array($permission), TRUE);
133

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

    
140
    // Go to page.
141
    $this->drupalGet('node/add/' . $content_type_machine);
142

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

    
150
    $this->drupalPost(NULL, $edit, t('Save'));
151
    $this->assertText(t('@content_type_friendly @title has been created', array(
152
      '@content_type_friendly' => $content_type_friendly,
153
      '@title' => $title,
154
    )));
155

    
156
    $this->drupalGet('node/add/' . $content_type_machine);
157

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

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

    
172
    $this->assertText('Display');
173
    $this->assertLinkByHref('http://www.example.com');
174
  }
175

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

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

    
192
    // Using all the default settings, so press the button.
193
    $this->drupalPost(NULL, array(), t('Save settings'));
194
    $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
195

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

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

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

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

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

    
238
    $this->drupalPost(NULL, $edit, t('Save'));
239
    $this->assertText(t('@content_type_friendly @title has been created', array(
240
      '@content_type_friendly' => $content_type_friendly,
241
      '@title' => $node_title,
242
    )));
243
  }
244

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

    
252
    $this->drupalCreateContentType(array(
253
      'type' => $content_type_machine,
254
      'name' => $content_type_friendly,
255
    ));
256

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

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

    
271
    $this->createNodeTypeUser($content_type_machine);
272

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

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

    
293
      $this->assertText($link_url);
294
      $this->assertNoText($link_text);
295
      $this->assertNoLinkByHref($link_url);
296
    }
297
  }
298

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

    
306
    $this->drupalCreateContentType(array(
307
      'type' => $content_type_machine,
308
      'name' => $content_type_friendly,
309
    ));
310

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

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

    
325
    $this->createNodeTypeUser($content_type_machine);
326

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

    
331
    $this->assertText('www.example.com');
332
    $this->assertNoText($link_text);
333
    $this->assertNoLinkByHref($link_url);
334
  }
335

    
336
  /**
337
   * Formatter URL.
338
   */
339
  public function testFormatterUrl() {
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
}