/**
  * Sets up the hooks in the test module.
  */
 protected function setupQueryTagTestHooks() {
   taxonomy_terms_static_reset();
   \Drupal::state()->set('taxonomy_test_query_alter', 0);
   \Drupal::state()->set('taxonomy_test_query_term_access_alter', 0);
   \Drupal::state()->set('taxonomy_test_query_taxonomy_term_access_alter', 0);
 }
Пример #2
0
 /**
  * Test term creation with a free-tagging vocabulary from the node form.
  */
 function testNodeTermCreationAndDeletion()
 {
     // Enable tags in the vocabulary.
     $field = $this->field;
     entity_get_form_display($field->entity_type, $field->bundle, 'default')->setComponent($field->getName(), array('type' => 'taxonomy_autocomplete', 'settings' => array('placeholder' => 'Start typing here.')))->save();
     $terms = array('term1' => $this->randomMachineName(), 'term2' => $this->randomMachineName(), 'term3' => $this->randomMachineName() . ', ' . $this->randomMachineName(), 'term4' => $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()] = 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 = taxonomy_get_tree($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]);
     }
     // 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());
     // Get the node.
     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
     $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())));
     // 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']->getName(), 0, 3);
     $json = $this->drupalGet('taxonomy/autocomplete/node/taxonomy_' . $this->vocabulary->id(), array('query' => array('q' => $input)));
     $this->assertEqual($json, '[{"value":"\\u0022' . $term_objects['term3']->getName() . '\\u0022","label":"' . $term_objects['term3']->getName() . '"}]', format_string('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term3']->getName())));
     // Test autocomplete on term 4 - it is alphanumeric only, so no extra
     // quoting.
     $input = substr($term_objects['term4']->getName(), 0, 3);
     $this->drupalGet('taxonomy/autocomplete/node/taxonomy_' . $this->vocabulary->id(), array('query' => array('q' => $input)));
     $this->assertRaw('[{"value":"' . $term_objects['term4']->getName() . '","label":"' . $term_objects['term4']->getName() . '"}', format_string('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term4']->getName())));
     // Test taxonomy autocomplete with a nonexistent field.
     $field_name = $this->randomMachineName();
     $tag = $this->randomMachineName();
     $message = t("Taxonomy field @field_name not found.", array('@field_name' => $field_name));
     $this->assertFalse(FieldStorageConfig::loadByName('node', $field_name), format_string('Field %field_name does not exist.', array('%field_name' => $field_name)));
     $this->drupalGet('taxonomy/autocomplete/node/' . $field_name, array('query' => array('q' => $tag)));
     $this->assertRaw($message, 'Autocomplete returns correct error message when the taxonomy field does not exist.');
 }
Пример #3
0
 /**
  * Test term creation with a free-tagging vocabulary from the node form.
  */
 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())));
 }