7ff471dfe5d4383c9963687f848cba5b99718e0d
[yaffs-website] / web / modules / contrib / token / tests / src / Kernel / EntityTest.php
1 <?php
2
3 namespace Drupal\Tests\token\Kernel;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\node\Entity\Node;
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 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());
64   }
65
66   function addTerm(VocabularyInterface $vocabulary, array $term = array()) {
67     $term += array(
68       'name' => Unicode::strtolower($this->randomMachineName(5)),
69       'vid' => $vocabulary->id(),
70     );
71     $term = entity_create('taxonomy_term', $term);
72     $term->save();
73     return $term;
74   }
75
76   /**
77    * Test the [entity:original:*] tokens.
78    */
79   function testEntityOriginal() {
80     $node = Node::create(['type' => 'page', 'title' => 'Original title']);
81     $node->save();
82
83     $tokens = array(
84       'nid' => $node->id(),
85       'title' => 'Original title',
86       'original' => NULL,
87       'original:nid' => NULL,
88     );
89     $this->assertTokens('node', array('node' => $node), $tokens);
90
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';
95
96     $tokens = array(
97       'nid' => $node->id(),
98       'title' => 'New title',
99       'original' => 'Original title',
100       'original:nid' => $node->id(),
101     );
102     $this->assertTokens('node', array('node' => $node), $tokens);
103   }
104 }