Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / tests / src / Kernel / EntityTest.php
1 <?php
2
3 namespace Drupal\Tests\token\Kernel;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\taxonomy\Entity\Term;
7 use Drupal\taxonomy\Entity\Vocabulary;
8 use Drupal\taxonomy\VocabularyInterface;
9
10 /**
11  * Tests entity tokens.
12  *
13  * @group token
14  */
15 class EntityTest extends KernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['node', 'taxonomy', 'text'];
23
24   /**
25    * {@inheritdoc}
26    */
27   public function setUp() {
28     parent::setUp();
29
30     // Create the default tags vocabulary.
31     $vocabulary = Vocabulary::create([
32       'name' => 'Tags',
33       'vid' => 'tags',
34     ]);
35     $vocabulary->save();
36
37     $this->installEntitySchema('taxonomy_term');
38     $this->installEntitySchema('user');
39     $this->installEntitySchema('node');
40
41     $this->vocab = $vocabulary;
42   }
43
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');
57
58     // Test that when we send the mis-matched entity type into
59     // Drupal\Core\Utility\Token::replace() that we still get the tokens
60     // replaced.
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());
65   }
66
67   function addTerm(VocabularyInterface $vocabulary, array $term = []) {
68     $term += [
69       'name' => mb_strtolower($this->randomMachineName(5)),
70       'vid' => $vocabulary->id(),
71     ];
72     $term = Term::create($term);
73     $term->save();
74     return $term;
75   }
76
77   /**
78    * Test the [entity:original:*] tokens.
79    */
80   function testEntityOriginal() {
81     $node = Node::create(['type' => 'page', 'title' => 'Original title']);
82     $node->save();
83
84     $tokens = [
85       'nid' => $node->id(),
86       'title' => 'Original title',
87       'original' => NULL,
88       'original:nid' => NULL,
89     ];
90     $this->assertTokens('node', ['node' => $node], $tokens);
91
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';
96
97     $tokens = [
98       'nid' => $node->id(),
99       'title' => 'New title',
100       'original' => 'Original title',
101       'original:nid' => $node->id(),
102     ];
103     $this->assertTokens('node', ['node' => $node], $tokens);
104   }
105
106 }