/** * Tests that a basic taxonomy entity query works. */ function testTaxonomyEfq() { $terms = array(); for ($i = 0; $i < 5; $i++) { $term = $this->createTerm($this->vocabulary); $terms[$term->id()] = $term; } $result = \Drupal::entityQuery('taxonomy_term')->execute(); sort($result); $this->assertEqual(array_keys($terms), $result, 'Taxonomy terms were retrieved by entity query.'); $tid = reset($result); $ids = (object) array('entity_type' => 'taxonomy_term', 'entity_id' => $tid, 'bundle' => $this->vocabulary->id()); $term = _field_create_entity_from_ids($ids); $this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.'); // Create a second vocabulary and five more terms. $vocabulary2 = $this->createVocabulary(); $terms2 = array(); for ($i = 0; $i < 5; $i++) { $term = $this->createTerm($vocabulary2); $terms2[$term->id()] = $term; } $result = \Drupal::entityQuery('taxonomy_term')->condition('vid', $vocabulary2->id())->execute(); sort($result); $this->assertEqual(array_keys($terms2), $result, format_string('Taxonomy terms from the %name vocabulary were retrieved by entity query.', array('%name' => $vocabulary2->label()))); $tid = reset($result); $ids = (object) array('entity_type' => 'taxonomy_term', 'entity_id' => $tid, 'bundle' => $vocabulary2->id()); $term = _field_create_entity_from_ids($ids); $this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.'); }
/** * Tests generate commands */ public function testDevelGenerate() { // Creating users. $edit = array('num' => 4); $this->drupalPostForm('admin/config/development/generate/user', $edit, t('Generate')); $this->assertText(t('4 users created.')); $this->assertText(t('Generate process complete.')); // Creating content. // First we create a node in order to test the Delete content checkbox. $this->drupalCreateNode(array('type' => 'article')); $edit = array('num' => 4, 'kill' => TRUE, 'node_types[article]' => TRUE, 'time_range' => 604800, 'max_comments' => 3, 'title_length' => 4); $this->drupalPostForm('admin/config/development/generate/content', $edit, t('Generate')); $this->assertText(t('Deleted 1 nodes.')); $this->assertText(t('Finished creating 4 nodes')); $this->assertText(t('Generate process complete.')); // Creating terms. $edit = array('vids[]' => $this->vocabulary->id(), 'num' => 5, 'title_length' => 12); $this->drupalPostForm('admin/config/development/generate/term', $edit, t('Generate')); $this->assertText(t('Created the following new terms: ')); $this->assertText(t('Generate process complete.')); // Creating vocabularies. $edit = array('num' => 5, 'title_length' => 12, 'kill' => TRUE); $this->drupalPostForm('admin/config/development/generate/vocabs', $edit, t('Generate')); $this->assertText(t('Created the following new vocabularies: ')); $this->assertText(t('Generate process complete.')); // Creating menus. $edit = array('num_menus' => 5, 'num_links' => 7, 'title_length' => 12, 'link_types[node]' => 1, 'link_types[front]' => 1, 'link_types[external]' => 1, 'max_depth' => 4, 'max_width' => 6, 'kill' => 1); $this->drupalPostForm('admin/config/development/generate/menu', $edit, t('Generate')); $this->assertText(t('Created the following new menus: ')); $this->assertText(t('Created 7 new menu links')); $this->assertText(t('Generate process complete.')); }
function addTerm(VocabularyInterface $vocabulary, array $term = array()) { $term += array('name' => Unicode::strtolower($this->randomMachineName(5)), 'vid' => $vocabulary->id()); $term = entity_create('taxonomy_term', $term); $term->save(); return $term; }
protected function setUp() { parent::setUp(); $this->vocabulary = $this->createVocabulary(); // RDF mapping - term bundle. rdf_get_mapping('taxonomy_term', $this->vocabulary->id())->setBundleMapping(array('types' => array('skos:Concept')))->setFieldMapping('name', array('properties' => array('rdfs:label', 'skos:prefLabel')))->save(); }
public function testTaxonomyImageAccess() { $user = $this->drupalCreateUser(array('administer site configuration', 'administer taxonomy', 'access user profiles')); $this->drupalLogin($user); // Create a term and upload the image. $files = $this->drupalGetTestFiles('image'); $image = array_pop($files); $edit['name[0][value]'] = $this->randomMachineName(); $edit['files[field_test_0]'] = drupal_realpath($image->uri); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save')); $this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save')); $terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $edit['name[0][value]'])); $term = reset($terms); $this->assertText(t('Created new term @name.', array('@name' => $term->getName()))); // Create a user that should have access to the file and one that doesn't. $access_user = $this->drupalCreateUser(array('access content')); $no_access_user = $this->drupalCreateUser(); $image = File::load($term->field_test->target_id); $this->drupalLogin($access_user); $this->drupalGet(file_create_url($image->getFileUri())); $this->assertResponse(200, 'Private image on term is accessible with right permission'); $this->drupalLogin($no_access_user); $this->drupalGet(file_create_url($image->getFileUri())); $this->assertResponse(403, 'Private image on term not accessible without right permission'); }
/** * Tests term indentation. */ function testTermIndentation() { // Create three taxonomy terms. $term1 = $this->createTerm($this->vocabulary); $term2 = $this->createTerm($this->vocabulary); $term3 = $this->createTerm($this->vocabulary); // Get the taxonomy storage. $taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term'); // Indent the second term under the first one. $edit = array('terms[tid:' . $term2->id() . ':0][term][tid]' => 2, 'terms[tid:' . $term2->id() . ':0][term][parent]' => 1, 'terms[tid:' . $term2->id() . ':0][term][depth]' => 1, 'terms[tid:' . $term2->id() . ':0][weight]' => 1); // Submit the edited form and check for HTML indentation element presence. $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->get('vid') . '/overview', $edit, t('Save')); $this->assertPattern('|<div class="js-indentation indentation"> </div>|'); // Check explicitly that term 2's parent is term 1. $parents = $taxonomy_storage->loadParents($term2->id()); $this->assertEqual(key($parents), 1, 'Term 1 is the term 2\'s parent'); // Move the second term back out to the root level. $edit = array('terms[tid:' . $term2->id() . ':0][term][tid]' => 2, 'terms[tid:' . $term2->id() . ':0][term][parent]' => 0, 'terms[tid:' . $term2->id() . ':0][term][depth]' => 0, 'terms[tid:' . $term2->id() . ':0][weight]' => 1); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->get('vid') . '/overview', $edit, t('Save')); // All terms back at the root level, no indentation should be present. $this->assertNoPattern('|<div class="js-indentation indentation"> </div>|'); // Check explicitly that term 2 has no parents. \Drupal::entityManager()->getStorage('taxonomy_term')->resetCache(); $parents = $taxonomy_storage->loadParents($term2->id()); $this->assertTrue(empty($parents), 'Term 2 has no parents now'); }
protected function setUp() { parent::setUp(); $web_user = $this->drupalCreateUser(array('bypass node access', 'administer taxonomy')); $this->drupalLogin($web_user); $this->vocabulary = $this->createVocabulary(); // Create the field. $this->fieldName = 'field_taxonomy_test'; $this->createTaxonomyTermReferenceField($this->fieldName, $this->vocabulary); // Set the RDF mapping for the new field. rdf_get_mapping('node', 'article')->setFieldMapping($this->fieldName, array('properties' => array('dc:subject'), 'mapping_type' => 'rel'))->save(); rdf_get_mapping('taxonomy_term', $this->vocabulary->id())->setBundleMapping(array('types' => array('skos:Concept')))->setFieldMapping('name', array('properties' => array('rdfs:label')))->save(); }
/** * Tests that the "tagged with" form element only shows for node types that support it. */ function testTaggedWithByNodeType() { // The tagging field is associated with one of our node types only. So the // "tagged with" form element on the view wizard should appear on the form // by default (when the wizard is configured to display all content) and // also when the node type that has the tagging field is selected, but not // when the node type that doesn't have the tagging field is selected. $tags_xpath = '//input[@name="show[tagged_with]"]'; $this->drupalGet('admin/structure/views/add'); $this->assertFieldByXpath($tags_xpath); $view['show[type]'] = $this->nodeTypeWithTags->id(); $this->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice')); $this->assertFieldByXpath($tags_xpath); $view['show[type]'] = $this->nodeTypeWithoutTags->id(); $this->drupalPostForm(NULL, $view, t('Update "of type" choice')); $this->assertNoFieldByXpath($tags_xpath); // If we add an instance of the tagging field to the second node type, the // "tagged with" form element should not appear for it too. entity_create('field_config', array('field_name' => $this->tagFieldName, 'entity_type' => 'node', 'bundle' => $this->nodeTypeWithoutTags->id(), 'settings' => array('handler' => 'default', 'handler_settings' => array('target_bundles' => array($this->tagVocabulary->id() => $this->tagVocabulary->id()), 'auto_create' => TRUE))))->save(); entity_get_form_display('node', $this->nodeTypeWithoutTags->id(), 'default')->setComponent($this->tagFieldName, array('type' => 'entity_reference_autocomplete_tags'))->save(); $view['show[type]'] = $this->nodeTypeWithTags->id(); $this->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice')); $this->assertFieldByXpath($tags_xpath); $view['show[type]'] = $this->nodeTypeWithoutTags->id(); $this->drupalPostForm(NULL, $view, t('Update "of type" choice')); $this->assertFieldByXpath($tags_xpath); }
/** * Tests the entity reference field type for referencing config entities. */ public function testConfigEntityReferenceItem() { $referenced_entity_id = $this->vocabulary->id(); // Just being able to create the entity like this verifies a lot of code. $entity = EntityTest::create(); $entity->field_test_taxonomy_vocabulary->target_id = $referenced_entity_id; $entity->name->value = $this->randomMachineName(); $entity->save(); $entity = entity_load('entity_test', $entity->id()); $this->assertTrue($entity->field_test_taxonomy_vocabulary instanceof FieldItemListInterface, 'Field implements interface.'); $this->assertTrue($entity->field_test_taxonomy_vocabulary[0] instanceof FieldItemInterface, 'Field item implements interface.'); $this->assertEqual($entity->field_test_taxonomy_vocabulary->target_id, $referenced_entity_id); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->label(), $this->vocabulary->label()); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->id(), $referenced_entity_id); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->uuid(), $this->vocabulary->uuid()); // Change the name of the term via the reference. $new_name = $this->randomMachineName(); $entity->field_test_taxonomy_vocabulary->entity->set('name', $new_name); $entity->field_test_taxonomy_vocabulary->entity->save(); // Verify it is the correct name. $vocabulary = Vocabulary::load($referenced_entity_id); $this->assertEqual($vocabulary->label(), $new_name); // Make sure the computed term reflects updates to the term id. $vocabulary2 = $vocabulary = Vocabulary::create(['name' => $this->randomMachineName(), 'vid' => Unicode::strtolower($this->randomMachineName()), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]); $vocabulary2->save(); $entity->field_test_taxonomy_vocabulary->target_id = $vocabulary2->id(); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->id(), $vocabulary2->id()); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->label(), $vocabulary2->label()); // Delete terms so we have nothing to reference and try again $this->vocabulary->delete(); $vocabulary2->delete(); $entity = EntityTest::create(array('name' => $this->randomMachineName())); $entity->save(); }
protected function setUp() { parent::setUp(); $web_user = $this->drupalCreateUser(array('bypass node access', 'administer taxonomy')); $this->drupalLogin($web_user); $this->vocabulary = $this->createVocabulary(); // Create the field. $this->fieldName = 'field_taxonomy_test'; $handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE); $this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); entity_get_form_display('node', 'article', 'default')->setComponent($this->fieldName, array('type' => 'options_select'))->save(); entity_get_display('node', 'article', 'full')->setComponent($this->fieldName, array('type' => 'entity_reference_label'))->save(); // Set the RDF mapping for the new field. rdf_get_mapping('node', 'article')->setFieldMapping($this->fieldName, array('properties' => array('dc:subject'), 'mapping_type' => 'rel'))->save(); rdf_get_mapping('taxonomy_term', $this->vocabulary->id())->setBundleMapping(array('types' => array('skos:Concept')))->setFieldMapping('name', array('properties' => array('rdfs:label')))->save(); }
/** * Tests that terms added to nodes are displayed in core RSS feed. * * Create a node and assert that taxonomy terms appear in rss.xml. */ function testTaxonomyRss() { // Create two taxonomy terms. $term1 = $this->createTerm($this->vocabulary); // RSS display must be added manually. $this->drupalGet("admin/structure/types/manage/article/display"); $edit = array("display_modes_custom[rss]" => '1'); $this->drupalPostForm(NULL, $edit, t('Save')); // Change the format to 'RSS category'. $this->drupalGet("admin/structure/types/manage/article/display/rss"); $edit = array("fields[taxonomy_" . $this->vocabulary->id() . "][type]" => 'entity_reference_rss_category'); $this->drupalPostForm(NULL, $edit, t('Save')); // Post an article. $edit = array(); $edit['title[0][value]'] = $this->randomMachineName(); $edit[$this->fieldName . '[]'] = $term1->id(); $this->drupalPostForm('node/add/article', $edit, t('Save')); // Check that the term is displayed when the RSS feed is viewed. $this->drupalGet('rss.xml'); $test_element = sprintf('<category %s>%s</category>', 'domain="' . $term1->url('canonical', array('absolute' => TRUE)) . '"', $term1->getName()); $this->assertRaw($test_element, 'Term is displayed when viewing the rss feed.'); // Test that the feed icon exists for the term. $this->drupalGet("taxonomy/term/{$term1->id()}"); $this->assertLinkByHref("taxonomy/term/{$term1->id()}/feed"); // Test that the feed page exists for the term. $this->drupalGet("taxonomy/term/{$term1->id()}/feed"); $this->assertRaw('<rss version="2.0"', "Feed page is RSS."); // Check that the "Exception value" is disabled by default. $this->drupalGet('taxonomy/term/all/feed'); $this->assertResponse(404); // Set the exception value to 'all'. $view = Views::getView('taxonomy_term'); $arguments = $view->getDisplay()->getOption('arguments'); $arguments['tid']['exception']['value'] = 'all'; $view->getDisplay()->overrideOption('arguments', $arguments); $view->storage->save(); // Check the article is shown in the feed. $node = $this->drupalGetNodeByTitle($edit['title[0][value]']); $raw_xml = '<title>' . $node->label() . '</title>'; $this->drupalGet('taxonomy/term/all/feed'); $this->assertRaw($raw_xml, "Raw text '{$raw_xml}' is found."); // Unpublish the article and check that it is not shown in the feed. $node->setPublished(FALSE)->save(); $this->drupalGet('taxonomy/term/all/feed'); $this->assertNoRaw($raw_xml); }
/** * Returns a new term with random name and description in $this->vocabulary. * * @return \Drupal\taxonomy\TermInterface * The created taxonomy term. */ protected function createTerm() { $filter_formats = filter_formats(); $format = array_pop($filter_formats); $term = Term::create(['name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'format' => $format->id(), 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]); $term->save(); return $term; }
/** * Returns a new term with random name and description in $this->vocabulary. * * @return \Drupal\taxonomy\TermInterface * The created taxonomy term. */ protected function createTerm() { $filter_formats = filter_formats(); $format = array_pop($filter_formats); $term = entity_create('taxonomy_term', array('name' => $this->randomName(), 'description' => $this->randomName(), 'format' => $format->format, 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $term->save(); return $term; }
protected function setUp() { parent::setUp(); // Create an administrative user. $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access'])); // Create a vocabulary and add two term reference fields to article nodes. $this->vocabulary = $this->createVocabulary(); $this->fieldName1 = Unicode::strtolower($this->randomMachineName()); $handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE); $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); entity_get_form_display('node', 'article', 'default')->setComponent($this->fieldName1, array('type' => 'options_select'))->save(); entity_get_display('node', 'article', 'default')->setComponent($this->fieldName1, array('type' => 'entity_reference_label'))->save(); $this->fieldName2 = Unicode::strtolower($this->randomMachineName()); $this->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); entity_get_form_display('node', 'article', 'default')->setComponent($this->fieldName2, array('type' => 'options_select'))->save(); entity_get_display('node', 'article', 'default')->setComponent($this->fieldName2, array('type' => 'entity_reference_label'))->save(); }
protected function setUp() { parent::setUp(); // Create an administrative user. $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access'])); // Create a vocabulary and add two term reference fields to article nodes. $this->vocabulary = $this->createVocabulary(); $this->fieldName1 = Unicode::strtolower($this->randomMachineName()); entity_create('field_storage_config', array('field_name' => $this->fieldName1, 'entity_type' => 'node', 'type' => 'taxonomy_term_reference', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings' => array('allowed_values' => array(array('vocabulary' => $this->vocabulary->id(), 'parent' => 0)))))->save(); entity_create('field_config', array('field_name' => $this->fieldName1, 'bundle' => 'article', 'entity_type' => 'node'))->save(); entity_get_form_display('node', 'article', 'default')->setComponent($this->fieldName1, array('type' => 'options_select'))->save(); entity_get_display('node', 'article', 'default')->setComponent($this->fieldName1, array('type' => 'taxonomy_term_reference_link'))->save(); $this->fieldName2 = Unicode::strtolower($this->randomMachineName()); entity_create('field_storage_config', array('field_name' => $this->fieldName2, 'entity_type' => 'node', 'type' => 'taxonomy_term_reference', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings' => array('allowed_values' => array(array('vocabulary' => $this->vocabulary->id(), 'parent' => 0)))))->save(); entity_create('field_config', array('field_name' => $this->fieldName2, 'bundle' => 'article', 'entity_type' => 'node'))->save(); entity_get_form_display('node', 'article', 'default')->setComponent($this->fieldName2, array('type' => 'options_select'))->save(); entity_get_display('node', 'article', 'default')->setComponent($this->fieldName2, array('type' => 'taxonomy_term_reference_link'))->save(); }
/** * Tests term reference field and widget with multiple vocabularies. */ function testTaxonomyTermFieldMultipleVocabularies() { // Create a term in each vocabulary. $term1 = $this->createTerm($this->vocabulary1); $term2 = $this->createTerm($this->vocabulary2); // Submit an entity with both terms. $this->drupalGet('entity_test/add'); // Just check if the widget for the select is displayed, the NULL value is // used to ignore the value check. $this->assertFieldByName("{$this->fieldName}[]", NULL, 'Widget is displayed.'); $edit = array("{$this->fieldName}[]" => array($term1->id(), $term2->id())); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\\d+)|', $this->url, $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created.'); // Render the entity. $entity = entity_load('entity_test', $id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->setRawContent(drupal_render($content)); $this->assertText($term1->getName(), 'Term 1 name is displayed.'); $this->assertText($term2->getName(), 'Term 2 name is displayed.'); // Delete vocabulary 2. $this->vocabulary2->delete(); // Re-render the content. $entity = entity_load('entity_test', $id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->setRawContent(drupal_render($content)); // Term 1 should still be displayed; term 2 should not be. $this->assertText($term1->getName(), 'Term 1 name is displayed.'); $this->assertNoText($term2->getName(), 'Term 2 name is not displayed.'); // Verify that field storage settings and field settings are correct. $field_storage = FieldStorageConfig::loadByName('entity_test', $this->fieldName); $this->assertEqual(count($field_storage->getSetting('allowed_values')), 1, 'Only one vocabulary is allowed for the field.'); // The widget should still be displayed. $this->drupalGet('entity_test/add'); // Just check if the widget for the select is displayed, the NULL value is // used to ignore the value check. $this->assertFieldByName("{$this->fieldName}[]", NULL, 'Widget is still displayed.'); // Term 1 should still pass validation. $edit = array("{$this->fieldName}[]" => array($term1->id())); $this->drupalPostForm(NULL, $edit, t('Save')); }
/** * Form constructor. * * Display a tree of all the terms in a vocabulary, with options to edit * each one. The form implements the Taxonomy Manager intefrace. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param VocabularyInterface $taxonomy_vocabulary * The vocabulary being with worked with * * @return array * The form structure. */ public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) { $form['voc'] = array('#type' => 'value', "#value" => $taxonomy_vocabulary); $form['#attached']['library'][] = 'taxonomy_manager/form'; if (TaxonomyManagerHelper::_taxonomy_manager_voc_is_empty($taxonomy_vocabulary->id())) { $form['text'] = array('#markup' => $this->t('No terms available')); $form[] = \Drupal::formBuilder()->getForm('Drupal\\taxonomy_manager\\Form\\AddTermsToVocabularyForm', $taxonomy_vocabulary); return $form; } $form['toolbar'] = array('#type' => 'fieldset', '#title' => $this->t('Toolbar')); $form['toolbar']['add'] = array('#type' => 'submit', '#name' => 'add', '#value' => $this->t('Add'), '#ajax' => array('callback' => '::addFormCallback')); $form['toolbar']['delete'] = array('#type' => 'submit', '#name' => 'delete', '#value' => $this->t('Delete'), '#ajax' => array('callback' => '::deleteFormCallback')); $form['toolbar']['move'] = array('#type' => 'submit', '#name' => 'move', '#value' => $this->t('Move'), '#ajax' => array('callback' => '::moveFormCallback')); $form['toolbar']['export'] = array('#type' => 'submit', '#name' => 'export', '#value' => $this->t('Export'), '#ajax' => array('callback' => '::exportFormCallback')); /* Taxonomy manager. */ $form['taxonomy']['#tree'] = TRUE; $form['taxonomy']['manager'] = array('#type' => 'fieldset', '#title' => HTML::escape($taxonomy_vocabulary->label()), '#tree' => TRUE); $form['taxonomy']['manager']['top'] = array('#markup' => '', '#prefix' => '<div class="taxonomy-manager-tree-top">', '#suffix' => '</div>'); /*$grippie_image = array( '#theme' => 'image', '#uri' => drupal_get_path('module', 'taxonomy_manager') . "/images/grippie.png", '#alt' => $this->t("Resize tree"), '#title' => $this->t("Resize tree"), '#attributes' => array('class' => array('div-grippie')), ); $form['taxonomy']['manager']['top']['size'] = array( '#markup' => '<div class="taxonomy-manager-tree-size">' . \Drupal::service('renderer')->render($grippie_image, true) . '</div>' );*/ $form['taxonomy']['manager']['tree'] = array('#type' => 'taxonomy_manager_tree', '#vocabulary' => $taxonomy_vocabulary->id(), '#pager_size' => \Drupal::config('taxonomy_manager.settings')->get('taxonomy_manager_pager_tree_page_size')); $form['taxonomy']['manager']['pager'] = array('#type' => 'pager'); // Add placeholder for term data form, the load-term-data field has AJAX // events attached and will trigger the load of the term data form. The // field is hidden via CSS and the value gets set in termData.js. $form['term-data']['#prefix'] = '<div id="taxonomy-term-data-form">'; $form['term-data']['#suffix'] = '</div>'; $form['load-term-data'] = array('#type' => 'textfield', '#ajax' => array('callback' => '::termDataCallback', 'event' => 'change')); return $form; }
/** * Creates a taxonomy term with specified name and other properties. * * @param array $properties * Array of properties and field values to set. * * @return \Drupal\taxonomy\TermInterface * The created taxonomy term. */ protected function createTermWithProperties($properties) { // Use the first available text format. $filter_formats = filter_formats(); $format = array_pop($filter_formats); $properties += array('name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'field_foo' => $this->randomMachineName()); $term = Term::create(['name' => $properties['name'], 'description' => $properties['description'], 'format' => $format->id(), 'vid' => $this->vocabulary->id(), 'langcode' => $properties['langcode']]); $term->field_foo->value = $properties['field_foo']; $term->save(); return $term; }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->installEntitySchema('user'); $this->installEntitySchema('node'); $this->installEntitySchema('taxonomy_term'); // Create the article content type with a text field. $node_type = NodeType::create(['type' => 'article']); $node_type->save(); $field_storage = FieldStorageConfig::create(['field_name' => 'test_field', 'entity_type' => 'node', 'type' => 'text']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'test_field', 'entity_type' => 'node', 'bundle' => 'article', 'label' => 'Test field']); $field->save(); // Create a reference field with the same name on user. $field_storage = FieldStorageConfig::create(['field_name' => 'test_field', 'entity_type' => 'user', 'type' => 'entity_reference']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'test_field', 'entity_type' => 'user', 'bundle' => 'user', 'label' => 'Test field']); $field->save(); $this->testFormat = FilterFormat::create(['format' => 'test', 'weight' => 1, 'filters' => ['filter_html_escape' => ['status' => TRUE]]]); $this->testFormat->save(); // Create a multi-value list_string field. $field_storage = FieldStorageConfig::create(['field_name' => 'test_list', 'entity_type' => 'node', 'type' => 'list_string', 'cardinality' => 2, 'settings' => ['allowed_values' => ['key1' => 'value1', 'key2' => 'value2']]]); $field_storage->save(); $this->field = FieldConfig::create(['field_name' => 'test_list', 'entity_type' => 'node', 'bundle' => 'article'])->save(); // Add an untranslatable node reference field. FieldStorageConfig::create(['field_name' => 'test_reference', 'type' => 'entity_reference', 'entity_type' => 'node', 'settings' => ['target_type' => 'node'], 'translatable' => FALSE])->save(); FieldConfig::create(['field_name' => 'test_reference', 'entity_type' => 'node', 'bundle' => 'article', 'label' => 'Test reference'])->save(); // Add an untranslatable taxonomy term reference field. $this->vocabulary = $this->createVocabulary(); FieldStorageConfig::create(['field_name' => 'test_term_reference', 'type' => 'entity_reference', 'entity_type' => 'node', 'settings' => ['target_type' => 'taxonomy_term'], 'translatable' => FALSE])->save(); FieldConfig::create(['field_name' => 'test_term_reference', 'entity_type' => 'node', 'bundle' => 'article', 'label' => 'Test term reference', 'settings' => ['handler' => 'default:taxonomy_term', 'handler_settings' => ['target_bundles' => [$this->vocabulary->id() => $this->vocabulary->id()]]]])->save(); // Add a field to terms of the created vocabulary. $storage = FieldStorageConfig::create(['field_name' => 'term_field', 'entity_type' => 'taxonomy_term', 'type' => 'text']); $storage->save(); $field = FieldConfig::create(['field_name' => 'term_field', 'entity_type' => 'taxonomy_term', 'bundle' => $this->vocabulary->id()]); $field->save(); // Add a second language. $language = ConfigurableLanguage::create(['id' => 'de', 'label' => 'German']); $language->save(); }
/** * Tests that the pager is displayed properly on the term overview page. */ public function testTaxonomyTermOverviewPager() { // Set limit to 3 terms per page. $this->config('taxonomy.settings')->set('terms_per_page_admin', '3')->save(); // Create 3 terms. for ($x = 1; $x <= 3; $x++) { $this->createTerm($this->vocabulary); } // Get Page 1. $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview'); $this->assertNoPattern('|<nav class="pager" [^>]*>|', 'Pager is not visible on page 1'); // Create 3 more terms to show pager. for ($x = 1; $x <= 3; $x++) { $this->createTerm($this->vocabulary); } // Get Page 1. $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview'); $this->assertPattern('|<nav class="pager" [^>]*>|', 'Pager is visible on page 1'); // Get Page 2. $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', ['query' => ['page' => 1]]); $this->assertPattern('|<nav class="pager" [^>]*>|', 'Pager is visible on page 2'); }
protected function setUp() { parent::setUp(); // Create two content types. One will have an autocomplete tagging field, // and one won't. $this->nodeTypeWithTags = $this->drupalCreateContentType(); $this->nodeTypeWithoutTags = $this->drupalCreateContentType(); // Create the vocabulary for the tag field. $this->tagVocabulary = entity_create('taxonomy_vocabulary', array('name' => 'Views testing tags', 'vid' => 'views_testing_tags')); $this->tagVocabulary->save(); // Create the tag field itself. $this->tagFieldName = 'field_views_testing_tags'; $this->tagFieldStorage = entity_create('field_storage_config', array('field_name' => $this->tagFieldName, 'entity_type' => 'node', 'type' => 'taxonomy_term_reference', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings' => array('allowed_values' => array(array('vocabulary' => $this->tagVocabulary->id(), 'parent' => 0))))); $this->tagFieldStorage->save(); // Create an instance of the tag field on one of the content types, and // configure it to display an autocomplete widget. $this->tagField = array('field_storage' => $this->tagFieldStorage, 'bundle' => $this->nodeTypeWithTags->id()); entity_create('field_config', $this->tagField)->save(); entity_get_form_display('node', $this->nodeTypeWithTags->id(), 'default')->setComponent('field_views_testing_tags', array('type' => 'taxonomy_autocomplete'))->save(); entity_get_display('node', $this->nodeTypeWithTags->id(), 'default')->setComponent('field_views_testing_tags', array('type' => 'taxonomy_term_reference_link', 'weight' => 10))->save(); entity_get_display('node', $this->nodeTypeWithTags->id(), 'teaser')->setComponent('field_views_testing_tags', array('type' => 'taxonomy_term_reference_link', 'weight' => 10))->save(); }
/** * Tests that vocabulary machine name changes are mirrored in field definitions. */ function testTaxonomyTermFieldChangeMachineName() { // Add several entries in the 'allowed_values' setting, to make sure that // they all get updated. $this->fieldStorage->setSetting('allowed_values', [array('vocabulary' => $this->vocabulary->id(), 'parent' => '0'), array('vocabulary' => $this->vocabulary->id(), 'parent' => '0'), array('vocabulary' => 'foo', 'parent' => '0')]); $this->fieldStorage->save(); // Change the machine name. $new_name = Unicode::strtolower($this->randomMachineName()); $this->vocabulary->set('vid', $new_name); $this->vocabulary->save(); // Check that the field is still attached to the vocabulary. $field_storage = FieldStorageConfig::loadByName('entity_test', $this->fieldName); $allowed_values = $field_storage->getSetting('allowed_values'); $this->assertEqual($allowed_values[0]['vocabulary'], $new_name, 'Index 0: Machine name was updated correctly.'); $this->assertEqual($allowed_values[1]['vocabulary'], $new_name, 'Index 1: Machine name was updated correctly.'); $this->assertEqual($allowed_values[2]['vocabulary'], 'foo', 'Index 2: Machine name was left untouched.'); }
/** * Tests generate commands */ public function testDevelGenerate() { // Creating users. $edit = array('num' => 4); $this->drupalPostForm('admin/config/development/generate/user', $edit, t('Generate')); $this->assertText(t('4 users created.')); $this->assertText(t('Generate process complete.')); // Creating content. // First we create a node in order to test the Delete content checkbox. $this->drupalCreateNode(array('type' => 'article')); $edit = array('num' => 4, 'kill' => TRUE, 'node_types[article]' => TRUE, 'time_range' => 604800, 'max_comments' => 3, 'title_length' => 4, 'add_alias' => 1); $this->drupalPostForm('admin/config/development/generate/content', $edit, t('Generate')); $this->assertText(t('Deleted 1 nodes.')); $this->assertText(t('Finished creating 4 nodes')); $this->assertText(t('Generate process complete.')); // Tests that nodes have been created in the generation process. $nodes = Node::loadMultiple(); $this->assert(count($nodes) == 4, 'Nodes generated successfully.'); // Tests url alias for the generated nodes. foreach ($nodes as $node) { $alias = 'node-' . $node->id() . '-' . $node->bundle(); $this->drupalGet($alias); $this->assertResponse('200'); $this->assertText($node->getTitle(), 'Generated url alias for the node works.'); } // Creating terms. $edit = array('vids[]' => $this->vocabulary->id(), 'num' => 5, 'title_length' => 12); $this->drupalPostForm('admin/config/development/generate/term', $edit, t('Generate')); $this->assertText(t('Created the following new terms: ')); $this->assertText(t('Generate process complete.')); // Creating vocabularies. $edit = array('num' => 5, 'title_length' => 12, 'kill' => TRUE); $this->drupalPostForm('admin/config/development/generate/vocabs', $edit, t('Generate')); $this->assertText(t('Created the following new vocabularies: ')); $this->assertText(t('Generate process complete.')); // Creating menus. $edit = array('num_menus' => 5, 'num_links' => 7, 'title_length' => 12, 'link_types[node]' => 1, 'link_types[front]' => 1, 'link_types[external]' => 1, 'max_depth' => 4, 'max_width' => 6, 'kill' => 1); $this->drupalPostForm('admin/config/development/generate/menu', $edit, t('Generate')); $this->assertText(t('Created the following new menus: ')); $this->assertText(t('Created 7 new menu links')); $this->assertText(t('Generate process complete.')); }
/** * Test taxonomy_term_load_multiple_by_name(). */ function testTaxonomyGetTermByName() { $term = $this->createTerm($this->vocabulary); // Load the term with the exact name. $terms = taxonomy_term_load_multiple_by_name($term->getName()); $this->assertTrue(isset($terms[$term->id()]), 'Term loaded using exact name.'); // Load the term with space concatenated. $terms = taxonomy_term_load_multiple_by_name(' ' . $term->getName() . ' '); $this->assertTrue(isset($terms[$term->id()]), 'Term loaded with extra whitespace.'); // Load the term with name uppercased. $terms = taxonomy_term_load_multiple_by_name(strtoupper($term->getName())); $this->assertTrue(isset($terms[$term->id()]), 'Term loaded with uppercased name.'); // Load the term with name lowercased. $terms = taxonomy_term_load_multiple_by_name(strtolower($term->getName())); $this->assertTrue(isset($terms[$term->id()]), 'Term loaded with lowercased name.'); // Try to load an invalid term name. $terms = taxonomy_term_load_multiple_by_name('Banana'); $this->assertFalse($terms, 'No term loaded with an invalid name.'); // Try to load the term using a substring of the name. $terms = taxonomy_term_load_multiple_by_name(Unicode::substr($term->getName(), 2), 'No term loaded with a substring of the name.'); $this->assertFalse($terms); // Create a new term in a different vocabulary with the same name. $new_vocabulary = $this->createVocabulary(); $new_term = entity_create('taxonomy_term', array('name' => $term->getName(), 'vid' => $new_vocabulary->id())); $new_term->save(); // Load multiple terms with the same name. $terms = taxonomy_term_load_multiple_by_name($term->getName()); $this->assertEqual(count($terms), 2, 'Two terms loaded with the same name.'); // Load single term when restricted to one vocabulary. $terms = taxonomy_term_load_multiple_by_name($term->getName(), $this->vocabulary->id()); $this->assertEqual(count($terms), 1, 'One term loaded when restricted by vocabulary.'); $this->assertTrue(isset($terms[$term->id()]), 'Term loaded using exact name and vocabulary machine name.'); // Create a new term with another name. $term2 = $this->createTerm($this->vocabulary); // Try to load a term by name that doesn't exist in this vocabulary but // exists in another vocabulary. $terms = taxonomy_term_load_multiple_by_name($term2->getName(), $new_vocabulary->id()); $this->assertFalse($terms, 'Invalid term name restricted by vocabulary machine name not loaded.'); // Try to load terms filtering by a non-existing vocabulary. $terms = taxonomy_term_load_multiple_by_name($term2->getName(), 'non_existing_vocabulary'); $this->assertEqual(count($terms), 0, 'No terms loaded when restricted by a non-existing vocabulary.'); }
/** * Tests translate link on vocabulary term list. */ function testTranslateLinkVocabularyAdminPage() { $this->drupalLogin($this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer taxonomy']))); $values = array('name' => $this->randomMachineName()); $translatable_tid = $this->createEntity($values, $this->langcodes[0], $this->vocabulary->id()); // Create an untranslatable vocabulary. $untranslatable_vocabulary = Vocabulary::create(['name' => 'untranslatable_voc', 'description' => $this->randomMachineName(), 'vid' => 'untranslatable_voc', 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'weight' => mt_rand(0, 10)]); $untranslatable_vocabulary->save(); $values = array('name' => $this->randomMachineName()); $untranslatable_tid = $this->createEntity($values, $this->langcodes[0], $untranslatable_vocabulary->id()); // Verify translation links. $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview'); $this->assertResponse(200, 'The translatable vocabulary page was found.'); $this->assertLinkByHref('term/' . $translatable_tid . '/translations', 0, 'The translations link exists for a translatable vocabulary.'); $this->assertLinkByHref('term/' . $translatable_tid . '/edit', 0, 'The edit link exists for a translatable vocabulary.'); $this->drupalGet('admin/structure/taxonomy/manage/' . $untranslatable_vocabulary->id() . '/overview'); $this->assertResponse(200); $this->assertLinkByHref('term/' . $untranslatable_tid . '/edit'); $this->assertNoLinkByHref('term/' . $untranslatable_tid . '/translations'); }
function testDefaultTermLanguage() { // Configure the vocabulary to not hide the language selector, and make the // default language of the terms fixed. $edit = array('default_language[langcode]' => 'bb', 'default_language[language_alterable]' => TRUE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save')); $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); $this->assertOptionSelected('edit-langcode-0-value', 'bb', 'The expected langcode was selected.'); // Make the default language of the terms to be the current interface. $edit = array('default_language[langcode]' => 'current_interface', 'default_language[language_alterable]' => TRUE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save')); $this->drupalGet('aa/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); $this->assertOptionSelected('edit-langcode-0-value', 'aa', "The expected langcode, 'aa', was selected."); $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); $this->assertOptionSelected('edit-langcode-0-value', 'bb', "The expected langcode, 'bb', was selected."); // Change the default language of the site and check if the default terms // language is still correctly selected. $this->config('system.site')->set('default_langcode', 'cc')->save(); $edit = array('default_language[langcode]' => LanguageInterface::LANGCODE_SITE_DEFAULT, 'default_language[language_alterable]' => TRUE); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save')); $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); $this->assertOptionSelected('edit-langcode-0-value', 'cc', "The expected langcode, 'cc', was selected."); }
/** * Check the breadcrumb on edit and delete a term page. */ public function testTermBreadcrumbs() { $edit = ['name[0][value]' => $this->randomMachineName(14), 'description[0][value]' => $this->randomMachineName(100), 'parent[]' => [0]]; // Create the term. $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save')); $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']); $term = reset($terms); $this->assertNotNull($term, 'Term found in database.'); // Check the breadcrumb on the term edit page. $this->drupalGet('taxonomy/term/' . $term->id() . '/edit'); $breadcrumbs = $this->cssSelect('nav.breadcrumb ol li a'); $this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.'); $this->assertIdentical((string) $breadcrumbs[0], 'Home', 'First breadcrumb text is Home'); $this->assertIdentical((string) $breadcrumbs[1], $term->label(), 'Second breadcrumb text is term name on term edit page.'); $this->assertEscaped((string) $breadcrumbs[1], 'breadcrumbs displayed and escaped.'); // Check the breadcrumb on the term delete page. $this->drupalGet('taxonomy/term/' . $term->id() . '/delete'); $breadcrumbs = $this->cssSelect('nav.breadcrumb ol li a'); $this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.'); $this->assertIdentical((string) $breadcrumbs[0], 'Home', 'First breadcrumb text is Home'); $this->assertIdentical((string) $breadcrumbs[1], $term->label(), 'Second breadcrumb text is term name on term delete page.'); $this->assertEscaped((string) $breadcrumbs[1], 'breadcrumbs displayed and escaped.'); }
/** * Retrieves suggestions for taxonomy term autocompletion by vocabulary ID. * * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary * The vocabulary to filter by. * * @return \Symfony\Component\HttpFoundation\JsonResponse * A JSON response containing the autocomplete suggestions for taxonomy * terms. */ public function autocompletePerVid(Request $request, VocabularyInterface $taxonomy_vocabulary) { // A comma-separated list of term names entered in the autocomplete form // element. Only the last term is used for autocompletion. $tags_typed = $request->query->get('q'); $tags_typed = Tags::explode($tags_typed); $tag_last = Unicode::strtolower(array_pop($tags_typed)); $matches = array(); if ($tag_last != '') { $vids = array($taxonomy_vocabulary->id()); $matches = $this->getMatchingTerms($tags_typed, $vids, $tag_last); } return new JsonResponse($matches); }
/** * Creates some terms and a node, then tests the tokens generated from them. */ function testTaxonomyTokenReplacement() { $token_service = \Drupal::token(); $language_interface = \Drupal::languageManager()->getCurrentLanguage(); // Create two taxonomy terms. $term1 = $this->createTerm($this->vocabulary); $term2 = $this->createTerm($this->vocabulary); // Edit $term2, setting $term1 as parent. $edit = array(); $edit['name[0][value]'] = '<blink>Blinking Text</blink>'; $edit['parent[]'] = array($term1->id()); $this->drupalPostForm('taxonomy/term/' . $term2->id() . '/edit', $edit, t('Save')); // Create node with term2. $edit = array(); $node = $this->drupalCreateNode(array('type' => 'article')); $edit[$this->fieldName . '[]'] = $term2->id(); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Generate and test sanitized tokens for term1. $tests = array(); $tests['[term:tid]'] = $term1->id(); $tests['[term:name]'] = Html::escape($term1->getName()); $tests['[term:description]'] = $term1->description->processed; $tests['[term:url]'] = $term1->url('canonical', array('absolute' => TRUE)); $tests['[term:node-count]'] = 0; $tests['[term:parent:name]'] = '[term:parent:name]'; $tests['[term:vocabulary:name]'] = Html::escape($this->vocabulary->label()); $tests['[term:vocabulary]'] = Html::escape($this->vocabulary->label()); $base_bubbleable_metadata = BubbleableMetadata::createFromObject($term1); $metadata_tests = array(); $metadata_tests['[term:tid]'] = $base_bubbleable_metadata; $metadata_tests['[term:name]'] = $base_bubbleable_metadata; $metadata_tests['[term:description]'] = $base_bubbleable_metadata; $metadata_tests['[term:url]'] = $base_bubbleable_metadata; $metadata_tests['[term:node-count]'] = $base_bubbleable_metadata; $metadata_tests['[term:parent:name]'] = $base_bubbleable_metadata; $bubbleable_metadata = clone $base_bubbleable_metadata; $metadata_tests['[term:vocabulary:name]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags()); $metadata_tests['[term:vocabulary]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags()); foreach ($tests as $input => $expected) { $bubbleable_metadata = new BubbleableMetadata(); $output = $token_service->replace($input, array('term' => $term1), array('langcode' => $language_interface->getId()), $bubbleable_metadata); $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', array('%token' => $input))); $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]); } // Generate and test sanitized tokens for term2. $tests = array(); $tests['[term:tid]'] = $term2->id(); $tests['[term:name]'] = Html::escape($term2->getName()); $tests['[term:description]'] = $term2->description->processed; $tests['[term:url]'] = $term2->url('canonical', array('absolute' => TRUE)); $tests['[term:node-count]'] = 1; $tests['[term:parent:name]'] = Html::escape($term1->getName()); $tests['[term:parent:url]'] = $term1->url('canonical', array('absolute' => TRUE)); $tests['[term:parent:parent:name]'] = '[term:parent:parent:name]'; $tests['[term:vocabulary:name]'] = Html::escape($this->vocabulary->label()); // Test to make sure that we generated something for each token. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); foreach ($tests as $input => $expected) { $output = $token_service->replace($input, array('term' => $term2), array('langcode' => $language_interface->getId())); $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', array('%token' => $input))); } // Generate and test unsanitized tokens. $tests['[term:name]'] = $term2->getName(); $tests['[term:description]'] = $term2->getDescription(); $tests['[term:parent:name]'] = $term1->getName(); $tests['[term:vocabulary:name]'] = $this->vocabulary->label(); foreach ($tests as $input => $expected) { $output = $token_service->replace($input, array('term' => $term2), array('langcode' => $language_interface->getId(), 'sanitize' => FALSE)); $this->assertEqual($output, $expected, format_string('Unsanitized taxonomy term token %token replaced.', array('%token' => $input))); } // Generate and test sanitized tokens. $tests = array(); $tests['[vocabulary:vid]'] = $this->vocabulary->id(); $tests['[vocabulary:name]'] = Html::escape($this->vocabulary->label()); $tests['[vocabulary:description]'] = Xss::filter($this->vocabulary->getDescription()); $tests['[vocabulary:node-count]'] = 1; $tests['[vocabulary:term-count]'] = 2; // Test to make sure that we generated something for each token. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); foreach ($tests as $input => $expected) { $output = $token_service->replace($input, array('vocabulary' => $this->vocabulary), array('langcode' => $language_interface->getId())); $this->assertEqual($output, $expected, format_string('Sanitized taxonomy vocabulary token %token replaced.', array('%token' => $input))); } // Generate and test unsanitized tokens. $tests['[vocabulary:name]'] = $this->vocabulary->label(); $tests['[vocabulary:description]'] = $this->vocabulary->getDescription(); foreach ($tests as $input => $expected) { $output = $token_service->replace($input, array('vocabulary' => $this->vocabulary), array('langcode' => $language_interface->getId(), 'sanitize' => FALSE)); $this->assertEqual($output, $expected, format_string('Unsanitized taxonomy vocabulary token %token replaced.', array('%token' => $input))); } }
/** * Route title callback. * * @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary * The taxonomy term. * * @return string * The term label. */ public function vocabularyTitle(VocabularyInterface $taxonomy_vocabulary) { return Xss::filter($taxonomy_vocabulary->label()); }