function TermTest::testNodeTermCreationAndDeletion

Same name in other branches
  1. 9 core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testNodeTermCreationAndDeletion()
  2. 8.9.x core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testNodeTermCreationAndDeletion()
  3. 10 core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testNodeTermCreationAndDeletion()

Tests term creation with a free-tagging vocabulary from the node form.

File

core/modules/taxonomy/tests/src/Functional/TermTest.php, line 189

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testNodeTermCreationAndDeletion() : void {
    // 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->assertSession()
        ->responseContains('placeholder="Start typing here."');
    // Preview and verify the terms appear but are not created.
    $this->submitForm($edit, 'Preview');
    foreach ($terms as $term) {
        $this->assertSession()
            ->pageTextContains($term);
    }
    $tree = $this->container
        ->get('entity_type.manager')
        ->getStorage('taxonomy_term')
        ->loadTree($this->vocabulary
        ->id());
    $this->assertEmpty($tree, 'The terms are not created on preview.');
    // Save, creating the terms.
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('Article ' . $edit['title[0][value]'] . ' has been created.');
    // Verify that the creation message contains a link to a node.
    $this->assertSession()
        ->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "node/")]');
    foreach ($terms as $term) {
        $this->assertSession()
            ->pageTextContains($term);
    }
    // Get the created terms.
    $term_objects = [];
    foreach ($terms as $key => $term) {
        $term_objects[$key] = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
            ->loadByProperties([
            'name' => $term,
        ]);
        $term_objects[$key] = reset($term_objects[$key]);
    }
    // Get the node.
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    // Test editing the node.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Save');
    foreach ($terms as $term) {
        $this->assertSession()
            ->pageTextContains($term);
    }
    // Delete term 1 from the term edit page.
    $this->drupalGet('taxonomy/term/' . $term_objects['term1']->id() . '/edit');
    $this->clickLink('Delete');
    $this->submitForm([], 'Delete');
    // Delete term 2 from the term delete page.
    $this->drupalGet('taxonomy/term/' . $term_objects['term2']->id() . '/delete');
    $this->submitForm([], 'Delete');
    // Verify that the terms appear on the node page after the two terms were
    // deleted.
    $term_names = [
        $term_objects['term3']->getName(),
        $term_objects['term4']->getName(),
    ];
    $this->drupalGet('node/' . $node->id());
    foreach ($term_names as $term_name) {
        $this->assertSession()
            ->pageTextContains($term_name);
    }
    $this->assertSession()
        ->pageTextNotContains($term_objects['term1']->getName());
    $this->assertSession()
        ->pageTextNotContains($term_objects['term2']->getName());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.