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