3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
9 * Tests the hook implementations that maintain the taxonomy index.
13 class TermIndexTest extends TaxonomyTestBase {
20 public static $modules = ['views'];
23 * Vocabulary for testing.
25 * @var \Drupal\taxonomy\VocabularyInterface
27 protected $vocabulary;
30 * Name of the taxonomy term reference field.
34 protected $fieldName1;
37 * Name of the taxonomy term reference field.
41 protected $fieldName2;
43 protected function setUp() {
46 // Create an administrative user.
47 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
49 // Create a vocabulary and add two term reference fields to article nodes.
50 $this->vocabulary = $this->createVocabulary();
52 $this->fieldName1 = Unicode::strtolower($this->randomMachineName());
55 $this->vocabulary->id() => $this->vocabulary->id(),
57 'auto_create' => TRUE,
59 $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
61 entity_get_form_display('node', 'article', 'default')
62 ->setComponent($this->fieldName1, [
63 'type' => 'options_select',
66 entity_get_display('node', 'article', 'default')
67 ->setComponent($this->fieldName1, [
68 'type' => 'entity_reference_label',
72 $this->fieldName2 = Unicode::strtolower($this->randomMachineName());
73 $this->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
75 entity_get_form_display('node', 'article', 'default')
76 ->setComponent($this->fieldName2, [
77 'type' => 'options_select',
80 entity_get_display('node', 'article', 'default')
81 ->setComponent($this->fieldName2, [
82 'type' => 'entity_reference_label',
88 * Tests that the taxonomy index is maintained properly.
90 public function testTaxonomyIndex() {
91 $node_storage = $this->container->get('entity.manager')->getStorage('node');
92 // Create terms in the vocabulary.
93 $term_1 = $this->createTerm($this->vocabulary);
94 $term_2 = $this->createTerm($this->vocabulary);
98 $edit['title[0][value]'] = $this->randomMachineName();
99 $edit['body[0][value]'] = $this->randomMachineName();
100 $edit["{$this->fieldName1}[]"] = $term_1->id();
101 $edit["{$this->fieldName2}[]"] = $term_1->id();
102 $this->drupalPostForm('node/add/article', $edit, t('Save'));
104 // Check that the term is indexed, and only once.
105 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
106 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
107 ':nid' => $node->id(),
108 ':tid' => $term_1->id(),
110 $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
112 // Update the article to change one term.
113 $edit["{$this->fieldName1}[]"] = $term_2->id();
114 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
116 // Check that both terms are indexed.
117 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
118 ':nid' => $node->id(),
119 ':tid' => $term_1->id(),
121 $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
122 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
123 ':nid' => $node->id(),
124 ':tid' => $term_2->id(),
126 $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
128 // Update the article to change another term.
129 $edit["{$this->fieldName2}[]"] = $term_2->id();
130 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
132 // Check that only one term is indexed.
133 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
134 ':nid' => $node->id(),
135 ':tid' => $term_1->id(),
137 $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
138 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
139 ':nid' => $node->id(),
140 ':tid' => $term_2->id(),
142 $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
144 // Redo the above tests without interface.
145 $node_storage->resetCache([$node->id()]);
146 $node = $node_storage->load($node->id());
147 $node->title = $this->randomMachineName();
149 // Update the article with no term changed.
152 // Check that the index was not changed.
153 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
154 ':nid' => $node->id(),
155 ':tid' => $term_1->id(),
157 $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
158 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
159 ':nid' => $node->id(),
160 ':tid' => $term_2->id(),
162 $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
164 // Update the article to change one term.
165 $node->{$this->fieldName1} = [['target_id' => $term_1->id()]];
168 // Check that both terms are indexed.
169 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
170 ':nid' => $node->id(),
171 ':tid' => $term_1->id(),
173 $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
174 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
175 ':nid' => $node->id(),
176 ':tid' => $term_2->id(),
178 $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
180 // Update the article to change another term.
181 $node->{$this->fieldName2} = [['target_id' => $term_1->id()]];
184 // Check that only one term is indexed.
185 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
186 ':nid' => $node->id(),
187 ':tid' => $term_1->id(),
189 $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
190 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
191 ':nid' => $node->id(),
192 ':tid' => $term_2->id(),
194 $this->assertEqual(0, $index_count, 'Term 2 is not indexed.');
198 * Tests that there is a link to the parent term on the child term page.
200 public function testTaxonomyTermHierarchyBreadcrumbs() {
201 // Create two taxonomy terms and set term2 as the parent of term1.
202 $term1 = $this->createTerm($this->vocabulary);
203 $term2 = $this->createTerm($this->vocabulary);
204 $term1->parent = [$term2->id()];
207 // Verify that the page breadcrumbs include a link to the parent term.
208 $this->drupalGet('taxonomy/term/' . $term1->id());
209 // Breadcrumbs are not rendered with a language, prevent the term
210 // language from being added to the options.
211 $this->assertRaw(\Drupal::l($term2->getName(), $term2->urlInfo('canonical', ['language' => NULL])), 'Parent term link is displayed when viewing the node.');