3 namespace Drupal\Tests\token\Kernel;
5 use Drupal\node\Entity\Node;
6 use Drupal\taxonomy\Entity\Term;
7 use Drupal\taxonomy\Entity\Vocabulary;
8 use Drupal\taxonomy\VocabularyInterface;
11 * Tests entity tokens.
15 class EntityTest extends KernelTestBase {
22 public static $modules = ['node', 'taxonomy', 'text'];
27 public function setUp() {
30 // Create the default tags vocabulary.
31 $vocabulary = Vocabulary::create([
37 $this->installEntitySchema('taxonomy_term');
38 $this->installEntitySchema('user');
39 $this->installEntitySchema('node');
41 $this->vocab = $vocabulary;
44 function testEntityMapping() {
45 /** @var \Drupal\token\TokenEntityMapperInterface $mapper */
46 $mapper = \Drupal::service('token.entity_mapper');
47 $this->assertIdentical($mapper->getEntityTypeForTokenType('node'), 'node');
48 $this->assertIdentical($mapper->getEntityTypeForTokenType('term'), 'taxonomy_term');
49 $this->assertIdentical($mapper->getEntityTypeForTokenType('vocabulary'), 'taxonomy_vocabulary');
50 $this->assertIdentical($mapper->getEntityTypeForTokenType('invalid'), FALSE);
51 $this->assertIdentical($mapper->getEntityTypeForTokenType('invalid', TRUE), 'invalid');
52 $this->assertIdentical($mapper->getTokenTypeForEntityType('node'), 'node');
53 $this->assertIdentical($mapper->getTokenTypeForEntityType('taxonomy_term'), 'term');
54 $this->assertIdentical($mapper->getTokenTypeForEntityType('taxonomy_vocabulary'), 'vocabulary');
55 $this->assertIdentical($mapper->getTokenTypeForEntityType('invalid'), FALSE);
56 $this->assertIdentical($mapper->getTokenTypeForEntityType('invalid', TRUE), 'invalid');
58 // Test that when we send the mis-matched entity type into
59 // Drupal\Core\Utility\Token::replace() that we still get the tokens
61 $vocabulary = Vocabulary::load('tags');
62 $term = $this->addTerm($vocabulary);
63 $this->assertIdentical(\Drupal::token()->replace('[vocabulary:name]', ['taxonomy_vocabulary' => $vocabulary]), $vocabulary->label());
64 $this->assertIdentical(\Drupal::token()->replace('[term:name][term:vocabulary:name]', ['taxonomy_term' => $term]), $term->label() . $vocabulary->label());
67 function addTerm(VocabularyInterface $vocabulary, array $term = []) {
69 'name' => mb_strtolower($this->randomMachineName(5)),
70 'vid' => $vocabulary->id(),
72 $term = Term::create($term);
78 * Test the [entity:original:*] tokens.
80 function testEntityOriginal() {
81 $node = Node::create(['type' => 'page', 'title' => 'Original title']);
86 'title' => 'Original title',
88 'original:nid' => NULL,
90 $this->assertTokens('node', ['node' => $node], $tokens);
92 // Emulate the original entity property that would be available from
93 // node_save() and change the title for the node.
94 $node->original = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($node->id());
95 $node->title = 'New title';
99 'title' => 'New title',
100 'original' => 'Original title',
101 'original:nid' => $node->id(),
103 $this->assertTokens('node', ['node' => $node], $tokens);