Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\taxonomy\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Tests the hook implementations that maintain the taxonomy index.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @group taxonomy
|
Chris@0
|
11 */
|
Chris@0
|
12 class TermIndexTest extends TaxonomyTestBase {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Modules to enable.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @var array
|
Chris@0
|
18 */
|
Chris@0
|
19 public static $modules = ['views'];
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Vocabulary for testing.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var \Drupal\taxonomy\VocabularyInterface
|
Chris@0
|
25 */
|
Chris@0
|
26 protected $vocabulary;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Name of the taxonomy term reference field.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var string
|
Chris@0
|
32 */
|
Chris@0
|
33 protected $fieldName1;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Name of the taxonomy term reference field.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @var string
|
Chris@0
|
39 */
|
Chris@0
|
40 protected $fieldName2;
|
Chris@0
|
41
|
Chris@0
|
42 protected function setUp() {
|
Chris@0
|
43 parent::setUp();
|
Chris@0
|
44
|
Chris@0
|
45 // Create an administrative user.
|
Chris@0
|
46 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
|
Chris@0
|
47
|
Chris@0
|
48 // Create a vocabulary and add two term reference fields to article nodes.
|
Chris@0
|
49 $this->vocabulary = $this->createVocabulary();
|
Chris@0
|
50
|
Chris@17
|
51 $this->fieldName1 = mb_strtolower($this->randomMachineName());
|
Chris@0
|
52 $handler_settings = [
|
Chris@0
|
53 'target_bundles' => [
|
Chris@0
|
54 $this->vocabulary->id() => $this->vocabulary->id(),
|
Chris@0
|
55 ],
|
Chris@0
|
56 'auto_create' => TRUE,
|
Chris@0
|
57 ];
|
Chris@0
|
58 $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
|
Chris@0
|
59
|
Chris@0
|
60 entity_get_form_display('node', 'article', 'default')
|
Chris@0
|
61 ->setComponent($this->fieldName1, [
|
Chris@0
|
62 'type' => 'options_select',
|
Chris@0
|
63 ])
|
Chris@0
|
64 ->save();
|
Chris@0
|
65 entity_get_display('node', 'article', 'default')
|
Chris@0
|
66 ->setComponent($this->fieldName1, [
|
Chris@0
|
67 'type' => 'entity_reference_label',
|
Chris@0
|
68 ])
|
Chris@0
|
69 ->save();
|
Chris@0
|
70
|
Chris@17
|
71 $this->fieldName2 = mb_strtolower($this->randomMachineName());
|
Chris@0
|
72 $this->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
|
Chris@0
|
73
|
Chris@0
|
74 entity_get_form_display('node', 'article', 'default')
|
Chris@0
|
75 ->setComponent($this->fieldName2, [
|
Chris@0
|
76 'type' => 'options_select',
|
Chris@0
|
77 ])
|
Chris@0
|
78 ->save();
|
Chris@0
|
79 entity_get_display('node', 'article', 'default')
|
Chris@0
|
80 ->setComponent($this->fieldName2, [
|
Chris@0
|
81 'type' => 'entity_reference_label',
|
Chris@0
|
82 ])
|
Chris@0
|
83 ->save();
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Tests that the taxonomy index is maintained properly.
|
Chris@0
|
88 */
|
Chris@0
|
89 public function testTaxonomyIndex() {
|
Chris@0
|
90 $node_storage = $this->container->get('entity.manager')->getStorage('node');
|
Chris@0
|
91 // Create terms in the vocabulary.
|
Chris@0
|
92 $term_1 = $this->createTerm($this->vocabulary);
|
Chris@0
|
93 $term_2 = $this->createTerm($this->vocabulary);
|
Chris@0
|
94
|
Chris@0
|
95 // Post an article.
|
Chris@0
|
96 $edit = [];
|
Chris@0
|
97 $edit['title[0][value]'] = $this->randomMachineName();
|
Chris@0
|
98 $edit['body[0][value]'] = $this->randomMachineName();
|
Chris@0
|
99 $edit["{$this->fieldName1}[]"] = $term_1->id();
|
Chris@0
|
100 $edit["{$this->fieldName2}[]"] = $term_1->id();
|
Chris@0
|
101 $this->drupalPostForm('node/add/article', $edit, t('Save'));
|
Chris@0
|
102
|
Chris@0
|
103 // Check that the term is indexed, and only once.
|
Chris@0
|
104 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
Chris@0
|
105 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
106 ':nid' => $node->id(),
|
Chris@0
|
107 ':tid' => $term_1->id(),
|
Chris@0
|
108 ])->fetchField();
|
Chris@0
|
109 $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
|
Chris@0
|
110
|
Chris@0
|
111 // Update the article to change one term.
|
Chris@0
|
112 $edit["{$this->fieldName1}[]"] = $term_2->id();
|
Chris@0
|
113 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@0
|
114
|
Chris@0
|
115 // Check that both terms are indexed.
|
Chris@0
|
116 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
117 ':nid' => $node->id(),
|
Chris@0
|
118 ':tid' => $term_1->id(),
|
Chris@0
|
119 ])->fetchField();
|
Chris@0
|
120 $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
|
Chris@0
|
121 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
122 ':nid' => $node->id(),
|
Chris@0
|
123 ':tid' => $term_2->id(),
|
Chris@0
|
124 ])->fetchField();
|
Chris@0
|
125 $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
|
Chris@0
|
126
|
Chris@0
|
127 // Update the article to change another term.
|
Chris@0
|
128 $edit["{$this->fieldName2}[]"] = $term_2->id();
|
Chris@0
|
129 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@0
|
130
|
Chris@0
|
131 // Check that only one term is indexed.
|
Chris@0
|
132 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
133 ':nid' => $node->id(),
|
Chris@0
|
134 ':tid' => $term_1->id(),
|
Chris@0
|
135 ])->fetchField();
|
Chris@0
|
136 $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
|
Chris@0
|
137 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
138 ':nid' => $node->id(),
|
Chris@0
|
139 ':tid' => $term_2->id(),
|
Chris@0
|
140 ])->fetchField();
|
Chris@0
|
141 $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
|
Chris@0
|
142
|
Chris@0
|
143 // Redo the above tests without interface.
|
Chris@0
|
144 $node_storage->resetCache([$node->id()]);
|
Chris@0
|
145 $node = $node_storage->load($node->id());
|
Chris@0
|
146 $node->title = $this->randomMachineName();
|
Chris@0
|
147
|
Chris@0
|
148 // Update the article with no term changed.
|
Chris@0
|
149 $node->save();
|
Chris@0
|
150
|
Chris@0
|
151 // Check that the index was not changed.
|
Chris@0
|
152 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
153 ':nid' => $node->id(),
|
Chris@0
|
154 ':tid' => $term_1->id(),
|
Chris@0
|
155 ])->fetchField();
|
Chris@0
|
156 $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
|
Chris@0
|
157 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
158 ':nid' => $node->id(),
|
Chris@0
|
159 ':tid' => $term_2->id(),
|
Chris@0
|
160 ])->fetchField();
|
Chris@0
|
161 $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
|
Chris@0
|
162
|
Chris@0
|
163 // Update the article to change one term.
|
Chris@0
|
164 $node->{$this->fieldName1} = [['target_id' => $term_1->id()]];
|
Chris@0
|
165 $node->save();
|
Chris@0
|
166
|
Chris@0
|
167 // Check that both terms are indexed.
|
Chris@0
|
168 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
169 ':nid' => $node->id(),
|
Chris@0
|
170 ':tid' => $term_1->id(),
|
Chris@0
|
171 ])->fetchField();
|
Chris@0
|
172 $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
|
Chris@0
|
173 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
174 ':nid' => $node->id(),
|
Chris@0
|
175 ':tid' => $term_2->id(),
|
Chris@0
|
176 ])->fetchField();
|
Chris@0
|
177 $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
|
Chris@0
|
178
|
Chris@0
|
179 // Update the article to change another term.
|
Chris@0
|
180 $node->{$this->fieldName2} = [['target_id' => $term_1->id()]];
|
Chris@0
|
181 $node->save();
|
Chris@0
|
182
|
Chris@0
|
183 // Check that only one term is indexed.
|
Chris@0
|
184 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
185 ':nid' => $node->id(),
|
Chris@0
|
186 ':tid' => $term_1->id(),
|
Chris@0
|
187 ])->fetchField();
|
Chris@0
|
188 $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
|
Chris@0
|
189 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
|
Chris@0
|
190 ':nid' => $node->id(),
|
Chris@0
|
191 ':tid' => $term_2->id(),
|
Chris@0
|
192 ])->fetchField();
|
Chris@0
|
193 $this->assertEqual(0, $index_count, 'Term 2 is not indexed.');
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * Tests that there is a link to the parent term on the child term page.
|
Chris@0
|
198 */
|
Chris@0
|
199 public function testTaxonomyTermHierarchyBreadcrumbs() {
|
Chris@0
|
200 // Create two taxonomy terms and set term2 as the parent of term1.
|
Chris@0
|
201 $term1 = $this->createTerm($this->vocabulary);
|
Chris@0
|
202 $term2 = $this->createTerm($this->vocabulary);
|
Chris@0
|
203 $term1->parent = [$term2->id()];
|
Chris@0
|
204 $term1->save();
|
Chris@0
|
205
|
Chris@0
|
206 // Verify that the page breadcrumbs include a link to the parent term.
|
Chris@0
|
207 $this->drupalGet('taxonomy/term/' . $term1->id());
|
Chris@0
|
208 // Breadcrumbs are not rendered with a language, prevent the term
|
Chris@0
|
209 // language from being added to the options.
|
Chris@18
|
210 $this->assertRaw(\Drupal::l($term2->getName(), $term2->toUrl('canonical', ['language' => NULL])), 'Parent term link is displayed when viewing the node.');
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 }
|