Projet

Général

Profil

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

root / drupal7 / sites / all / modules / link / tests / link.token.test @ 39a181a4

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains simpletests making sure token integration works.
6
 */
7

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

    
13
  /**
14
   * Get Info.
15
   */
16
  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
  /**
26
   * Setup.
27
   */
28
  public function setUp($modules = array()) {
29
    parent::setUp(array('token'));
30
  }
31

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

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

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

    
56
    // $this->drupalLogin($this->web_user);.
57
    $this->drupalGet('node/add/page');
58

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

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

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

    
74
  /**
75
   * Creates a link field with a static title and an admin-entered token.
76
   *
77
   * Creates a node with a link and checks the title value.
78
   */
79
  public function testStaticTokenLinkCreate() {
80

    
81
    // Create field.
82
    $name = $this->randomName();
83
    $settings = array(
84
      'instance[settings][title]' => 'value',
85
      'instance[settings][title_value]' => $name . ' [node:content-type:machine-name]',
86
    );
87
    $field_name = $this->createLinkField('page', $settings);
88

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

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

    
97
    // $this->drupalLogin($this->web_user);.
98
    $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
    // Change to anonymous user.
109
    $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
   *
118
   * 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
  public function testStaticTokenLinkCreate2() {
125

    
126
    // Create field.
127
    $name = $this->randomName();
128
    $settings = array(
129
      'instance[settings][title]' => 'value',
130
      'instance[settings][title_value]' => $name . ' [node:title]',
131
    );
132
    $field_name = $this->createLinkField('page', $settings);
133

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
219
    // $this->drupalGet('node/'. $node->nid);.
220
    $this->assertText($title, 'Make sure the link title/text shows');
221
    $this->assertRaw(' title="' . $test_link_url . '"', "Do we show the link url as the title attribute?");
222
    $this->assertNoRaw(' title="[' . $field_name . '-url]"');
223
    $this->assertTrue(module_exists('token'), t('Assure that Token Module is enabled.'));
224
    // $this->fail($this->content);.
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
231
   * attribute isn't supposed to show.
232
   *
233
   * @codingStandardsIgnoreStart
234
   */
235
  public function _test_Link_With_Title_Attribute_token_title_form() {
236
    // @codingStandardsIgnoreEnd
237
    $this->loginWithPermissions($this->permissions);
238
    $this->acquireContentTypes(1);
239
    $field_settings = array(
240
      'type' => 'link',
241
      'widget_type' => 'link',
242
      'type_name' => $this->content_types[0]->name,
243
      'attributes' => array(
244
        'class' => '',
245
        'target' => 'default',
246
        'rel' => 'nofollow',
247
        'title' => '',
248
      ),
249
    );
250

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

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

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

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

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

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

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

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

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

    
291
  /**
292
   * Trying to set the url to contain a token.
293
   *
294
   * @codingStandardsIgnoreStart
295
   */
296
  public function _testUserTokenLinkCreateInURL() {
297
    //@codingStandardsIgnoreEnd
298

    
299
    $this->web_user = $this->drupalCreateUser(array(
300
      'administer content types',
301
      'administer fields',
302
      'access content',
303
      'create page content',
304
    ));
305
    $this->drupalLogin($this->web_user);
306

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

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

    
324
    // Create page form.
325
    $this->drupalGet('node/add/page');
326
    $field_name = 'field_' . $name;
327
    $this->assertField($field_name . '[0][title]', 'Title found');
328
    $this->assertField($field_name . '[0][url]', 'URL found');
329

    
330
    $input = array(
331
      'href' => 'http://example.com/' . $this->randomName(),
332
      'label' => $this->randomName(),
333
    );
334

    
335
    $this->drupalLogin($this->web_user);
336
    $this->drupalGet('node/add/page');
337

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

    
346
    // Change to anonymous user.
347
    $this->drupalLogout();
348
    $this->drupalGet($url);
349

    
350
    $this->assertRaw(l($input['label'], $input['href'] . '/page'));
351
    // $this->fail($this->content);.
352
  }
353

    
354
  /**
355
   * Trying to set the url to contain a token.
356
   *
357
   * @codingStandardsIgnoreStart
358
   */
359
  public function _testUserTokenLinkCreateInURL2() {
360
    // @codingStandardsIgnoreEnd
361

    
362
    $this->web_user = $this->drupalCreateUser(array(
363
      'administer content types',
364
      'administer fields',
365
      'access content',
366
      'create page content',
367
    ));
368
    $this->drupalLogin($this->web_user);
369

    
370
    // Create field.
371
    $name = strtolower($this->randomName());
372
    $edit = array(
373
      '_add_new_field[label]' => $name,
374
      '_add_new_field[field_name]' => $name,
375
      '_add_new_field[type]' => 'link',
376
      '_add_new_field[widget_type]' => 'link',
377
    );
378
    $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
379
    $this->drupalPost(NULL, array(
380
      'title' => 'required',
381
      'enable_tokens' => 1,
382
    ), t('Save field settings'));
383

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

    
387
    // Create page form.
388
    $this->drupalGet('node/add/page');
389
    $field_name = 'field_' . $name;
390
    $this->assertField($field_name . '[0][title]', 'Title found');
391
    $this->assertField($field_name . '[0][url]', 'URL found');
392

    
393
    $input = array(
394
      'href' => 'http://example.com/' . $this->randomName(),
395
      'label' => $this->randomName(),
396
    );
397

    
398
    $this->drupalLogin($this->web_user);
399
    $this->drupalGet('node/add/page');
400

    
401
    $edit = array(
402
      'title' => $input['label'],
403
      $field_name . '[0][title]' => $input['label'],
404
      $field_name . '[0][url]' => $input['href'] . "/[author-uid]",
405
    );
406
    $this->drupalPost(NULL, $edit, t('Save'));
407
    $url = $this->getUrl();
408

    
409
    // Change to anonymous user.
410
    $this->drupalLogout();
411
    $this->drupalGet($url);
412

    
413
    $this->assertRaw(l($input['label'], $input['href'] . '/' . $this->web_user->uid));
414
  }
415

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

    
427
    $this->web_user = $this->drupalCreateUser(array(
428
      'administer content types',
429
      'administer fields',
430
      'access content',
431
      'create page content',
432
    ));
433

    
434
    $this->drupalLogin($this->web_user);
435

    
436
    // Create field.
437
    $name = strtolower($this->randomName());
438
    $field_name = 'field_' . $name;
439
    $edit = array(
440
      'fields[_add_new_field][label]' => $name,
441
      'fields[_add_new_field][field_name]' => $name,
442
      'fields[_add_new_field][type]' => 'link_field',
443
      'fields[_add_new_field][widget_type]' => 'link_field',
444
    );
445
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
446
    $this->drupalPost(NULL, array(), t('Save field settings'));
447
    $this->drupalPost(NULL, array(
448
      'instance[settings][url]' => 1,
449
      'instance[settings][enable_tokens]' => 0,
450
    ), t('Save settings'));
451

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

    
455
    // Create page form.
456
    $this->drupalGet('node/add/page');
457
    $this->assertField($field_name . '[und][0][url]', 'URL found');
458

    
459
    $input = array(
460
      'title' => 'This & That',
461
      'href' => '',
462
    );
463

    
464
    $edit = array(
465
      'title' => $name,
466
      $field_name . '[und][0][title]' => $input['title'],
467
      $field_name . '[und][0][url]' => $input['href'],
468
    );
469
    $this->drupalPost(NULL, $edit, t('Save'));
470

    
471
    $url = $this->getUrl();
472

    
473
    // Change to anonymous user.
474
    $this->drupalLogout();
475
    $this->drupalGet($url);
476

    
477
    $this->assertRaw('This &amp; That');
478
  }
479

    
480
}