1 taxonomy.test TaxonomyTermIndexTestCase::testTaxonomyIndex()

Tests that the taxonomy index is maintained properly.

File

core/modules/taxonomy/tests/taxonomy.test, line 1225
Tests for taxonomy.module.

Class

TaxonomyTermIndexTestCase
Tests the hook implementations that maintain the taxonomy index.

Code

function testTaxonomyIndex() {
  // Create terms in the vocabulary.
  $term_1 = $this->createTerm($this->vocabulary);
  $term_2 = $this->createTerm($this->vocabulary);

  // Post a post.
  $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->backdropPost('node/add/post', $edit, t('Save'));

  // Check that the term is indexed, and only once.
  $node = $this->backdropGetNodeByTitle($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, 'Term 1 is indexed once.');

  // Update the post to change one term.
  $edit["{$this->field_name_1}[$langcode][]"] = $term_2->tid;
  $this->backdropPost('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, '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, 'Term 2 is indexed.');

  // Update the post to change another term.
  $edit["{$this->field_name_2}[$langcode][]"] = $term_2->tid;
  $this->backdropPost('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, '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, 'Term 2 is indexed once.');

  // Redo the above tests without interface.
  $node->title = $this->randomName();
  unset($node->{$this->field_name_1});
  unset($node->{$this->field_name_2});

  // Update the post with no term changed.
  $node->save();

  // 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, '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, 'Term 2 is indexed once.');

  // Update the post to change one term.
  $node->{$this->field_name_1}[$langcode] = array(array('tid' => $term_1->tid));
  $node->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, '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, 'Term 2 is indexed.');

  // Update the post to change another term.
  $node->{$this->field_name_2}[$langcode] = array(array('tid' => $term_1->tid));
  $node->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(1, $index_count, '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, 'Term 2 is not indexed.');
}