Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\Tests\taxonomy\Functional;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\Component\Utility\Tags;
|
Chris@17
|
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
Chris@17
|
7 use Drupal\field\Entity\FieldConfig;
|
Chris@17
|
8 use Drupal\taxonomy\Entity\Term;
|
Chris@17
|
9 use Drupal\taxonomy\Entity\Vocabulary;
|
Chris@17
|
10
|
Chris@17
|
11 /**
|
Chris@17
|
12 * Tests load, save and delete for taxonomy terms.
|
Chris@17
|
13 *
|
Chris@17
|
14 * @group taxonomy
|
Chris@17
|
15 */
|
Chris@17
|
16 class TermTest extends TaxonomyTestBase {
|
Chris@17
|
17
|
Chris@17
|
18 /**
|
Chris@17
|
19 * Vocabulary for testing.
|
Chris@17
|
20 *
|
Chris@17
|
21 * @var \Drupal\taxonomy\VocabularyInterface
|
Chris@17
|
22 */
|
Chris@17
|
23 protected $vocabulary;
|
Chris@17
|
24
|
Chris@17
|
25 /**
|
Chris@17
|
26 * Taxonomy term reference field for testing.
|
Chris@17
|
27 *
|
Chris@17
|
28 * @var \Drupal\field\FieldConfigInterface
|
Chris@17
|
29 */
|
Chris@17
|
30 protected $field;
|
Chris@17
|
31
|
Chris@17
|
32 /**
|
Chris@17
|
33 * Modules to enable.
|
Chris@17
|
34 *
|
Chris@17
|
35 * @var string[]
|
Chris@17
|
36 */
|
Chris@17
|
37 public static $modules = ['block'];
|
Chris@17
|
38
|
Chris@17
|
39 /**
|
Chris@17
|
40 * {@inheritdoc}
|
Chris@17
|
41 */
|
Chris@17
|
42 protected function setUp() {
|
Chris@17
|
43 parent::setUp();
|
Chris@17
|
44
|
Chris@17
|
45 $this->drupalPlaceBlock('local_actions_block');
|
Chris@17
|
46 $this->drupalPlaceBlock('local_tasks_block');
|
Chris@17
|
47 $this->drupalPlaceBlock('page_title_block');
|
Chris@17
|
48
|
Chris@17
|
49 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
|
Chris@17
|
50 $this->vocabulary = $this->createVocabulary();
|
Chris@17
|
51
|
Chris@17
|
52 $field_name = 'taxonomy_' . $this->vocabulary->id();
|
Chris@17
|
53
|
Chris@17
|
54 $handler_settings = [
|
Chris@17
|
55 'target_bundles' => [
|
Chris@17
|
56 $this->vocabulary->id() => $this->vocabulary->id(),
|
Chris@17
|
57 ],
|
Chris@17
|
58 'auto_create' => TRUE,
|
Chris@17
|
59 ];
|
Chris@17
|
60 $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
|
Chris@17
|
61 $this->field = FieldConfig::loadByName('node', 'article', $field_name);
|
Chris@17
|
62
|
Chris@17
|
63 entity_get_form_display('node', 'article', 'default')
|
Chris@17
|
64 ->setComponent($field_name, [
|
Chris@17
|
65 'type' => 'options_select',
|
Chris@17
|
66 ])
|
Chris@17
|
67 ->save();
|
Chris@17
|
68 entity_get_display('node', 'article', 'default')
|
Chris@17
|
69 ->setComponent($field_name, [
|
Chris@17
|
70 'type' => 'entity_reference_label',
|
Chris@17
|
71 ])
|
Chris@17
|
72 ->save();
|
Chris@17
|
73 }
|
Chris@17
|
74
|
Chris@17
|
75 /**
|
Chris@17
|
76 * The "parent" field must restrict references to the same vocabulary.
|
Chris@17
|
77 */
|
Chris@17
|
78 public function testParentHandlerSettings() {
|
Chris@17
|
79 $vocabulary_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('taxonomy_term', $this->vocabulary->id());
|
Chris@17
|
80 $parent_target_bundles = $vocabulary_fields['parent']->getSetting('handler_settings')['target_bundles'];
|
Chris@17
|
81 $this->assertIdentical([$this->vocabulary->id() => $this->vocabulary->id()], $parent_target_bundles);
|
Chris@17
|
82 }
|
Chris@17
|
83
|
Chris@17
|
84 /**
|
Chris@17
|
85 * Test terms in a single and multiple hierarchy.
|
Chris@17
|
86 */
|
Chris@17
|
87 public function testTaxonomyTermHierarchy() {
|
Chris@17
|
88 // Create two taxonomy terms.
|
Chris@17
|
89 $term1 = $this->createTerm($this->vocabulary);
|
Chris@17
|
90 $term2 = $this->createTerm($this->vocabulary);
|
Chris@17
|
91
|
Chris@17
|
92 // Get the taxonomy storage.
|
Chris@18
|
93 /** @var \Drupal\taxonomy\TermStorageInterface $taxonomy_storage */
|
Chris@18
|
94 $taxonomy_storage = $this->container->get('entity_type.manager')->getStorage('taxonomy_term');
|
Chris@17
|
95
|
Chris@17
|
96 // Check that hierarchy is flat.
|
Chris@18
|
97 $this->assertEquals(0, $taxonomy_storage->getVocabularyHierarchyType($this->vocabulary->id()), 'Vocabulary is flat.');
|
Chris@17
|
98
|
Chris@17
|
99 // Edit $term2, setting $term1 as parent.
|
Chris@17
|
100 $edit = [];
|
Chris@17
|
101 $edit['parent[]'] = [$term1->id()];
|
Chris@17
|
102 $this->drupalPostForm('taxonomy/term/' . $term2->id() . '/edit', $edit, t('Save'));
|
Chris@17
|
103
|
Chris@17
|
104 // Check the hierarchy.
|
Chris@17
|
105 $children = $taxonomy_storage->loadChildren($term1->id());
|
Chris@17
|
106 $parents = $taxonomy_storage->loadParents($term2->id());
|
Chris@17
|
107 $this->assertTrue(isset($children[$term2->id()]), 'Child found correctly.');
|
Chris@17
|
108 $this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');
|
Chris@17
|
109
|
Chris@17
|
110 // Load and save a term, confirming that parents are still set.
|
Chris@17
|
111 $term = Term::load($term2->id());
|
Chris@17
|
112 $term->save();
|
Chris@17
|
113 $parents = $taxonomy_storage->loadParents($term2->id());
|
Chris@17
|
114 $this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');
|
Chris@17
|
115
|
Chris@17
|
116 // Create a third term and save this as a parent of term2.
|
Chris@17
|
117 $term3 = $this->createTerm($this->vocabulary);
|
Chris@17
|
118 $term2->parent = [$term1->id(), $term3->id()];
|
Chris@17
|
119 $term2->save();
|
Chris@17
|
120 $parents = $taxonomy_storage->loadParents($term2->id());
|
Chris@17
|
121 $this->assertTrue(isset($parents[$term1->id()]) && isset($parents[$term3->id()]), 'Both parents found successfully.');
|
Chris@17
|
122 }
|
Chris@17
|
123
|
Chris@17
|
124 /**
|
Chris@17
|
125 * Tests that many terms with parents show on each page
|
Chris@17
|
126 */
|
Chris@17
|
127 public function testTaxonomyTermChildTerms() {
|
Chris@17
|
128 // Set limit to 10 terms per page. Set variable to 9 so 10 terms appear.
|
Chris@17
|
129 $this->config('taxonomy.settings')->set('terms_per_page_admin', '9')->save();
|
Chris@17
|
130 $term1 = $this->createTerm($this->vocabulary);
|
Chris@17
|
131 $terms_array = [];
|
Chris@17
|
132
|
Chris@17
|
133 $taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
|
Chris@17
|
134
|
Chris@17
|
135 // Create 40 terms. Terms 1-12 get parent of $term1. All others are
|
Chris@17
|
136 // individual terms.
|
Chris@17
|
137 for ($x = 1; $x <= 40; $x++) {
|
Chris@17
|
138 $edit = [];
|
Chris@17
|
139 // Set terms in order so we know which terms will be on which pages.
|
Chris@17
|
140 $edit['weight'] = $x;
|
Chris@17
|
141
|
Chris@17
|
142 // Set terms 1-20 to be children of first term created.
|
Chris@17
|
143 if ($x <= 12) {
|
Chris@17
|
144 $edit['parent'] = $term1->id();
|
Chris@17
|
145 }
|
Chris@17
|
146 $term = $this->createTerm($this->vocabulary, $edit);
|
Chris@17
|
147 $children = $taxonomy_storage->loadChildren($term1->id());
|
Chris@17
|
148 $parents = $taxonomy_storage->loadParents($term->id());
|
Chris@17
|
149 $terms_array[$x] = Term::load($term->id());
|
Chris@17
|
150 }
|
Chris@17
|
151
|
Chris@17
|
152 // Get Page 1.
|
Chris@17
|
153 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
|
Chris@17
|
154 $this->assertText($term1->getName(), 'Parent Term is displayed on Page 1');
|
Chris@17
|
155 for ($x = 1; $x <= 13; $x++) {
|
Chris@17
|
156 $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 1');
|
Chris@17
|
157 }
|
Chris@17
|
158
|
Chris@17
|
159 // Get Page 2.
|
Chris@17
|
160 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', ['query' => ['page' => 1]]);
|
Chris@17
|
161 $this->assertText($term1->getName(), 'Parent Term is displayed on Page 2');
|
Chris@17
|
162 for ($x = 1; $x <= 18; $x++) {
|
Chris@17
|
163 $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 2');
|
Chris@17
|
164 }
|
Chris@17
|
165
|
Chris@17
|
166 // Get Page 3.
|
Chris@17
|
167 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', ['query' => ['page' => 2]]);
|
Chris@17
|
168 $this->assertNoText($term1->getName(), 'Parent Term is not displayed on Page 3');
|
Chris@17
|
169 for ($x = 1; $x <= 17; $x++) {
|
Chris@17
|
170 $this->assertNoText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' not found on Page 3');
|
Chris@17
|
171 }
|
Chris@17
|
172 for ($x = 18; $x <= 25; $x++) {
|
Chris@17
|
173 $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 3');
|
Chris@17
|
174 }
|
Chris@17
|
175 }
|
Chris@17
|
176
|
Chris@17
|
177 /**
|
Chris@17
|
178 * Test that hook_node_$op implementations work correctly.
|
Chris@17
|
179 *
|
Chris@17
|
180 * Save & edit a node and assert that taxonomy terms are saved/loaded properly.
|
Chris@17
|
181 */
|
Chris@17
|
182 public function testTaxonomyNode() {
|
Chris@17
|
183 // Create two taxonomy terms.
|
Chris@17
|
184 $term1 = $this->createTerm($this->vocabulary);
|
Chris@17
|
185 $term2 = $this->createTerm($this->vocabulary);
|
Chris@17
|
186
|
Chris@17
|
187 // Post an article.
|
Chris@17
|
188 $edit = [];
|
Chris@17
|
189 $edit['title[0][value]'] = $this->randomMachineName();
|
Chris@17
|
190 $edit['body[0][value]'] = $this->randomMachineName();
|
Chris@17
|
191 $edit[$this->field->getName() . '[]'] = $term1->id();
|
Chris@17
|
192 $this->drupalPostForm('node/add/article', $edit, t('Save'));
|
Chris@17
|
193
|
Chris@17
|
194 // Check that the term is displayed when the node is viewed.
|
Chris@17
|
195 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
Chris@17
|
196 $this->drupalGet('node/' . $node->id());
|
Chris@17
|
197 $this->assertText($term1->getName(), 'Term is displayed when viewing the node.');
|
Chris@17
|
198
|
Chris@17
|
199 $this->clickLink(t('Edit'));
|
Chris@17
|
200 $this->assertText($term1->getName(), 'Term is displayed when editing the node.');
|
Chris@17
|
201 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@17
|
202 $this->assertText($term1->getName(), 'Term is displayed after saving the node with no changes.');
|
Chris@17
|
203
|
Chris@17
|
204 // Edit the node with a different term.
|
Chris@17
|
205 $edit[$this->field->getName() . '[]'] = $term2->id();
|
Chris@17
|
206 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@17
|
207
|
Chris@17
|
208 $this->drupalGet('node/' . $node->id());
|
Chris@17
|
209 $this->assertText($term2->getName(), 'Term is displayed when viewing the node.');
|
Chris@17
|
210
|
Chris@17
|
211 // Preview the node.
|
Chris@17
|
212 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
|
Chris@17
|
213 $this->assertUniqueText($term2->getName(), 'Term is displayed when previewing the node.');
|
Chris@17
|
214 $this->drupalPostForm('node/' . $node->id() . '/edit', NULL, t('Preview'));
|
Chris@17
|
215 $this->assertUniqueText($term2->getName(), 'Term is displayed when previewing the node again.');
|
Chris@17
|
216 }
|
Chris@17
|
217
|
Chris@17
|
218 /**
|
Chris@17
|
219 * Test term creation with a free-tagging vocabulary from the node form.
|
Chris@17
|
220 */
|
Chris@17
|
221 public function testNodeTermCreationAndDeletion() {
|
Chris@17
|
222 // Enable tags in the vocabulary.
|
Chris@17
|
223 $field = $this->field;
|
Chris@17
|
224 entity_get_form_display($field->getTargetEntityTypeId(), $field->getTargetBundle(), 'default')
|
Chris@17
|
225 ->setComponent($field->getName(), [
|
Chris@17
|
226 'type' => 'entity_reference_autocomplete_tags',
|
Chris@17
|
227 'settings' => [
|
Chris@17
|
228 'placeholder' => 'Start typing here.',
|
Chris@17
|
229 ],
|
Chris@17
|
230 ])
|
Chris@17
|
231 ->save();
|
Chris@17
|
232 // Prefix the terms with a letter to ensure there is no clash in the first
|
Chris@17
|
233 // three letters.
|
Chris@17
|
234 // @see https://www.drupal.org/node/2397691
|
Chris@17
|
235 $terms = [
|
Chris@17
|
236 'term1' => 'a' . $this->randomMachineName(),
|
Chris@17
|
237 'term2' => 'b' . $this->randomMachineName(),
|
Chris@17
|
238 'term3' => 'c' . $this->randomMachineName() . ', ' . $this->randomMachineName(),
|
Chris@17
|
239 'term4' => 'd' . $this->randomMachineName(),
|
Chris@17
|
240 ];
|
Chris@17
|
241
|
Chris@17
|
242 $edit = [];
|
Chris@17
|
243 $edit['title[0][value]'] = $this->randomMachineName();
|
Chris@17
|
244 $edit['body[0][value]'] = $this->randomMachineName();
|
Chris@17
|
245 // Insert the terms in a comma separated list. Vocabulary 1 is a
|
Chris@17
|
246 // free-tagging field created by the default profile.
|
Chris@17
|
247 $edit[$field->getName() . '[target_id]'] = Tags::implode($terms);
|
Chris@17
|
248
|
Chris@17
|
249 // Verify the placeholder is there.
|
Chris@17
|
250 $this->drupalGet('node/add/article');
|
Chris@17
|
251 $this->assertRaw('placeholder="Start typing here."', 'Placeholder is present.');
|
Chris@17
|
252
|
Chris@17
|
253 // Preview and verify the terms appear but are not created.
|
Chris@17
|
254 $this->drupalPostForm(NULL, $edit, t('Preview'));
|
Chris@17
|
255 foreach ($terms as $term) {
|
Chris@17
|
256 $this->assertText($term, 'The term appears on the node preview.');
|
Chris@17
|
257 }
|
Chris@17
|
258 $tree = $this->container->get('entity.manager')->getStorage('taxonomy_term')->loadTree($this->vocabulary->id());
|
Chris@17
|
259 $this->assertTrue(empty($tree), 'The terms are not created on preview.');
|
Chris@17
|
260
|
Chris@17
|
261 // taxonomy.module does not maintain its static caches.
|
Chris@17
|
262 taxonomy_terms_static_reset();
|
Chris@17
|
263
|
Chris@17
|
264 // Save, creating the terms.
|
Chris@17
|
265 $this->drupalPostForm('node/add/article', $edit, t('Save'));
|
Chris@17
|
266 $this->assertText(t('@type @title has been created.', ['@type' => t('Article'), '@title' => $edit['title[0][value]']]), 'The node was created successfully.');
|
Chris@17
|
267
|
Chris@17
|
268 // Verify that the creation message contains a link to a node.
|
Chris@17
|
269 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/']);
|
Chris@17
|
270 $this->assert(isset($view_link), 'The message area contains a link to a node');
|
Chris@17
|
271
|
Chris@17
|
272 foreach ($terms as $term) {
|
Chris@17
|
273 $this->assertText($term, 'The term was saved and appears on the node page.');
|
Chris@17
|
274 }
|
Chris@17
|
275
|
Chris@17
|
276 // Get the created terms.
|
Chris@17
|
277 $term_objects = [];
|
Chris@17
|
278 foreach ($terms as $key => $term) {
|
Chris@17
|
279 $term_objects[$key] = taxonomy_term_load_multiple_by_name($term);
|
Chris@17
|
280 $term_objects[$key] = reset($term_objects[$key]);
|
Chris@17
|
281 }
|
Chris@17
|
282
|
Chris@17
|
283 // Get the node.
|
Chris@17
|
284 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
Chris@17
|
285
|
Chris@17
|
286 // Test editing the node.
|
Chris@17
|
287 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@17
|
288 foreach ($terms as $term) {
|
Chris@17
|
289 $this->assertText($term, 'The term was retained after edit and still appears on the node page.');
|
Chris@17
|
290 }
|
Chris@17
|
291
|
Chris@17
|
292 // Delete term 1 from the term edit page.
|
Chris@17
|
293 $this->drupalGet('taxonomy/term/' . $term_objects['term1']->id() . '/edit');
|
Chris@17
|
294 $this->clickLink(t('Delete'));
|
Chris@17
|
295 $this->drupalPostForm(NULL, NULL, t('Delete'));
|
Chris@17
|
296
|
Chris@17
|
297 // Delete term 2 from the term delete page.
|
Chris@17
|
298 $this->drupalGet('taxonomy/term/' . $term_objects['term2']->id() . '/delete');
|
Chris@17
|
299 $this->drupalPostForm(NULL, [], t('Delete'));
|
Chris@17
|
300 $term_names = [$term_objects['term3']->getName(), $term_objects['term4']->getName()];
|
Chris@17
|
301
|
Chris@17
|
302 $this->drupalGet('node/' . $node->id());
|
Chris@17
|
303
|
Chris@17
|
304 foreach ($term_names as $term_name) {
|
Chris@17
|
305 $this->assertText($term_name, format_string('The term %name appears on the node page after two terms, %deleted1 and %deleted2, were deleted.', ['%name' => $term_name, '%deleted1' => $term_objects['term1']->getName(), '%deleted2' => $term_objects['term2']->getName()]));
|
Chris@17
|
306 }
|
Chris@17
|
307 $this->assertNoText($term_objects['term1']->getName(), format_string('The deleted term %name does not appear on the node page.', ['%name' => $term_objects['term1']->getName()]));
|
Chris@17
|
308 $this->assertNoText($term_objects['term2']->getName(), format_string('The deleted term %name does not appear on the node page.', ['%name' => $term_objects['term2']->getName()]));
|
Chris@17
|
309 }
|
Chris@17
|
310
|
Chris@17
|
311 /**
|
Chris@17
|
312 * Save, edit and delete a term using the user interface.
|
Chris@17
|
313 */
|
Chris@17
|
314 public function testTermInterface() {
|
Chris@17
|
315 \Drupal::service('module_installer')->install(['views']);
|
Chris@17
|
316 $edit = [
|
Chris@17
|
317 'name[0][value]' => $this->randomMachineName(12),
|
Chris@17
|
318 'description[0][value]' => $this->randomMachineName(100),
|
Chris@17
|
319 ];
|
Chris@17
|
320 // Explicitly set the parents field to 'root', to ensure that
|
Chris@17
|
321 // TermForm::save() handles the invalid term ID correctly.
|
Chris@17
|
322 $edit['parent[]'] = [0];
|
Chris@17
|
323
|
Chris@17
|
324 // Create the term to edit.
|
Chris@17
|
325 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
|
Chris@17
|
326
|
Chris@17
|
327 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
|
Chris@17
|
328 $term = reset($terms);
|
Chris@17
|
329 $this->assertNotNull($term, 'Term found in database.');
|
Chris@17
|
330
|
Chris@17
|
331 // Submitting a term takes us to the add page; we need the List page.
|
Chris@17
|
332 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
|
Chris@17
|
333
|
Chris@17
|
334 $this->clickLink(t('Edit'));
|
Chris@17
|
335
|
Chris@17
|
336 $this->assertRaw($edit['name[0][value]'], 'The randomly generated term name is present.');
|
Chris@17
|
337 $this->assertText($edit['description[0][value]'], 'The randomly generated term description is present.');
|
Chris@17
|
338
|
Chris@17
|
339 $edit = [
|
Chris@17
|
340 'name[0][value]' => $this->randomMachineName(14),
|
Chris@17
|
341 'description[0][value]' => $this->randomMachineName(102),
|
Chris@17
|
342 ];
|
Chris@17
|
343
|
Chris@17
|
344 // Edit the term.
|
Chris@17
|
345 $this->drupalPostForm('taxonomy/term/' . $term->id() . '/edit', $edit, t('Save'));
|
Chris@17
|
346
|
Chris@17
|
347 // Check that the term is still present at admin UI after edit.
|
Chris@17
|
348 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
|
Chris@17
|
349 $this->assertText($edit['name[0][value]'], 'The randomly generated term name is present.');
|
Chris@17
|
350 $this->assertLink(t('Edit'));
|
Chris@17
|
351
|
Chris@17
|
352 // Check the term link can be clicked through to the term page.
|
Chris@17
|
353 $this->clickLink($edit['name[0][value]']);
|
Chris@17
|
354 $this->assertResponse(200, 'Term page can be accessed via the listing link.');
|
Chris@17
|
355
|
Chris@17
|
356 // View the term and check that it is correct.
|
Chris@17
|
357 $this->drupalGet('taxonomy/term/' . $term->id());
|
Chris@17
|
358 $this->assertText($edit['name[0][value]'], 'The randomly generated term name is present.');
|
Chris@17
|
359 $this->assertText($edit['description[0][value]'], 'The randomly generated term description is present.');
|
Chris@17
|
360
|
Chris@17
|
361 // Did this page request display a 'term-listing-heading'?
|
Chris@17
|
362 $this->assertTrue($this->xpath('//div[contains(@class, "field--name-description")]'), 'Term page displayed the term description element.');
|
Chris@17
|
363 // Check that it does NOT show a description when description is blank.
|
Chris@17
|
364 $term->setDescription(NULL);
|
Chris@17
|
365 $term->save();
|
Chris@17
|
366 $this->drupalGet('taxonomy/term/' . $term->id());
|
Chris@17
|
367 $this->assertFalse($this->xpath('//div[contains(@class, "field--entity-taxonomy-term--description")]'), 'Term page did not display the term description when description was blank.');
|
Chris@17
|
368
|
Chris@17
|
369 // Check that the description value is processed.
|
Chris@17
|
370 $value = $this->randomMachineName();
|
Chris@17
|
371 $term->setDescription($value);
|
Chris@17
|
372 $term->save();
|
Chris@17
|
373 $this->assertEqual($term->description->processed, "<p>$value</p>\n");
|
Chris@17
|
374
|
Chris@17
|
375 // Check that the term feed page is working.
|
Chris@17
|
376 $this->drupalGet('taxonomy/term/' . $term->id() . '/feed');
|
Chris@17
|
377
|
Chris@17
|
378 // Delete the term.
|
Chris@17
|
379 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
|
Chris@17
|
380 $this->clickLink(t('Delete'));
|
Chris@17
|
381 $this->drupalPostForm(NULL, NULL, t('Delete'));
|
Chris@17
|
382
|
Chris@17
|
383 // Assert that the term no longer exists.
|
Chris@17
|
384 $this->drupalGet('taxonomy/term/' . $term->id());
|
Chris@17
|
385 $this->assertResponse(404, 'The taxonomy term page was not found.');
|
Chris@17
|
386 }
|
Chris@17
|
387
|
Chris@17
|
388 /**
|
Chris@17
|
389 * Save, edit and delete a term using the user interface.
|
Chris@17
|
390 */
|
Chris@17
|
391 public function testTermReorder() {
|
Chris@17
|
392 $assert = $this->assertSession();
|
Chris@17
|
393 $this->createTerm($this->vocabulary);
|
Chris@17
|
394 $this->createTerm($this->vocabulary);
|
Chris@17
|
395 $this->createTerm($this->vocabulary);
|
Chris@17
|
396
|
Chris@17
|
397 $taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
|
Chris@17
|
398
|
Chris@17
|
399 // Fetch the created terms in the default alphabetical order, i.e. term1
|
Chris@17
|
400 // precedes term2 alphabetically, and term2 precedes term3.
|
Chris@17
|
401 $taxonomy_storage->resetCache();
|
Chris@17
|
402 list($term1, $term2, $term3) = $taxonomy_storage->loadTree($this->vocabulary->id(), 0, NULL, TRUE);
|
Chris@17
|
403
|
Chris@17
|
404 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
|
Chris@17
|
405
|
Chris@17
|
406 // Each term has four hidden fields, "tid:1:0[tid]", "tid:1:0[parent]",
|
Chris@17
|
407 // "tid:1:0[depth]", and "tid:1:0[weight]". Change the order to term2,
|
Chris@17
|
408 // term3, term1 by setting weight property, make term3 a child of term2 by
|
Chris@17
|
409 // setting the parent and depth properties, and update all hidden fields.
|
Chris@17
|
410 $hidden_edit = [
|
Chris@17
|
411 'terms[tid:' . $term2->id() . ':0][term][tid]' => $term2->id(),
|
Chris@17
|
412 'terms[tid:' . $term2->id() . ':0][term][parent]' => 0,
|
Chris@17
|
413 'terms[tid:' . $term2->id() . ':0][term][depth]' => 0,
|
Chris@17
|
414 'terms[tid:' . $term3->id() . ':0][term][tid]' => $term3->id(),
|
Chris@17
|
415 'terms[tid:' . $term3->id() . ':0][term][parent]' => $term2->id(),
|
Chris@17
|
416 'terms[tid:' . $term3->id() . ':0][term][depth]' => 1,
|
Chris@17
|
417 'terms[tid:' . $term1->id() . ':0][term][tid]' => $term1->id(),
|
Chris@17
|
418 'terms[tid:' . $term1->id() . ':0][term][parent]' => 0,
|
Chris@17
|
419 'terms[tid:' . $term1->id() . ':0][term][depth]' => 0,
|
Chris@17
|
420 ];
|
Chris@17
|
421 // Because we can't post hidden form elements, we have to change them in
|
Chris@17
|
422 // code here, and then submit.
|
Chris@17
|
423 foreach ($hidden_edit as $field => $value) {
|
Chris@17
|
424 $node = $assert->hiddenFieldExists($field);
|
Chris@17
|
425 $node->setValue($value);
|
Chris@17
|
426 }
|
Chris@17
|
427 // Edit non-hidden elements within drupalPostForm().
|
Chris@17
|
428 $edit = [
|
Chris@17
|
429 'terms[tid:' . $term2->id() . ':0][weight]' => 0,
|
Chris@17
|
430 'terms[tid:' . $term3->id() . ':0][weight]' => 1,
|
Chris@17
|
431 'terms[tid:' . $term1->id() . ':0][weight]' => 2,
|
Chris@17
|
432 ];
|
Chris@17
|
433 $this->drupalPostForm(NULL, $edit, 'Save');
|
Chris@17
|
434
|
Chris@17
|
435 $taxonomy_storage->resetCache();
|
Chris@17
|
436 $terms = $taxonomy_storage->loadTree($this->vocabulary->id());
|
Chris@17
|
437 $this->assertEqual($terms[0]->tid, $term2->id(), 'Term 2 was moved above term 1.');
|
Chris@17
|
438 $this->assertEqual($terms[1]->parents, [$term2->id()], 'Term 3 was made a child of term 2.');
|
Chris@17
|
439 $this->assertEqual($terms[2]->tid, $term1->id(), 'Term 1 was moved below term 2.');
|
Chris@17
|
440
|
Chris@17
|
441 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', [], t('Reset to alphabetical'));
|
Chris@17
|
442 // Submit confirmation form.
|
Chris@17
|
443 $this->drupalPostForm(NULL, [], t('Reset to alphabetical'));
|
Chris@17
|
444 // Ensure form redirected back to overview.
|
Chris@17
|
445 $this->assertUrl('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
|
Chris@17
|
446
|
Chris@17
|
447 $taxonomy_storage->resetCache();
|
Chris@17
|
448 $terms = $taxonomy_storage->loadTree($this->vocabulary->id(), 0, NULL, TRUE);
|
Chris@17
|
449 $this->assertEqual($terms[0]->id(), $term1->id(), 'Term 1 was moved to back above term 2.');
|
Chris@17
|
450 $this->assertEqual($terms[1]->id(), $term2->id(), 'Term 2 was moved to back below term 1.');
|
Chris@17
|
451 $this->assertEqual($terms[2]->id(), $term3->id(), 'Term 3 is still below term 2.');
|
Chris@17
|
452 $this->assertEqual($terms[2]->parents, [$term2->id()], 'Term 3 is still a child of term 2.');
|
Chris@17
|
453 }
|
Chris@17
|
454
|
Chris@17
|
455 /**
|
Chris@17
|
456 * Test saving a term with multiple parents through the UI.
|
Chris@17
|
457 */
|
Chris@17
|
458 public function testTermMultipleParentsInterface() {
|
Chris@17
|
459 // Add a new term to the vocabulary so that we can have multiple parents.
|
Chris@17
|
460 $parent = $this->createTerm($this->vocabulary);
|
Chris@17
|
461
|
Chris@17
|
462 // Add a new term with multiple parents.
|
Chris@17
|
463 $edit = [
|
Chris@17
|
464 'name[0][value]' => $this->randomMachineName(12),
|
Chris@17
|
465 'description[0][value]' => $this->randomMachineName(100),
|
Chris@17
|
466 'parent[]' => [0, $parent->id()],
|
Chris@17
|
467 ];
|
Chris@17
|
468 // Save the new term.
|
Chris@17
|
469 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
|
Chris@17
|
470
|
Chris@17
|
471 // Check that the term was successfully created.
|
Chris@17
|
472 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
|
Chris@17
|
473 $term = reset($terms);
|
Chris@17
|
474 $this->assertNotNull($term, 'Term found in database.');
|
Chris@17
|
475 $this->assertEqual($edit['name[0][value]'], $term->getName(), 'Term name was successfully saved.');
|
Chris@17
|
476 $this->assertEqual($edit['description[0][value]'], $term->getDescription(), 'Term description was successfully saved.');
|
Chris@17
|
477 // Check that the parent tid is still there. The other parent (<root>) is
|
Chris@17
|
478 // not added by \Drupal\taxonomy\TermStorageInterface::loadParents().
|
Chris@17
|
479 $parents = $this->container->get('entity.manager')->getStorage('taxonomy_term')->loadParents($term->id());
|
Chris@17
|
480 $parent = reset($parents);
|
Chris@17
|
481 $this->assertEqual($edit['parent[]'][1], $parent->id(), 'Term parents were successfully saved.');
|
Chris@17
|
482 }
|
Chris@17
|
483
|
Chris@17
|
484 /**
|
Chris@17
|
485 * Test taxonomy_term_load_multiple_by_name().
|
Chris@17
|
486 */
|
Chris@17
|
487 public function testTaxonomyGetTermByName() {
|
Chris@17
|
488 $term = $this->createTerm($this->vocabulary);
|
Chris@17
|
489
|
Chris@17
|
490 // Load the term with the exact name.
|
Chris@17
|
491 $terms = taxonomy_term_load_multiple_by_name($term->getName());
|
Chris@17
|
492 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded using exact name.');
|
Chris@17
|
493
|
Chris@17
|
494 // Load the term with space concatenated.
|
Chris@17
|
495 $terms = taxonomy_term_load_multiple_by_name(' ' . $term->getName() . ' ');
|
Chris@17
|
496 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded with extra whitespace.');
|
Chris@17
|
497
|
Chris@17
|
498 // Load the term with name uppercased.
|
Chris@17
|
499 $terms = taxonomy_term_load_multiple_by_name(strtoupper($term->getName()));
|
Chris@17
|
500 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded with uppercased name.');
|
Chris@17
|
501
|
Chris@17
|
502 // Load the term with name lowercased.
|
Chris@17
|
503 $terms = taxonomy_term_load_multiple_by_name(strtolower($term->getName()));
|
Chris@17
|
504 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded with lowercased name.');
|
Chris@17
|
505
|
Chris@17
|
506 // Try to load an invalid term name.
|
Chris@17
|
507 $terms = taxonomy_term_load_multiple_by_name('Banana');
|
Chris@17
|
508 $this->assertFalse($terms, 'No term loaded with an invalid name.');
|
Chris@17
|
509
|
Chris@17
|
510 // Try to load the term using a substring of the name.
|
Chris@17
|
511 $terms = taxonomy_term_load_multiple_by_name(mb_substr($term->getName(), 2), 'No term loaded with a substring of the name.');
|
Chris@17
|
512 $this->assertFalse($terms);
|
Chris@17
|
513
|
Chris@17
|
514 // Create a new term in a different vocabulary with the same name.
|
Chris@17
|
515 $new_vocabulary = $this->createVocabulary();
|
Chris@17
|
516 $new_term = Term::create([
|
Chris@17
|
517 'name' => $term->getName(),
|
Chris@17
|
518 'vid' => $new_vocabulary->id(),
|
Chris@17
|
519 ]);
|
Chris@17
|
520 $new_term->save();
|
Chris@17
|
521
|
Chris@17
|
522 // Load multiple terms with the same name.
|
Chris@17
|
523 $terms = taxonomy_term_load_multiple_by_name($term->getName());
|
Chris@17
|
524 $this->assertEqual(count($terms), 2, 'Two terms loaded with the same name.');
|
Chris@17
|
525
|
Chris@17
|
526 // Load single term when restricted to one vocabulary.
|
Chris@17
|
527 $terms = taxonomy_term_load_multiple_by_name($term->getName(), $this->vocabulary->id());
|
Chris@17
|
528 $this->assertEqual(count($terms), 1, 'One term loaded when restricted by vocabulary.');
|
Chris@17
|
529 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded using exact name and vocabulary machine name.');
|
Chris@17
|
530
|
Chris@17
|
531 // Create a new term with another name.
|
Chris@17
|
532 $term2 = $this->createTerm($this->vocabulary);
|
Chris@17
|
533
|
Chris@17
|
534 // Try to load a term by name that doesn't exist in this vocabulary but
|
Chris@17
|
535 // exists in another vocabulary.
|
Chris@17
|
536 $terms = taxonomy_term_load_multiple_by_name($term2->getName(), $new_vocabulary->id());
|
Chris@17
|
537 $this->assertFalse($terms, 'Invalid term name restricted by vocabulary machine name not loaded.');
|
Chris@17
|
538
|
Chris@17
|
539 // Try to load terms filtering by a non-existing vocabulary.
|
Chris@17
|
540 $terms = taxonomy_term_load_multiple_by_name($term2->getName(), 'non_existing_vocabulary');
|
Chris@17
|
541 $this->assertEqual(count($terms), 0, 'No terms loaded when restricted by a non-existing vocabulary.');
|
Chris@17
|
542 }
|
Chris@17
|
543
|
Chris@17
|
544 /**
|
Chris@17
|
545 * Tests that editing and saving a node with no changes works correctly.
|
Chris@17
|
546 */
|
Chris@17
|
547 public function testReSavingTags() {
|
Chris@17
|
548 // Enable tags in the vocabulary.
|
Chris@17
|
549 $field = $this->field;
|
Chris@17
|
550 entity_get_form_display($field->getTargetEntityTypeId(), $field->getTargetBundle(), 'default')
|
Chris@17
|
551 ->setComponent($field->getName(), [
|
Chris@17
|
552 'type' => 'entity_reference_autocomplete_tags',
|
Chris@17
|
553 ])
|
Chris@17
|
554 ->save();
|
Chris@17
|
555
|
Chris@17
|
556 // Create a term and a node using it.
|
Chris@17
|
557 $term = $this->createTerm($this->vocabulary);
|
Chris@17
|
558 $edit = [];
|
Chris@17
|
559 $edit['title[0][value]'] = $this->randomMachineName(8);
|
Chris@17
|
560 $edit['body[0][value]'] = $this->randomMachineName(16);
|
Chris@17
|
561 $edit[$this->field->getName() . '[target_id]'] = $term->getName();
|
Chris@17
|
562 $this->drupalPostForm('node/add/article', $edit, t('Save'));
|
Chris@17
|
563
|
Chris@17
|
564 // Check that the term is displayed when editing and saving the node with no
|
Chris@17
|
565 // changes.
|
Chris@17
|
566 $this->clickLink(t('Edit'));
|
Chris@17
|
567 $this->assertRaw($term->getName(), 'Term is displayed when editing the node.');
|
Chris@17
|
568 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@17
|
569 $this->assertRaw($term->getName(), 'Term is displayed after saving the node with no changes.');
|
Chris@17
|
570 }
|
Chris@17
|
571
|
Chris@17
|
572 /**
|
Chris@17
|
573 * Check the breadcrumb on edit and delete a term page.
|
Chris@17
|
574 */
|
Chris@17
|
575 public function testTermBreadcrumbs() {
|
Chris@17
|
576 $edit = [
|
Chris@17
|
577 'name[0][value]' => $this->randomMachineName(14),
|
Chris@17
|
578 'description[0][value]' => $this->randomMachineName(100),
|
Chris@17
|
579 'parent[]' => [0],
|
Chris@17
|
580 ];
|
Chris@17
|
581
|
Chris@17
|
582 // Create the term.
|
Chris@17
|
583 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, 'Save');
|
Chris@17
|
584
|
Chris@17
|
585 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
|
Chris@17
|
586 $term = reset($terms);
|
Chris@17
|
587 $this->assertNotNull($term, 'Term found in database.');
|
Chris@17
|
588
|
Chris@17
|
589 // Check the breadcrumb on the term edit page.
|
Chris@17
|
590 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
|
Chris@17
|
591 $breadcrumbs = $this->getSession()->getPage()->findAll('css', 'nav.breadcrumb ol li a');
|
Chris@17
|
592 $this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.');
|
Chris@17
|
593 $this->assertIdentical($breadcrumbs[0]->getText(), 'Home', 'First breadcrumb text is Home');
|
Chris@17
|
594 $this->assertIdentical($breadcrumbs[1]->getText(), $term->label(), 'Second breadcrumb text is term name on term edit page.');
|
Chris@17
|
595 $this->assertEscaped($breadcrumbs[1]->getText(), 'breadcrumbs displayed and escaped.');
|
Chris@17
|
596
|
Chris@17
|
597 // Check the breadcrumb on the term delete page.
|
Chris@17
|
598 $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
|
Chris@17
|
599 $breadcrumbs = $this->getSession()->getPage()->findAll('css', 'nav.breadcrumb ol li a');
|
Chris@17
|
600 $this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.');
|
Chris@17
|
601 $this->assertIdentical($breadcrumbs[0]->getText(), 'Home', 'First breadcrumb text is Home');
|
Chris@17
|
602 $this->assertIdentical($breadcrumbs[1]->getText(), $term->label(), 'Second breadcrumb text is term name on term delete page.');
|
Chris@17
|
603 $this->assertEscaped($breadcrumbs[1]->getText(), 'breadcrumbs displayed and escaped.');
|
Chris@17
|
604 }
|
Chris@17
|
605
|
Chris@17
|
606 }
|