Projet

Général

Profil

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

root / drupal7 / sites / all / modules / token / token.test @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Test integration for the token module.
6
 */
7

    
8
/**
9
 * Helper test class with some added functions for testing.
10
 */
11
class TokenTestHelper extends DrupalWebTestCase {
12
  protected $profile = 'testing';
13

    
14
  public function setUp($modules = array()) {
15
    $modules[] = 'path';
16
    $modules[] = 'token';
17
    $modules[] = 'token_test';
18
    parent::setUp($modules);
19

    
20
    variable_set('clean_url', 1);
21
  }
22

    
23
  function assertToken($type, array $data, $token, $expected, array $options = array()) {
24
    return $this->assertTokens($type, $data, array($token => $expected), $options);
25
  }
26

    
27
  function assertTokens($type, array $data, array $tokens, array $options = array()) {
28
    $input = $this->mapTokenNames($type, array_keys($tokens));
29
    $replacements = token_generate($type, $input, $data, $options);
30
    foreach ($tokens as $name => $expected) {
31
      $token = $input[$name];
32
      if (!isset($expected)) {
33
        $this->assertTrue(!isset($values[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
34
      }
35
      elseif (!isset($replacements[$token])) {
36
        $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
37
      }
38
      elseif (!empty($options['regex'])) {
39
        $this->assertTrue(preg_match('/^' . $expected . '$/', $replacements[$token]), t("Token value for @token was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
40
      }
41
      else {
42
        $this->assertIdentical($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
43
      }
44
    }
45

    
46
    return $replacements;
47
  }
48

    
49
  function mapTokenNames($type, array $tokens = array()) {
50
    $return = array();
51
    foreach ($tokens as $token) {
52
      $return[$token] = "[$type:$token]";
53
    }
54
    return $return;
55
  }
56

    
57
  function assertNoTokens($type, array $data, array $tokens, array $options = array()) {
58
    $input = $this->mapTokenNames($type, $tokens);
59
    $replacements = token_generate($type, $input, $data, $options);
60
    foreach ($tokens as $name) {
61
      $token = $input[$name];
62
      $this->assertTrue(!isset($replacements[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
63
    }
64
    return $values;
65
  }
66

    
67
  function saveAlias($source, $alias, $language = LANGUAGE_NONE) {
68
    $alias = array(
69
      'source' => $source,
70
      'alias' => $alias,
71
      'language' => $language,
72
    );
73
    path_save($alias);
74
    return $alias;
75
  }
76

    
77
  function saveEntityAlias($entity_type, $entity, $alias, $language = LANGUAGE_NONE) {
78
    $uri = entity_uri($entity_type, $entity);
79
    return $this->saveAlias($uri['path'], $alias, $language);
80
  }
81

    
82
  /**
83
   * Make a page request and test for token generation.
84
   */
85
  function assertPageTokens($url, array $tokens, array $data = array(), array $options = array()) {
86
    if (empty($tokens)) {
87
      return TRUE;
88
    }
89

    
90
    $token_page_tokens = array(
91
      'tokens' => $tokens,
92
      'data' => $data,
93
      'options' => $options,
94
    );
95
    variable_set('token_page_tokens', $token_page_tokens);
96

    
97
    $options += array('url_options' => array());
98
    $this->drupalGet($url, $options['url_options']);
99
    $this->refreshVariables();
100
    $result = variable_get('token_page_tokens', array());
101

    
102
    if (!isset($result['values']) || !is_array($result['values'])) {
103
      return $this->fail('Failed to generate tokens.');
104
    }
105

    
106
    foreach ($tokens as $token => $expected) {
107
      if (!isset($expected)) {
108
        $this->assertTrue(!isset($result['values'][$token]) || $result['values'][$token] === $token, t("Token value for @token was not generated.", array('@token' => $token)));
109
      }
110
      elseif (!isset($result['values'][$token])) {
111
        $this->fail(t('Failed to generate token @token.', array('@token' => $token)));
112
      }
113
      else {
114
        $this->assertIdentical($result['values'][$token], (string) $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@token' => $token, '@actual' => $result['values'][$token], '@expected' => $expected)));
115
      }
116
    }
117
  }
118
}
119

    
120
class TokenUnitTestCase extends TokenTestHelper {
121
  public static function getInfo() {
122
    return array(
123
      'name' => 'Token unit tests',
124
      'description' => 'Test basic, low-level token functions.',
125
      'group' => 'Token',
126
    );
127
  }
128

    
129
  /**
130
   * Test token_get_invalid_tokens() and token_get_invalid_tokens_by_context().
131
   */
132
  public function testGetInvalidTokens() {
133
    $tests = array();
134
    $tests[] = array(
135
      'valid tokens' => array(
136
        '[node:title]',
137
        '[node:created:short]',
138
        '[node:created:custom:invalid]',
139
        '[node:created:custom:mm-YYYY]',
140
        '[site:name]',
141
        '[site:slogan]',
142
        '[current-date:short]',
143
        '[current-user:uid]',
144
        '[current-user:ip-address]',
145
      ),
146
      'invalid tokens' => array(
147
        '[node:title:invalid]',
148
        '[node:created:invalid]',
149
        '[node:created:short:invalid]',
150
        '[invalid:title]',
151
        '[site:invalid]',
152
        '[user:ip-address]',
153
        '[user:uid]',
154
        '[comment:cid]',
155
        // Deprecated tokens
156
        '[node:tnid]',
157
        '[node:type]',
158
        '[node:type-name]',
159
        '[date:short]',
160
      ),
161
      'types' => array('node'),
162
    );
163
    $tests[] = array(
164
      'valid tokens' => array(
165
        '[node:title]',
166
        '[node:created:short]',
167
        '[node:created:custom:invalid]',
168
        '[node:created:custom:mm-YYYY]',
169
        '[site:name]',
170
        '[site:slogan]',
171
        '[user:uid]',
172
        '[current-date:short]',
173
        '[current-user:uid]',
174
      ),
175
      'invalid tokens' => array(
176
        '[node:title:invalid]',
177
        '[node:created:invalid]',
178
        '[node:created:short:invalid]',
179
        '[invalid:title]',
180
        '[site:invalid]',
181
        '[user:ip-address]',
182
        '[comment:cid]',
183
        // Deprecated tokens
184
        '[node:tnid]',
185
        '[node:type]',
186
        '[node:type-name]',
187
      ),
188
      'types' => array('all'),
189
    );
190

    
191
    foreach ($tests as $test) {
192
      $tokens = array_merge($test['valid tokens'], $test['invalid tokens']);
193
      shuffle($tokens);
194

    
195
      $invalid_tokens = token_get_invalid_tokens_by_context(implode(' ', $tokens), $test['types']);
196

    
197
      sort($invalid_tokens);
198
      sort($test['invalid tokens']);
199
      $this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly: ' . implode(', ', $invalid_tokens));
200
    }
201
  }
202
}
203

    
204
class TokenURLTestCase extends TokenTestHelper {
205
  public static function getInfo() {
206
    return array(
207
      'name' => 'URL token tests',
208
      'description' => 'Test the URL tokens.',
209
      'group' => 'Token',
210
    );
211
  }
212

    
213
  public function setUp($modules = array()) {
214
    parent::setUp($modules);
215
    $this->saveAlias('node/1', 'first-node');
216
  }
217

    
218
  function testURLTokens() {
219
    $tokens = array(
220
      'absolute' => 'http://example.com/first-node',
221
      'relative' => base_path() . 'first-node',
222
      'path' => 'first-node',
223
      'brief' => 'example.com/first-node',
224
      'args:value:0' => 'first-node',
225
      'args:value:1' => NULL,
226
      'args:value:N' => NULL,
227
      'unaliased' => 'http://example.com/node/1',
228
      'unaliased:relative' => base_path() . 'node/1',
229
      'unaliased:path' => 'node/1',
230
      'unaliased:brief' => 'example.com/node/1',
231
      'unaliased:args:value:0' => 'node',
232
      'unaliased:args:value:1' => '1',
233
      'unaliased:args:value:2' => NULL,
234
      // Deprecated tokens.
235
      'alias' => 'first-node',
236
    );
237
    $this->assertTokens('url', array('path' => 'node/1', 'options' => array('base_url' => 'http://example.com')), $tokens);
238
  }
239
}
240

    
241
class TokenCommentTestCase extends TokenTestHelper {
242
  public static function getInfo() {
243
    return array(
244
      'name' => 'Comment token tests',
245
      'description' => 'Test the comment tokens.',
246
      'group' => 'Token',
247
    );
248
  }
249

    
250
  public function setUp($modules = array()) {
251
    $modules[] = 'comment';
252
    parent::setUp($modules);
253
  }
254

    
255
  function testCommentTokens() {
256
    $node = $this->drupalCreateNode(array('comment' => COMMENT_NODE_OPEN));
257

    
258
    $parent_comment = new stdClass;
259
    $parent_comment->nid = $node->nid;
260
    $parent_comment->pid = 0;
261
    $parent_comment->cid = NULL;
262
    $parent_comment->uid = 0;
263
    $parent_comment->name = 'anonymous user';
264
    $parent_comment->mail = 'anonymous@example.com';
265
    $parent_comment->subject = $this->randomName();
266
    $parent_comment->timestamp = mt_rand($node->created, REQUEST_TIME);
267
    $parent_comment->language = LANGUAGE_NONE;
268
    $parent_comment->body[LANGUAGE_NONE][0] = $this->randomName();
269
    comment_save($parent_comment);
270

    
271
    $tokens = array(
272
      'url' => url('comment/' . $parent_comment->cid, array('fragment' => 'comment-' . $parent_comment->cid, 'absolute' => TRUE)),
273
      'url:absolute' => url('comment/' . $parent_comment->cid, array('fragment' => 'comment-' . $parent_comment->cid, 'absolute' => TRUE)),
274
      'url:relative' => url('comment/' . $parent_comment->cid, array('fragment' => 'comment-' . $parent_comment->cid, 'absolute' => FALSE)),
275
      'url:path' => 'comment/' . $parent_comment->cid,
276
      'parent:url:absolute' => NULL,
277
    );
278
    $this->assertTokens('comment', array('comment' => $parent_comment), $tokens);
279

    
280
    $comment = new stdClass();
281
    $comment->nid = $node->nid;
282
    $comment->pid = $parent_comment->cid;
283
    $comment->cid = NULL;
284
    $comment->uid = 1;
285
    $comment->subject = $this->randomName();
286
    $comment->timestamp = mt_rand($parent_comment->created, REQUEST_TIME);
287
    $comment->language = LANGUAGE_NONE;
288
    $comment->body[LANGUAGE_NONE][0] = $this->randomName();
289
    comment_save($comment);
290

    
291
    $tokens = array(
292
      'url' => url('comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid, 'absolute' => TRUE)),
293
      'url:absolute' => url('comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid, 'absolute' => TRUE)),
294
      'url:relative' => url('comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid, 'absolute' => FALSE)),
295
      'url:path' => 'comment/' . $comment->cid,
296
      'parent:url:absolute' => url('comment/' . $parent_comment->cid, array('fragment' => 'comment-' . $parent_comment->cid, 'absolute' => TRUE)),
297
    );
298
    $this->assertTokens('comment', array('comment' => $comment), $tokens);
299
  }
300
}
301

    
302
class TokenNodeTestCase extends TokenTestHelper {
303
  protected $profile = 'standard';
304

    
305
  public static function getInfo() {
306
    return array(
307
      'name' => 'Node and content type token tests',
308
      'description' => 'Test the node and content type tokens.',
309
      'group' => 'Token',
310
    );
311
  }
312

    
313
  function testNodeTokens() {
314
    $source_node = $this->drupalCreateNode(array('log' => $this->randomName(), 'path' => array('alias' => 'content/source-node')));
315
    $tokens = array(
316
      'source' => NULL,
317
      'source:nid' => NULL,
318
      'log' => $source_node->log,
319
      'url:path' => 'content/source-node',
320
      'url:absolute' => url("node/{$source_node->nid}", array('absolute' => TRUE)),
321
      'url:relative' => url("node/{$source_node->nid}", array('absolute' => FALSE)),
322
      'url:unaliased:path' => "node/{$source_node->nid}",
323
      'content-type' => 'Basic page',
324
      'content-type:name' => 'Basic page',
325
      'content-type:machine-name' => 'page',
326
      'content-type:description' => "Use <em>basic pages</em> for your static content, such as an 'About us' page.",
327
      'content-type:node-count' => 1,
328
      'content-type:edit-url' => url('admin/structure/types/manage/page', array('absolute' => TRUE)),
329
      // Deprecated tokens.
330
      'tnid' => 0,
331
      'type' => 'page',
332
      'type-name' => 'Basic page',
333
      'url:alias' => 'content/source-node',
334
    );
335
    $this->assertTokens('node', array('node' => $source_node), $tokens);
336

    
337
    $translated_node = $this->drupalCreateNode(array('tnid' => $source_node->nid, 'type' => 'article'));
338
    $tokens = array(
339
      'source' => $source_node->title,
340
      'source:nid' => $source_node->nid,
341
      'log' => '',
342
      'url:path' => "node/{$translated_node->nid}",
343
      'url:absolute' => url("node/{$translated_node->nid}", array('absolute' => TRUE)),
344
      'url:relative' => url("node/{$translated_node->nid}", array('absolute' => FALSE)),
345
      'url:unaliased:path' => "node/{$translated_node->nid}",
346
      'content-type' => 'Article',
347
      'content-type:name' => 'Article',
348
      'content-type:machine-name' => 'article',
349
      'content-type:description' => "Use <em>articles</em> for time-sensitive content like news, press releases or blog posts.",
350
      'content-type:node-count' => 1,
351
      'content-type:edit-url' => url('admin/structure/types/manage/article', array('absolute' => TRUE)),
352
      // Deprecated tokens.
353
      'type' => 'article',
354
      'type-name' => 'Article',
355
      'tnid' => $source_node->nid,
356
      'url:alias' => "node/{$translated_node->nid}",
357
    );
358
    $this->assertTokens('node', array('node' => $translated_node), $tokens);
359
  }
360
}
361

    
362
class TokenMenuTestCase extends TokenTestHelper {
363
  public static function getInfo() {
364
    return array(
365
      'name' => 'Menu link and menu token tests',
366
      'description' => 'Test the menu tokens.',
367
      'group' => 'Token',
368
    );
369
  }
370

    
371
  public function setUp($modules = array()) {
372
    $modules[] = 'menu';
373
    parent::setUp($modules);
374
  }
375

    
376
  function testMenuTokens() {
377
    // Add a root link.
378
    $root_link = array(
379
      'link_path' => 'root',
380
      'link_title' => 'Root link',
381
      'menu_name' => 'main-menu',
382
    );
383
    menu_link_save($root_link);
384

    
385
    // Add another link with the root link as the parent
386
    $parent_link = array(
387
      'link_path' => 'root/parent',
388
      'link_title' => 'Parent link',
389
      'menu_name' => 'main-menu',
390
      'plid' => $root_link['mlid'],
391
    );
392
    menu_link_save($parent_link);
393

    
394
    // Test menu link tokens.
395
    $tokens = array(
396
      'mlid' => $parent_link['mlid'],
397
      'title' => 'Parent link',
398
      'menu' => 'Main menu',
399
      'menu:name' => 'Main menu',
400
      'menu:machine-name' => 'main-menu',
401
      'menu:description' => 'The <em>Main</em> menu is used on many sites to show the major sections of the site, often in a top navigation bar.',
402
      'menu:menu-link-count' => 2,
403
      'menu:edit-url' => url("admin/structure/menu/manage/main-menu", array('absolute' => TRUE)),
404
      'url' => url('root/parent', array('absolute' => TRUE)),
405
      'url:absolute' => url('root/parent', array('absolute' => TRUE)),
406
      'url:relative' => url('root/parent', array('absolute' => FALSE)),
407
      'url:path' => 'root/parent',
408
      'url:alias' => 'root/parent',
409
      'edit-url' => url("admin/structure/menu/item/{$parent_link['mlid']}/edit", array('absolute' => TRUE)),
410
      'parent' => 'Root link',
411
      'parent:mlid' => $root_link['mlid'],
412
      'parent:title' => 'Root link',
413
      'parent:menu' => 'Main menu',
414
      'parent:parent' => NULL,
415
      'parents' => 'Root link',
416
      'parents:count' => 1,
417
      'parents:keys' => $root_link['mlid'],
418
      'root' => 'Root link',
419
      'root:mlid' => $root_link['mlid'],
420
      'root:parent' => NULL,
421
      'root:root' => NULL,
422
    );
423
    $this->assertTokens('menu-link', array('menu-link' => $parent_link), $tokens);
424

    
425
    // Add a node menu link
426
    $node_link = array(
427
      'enabled' => TRUE,
428
      'link_title' => 'Node link',
429
      'plid' => $parent_link['mlid'],
430
      'customized' => 0,
431
      'description' => '',
432
    );
433
    $node = $this->drupalCreateNode(array('menu' => $node_link));
434

    
435
    // Test [node:menu] tokens.
436
    $tokens = array(
437
      'menu-link' => 'Node link',
438
      'menu-link:mlid' => $node->menu['mlid'],
439
      'menu-link:title' => 'Node link',
440
      'menu-link:menu' => 'Main menu',
441
      'menu-link:url' => url('node/' . $node->nid, array('absolute' => TRUE)),
442
      'menu-link:url:path' => 'node/' . $node->nid,
443
      'menu-link:edit-url' => url("admin/structure/menu/item/{$node->menu['mlid']}/edit", array('absolute' => TRUE)),
444
      'menu-link:parent' => 'Parent link',
445
      'menu-link:parent:mlid' => $node->menu['plid'],
446
      'menu-link:parent:mlid' => $parent_link['mlid'],
447
      'menu-link:parents' => 'Root link, Parent link',
448
      'menu-link:parents:count' => 2,
449
      'menu-link:parents:keys' => $root_link['mlid'] . ', ' . $parent_link['mlid'],
450
      'menu-link:root' => 'Root link',
451
      'menu-link:root:mlid' => $root_link['mlid'],
452
    );
453
    $this->assertTokens('node', array('node' => $node), $tokens);
454

    
455
    // Reload the node which will not have $node->menu defined and re-test.
456
    $loaded_node = node_load($node->nid);
457
    $this->assertTokens('node', array('node' => $loaded_node), $tokens);
458

    
459
    // Regression test for http://drupal.org/node/1317926 to ensure the
460
    // original node object is not changed when calling menu_node_prepare().
461
    $this->assertTrue(!isset($loaded_node->menu), t('The $node->menu property was not modified during token replacement.'), 'Regression');
462
  }
463
}
464

    
465
class TokenTaxonomyTestCase extends TokenTestHelper {
466
  protected $vocab;
467

    
468
  public static function getInfo() {
469
    return array(
470
      'name' => 'Taxonomy and vocabulary token tests',
471
      'description' => 'Test the taxonomy tokens.',
472
      'group' => 'Token',
473
    );
474
  }
475

    
476
  public function setUp($modules = array()) {
477
    $modules[] = 'taxonomy';
478
    parent::setUp($modules);
479

    
480
    // Create the default tags vocabulary.
481
    $vocabulary = (object) array(
482
      'name' => 'Tags',
483
      'machine_name' => 'tags',
484
    );
485
    taxonomy_vocabulary_save($vocabulary);
486
    $this->vocab = $vocabulary;
487
  }
488

    
489
  /**
490
   * Test the additional taxonomy term tokens.
491
   */
492
  function testTaxonomyTokens() {
493
    $root_term = $this->addTerm($this->vocab, array('name' => 'Root term', 'path' => array('alias' => 'root-term')));
494
    $tokens = array(
495
      'url' => url("taxonomy/term/{$root_term->tid}", array('absolute' => TRUE)),
496
      'url:absolute' => url("taxonomy/term/{$root_term->tid}", array('absolute' => TRUE)),
497
      'url:relative' => url("taxonomy/term/{$root_term->tid}", array('absolute' => FALSE)),
498
      'url:path' => 'root-term',
499
      'url:unaliased:path' => "taxonomy/term/{$root_term->tid}",
500
      'edit-url' => url("taxonomy/term/{$root_term->tid}/edit", array('absolute' => TRUE)),
501
      'parents' => NULL,
502
      'parents:count' => NULL,
503
      'parents:keys' => NULL,
504
      'root' => NULL,
505
      // Deprecated tokens
506
      'url:alias' => 'root-term',
507
    );
508
    $this->assertTokens('term', array('term' => $root_term), $tokens);
509

    
510
    $parent_term = $this->addTerm($this->vocab, array('name' => 'Parent term', 'parent' => $root_term->tid));
511
    $tokens = array(
512
      'url' => url("taxonomy/term/{$parent_term->tid}", array('absolute' => TRUE)),
513
      'url:absolute' => url("taxonomy/term/{$parent_term->tid}", array('absolute' => TRUE)),
514
      'url:relative' => url("taxonomy/term/{$parent_term->tid}", array('absolute' => FALSE)),
515
      'url:path' => "taxonomy/term/{$parent_term->tid}",
516
      'url:unaliased:path' => "taxonomy/term/{$parent_term->tid}",
517
      'edit-url' => url("taxonomy/term/{$parent_term->tid}/edit", array('absolute' => TRUE)),
518
      'parents' => 'Root term',
519
      'parents:count' => 1,
520
      'parents:keys' => $root_term->tid,
521
      'root' => check_plain($root_term->name),
522
      'root:tid' => $root_term->tid,
523
      // Deprecated tokens
524
      'url:alias' => "taxonomy/term/{$parent_term->tid}",
525
    );
526
    $this->assertTokens('term', array('term' => $parent_term), $tokens);
527

    
528
    $term = $this->addTerm($this->vocab, array('name' => 'Test term', 'parent' => $parent_term->tid));
529
    $tokens = array(
530
      'parents' => 'Root term, Parent term',
531
      'parents:count' => 2,
532
      'parents:keys' => implode(', ', array($root_term->tid, $parent_term->tid)),
533
    );
534
    $this->assertTokens('term', array('term' => $term), $tokens);
535
  }
536

    
537
  /**
538
   * Test the additional vocabulary tokens.
539
   */
540
  function testVocabularyTokens() {
541
    $vocabulary = $this->vocab;
542
    $tokens = array(
543
      'machine-name' => 'tags',
544
      'edit-url' => url("admin/structure/taxonomy/{$vocabulary->machine_name}/edit", array('absolute' => TRUE)),
545
    );
546
    $this->assertTokens('vocabulary', array('vocabulary' => $vocabulary), $tokens);
547
  }
548

    
549
  function addVocabulary(array $vocabulary = array()) {
550
    $vocabulary += array(
551
      'name' => drupal_strtolower($this->randomName(5)),
552
      'nodes' => array('article' => 'article'),
553
    );
554
    $vocabulary = (object) $vocabulary;
555
    taxonomy_vocabulary_save($vocabulary);
556
    return $vocabulary;
557
  }
558

    
559
  function addTerm(stdClass $vocabulary, array $term = array()) {
560
    $term += array(
561
      'name' => drupal_strtolower($this->randomName(5)),
562
      'vid' => $vocabulary->vid,
563
    );
564
    $term = (object) $term;
565
    taxonomy_term_save($term);
566
    return $term;
567
  }
568
}
569

    
570
class TokenUserTestCase extends TokenTestHelper {
571
  protected $account = NULL;
572

    
573
  public static function getInfo() {
574
    return array(
575
      'name' => 'User token tests',
576
      'description' => 'Test the user tokens.',
577
      'group' => 'Token',
578
    );
579
  }
580

    
581
  public function setUp($modules = array()) {
582
    parent::setUp($modules);
583

    
584
    // Enable user pictures.
585
    variable_set('user_pictures', 1);
586
    variable_set('user_picture_file_size', '');
587

    
588
    // Set up the pictures directory.
589
    $picture_path = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
590
    if (!file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY)) {
591
      $this->fail('Could not create directory ' . $picture_path . '.');
592
    }
593

    
594
    $this->account = $this->drupalCreateUser(array('administer users'));
595
    $this->drupalLogin($this->account);
596
  }
597

    
598
  function testUserTokens() {
599
    // Add a user picture to the account.
600
    $image = current($this->drupalGetTestFiles('image'));
601
    $edit = array('files[picture_upload]' => drupal_realpath($image->uri));
602
    $this->drupalPost('user/' . $this->account->uid . '/edit', $edit, t('Save'));
603

    
604
    // Load actual user data from database.
605
    $this->account = user_load($this->account->uid, TRUE);
606
    $this->assertTrue(!empty($this->account->picture->fid), 'User picture uploaded.');
607

    
608
    $user_tokens = array(
609
      'picture' => theme('user_picture', array('account' => $this->account)),
610
      'picture:fid' => $this->account->picture->fid,
611
      'picture:size-raw' => 125,
612
      'ip-address' => NULL,
613
      'roles' => implode(', ', $this->account->roles),
614
      'roles:keys' => implode(', ', array_keys($this->account->roles)),
615
    );
616
    $this->assertTokens('user', array('user' => $this->account), $user_tokens);
617

    
618
    $edit = array('user_pictures' => FALSE);
619
    $this->drupalPost('admin/config/people/accounts', $edit, 'Save configuration');
620
    $this->assertText('The configuration options have been saved.');
621

    
622
    // Remove the simpletest-created user role.
623
    user_role_delete(end($this->account->roles));
624
    $this->account = user_load($this->account->uid, TRUE);
625

    
626
    $user_tokens = array(
627
      'picture' => NULL,
628
      'picture:fid' => NULL,
629
      'ip-address' => NULL,
630
      'roles' => 'authenticated user',
631
      'roles:keys' => (string) DRUPAL_AUTHENTICATED_RID,
632
    );
633
    $this->assertTokens('user', array('user' => $this->account), $user_tokens);
634

    
635
    // The ip address token should work for the current user token type.
636
    $tokens = array(
637
      'ip-address' => ip_address(),
638
    );
639
    $this->assertTokens('current-user', array(), $tokens);
640

    
641
    $anonymous = drupal_anonymous_user();
642
    // Mess with the role array to ensure we still get consistent output.
643
    $anonymous->roles[DRUPAL_ANONYMOUS_RID] = DRUPAL_ANONYMOUS_RID;
644
    $tokens = array(
645
      'roles' => 'anonymous user',
646
      'roles:keys' => (string) DRUPAL_ANONYMOUS_RID,
647
    );
648
    $this->assertTokens('user', array('user' => $anonymous), $tokens);
649
  }
650
}
651

    
652
class TokenEntityTestCase extends TokenTestHelper {
653
  public static function getInfo() {
654
    return array(
655
      'name' => 'Entity token tests',
656
      'description' => 'Test the entity tokens.',
657
      'group' => 'Token',
658
    );
659
  }
660

    
661
  public function setUp($modules = array()) {
662
    $modules[] = 'taxonomy';
663
    parent::setUp($modules);
664

    
665
    // Create the default tags vocabulary.
666
    $vocabulary = (object) array(
667
      'name' => 'Tags',
668
      'machine_name' => 'tags',
669
    );
670
    taxonomy_vocabulary_save($vocabulary);
671
    $this->vocab = $vocabulary;
672
  }
673

    
674
  function testEntityMapping() {
675
    $this->assertIdentical(token_get_entity_mapping('token', 'node'), 'node');
676
    $this->assertIdentical(token_get_entity_mapping('token', 'term'), 'taxonomy_term');
677
    $this->assertIdentical(token_get_entity_mapping('token', 'vocabulary'), 'taxonomy_vocabulary');
678
    $this->assertIdentical(token_get_entity_mapping('token', 'invalid'), FALSE);
679
    $this->assertIdentical(token_get_entity_mapping('token', 'invalid', TRUE), 'invalid');
680
    $this->assertIdentical(token_get_entity_mapping('entity', 'node'), 'node');
681
    $this->assertIdentical(token_get_entity_mapping('entity', 'taxonomy_term'), 'term');
682
    $this->assertIdentical(token_get_entity_mapping('entity', 'taxonomy_vocabulary'), 'vocabulary');
683
    $this->assertIdentical(token_get_entity_mapping('entity', 'invalid'), FALSE);
684
    $this->assertIdentical(token_get_entity_mapping('entity', 'invalid', TRUE), 'invalid');
685

    
686
    // Test that when we send the mis-matched entity type into token_replace()
687
    // that we still get the tokens replaced.
688
    $vocabulary = taxonomy_vocabulary_machine_name_load('tags');
689
    $term = $this->addTerm($vocabulary);
690
    $this->assertIdentical(token_replace('[vocabulary:name]', array('taxonomy_vocabulary' => $vocabulary)), $vocabulary->name);
691
    $this->assertIdentical(token_replace('[term:name][term:vocabulary:name]', array('taxonomy_term' => $term)), $term->name . $vocabulary->name);
692
  }
693

    
694
  function addTerm(stdClass $vocabulary, array $term = array()) {
695
    $term += array(
696
      'name' => drupal_strtolower($this->randomName(5)),
697
      'vid' => $vocabulary->vid,
698
    );
699
    $term = (object) $term;
700
    taxonomy_term_save($term);
701
    return $term;
702
  }
703

    
704
  /**
705
   * Test the [entity:original:*] tokens.
706
   */
707
  function testEntityOriginal() {
708
    $node = $this->drupalCreateNode(array('title' => 'Original title'));
709

    
710
    $tokens = array(
711
      'nid' => $node->nid,
712
      'title' => 'Original title',
713
      'original' => NULL,
714
      'original:nid' => NULL,
715
    );
716
    $this->assertTokens('node', array('node' => $node), $tokens);
717

    
718
    // Emulate the original entity property that would be available from
719
    // node_save() and change the title for the node.
720
    $node->original = entity_load_unchanged('node', $node->nid);
721
    $node->title = 'New title';
722

    
723
    $tokens = array(
724
      'nid' => $node->nid,
725
      'title' => 'New title',
726
      'original' => 'Original title',
727
      'original:nid' => $node->nid,
728
    );
729
    $this->assertTokens('node', array('node' => $node), $tokens);
730
  }
731
}
732

    
733
/**
734
 * Test the profile tokens.
735
 */
736
class TokenProfileTestCase extends TokenTestHelper {
737
  private $account;
738

    
739
  public static function getInfo() {
740
    return array(
741
      'name' => 'Profile token tests',
742
      'description' => 'Test the profile tokens.',
743
      'group' => 'Token',
744
    );
745
  }
746

    
747
  public function setUp($modules = array()) {
748
    $modules[] = 'profile';
749
    parent::setUp($modules);
750
    $this->account = $this->drupalCreateUser(array('administer users'));
751
    $this->drupalLogin($this->account);
752
  }
753

    
754
  /**
755
   * Test the profile tokens.
756
   */
757
  function testProfileTokens() {
758
    $field_types = _profile_field_types();
759
    foreach (array_keys($field_types) as $field_type) {
760
      $field = array();
761
      switch ($field_type) {
762
        case 'checkbox':
763
          $field['title'] = 'This is a checkbox';
764
          break;
765
        case 'selection':
766
          $field['options'] = implode("\n", array('Red', 'Blue', 'Green'));
767
          break;
768
      }
769
      $this->addProfileField($field_type, $field);
770
    }
771

    
772
    // Submit the profile fields for the user.
773
    $edit = array(
774
      'profile_textfield' => 'This is a text field',
775
      'profile_textarea' => "First paragraph.\n\nSecond paragraph.",
776
      'profile_checkbox' => TRUE,
777
      'profile_selection' => 'Red',
778
      'profile_list' => ' Drupal ,  Joomla ',
779
      'profile_url' => 'http://www.example.com/',
780
      'profile_date[month]' => 5,
781
      'profile_date[day]' => 20,
782
      'profile_date[year]' => 1984,
783
    );
784
    $this->drupalPost("user/{$this->account->uid}/edit/SimpleTest", $edit, 'Save');
785
    $account = user_load($this->account->uid, TRUE);
786

    
787
    // Test the profile token values.
788
    $tokens = array(
789
      'profile-textfield' => 'This is a text field',
790
      'profile-textarea' => "<p>First paragraph.</p>\n<p>Second paragraph.</p>\n",
791
      'profile-checkbox' => 'This is a checkbox',
792
      'profile-selection' => 'Red',
793
      'profile-list' => 'Drupal, Joomla',
794
      'profile-url' => 'http://www.example.com/',
795
      'profile-date' => format_date(453859200, 'medium', '', NULL),
796
      'profile-date:raw' => '453859200',
797
      'profile-date:custom:Y' => '1984',
798
    );
799
    $this->assertTokens('user', array('user' => $account), $tokens);
800

    
801
    // 'Un-select' the checkbox and select profile fields.
802
    $edit = array(
803
      'profile_checkbox' => FALSE,
804
      'profile_selection' => '0',
805
    );
806
    $this->drupalPost("user/{$this->account->uid}/edit/SimpleTest", $edit, 'Save');
807
    $account = user_load($this->account->uid, TRUE);
808

    
809
    // The checkbox and select profile tokens should no longer return a value.
810
    $tokens = array(
811
      'profile-checkbox' => NULL,
812
      'profile-selection' => NULL,
813
    );
814
    $this->assertTokens('user', array('user' => $account), $tokens);
815
  }
816

    
817
  /**
818
   * Add a profile field.
819
   *
820
   * @param $type
821
   *   The profile field type.
822
   * @param $field
823
   *   (optional) An array of the profile field properties.
824
   *
825
   * @return
826
   *   The saved profile field record object.
827
   *
828
   * @see drupal_form_submit()
829
   */
830
  function addProfileField($type, array $field = array()) {
831
    $field += array(
832
      'type' => $type,
833
      'category' => 'SimpleTest',
834
      'title' => $this->randomName(8),
835
      'name' => 'profile_' . $type,
836
      'explanation' => $this->randomName(50),
837
      'autocomplete' => 0,
838
      'required' => 0,
839
      'register' => 0,
840
    );
841
    drupal_write_record('profile_field', $field);
842

    
843
    // Verify the profile field was created successfully.
844
    $saved_field = db_query("SELECT * FROM {profile_field} WHERE type = :type AND name = :name", array(':type' => $type, ':name' => $field['name']))->fetchObject();
845
    if (empty($saved_field)) {
846
      $this->fail(t('Failed to create profile field @name.', array('@name' => $saved_field->name)));
847
    }
848

    
849
    return $saved_field;
850
  }
851
}
852

    
853
/**
854
 * Test the current page tokens.
855
 */
856
class TokenCurrentPageTestCase extends TokenTestHelper {
857
  public static function getInfo() {
858
    return array(
859
      'name' => 'Current page token tests',
860
      'description' => 'Test the [current-page:*] tokens.',
861
      'group' => 'Token',
862
    );
863
  }
864

    
865
  function testCurrentPageTokens() {
866
    $tokens = array(
867
      '[current-page:title]' => t('Welcome to @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))),
868
      '[current-page:url]' => url('node', array('absolute' => TRUE)),
869
      '[current-page:url:absolute]' => url('node', array('absolute' => TRUE)),
870
      '[current-page:url:relative]' => url('node', array('absolute' => FALSE)),
871
      '[current-page:url:path]' => 'node',
872
      '[current-page:url:args:value:0]' => 'node',
873
      '[current-page:url:args:value:1]' => NULL,
874
      '[current-page:url:unaliased]' => url('node', array('absolute' => TRUE, 'alias' => TRUE)),
875
      '[current-page:page-number]' => 1,
876
      '[current-page:query:foo]' => NULL,
877
      '[current-page:query:bar]' => NULL,
878
      '[current-page:query:q]' => 'node',
879
      // Deprecated tokens
880
      '[current-page:arg:0]' => 'node',
881
      '[current-page:arg:1]' => NULL,
882
    );
883
    $this->assertPageTokens('', $tokens);
884

    
885
    $node = $this->drupalCreateNode(array('title' => 'Node title', 'path' => array('alias' => 'node-alias')));
886
    $tokens = array(
887
      '[current-page:title]' => 'Node title',
888
      '[current-page:url]' => url("node/{$node->nid}", array('absolute' => TRUE)),
889
      '[current-page:url:absolute]' => url("node/{$node->nid}", array('absolute' => TRUE)),
890
      '[current-page:url:relative]' => url("node/{$node->nid}", array('absolute' => FALSE)),
891
      '[current-page:url:alias]' => 'node-alias',
892
      '[current-page:url:args:value:0]' => 'node-alias',
893
      '[current-page:url:args:value:1]' => NULL,
894
      '[current-page:url:unaliased]' => url("node/{$node->nid}", array('absolute' => TRUE, 'alias' => TRUE)),
895
      '[current-page:url:unaliased:args:value:0]' => 'node',
896
      '[current-page:url:unaliased:args:value:1]' => $node->nid,
897
      '[current-page:url:unaliased:args:value:2]' => NULL,
898
      '[current-page:page-number]' => 1,
899
      '[current-page:query:foo]' => 'bar',
900
      '[current-page:query:bar]' => NULL,
901
      '[current-page:query:q]' => 'node/1',
902
      // Deprecated tokens
903
      '[current-page:arg:0]' => 'node',
904
      '[current-page:arg:1]' => 1,
905
      '[current-page:arg:2]' => NULL,
906
    );
907
    $this->assertPageTokens("node/{$node->nid}", $tokens, array(), array('url_options' => array('query' => array('foo' => 'bar'))));
908
  }
909
}
910

    
911
class TokenArrayTestCase extends TokenTestHelper {
912
  public static function getInfo() {
913
    return array(
914
      'name' => 'Array token tests',
915
      'description' => 'Test the array tokens.',
916
      'group' => 'Token',
917
    );
918
  }
919

    
920
  function testArrayTokens() {
921
    // Test a simple array.
922
    $array = array(0 => 'a', 1 => 'b', 2 => 'c', 4 => 'd');
923
    $tokens = array(
924
      'first' => 'a',
925
      'last' => 'd',
926
      'value:0' => 'a',
927
      'value:2' => 'c',
928
      'count' => 4,
929
      'keys' => '0, 1, 2, 4',
930
      'keys:value:3' => '4',
931
      'keys:join' => '0124',
932
      'reversed' => 'd, c, b, a',
933
      'reversed:keys' => '4, 2, 1, 0',
934
      'join:/' => 'a/b/c/d',
935
      'join' => 'abcd',
936
      'join:, ' => 'a, b, c, d',
937
      'join: ' => 'a b c d',
938
    );
939
    $this->assertTokens('array', array('array' => $array), $tokens);
940

    
941
    // Test a mixed simple and render array.
942
    // 2 => c, 0 => a, 4 => d, 1 => b
943
    $array = array(
944
      '#property' => 'value',
945
      0 => 'a',
946
      1 => array('#markup' => 'b', '#weight' => 0.01),
947
      2 => array('#markup' => 'c', '#weight' => -10),
948
      4 => array('#markup' => 'd', '#weight' => 0),
949
    );
950
    $tokens = array(
951
      'first' => 'c',
952
      'last' => 'b',
953
      'value:0' => 'a',
954
      'value:2' => 'c',
955
      'count' => 4,
956
      'keys' => '2, 0, 4, 1',
957
      'keys:value:3' => '1',
958
      'keys:join' => '2041',
959
      'reversed' => 'b, d, a, c',
960
      'reversed:keys' => '1, 4, 0, 2',
961
      'join:/' => 'c/a/d/b',
962
      'join' => 'cadb',
963
      'join:, ' => 'c, a, d, b',
964
      'join: ' => 'c a d b',
965
    );
966
    $this->assertTokens('array', array('array' => $array), $tokens);
967
  }
968
}
969

    
970
class TokenRandomTestCase extends TokenTestHelper {
971
  public static function getInfo() {
972
    return array(
973
      'name' => 'Random token tests',
974
      'description' => 'Test the random tokens.',
975
      'group' => 'Token',
976
    );
977
  }
978

    
979
  function testRandomTokens() {
980
    $tokens = array(
981
      'number' => '[0-9]{1,}',
982
      'hash:md5' => '[0-9a-f]{32}',
983
      'hash:sha1' => '[0-9a-f]{40}',
984
      'hash:sha256' => '[0-9a-f]{64}',
985
      'hash:invalid-algo' => NULL,
986
    );
987

    
988
    $first_set = $this->assertTokens('random', array(), $tokens, array('regex' => TRUE));
989
    $second_set = $this->assertTokens('random', array(), $tokens, array('regex' => TRUE));
990
    foreach ($first_set as $token => $value) {
991
      $this->assertNotIdentical($first_set[$token], $second_set[$token]);
992
    }
993
  }
994
}
995

    
996
/**
997
 * @todo Remove when http://drupal.org/node/1173706 is fixed.
998
 */
999
class TokenDateTestCase extends TokenTestHelper {
1000
  public static function getInfo() {
1001
    return array(
1002
      'name' => 'Date token tests',
1003
      'description' => 'Test the date tokens.',
1004
      'group' => 'Token',
1005
    );
1006
  }
1007

    
1008
  function testDateTokens() {
1009
    $tokens = array(
1010
      'token_test' => '1984',
1011
      'invalid_format' => NULL,
1012
    );
1013

    
1014
    $this->assertTokens('date', array('date' => 453859200), $tokens);
1015
  }
1016
}
1017

    
1018
class TokenFileTestCase extends TokenTestHelper {
1019
  public static function getInfo() {
1020
    return array(
1021
      'name' => 'File token tests',
1022
      'description' => 'Test the file tokens.',
1023
      'group' => 'Token',
1024
    );
1025
  }
1026

    
1027
  function testFileTokens() {
1028
    // Create a test file object.
1029
    $file = new stdClass();
1030
    $file->fid = 1;
1031
    $file->filename = 'test.png';
1032
    $file->filesize = 100;
1033
    $file->uri = 'public://images/test.png';
1034
    $file->filemime = 'image/png';
1035

    
1036
    $tokens = array(
1037
      'basename' => 'test.png',
1038
      'extension' => 'png',
1039
      'size-raw' => 100,
1040
    );
1041
    $this->assertTokens('file', array('file' => $file), $tokens);
1042

    
1043
    // Test a file with no extension and a fake name.
1044
    $file->filename = 'Test PNG image';
1045
    $file->uri = 'public://images/test';
1046

    
1047
    $tokens = array(
1048
      'basename' => 'test',
1049
      'extension' => '',
1050
      'size-raw' => 100,
1051
    );
1052
    $this->assertTokens('file', array('file' => $file), $tokens);
1053
  }
1054
}
1055

    
1056
class TokenBlockTestCase extends TokenTestHelper {
1057
  public static function getInfo() {
1058
    return array(
1059
      'name' => 'Block token tests',
1060
      'description' => 'Test the block title token replacement.',
1061
      'group' => 'Token',
1062
    );
1063
  }
1064

    
1065
  public function setUp($modules = array()) {
1066
    $modules[] = 'block';
1067
    parent::setUp($modules);
1068

    
1069
    $this->admin_user = $this->drupalCreateUser(array('access content', 'administer blocks'));
1070
    $this->drupalLogin($this->admin_user);
1071
  }
1072

    
1073
  public function testBlockTitleTokens() {
1074
    $edit['title'] = '[user:name]';
1075
    $edit['info'] = 'Test token title block';
1076
    $edit['body[value]'] = 'This is the test token title block.';
1077
    $this->drupalPost('admin/structure/block/add', $edit, 'Save block');
1078
    // Ensure token validation is working on the block.
1079
    $this->assertText('The Block title is using the following invalid tokens: [user:name].');
1080

    
1081
    // Create the block for real now with a valid title.
1082
    $edit['title'] = '[current-page:title] block title';
1083
    $edit['regions[bartik]'] = 'sidebar_first';
1084
    $this->drupalPost(NULL, $edit, 'Save block');
1085

    
1086
    $this->drupalGet('node');
1087
    $this->assertText('Welcome to ' . variable_get('site_name', 'Drupal') . ' block title');
1088

    
1089
    // Ensure that tokens are not double-escaped when output as a block title.
1090
    $node = $this->drupalCreateNode(array('title' => "Site's first node"));
1091
    $this->drupalGet('node/' . $node->nid);
1092
    // The apostraphe should only be escaped once via check_plain().
1093
    $this->assertRaw("Site&#039;s first node block title");
1094
  }
1095
}