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