function TermIndexTest::testTaxonomyIndex

Tests that the taxonomy index is maintained properly.

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermIndexTest.php, line 97
Definition of Drupal\taxonomy\Tests\TermIndexTest.

Class

TermIndexTest
Tests the hook implementations that maintain the taxonomy index.

Namespace

Drupal\taxonomy\Tests

Code

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_NOT_SPECIFIED;
  $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, '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, '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 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, '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 article 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 article 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 article 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.');
}