function TermTest::testTaxonomyNode

Same name and namespace in other branches
  1. 8.9.x core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testTaxonomyNode()

Tests that hook_node_$op implementations work correctly.

Save & edit a node and assert that taxonomy terms are saved/loaded properly.

File

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

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTaxonomyNode() {
    // Create two taxonomy terms.
    $term1 = $this->createTerm($this->vocabulary);
    $term2 = $this->createTerm($this->vocabulary);
    // Post an article.
    $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName();
    $edit['body[0][value]'] = $this->randomMachineName();
    $edit[$this->field
        ->getName() . '[]'] = $term1->id();
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    // Check that the term is displayed when the node is viewed.
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()
        ->pageTextContains($term1->getName());
    $this->clickLink('Edit');
    $this->assertSession()
        ->pageTextContains($term1->getName());
    $this->submitForm([], 'Save');
    $this->assertSession()
        ->pageTextContains($term1->getName());
    // Edit the node with a different term.
    $edit[$this->field
        ->getName() . '[]'] = $term2->id();
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()
        ->pageTextContains($term2->getName());
    // Preview the node.
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Preview');
    // Ensure that term is displayed when previewing the node.
    $this->assertSession()
        ->pageTextContainsOnce($term2->getName());
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm([], 'Preview');
    // Ensure that term is displayed when previewing the node again.
    $this->assertSession()
        ->pageTextContainsOnce($term2->getName());
}

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