1 taxonomy.test TaxonomyTermTestCase::testNodeTermCreationAndDeletion()

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

File

core/modules/taxonomy/tests/taxonomy.test, line 599
Tests for taxonomy.module.

Class

TaxonomyTermTestCase
Tests for taxonomy term functions.

Code

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(
    'term1' => $this->randomName(),
    'term2' => $this->randomName(),
    'term3' => $this->randomName() . ', ' . $this->randomName(),
    'term4' => $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]"] = backdrop_implode_tags($terms);

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

  // Save, creating the terms.
  $this->backdropPost('node/add/page', $edit, t('Save'));
  $this->assertRaw(t('@type %title has been created.', array('@type' => t('Page'), '%title' => $edit["title"])), '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]);
  }

  // Test editing the node.
  $node = $this->backdropGetNodeByTitle($edit["title"]);
  $this->backdropPost('node/' . $node->nid . '/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->backdropPost('taxonomy/term/' . $term_objects['term1']->tid . '/edit', array(), t('Delete'));
  $this->backdropPost(NULL, NULL, t('Delete'));

  // Delete term 2 from the term delete page.
  $this->backdropPost('taxonomy/term/' . $term_objects['term2']->tid . '/delete', array(), t('Delete'));
  $term_names = array($term_objects['term3']->name, $term_objects['term4']->name);

  // Get the node and verify that the terms that should be there still are.
  $this->backdropGet('node/' . $node->nid);

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

  // Test autocomplete on term 3, which contains a comma.
  // The term will be quoted, and the " will be encoded in unicode (\u0022).
  $input = substr($term_objects['term3']->name, 0, 3);
  $this->backdropGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input);
  $this->assertRaw('{"\u0022' . $term_objects['term3']->name . '\u0022":"' . $term_objects['term3']->name . '"}', format_string('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term3']->name)));

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

  // Test taxonomy autocomplete with a nonexistent field.
  $field_name = $this->randomName();
  $tag = $this->randomName();
  $message = t("Taxonomy field @field_name not found.", array('@field_name' => $field_name));
  $this->assertFalse(field_info_field($field_name), format_string('Field %field_name does not exist.', array('%field_name' => $field_name)));
  $this->backdropGet('taxonomy/autocomplete/' . $field_name . '/' . $tag);
  $this->assertRaw($message, 'Autocomplete returns correct error message when the taxonomy field does not exist.');

  // Test the autocomplete path without passing a field_name and terms.
  // This should not trigger a PHP notice.
  $field_name = '';
  $message = t("Taxonomy field @field_name not found.", array('@field_name' => $field_name));
  $this->backdropGet('taxonomy/autocomplete');
  $this->assertRaw($message, 'Autocomplete returns correct error message when no taxonomy field is given.');
}