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