3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 * Tests the hook implementations that maintain the taxonomy index.
12 class TermIndexTest extends TaxonomyTestBase {
19 public static $modules = ['views'];
22 * Vocabulary for testing.
24 * @var \Drupal\taxonomy\VocabularyInterface
26 protected $vocabulary;
29 * Name of the taxonomy term reference field.
33 protected $fieldName1;
36 * Name of the taxonomy term reference field.
40 protected $fieldName2;
42 protected function setUp() {
45 // Create an administrative user.
46 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
48 // Create a vocabulary and add two term reference fields to article nodes.
49 $this->vocabulary = $this->createVocabulary();
51 $this->fieldName1 = mb_strtolower($this->randomMachineName());
54 $this->vocabulary->id() => $this->vocabulary->id(),
56 'auto_create' => TRUE,
58 $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
60 entity_get_form_display('node', 'article', 'default')
61 ->setComponent($this->fieldName1, [
62 'type' => 'options_select',
65 entity_get_display('node', 'article', 'default')
66 ->setComponent($this->fieldName1, [
67 'type' => 'entity_reference_label',
71 $this->fieldName2 = mb_strtolower($this->randomMachineName());
72 $this->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
74 entity_get_form_display('node', 'article', 'default')
75 ->setComponent($this->fieldName2, [
76 'type' => 'options_select',
79 entity_get_display('node', 'article', 'default')
80 ->setComponent($this->fieldName2, [
81 'type' => 'entity_reference_label',
87 * Tests that the taxonomy index is maintained properly.
89 public function testTaxonomyIndex() {
90 $node_storage = $this->container->get('entity.manager')->getStorage('node');
91 // Create terms in the vocabulary.
92 $term_1 = $this->createTerm($this->vocabulary);
93 $term_2 = $this->createTerm($this->vocabulary);
97 $edit['title[0][value]'] = $this->randomMachineName();
98 $edit['body[0][value]'] = $this->randomMachineName();
99 $edit["{$this->fieldName1}[]"] = $term_1->id();
100 $edit["{$this->fieldName2}[]"] = $term_1->id();
101 $this->drupalPostForm('node/add/article', $edit, t('Save'));
103 // Check that the term is indexed, and only once.
104 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
105 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
106 ':nid' => $node->id(),
107 ':tid' => $term_1->id(),
109 $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
111 // Update the article to change one term.
112 $edit["{$this->fieldName1}[]"] = $term_2->id();
113 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
115 // Check that both terms are indexed.
116 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
117 ':nid' => $node->id(),
118 ':tid' => $term_1->id(),
120 $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
121 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
122 ':nid' => $node->id(),
123 ':tid' => $term_2->id(),
125 $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
127 // Update the article to change another term.
128 $edit["{$this->fieldName2}[]"] = $term_2->id();
129 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
131 // Check that only one term is indexed.
132 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
133 ':nid' => $node->id(),
134 ':tid' => $term_1->id(),
136 $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
137 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
138 ':nid' => $node->id(),
139 ':tid' => $term_2->id(),
141 $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
143 // Redo the above tests without interface.
144 $node_storage->resetCache([$node->id()]);
145 $node = $node_storage->load($node->id());
146 $node->title = $this->randomMachineName();
148 // Update the article with no term changed.
151 // Check that the index was not changed.
152 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
153 ':nid' => $node->id(),
154 ':tid' => $term_1->id(),
156 $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
157 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
158 ':nid' => $node->id(),
159 ':tid' => $term_2->id(),
161 $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
163 // Update the article to change one term.
164 $node->{$this->fieldName1} = [['target_id' => $term_1->id()]];
167 // Check that both terms are indexed.
168 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
169 ':nid' => $node->id(),
170 ':tid' => $term_1->id(),
172 $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
173 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
174 ':nid' => $node->id(),
175 ':tid' => $term_2->id(),
177 $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
179 // Update the article to change another term.
180 $node->{$this->fieldName2} = [['target_id' => $term_1->id()]];
183 // Check that only one term is indexed.
184 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
185 ':nid' => $node->id(),
186 ':tid' => $term_1->id(),
188 $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
189 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
190 ':nid' => $node->id(),
191 ':tid' => $term_2->id(),
193 $this->assertEqual(0, $index_count, 'Term 2 is not indexed.');
197 * Tests that there is a link to the parent term on the child term page.
199 public function testTaxonomyTermHierarchyBreadcrumbs() {
200 // Create two taxonomy terms and set term2 as the parent of term1.
201 $term1 = $this->createTerm($this->vocabulary);
202 $term2 = $this->createTerm($this->vocabulary);
203 $term1->parent = [$term2->id()];
206 // Verify that the page breadcrumbs include a link to the parent term.
207 $this->drupalGet('taxonomy/term/' . $term1->id());
208 // Breadcrumbs are not rendered with a language, prevent the term
209 // language from being added to the options.
210 $this->assertRaw(\Drupal::l($term2->getName(), $term2->urlInfo('canonical', ['language' => NULL])), 'Parent term link is displayed when viewing the node.');