TaxonomyTermTestCase::testNodeTermCreationAndDeletion

  1. drupal
    1. 8
    2. 7
Versions
7 TaxonomyTermTestCase::testNodeTermCreationAndDeletion()

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

Code

modules/taxonomy/taxonomy.test, line 575

<?php
function testNodeTermCreationAndDeletion() {
  // Enable tags in the vocabulary.
  $instance = $this->instance;
  $instance['widget'] = array('type' => 'taxonomy_autocomplete');
  $instance['bundle'] = 'page';
  field_create_instance($instance);
  $terms = array(
    $this->randomName(),
    $this->randomName() . ', ' . $this->randomName(),
    $this->randomName(),
  );

  $edit = array();
  $langcode = LANGUAGE_NONE;
  $edit["title"] = $this->randomName();
  $edit["body[$langcode][0][value]"] = $this->randomName();
  // Insert the terms in a comma separated list. Vocabulary 1 is a
  // free-tagging field created by the default profile.
  $edit[$instance['field_name'] . "[$langcode]"] = drupal_implode_tags($terms);

  // Preview and verify the terms appear but are not created.
  $this->drupalPost('node/add/page', $edit, t('Preview'));
  foreach ($terms as $term) {
    $this->assertText($term, t('The term appears on the node preview'));
  }
  $tree = taxonomy_get_tree($this->vocabulary->vid);
  $this->assertTrue(empty($tree), t('The terms are not created on preview.'));

  // taxonomy.module does not maintain its static caches.
  drupal_static_reset();

  // Save, creating the terms.
  $this->drupalPost('node/add/page', $edit, t('Save'));
  $this->assertRaw(t('@type %title has been created.', array('@type' => t('Basic page'), '%title' => $edit["title"])), t('The node was created successfully'));
  foreach ($terms as $term) {
    $this->assertText($term, t('The term was saved and appears on the node page'));
  }

  // Get the created terms.
  list($term1, $term2, $term3) = array_values(taxonomy_term_load_multiple(FALSE));

  // Delete term 1.
  $this->drupalPost('taxonomy/term/' . $term1->tid . '/edit', array(), t('Delete'));
  $this->drupalPost(NULL, NULL, t('Delete'));
  $term_names = array($term2->name, $term3->name);

  // Get the node.
  $node = $this->drupalGetNodeByTitle($edit["title"]);
  $this->drupalGet('node/' . $node->nid);

  foreach ($term_names as $term_name) {
    $this->assertText($term_name, t('The term %name appears on the node page after one term %deleted was deleted', array('%name' => $term_name, '%deleted' => $term1->name)));
  }
  $this->assertNoText($term1->name, t('The deleted term %name does not appear on the node page.', array('%name' => $term1->name)));

  // Test autocomplete on term 2 - it contains a comma, so expect the key to
  // be quoted.
  $input = substr($term2->name, 0, 3);
  $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input);
  $this->assertRaw('{"\"' . $term2->name . '\"":"' . $term2->name . '"}', t('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term2->name)));

  // Test autocomplete on term 3 - it is alphanumeric only, so no extra
  // quoting.
  $input = substr($term3->name, 0, 3);
  $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input);
  $this->assertRaw('{"' . $term3->name . '":"' . $term3->name . '"}', t('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term3->name)));
}
?>