X-Git-Url: https://yaffs.net/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fpath%2Ftests%2Fsrc%2FKernel%2FPathItemTest.php;fp=web%2Fcore%2Fmodules%2Fpath%2Ftests%2Fsrc%2FKernel%2FPathItemTest.php;h=d7627c3690c8646ea8b5a55542de5f59b0db12c5;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=0000000000000000000000000000000000000000;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/path/tests/src/Kernel/PathItemTest.php b/web/core/modules/path/tests/src/Kernel/PathItemTest.php new file mode 100644 index 000000000..d7627c369 --- /dev/null +++ b/web/core/modules/path/tests/src/Kernel/PathItemTest.php @@ -0,0 +1,194 @@ +installEntitySchema('node'); + $this->installEntitySchema('user'); + + $this->installSchema('node', ['node_access']); + + $node_type = NodeType::create(['type' => 'foo']); + $node_type->save(); + + $this->installConfig(['language']); + ConfigurableLanguage::createFromLangcode('de')->save(); + } + + /** + * Test creating, loading, updating and deleting aliases through PathItem. + */ + public function testPathItem() { + + /** @var \Drupal\Core\Path\AliasStorageInterface $alias_storage */ + $alias_storage = \Drupal::service('path.alias_storage'); + + $node_storage = \Drupal::entityTypeManager()->getStorage('node'); + + $node = Node::create([ + 'title' => 'Testing create()', + 'type' => 'foo', + 'path' => ['alias' => '/foo'], + ]); + $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals('/foo', $node->get('path')->alias); + + $node->save(); + $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals('/foo', $node->get('path')->alias); + + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertEquals('/foo', $stored_alias); + + $node_storage->resetCache(); + + /** @var \Drupal\node\NodeInterface $loaded_node */ + $loaded_node = $node_storage->load($node->id()); + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/foo', $loaded_node->get('path')->alias); + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $this->assertEquals('/foo', $loaded_node->get('path')[0]->get('alias')->getValue()); + + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $values = $loaded_node->get('path')->getValue(); + $this->assertEquals('/foo', $values[0]['alias']); + + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $this->assertEquals('/foo', $loaded_node->path->alias); + + // Add a translation, verify it is being saved as expected. + $translation = $loaded_node->addTranslation('de', $loaded_node->toArray()); + $translation->get('path')->alias = '/furchtbar'; + $translation->save(); + + // Assert the alias on the English node, the German translation, and the + // stored aliases. + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $this->assertEquals('/foo', $loaded_node->path->alias); + $translation = $loaded_node->getTranslation('de'); + $this->assertEquals('/furchtbar', $translation->path->alias); + + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertEquals('/foo', $stored_alias); + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $translation->language()->getId()); + $this->assertEquals('/furchtbar', $stored_alias); + + $loaded_node->get('path')->alias = '/bar'; + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $loaded_node->save(); + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $loaded_node->get('path')->alias = '/bar'; + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $loaded_node->save(); + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertEquals('/bar', $stored_alias); + + $old_alias = $alias_storage->lookupPathSource('/foo', $node->language()->getId()); + $this->assertFalse($old_alias); + + // Reload the node to make sure that it is possible to set a value + // immediately after loading. + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $loaded_node->get('path')->alias = '/foobar'; + $loaded_node->save(); + + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/foobar', $loaded_node->get('path')->alias); + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertEquals('/foobar', $stored_alias); + + $old_alias = $alias_storage->lookupPathSource('/bar', $node->language()->getId()); + $this->assertFalse($old_alias); + + $loaded_node->get('path')->alias = ''; + $this->assertEquals('', $loaded_node->get('path')->alias); + + $loaded_node->save(); + + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertFalse($stored_alias); + + // Check that reading, updating and reading the computed alias again in the + // same request works without clearing any caches in between. + $loaded_node = $node_storage->load($node->id()); + $loaded_node->get('path')->alias = '/foo'; + $loaded_node->save(); + + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/foo', $loaded_node->get('path')->alias); + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertEquals('/foo', $stored_alias); + + $loaded_node->get('path')->alias = '/foobar'; + $loaded_node->save(); + + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/foobar', $loaded_node->get('path')->alias); + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertEquals('/foobar', $stored_alias); + + // Check that \Drupal\Core\Field\FieldItemList::equals() for the path field + // type. + $node = Node::create([ + 'title' => $this->randomString(), + 'type' => 'foo', + 'path' => ['alias' => '/foo'], + ]); + $second_node = Node::create([ + 'title' => $this->randomString(), + 'type' => 'foo', + 'path' => ['alias' => '/foo'], + ]); + $this->assertTrue($node->get('path')->equals($second_node->get('path'))); + + // Change the alias for the second node to a different one and try again. + $second_node->get('path')->alias = '/foobar'; + $this->assertFalse($node->get('path')->equals($second_node->get('path'))); + } + +}