function TermIndexTest::testTaxonomyIndex in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/taxonomy/src/Tests/TermIndexTest.php \Drupal\taxonomy\Tests\TermIndexTest::testTaxonomyIndex()
Tests that the taxonomy index is maintained properly.
File
- core/
modules/ taxonomy/ src/ Tests/ TermIndexTest.php, line 95 - Contains \Drupal\taxonomy\Tests\TermIndexTest.
Class
- TermIndexTest
- Tests the hook implementations that maintain the taxonomy index.
Namespace
Drupal\taxonomy\TestsCode
function testTaxonomyIndex() {
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
// Create terms in the vocabulary.
$term_1 = $this
->createTerm($this->vocabulary);
$term_2 = $this
->createTerm($this->vocabulary);
// Post an article.
$edit = array();
$edit['title[0][value]'] = $this
->randomMachineName();
$edit['body[0][value]'] = $this
->randomMachineName();
$edit["{$this->fieldName1}[]"] = $term_1
->id();
$edit["{$this->fieldName2}[]"] = $term_1
->id();
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
// Check that the term is indexed, and only once.
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node
->id(),
':tid' => $term_1
->id(),
))
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 1 is indexed once.');
// Update the article to change one term.
$edit["{$this->fieldName1}[]"] = $term_2
->id();
$this
->drupalPostForm('node/' . $node
->id() . '/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
->id(),
':tid' => $term_1
->id(),
))
->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
->id(),
':tid' => $term_2
->id(),
))
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 2 is indexed.');
// Update the article to change another term.
$edit["{$this->fieldName2}[]"] = $term_2
->id();
$this
->drupalPostForm('node/' . $node
->id() . '/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
->id(),
':tid' => $term_1
->id(),
))
->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
->id(),
':tid' => $term_2
->id(),
))
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 2 is indexed once.');
// Redo the above tests without interface.
$node_storage
->resetCache(array(
$node
->id(),
));
$node = $node_storage
->load($node
->id());
$node->title = $this
->randomMachineName();
// 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
->id(),
':tid' => $term_1
->id(),
))
->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
->id(),
':tid' => $term_2
->id(),
))
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 2 is indexed once.');
// Update the article to change one term.
$node->{$this->fieldName1} = array(
array(
'target_id' => $term_1
->id(),
),
);
$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
->id(),
':tid' => $term_1
->id(),
))
->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
->id(),
':tid' => $term_2
->id(),
))
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 2 is indexed.');
// Update the article to change another term.
$node->{$this->fieldName2} = array(
array(
'target_id' => $term_1
->id(),
),
);
$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
->id(),
':tid' => $term_1
->id(),
))
->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
->id(),
':tid' => $term_2
->id(),
))
->fetchField();
$this
->assertEqual(0, $index_count, 'Term 2 is not indexed.');
}