You are here

function TermTest::testTaxonomyNode in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/taxonomy/src/Tests/TermTest.php \Drupal\taxonomy\Tests\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/src/Tests/TermTest.php, line 179
Contains \Drupal\taxonomy\Tests\TermTest.

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyNode() {

  // Create two taxonomy terms.
  $term1 = $this
    ->createTerm($this->vocabulary);
  $term2 = $this
    ->createTerm($this->vocabulary);

  // Post an article.
  $edit = array();
  $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, array(), 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.');
}