annotate core/modules/taxonomy/tests/src/Functional/TokenReplaceTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\taxonomy\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
Chris@0 6 use Drupal\Core\Render\BubbleableMetadata;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Generates text using placeholders for dummy content to check taxonomy token
Chris@0 10 * replacement.
Chris@0 11 *
Chris@0 12 * @group taxonomy
Chris@0 13 */
Chris@0 14 class TokenReplaceTest extends TaxonomyTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * The vocabulary used for creating terms.
Chris@0 18 *
Chris@0 19 * @var \Drupal\taxonomy\VocabularyInterface
Chris@0 20 */
Chris@0 21 protected $vocabulary;
Chris@0 22
Chris@0 23 /**
Chris@0 24 * Name of the taxonomy term reference field.
Chris@0 25 *
Chris@0 26 * @var string
Chris@0 27 */
Chris@0 28 protected $fieldName;
Chris@0 29
Chris@0 30 protected function setUp() {
Chris@0 31 parent::setUp();
Chris@0 32 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
Chris@0 33 $this->vocabulary = $this->createVocabulary();
Chris@0 34 $this->fieldName = 'taxonomy_' . $this->vocabulary->id();
Chris@0 35
Chris@0 36 $handler_settings = [
Chris@0 37 'target_bundles' => [
Chris@0 38 $this->vocabulary->id() => $this->vocabulary->id(),
Chris@0 39 ],
Chris@0 40 'auto_create' => TRUE,
Chris@0 41 ];
Chris@0 42 $this->createEntityReferenceField('node', 'article', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
Chris@0 43
Chris@0 44 entity_get_form_display('node', 'article', 'default')
Chris@0 45 ->setComponent($this->fieldName, [
Chris@0 46 'type' => 'options_select',
Chris@0 47 ])
Chris@0 48 ->save();
Chris@0 49 entity_get_display('node', 'article', 'default')
Chris@0 50 ->setComponent($this->fieldName, [
Chris@0 51 'type' => 'entity_reference_label',
Chris@0 52 ])
Chris@0 53 ->save();
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Creates some terms and a node, then tests the tokens generated from them.
Chris@0 58 */
Chris@0 59 public function testTaxonomyTokenReplacement() {
Chris@0 60 $token_service = \Drupal::token();
Chris@0 61 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
Chris@0 62
Chris@0 63 // Create two taxonomy terms.
Chris@0 64 $term1 = $this->createTerm($this->vocabulary);
Chris@0 65 $term2 = $this->createTerm($this->vocabulary);
Chris@0 66
Chris@0 67 // Edit $term2, setting $term1 as parent.
Chris@0 68 $edit = [];
Chris@0 69 $edit['name[0][value]'] = '<blink>Blinking Text</blink>';
Chris@0 70 $edit['parent[]'] = [$term1->id()];
Chris@0 71 $this->drupalPostForm('taxonomy/term/' . $term2->id() . '/edit', $edit, t('Save'));
Chris@0 72
Chris@0 73 // Create node with term2.
Chris@0 74 $edit = [];
Chris@0 75 $node = $this->drupalCreateNode(['type' => 'article']);
Chris@0 76 $edit[$this->fieldName . '[]'] = $term2->id();
Chris@0 77 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
Chris@0 78
Chris@0 79 // Generate and test sanitized tokens for term1.
Chris@0 80 $tests = [];
Chris@0 81 $tests['[term:tid]'] = $term1->id();
Chris@0 82 $tests['[term:name]'] = $term1->getName();
Chris@0 83 $tests['[term:description]'] = $term1->description->processed;
Chris@18 84 $tests['[term:url]'] = $term1->toUrl('canonical', ['absolute' => TRUE])->toString();
Chris@0 85 $tests['[term:node-count]'] = 0;
Chris@0 86 $tests['[term:parent:name]'] = '[term:parent:name]';
Chris@0 87 $tests['[term:vocabulary:name]'] = $this->vocabulary->label();
Chris@0 88 $tests['[term:vocabulary]'] = $this->vocabulary->label();
Chris@0 89
Chris@0 90 $base_bubbleable_metadata = BubbleableMetadata::createFromObject($term1);
Chris@0 91
Chris@0 92 $metadata_tests = [];
Chris@0 93 $metadata_tests['[term:tid]'] = $base_bubbleable_metadata;
Chris@0 94 $metadata_tests['[term:name]'] = $base_bubbleable_metadata;
Chris@0 95 $metadata_tests['[term:description]'] = $base_bubbleable_metadata;
Chris@0 96 $metadata_tests['[term:url]'] = $base_bubbleable_metadata;
Chris@0 97 $metadata_tests['[term:node-count]'] = $base_bubbleable_metadata;
Chris@0 98 $metadata_tests['[term:parent:name]'] = $base_bubbleable_metadata;
Chris@0 99 $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0 100 $metadata_tests['[term:vocabulary:name]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags());
Chris@0 101 $metadata_tests['[term:vocabulary]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags());
Chris@0 102
Chris@0 103 foreach ($tests as $input => $expected) {
Chris@0 104 $bubbleable_metadata = new BubbleableMetadata();
Chris@0 105 $output = $token_service->replace($input, ['term' => $term1], ['langcode' => $language_interface->getId()], $bubbleable_metadata);
Chris@0 106 $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', ['%token' => $input]));
Chris@0 107 $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
Chris@0 108 }
Chris@0 109
Chris@0 110 // Generate and test sanitized tokens for term2.
Chris@0 111 $tests = [];
Chris@0 112 $tests['[term:tid]'] = $term2->id();
Chris@0 113 $tests['[term:name]'] = $term2->getName();
Chris@0 114 $tests['[term:description]'] = $term2->description->processed;
Chris@18 115 $tests['[term:url]'] = $term2->toUrl('canonical', ['absolute' => TRUE])->toString();
Chris@0 116 $tests['[term:node-count]'] = 1;
Chris@0 117 $tests['[term:parent:name]'] = $term1->getName();
Chris@18 118 $tests['[term:parent:url]'] = $term1->toUrl('canonical', ['absolute' => TRUE])->toString();
Chris@0 119 $tests['[term:parent:parent:name]'] = '[term:parent:parent:name]';
Chris@0 120 $tests['[term:vocabulary:name]'] = $this->vocabulary->label();
Chris@0 121
Chris@0 122 // Test to make sure that we generated something for each token.
Chris@0 123 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
Chris@0 124
Chris@0 125 foreach ($tests as $input => $expected) {
Chris@0 126 $output = $token_service->replace($input, ['term' => $term2], ['langcode' => $language_interface->getId()]);
Chris@0 127 $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', ['%token' => $input]));
Chris@0 128 }
Chris@0 129
Chris@0 130 // Generate and test sanitized tokens.
Chris@0 131 $tests = [];
Chris@0 132 $tests['[vocabulary:vid]'] = $this->vocabulary->id();
Chris@0 133 $tests['[vocabulary:name]'] = $this->vocabulary->label();
Chris@0 134 $tests['[vocabulary:description]'] = $this->vocabulary->getDescription();
Chris@0 135 $tests['[vocabulary:node-count]'] = 1;
Chris@0 136 $tests['[vocabulary:term-count]'] = 2;
Chris@0 137
Chris@0 138 // Test to make sure that we generated something for each token.
Chris@0 139 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
Chris@0 140
Chris@0 141 foreach ($tests as $input => $expected) {
Chris@0 142 $output = $token_service->replace($input, ['vocabulary' => $this->vocabulary], ['langcode' => $language_interface->getId()]);
Chris@0 143 $this->assertEqual($output, $expected, format_string('Sanitized taxonomy vocabulary token %token replaced.', ['%token' => $input]));
Chris@0 144 }
Chris@0 145 }
Chris@0 146
Chris@0 147 }