You are here

function TermTest::testNodeTermCreationAndDeletion 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::testNodeTermCreationAndDeletion()

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

File

core/modules/taxonomy/src/Tests/TermTest.php, line 218
Contains \Drupal\taxonomy\Tests\TermTest.

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\taxonomy\Tests

Code

function testNodeTermCreationAndDeletion() {

  // Enable tags in the vocabulary.
  $field = $this->field;
  entity_get_form_display($field
    ->getTargetEntityTypeId(), $field
    ->getTargetBundle(), 'default')
    ->setComponent($field
    ->getName(), array(
    'type' => 'entity_reference_autocomplete_tags',
    'settings' => array(
      '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 = array(
    'term1' => 'a' . $this
      ->randomMachineName(),
    'term2' => 'b' . $this
      ->randomMachineName(),
    'term3' => 'c' . $this
      ->randomMachineName() . ', ' . $this
      ->randomMachineName(),
    'term4' => 'd' . $this
      ->randomMachineName(),
  );
  $edit = array();
  $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.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
    ->assertRaw(t('@type %title has been created.', array(
    '@type' => t('Article'),
    '%title' => $edit['title[0][value]'],
  )), 'The node was created successfully.');
  foreach ($terms as $term) {
    $this
      ->assertText($term, 'The term was saved and appears on the node page.');
  }

  // Get the created terms.
  $term_objects = array();
  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, array(), t('Delete'));
  $term_names = array(
    $term_objects['term3']
      ->getName(),
    $term_objects['term4']
      ->getName(),
  );
  $this
    ->drupalGet('node/' . $node
    ->id());
  foreach ($term_names as $term_name) {
    $this
      ->assertText($term_name, format_string('The term %name appears on the node page after two terms, %deleted1 and %deleted2, were deleted.', array(
      '%name' => $term_name,
      '%deleted1' => $term_objects['term1']
        ->getName(),
      '%deleted2' => $term_objects['term2']
        ->getName(),
    )));
  }
  $this
    ->assertNoText($term_objects['term1']
    ->getName(), format_string('The deleted term %name does not appear on the node page.', array(
    '%name' => $term_objects['term1']
      ->getName(),
  )));
  $this
    ->assertNoText($term_objects['term2']
    ->getName(), format_string('The deleted term %name does not appear on the node page.', array(
    '%name' => $term_objects['term2']
      ->getName(),
  )));
}