Chris@0: drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access'])); Chris@0: $this->vocabulary = $this->createVocabulary(); Chris@0: $this->fieldName = 'taxonomy_' . $this->vocabulary->id(); Chris@0: Chris@0: $handler_settings = [ Chris@0: 'target_bundles' => [ Chris@0: $this->vocabulary->id() => $this->vocabulary->id(), Chris@0: ], Chris@0: 'auto_create' => TRUE, Chris@0: ]; Chris@0: $this->createEntityReferenceField('node', 'article', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); Chris@0: Chris@0: entity_get_form_display('node', 'article', 'default') Chris@0: ->setComponent($this->fieldName, [ Chris@0: 'type' => 'options_select', Chris@0: ]) Chris@0: ->save(); Chris@0: entity_get_display('node', 'article', 'default') Chris@0: ->setComponent($this->fieldName, [ Chris@0: 'type' => 'entity_reference_label', Chris@0: ]) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates some terms and a node, then tests the tokens generated from them. Chris@0: */ Chris@0: public function testTaxonomyTokenReplacement() { Chris@0: $token_service = \Drupal::token(); Chris@0: $language_interface = \Drupal::languageManager()->getCurrentLanguage(); Chris@0: Chris@0: // Create two taxonomy terms. Chris@0: $term1 = $this->createTerm($this->vocabulary); Chris@0: $term2 = $this->createTerm($this->vocabulary); Chris@0: Chris@0: // Edit $term2, setting $term1 as parent. Chris@0: $edit = []; Chris@0: $edit['name[0][value]'] = 'Blinking Text'; Chris@0: $edit['parent[]'] = [$term1->id()]; Chris@0: $this->drupalPostForm('taxonomy/term/' . $term2->id() . '/edit', $edit, t('Save')); Chris@0: Chris@0: // Create node with term2. Chris@0: $edit = []; Chris@0: $node = $this->drupalCreateNode(['type' => 'article']); Chris@0: $edit[$this->fieldName . '[]'] = $term2->id(); Chris@0: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); Chris@0: Chris@0: // Generate and test sanitized tokens for term1. Chris@0: $tests = []; Chris@0: $tests['[term:tid]'] = $term1->id(); Chris@0: $tests['[term:name]'] = $term1->getName(); Chris@0: $tests['[term:description]'] = $term1->description->processed; Chris@18: $tests['[term:url]'] = $term1->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@0: $tests['[term:node-count]'] = 0; Chris@0: $tests['[term:parent:name]'] = '[term:parent:name]'; Chris@0: $tests['[term:vocabulary:name]'] = $this->vocabulary->label(); Chris@0: $tests['[term:vocabulary]'] = $this->vocabulary->label(); Chris@0: Chris@0: $base_bubbleable_metadata = BubbleableMetadata::createFromObject($term1); Chris@0: Chris@0: $metadata_tests = []; Chris@0: $metadata_tests['[term:tid]'] = $base_bubbleable_metadata; Chris@0: $metadata_tests['[term:name]'] = $base_bubbleable_metadata; Chris@0: $metadata_tests['[term:description]'] = $base_bubbleable_metadata; Chris@0: $metadata_tests['[term:url]'] = $base_bubbleable_metadata; Chris@0: $metadata_tests['[term:node-count]'] = $base_bubbleable_metadata; Chris@0: $metadata_tests['[term:parent:name]'] = $base_bubbleable_metadata; Chris@0: $bubbleable_metadata = clone $base_bubbleable_metadata; Chris@0: $metadata_tests['[term:vocabulary:name]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags()); Chris@0: $metadata_tests['[term:vocabulary]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags()); Chris@0: Chris@0: foreach ($tests as $input => $expected) { Chris@0: $bubbleable_metadata = new BubbleableMetadata(); Chris@0: $output = $token_service->replace($input, ['term' => $term1], ['langcode' => $language_interface->getId()], $bubbleable_metadata); Chris@0: $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', ['%token' => $input])); Chris@0: $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]); Chris@0: } Chris@0: Chris@0: // Generate and test sanitized tokens for term2. Chris@0: $tests = []; Chris@0: $tests['[term:tid]'] = $term2->id(); Chris@0: $tests['[term:name]'] = $term2->getName(); Chris@0: $tests['[term:description]'] = $term2->description->processed; Chris@18: $tests['[term:url]'] = $term2->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@0: $tests['[term:node-count]'] = 1; Chris@0: $tests['[term:parent:name]'] = $term1->getName(); Chris@18: $tests['[term:parent:url]'] = $term1->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@0: $tests['[term:parent:parent:name]'] = '[term:parent:parent:name]'; Chris@0: $tests['[term:vocabulary:name]'] = $this->vocabulary->label(); Chris@0: Chris@0: // Test to make sure that we generated something for each token. Chris@0: $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); Chris@0: Chris@0: foreach ($tests as $input => $expected) { Chris@0: $output = $token_service->replace($input, ['term' => $term2], ['langcode' => $language_interface->getId()]); Chris@0: $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', ['%token' => $input])); Chris@0: } Chris@0: Chris@0: // Generate and test sanitized tokens. Chris@0: $tests = []; Chris@0: $tests['[vocabulary:vid]'] = $this->vocabulary->id(); Chris@0: $tests['[vocabulary:name]'] = $this->vocabulary->label(); Chris@0: $tests['[vocabulary:description]'] = $this->vocabulary->getDescription(); Chris@0: $tests['[vocabulary:node-count]'] = 1; Chris@0: $tests['[vocabulary:term-count]'] = 2; Chris@0: Chris@0: // Test to make sure that we generated something for each token. Chris@0: $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); Chris@0: Chris@0: foreach ($tests as $input => $expected) { Chris@0: $output = $token_service->replace($input, ['vocabulary' => $this->vocabulary], ['langcode' => $language_interface->getId()]); Chris@0: $this->assertEqual($output, $expected, format_string('Sanitized taxonomy vocabulary token %token replaced.', ['%token' => $input])); Chris@0: } Chris@0: } Chris@0: Chris@0: }