Projet

Général

Profil

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

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

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 testFormatterURL() {
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
    // Now add a singleton field.
267
    $single_field_name_friendly = $this->randomName(20);
268
    $single_field_name_machine = strtolower($this->randomName(10));
269
    //$single_field_name = 'field_'. $single_field_name_machine;
270
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
271

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

    
280
    $this->createNodeTypeUser($content_type_machine);
281
    
282
    $link_tests = array(
283
      'plain' => array(
284
        'text' => 'Display',
285
        'url' => 'http://www.example.com/',
286
      ),
287
      'query' => array(
288
        'text' => 'Display',
289
        'url' => 'http://www.example.com/?q=test',
290
      ),
291
      'fragment' => array(
292
        'text' => 'Display',
293
        'url' => 'http://www.example.com/#test',
294
      ),
295
    );
296

    
297
    foreach ($link_tests as $key => $link_test) {
298
      $link_text = $link_test['text'];
299
      $link_url = $link_test['url'];
300
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
301
  
302
      $this->assertNoText($link_text);
303
      $this->assertLinkByHref($link_url);
304
    }
305
  }
306

    
307
  function testFormatterShort() {
308
    $content_type_friendly = $this->randomName(20);
309
    $content_type_machine = strtolower($this->randomName(10));
310

    
311
    $this->drupalCreateContentType(array(
312
      'type' => $content_type_machine,
313
      'name' => $content_type_friendly,
314
    ));
315

    
316
    // Now add a singleton field.
317
    $single_field_name_friendly = $this->randomName(20);
318
    $single_field_name_machine = strtolower($this->randomName(10));
319
    //$single_field_name = 'field_'. $single_field_name_machine;
320
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
321

    
322
    // Okay, now we want to make sure this display is changed:
323
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
324
    $edit = array(
325
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
326
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_short',
327
    );
328
    $this->drupalPost(NULL, $edit, t('Save'));
329

    
330
    $this->createNodeTypeUser($content_type_machine);
331

    
332
    $link_tests = array(
333
      'plain' => array(
334
        'text' => 'Display',
335
        'url' => 'http://www.example.com/',
336
      ),
337
      'query' => array(
338
        'text' => 'Display',
339
        'url' => 'http://www.example.com/?q=test',
340
      ),
341
      'fragment' => array(
342
        'text' => 'Display',
343
        'url' => 'http://www.example.com/#test',
344
      ),
345
    );
346

    
347
    foreach ($link_tests as $key => $link_test) {
348
      $link_text = $link_test['text'];
349
      $link_url = $link_test['url'];
350
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
351
  
352
      $this->assertText('Link');
353
      $this->assertNoText($link_text);
354
      $this->assertLinkByHref($link_url);
355
    }
356
  }
357

    
358
  function testFormatterLabel() {
359
    $content_type_friendly = $this->randomName(20);
360
    $content_type_machine = strtolower($this->randomName(10));
361

    
362
    $this->drupalCreateContentType(array(
363
      'type' => $content_type_machine,
364
      'name' => $content_type_friendly,
365
    ));
366

    
367
    // Now add a singleton field.
368
    $single_field_name_friendly = $this->randomName(20);
369
    $single_field_name_machine = strtolower($this->randomName(10));
370
    //$single_field_name = 'field_'. $single_field_name_machine;
371
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
372

    
373
    // Okay, now we want to make sure this display is changed:
374
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
375
    $edit = array(
376
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
377
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_label',
378
    );
379
    $this->drupalPost(NULL, $edit, t('Save'));
380

    
381
    $this->createNodeTypeUser($content_type_machine);
382

    
383
    $link_tests = array(
384
      'plain' => array(
385
        'text' => 'Display',
386
        'url' => 'http://www.example.com/',
387
      ),
388
      'query' => array(
389
        'text' => 'Display',
390
        'url' => 'http://www.example.com/?q=test',
391
      ),
392
      'fragment' => array(
393
        'text' => 'Display',
394
        'url' => 'http://www.example.com/#test',
395
      ),
396
    );
397

    
398
    foreach ($link_tests as $key => $link_test) {
399
      $link_text = $link_test['text'];
400
      $link_url = $link_test['url'];  
401
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
402
  
403
      $this->assertNoText($link_text);
404
      $this->assertText($single_field_name_friendly);
405
      $this->assertLinkByHref($link_url);
406
    }
407
  }
408

    
409
  function testFormatterSeparate() {
410
    $content_type_friendly = $this->randomName(20);
411
    $content_type_machine = strtolower($this->randomName(10));
412

    
413
    $this->drupalCreateContentType(array(
414
      'type' => $content_type_machine,
415
      'name' => $content_type_friendly,
416
    ));
417

    
418
    // Now add a singleton field.
419
    $single_field_name_friendly = $this->randomName(20);
420
    $single_field_name_machine = strtolower($this->randomName(10));
421
    //$single_field_name = 'field_'. $single_field_name_machine;
422
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
423

    
424
    // Okay, now we want to make sure this display is changed:
425
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
426
    $edit = array(
427
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
428
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_separate',
429
    );
430
    $this->drupalPost(NULL, $edit, t('Save'));
431

    
432
    $this->createNodeTypeUser($content_type_machine);
433

    
434
    $plain_url = 'http://www.example.com/';
435
    $link_tests = array(
436
      'plain' => array(
437
        'text' => $this->randomName(20),
438
        'url' => $plain_url,
439
      ),
440
      'query' => array(
441
        'text' => $this->randomName(20),
442
        'url' => $plain_url . '?q=test',
443
      ),
444
      'fragment' => array(
445
        'text' => $this->randomName(20),
446
        'url' => $plain_url . '#test',
447
      ),
448
    );
449

    
450
    foreach ($link_tests as $key => $link_test) {
451
      $link_text = $link_test['text'];
452
      $link_url = $link_test['url'];
453
      $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
454
  
455
      $this->assertText($link_text);
456
      $this->assertLink($plain_url);
457
      $this->assertLinkByHref($link_url);
458
    }
459
  }
460
  
461
  function testFormatterPlainTitle() {
462
    $content_type_friendly = $this->randomName(20);
463
    $content_type_machine = strtolower($this->randomName(10));
464
    
465
    $this->drupalCreateContentType(array(
466
      'type' => $content_type_machine,
467
      'name' => $content_type_friendly,
468
    ));
469
    
470
    // Now add a singleton field.
471
    $single_field_name_friendly = $this->randomName(20);
472
    $single_field_name_machine = strtolower($this->randomName(10));
473
    //$single_field_name = 'field_'. $single_field_name_machine;
474
    $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
475
    
476
    // Okay, now we want to make sure this display is changed:
477
    $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
478
    $edit = array(
479
      'fields[field_' . $single_field_name_machine . '][label]' => 'above',
480
      'fields[field_' . $single_field_name_machine . '][type]' => 'link_title_plain',
481
    );
482
    $this->drupalPost(NULL, $edit, t('Save'));
483
    
484
    $this->createNodeTypeUser($content_type_machine);
485
    
486
    $link_text = 'Display';
487
    $link_url = 'http://www.example.com/';
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->assertNoText($link_url);
492
    $this->assertNoLinkByHref($link_url);
493
  }
494
}