function TermTest::testNodeTermCreationAndDeletion
Same name in other branches
- 9 core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testNodeTermCreationAndDeletion()
- 10 core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testNodeTermCreationAndDeletion()
- 11.x core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testNodeTermCreationAndDeletion()
Test term creation with a free-tagging vocabulary from the node form.
File
-
core/
modules/ taxonomy/ tests/ src/ Functional/ TermTest.php, line 231
Class
- TermTest
- Tests load, save and delete for taxonomy terms.
Namespace
Drupal\Tests\taxonomy\FunctionalCode
public function testNodeTermCreationAndDeletion() {
// Enable tags in the vocabulary.
$field = $this->field;
\Drupal::service('entity_display.repository')->getFormDisplay($field->getTargetEntityTypeId(), $field->getTargetBundle())
->setComponent($field->getName(), [
'type' => 'entity_reference_autocomplete_tags',
'settings' => [
'placeholder' => 'Start typing here.',
],
])
->save();
// Prefix the terms with a letter to ensure there is no clash in the first
// three letters.
// @see https://www.drupal.org/node/2397691
$terms = [
'term1' => 'a' . $this->randomMachineName(),
'term2' => 'b' . $this->randomMachineName(),
'term3' => 'c' . $this->randomMachineName() . ', ' . $this->randomMachineName(),
'term4' => 'd' . $this->randomMachineName(),
];
$edit = [];
$edit['title[0][value]'] = $this->randomMachineName();
$edit['body[0][value]'] = $this->randomMachineName();
// Insert the terms in a comma separated list. Vocabulary 1 is a
// free-tagging field created by the default profile.
$edit[$field->getName() . '[target_id]'] = Tags::implode($terms);
// Verify the placeholder is there.
$this->drupalGet('node/add/article');
$this->assertRaw('placeholder="Start typing here."', 'Placeholder is present.');
// Preview and verify the terms appear but are not created.
$this->drupalPostForm(NULL, $edit, t('Preview'));
foreach ($terms as $term) {
$this->assertText($term, 'The term appears on the node preview.');
}
$tree = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_term')
->loadTree($this->vocabulary
->id());
$this->assertTrue(empty($tree), 'The terms are not created on preview.');
// taxonomy.module does not maintain its static caches.
taxonomy_terms_static_reset();
// Save, creating the terms.
$this->drupalPostForm('node/add/article', $edit, t('Save'));
$this->assertText(t('@type @title has been created.', [
'@type' => t('Article'),
'@title' => $edit['title[0][value]'],
]), 'The node was created successfully.');
// Verify that the creation message contains a link to a node.
$view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [
':href' => 'node/',
]);
$this->assert(isset($view_link), 'The message area contains a link to a node');
foreach ($terms as $term) {
$this->assertText($term, 'The term was saved and appears on the node page.');
}
// Get the created terms.
$term_objects = [];
foreach ($terms as $key => $term) {
$term_objects[$key] = taxonomy_term_load_multiple_by_name($term);
$term_objects[$key] = reset($term_objects[$key]);
}
// Get the node.
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
// Test editing the node.
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
foreach ($terms as $term) {
$this->assertText($term, 'The term was retained after edit and still appears on the node page.');
}
// Delete term 1 from the term edit page.
$this->drupalGet('taxonomy/term/' . $term_objects['term1']->id() . '/edit');
$this->clickLink(t('Delete'));
$this->drupalPostForm(NULL, NULL, t('Delete'));
// Delete term 2 from the term delete page.
$this->drupalGet('taxonomy/term/' . $term_objects['term2']->id() . '/delete');
$this->drupalPostForm(NULL, [], t('Delete'));
$term_names = [
$term_objects['term3']->getName(),
$term_objects['term4']->getName(),
];
$this->drupalGet('node/' . $node->id());
foreach ($term_names as $term_name) {
$this->assertText($term_name, new FormattableMarkup('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(),
]));
}
$this->assertNoText($term_objects['term1']->getName(), new FormattableMarkup('The deleted term %name does not appear on the node page.', [
'%name' => $term_objects['term1']->getName(),
]));
$this->assertNoText($term_objects['term2']->getName(), new FormattableMarkup('The deleted term %name does not appear on the node page.', [
'%name' => $term_objects['term2']->getName(),
]));
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.