From 56026aa2924bc6cfd1cb09a60d1880cbd67a567c Mon Sep 17 00:00:00 2001 From: Dries Buytaert <dries@buytaert.net> Date: Fri, 29 Jan 2010 22:56:54 +0000 Subject: [PATCH] - Patch #610022 by mcarbone, te-brian, David_Rothstein: fixed taxonomy tokens, fix old queries. --- modules/taxonomy/taxonomy.test | 110 +++++++++++++++++++++++++++ modules/taxonomy/taxonomy.tokens.inc | 22 ++++-- 2 files changed, 125 insertions(+), 7 deletions(-) diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test index d0db7b734e2a..7d5a0ef93d60 100644 --- a/modules/taxonomy/taxonomy.test +++ b/modules/taxonomy/taxonomy.test @@ -33,6 +33,7 @@ class TaxonomyWebTestCase extends DrupalWebTestCase { function createTerm($vocabulary) { $term = new stdClass(); $term->name = $this->randomName(); + $term->description = $this->randomName(); $term->vid = $vocabulary->vid; taxonomy_term_save($term); return $term; @@ -834,3 +835,112 @@ class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase { $this->assertText($term->name, t('Term name is displayed')); } } + +/** + * Test taxonomy token replacement in strings. + */ +class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase { + public static function getInfo() { + return array( + 'name' => 'Taxonomy token replacement', + 'description' => 'Generates text using placeholders for dummy content to check taxonomy token replacement.', + 'group' => 'Taxonomy', + ); + } + + function setUp() { + parent::setUp(); + $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access')); + $this->drupalLogin($this->admin_user); + $this->vocabulary = $this->createVocabulary(); + $this->langcode = LANGUAGE_NONE; + + $this->instance = array( + 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name, + 'bundle' => 'article', + 'object_type' => 'node', + 'widget' => array( + 'type' => 'options_select', + ), + 'display' => array( + 'full' => array( + 'type' => 'taxonomy_term_reference_link', + ), + ), + ); + field_create_instance($this->instance); + } + + /** + * Creates some terms and a node, then tests the tokens generated from them. + */ + function testTaxonomyTokenReplacement() { + // Create two taxonomy terms. + $term1 = $this->createTerm($this->vocabulary); + $term2 = $this->createTerm($this->vocabulary); + + // Edit $term2, setting $term1 as parent. + $edit = array(); + $edit['parent[]'] = array($term1->tid); + $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save')); + + // Create node with term2. + $edit = array(); + $node = $this->drupalCreateNode(array('type' => 'article')); + $edit[$this->instance['field_name'] . '[' . $this->langcode . '][]'] = $term2->tid; + $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); + + // Generate term token strings (before and after replacement) for term2. + $source = '[term:tid]'; + $source .= '[term:vid]'; + $source .= '[term:name]'; + $source .= '[term:description]'; + $source .= '[term:url]'; + $source .= '[term:node-count]'; + $source .= '[term:parent:name]'; + $source .= '[term:vocabulary:name]'; + + $target = $term2->tid; + $target .= $term2->vid; + $target .= check_plain($term2->name); + $target .= check_markup($term2->description, $term2->format); + $target .= url('taxonomy/term/' . $term2->tid, array('absolute' => TRUE)); + $target .= 1; + $target .= check_plain($term1->name); + $target .= check_plain($this->vocabulary->name); + + $result = token_replace($source, array('term' => $term2)); + $this->assertEqual(strcmp($target, $result), 0, t('Taxonomy term placeholder tokens replaced.')); + + // Generate vocabulary token strings (before and after replacement). + $source = '[vocabulary:vid]'; + $source .= '[vocabulary:name]'; + $source .= '[vocabulary:description]'; + $source .= '[vocabulary:node-count]'; + $source .= '[vocabulary:term-count]'; + + $target = $this->vocabulary->vid; + $target .= check_plain($this->vocabulary->name); + $target .= filter_xss($this->vocabulary->description); + $target .= 1; + $target .= 2; + + $result = token_replace($source, array('vocabulary' => $this->vocabulary)); + $this->assertEqual(strcmp($target, $result), 0, t('Taxonomy vocabulary placeholder tokens replaced.')); + + // Check that the results of token_generate are sanitized properly. This + // does NOT test the cleanliness of every token -- just that the $sanitize + // flag is being passed properly through the call stack and being handled + // correctly by a 'known' token, [term:name]. + $edit = array(); + $edit['name'] = '<blink>Blinking Text</blink>'; + $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save')); + + $raw_tokens = array('name' => '[term:name]'); + $generated = token_generate('term', $raw_tokens, array('term' => $term2)); + $this->assertEqual(strcmp($generated['[term:name]'], check_plain($term2->name)), 0, t('Token sanitized.')); + + $generated = token_generate('term', $raw_tokens, array('term' => $term2), array('sanitize' => FALSE)); + $this->assertEqual(strcmp($generated['[term:name]'], $term2->name), 0, t('Unsanitized token generated properly.')); + } +} diff --git a/modules/taxonomy/taxonomy.tokens.inc b/modules/taxonomy/taxonomy.tokens.inc index ae9a7f2de332..03268b28f871 100644 --- a/modules/taxonomy/taxonomy.tokens.inc +++ b/modules/taxonomy/taxonomy.tokens.inc @@ -119,12 +119,14 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options = break; case 'url': - $replacements[$original] = url('taxonomy/term/' . $term, array('absolute' => TRUE)); + $replacements[$original] = url('taxonomy/term/' . $term->tid, array('absolute' => TRUE)); break; case 'node-count': - $sql = "SELECT COUNT (1) FROM {taxonomy_term_node} tn WHERE tn.tid = :tid"; - $count = db_query($sql, array(':tid' => $term->tid))->fetchField(); + $query = db_select('taxonomy_index'); + $query->condition('tid', $term->tid); + $query->addTag('term_node_count'); + $count = $query->countQuery()->execute()->fetchField(); $replacements[$original] = $count; break; @@ -171,14 +173,20 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options = break; case 'term-count': - $sql = "SELECT COUNT (1) FROM {taxonomy_term_data} td WHERE td.vid = :vid"; - $count = db_query($sql, array(':vid' => $vocabulary->vid))->fetchField(); + $query = db_select('taxonomy_term_data'); + $query->condition('vid', $vocabulary->vid); + $query->addTag('vocabulary_term_count'); + $count = $query->countQuery()->execute()->fetchField(); $replacements[$original] = $count; break; case 'node-count': - $sql = "SELECT COUNT (1) FROM {taxonomy_term_node} tn LEFT JOIN {taxonomy_term_data} td ON tn.tid = td.tid WHERE td.vid = :vid"; - $count = db_query($sql, array(':vid' => $vocabulary->vid))->fetchField(); + $query = db_select('taxonomy_index', 'ti'); + $query->addExpression('COUNT(DISTINCT ti.nid)'); + $query->leftJoin('taxonomy_term_data', 'td', 'ti.tid = td.tid'); + $query->condition('td.vid', $vocabulary->vid); + $query->addTag('vocabulary_node_count'); + $count = $query->execute()->fetchField(); $replacements[$original] = $count; break; } -- GitLab