3 namespace Drupal\Tests\token\Kernel;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\node\Entity\Node;
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 token_replace()
59 // that we still get the tokens replaced.
60 $vocabulary = entity_load('taxonomy_vocabulary', 'tags');
61 $term = $this->addTerm($vocabulary);
62 $this->assertIdentical(\Drupal::token()->replace('[vocabulary:name]', array('taxonomy_vocabulary' => $vocabulary)), $vocabulary->label());
63 $this->assertIdentical(\Drupal::token()->replace('[term:name][term:vocabulary:name]', array('taxonomy_term' => $term)), $term->label() . $vocabulary->label());
66 function addTerm(VocabularyInterface $vocabulary, array $term = array()) {
68 'name' => Unicode::strtolower($this->randomMachineName(5)),
69 'vid' => $vocabulary->id(),
71 $term = entity_create('taxonomy_term', $term);
77 * Test the [entity:original:*] tokens.
79 function testEntityOriginal() {
80 $node = Node::create(['type' => 'page', 'title' => 'Original title']);
85 'title' => 'Original title',
87 'original:nid' => NULL,
89 $this->assertTokens('node', array('node' => $node), $tokens);
91 // Emulate the original entity property that would be available from
92 // node_save() and change the title for the node.
93 $node->original = entity_load_unchanged('node', $node->id());
94 $node->title = 'New title';
98 'title' => 'New title',
99 'original' => 'Original title',
100 'original:nid' => $node->id(),
102 $this->assertTokens('node', array('node' => $node), $tokens);