Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / path / tests / src / Kernel / PathItemTest.php
1 <?php
2
3 namespace Drupal\Tests\path\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9
10 /**
11  * Tests loading and storing data using PathItem.
12  *
13  * @group path
14  */
15 class PathItemTest extends KernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['path', 'node', 'user', 'system', 'language', 'content_translation'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     $this->installEntitySchema('node');
31     $this->installEntitySchema('user');
32
33     $this->installSchema('node', ['node_access']);
34
35     $node_type = NodeType::create(['type' => 'foo']);
36     $node_type->save();
37
38     $this->installConfig(['language']);
39     ConfigurableLanguage::createFromLangcode('de')->save();
40   }
41
42   /**
43    * Test creating, loading, updating and deleting aliases through PathItem.
44    */
45   public function testPathItem() {
46
47     /** @var \Drupal\Core\Path\AliasStorageInterface $alias_storage */
48     $alias_storage = \Drupal::service('path.alias_storage');
49
50     $node_storage = \Drupal::entityTypeManager()->getStorage('node');
51
52     $node = Node::create([
53       'title' => 'Testing create()',
54       'type' => 'foo',
55       'path' => ['alias' => '/foo'],
56     ]);
57     $this->assertFalse($node->get('path')->isEmpty());
58     $this->assertEquals('/foo', $node->get('path')->alias);
59
60     $node->save();
61     $this->assertFalse($node->get('path')->isEmpty());
62     $this->assertEquals('/foo', $node->get('path')->alias);
63
64     $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
65     $this->assertEquals('/foo', $stored_alias);
66
67     $node_storage->resetCache();
68
69     /** @var \Drupal\node\NodeInterface $loaded_node */
70     $loaded_node = $node_storage->load($node->id());
71     $this->assertFalse($loaded_node->get('path')->isEmpty());
72     $this->assertEquals('/foo', $loaded_node->get('path')->alias);
73     $node_storage->resetCache();
74     $loaded_node = $node_storage->load($node->id());
75     $this->assertEquals('/foo', $loaded_node->get('path')[0]->get('alias')->getValue());
76
77     $node_storage->resetCache();
78     $loaded_node = $node_storage->load($node->id());
79     $values = $loaded_node->get('path')->getValue();
80     $this->assertEquals('/foo', $values[0]['alias']);
81
82     $node_storage->resetCache();
83     $loaded_node = $node_storage->load($node->id());
84     $this->assertEquals('/foo', $loaded_node->path->alias);
85
86     // Add a translation, verify it is being saved as expected.
87     $translation = $loaded_node->addTranslation('de', $loaded_node->toArray());
88     $translation->get('path')->alias = '/furchtbar';
89     $translation->save();
90
91     // Assert the alias on the English node, the German translation, and the
92     // stored aliases.
93     $node_storage->resetCache();
94     $loaded_node = $node_storage->load($node->id());
95     $this->assertEquals('/foo', $loaded_node->path->alias);
96     $translation = $loaded_node->getTranslation('de');
97     $this->assertEquals('/furchtbar', $translation->path->alias);
98
99     $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
100     $this->assertEquals('/foo', $stored_alias);
101     $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $translation->language()->getId());
102     $this->assertEquals('/furchtbar', $stored_alias);
103
104     $loaded_node->get('path')->alias = '/bar';
105     $this->assertFalse($loaded_node->get('path')->isEmpty());
106     $this->assertEquals('/bar', $loaded_node->get('path')->alias);
107
108     $loaded_node->save();
109     $this->assertFalse($loaded_node->get('path')->isEmpty());
110     $this->assertEquals('/bar', $loaded_node->get('path')->alias);
111
112     $node_storage->resetCache();
113     $loaded_node = $node_storage->load($node->id());
114     $this->assertFalse($loaded_node->get('path')->isEmpty());
115     $this->assertEquals('/bar', $loaded_node->get('path')->alias);
116
117     $loaded_node->get('path')->alias = '/bar';
118     $this->assertFalse($loaded_node->get('path')->isEmpty());
119     $this->assertEquals('/bar', $loaded_node->get('path')->alias);
120
121     $loaded_node->save();
122     $this->assertFalse($loaded_node->get('path')->isEmpty());
123     $this->assertEquals('/bar', $loaded_node->get('path')->alias);
124
125     $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
126     $this->assertEquals('/bar', $stored_alias);
127
128     $old_alias = $alias_storage->lookupPathSource('/foo', $node->language()->getId());
129     $this->assertFalse($old_alias);
130
131     // Reload the node to make sure that it is possible to set a value
132     // immediately after loading.
133     $node_storage->resetCache();
134     $loaded_node = $node_storage->load($node->id());
135     $loaded_node->get('path')->alias = '/foobar';
136     $loaded_node->save();
137
138     $node_storage->resetCache();
139     $loaded_node = $node_storage->load($node->id());
140     $this->assertFalse($loaded_node->get('path')->isEmpty());
141     $this->assertEquals('/foobar', $loaded_node->get('path')->alias);
142     $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
143     $this->assertEquals('/foobar', $stored_alias);
144
145     $old_alias = $alias_storage->lookupPathSource('/bar', $node->language()->getId());
146     $this->assertFalse($old_alias);
147
148     $loaded_node->get('path')->alias = '';
149     $this->assertEquals('', $loaded_node->get('path')->alias);
150
151     $loaded_node->save();
152
153     $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
154     $this->assertFalse($stored_alias);
155
156     // Check that reading, updating and reading the computed alias again in the
157     // same request works without clearing any caches in between.
158     $loaded_node = $node_storage->load($node->id());
159     $loaded_node->get('path')->alias = '/foo';
160     $loaded_node->save();
161
162     $this->assertFalse($loaded_node->get('path')->isEmpty());
163     $this->assertEquals('/foo', $loaded_node->get('path')->alias);
164     $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
165     $this->assertEquals('/foo', $stored_alias);
166
167     $loaded_node->get('path')->alias = '/foobar';
168     $loaded_node->save();
169
170     $this->assertFalse($loaded_node->get('path')->isEmpty());
171     $this->assertEquals('/foobar', $loaded_node->get('path')->alias);
172     $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
173     $this->assertEquals('/foobar', $stored_alias);
174
175     // Check that \Drupal\Core\Field\FieldItemList::equals() for the path field
176     // type.
177     $node = Node::create([
178       'title' => $this->randomString(),
179       'type' => 'foo',
180       'path' => ['alias' => '/foo'],
181     ]);
182     $second_node = Node::create([
183       'title' => $this->randomString(),
184       'type' => 'foo',
185       'path' => ['alias' => '/foo'],
186     ]);
187     $this->assertTrue($node->get('path')->equals($second_node->get('path')));
188
189     // Change the alias for the second node to a different one and try again.
190     $second_node->get('path')->alias = '/foobar';
191     $this->assertFalse($node->get('path')->equals($second_node->get('path')));
192   }
193
194 }