diff --git a/core/modules/node/src/Tests/NodeTokenLanguageTest.php b/core/modules/node/src/Tests/NodeTokenLanguageTest.php index 41ab17b..0ec44fe 100644 --- a/core/modules/node/src/Tests/NodeTokenLanguageTest.php +++ b/core/modules/node/src/Tests/NodeTokenLanguageTest.php @@ -7,11 +7,11 @@ namespace Drupal\node\Tests; -use Drupal\Component\Render\HtmlEscapedText; use Drupal\simpletest\WebTestBase; use Drupal\language\Entity\ConfigurableLanguage; use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\Node; +use Drupal\system\Tests\Utility\AssertTokenReplacementTrait; /** * Check if node tokens are multilingual. @@ -20,6 +20,8 @@ */ class NodeTokenLanguageTest extends WebTestBase { + use AssertTokenReplacementTrait; + /** * Modules to enable. * @@ -86,21 +88,40 @@ public function testMultilingualNodeTokens() { // Create English "Basic page" node. $langcode = 'en'; $this->drupalLogin($this->users[$langcode]); - $fields = $this->getFieldsArray($langcode); - - $node = Node::create($fields); + $node = Node::create($this->getFieldsArray($langcode)); $node->save(); - $this->assertTokenReplacement($node); + $this->executeLanguageSensitiveTokenReplacementAssertions($node, 'English node token %token replaced with %output which is equal to %expected'); // Create italian translation with new field values. $langcode = 'it'; $this->drupalLogin($this->users[$langcode]); - $fields = $this->getFieldsArray($langcode); - $node->addTranslation($langcode, $fields); + $node->addTranslation($langcode, $this->getFieldsArray($langcode)); $translation = $node->getTranslation($langcode); - $this->assertTokenReplacement($translation); + $this->executeLanguageSensitiveTokenReplacementAssertions($translation, 'Italian node token %token replaced with %output which is equal to %expected'); + } + + /** + * Asserts language sensitive token replacement for general node tokens. + * + * @param \Drupal\node\Entity\Node $node + * The node to assert. + * @param $message + * A message to describe the test results. + * @see AssertTokenReplacementTrait::assertTokenReplacement() + */ + protected function executeLanguageSensitiveTokenReplacementAssertions(Node $node, $message) { + $tests['[node:title]'] = $node->getTitle(); + $tests['[node:body]'] = $node->body->processed; + $tests['[node:summary]'] = !empty($node->body->summary) ? $node->body->summary_processed : $node->body->processed; + $tests['[node:author]'] = $node->getOwner()->getAccountName(); + $tests['[node:created]'] = \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'medium', '', NULL, $node->language()->getId()); + $tests['[node:changed]'] = \Drupal::service('date.formatter')->format($node->getChangedTime(), 'medium', '', NULL, $node->language()->getId()); + + $data = ['node' => $node]; + $options = ['langcode' => $node->language()->getId()]; + $this->assertTokenReplacement($tests, $data, $options, $message); } /** @@ -125,41 +146,4 @@ protected function getFieldsArray($langcode) { ]; } - /** - * Asserts if tokens are correctly replaced. - * - * @param \Drupal\node\Entity\Node $node - * The node for which to test token replacement. - */ - protected function assertTokenReplacement(Node $node) { - $options = ['langcode' => $node->language()->getId()]; - - $tokens = [ - '[node:title]' => [ - 'expected' => new HtmlEscapedText($node->title->value), - 'actual' => \Drupal::token()->replace('[node:title]', ['node' => $node], $options) - ], - '[node:body]' => [ - 'expected' => $node->get('body')[0]->processed, - 'actual' => $token_replacement = \Drupal::token()->replace('[node:body]', ['node' => $node], $options) - ], - '[node:author]' => [ - 'expected' => $node->getOwner()->getAccountName(), - 'actual' => \Drupal::token()->replace('[node:author]', ['node' => $node], $options) - ], - '[node:created]' => [ - 'expected' => \Drupal::service('date.formatter')->format($node->created->value, 'medium', '', NULL, $options['langcode']), - 'actual' => \Drupal::token()->replace('[node:created]', ['node' => $node], $options) - ], - '[node:changed]' => [ - 'expected' => \Drupal::service('date.formatter')->format($node->changed->value, 'medium', '', NULL, $options['langcode']), - 'actual' => \Drupal::token()->replace('[node:changed]', ['node' => $node], $options), - ], - ]; - - foreach ($tokens as $token => $replacements) { - $this->assertEqual($replacements['expected'], $replacements['actual'], $token . ' replaced successfully'); - } - } - } diff --git a/core/modules/system/src/Tests/Utility/AssertTokenReplacementTrait.php b/core/modules/system/src/Tests/Utility/AssertTokenReplacementTrait.php new file mode 100644 index 0000000..4c9f4fc --- /dev/null +++ b/core/modules/system/src/Tests/Utility/AssertTokenReplacementTrait.php @@ -0,0 +1,84 @@ + $expected) { + $bubbleable_metadata = new BubbleableMetadata(); + $output = \Drupal::token()->replace($token, $data, $options, $bubbleable_metadata); + $this->assertEqual($output, new HtmlEscapedText($expected), new FormattableMarkup($message, [ + '%token' => $token, + '%output' => $output, + '%expected' => $expected, + ])); + + $this->assertEqual($bubbleable_metadata, $metadata_tests[$token]); + } + } + + /** + * Asserts if tokens are correctly replaced. + * + * @param array $tests + * Expected results keyed by their respective tokens. + * @param array $data + * The data to perform the replacement on. @see Token::replace(). + * @param array $options + * Additional replacement options. @see Token::replace(). + * @param $message + * The message to display with the assertion. Can contain replacement + * tokens: + * - %token for the token name + * - %output for the value returned by the token replacement service + * - %expected for the expected result + */ + protected function assertTokenReplacement(array $tests, array $data, array $options, $message) { + foreach ($tests as $token => $expected) { + $output = \Drupal::token()->replace($token, $data, $options); + $expected = ($expected instanceof MarkupInterface) ? $expected : new HtmlEscapedText($expected); + $this->assertEqual($output, $expected, new FormattableMarkup($message, [ + '%token' => $token, + '%output' => $output, + '%expected' => $expected, + ])); + } + } + +} diff --git a/core/modules/taxonomy/src/Tests/TaxonomyTokenLanguageTest.php b/core/modules/taxonomy/src/Tests/TaxonomyTokenLanguageTest.php index 9c5e682..29a5386 100644 --- a/core/modules/taxonomy/src/Tests/TaxonomyTokenLanguageTest.php +++ b/core/modules/taxonomy/src/Tests/TaxonomyTokenLanguageTest.php @@ -7,8 +7,8 @@ namespace Drupal\taxonomy\Tests; -use Drupal\Component\Render\HtmlEscapedText; use Drupal\language\Entity\ConfigurableLanguage; +use Drupal\system\Tests\Utility\AssertTokenReplacementTrait; use Drupal\taxonomy\Entity\Term; /** @@ -18,6 +18,8 @@ */ class TaxonomyTokenLanguageTest extends TaxonomyTestBase { + use AssertTokenReplacementTrait; + /** * Vocabulary for testing. * @@ -30,7 +32,7 @@ class TaxonomyTokenLanguageTest extends TaxonomyTestBase { * * @var array */ - public static $modules = ['node', 'language', 'content_translation']; + public static $modules = ['taxonomy', 'language', 'content_translation']; /** * {@inheritdoc} @@ -62,14 +64,12 @@ public function testMultilingualTaxonomyTokens() { $langcode = 'en'; $fields = $this->getFieldsArray($langcode); $term = $this->createTerm($this->vocabulary, $fields); - - $this->verifyTokensCorrectlyReplaced($term); + $this->executeLanguageSensitiveTokenReplacementAssertions($term, "English taxonomy term token %token replaced with %output which is equal to %expected"); $langcode = 'it'; $fields = $this->getFieldsArray($langcode); $term = $term->addTranslation($langcode, $fields); - - $this->verifyTokensCorrectlyReplaced($term); + $this->executeLanguageSensitiveTokenReplacementAssertions($term, "Italian taxonomy term token %token replaced with %output which is equal to %expected"); } /** @@ -90,32 +90,22 @@ protected function getFieldsArray($langcode) { } /** - * Verifies if tokens were correctly replaced. + * Asserts language sensitive token replacement for general taxonomy tokens. * * @param \Drupal\taxonomy\Entity\Term $term - * The term that needs to be checked. + * The term to assert. + * @param $message + * A message to describe the test results. + * @see AssertTokenReplacementTrait::assertTokenReplacement() */ - protected function verifyTokensCorrectlyReplaced(Term $term) { - $options = ['langcode' => $term->language()->getId()]; + protected function executeLanguageSensitiveTokenReplacementAssertions(Term $term, $message) { + $tests['[term:name]'] = $term->getName(); + $tests['[term:description]'] = $term->getDescription(); + $tests['[term:url]'] = $term->url('canonical', ['absolute' => TRUE]); - $tokens = [ - '[term:name]' => [ - 'expected' => new HtmlEscapedText($term->name->value), - 'actual' => \Drupal::token()->replace('[term:name]', ['term' => $term], $options) - ], - '[term:description]' => [ - 'expected' => new HtmlEscapedText($term->description->value), - 'actual' => $token_replacement = \Drupal::token()->replace('[term:description]', ['term' => $term], $options) - ], - '[term:url]' => [ - 'expected' => $term->url('canonical', ['absolute' => TRUE]), - 'actual' => \Drupal::token()->replace('[term:url]', ['term' => $term], $options) - ] - ]; - - foreach ($tokens as $token => $replacements) { - $this->assertEqual($replacements['expected'], $replacements['actual'], $token . ' replaced successfully'); - } + $data = ['term' => $term]; + $options = ['langcode' => $term->language()->getId()]; + $this->assertTokenReplacement($tests, $data, $options, $message); } } diff --git a/core/modules/taxonomy/src/Tests/TokenReplaceTest.php b/core/modules/taxonomy/src/Tests/TokenReplaceTest.php index 12206ef..494a3d9 100644 --- a/core/modules/taxonomy/src/Tests/TokenReplaceTest.php +++ b/core/modules/taxonomy/src/Tests/TokenReplaceTest.php @@ -7,10 +7,9 @@ namespace Drupal\taxonomy\Tests; -Use Drupal\Component\Render\FormattableMarkup; -use Drupal\Component\Render\HtmlEscapedText; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Render\BubbleableMetadata; +use Drupal\system\Tests\Utility\AssertTokenReplacementTrait; /** * Generates text using placeholders. @@ -22,6 +21,8 @@ */ class TokenReplaceTest extends TaxonomyTestBase { + use AssertTokenReplacementTrait; + /** * The vocabulary used for creating terms. * @@ -49,7 +50,6 @@ class TokenReplaceTest extends TaxonomyTestBase { protected function setUp() { parent::setUp(); - $this->tokenService = \Drupal::token(); $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access'])); $this->vocabulary = $this->createVocabulary(['name' => 'V1 "<name>"']); $this->fieldName = 'taxonomy_' . $this->vocabulary->id(); @@ -129,7 +129,7 @@ public function testTaxonomyTokenReplacement() { $data = ['term' => $term1]; $options = ['langcode' => $language_interface->getId()]; - $msg = 'Sanitized taxonomy term 1 token %token replaced with %output. Expected is %expected'; + $msg = 'Taxonomy term 1 token %token replaced with %output which is equal to %expected'; $this->assertTokenReplacementAndCheckMetadata($tests, $data, $options, $msg, $metadata_tests); // Generate and test sanitized tokens for term2. @@ -154,9 +154,33 @@ public function testTaxonomyTokenReplacement() { // Test to make sure that we generated something for each token. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); + $base_bubbleable_metadata = BubbleableMetadata::createFromObject($term2); + $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; + $bubbleable_metadata = clone $base_bubbleable_metadata; + $bubbleable_metadata = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags()); + $metadata_tests['[term:vocabulary]'] = $bubbleable_metadata; + $metadata_tests['[term:vocabulary:name]'] = $bubbleable_metadata; + $base_bubbleable_metadata = BubbleableMetadata::createFromObject($term1)->addCacheTags($term2->getCacheTags()); + $metadata_tests['[term:parent]'] = $base_bubbleable_metadata; + $metadata_tests['[term:parent:tid]'] = $base_bubbleable_metadata; + $metadata_tests['[term:parent:name]'] = $base_bubbleable_metadata; + $metadata_tests['[term:parent:description]'] = $base_bubbleable_metadata; + $metadata_tests['[term:parent:url]'] = $base_bubbleable_metadata; + $metadata_tests['[term:parent:node-count]'] = $base_bubbleable_metadata; + $metadata_tests['[term:parent:parent:name]'] = $base_bubbleable_metadata; + $bubbleable_metadata = clone $base_bubbleable_metadata; + $bubbleable_metadata = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags()); + $metadata_tests['[term:parent:vocabulary:name]'] = $bubbleable_metadata; + $metadata_tests['[term:parent:vocabulary]'] = $bubbleable_metadata; + $data = ['term' => $term2]; - $msg = 'Sanitized taxonomy term 2 token %token replaced with %output. Expected is %expected'; - $this->assertTokenReplacement($tests, $data, $options, $msg); + $msg = 'Taxonomy term 2 token %token replaced with %output which is equal to %expected'; + $this->assertTokenReplacementAndCheckMetadata($tests, $data, $options, $msg, $metadata_tests); // Generate and test sanitized tokens. $tests = array(); @@ -169,71 +193,18 @@ public function testTaxonomyTokenReplacement() { // Test to make sure that we generated something for each token. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); - $data = ['vocabulary' => $this->vocabulary]; - $msg = 'Sanitized taxonomy vocabulary token %token replaced with %output. Expected is %expected'; - $this->assertTokenReplacement($tests, $data, $options, $msg); - } + $base_bubbleable_metadata = BubbleableMetadata::createFromObject($this->vocabulary); - /** - * Checks token replacement and metadata. - * - * Asserts if tokens are correctly replaced and verifies that the correct - * metadata has been set. - * - * @param array $tests - * Expected results keyed by their respective tokens. - * @param array $data - * The data to perform the replacement on. @see Token::replace(). - * @param array $options - * Aditional replacement options. @see Token::replace(). - * @param $message - * The message to display with the assertion. Can contain replacement - * tokens: - * - %token for the token name - * - %output for the value returned by the token replacement service - * - %expected for the expected result - * @param array $metadata_tests - * The metadata to verify. Keyed by the relevant token. - */ - protected function assertTokenReplacementAndCheckMetadata(array $tests, array $data, array $options, $message, array $metadata_tests) { - foreach ($tests as $token => $expected) { - $bubbleable_metadata = new BubbleableMetadata(); - $output = $this->tokenService->replace($token, $data, $options, $bubbleable_metadata); - $this->assertEqual($output, new HtmlEscapedText($expected), new FormattableMarkup($message, [ - '%token' => $token, - '%output' => $output, - '%expected' => $expected, - ])); - - $this->assertEqual($bubbleable_metadata, $metadata_tests[$token]); - } - } + $metadata_tests = array(); + $metadata_tests['[vocabulary:vid]'] = $base_bubbleable_metadata; + $metadata_tests['[vocabulary:name]'] = $base_bubbleable_metadata; + $metadata_tests['[vocabulary:description]'] = $base_bubbleable_metadata; + $metadata_tests['[vocabulary:node-count]'] = $base_bubbleable_metadata; + $metadata_tests['[vocabulary:term-count]'] = $base_bubbleable_metadata; - /** - * Asserts if tokens are correctly replaced. - * - * @param array $tests - * Expected results keyed by their respective tokens. - * @param array $data - * The data to perform the replacement on. @see Token::replace(). - * @param array $options - * Aditional replacement options. @see Token::replace(). - * @param $message - * The message to display with the assertion. Can contain replacement - * tokens: - * - %token for the token name - * - %output for the value returned by the token replacement service - * - %expected for the expected result - */ - protected function assertTokenReplacement(array $tests, array $data, array $options, $message) { - foreach ($tests as $token => $expected) { - $output = $this->tokenService->replace($token, $data, $options); - $this->assertEqual($output, new HtmlEscapedText($expected), new FormattableMarkup($message, [ - '%token' => $token, - '%output' => $output, - '%expected' => $expected, - ])); - } + $data = ['vocabulary' => $this->vocabulary]; + $msg = 'Taxonomy vocabulary token %token replaced with %output which is equal to %expected'; + $this->assertTokenReplacementAndCheckMetadata($tests, $data, $options, $msg, $metadata_tests); } }