function TermIndexTest::testTaxonomyIndex

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/tests/src/Functional/TermIndexTest.php \Drupal\Tests\taxonomy\Functional\TermIndexTest::testTaxonomyIndex()
  2. 10 core/modules/taxonomy/tests/src/Functional/TermIndexTest.php \Drupal\Tests\taxonomy\Functional\TermIndexTest::testTaxonomyIndex()
  3. 11.x core/modules/taxonomy/tests/src/Functional/TermIndexTest.php \Drupal\Tests\taxonomy\Functional\TermIndexTest::testTaxonomyIndex()

Tests that the taxonomy index is maintained properly.

File

core/modules/taxonomy/tests/src/Functional/TermIndexTest.php, line 103

Class

TermIndexTest
Tests the hook implementations that maintain the taxonomy index.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTaxonomyIndex() {
    $node_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    // Create terms in the vocabulary.
    $term_1 = $this->createTerm($this->vocabulary);
    $term_2 = $this->createTerm($this->vocabulary);
    // Post an article.
    $edit = [];
    $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]']);
    $connection = Database::getConnection();
    $index_count = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_1->id())
        ->countQuery()
        ->execute()
        ->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 = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_1->id())
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
    $index_count = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_2->id())
        ->countQuery()
        ->execute()
        ->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 = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_1->id())
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
    $index_count = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_2->id())
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
    // Redo the above tests without interface.
    $node_storage->resetCache([
        $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 = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_1->id())
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
    $index_count = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_2->id())
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
    // Update the article to change one term.
    $node->{$this->fieldName1} = [
        [
            'target_id' => $term_1->id(),
        ],
    ];
    $node->save();
    // Check that both terms are indexed.
    $index_count = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_1->id())
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
    $index_count = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_2->id())
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
    // Update the article to change another term.
    $node->{$this->fieldName2} = [
        [
            'target_id' => $term_1->id(),
        ],
    ];
    $node->save();
    // Check that only one term is indexed.
    $index_count = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_1->id())
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
    $index_count = $connection->select('taxonomy_index')
        ->condition('nid', $node->id())
        ->condition('tid', $term_2->id())
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual(0, $index_count, 'Term 2 is not indexed.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.