Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Field attributes test.
6
 */
7

    
8
/**
9
 * Field attributes test.
10
 */
11
class LinkFieldAttributesTest extends LinkBaseTestClass {
12

    
13
  /**
14
   * Track a zebra value, used to alternate the output.
15
   *
16
   * @var int
17
   */
18
  private $zebra;
19

    
20
  /**
21
   * Get Info.
22
   */
23
  public static function getInfo() {
24
    return array(
25
      'name' => 'Link field attribute Tests',
26
      'description' => 'Tests the field attributes, making sure they appear in various displayed situations.',
27
      'group' => 'Link',
28
    );
29
  }
30

    
31
  /**
32
   * {@inheritdoc}
33
   */
34
  public function setUp(array $modules = array()) {
35
    parent::setUp($modules);
36

    
37
    $this->zebra = 0;
38
  }
39

    
40
  /**
41
   * Create Link.
42
   */
43
  protected function createLink($url, $title, $attributes = array()) {
44
    return array(
45
      'url' => $url,
46
      'title' => $title,
47
      'attributes' => $attributes,
48
    );
49
  }
50

    
51
  /**
52
   * Assert Link On Node.
53
   */
54
  protected function assertLinkOnNode($field_name, $link_value, $message = '', $group = 'Other') {
55
    $this->zebra++;
56
    $zebra_string = ($this->zebra % 2 == 0) ? 'even' : 'odd';
57
    $cssFieldLocator = 'field-' . str_replace('_', '-', $field_name);
58
    $this->assertPattern('@<div class="field field-type-link ' . $cssFieldLocator . '".*<div class="field-item ' . $zebra_string . '">\s*' . $link_value . '\s*</div>@is',
59
      $message,
60
      $group);
61
  }
62

    
63
  /**
64
   * Test Basic.
65
   *
66
   * A simple test that just creates a new node type, adds a link field to it,
67
   * creates a new node of that type, and makes sure that the node is being
68
   * displayed.
69
   */
70
  public function testBasic() {
71
    $content_type_friendly = $this->randomName(20);
72
    $content_type_machine = strtolower($this->randomName(10));
73
    $title = $this->randomName(20);
74

    
75
    $this->drupalGet('admin/structure/types');
76

    
77
    // Create the content type.
78
    $this->clickLink(t('Add content type'));
79

    
80
    $edit = array(
81
      'name' => $content_type_friendly,
82
      'type' => $content_type_machine,
83
    );
84
    $this->drupalPost(NULL, $edit, t('Save and add fields'));
85
    $this->assertText(t('The content type @name has been added.', array('@name' => $content_type_friendly)));
86

    
87
    // Now add a singleton field.
88
    $single_field_name_friendly = $this->randomName(20);
89
    $single_field_name_machine = strtolower($this->randomName(10));
90

    
91
    $edit = array(
92
      'fields[_add_new_field][label]' => $single_field_name_friendly,
93
      'fields[_add_new_field][field_name]' => $single_field_name_machine,
94
      'fields[_add_new_field][type]' => 'link_field',
95
      'fields[_add_new_field][widget_type]' => 'link_field',
96

    
97
    );
98
    $this->drupalPost(NULL, $edit, t('Save'));
99

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

    
103
    // Using all the default settings, so press the button.
104
    $this->drupalPost(NULL, array(), t('Save settings'));
105
    $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
106

    
107
    // Somehow clicking "save" isn't enough, and we have to do a
108
    // node_types_rebuild().
109
    node_types_rebuild();
110
    menu_rebuild();
111
    $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
112
    $this->assertTrue($type_exists, 'The new content type has been created in the database.');
113

    
114
    $permission = 'create ' . $content_type_machine . ' content';
115

    
116
    // Reset the permissions cache.
117
    $this->checkPermissions(array($permission), TRUE);
118

    
119
    // Now that we have a new content type, create a user that has privileges
120
    // on the content type.
121
    $permissions = array_merge($this->permissions, array($permission));
122
    $this->web_user = $this->drupalCreateUser($permissions);
123
    $this->drupalLogin($this->web_user);
124

    
125
    // Go to page.
126
    $this->drupalGet('node/add/' . $content_type_machine);
127

    
128
    // Add a node.
129
    $edit = array(
130
      'title' => $title,
131
      'field_' . $single_field_name_machine . '[und][0][title]' => 'Link',
132
      'field_' . $single_field_name_machine . '[und][0][url]' => 'http://www.drupal.org/',
133
    );
134

    
135
    $this->drupalPost(NULL, $edit, t('Save'));
136
    $this->assertText(t('@content_type_friendly @title has been created', array(
137
      '@content_type_friendly' => $content_type_friendly,
138
      '@title' => $title,
139
    )));
140

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

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

    
150
    // Now we can fill in the second item in the multivalue field and save.
151
    $this->drupalPost(NULL, $edit, t('Save'));
152
    $this->assertText(t('@content_type_friendly @title has been created', array(
153
      '@content_type_friendly' => $content_type_friendly,
154
      '@title' => $title,
155
    )));
156

    
157
    $this->assertText('Display');
158
    $this->assertLinkByHref('http://www.example.com');
159
  }
160

    
161
  /**
162
   * Create Simple Link Field.
163
   */
164
  protected function createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine) {
165
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/fields');
166
    $edit = array(
167
      'fields[_add_new_field][label]' => $single_field_name_friendly,
168
      'fields[_add_new_field][field_name]' => $single_field_name_machine,
169
      'fields[_add_new_field][type]' => 'link_field',
170
      'fields[_add_new_field][widget_type]' => 'link_field',
171
    );
172
    $this->drupalPost(NULL, $edit, t('Save'));
173

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

    
177
    // Using all the default settings, so press the button.
178
    $this->drupalPost(NULL, array(), t('Save settings'));
179
    $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
180

    
181
    // Somehow clicking "save" isn't enough, and we have to do a
182
    // node_types_rebuild().
183
    node_types_rebuild();
184
    menu_rebuild();
185
    $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
186
    $this->assertTrue($type_exists, 'The new content type has been created in the database.');
187
  }
188

    
189
  /**
190
   * Create Node Type User.
191
   */
192
  protected function createNodeTypeUser($content_type_machine) {
193
    $permission = 'create ' . $content_type_machine . ' content';
194
    // Reset the permissions cache.
195
    $this->checkPermissions(array($permission), TRUE);
196

    
197
    // Now that we have a new content type, create a user that has privileges
198
    // on the content type.
199
    $permissions = array_merge($this->permissions, array($permission));
200
    $this->web_user = $this->drupalCreateUser($permissions);
201
    $this->drupalLogin($this->web_user);
202
  }
203

    
204
  /**
205
   * Create Node For Testing.
206
   */
207
  protected function createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $title, $url, $node_title = '') {
208
    $this->drupalGet('node/add/' . $content_type_machine);
209

    
210
    if (!$node_title) {
211
      $node_title = $this->randomName(20);
212
    }
213
    $edit = array(
214
      'title' => $node_title,
215
    );
216
    if ($url) {
217
      $edit['field_' . $single_field_name_machine . '[und][0][url]'] = $url;
218
    }
219
    if ($title) {
220
      $edit['field_' . $single_field_name_machine . '[und][0][title]'] = $title;
221
    }
222

    
223
    $this->drupalPost(NULL, $edit, t('Save'));
224
    $this->assertText(t('@content_type_friendly @title has been created', array(
225
      '@content_type_friendly' => $content_type_friendly,
226
      '@title' => $node_title,
227
    )));
228
  }
229

    
230
  /**
231
   * Test the link_plain formatter and it's output.
232
   */
233
  public function testFormatterPlain() {
234
    $content_type_friendly = $this->randomName(20);
235
    $content_type_machine = strtolower($this->randomName(10));
236

    
237
    $this->drupalCreateContentType(array(
238
      'type' => $content_type_machine,
239
      'name' => $content_type_friendly,
240
    ));
241

    
242
    // Now add a singleton field.
243
    $single_field_name_friendly = $this->randomName(20);
244
    $single_field_name_machine = strtolower($this->randomName(10));
245
    // $single_field_name = 'field_'. $single_field_name_machine;.
246
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
247

    
248
    // Okay, now we want to make sure this display is changed:
249
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
250
    $edit = array(
251
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
252
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_plain',
253
    );
254
    $this->drupalPost(NULL, $edit, t('Save'));
255

    
256
    $this->createNodeTypeUser($content_type_machine);
257

    
258
    $link_tests = array(
259
      'plain' => array(
260
        'text' => 'Display',
261
        'url' => 'http://www.example.com/',
262
      ),
263
      'query' => array(
264
        'text' => 'Display',
265
        'url' => 'http://www.example.com/?q=test',
266
      ),
267
      'fragment' => array(
268
        'text' => 'Display',
269
        'url' => 'http://www.example.com/#test',
270
      ),
271
    );
272

    
273
    foreach ($link_tests as $link_test) {
274
      $link_text = $link_test['text'];
275
      $link_url = $link_test['url'];
276
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
277

    
278
      $this->assertText($link_url);
279
      $this->assertNoText($link_text);
280
      $this->assertNoLinkByHref($link_url);
281
    }
282
  }
283

    
284
  /**
285
   * Formatter Host.
286
   */
287
  public function testFormatterHost() {
288
    $content_type_friendly = $this->randomName(20);
289
    $content_type_machine = strtolower($this->randomName(10));
290

    
291
    $this->drupalCreateContentType(array(
292
      'type' => $content_type_machine,
293
      'name' => $content_type_friendly,
294
    ));
295

    
296
    // Now add a singleton field.
297
    $single_field_name_friendly = $this->randomName(20);
298
    $single_field_name_machine = strtolower($this->randomName(10));
299
    // $single_field_name = 'field_'. $single_field_name_machine;.
300
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
301

    
302
    // Okay, now we want to make sure this display is changed:
303
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
304
    $edit = array(
305
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
306
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_host',
307
    );
308
    $this->drupalPost(NULL, $edit, t('Save'));
309

    
310
    $this->createNodeTypeUser($content_type_machine);
311

    
312
    $link_text = 'Display';
313
    $link_url = 'http://www.example.com/';
314
    $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
315

    
316
    $this->assertText('www.example.com');
317
    $this->assertNoText($link_text);
318
    $this->assertNoLinkByHref($link_url);
319
  }
320

    
321
  /**
322
   * Formatter URL.
323
   */
324
  public function testFormatterUrl() {
325
    $content_type_friendly = $this->randomName(20);
326
    $content_type_machine = strtolower($this->randomName(10));
327

    
328
    $this->drupalCreateContentType(array(
329
      'type' => $content_type_machine,
330
      'name' => $content_type_friendly,
331
    ));
332

    
333
    // Now add a singleton field.
334
    $single_field_name_friendly = $this->randomName(20);
335
    $single_field_name_machine = strtolower($this->randomName(10));
336
    // $single_field_name = 'field_'. $single_field_name_machine;.
337
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
338

    
339
    // Okay, now we want to make sure this display is changed:
340
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
341
    $edit = array(
342
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
343
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_url',
344
    );
345
    $this->drupalPost(NULL, $edit, t('Save'));
346

    
347
    $this->createNodeTypeUser($content_type_machine);
348

    
349
    $link_tests = array(
350
      'plain' => array(
351
        'text' => 'Display',
352
        'url' => 'http://www.example.com/',
353
      ),
354
      'query' => array(
355
        'text' => 'Display',
356
        'url' => 'http://www.example.com/?q=test',
357
      ),
358
      'fragment' => array(
359
        'text' => 'Display',
360
        'url' => 'http://www.example.com/#test',
361
      ),
362
    );
363

    
364
    foreach ($link_tests as $link_test) {
365
      $link_text = $link_test['text'];
366
      $link_url = $link_test['url'];
367
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
368

    
369
      $this->assertNoText($link_text);
370
      $this->assertLinkByHref($link_url);
371
    }
372
  }
373

    
374
  /**
375
   * Formatter Short.
376
   */
377
  public function testFormatterShort() {
378
    $content_type_friendly = $this->randomName(20);
379
    $content_type_machine = strtolower($this->randomName(10));
380

    
381
    $this->drupalCreateContentType(array(
382
      'type' => $content_type_machine,
383
      'name' => $content_type_friendly,
384
    ));
385

    
386
    // Now add a singleton field.
387
    $single_field_name_friendly = $this->randomName(20);
388
    $single_field_name_machine = strtolower($this->randomName(10));
389
    // $single_field_name = 'field_'. $single_field_name_machine;.
390
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
391

    
392
    // Okay, now we want to make sure this display is changed:
393
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
394
    $edit = array(
395
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
396
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_short',
397
    );
398
    $this->drupalPost(NULL, $edit, t('Save'));
399

    
400
    $this->createNodeTypeUser($content_type_machine);
401

    
402
    $link_tests = array(
403
      'plain' => array(
404
        'text' => 'Display',
405
        'url' => 'http://www.example.com/',
406
      ),
407
      'query' => array(
408
        'text' => 'Display',
409
        'url' => 'http://www.example.com/?q=test',
410
      ),
411
      'fragment' => array(
412
        'text' => 'Display',
413
        'url' => 'http://www.example.com/#test',
414
      ),
415
    );
416

    
417
    foreach ($link_tests as $link_test) {
418
      $link_text = $link_test['text'];
419
      $link_url = $link_test['url'];
420
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
421

    
422
      $this->assertText('Link');
423
      $this->assertNoText($link_text);
424
      $this->assertLinkByHref($link_url);
425
    }
426
  }
427

    
428
  /**
429
   * Formatter Label.
430
   */
431
  public function testFormatterLabel() {
432
    $content_type_friendly = $this->randomName(20);
433
    $content_type_machine = strtolower($this->randomName(10));
434

    
435
    $this->drupalCreateContentType(array(
436
      'type' => $content_type_machine,
437
      'name' => $content_type_friendly,
438
    ));
439

    
440
    // Now add a singleton field.
441
    $single_field_name_friendly = $this->randomName(20);
442
    $single_field_name_machine = strtolower($this->randomName(10));
443
    // $single_field_name = 'field_'. $single_field_name_machine;.
444
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
445

    
446
    // Okay, now we want to make sure this display is changed:
447
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
448
    $edit = array(
449
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
450
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_label',
451
    );
452
    $this->drupalPost(NULL, $edit, t('Save'));
453

    
454
    $this->createNodeTypeUser($content_type_machine);
455

    
456
    $link_tests = array(
457
      'plain' => array(
458
        'text' => 'Display',
459
        'url' => 'http://www.example.com/',
460
      ),
461
      'query' => array(
462
        'text' => 'Display',
463
        'url' => 'http://www.example.com/?q=test',
464
      ),
465
      'fragment' => array(
466
        'text' => 'Display',
467
        'url' => 'http://www.example.com/#test',
468
      ),
469
    );
470

    
471
    foreach ($link_tests as $link_test) {
472
      $link_text = $link_test['text'];
473
      $link_url = $link_test['url'];
474
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
475

    
476
      $this->assertNoText($link_text);
477
      $this->assertText($single_field_name_friendly);
478
      $this->assertLinkByHref($link_url);
479
    }
480
  }
481

    
482
  /**
483
   * Formatter Separate.
484
   */
485
  public function testFormatterSeparate() {
486
    $content_type_friendly = $this->randomName(20);
487
    $content_type_machine = strtolower($this->randomName(10));
488

    
489
    $this->drupalCreateContentType(array(
490
      'type' => $content_type_machine,
491
      'name' => $content_type_friendly,
492
    ));
493

    
494
    // Now add a singleton field.
495
    $single_field_name_friendly = $this->randomName(20);
496
    $single_field_name_machine = strtolower($this->randomName(10));
497
    // $single_field_name = 'field_'. $single_field_name_machine;.
498
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
499

    
500
    // Okay, now we want to make sure this display is changed:
501
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
502
    $edit = array(
503
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
504
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_separate',
505
    );
506
    $this->drupalPost(NULL, $edit, t('Save'));
507

    
508
    $this->createNodeTypeUser($content_type_machine);
509

    
510
    $plain_url = 'http://www.example.com/';
511
    $link_tests = array(
512
      'plain' => array(
513
        'text' => $this->randomName(20),
514
        'url' => $plain_url,
515
      ),
516
      'query' => array(
517
        'text' => $this->randomName(20),
518
        'url' => $plain_url . '?q=test',
519
      ),
520
      'fragment' => array(
521
        'text' => $this->randomName(20),
522
        'url' => $plain_url . '#test',
523
      ),
524
    );
525

    
526
    foreach ($link_tests as $link_test) {
527
      $link_text = $link_test['text'];
528
      $link_url = $link_test['url'];
529
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
530

    
531
      $this->assertText($link_text);
532
      $this->assertLink($plain_url);
533
      $this->assertLinkByHref($link_url);
534
    }
535
  }
536

    
537
  /**
538
   * Formatter Plain Title.
539
   */
540
  public function testFormatterPlainTitle() {
541
    $content_type_friendly = $this->randomName(20);
542
    $content_type_machine = strtolower($this->randomName(10));
543

    
544
    $this->drupalCreateContentType(array(
545
      'type' => $content_type_machine,
546
      'name' => $content_type_friendly,
547
    ));
548

    
549
    // Now add a singleton field.
550
    $single_field_name_friendly = $this->randomName(20);
551
    $single_field_name_machine = strtolower($this->randomName(10));
552
    // $single_field_name = 'field_'. $single_field_name_machine;.
553
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
554

    
555
    // Okay, now we want to make sure this display is changed:
556
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
557
    $edit = array(
558
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
559
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_title_plain',
560
    );
561
    $this->drupalPost(NULL, $edit, t('Save'));
562

    
563
    $this->createNodeTypeUser($content_type_machine);
564

    
565
    $link_text = 'Display';
566
    $link_url = 'http://www.example.com/';
567
    $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
568

    
569
    $this->assertText($link_text);
570
    $this->assertNoText($link_url);
571
    $this->assertNoLinkByHref($link_url);
572
  }
573

    
574
}