Projet

Général

Profil

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

root / drupal7 / sites / all / modules / link / tests / link.attribute.test @ 7fe061e8

1
<?php
2

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

    
8
class LinkAttributeCrudTest extends DrupalWebTestCase {
9
  private $zebra;
10

    
11
  protected $permissions = array(
12
    'access content',
13
    'administer content types',
14
    'administer nodes',
15
    'administer filters',
16
    'access comments',
17
    'post comments',
18
    'skip comment approval',
19
    'access administration pages',
20
  );
21

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

    
30
  function setup() {
31
    parent::setup('field_ui', 'link');
32
    $this->zebra = 0;
33
    // Create and login user.
34
    $this->web_user = $this->drupalCreateUser(array('administer content types'));
35
    $this->drupalLogin($this->web_user);
36
  }
37

    
38
  protected function createLink($url, $title, $attributes = array()) {
39
    return array(
40
      'url' => $url,
41
      'title' => $title,
42
      'attributes' => $attributes,
43
    );
44
  }
45

    
46
  protected function assertLinkOnNode($field_name, $link_value, $message = '', $group = 'Other') {
47
    $this->zebra++;
48
    $zebra_string = ($this->zebra % 2 == 0) ? 'even' : 'odd';
49
    $cssFieldLocator = 'field-' . str_replace('_', '-', $field_name);
50
    $this->assertPattern('@<div class="field field-type-link ' . $cssFieldLocator . '".*<div class="field-item ' . $zebra_string . '">\s*' . $link_value . '\s*</div>@is',
51
                         $message,
52
                         $group);
53
  }
54

    
55
  /**
56
   * A simple test that just creates a new node type, adds a link field to it, creates a new node of that type, and makes sure
57
   * that the node is being displayed.
58
   */
59
  function testBasic() {
60
    $content_type_friendly = $this->randomName(20);
61
    $content_type_machine = strtolower($this->randomName(10));
62
    $title = $this->randomName(20);
63

    
64
    $this->drupalGet('admin/structure/types');
65

    
66
    // Create the content type.
67
    $this->clickLink(t('Add content type'));
68

    
69
    $edit = array(
70
      'name' => $content_type_friendly,
71
      'type' => $content_type_machine,
72
    );
73
    $this->drupalPost(NULL, $edit, t('Save and add fields'));
74
    $this->assertText(t('The content type @name has been added.', array('@name' => $content_type_friendly)));
75

    
76
    // Now add a singleton field.
77
    $single_field_name_friendly = $this->randomName(20);
78
    $single_field_name_machine = strtolower($this->randomName(10));
79
    $single_field_name = 'field_' . $single_field_name_machine;
80
    $edit = array(
81
      'fields[_add_new_field][label]' => $single_field_name_friendly,
82
      'fields[_add_new_field][field_name]' => $single_field_name_machine,
83
      'fields[_add_new_field][type]' => 'link_field',
84
      'fields[_add_new_field][widget_type]' => 'link_field',
85

    
86
    );
87
    $this->drupalPost(NULL, $edit, t('Save'));
88

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

    
92
    // Using all the default settings, so press the button.
93
    $this->drupalPost(NULL, array(), t('Save settings'));
94
    $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
95

    
96
    // Somehow clicking "save" isn't enough, and we have to do a
97
    // node_types_rebuild().
98
    node_types_rebuild();
99
    menu_rebuild();
100
    $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
101
    $this->assertTrue($type_exists, 'The new content type has been created in the database.');
102

    
103
    $permission = 'create ' . $content_type_machine . ' content';
104
    $permission_edit = 'edit ' . $content_type_machine . ' content';
105
    // Reset the permissions cache.
106
    $this->checkPermissions(array($permission), TRUE);
107

    
108
    // Now that we have a new content type, create a user that has privileges
109
    // on the content type.
110
    $permissions = array_merge($this->permissions, array($permission));
111
    $this->web_user = $this->drupalCreateUser($permissions);
112
    $this->drupalLogin($this->web_user);
113

    
114
    // Go to page.
115
    $this->drupalGet('node/add/' . $content_type_machine);
116

    
117
    // Add a node.
118
    $edit = array(
119
      'title' => $title,
120
      'field_' . $single_field_name_machine . '[und][0][title]' => 'Link',
121
      'field_' . $single_field_name_machine . '[und][0][url]' => 'http://www.drupal.org/',
122
    );
123

    
124
    $this->drupalPost(NULL, $edit, t('Save'));
125
    $this->assertText(t('@content_type_friendly @title has been created', array('@content_type_friendly' => $content_type_friendly, '@title' => $title)));
126

    
127
    $this->drupalGet('node/add/' . $content_type_machine);
128

    
129
    // Create a node:
130
    $edit = array(
131
      'title' => $title,
132
      'field_' . $single_field_name_machine . '[und][0][url]' => 'http://www.example.com/',
133
      'field_' . $single_field_name_machine . '[und][0][title]' => 'Display',
134
    );
135

    
136
    // Now we can fill in the second item in the multivalue field and save.
137
    $this->drupalPost(NULL, $edit, t('Save'));
138
    $this->assertText(t('@content_type_friendly @title has been created', array('@content_type_friendly' => $content_type_friendly, '@title' => $title)));
139

    
140
    $this->assertText('Display');
141
    $this->assertLinkByHref('http://www.example.com');
142
  }
143

    
144
  protected function createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine) {
145
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/fields');
146
    $edit = array(
147
      'fields[_add_new_field][label]' => $single_field_name_friendly,
148
      'fields[_add_new_field][field_name]' => $single_field_name_machine,
149
      'fields[_add_new_field][type]' => 'link_field',
150
      'fields[_add_new_field][widget_type]' => 'link_field',
151
    );
152
    $this->drupalPost(NULL, $edit, t('Save'));
153

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

    
157
    // Using all the default settings, so press the button.
158
    $this->drupalPost(NULL, array(), t('Save settings'));
159
    $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
160

    
161
    // Somehow clicking "save" isn't enough, and we have to do a
162
    // node_types_rebuild().
163
    node_types_rebuild();
164
    menu_rebuild();
165
    $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
166
    $this->assertTrue($type_exists, 'The new content type has been created in the database.');
167
  }
168

    
169
  protected function createNodeTypeUser($content_type_machine) {
170
    $permission = 'create ' . $content_type_machine . ' content';
171
    $permission_edit = 'edit ' . $content_type_machine . ' content';
172
    // Reset the permissions cache.
173
    $this->checkPermissions(array($permission), TRUE);
174

    
175
    // Now that we have a new content type, create a user that has privileges
176
    // on the content type.
177
    $permissions = array_merge($this->permissions, array($permission));
178
    $this->web_user = $this->drupalCreateUser($permissions);
179
    $this->drupalLogin($this->web_user);
180
  }
181

    
182
  protected function createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $title, $url, $node_title = '') {
183
    $this->drupalGet('node/add/' . $content_type_machine);
184

    
185
    if (!$node_title) {
186
      $node_title = $this->randomName(20);
187
    }
188
    $edit = array(
189
      'title' => $node_title,
190
    );
191
    if ($url) {
192
      $edit['field_' . $single_field_name_machine . '[und][0][url]'] = $url;
193
    }
194
    if ($title) {
195
      $edit['field_' . $single_field_name_machine . '[und][0][title]'] = $title;
196
    }
197

    
198
    $this->drupalPost(NULL, $edit, t('Save'));
199
    $this->assertText(t('@content_type_friendly @title has been created', array('@content_type_friendly' => $content_type_friendly, '@title' => $node_title)));
200

    
201
  }
202

    
203
  /**
204
   * Test the link_plain formatter and it's output.
205
   */
206
  function testFormatterPlain() {
207
    $content_type_friendly = $this->randomName(20);
208
    $content_type_machine = strtolower($this->randomName(10));
209

    
210
    $this->drupalCreateContentType(array(
211
      'type' => $content_type_machine,
212
      'name' => $content_type_friendly,
213
    ));
214

    
215
    // Now add a singleton field.
216
    $single_field_name_friendly = $this->randomName(20);
217
    $single_field_name_machine = strtolower($this->randomName(10));
218
    //$single_field_name = 'field_'. $single_field_name_machine;
219
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
220

    
221
    // Okay, now we want to make sure this display is changed:
222
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
223
    $edit = array(
224
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
225
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_plain',
226
    );
227
    $this->drupalPost(NULL, $edit, t('Save'));
228

    
229
    $this->createNodeTypeUser($content_type_machine);
230
    
231
    $link_tests = array(
232
      'plain' => array(
233
        'text' => 'Display',
234
        'url' => 'http://www.example.com/',
235
      ),
236
      'query' => array(
237
        'text' => 'Display',
238
        'url' => 'http://www.example.com/?q=test',
239
      ),
240
      'fragment' => array(
241
        'text' => 'Display',
242
        'url' => 'http://www.example.com/#test',
243
      ),
244
    );
245

    
246
    foreach ($link_tests as $key => $link_test) {
247
      $link_text = $link_test['text'];
248
      $link_url = $link_test['url'];
249
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
250
  
251
      $this->assertText($link_url);
252
      $this->assertNoText($link_text);
253
      $this->assertNoLinkByHref($link_url);
254
    }
255
  }
256

    
257
  function testFormatterHost() {
258
    $content_type_friendly = $this->randomName(20);
259
    $content_type_machine = strtolower($this->randomName(10));
260

    
261
    $this->drupalCreateContentType(array(
262
      'type' => $content_type_machine,
263
      'name' => $content_type_friendly,
264
    ));
265

    
266

    
267
    // Now add a singleton field.
268
    $single_field_name_friendly = $this->randomName(20);
269
    $single_field_name_machine = strtolower($this->randomName(10));
270
    //$single_field_name = 'field_'. $single_field_name_machine;
271
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
272

    
273
    // Okay, now we want to make sure this display is changed:
274
    $this->drupalGet('admin/structure/types/manage/'. $content_type_machine .'/display');
275
    $edit = array(
276
      'fields[field_'. $single_field_name_machine .'][label]' => 'above',
277
      'fields[field_'. $single_field_name_machine .'][type]' => 'link_host',
278
    );
279
    $this->drupalPost(NULL, $edit, t('Save'));
280

    
281
    $this->createNodeTypeUser($content_type_machine);
282

    
283
    $link_text = 'Display';
284
    $link_url = 'http://www.example.com/';
285
    $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
286

    
287
    $this->assertText('www.example.com');
288
    $this->assertNoText($link_text);
289
    $this->assertNoLinkByHref($link_url);
290
  }
291

    
292
  function testFormatterURL() {
293
    $content_type_friendly = $this->randomName(20);
294
    $content_type_machine = strtolower($this->randomName(10));
295

    
296
    $this->drupalCreateContentType(array(
297
      'type' => $content_type_machine,
298
      'name' => $content_type_friendly,
299
    ));
300

    
301
    // Now add a singleton field.
302
    $single_field_name_friendly = $this->randomName(20);
303
    $single_field_name_machine = strtolower($this->randomName(10));
304
    //$single_field_name = 'field_'. $single_field_name_machine;
305
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
306

    
307
    // Okay, now we want to make sure this display is changed:
308
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
309
    $edit = array(
310
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
311
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_url',
312
    );
313
    $this->drupalPost(NULL, $edit, t('Save'));
314

    
315
    $this->createNodeTypeUser($content_type_machine);
316
    
317
    $link_tests = array(
318
      'plain' => array(
319
        'text' => 'Display',
320
        'url' => 'http://www.example.com/',
321
      ),
322
      'query' => array(
323
        'text' => 'Display',
324
        'url' => 'http://www.example.com/?q=test',
325
      ),
326
      'fragment' => array(
327
        'text' => 'Display',
328
        'url' => 'http://www.example.com/#test',
329
      ),
330
    );
331

    
332
    foreach ($link_tests as $key => $link_test) {
333
      $link_text = $link_test['text'];
334
      $link_url = $link_test['url'];
335
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
336
  
337
      $this->assertNoText($link_text);
338
      $this->assertLinkByHref($link_url);
339
    }
340
  }
341

    
342
  function testFormatterShort() {
343
    $content_type_friendly = $this->randomName(20);
344
    $content_type_machine = strtolower($this->randomName(10));
345

    
346
    $this->drupalCreateContentType(array(
347
      'type' => $content_type_machine,
348
      'name' => $content_type_friendly,
349
    ));
350

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

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

    
365
    $this->createNodeTypeUser($content_type_machine);
366

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

    
382
    foreach ($link_tests as $key => $link_test) {
383
      $link_text = $link_test['text'];
384
      $link_url = $link_test['url'];
385
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
386
  
387
      $this->assertText('Link');
388
      $this->assertNoText($link_text);
389
      $this->assertLinkByHref($link_url);
390
    }
391
  }
392

    
393
  function testFormatterLabel() {
394
    $content_type_friendly = $this->randomName(20);
395
    $content_type_machine = strtolower($this->randomName(10));
396

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

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

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

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

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

    
433
    foreach ($link_tests as $key => $link_test) {
434
      $link_text = $link_test['text'];
435
      $link_url = $link_test['url'];  
436
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
437
  
438
      $this->assertNoText($link_text);
439
      $this->assertText($single_field_name_friendly);
440
      $this->assertLinkByHref($link_url);
441
    }
442
  }
443

    
444
  function testFormatterSeparate() {
445
    $content_type_friendly = $this->randomName(20);
446
    $content_type_machine = strtolower($this->randomName(10));
447

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

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

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

    
467
    $this->createNodeTypeUser($content_type_machine);
468

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

    
485
    foreach ($link_tests as $key => $link_test) {
486
      $link_text = $link_test['text'];
487
      $link_url = $link_test['url'];
488
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
489
  
490
      $this->assertText($link_text);
491
      $this->assertLink($plain_url);
492
      $this->assertLinkByHref($link_url);
493
    }
494
  }
495
  
496
  function testFormatterPlainTitle() {
497
    $content_type_friendly = $this->randomName(20);
498
    $content_type_machine = strtolower($this->randomName(10));
499
    
500
    $this->drupalCreateContentType(array(
501
      'type' => $content_type_machine,
502
      'name' => $content_type_friendly,
503
    ));
504
    
505
    // Now add a singleton field.
506
    $single_field_name_friendly = $this->randomName(20);
507
    $single_field_name_machine = strtolower($this->randomName(10));
508
    //$single_field_name = 'field_'. $single_field_name_machine;
509
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
510
    
511
    // Okay, now we want to make sure this display is changed:
512
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
513
    $edit = array(
514
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
515
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_title_plain',
516
    );
517
    $this->drupalPost(NULL, $edit, t('Save'));
518
    
519
    $this->createNodeTypeUser($content_type_machine);
520
    
521
    $link_text = 'Display';
522
    $link_url = 'http://www.example.com/';
523
    $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
524
    
525
    $this->assertText($link_text);
526
    $this->assertNoText($link_url);
527
    $this->assertNoLinkByHref($link_url);
528
  }
529
}