Projet

Général

Profil

Paste
Télécharger (14,3 ko) Statistiques
| Branche: | Révision:

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

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5 bad4e148 Assos Assos
 * Testing that tokens can be used in link titles.
6 85ad3d82 Assos Assos
 */
7
8
/**
9 39a181a4 Assos Assos
 * Testing that tokens can be used in link titles.
10 85ad3d82 Assos Assos
 */
11
class LinkTokenTest extends LinkBaseTestClass {
12
13 39a181a4 Assos Assos
  /**
14
   * Get Info.
15
   */
16 85ad3d82 Assos Assos
  public static function getInfo() {
17
    return array(
18
      'name' => 'Link tokens - browser test',
19
      'description' => 'Tests including tokens in link titles, making sure they appear in node views.',
20
      'group' => 'Link',
21
      'dependencies' => array('token'),
22
    );
23
  }
24
25 39a181a4 Assos Assos
  /**
26 8e7483ab Assos Assos
   * {@inheritdoc}
27 39a181a4 Assos Assos
   */
28 8e7483ab Assos Assos
  public function setUp(array $modules = array()) {
29
    $modules[] = 'token';
30
    parent::setUp($modules);
31 85ad3d82 Assos Assos
  }
32
33
  /**
34
   * Creates a link field with a required title enabled for user-entered tokens.
35 39a181a4 Assos Assos
   *
36 85ad3d82 Assos Assos
   * Creates a node with a token in the link title and checks the value.
37
   */
38 39a181a4 Assos Assos
  public function testUserTokenLinkCreate() {
39
    // Create field.
40 85ad3d82 Assos Assos
    $settings = array(
41
      'instance[settings][enable_tokens]' => 1,
42
    );
43
    $field_name = $this->createLinkField('page',
44 39a181a4 Assos Assos
      $settings);
45 85ad3d82 Assos Assos
46 39a181a4 Assos Assos
    // Create page form.
47 85ad3d82 Assos Assos
    $this->drupalGet('node/add/page');
48 39a181a4 Assos Assos
    // $field_name = 'field_' . $name;.
49 85ad3d82 Assos Assos
    $this->assertField($field_name . '[und][0][title]', 'Title found');
50
    $this->assertField($field_name . '[und][0][url]', 'URL found');
51
52
    $input = array(
53 39a181a4 Assos Assos
      'href' => 'http://example.com/' . $this->randomName(),
54
      'label' => $this->randomName(),
55 85ad3d82 Assos Assos
    );
56
57 39a181a4 Assos Assos
    // $this->drupalLogin($this->web_user);.
58 85ad3d82 Assos Assos
    $this->drupalGet('node/add/page');
59
60
    $edit = array(
61
      'title' => $input['label'],
62
      $field_name . '[und][0][title]' => $input['label'] . " [node:content-type:machine-name]",
63
      $field_name . '[und][0][url]' => $input['href'],
64
    );
65
    $this->drupalPost(NULL, $edit, t('Save'));
66
    $url = $this->getUrl();
67
68 39a181a4 Assos Assos
    // Change to anonymous user.
69 85ad3d82 Assos Assos
    $this->drupalLogout();
70
    $this->drupalGet($url);
71
72
    $this->assertRaw(l($input['label'] . ' page', $input['href']));
73
  }
74
75
  /**
76
   * Creates a link field with a static title and an admin-entered token.
77 39a181a4 Assos Assos
   *
78 85ad3d82 Assos Assos
   * Creates a node with a link and checks the title value.
79
   */
80 39a181a4 Assos Assos
  public function testStaticTokenLinkCreate() {
81
    // Create field.
82 85ad3d82 Assos Assos
    $name = $this->randomName();
83
    $settings = array(
84
      'instance[settings][title]' => 'value',
85 39a181a4 Assos Assos
      'instance[settings][title_value]' => $name . ' [node:content-type:machine-name]',
86
    );
87 85ad3d82 Assos Assos
    $field_name = $this->createLinkField('page', $settings);
88
89 39a181a4 Assos Assos
    // Create page form.
90 85ad3d82 Assos Assos
    $this->drupalGet('node/add/page');
91
    $this->assertField($field_name . '[und][0][url]', 'URL found');
92
93
    $input = array(
94 39a181a4 Assos Assos
      'href' => 'http://example.com/' . $this->randomName(),
95 85ad3d82 Assos Assos
    );
96
97 39a181a4 Assos Assos
    // $this->drupalLogin($this->web_user);.
98 85ad3d82 Assos Assos
    $this->drupalGet('node/add/page');
99
100
    $edit = array(
101
      'title' => $name,
102
      $field_name . '[und][0][url]' => $input['href'],
103
    );
104
    $this->drupalPost(NULL, $edit, t('Save'));
105
106
    $url = $this->getUrl();
107
108 39a181a4 Assos Assos
    // Change to anonymous user.
109 85ad3d82 Assos Assos
    $this->drupalLogout();
110
    $this->drupalGet($url);
111
112
    $this->assertRaw(l($name . ' page', $input['href']));
113
  }
114
115
  /**
116
   * Creates a link field with a static title and an admin-entered token.
117 39a181a4 Assos Assos
   *
118 85ad3d82 Assos Assos
   * Creates a node with a link and checks the title value.
119
   *
120
   * Basically, I want to make sure the [title-raw] token works, because it's a
121
   * token that changes from node to node, where [type]'s always going to be the
122
   * same.
123
   */
124 39a181a4 Assos Assos
  public function testStaticTokenLinkCreate2() {
125
    // Create field.
126 85ad3d82 Assos Assos
    $name = $this->randomName();
127
    $settings = array(
128
      'instance[settings][title]' => 'value',
129 39a181a4 Assos Assos
      'instance[settings][title_value]' => $name . ' [node:title]',
130
    );
131 85ad3d82 Assos Assos
    $field_name = $this->createLinkField('page', $settings);
132
133 39a181a4 Assos Assos
    // Create page form.
134 85ad3d82 Assos Assos
    $this->drupalGet('node/add/page');
135
    $this->assertField($field_name . '[und][0][url]', 'URL found');
136
137
    $input = array(
138 39a181a4 Assos Assos
      'href' => 'http://example.com/' . $this->randomName(),
139 85ad3d82 Assos Assos
    );
140
141 39a181a4 Assos Assos
    // $this->drupalLogin($this->web_user);.
142 85ad3d82 Assos Assos
    $this->drupalGet('node/add/page');
143
144
    $edit = array(
145
      'title' => $name,
146
      $field_name . '[und][0][url]' => $input['href'],
147
    );
148
    $this->drupalPost(NULL, $edit, t('Save'));
149
150
    $url = $this->getUrl();
151
152 39a181a4 Assos Assos
    // Change to anonymous user.
153 85ad3d82 Assos Assos
    $this->drupalLogout();
154
    $this->drupalGet($url);
155
156 c8740e19 Assos Assos
    $this->assertRaw(l($name . ' ' . $name, $input['href']));
157 85ad3d82 Assos Assos
  }
158
159 39a181a4 Assos Assos
  /**
160
   * This test doesn't seem to actually work, due to lack of 'title' in url.
161
   *
162
   * @codingStandardsIgnoreStart
163
   */
164 8e7483ab Assos Assos
  public function xTestLinkWithTitleAttributeTokenUrlForm() {
165 39a181a4 Assos Assos
    /* $this->loginWithPermissions($this->permissions);
166 85ad3d82 Assos Assos
    $this->acquireContentTypes(1);
167
    $field_settings = array(
168 39a181a4 Assos Assos
    'type' => 'link',
169
    'widget_type' => 'link',
170
    'type_name' => $this->content_types[0]->name,
171
    'attributes' => array(
172
    'class' => '',
173
    'target' => 'default',
174
    'rel' => 'nofollow',
175
    'title' => '',
176
    ),
177 85ad3d82 Assos Assos
    );
178
179
    $field = $this->createField($field_settings, 0);
180
    //$this->fail('<pre>'. print_r($field, TRUE) .'</pre>');
181
    $field_name = $field['field_name'];
182
    $field_db_info = content_database_info($field);
183
    $url_type = str_replace('_', '-', $this->content_types[0]->type);
184
185
    $edit = array('attributes[title]' => '['. $field_name .'-url]',
186 39a181a4 Assos Assos
    'enable_tokens' => TRUE);
187
    // @codingStandardsIgnoreLine
188 85ad3d82 Assos Assos
    $this->drupalPost('admin/content/node-type/'. $url_type .'/fields/'. $field['field_name'],
189 39a181a4 Assos Assos
    $edit, t('Save field settings'));
190 85ad3d82 Assos Assos
    $this->assertText(t('Saved field @field_name', array('@field_name' => $field['field_name'])));*/
191 8e7483ab Assos Assos
    // @codingStandardsIgnoreEnd
192 85ad3d82 Assos Assos
    $name = $this->randomName();
193
    $settings = array(
194
      'instance[settings][attributes][rel]' => 'nofollow',
195
    );
196
197
    $field_name = $this->createLinkField('page', $settings);
198
199
    // So, having saved this field_name, let's see if it works...
200 39a181a4 Assos Assos
    // $this->acquireNodes(1);
201
    // $node = node_load($this->nodes[0]->nid);
202
    // $this->drupalGet('node/'. $this->nodes[0]->nid);.
203 85ad3d82 Assos Assos
    $edit = array();
204
    $test_link_url = 'http://www.example.com/test';
205 c8740e19 Assos Assos
    $edit[$field_name . '[und][0][url]'] = $test_link_url;
206
    $title = 'title_' . $this->randomName(20);
207
    $edit[$field_name . '[und][0][title]'] = $title;
208 85ad3d82 Assos Assos
    $edit['title'] = $name;
209
210
    $this->drupalGet('node/add/page');
211
    $this->drupalPost(NULL, $edit, t('Save'));
212
213
    // Make sure we get a new version!
214 39a181a4 Assos Assos
    // $node = node_load($this->nodes[0]->nid, NULL, TRUE);.
215 85ad3d82 Assos Assos
    $this->assertText(t('Basic page @title has been updated.',
216 39a181a4 Assos Assos
      array('@title' => $name)));
217 85ad3d82 Assos Assos
218 39a181a4 Assos Assos
    // $this->drupalGet('node/'. $node->nid);.
219 85ad3d82 Assos Assos
    $this->assertText($title, 'Make sure the link title/text shows');
220 c8740e19 Assos Assos
    $this->assertRaw(' title="' . $test_link_url . '"', "Do we show the link url as the title attribute?");
221
    $this->assertNoRaw(' title="[' . $field_name . '-url]"');
222 85ad3d82 Assos Assos
    $this->assertTrue(module_exists('token'), t('Assure that Token Module is enabled.'));
223 39a181a4 Assos Assos
    // $this->fail($this->content);.
224 85ad3d82 Assos Assos
  }
225
226
  /**
227 39a181a4 Assos Assos
   * Link With Title Attribute token title form.
228
   *
229 85ad3d82 Assos Assos
   * If the title of the link is set to the title attribute, then the title
230
   * attribute isn't supposed to show.
231 39a181a4 Assos Assos
   *
232
   * @codingStandardsIgnoreStart
233 85ad3d82 Assos Assos
   */
234 8e7483ab Assos Assos
  public function xTestLinkWithTitleAttributeTokenTitleForm() {
235 39a181a4 Assos Assos
    // @codingStandardsIgnoreEnd
236 85ad3d82 Assos Assos
    $this->loginWithPermissions($this->permissions);
237
    $this->acquireContentTypes(1);
238
    $field_settings = array(
239
      'type' => 'link',
240
      'widget_type' => 'link',
241
      'type_name' => $this->content_types[0]->name,
242
      'attributes' => array(
243
        'class' => '',
244
        'target' => 'default',
245
        'rel' => 'nofollow',
246
        'title' => '',
247
      ),
248
    );
249
250
    $field = $this->createField($field_settings, 0);
251
    $field_name = $field['field_name'];
252
    $url_type = str_replace('_', '-', $this->content_types[0]->type);
253
254 39a181a4 Assos Assos
    $edit = array(
255
      'attributes[title]' => '[' . $field_name . '-title]',
256
      'enable_tokens' => TRUE,
257
    );
258 85ad3d82 Assos Assos
259 c8740e19 Assos Assos
    $this->drupalPost('admin/content/node-type/' . $url_type . '/fields/' . $field['field_name'],
260 39a181a4 Assos Assos
      $edit, t('Save field settings'));
261 85ad3d82 Assos Assos
    $this->assertText(t('Saved field @field_name', array('@field_name' => $field['field_name'])));
262
263
    // So, having saved this field_name, let's see if it works...
264
    $this->acquireNodes(1);
265
266 c8740e19 Assos Assos
    $this->drupalGet('node/' . $this->nodes[0]->nid);
267 85ad3d82 Assos Assos
268
    $edit = array();
269 c8740e19 Assos Assos
    $edit[$field['field_name'] . '[0][url]'] = 'http://www.example.com/test';
270
    $title = 'title_' . $this->randomName(20);
271
    $edit[$field['field_name'] . '[0][title]'] = $title;
272 85ad3d82 Assos Assos
273 c8740e19 Assos Assos
    $this->drupalPost('node/' . $this->nodes[0]->nid . '/edit', $edit, t('Save'));
274 85ad3d82 Assos Assos
275
    // Make sure we get a new version!
276
    $node = node_load($this->nodes[0]->nid, NULL, TRUE);
277
    $this->assertText(t('@type @title has been updated.',
278 39a181a4 Assos Assos
      array(
279
        '@title' => $node->title,
280
        '@type' => $this->content_types[0]->name,
281
      )));
282 85ad3d82 Assos Assos
283 c8740e19 Assos Assos
    $this->drupalGet('node/' . $node->nid);
284 85ad3d82 Assos Assos
    $this->assertText($title, 'Make sure the link title/text shows');
285 c8740e19 Assos Assos
    $this->assertNoRaw(' title="' . $title . '"', "We should not show the link title as the title attribute?");
286
    $this->assertNoRaw(' title="[' . $field_name . '-title]"');
287 39a181a4 Assos Assos
    // $this->fail($this->content);.
288 85ad3d82 Assos Assos
  }
289
290
  /**
291 39a181a4 Assos Assos
   * Trying to set the url to contain a token.
292 85ad3d82 Assos Assos
   */
293 8e7483ab Assos Assos
  public function xTestUserTokenLinkCreateInUrl() {
294 39a181a4 Assos Assos
    $this->web_user = $this->drupalCreateUser(array(
295
      'administer content types',
296
      'administer fields',
297
      'access content',
298
      'create page content',
299
    ));
300 85ad3d82 Assos Assos
    $this->drupalLogin($this->web_user);
301
302 39a181a4 Assos Assos
    // Create field.
303 85ad3d82 Assos Assos
    $name = strtolower($this->randomName());
304
    $edit = array(
305
      '_add_new_field[label]' => $name,
306
      '_add_new_field[field_name]' => $name,
307
      '_add_new_field[type]' => 'link',
308
      '_add_new_field[widget_type]' => 'link',
309
    );
310
    $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
311
    $this->drupalPost(NULL, array(
312
      'title' => 'required',
313 39a181a4 Assos Assos
      'enable_tokens' => 1,
314
    ), t('Save field settings'));
315 85ad3d82 Assos Assos
316
    // Is field created?
317
    $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');
318
319 39a181a4 Assos Assos
    // Create page form.
320 85ad3d82 Assos Assos
    $this->drupalGet('node/add/page');
321
    $field_name = 'field_' . $name;
322
    $this->assertField($field_name . '[0][title]', 'Title found');
323
    $this->assertField($field_name . '[0][url]', 'URL found');
324
325
    $input = array(
326 39a181a4 Assos Assos
      'href' => 'http://example.com/' . $this->randomName(),
327
      'label' => $this->randomName(),
328 85ad3d82 Assos Assos
    );
329
330
    $this->drupalLogin($this->web_user);
331
    $this->drupalGet('node/add/page');
332
333
    $edit = array(
334
      'title' => $input['label'],
335
      $field_name . '[0][title]' => $input['label'],
336
      $field_name . '[0][url]' => $input['href'] . "/[type]",
337
    );
338
    $this->drupalPost(NULL, $edit, t('Save'));
339
    $url = $this->getUrl();
340
341 39a181a4 Assos Assos
    // Change to anonymous user.
342 85ad3d82 Assos Assos
    $this->drupalLogout();
343
    $this->drupalGet($url);
344
345 c8740e19 Assos Assos
    $this->assertRaw(l($input['label'], $input['href'] . '/page'));
346 39a181a4 Assos Assos
    // $this->fail($this->content);.
347 85ad3d82 Assos Assos
  }
348
349
  /**
350 39a181a4 Assos Assos
   * Trying to set the url to contain a token.
351 85ad3d82 Assos Assos
   */
352 8e7483ab Assos Assos
  public function xTestUserTokenLinkCreateInUrl2() {
353 39a181a4 Assos Assos
    $this->web_user = $this->drupalCreateUser(array(
354
      'administer content types',
355
      'administer fields',
356
      'access content',
357
      'create page content',
358
    ));
359 85ad3d82 Assos Assos
    $this->drupalLogin($this->web_user);
360
361 39a181a4 Assos Assos
    // Create field.
362 85ad3d82 Assos Assos
    $name = strtolower($this->randomName());
363
    $edit = array(
364
      '_add_new_field[label]' => $name,
365
      '_add_new_field[field_name]' => $name,
366
      '_add_new_field[type]' => 'link',
367
      '_add_new_field[widget_type]' => 'link',
368
    );
369
    $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
370
    $this->drupalPost(NULL, array(
371
      'title' => 'required',
372 39a181a4 Assos Assos
      'enable_tokens' => 1,
373
    ), t('Save field settings'));
374 85ad3d82 Assos Assos
375
    // Is field created?
376
    $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');
377
378 39a181a4 Assos Assos
    // Create page form.
379 85ad3d82 Assos Assos
    $this->drupalGet('node/add/page');
380
    $field_name = 'field_' . $name;
381
    $this->assertField($field_name . '[0][title]', 'Title found');
382
    $this->assertField($field_name . '[0][url]', 'URL found');
383
384
    $input = array(
385 39a181a4 Assos Assos
      'href' => 'http://example.com/' . $this->randomName(),
386
      'label' => $this->randomName(),
387 85ad3d82 Assos Assos
    );
388
389
    $this->drupalLogin($this->web_user);
390
    $this->drupalGet('node/add/page');
391
392
    $edit = array(
393
      'title' => $input['label'],
394
      $field_name . '[0][title]' => $input['label'],
395
      $field_name . '[0][url]' => $input['href'] . "/[author-uid]",
396
    );
397
    $this->drupalPost(NULL, $edit, t('Save'));
398
    $url = $this->getUrl();
399
400 39a181a4 Assos Assos
    // Change to anonymous user.
401 85ad3d82 Assos Assos
    $this->drupalLogout();
402
    $this->drupalGet($url);
403
404 c8740e19 Assos Assos
    $this->assertRaw(l($input['label'], $input['href'] . '/' . $this->web_user->uid));
405
  }
406 39a181a4 Assos Assos
407 c8740e19 Assos Assos
  /**
408 39a181a4 Assos Assos
   * CRUD Title Only Title No Link.
409
   *
410
   * Test that if you have a title and no url on a field which does not have
411
   * tokens enabled, that the title is sanitized once.
412
   *
413
   * @codingStandardsIgnoreStart
414 c8740e19 Assos Assos
   */
415 39a181a4 Assos Assos
  public function testCRUDTitleOnlyTitleNoLink2() {
416
    //@codingStandardsIgnoreEnd
417
418
    $this->web_user = $this->drupalCreateUser(array(
419
      'administer content types',
420
      'administer fields',
421
      'access content',
422
      'create page content',
423
    ));
424
425 c8740e19 Assos Assos
    $this->drupalLogin($this->web_user);
426
427 39a181a4 Assos Assos
    // Create field.
428 c8740e19 Assos Assos
    $name = strtolower($this->randomName());
429
    $field_name = 'field_' . $name;
430
    $edit = array(
431
      'fields[_add_new_field][label]' => $name,
432
      'fields[_add_new_field][field_name]' => $name,
433
      'fields[_add_new_field][type]' => 'link_field',
434
      'fields[_add_new_field][widget_type]' => 'link_field',
435
    );
436
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
437
    $this->drupalPost(NULL, array(), t('Save field settings'));
438
    $this->drupalPost(NULL, array(
439
      'instance[settings][url]' => 1,
440
      'instance[settings][enable_tokens]' => 0,
441
    ), t('Save settings'));
442
443
    // Is field created?
444
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
445 39a181a4 Assos Assos
446
    // Create page form.
447 c8740e19 Assos Assos
    $this->drupalGet('node/add/page');
448
    $this->assertField($field_name . '[und][0][url]', 'URL found');
449
450
    $input = array(
451
      'title' => 'This & That',
452
      'href' => '',
453
    );
454
455
    $edit = array(
456
      'title' => $name,
457
      $field_name . '[und][0][title]' => $input['title'],
458
      $field_name . '[und][0][url]' => $input['href'],
459
    );
460
    $this->drupalPost(NULL, $edit, t('Save'));
461
462
    $url = $this->getUrl();
463 39a181a4 Assos Assos
464
    // Change to anonymous user.
465 c8740e19 Assos Assos
    $this->drupalLogout();
466
    $this->drupalGet($url);
467
468
    $this->assertRaw('This &amp; That');
469 85ad3d82 Assos Assos
  }
470 39a181a4 Assos Assos
471 85ad3d82 Assos Assos
}