LinkTokenTest.test 14.3 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Testing that tokens can be used in link titles.
6
7
8
 */

/**
9
 * Testing that tokens can be used in link titles.
10
11
 */
class LinkTokenTest extends LinkBaseTestClass {
12

13
14
15
  /**
   * Get Info.
   */
16
  public static function getInfo() {
17
    return array(
18
19
20
21
      'name' => 'Link tokens - browser test',
      'description' => 'Tests including tokens in link titles, making sure they appear in node views.',
      'group' => 'Link',
      'dependencies' => array('token'),
22
23
24
    );
  }

25
  /**
26
   * {@inheritdoc}
27
   */
28
29
30
  public function setUp(array $modules = array()) {
    $modules[] = 'token';
    parent::setUp($modules);
31
32
33
34
  }

  /**
   * Creates a link field with a required title enabled for user-entered tokens.
35
   *
36
37
   * Creates a node with a token in the link title and checks the value.
   */
38
39
  public function testUserTokenLinkCreate() {
    // Create field.
40
41
42
43
    $settings = array(
      'instance[settings][enable_tokens]' => 1,
    );
    $field_name = $this->createLinkField('page',
44
      $settings);
45

46
    // Create page form.
47
    $this->drupalGet('node/add/page');
48
    // $field_name = 'field_' . $name;.
49
50
51
52
    $this->assertField($field_name . '[und][0][title]', 'Title found');
    $this->assertField($field_name . '[und][0][url]', 'URL found');

    $input = array(
53
54
      'href' => 'http://example.com/' . $this->randomName(),
      'label' => $this->randomName(),
55
    );
56

57
    // $this->drupalLogin($this->web_user);.
58
59
60
61
62
63
64
    $this->drupalGet('node/add/page');

    $edit = array(
      'title' => $input['label'],
      $field_name . '[und][0][title]' => $input['label'] . " [node:content-type:machine-name]",
      $field_name . '[und][0][url]' => $input['href'],
    );
65
    $this->drupalPost(NULL, $edit, t('Save'));
66
67
    $url = $this->getUrl();

68
    // Change to anonymous user.
69
    $this->drupalLogout();
70
71
72
73
    $this->drupalGet($url);

    $this->assertRaw(l($input['label'] . ' page', $input['href']));
  }
74

75
76
  /**
   * Creates a link field with a static title and an admin-entered token.
77
   *
78
79
   * Creates a node with a link and checks the title value.
   */
80
81
  public function testStaticTokenLinkCreate() {
    // Create field.
82
83
84
    $name = $this->randomName();
    $settings = array(
      'instance[settings][title]' => 'value',
85
86
      'instance[settings][title_value]' => $name . ' [node:content-type:machine-name]',
    );
87
88
    $field_name = $this->createLinkField('page', $settings);

89
    // Create page form.
90
91
92
93
    $this->drupalGet('node/add/page');
    $this->assertField($field_name . '[und][0][url]', 'URL found');

    $input = array(
94
      'href' => 'http://example.com/' . $this->randomName(),
95
96
    );

97
    // $this->drupalLogin($this->web_user);.
98
99
100
101
102
103
    $this->drupalGet('node/add/page');

    $edit = array(
      'title' => $name,
      $field_name . '[und][0][url]' => $input['href'],
    );
104
105
    $this->drupalPost(NULL, $edit, t('Save'));

106
107
    $url = $this->getUrl();

108
    // Change to anonymous user.
109
    $this->drupalLogout();
110
111
112
113
    $this->drupalGet($url);

    $this->assertRaw(l($name . ' page', $input['href']));
  }
114

115
116
  /**
   * Creates a link field with a static title and an admin-entered token.
117
   *
118
119
120
121
122
123
   * Creates a node with a link and checks the title value.
   *
   * Basically, I want to make sure the [title-raw] token works, because it's a
   * token that changes from node to node, where [type]'s always going to be the
   * same.
   */
124
125
  public function testStaticTokenLinkCreate2() {
    // Create field.
126
127
128
    $name = $this->randomName();
    $settings = array(
      'instance[settings][title]' => 'value',
129
130
      'instance[settings][title_value]' => $name . ' [node:title]',
    );
131
132
    $field_name = $this->createLinkField('page', $settings);

133
    // Create page form.
134
135
136
137
    $this->drupalGet('node/add/page');
    $this->assertField($field_name . '[und][0][url]', 'URL found');

    $input = array(
138
      'href' => 'http://example.com/' . $this->randomName(),
139
140
    );

141
    // $this->drupalLogin($this->web_user);.
142
143
144
145
146
147
    $this->drupalGet('node/add/page');

    $edit = array(
      'title' => $name,
      $field_name . '[und][0][url]' => $input['href'],
    );
148
149
    $this->drupalPost(NULL, $edit, t('Save'));

150
151
    $url = $this->getUrl();

152
    // Change to anonymous user.
153
    $this->drupalLogout();
154
155
    $this->drupalGet($url);

156
    $this->assertRaw(l($name . ' ' . $name, $input['href']));
157
  }
158

159
160
161
162
163
  /**
   * This test doesn't seem to actually work, due to lack of 'title' in url.
   *
   * @codingStandardsIgnoreStart
   */
164
  public function xTestLinkWithTitleAttributeTokenUrlForm() {
165
    /* $this->loginWithPermissions($this->permissions);
166
167
    $this->acquireContentTypes(1);
    $field_settings = array(
168
169
170
171
172
173
174
175
176
    'type' => 'link',
    'widget_type' => 'link',
    'type_name' => $this->content_types[0]->name,
    'attributes' => array(
    'class' => '',
    'target' => 'default',
    'rel' => 'nofollow',
    'title' => '',
    ),
177
    );
178

179
180
181
182
183
    $field = $this->createField($field_settings, 0);
    //$this->fail('<pre>'. print_r($field, TRUE) .'</pre>');
    $field_name = $field['field_name'];
    $field_db_info = content_database_info($field);
    $url_type = str_replace('_', '-', $this->content_types[0]->type);
184

185
    $edit = array('attributes[title]' => '['. $field_name .'-url]',
186
187
    'enable_tokens' => TRUE);
    // @codingStandardsIgnoreLine
188
    $this->drupalPost('admin/content/node-type/'. $url_type .'/fields/'. $field['field_name'],
189
    $edit, t('Save field settings'));
190
    $this->assertText(t('Saved field @field_name', array('@field_name' => $field['field_name'])));*/
191
    // @codingStandardsIgnoreEnd
192
193
194
195
    $name = $this->randomName();
    $settings = array(
      'instance[settings][attributes][rel]' => 'nofollow',
    );
196

197
    $field_name = $this->createLinkField('page', $settings);
198

199
    // So, having saved this field_name, let's see if it works...
200
201
    // $this->acquireNodes(1);
    // $node = node_load($this->nodes[0]->nid);
202
    // $this->drupalGet('node/'. $this->nodes[0]->nid);.
203
204
    $edit = array();
    $test_link_url = 'http://www.example.com/test';
205
206
207
    $edit[$field_name . '[und][0][url]'] = $test_link_url;
    $title = 'title_' . $this->randomName(20);
    $edit[$field_name . '[und][0][title]'] = $title;
208
    $edit['title'] = $name;
209

210
211
    $this->drupalGet('node/add/page');
    $this->drupalPost(NULL, $edit, t('Save'));
212

213
    // Make sure we get a new version!
214
    // $node = node_load($this->nodes[0]->nid, NULL, TRUE);.
215
    $this->assertText(t('Basic page @title has been updated.',
216
      array('@title' => $name)));
217

218
    // $this->drupalGet('node/'. $node->nid);.
219
    $this->assertText($title, 'Make sure the link title/text shows');
220
221
    $this->assertRaw(' title="' . $test_link_url . '"', "Do we show the link url as the title attribute?");
    $this->assertNoRaw(' title="[' . $field_name . '-url]"');
222
    $this->assertTrue(module_exists('token'), t('Assure that Token Module is enabled.'));
223
    // $this->fail($this->content);.
224
  }
225

226
  /**
227
228
   * Link With Title Attribute token title form.
   *
229
230
   * If the title of the link is set to the title attribute, then the title
   * attribute isn't supposed to show.
231
232
   *
   * @codingStandardsIgnoreStart
233
   */
234
  public function xTestLinkWithTitleAttributeTokenTitleForm() {
235
    // @codingStandardsIgnoreEnd
236
237
238
239
240
241
242
243
244
245
246
247
248
    $this->loginWithPermissions($this->permissions);
    $this->acquireContentTypes(1);
    $field_settings = array(
      'type' => 'link',
      'widget_type' => 'link',
      'type_name' => $this->content_types[0]->name,
      'attributes' => array(
        'class' => '',
        'target' => 'default',
        'rel' => 'nofollow',
        'title' => '',
      ),
    );
249

250
251
252
    $field = $this->createField($field_settings, 0);
    $field_name = $field['field_name'];
    $url_type = str_replace('_', '-', $this->content_types[0]->type);
253

254
255
256
257
    $edit = array(
      'attributes[title]' => '[' . $field_name . '-title]',
      'enable_tokens' => TRUE,
    );
258

259
    $this->drupalPost('admin/content/node-type/' . $url_type . '/fields/' . $field['field_name'],
260
      $edit, t('Save field settings'));
261
    $this->assertText(t('Saved field @field_name', array('@field_name' => $field['field_name'])));
262

263
264
    // So, having saved this field_name, let's see if it works...
    $this->acquireNodes(1);
265

266
    $this->drupalGet('node/' . $this->nodes[0]->nid);
267

268
    $edit = array();
269
270
271
    $edit[$field['field_name'] . '[0][url]'] = 'http://www.example.com/test';
    $title = 'title_' . $this->randomName(20);
    $edit[$field['field_name'] . '[0][title]'] = $title;
272

273
    $this->drupalPost('node/' . $this->nodes[0]->nid . '/edit', $edit, t('Save'));
274

275
276
277
    // Make sure we get a new version!
    $node = node_load($this->nodes[0]->nid, NULL, TRUE);
    $this->assertText(t('@type @title has been updated.',
278
279
280
281
      array(
        '@title' => $node->title,
        '@type' => $this->content_types[0]->name,
      )));
282

283
    $this->drupalGet('node/' . $node->nid);
284
    $this->assertText($title, 'Make sure the link title/text shows');
285
286
    $this->assertNoRaw(' title="' . $title . '"', "We should not show the link title as the title attribute?");
    $this->assertNoRaw(' title="[' . $field_name . '-title]"');
287
    // $this->fail($this->content);.
288
289
290
  }

  /**
291
   * Trying to set the url to contain a token.
292
   */
293
  public function xTestUserTokenLinkCreateInUrl() {
294
295
296
297
298
299
    $this->web_user = $this->drupalCreateUser(array(
      'administer content types',
      'administer fields',
      'access content',
      'create page content',
    ));
300
    $this->drupalLogin($this->web_user);
301

302
    // Create field.
303
304
305
306
307
308
309
310
311
312
    $name = strtolower($this->randomName());
    $edit = array(
      '_add_new_field[label]' => $name,
      '_add_new_field[field_name]' => $name,
      '_add_new_field[type]' => 'link',
      '_add_new_field[widget_type]' => 'link',
    );
    $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
    $this->drupalPost(NULL, array(
      'title' => 'required',
313
314
      'enable_tokens' => 1,
    ), t('Save field settings'));
315
316
317
318

    // Is field created?
    $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');

319
    // Create page form.
320
321
322
323
324
325
    $this->drupalGet('node/add/page');
    $field_name = 'field_' . $name;
    $this->assertField($field_name . '[0][title]', 'Title found');
    $this->assertField($field_name . '[0][url]', 'URL found');

    $input = array(
326
327
      'href' => 'http://example.com/' . $this->randomName(),
      'label' => $this->randomName(),
328
    );
329

330
    $this->drupalLogin($this->web_user);
331
332
333
334
335
336
337
    $this->drupalGet('node/add/page');

    $edit = array(
      'title' => $input['label'],
      $field_name . '[0][title]' => $input['label'],
      $field_name . '[0][url]' => $input['href'] . "/[type]",
    );
338
    $this->drupalPost(NULL, $edit, t('Save'));
339
340
    $url = $this->getUrl();

341
    // Change to anonymous user.
342
    $this->drupalLogout();
343
344
    $this->drupalGet($url);

345
    $this->assertRaw(l($input['label'], $input['href'] . '/page'));
346
    // $this->fail($this->content);.
347
  }
348

349
  /**
350
   * Trying to set the url to contain a token.
351
   */
352
  public function xTestUserTokenLinkCreateInUrl2() {
353
354
355
356
357
358
    $this->web_user = $this->drupalCreateUser(array(
      'administer content types',
      'administer fields',
      'access content',
      'create page content',
    ));
359
    $this->drupalLogin($this->web_user);
360

361
    // Create field.
362
363
364
365
366
367
368
369
370
371
    $name = strtolower($this->randomName());
    $edit = array(
      '_add_new_field[label]' => $name,
      '_add_new_field[field_name]' => $name,
      '_add_new_field[type]' => 'link',
      '_add_new_field[widget_type]' => 'link',
    );
    $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
    $this->drupalPost(NULL, array(
      'title' => 'required',
372
373
      'enable_tokens' => 1,
    ), t('Save field settings'));
374
375
376
377

    // Is field created?
    $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');

378
    // Create page form.
379
380
381
382
383
384
    $this->drupalGet('node/add/page');
    $field_name = 'field_' . $name;
    $this->assertField($field_name . '[0][title]', 'Title found');
    $this->assertField($field_name . '[0][url]', 'URL found');

    $input = array(
385
386
      'href' => 'http://example.com/' . $this->randomName(),
      'label' => $this->randomName(),
387
    );
388

389
    $this->drupalLogin($this->web_user);
390
391
392
393
394
395
396
    $this->drupalGet('node/add/page');

    $edit = array(
      'title' => $input['label'],
      $field_name . '[0][title]' => $input['label'],
      $field_name . '[0][url]' => $input['href'] . "/[author-uid]",
    );
397
    $this->drupalPost(NULL, $edit, t('Save'));
398
399
    $url = $this->getUrl();

400
    // Change to anonymous user.
401
    $this->drupalLogout();
402
403
    $this->drupalGet($url);

404
    $this->assertRaw(l($input['label'], $input['href'] . '/' . $this->web_user->uid));
405
  }
406

407
  /**
408
409
410
411
412
413
   * CRUD Title Only Title No Link.
   *
   * Test that if you have a title and no url on a field which does not have
   * tokens enabled, that the title is sanitized once.
   *
   * @codingStandardsIgnoreStart
414
   */
415
416
417
418
419
420
421
422
423
424
  public function testCRUDTitleOnlyTitleNoLink2() {
    //@codingStandardsIgnoreEnd

    $this->web_user = $this->drupalCreateUser(array(
      'administer content types',
      'administer fields',
      'access content',
      'create page content',
    ));

425
426
    $this->drupalLogin($this->web_user);

427
    // Create field.
428
    $name = strtolower($this->randomName());
429
    $field_name = 'field_' . $name;
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
    $edit = array(
      'fields[_add_new_field][label]' => $name,
      'fields[_add_new_field][field_name]' => $name,
      'fields[_add_new_field][type]' => 'link_field',
      'fields[_add_new_field][widget_type]' => 'link_field',
    );
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
    $this->drupalPost(NULL, array(), t('Save field settings'));
    $this->drupalPost(NULL, array(
      'instance[settings][url]' => 1,
      'instance[settings][enable_tokens]' => 0,
    ), t('Save settings'));

    // Is field created?
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
445
446

    // Create page form.
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    $this->drupalGet('node/add/page');
    $this->assertField($field_name . '[und][0][url]', 'URL found');

    $input = array(
      'title' => 'This & That',
      'href' => '',
    );

    $edit = array(
      'title' => $name,
      $field_name . '[und][0][title]' => $input['title'],
      $field_name . '[und][0][url]' => $input['href'],
    );
    $this->drupalPost(NULL, $edit, t('Save'));

    $url = $this->getUrl();
463
464

    // Change to anonymous user.
465
466
467
468
469
    $this->drupalLogout();
    $this->drupalGet($url);

    $this->assertRaw('This &amp; That');
  }
470

471
}