function TermTest::testNodeTermCreationAndDeletion

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

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php, line 142
Definition of Drupal\taxonomy\Tests\TermTest.

Class

TermTest
Tests for taxonomy term functions.

Namespace

Drupal\taxonomy\Tests

Code

function testNodeTermCreationAndDeletion() {

  // Enable tags in the vocabulary.
  $instance = $this->instance;
  $instance['widget'] = array(
    'type' => 'taxonomy_autocomplete',
    'settings' => array(
      'placeholder' => 'Start typing here.',
    ),
  );
  field_update_instance($instance);
  $terms = array(
    'term1' => $this
      ->randomName(),
    'term2' => $this
      ->randomName(),
    'term3' => $this
      ->randomName() . ', ' . $this
      ->randomName(),
    'term4' => $this
      ->randomName(),
  );
  $edit = array();
  $langcode = LANGUAGE_NOT_SPECIFIED;
  $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);

  // Verify the placeholder is there.
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertRaw('placeholder="Start typing here."');

  // Preview and verify the terms appear but are not created.
  $this
    ->drupalPost(NULL, $edit, t('Preview'));
  foreach ($terms as $term) {
    $this
      ->assertText($term, 'The term appears on the node preview');
  }
  $tree = taxonomy_get_tree($this->vocabulary->vid);
  $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
    ->drupalPost('node/add/article', $edit, t('Save'));
  $this
    ->assertRaw(t('@type %title has been created.', array(
    '@type' => t('Article'),
    '%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]);
  }

  // Delete term 1 from the term edit page.
  $this
    ->drupalPost('taxonomy/term/' . $term_objects['term1']->tid . '/edit', array(), t('Delete'));
  $this
    ->drupalPost(NULL, NULL, t('Delete'));

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

  // Get the node.
  $node = $this
    ->drupalGetNodeByTitle($edit["title"]);
  $this
    ->drupalGet('node/' . $node->nid);
  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']->name,
      '%deleted2' => $term_objects['term2']->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);
  $json = $this
    ->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name, array(
    'query' => array(
      'q' => $input,
    ),
  ));
  $this
    ->assertEqual($json, '{"\\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
    ->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name, array(
    'query' => array(
      'q' => $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
    ->drupalGet('taxonomy/autocomplete/' . $field_name, array(
    'query' => array(
      'q' => $tag,
    ),
  ));
  $this
    ->assertRaw($message, 'Autocomplete returns correct error message when the taxonomy field does not exist.');
}