View source
<?php
namespace Drupal\Tests\node_summary_token_from_p;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTestBase;
class HookTokensTest extends TokenReplaceKernelTestBase {
public static $modules = [
'node',
'filter',
'node_summary_token_from_p',
];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'filter',
'node',
]);
$article_node_type = NodeType::create([
'type' => 'article',
'name' => 'Article',
]);
$article_node_type
->save();
node_add_body_field($article_node_type);
$article_node_type_without_body = NodeType::create([
'type' => 'article_wo_body',
'name' => 'Article without body',
]);
$article_node_type_without_body
->save();
FilterFormat::create([
'format' => 'html',
'name' => 'html',
'filters' => [],
])
->save();
$field_storage = FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'field_text',
'type' => 'text_long',
'cardinality' => 1,
]);
$field_storage
->save();
$field = FieldConfig::create([
'entity_type' => 'node',
'bundle' => 'article_wo_body',
'field_name' => 'field_text',
'label' => 'field_text',
'required' => FALSE,
]);
$field
->save();
$entityDisplayRepository = $this->container
->get('entity_display.repository');
$display = $entityDisplayRepository
->getViewDisplay('node', 'article_wo_body');
$display
->setComponent('field_text', [
'type' => 'text_default',
]);
$display
->save();
}
public function testNodeSummaryTokenReplacement() {
$account = $this
->createUser();
$node = Node::create([
'type' => 'article',
'tnid' => 0,
'uid' => $account
->id(),
'title' => 'Title',
'body' => [
[
'value' => 'Regular NODE body for the test.',
'summary' => 'Fancy NODE summary.',
'format' => 'plain_text',
],
],
]);
$node
->save();
$tests = [];
$tests['[node:summary]'] = $node->body->summary_processed;
$base_bubbleable_metadata = BubbleableMetadata::createFromObject($node);
$metadata_tests = [];
$metadata_tests['[node:summary]'] = $base_bubbleable_metadata;
$this
->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');
foreach ($tests as $input => $expected) {
$bubbleable_metadata = new BubbleableMetadata();
$output = $this->tokenService
->replace($input, [
'node' => $node,
], [
'langcode' => $this->interfaceLanguage
->getId(),
], $bubbleable_metadata);
$this
->assertEqual($output, $expected, new FormattableMarkup('Node token %token replaced.', [
'%token' => $input,
]));
$this
->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
}
$node = Node::create([
'type' => 'article',
'uid' => $account
->id(),
'title' => '<blink>Blinking Text</blink>',
'body' => [
[
'value' => 'A string that looks random like TR5c2I',
'format' => 'plain_text',
],
],
]);
$node
->save();
$tests = [];
$tests['[node:summary]'] = $node->body->processed;
$this
->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated for node without a summary.');
foreach ($tests as $input => $expected) {
$output = $this->tokenService
->replace($input, [
'node' => $node,
], [
'language' => $this->interfaceLanguage,
]);
$this
->assertEqual($output, $expected, new FormattableMarkup('Node token %token replaced for node without a summary.', [
'%token' => $input,
]));
}
}
public function testNodeSummaryTokenFromFieldTextReplacement() {
$account = $this
->createUser();
$node = Node::create([
'type' => 'article_wo_body',
'tnid' => 0,
'uid' => $account
->id(),
'title' => 'Title',
'field_text' => [
[
'value' => '<p>This is sentence 1. This is sentence 2?</p><p>This is sentence 3!</p><p>This is sentence 4.</p>',
'format' => 'html',
],
],
]);
$node
->save();
$tests = [];
$tests['[node:summary]'] = 'This is sentence 1. This is sentence 2? This is sentence 3!';
$base_bubbleable_metadata = BubbleableMetadata::createFromObject($node);
$metadata_tests = [];
$metadata_tests['[node:summary]'] = $base_bubbleable_metadata;
$this
->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');
foreach ($tests as $input => $expected) {
$bubbleable_metadata = new BubbleableMetadata();
$output = $this->tokenService
->replace($input, [
'node' => $node,
], [
'langcode' => $this->interfaceLanguage
->getId(),
], $bubbleable_metadata);
$this
->assertEqual($output, $expected, new FormattableMarkup('Node token %token replaced.', [
'%token' => $input,
]));
$this
->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
}
}
}