summaryrefslogtreecommitdiff
path: root/modules/taxonomy/taxonomy.test
diff options
context:
space:
mode:
authorwebchick <webchick@24967.no-reply.drupal.org>2011-12-22 01:11:01 -0800
committerwebchick <webchick@24967.no-reply.drupal.org>2011-12-22 01:11:01 -0800
commit2550674cc80bc67a8efdbc645409ef0300a00266 (patch)
treee10433d5ea980053ef967baa52375c1cc280fb91 /modules/taxonomy/taxonomy.test
parent24c357b5c521ade1d9002c43167695d3f769b8fa (diff)
downloadbrdo-2550674cc80bc67a8efdbc645409ef0300a00266.tar.gz
brdo-2550674cc80bc67a8efdbc645409ef0300a00266.tar.bz2
Issue #1050466 by xjm, makara, rhayun: Fixed The taxonomy index should be maintained in a node hook, not a field hook.
Diffstat (limited to 'modules/taxonomy/taxonomy.test')
-rw-r--r--modules/taxonomy/taxonomy.test202
1 files changed, 202 insertions, 0 deletions
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index 183e808ed..03fdcb095 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -833,6 +833,208 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase {
}
/**
+ * Tests the hook implementations that maintain the taxonomy index.
+ */
+class TaxonomyTermIndexTestCase extends TaxonomyWebTestCase {
+
+ public static function getInfo() {
+ return array(
+ 'name' => 'Taxonomy term index',
+ 'description' => 'Tests the hook implementations that maintain the taxonomy index.',
+ 'group' => 'Taxonomy',
+ );
+ }
+
+ function setUp() {
+ parent::setUp('taxonomy');
+
+ // Create an administrative user.
+ $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
+ $this->drupalLogin($this->admin_user);
+
+ // Create a vocabulary and add two term reference fields to article nodes.
+ $this->vocabulary = $this->createVocabulary();
+
+ $this->field_name_1 = drupal_strtolower($this->randomName());
+ $this->field_1 = array(
+ 'field_name' => $this->field_name_1,
+ 'type' => 'taxonomy_term_reference',
+ 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+ 'settings' => array(
+ 'allowed_values' => array(
+ array(
+ 'vocabulary' => $this->vocabulary->machine_name,
+ 'parent' => 0,
+ ),
+ ),
+ ),
+ );
+ field_create_field($this->field_1);
+ $this->instance_1 = array(
+ 'field_name' => $this->field_name_1,
+ 'bundle' => 'article',
+ 'entity_type' => 'node',
+ 'widget' => array(
+ 'type' => 'options_select',
+ ),
+ 'display' => array(
+ 'default' => array(
+ 'type' => 'taxonomy_term_reference_link',
+ ),
+ ),
+ );
+ field_create_instance($this->instance_1);
+
+ $this->field_name_2 = drupal_strtolower($this->randomName());
+ $this->field_2 = array(
+ 'field_name' => $this->field_name_2,
+ 'type' => 'taxonomy_term_reference',
+ 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+ 'settings' => array(
+ 'allowed_values' => array(
+ array(
+ 'vocabulary' => $this->vocabulary->machine_name,
+ 'parent' => 0,
+ ),
+ ),
+ ),
+ );
+ field_create_field($this->field_2);
+ $this->instance_2 = array(
+ 'field_name' => $this->field_name_2,
+ 'bundle' => 'article',
+ 'entity_type' => 'node',
+ 'widget' => array(
+ 'type' => 'options_select',
+ ),
+ 'display' => array(
+ 'default' => array(
+ 'type' => 'taxonomy_term_reference_link',
+ ),
+ ),
+ );
+ field_create_instance($this->instance_2);
+ }
+
+ /**
+ * Tests that the taxonomy index is maintained properly.
+ */
+ function testTaxonomyIndex() {
+ // Create terms in the vocabulary.
+ $term_1 = $this->createTerm($this->vocabulary);
+ $term_2 = $this->createTerm($this->vocabulary);
+
+ // Post an article.
+ $edit = array();
+ $langcode = LANGUAGE_NONE;
+ $edit["title"] = $this->randomName();
+ $edit["body[$langcode][0][value]"] = $this->randomName();
+ $edit["{$this->field_name_1}[$langcode][]"] = $term_1->tid;
+ $edit["{$this->field_name_2}[$langcode][]"] = $term_1->tid;
+ $this->drupalPost('node/add/article', $edit, t('Save'));
+
+ // Check that the term is indexed, and only once.
+ $node = $this->drupalGetNodeByTitle($edit["title"]);
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_1->tid,
+ ))->fetchField();
+ $this->assertEqual(1, $index_count, t('Term 1 is indexed once.'));
+
+ // Update the article to change one term.
+ $edit["{$this->field_name_1}[$langcode][]"] = $term_2->tid;
+ $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
+
+ // Check that both terms are indexed.
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_1->tid,
+ ))->fetchField();
+ $this->assertEqual(1, $index_count, t('Term 1 is indexed.'));
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_2->tid,
+ ))->fetchField();
+ $this->assertEqual(1, $index_count, t('Term 2 is indexed.'));
+
+ // Update the article to change another term.
+ $edit["{$this->field_name_2}[$langcode][]"] = $term_2->tid;
+ $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
+
+ // Check that only one term is indexed.
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_1->tid,
+ ))->fetchField();
+ $this->assertEqual(0, $index_count, t('Term 1 is not indexed.'));
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_2->tid,
+ ))->fetchField();
+ $this->assertEqual(1, $index_count, t('Term 2 is indexed once.'));
+
+ // Redo the above tests without interface.
+ $update_node = array(
+ 'nid' => $node->nid,
+ 'vid' => $node->vid,
+ 'uid' => $node->uid,
+ 'type' => $node->type,
+ 'title' => $this->randomName(),
+ );
+
+ // Update the article with no term changed.
+ $updated_node = (object) $update_node;
+ node_save($updated_node);
+
+ // Check that the index was not changed.
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_1->tid,
+ ))->fetchField();
+ $this->assertEqual(0, $index_count, t('Term 1 is not indexed.'));
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_2->tid,
+ ))->fetchField();
+ $this->assertEqual(1, $index_count, t('Term 2 is indexed once.'));
+
+ // Update the article to change one term.
+ $update_node[$this->field_name_1][$langcode] = array(array('tid' => $term_1->tid));
+ $updated_node = (object) $update_node;
+ node_save($updated_node);
+
+ // Check that both terms are indexed.
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_1->tid,
+ ))->fetchField();
+ $this->assertEqual(1, $index_count, t('Term 1 is indexed.'));
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_2->tid,
+ ))->fetchField();
+ $this->assertEqual(1, $index_count, t('Term 2 is indexed.'));
+
+ // Update the article to change another term.
+ $update_node[$this->field_name_2][$langcode] = array(array('tid' => $term_1->tid));
+ $updated_node = (object) $update_node;
+ node_save($updated_node);
+
+ // Check that only one term is indexed.
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_1->tid,
+ ))->fetchField();
+ $this->assertEqual(1, $index_count, t('Term 1 is indexed once.'));
+ $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
+ ':nid' => $node->nid,
+ ':tid' => $term_2->tid,
+ ))->fetchField();
+ $this->assertEqual(0, $index_count, t('Term 2 is not indexed.'));
+ }
+}
+
+/**
* Test the taxonomy_term_load_multiple() function.
*/
class TaxonomyLoadMultipleUnitTest extends TaxonomyWebTestCase {