diff --git a/tests/src/Kernel/FieldTest.php b/tests/src/Kernel/FieldTest.php index ea1257e..20839ac 100644 --- a/tests/src/Kernel/FieldTest.php +++ b/tests/src/Kernel/FieldTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\token\Kernel; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\Entity\EntityViewMode; use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\Markup; @@ -146,4 +147,53 @@ class FieldTest extends KernelTestBase { $this->assertEqual($token_info['module'], 'token', 'The token info module is correct.'); } + /** + * Test tokens on node with the token view mode overriding default formatters. + */ + public function testTokenViewMode() { + $value = 'A really long string that should be trimmed by the special formatter on token view we are going to have.'; + Unicode::check(); + + // Create a node with a value in the text field and test its token. + $format = FilterFormat::create([ + 'format' => 'test', + 'weight' => 1, + 'filters' => [ + 'filter_html_escape' => ['status' => TRUE], + ], + ]); + $format->save(); + + $entity = Node::create([ + 'title' => 'Test node title', + 'type' => 'article', + 'test_field' => [ + 'value' => $value, + 'format' => $format->id(), + ], + ]); + $entity->save(); + + $this->assertTokens('node', ['node' => $entity], [ + 'test_field' => Markup::create($value), + ]); + + $view_mode = EntityViewMode::create([ + 'id' => 'node.token', + 'targetEntityType' => 'node', + ]); + $view_mode->save(); + $entity_display = entity_get_display('node', 'article', 'token'); + $entity_display->setComponent('test_field', [ + 'type' => 'text_trimmed', + 'settings' => [ + 'trim_length' => 50, + ] + ]); + $entity_display->save(); + + $this->assertTokens('node', ['node' => $entity], [ + 'test_field' => Markup::create(substr($value, 0, 50)), + ]); + } }