3 namespace Drupal\Tests\path\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
11 * Tests loading and storing data using PathItem.
15 class PathItemTest extends KernelTestBase {
22 public static $modules = ['path', 'node', 'user', 'system', 'language', 'content_translation'];
27 protected function setUp() {
30 $this->installEntitySchema('node');
31 $this->installEntitySchema('user');
33 $this->installSchema('node', ['node_access']);
35 $node_type = NodeType::create(['type' => 'foo']);
38 $this->installConfig(['language']);
39 ConfigurableLanguage::createFromLangcode('de')->save();
43 * Test creating, loading, updating and deleting aliases through PathItem.
45 public function testPathItem() {
47 /** @var \Drupal\Core\Path\AliasStorageInterface $alias_storage */
48 $alias_storage = \Drupal::service('path.alias_storage');
50 $node_storage = \Drupal::entityTypeManager()->getStorage('node');
52 $node = Node::create([
53 'title' => 'Testing create()',
55 'path' => ['alias' => '/foo'],
57 $this->assertFalse($node->get('path')->isEmpty());
58 $this->assertEquals('/foo', $node->get('path')->alias);
61 $this->assertFalse($node->get('path')->isEmpty());
62 $this->assertEquals('/foo', $node->get('path')->alias);
64 $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
65 $this->assertEquals('/foo', $stored_alias);
67 $node_storage->resetCache();
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());
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']);
82 $node_storage->resetCache();
83 $loaded_node = $node_storage->load($node->id());
84 $this->assertEquals('/foo', $loaded_node->path->alias);
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';
91 // Assert the alias on the English node, the German translation, and the
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);
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);
104 $loaded_node->get('path')->alias = '/bar';
105 $this->assertFalse($loaded_node->get('path')->isEmpty());
106 $this->assertEquals('/bar', $loaded_node->get('path')->alias);
108 $loaded_node->save();
109 $this->assertFalse($loaded_node->get('path')->isEmpty());
110 $this->assertEquals('/bar', $loaded_node->get('path')->alias);
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);
117 $loaded_node->get('path')->alias = '/bar';
118 $this->assertFalse($loaded_node->get('path')->isEmpty());
119 $this->assertEquals('/bar', $loaded_node->get('path')->alias);
121 $loaded_node->save();
122 $this->assertFalse($loaded_node->get('path')->isEmpty());
123 $this->assertEquals('/bar', $loaded_node->get('path')->alias);
125 $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
126 $this->assertEquals('/bar', $stored_alias);
128 $old_alias = $alias_storage->lookupPathSource('/foo', $node->language()->getId());
129 $this->assertFalse($old_alias);
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();
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);
145 $old_alias = $alias_storage->lookupPathSource('/bar', $node->language()->getId());
146 $this->assertFalse($old_alias);
148 $loaded_node->get('path')->alias = '';
149 $this->assertEquals('', $loaded_node->get('path')->alias);
151 $loaded_node->save();
153 $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());
154 $this->assertFalse($stored_alias);
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();
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);
167 $loaded_node->get('path')->alias = '/foobar';
168 $loaded_node->save();
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);
175 // Check that \Drupal\Core\Field\FieldItemList::equals() for the path field
177 $node = Node::create([
178 'title' => $this->randomString(),
180 'path' => ['alias' => '/foo'],
182 $second_node = Node::create([
183 'title' => $this->randomString(),
185 'path' => ['alias' => '/foo'],
187 $this->assertTrue($node->get('path')->equals($second_node->get('path')));
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')));
193 // Test the generateSampleValue() method.
194 $node = Node::create([
195 'title' => $this->randomString(),
197 'path' => ['alias' => '/foo'],
200 $path_field = $node->get('path');
201 $path_field->generateSampleItems();
203 $this->assertStringStartsWith('/', $node->get('path')->alias);