assertTokens($type, $object, array($token => $expected), $options); } function assertTokens($type, $object, array $tokens, array $options = array()) { $token_input = drupal_map_assoc(array_keys($tokens)); $values = token_generate($type, $token_input, array($type => $object), $options); foreach ($tokens as $token => $expected) { $this->assertIdentical($values[$token], $expected, t("Token value for [@type:@token] was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $values[$token], '@expected' => $expected))); } } function saveAlias($source, $alias, $language = LANGUAGE_NONE) { $alias = array( 'source' => $source, 'alias' => $alias, 'language' => $language, ); path_save($alias); return $alias; } function saveEntityAlias($entity_type, $entity, $alias, $language = LANGUAGE_NONE) { $uri = entity_uri($entity_type, $entity); return $this->saveAlias($uri['path'], $alias, $language); } } class TokenUnitTestCase extends TokenTestHelper { public static function getInfo() { return array( 'name' => 'Token unit tests', 'description' => 'Test basic, low-level token functions.', 'group' => 'Token', ); } /** * Test token_get_invalid_tokens() and token_get_invalid_tokens_by_context(). */ public function testGetInvalidTokens() { $tests = array(); $tests[] = array( 'valid tokens' => array( '[node:title]', '[node:created:short]', '[node:created:custom:invalid]', '[node:created:custom:mm-YYYY]', '[site:name]', '[site:slogan]', ), 'invalid tokens' => array( '[node:title:invalid]', '[node:created:invalid]', '[node:created:short:invalid]', '[invalid:title]', '[site:invalid]', ), 'types' => array('node'), ); foreach ($tests as $test) { $tokens = array_merge($test['valid tokens'], $test['invalid tokens']); shuffle($tokens); $invalid_tokens = token_get_invalid_tokens_by_context(implode(' ', $tokens), $test['types']); sort($invalid_tokens); sort($test['invalid tokens']); $this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly.'); } } } class TokenNodeTestCase extends TokenTestHelper { public static function getInfo() { return array( 'name' => 'Node token tests', 'description' => 'Test the node tokens.', 'group' => 'Token', ); } function testNodeTokens() { $source_node = $this->drupalCreateNode(array('log' => $this->randomName(), 'path' => array('alias' => 'content/source-node'))); $translated_node = $this->drupalCreateNode(array('tnid' => $source_node->nid)); // Test [node:source] tokens. $this->assertToken('node', $translated_node, 'source', $source_node->title); $this->assertToken('node', $translated_node, 'source:nid', $source_node->nid); //$this->assertToken('node', $source_node, 'source', '', array('clear' => TRUE)); // Test [node:log] token. $this->assertToken('node', $source_node, 'log', $source_node->log); $this->assertToken('node', $translated_node, 'log', ''); // Test [node:path] token. $this->assertToken('node', $source_node, 'path', 'content/source-node'); $this->assertToken('node', $translated_node, 'path', 'node/' . $translated_node->nid); } } class TokenMenuTestCase extends TokenTestHelper { public static function getInfo() { return array( 'name' => 'Menu token tests', 'description' => 'Test the menu tokens.', 'group' => 'Token', ); } function setUp() { parent::setUp(array('menu')); } function testMenuTokens() { $root_link = array( 'link_path' => '', 'link_title' => 'Front link', 'menu_name' => 'main-menu', ); menu_link_save($root_link); $node_link = array( 'enabled' => TRUE, 'link_title' => 'Node link', 'plid' => $root_link['mlid'], 'customized' => 0, 'description' => '', ); $node = $this->drupalCreateNode(array('menu' => $node_link)); // Test [node:menu] tokens. $tokens = array( 'menu-link' => 'Node link', 'menu-link:mlid' => $node->menu['mlid'], 'menu-link:title' => 'Node link', 'menu-link:menu' => 'main-menu', 'menu-link:menu-name' => 'Main menu', 'menu-link:path' => 'node/' . $node->nid, 'menu-link:url' => url('node/' . $node->nid, array('absolute' => TRUE)), 'menu-link:parent' => 'Front link', 'menu-link:parent:mlid' => $node->menu['plid'], 'menu-link:parent:mlid' => $root_link['mlid'], ); $this->assertTokens('node', $node, $tokens); } } class TokenTaxonomyTestCase extends TokenTestHelper { protected $vocab; public static function getInfo() { return array( 'name' => 'Taxonomy token tests', 'description' => 'Test the taxonomy tokens.', 'group' => 'Token', ); } function setUp() { parent::setUp(array('taxonomy')); // Use the default 'Tags' taxonomy included with the standard install profile. $tags_vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Tags'")->fetchField(); $this->vocab = taxonomy_vocabulary_load($tags_vid); $this->vocab->name = 'tags'; } function testTaxonomyTokens() { $term1 = $this->addTerm($this->vocab, array('path' => array('alias' => 'parent-term'))); $term2 = $this->addTerm($this->vocab, array('parent' => $term1->tid)); $this->assertToken('term', $term1, 'path', 'parent-term'); $this->assertToken('term', $term2, 'path', 'taxonomy/term/'. $term2->tid); } function addVocabulary(array $vocabulary = array()) { $vocabulary += array( 'name' => drupal_strtolower($this->randomName(5)), 'nodes' => array('article' => 'article'), ); $vocabulary = (object) $vocabulary; taxonomy_vocabulary_save($vocabulary); return $vocabulary; } function addTerm(stdClass $vocabulary, array $term = array()) { $term += array( 'name' => drupal_strtolower($this->randomName(5)), 'vid' => $vocabulary->vid, ); $term = (object) $term; taxonomy_term_save($term); return $term; } }