You are here

public function TermTest::testTaxonomyNode in Drupal 8

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

Test 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 192

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
    ->drupalPostForm('node/add/article', $edit, t('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
    ->assertText($term1
    ->getName(), 'Term is displayed when viewing the node.');
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertText($term1
    ->getName(), 'Term is displayed when editing the node.');
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->assertText($term1
    ->getName(), 'Term is displayed after saving the node with no changes.');

  // Edit the node with a different term.
  $edit[$this->field
    ->getName() . '[]'] = $term2
    ->id();
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertText($term2
    ->getName(), 'Term is displayed when viewing the node.');

  // Preview the node.
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, t('Preview'));
  $this
    ->assertUniqueText($term2
    ->getName(), 'Term is displayed when previewing the node.');
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', NULL, t('Preview'));
  $this
    ->assertUniqueText($term2
    ->getName(), 'Term is displayed when previewing the node again.');
}