source: sipei/modules/token/token.test

drupal-6.x
Last change on this file was ffa4103, checked in by Luis Peña <lpena@…>, 12 años ago

Cambiando el nombre de modulos a modules

  • Propiedad mode establecida a 100755
File size: 18.5 KB
Línea 
1<?php
2
3/**
4 * @file
5 * Tests for the token module.
6 */
7
8/**
9 * Helper test class with some added functions for testing.
10 */
11class TokenTestHelper extends DrupalWebTestCase {
12  function setUp(array $modules = array()) {
13    $modules[] = 'path';
14    $modules[] = 'token';
15    $modules[] = 'token_test';
16    parent::setUp($modules);
17  }
18
19  function assertToken($type, $object, $token, $expected, array $options = array()) {
20    $this->assertTokens($type, $object, array($token => $expected), $options);
21  }
22
23  function assertTokens($type, $object, array $tokens, array $options = array()) {
24    $values = token_get_values($type, $object, TRUE, $options);
25    $values = array_combine($values->tokens, $values->values);
26    foreach ($tokens as $token => $expected) {
27      if (!isset($expected)) {
28        $this->assertTrue(!isset($values[$token]), t("Token value for [@token] was not generated.", array('@token' => $token)));
29      }
30      elseif (!isset($values[$token])) {
31        $this->fail(t("Token value for [@token] was not generated.", array('@token' => $token)));
32      }
33      else {
34        $this->assertIdentical($values[$token], $expected, t("Token value for [@token] was '@actual', expected value '@expected'.", array('@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
35      }
36    }
37  }
38
39  /**
40   * Make a page request and test for token generation.
41   */
42  function assertPageTokens($url, array $tokens, array $data = array('global' => NULL), array $options = array()) {
43    if (empty($tokens)) {
44      return TRUE;
45    }
46
47    $token_page_tokens = array(
48      'tokens' => $tokens,
49      'data' => $data,
50      'options' => $options,
51    );
52    variable_set('token_page_tokens', $token_page_tokens);
53
54    $options += array('url_options' => array());
55    $this->drupalGet($url, $options['url_options']);
56    $this->refreshVariables();
57    $result = variable_get('token_page_tokens', array());
58
59    if (!isset($result['values']) || !is_array($result['values'])) {
60      return $this->fail('Failed to generate tokens.');
61    }
62
63    foreach ($tokens as $token => $expected) {
64      if (!isset($expected)) {
65        $this->assertTrue(!isset($result['values'][$token]) || $result['values'][$token] === $token, t("Token value for @token was not generated.", array('@token' => $token)));
66      }
67      elseif (!isset($result['values'][$token])) {
68        $this->fail(t('Failed to generate token @token.', array('@token' => $token)));
69      }
70      else {
71        $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)));
72      }
73    }
74  }
75}
76
77class TokenUnitTestCase extends TokenTestHelper {
78  public static function getInfo() {
79    return array(
80      'name' => 'Token unit tests',
81      'description' => 'Test basic, low-level token functions.',
82      'group' => 'Token',
83    );
84  }
85
86  /**
87   * Test token_get_invalid_tokens() and token_get_invalid_tokens_by_context().
88   */
89  public function testGetInvalidTokens() {
90    $tests = array();
91    $tests[] = array(
92      'valid tokens' => array(
93        '[title-raw]',
94        '[yyyy]',
95        '[mod-yyyy]',
96        '[site-name]',
97        '[site-slogan]',
98        '[user-id]',
99      ),
100      'invalid tokens' => array(
101        '[title-invalid]',
102        '[invalid]',
103        '[mod-invalid]',
104        '[invalid-title]',
105        '[site-invalid]',
106        '[uid]',
107        '[comment-cid]',
108      ),
109      'types' => array('node'),
110    );
111    $tests[] = array(
112      'valid tokens' => array(
113        '[title-raw]',
114        '[yyyy]',
115        '[mod-yyyy]',
116        '[site-name]',
117        '[site-slogan]',
118        '[user-id]',
119        '[uid]',
120        '[comment-cid]',
121      ),
122      'invalid tokens' => array(
123        '[title-invalid]',
124        '[invalid]',
125        '[mod-invalid]',
126        '[invalid-title]',
127        '[site-invalid]',
128      ),
129      'types' => array('all'),
130    );
131    $tests[] = array(
132      'valid tokens' => array(
133        '[alpha]',
134        '[beta-1]',
135        '[beta-2]',
136        '[gamma_A]',
137        '[delta-extra]',
138        '[epsilon-zeta-A]',
139      ),
140      'invalid tokens' => array(
141        '[alpha-plus]',
142        '[beta]',
143        '[beta-]',
144        '[beta_]',
145        '[beta_1]',
146        '[beta-A]',
147        '[gamma]',
148        '[gamma_]',
149        '[gamma-A]',
150        '[delta]',
151        '[epsilon-zeta-]',
152      ),
153      'types' => array('all'),
154    );
155
156    foreach ($tests as $test) {
157      $tokens = array_merge($test['valid tokens'], $test['invalid tokens']);
158      shuffle($tokens);
159
160      $invalid_tokens = token_get_invalid_tokens_by_context(implode(' ', $tokens), $test['types']);
161
162      sort($invalid_tokens);
163      sort($test['invalid tokens']);
164      $this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly: ' . implode(', ', $invalid_tokens));
165    }
166  }
167
168  /**
169   * Test the $options['clear'] parameter for token_replace().
170   */
171  public function testClearOption() {
172    $tests[] = array(
173      'input' => 'Foo [site-name][invalid-token] bar [another-invalid-token] [invalid-token]',
174      'output' => 'Foo Drupal bar  ',
175      'options' => array('clear' => TRUE),
176    );
177    $tests[] = array(
178      'input' => 'Foo [site-name][invalid-token] bar [another-invalid-token] [invalid-token]',
179      'output' => 'Foo Drupal[invalid-token] bar [another-invalid-token] [invalid-token]',
180      'options' => array(),
181    );
182
183    foreach ($tests as $test) {
184      $output = token_replace($test['input'], 'global', NULL, TOKEN_PREFIX, TOKEN_SUFFIX, $test['options']);
185      $this->assertIdentical($output, $test['output']);
186    }
187  }
188
189  /**
190   * Test whether token-replacement works in various contexts.
191   *
192   * @see http://drupal.org/node/733192
193   */
194  function testSystemTokenRecognition() {
195    global $language;
196
197    // Generate prefixes and suffixes for the token context.
198    $tests = array(
199      array('prefix' => 'this is the ', 'suffix' => ' site'),
200      array('prefix' => 'this is the', 'suffix' => 'site'),
201      array('prefix' => '[', 'suffix' => ']'),
202      array('prefix' => '', 'suffix' => ']]]'),
203      array('prefix' => '[[[', 'suffix' => ''),
204      array('prefix' => ':[:', 'suffix' => '--]'),
205      array('prefix' => '-[-', 'suffix' => ':]:'),
206      array('prefix' => '[:', 'suffix' => ']'),
207      array('prefix' => '[site:', 'suffix' => ':name]'),
208      array('prefix' => '[site:', 'suffix' => ']'),
209    );
210
211    // Check if the token is recognized in each of the contexts.
212    foreach ($tests as $test) {
213      $input = $test['prefix'] . '[site-name]' . $test['suffix'];
214      $expected = $test['prefix'] . 'Drupal' . $test['suffix'];
215      $output = token_replace($input);
216      $this->assertEqual($output, $expected);
217    }
218  }
219}
220
221class TokenNodeTestCase extends TokenTestHelper {
222  public static function getInfo() {
223    return array(
224      'name' => 'Node token tests',
225      'description' => 'Test the node tokens.',
226      'group' => 'Token',
227    );
228  }
229
230  function testNodeTokens() {
231    $time = time();
232    $created = gmmktime(0, 0, 0, 11, 19, 1978);
233    $changed = gmmktime(0, 0, 0, 7, 4, 1984);
234    $node = $this->drupalCreateNode(array(
235      'type' => 'page',
236      'language' => 'und',
237      'created' => $created,
238      'log' => '<blink>' . $this->randomName() . '</blink>',
239    ));
240    $node->changed = $changed;
241    path_set_alias('node/' . $node->nid, 'content/first-node');
242
243    $tokens = array(
244      'nid' => $node->nid,
245      'type' => 'page',
246      'type-name' => 'Page',
247      'language' => 'und',
248      'node-path' => 'content/first-node',
249      'node-url' => url('node/' . $node->nid, array('absolute' => TRUE)),
250      'small' => '11/19/1978 - 00:00',
251      'yyyy' => '1978',
252      'yy' => '78',
253      'month' => 'November',
254      'mon' => 'Nov',
255      'mm' => '11',
256      'm' => '11',
257      'ww' => '46',
258      'date' => '7',
259      'day' => 'Sunday',
260      'ddd' => 'Sun',
261      'dd' => '19',
262      'd' => '19',
263      'raw' => 280281600,
264      'since' => format_interval($time - 280281600),
265      'mod-small' => '07/04/1984 - 00:00',
266      'mod-yyyy' => '1984',
267      'mod-yy' => '84',
268      'mod-month' => 'July',
269      'mod-mon' => 'Jul',
270      'mod-mm' => '07',
271      'mod-m' => '7',
272      'mod-ww' => '27',
273      'mod-date' => '3',
274      'mod-day' => 'Wednesday',
275      'mod-ddd' => 'Wed',
276      'mod-dd' => '04',
277      'mod-d' => '4',
278      'mod-raw' => 457747200,
279      'mod-since' => format_interval($time - 457747200),
280      'log' => filter_xss($node->log),
281      'log-raw' => $node->log,
282    );
283    $this->assertTokens('node', $node, $tokens);
284  }
285}
286
287class TokenCommentTestCase extends TokenTestHelper {
288  protected $node;
289
290  public static function getInfo() {
291    return array(
292      'name' => 'Comment token tests',
293      'description' => 'Test the comment tokens.',
294      'group' => 'Token',
295    );
296  }
297
298  function setUp(array $modules = array()) {
299    $modules[] = 'comment';
300    parent::setUp($modules);
301    $this->node = $this->drupalCreateNode(array('comment' => 2));
302  }
303
304  function loadComment($cid) {
305    return db_fetch_object(db_query('SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.signature_format, u.picture, u.data, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d', $cid));
306  }
307
308  function createComment(array $comment) {
309    $comment += array(
310      'cid' => 0,
311      'nid' => $this->node->nid,
312      'pid' => 0,
313      'uid' => 0,
314      'subject' => $this->randomName(),
315      'comment' => $this->randomName(64),
316      'format' => 1,
317      'timestamp' => gmmktime(0, 0, 0, 7, 4, 1984),
318      'status' => COMMENT_PUBLISHED,
319    );
320
321    $cid = comment_save($comment);
322    return $this->loadComment($cid);
323  }
324
325  function testCommentTokens() {
326    $time = time();
327    $comment = $this->createComment(array(
328      'timestamp' => gmmktime(0, 0, 0, 7, 4, 1984),
329    ));
330
331    $tokens = array(
332      'comment-cid' => $comment->cid,
333      'comment-nid' => $this->node->nid,
334      'comment-yyyy' => '1984',
335      'comment-yy' => '84',
336      'comment-month' => 'July',
337      'comment-mon' => 'Jul',
338      'comment-mm' => '07',
339      'comment-m' => '7',
340      'comment-ww' => '27',
341      'comment-date' => '3',
342      'comment-day' => 'Wednesday',
343      'comment-ddd' => 'Wed',
344      'comment-dd' => '04',
345      'comment-d' => '4',
346      'comment-raw' => '457747200',
347      'comment-since' => format_interval($time - 457747200),
348      'comment-node-title' => check_plain($this->node->title),
349      'comment-node-title-raw' => $this->node->title,
350    );
351    $this->assertTokens('comment', $comment, $tokens);
352
353  }
354}
355
356class TokenTaxonomyTestCase extends TokenTestHelper {
357  protected $vocabulary;
358
359  public static function getInfo() {
360    return array(
361      'name' => 'Taxonomy and vocabulary token tests',
362      'description' => 'Test the taxonomy tokens.',
363      'group' => 'Token',
364    );
365  }
366
367  function setUp(array $modules = array()) {
368    $modules[] = 'taxonomy';
369    parent::setUp($modules);
370    // Reset the static taxonomy.module caches.
371    taxonomy_vocabulary_load(0, TRUE);
372    taxonomy_get_term(0, TRUE);
373  }
374
375  function addVocabulary(array $vocabulary = array()) {
376    $vocabulary += array(
377      'name' => drupal_strtolower($this->randomName(5)),
378      'nodes' => array('story' => 'story'),
379    );
380    taxonomy_save_vocabulary($vocabulary);
381    return (object) $vocabulary;
382  }
383
384  function addTerm(stdClass $vocabulary, array $term = array()) {
385    $term += array(
386      'name' => drupal_strtolower($this->randomName(5)),
387      'vid' => $vocabulary->vid,
388    );
389    taxonomy_save_term($term);
390    return (object) $term;
391  }
392
393  function testTaxonomyTokens() {
394    $vocabulary = $this->addVocabulary(array(
395      'name' => '<blink>Vocab Name</blink>',
396      'description' => '<blink>Vocab Description</blink>',
397    ));
398    $term = $this->addTerm($vocabulary, array(
399      'name' => '<blink>Term Name</blink>',
400      'description' => '<blink>Term Description</blink>',
401    ));
402
403    $tokens = array(
404      'tid' => $term->tid,
405      'cat' => check_plain($term->name),
406      'cat-raw' => $term->name,
407      'cat-description' => 'Term Description',
408      'vid' => $vocabulary->vid,
409      'vocab' => check_plain($vocabulary->name),
410      'vocab-raw' => $vocabulary->name,
411      'vocab-description' => 'Vocab Description',
412      'vocab-description-raw' => $vocabulary->description,
413    );
414    $this->assertTokens('taxonomy', $term, $tokens);
415
416    $tokens = array(
417      'vocabulary-vid' => $vocabulary->vid,
418      'vocabulary-name' => check_plain($vocabulary->name),
419      'vocabulary-name-raw' => $vocabulary->name,
420      'vocabulary-description' => 'Vocab Description',
421      'vocabulary-description-raw' => $vocabulary->description,
422    );
423    $this->assertTokens('vocabulary', $vocabulary, $tokens);
424  }
425}
426
427class TokenMenuTestCase extends TokenTestHelper {
428  public static function getInfo() {
429    return array(
430      'name' => 'Menu token tests',
431      'description' => 'Test the menu tokens.',
432      'group' => 'Token',
433    );
434  }
435
436  function setUp(array $modules = array()) {
437    $modules[] = 'menu';
438    parent::setUp($modules);
439  }
440
441  function testMenuTokens() {
442    $root_link = array(
443      'link_path' => 'root',
444      'link_title' => 'Root link',
445      'menu_name' => 'primary-links',
446    );
447    menu_link_save($root_link);
448
449    // Add another link with the root link as the parent
450    $parent_link = array(
451      'link_path' => 'root/parent',
452      'link_title' => 'Parent link',
453      'menu_name' => 'primary-links',
454      'plid' => $root_link['mlid'],
455    );
456    menu_link_save($parent_link);
457
458    $node_link = array(
459      'enabled' => TRUE,
460      'link_title' => 'Node link',
461      'plid' => $parent_link['mlid'],
462      'customized' => 0,
463    );
464    $node = $this->drupalCreateNode(array('menu' => $node_link));
465
466    // Test [node:menu] tokens.
467    $tokens = array(
468      'menu' => 'Primary links',
469      'menu-raw' => 'Primary links',
470      'menupath' => 'Root link/Parent link/Node link',
471      'menupath-raw' => 'Root link/Parent link/Node link',
472      'menu-link-title' => 'Node link',
473      'menu-link-title-raw' => 'Node link',
474      'menu-link-mlid' => $node->menu['mlid'],
475      'menu-link-plid' => $node->menu['plid'],
476      'menu-link-plid' => $parent_link['mlid'],
477      'menu-link-parent-path' => 'root/parent',
478      'menu-link-parent-path-raw' => 'root/parent',
479    );
480    $this->assertTokens('node', $node, $tokens);
481  }
482}
483
484/*
485 * Unit tests for the book tokens provided by Pathauto.
486 */
487class TokenBookTestCase extends TokenTestHelper {
488  public static function getInfo() {
489    return array(
490      'name' => 'Book tokens',
491      'description' => 'Tests the book tokens.',
492      'group' => 'Token',
493    );
494  }
495
496  function setUp(array $modules = array()) {
497    $modules[] = 'book';
498    $modules[] = 'menu';
499    parent::setUp($modules);
500
501    variable_set('book_allowed_types', array('book', 'page'));
502  }
503
504  function testBookTokens() {
505    // Add a non-book node.
506    $non_book_node = $this->drupalCreateNode(array('type' => 'book'));
507    $tokens = array(
508      'book' => '',
509      'book-raw' => '',
510      'book_id' => '',
511      'bookpath' => '',
512      'bookpath-raw' => '',
513    );
514    $this->assertTokens('node', $non_book_node, $tokens);
515
516    // Add a root book page.
517    $parent_node = $this->drupalCreateNode(array(
518      'type' => 'book',
519      'title' => 'Root',
520      'book' => array('bid' => 'new') + _book_link_defaults('new'),
521    ));
522    $tokens = array(
523      'book' => 'Root',
524      'book-raw' => 'Root',
525      'book_id' => $parent_node->book['bid'],
526      'bookpath' => '',
527      'bookpath-raw' => '',
528      // Check that even those book menu links have been created for this node,
529      // that the menu links still return nothing.
530      'menu' => '',
531      'menupath' => '',
532      'menu-link-title' => '',
533      'menu-link-title-raw' => '',
534      'menu-link-mlid' => '',
535      'menu-link-plid' => '',
536      'menu-link-parent-path' => '',
537    );
538    $this->assertTokens('node', $parent_node, $tokens);
539
540    // Add a first child page.
541    $child_node1 = $this->drupalCreateNode(array(
542      'type' => 'book',
543      'title' => 'Sub page1',
544      'book' => array(
545        'bid' => $parent_node->book['bid'],
546        'plid' => $parent_node->book['mlid'],
547      ) + _book_link_defaults('new'),
548    ));
549    $tokens = array(
550      'book' => 'Root',
551      'book-raw' => 'Root',
552      'book_id' => $parent_node->book['bid'],
553      'bookpath' => 'Root',
554      'bookpath-raw' => 'Root',
555    );
556    $this->assertTokens('node', $child_node1, $tokens);
557
558    // Add a second child page.
559    $child_node2 = $this->drupalCreateNode(array(
560      'type' => 'book',
561      'title' => 'Sub page2',
562      'book' => array(
563        'bid' => $parent_node->book['bid'],
564        'plid' => $parent_node->book['mlid'],
565      ) + _book_link_defaults('new'),
566    ));
567    $tokens = array(
568      'book' => 'Root',
569      'book-raw' => 'Root',
570      'book_id' => $parent_node->book['bid'],
571      'bookpath' => 'Root',
572      'bookpath-raw' => 'Root',
573    );
574    $this->assertTokens('node', $child_node2, $tokens);
575
576    // Add a child page on an existing child page.
577    $sub_child_node1 = $this->drupalCreateNode(array(
578      'type' => 'page',
579      'title' => 'Sub-sub Page1',
580      'book' => array(
581        'bid' => $parent_node->book['bid'],
582        'plid' => $child_node1->book['mlid'],
583      ) + _book_link_defaults('new'),
584    ));
585    $tokens = array(
586      'book' => 'Root',
587      'book-raw' => 'Root',
588      'book_id' => $parent_node->book['bid'],
589      'bookpath' => 'Root/Sub page1',
590      'bookpath-raw' => 'Root/Sub page1',
591    );
592    $this->assertTokens('node', $sub_child_node1, $tokens);
593  }
594}
595
596/**
597 * Test the current page tokens.
598 */
599class TokenCurrentPageTestCase extends TokenTestHelper {
600  public static function getInfo() {
601    return array(
602      'name' => 'Current page token tests',
603      'description' => 'Test the current page tokens.',
604      'group' => 'Token',
605    );
606  }
607
608  function testCurrentPageTokens() {
609    $tokens = array(
610      '[current-page-title]' => '',
611      '[current-page-path]' => 'node',
612      '[current-page-url]' => url('node', array('absolute' => TRUE)),
613      '[current-page-number]' => 1,
614    );
615    $this->assertPageTokens('', $tokens);
616
617    $node = $this->drupalCreateNode(array('title' => 'Node title', 'path' => 'node-alias'));
618    $tokens = array(
619      '[current-page-title]' => 'Node title',
620      '[current-page-path]' => 'node-alias',
621      '[current-page-url]' => url("node/{$node->nid}", array('absolute' => TRUE)),
622      '[current-page-number]' => 1,
623    );
624    $this->assertPageTokens("node/{$node->nid}", $tokens);
625  }
626}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.