annotate core/modules/node/tests/src/Kernel/NodeTokenReplaceTest.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\node\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Component\Render\FormattableMarkup;
Chris@0 6 use Drupal\Component\Utility\Html;
Chris@0 7 use Drupal\Core\Render\BubbleableMetadata;
Chris@0 8 use Drupal\node\Entity\Node;
Chris@0 9 use Drupal\node\Entity\NodeType;
Chris@0 10 use Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTestBase;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Generates text using placeholders for dummy content to check node token
Chris@0 14 * replacement.
Chris@0 15 *
Chris@0 16 * @group node
Chris@0 17 */
Chris@0 18 class NodeTokenReplaceTest extends TokenReplaceKernelTestBase {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Modules to enable.
Chris@0 22 *
Chris@0 23 * @var array
Chris@0 24 */
Chris@0 25 public static $modules = ['node', 'filter'];
Chris@0 26
Chris@0 27 /**
Chris@0 28 * {@inheritdoc}
Chris@0 29 */
Chris@0 30 protected function setUp() {
Chris@0 31 parent::setUp();
Chris@0 32 $this->installConfig(['filter', 'node']);
Chris@0 33
Chris@0 34 $node_type = NodeType::create(['type' => 'article', 'name' => 'Article']);
Chris@0 35 $node_type->save();
Chris@0 36 node_add_body_field($node_type);
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Creates a node, then tests the tokens generated from it.
Chris@0 41 */
Chris@0 42 public function testNodeTokenReplacement() {
Chris@0 43 $url_options = [
Chris@0 44 'absolute' => TRUE,
Chris@0 45 'language' => $this->interfaceLanguage,
Chris@0 46 ];
Chris@0 47
Chris@0 48 // Create a user and a node.
Chris@0 49 $account = $this->createUser();
Chris@0 50 /* @var $node \Drupal\node\NodeInterface */
Chris@0 51 $node = Node::create([
Chris@0 52 'type' => 'article',
Chris@0 53 'tnid' => 0,
Chris@0 54 'uid' => $account->id(),
Chris@0 55 'title' => '<blink>Blinking Text</blink>',
Chris@0 56 'body' => [['value' => 'Regular NODE body for the test.', 'summary' => 'Fancy NODE summary.', 'format' => 'plain_text']],
Chris@0 57 ]);
Chris@0 58 $node->save();
Chris@0 59
Chris@0 60 // Generate and test tokens.
Chris@0 61 $tests = [];
Chris@0 62 $tests['[node:nid]'] = $node->id();
Chris@0 63 $tests['[node:vid]'] = $node->getRevisionId();
Chris@0 64 $tests['[node:type]'] = 'article';
Chris@0 65 $tests['[node:type-name]'] = 'Article';
Chris@0 66 $tests['[node:title]'] = Html::escape($node->getTitle());
Chris@0 67 $tests['[node:body]'] = $node->body->processed;
Chris@0 68 $tests['[node:summary]'] = $node->body->summary_processed;
Chris@0 69 $tests['[node:langcode]'] = $node->language()->getId();
Chris@18 70 $tests['[node:url]'] = $node->toUrl('canonical', $url_options)->toString();
Chris@18 71 $tests['[node:edit-url]'] = $node->toUrl('edit-form', $url_options)->toString();
Chris@18 72 $tests['[node:author]'] = $account->getAccountName();
Chris@0 73 $tests['[node:author:uid]'] = $node->getOwnerId();
Chris@18 74 $tests['[node:author:name]'] = $account->getAccountName();
Chris@18 75 /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
Chris@18 76 $date_formatter = $this->container->get('date.formatter');
Chris@18 77 $tests['[node:created:since]'] = $date_formatter->formatTimeDiffSince($node->getCreatedTime(), ['langcode' => $this->interfaceLanguage->getId()]);
Chris@18 78 $tests['[node:changed:since]'] = $date_formatter->formatTimeDiffSince($node->getChangedTime(), ['langcode' => $this->interfaceLanguage->getId()]);
Chris@0 79
Chris@0 80 $base_bubbleable_metadata = BubbleableMetadata::createFromObject($node);
Chris@0 81
Chris@0 82 $metadata_tests = [];
Chris@0 83 $metadata_tests['[node:nid]'] = $base_bubbleable_metadata;
Chris@0 84 $metadata_tests['[node:vid]'] = $base_bubbleable_metadata;
Chris@0 85 $metadata_tests['[node:type]'] = $base_bubbleable_metadata;
Chris@0 86 $metadata_tests['[node:type-name]'] = $base_bubbleable_metadata;
Chris@0 87 $metadata_tests['[node:title]'] = $base_bubbleable_metadata;
Chris@0 88 $metadata_tests['[node:body]'] = $base_bubbleable_metadata;
Chris@0 89 $metadata_tests['[node:summary]'] = $base_bubbleable_metadata;
Chris@0 90 $metadata_tests['[node:langcode]'] = $base_bubbleable_metadata;
Chris@0 91 $metadata_tests['[node:url]'] = $base_bubbleable_metadata;
Chris@0 92 $metadata_tests['[node:edit-url]'] = $base_bubbleable_metadata;
Chris@0 93 $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0 94 $metadata_tests['[node:author]'] = $bubbleable_metadata->addCacheTags(['user:1']);
Chris@0 95 $metadata_tests['[node:author:uid]'] = $bubbleable_metadata;
Chris@0 96 $metadata_tests['[node:author:name]'] = $bubbleable_metadata;
Chris@0 97 $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0 98 $metadata_tests['[node:created:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
Chris@0 99 $metadata_tests['[node:changed:since]'] = $bubbleable_metadata;
Chris@0 100
Chris@0 101 // Test to make sure that we generated something for each token.
Chris@0 102 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
Chris@0 103
Chris@0 104 foreach ($tests as $input => $expected) {
Chris@0 105 $bubbleable_metadata = new BubbleableMetadata();
Chris@0 106 $output = $this->tokenService->replace($input, ['node' => $node], ['langcode' => $this->interfaceLanguage->getId()], $bubbleable_metadata);
Chris@0 107 $this->assertEqual($output, $expected, format_string('Node token %token replaced.', ['%token' => $input]));
Chris@0 108 $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
Chris@0 109 }
Chris@0 110
Chris@0 111 // Repeat for a node without a summary.
Chris@0 112 $node = Node::create([
Chris@0 113 'type' => 'article',
Chris@0 114 'uid' => $account->id(),
Chris@0 115 'title' => '<blink>Blinking Text</blink>',
Chris@0 116 'body' => [['value' => 'A string that looks random like TR5c2I', 'format' => 'plain_text']],
Chris@0 117 ]);
Chris@0 118 $node->save();
Chris@0 119
Chris@0 120 // Generate and test token - use full body as expected value.
Chris@0 121 $tests = [];
Chris@0 122 $tests['[node:summary]'] = $node->body->processed;
Chris@0 123
Chris@0 124 // Test to make sure that we generated something for each token.
Chris@0 125 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated for node without a summary.');
Chris@0 126
Chris@0 127 foreach ($tests as $input => $expected) {
Chris@0 128 $output = $this->tokenService->replace($input, ['node' => $node], ['language' => $this->interfaceLanguage]);
Chris@0 129 $this->assertEqual($output, $expected, new FormattableMarkup('Node token %token replaced for node without a summary.', ['%token' => $input]));
Chris@0 130 }
Chris@0 131 }
Chris@0 132
Chris@0 133 }