diff --git a/core/modules/path/tests/src/Kernel/PathItemTest.php b/core/modules/path/tests/src/Kernel/PathItemTest.php index 9a85a27958..7ddcdd0e92 100644 --- a/core/modules/path/tests/src/Kernel/PathItemTest.php +++ b/core/modules/path/tests/src/Kernel/PathItemTest.php @@ -49,16 +49,63 @@ public function testPathItem() { $node_storage = \Drupal::entityTypeManager()->getStorage('node'); + // Ensure that empty() and count() act the same no matter which order they + // are called in. + // First test with an entity that doesn't have the path field set. + $node = Node::create([ + 'title' => 'Testing no path', + 'type' => 'foo', + ]); + // Test using count() first. + $this->assertEquals(1, count($node->get('path'))); + $this->assertTrue($node->get('path')->isEmpty()); + + $node->save(); + $this->assertEquals(1, count($node->get('path'))); + $this->assertFalse($node->get('path')->isEmpty()); + + $node = Node::create([ + 'title' => 'Testing no path', + 'type' => 'foo', + ]); + // Test using isEmpty() first. + $this->assertTrue($node->get('path')->isEmpty()); + $this->assertEquals(1, count($node->get('path'))); + + + $node->save(); + $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals(1, count($node->get('path'))); + + // Test with an entity with path field set. + $node = Node::create([ + 'title' => 'Testing create()', + 'type' => 'foo', + 'path' => ['alias' => '/this-is-not-the-path-you-are-looking-for'], + ]); + // Test using count() first. + $this->assertEquals(1, count($node->get('path'))); + $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals('/this-is-not-the-path-you-are-looking-for', $node->get('path')->alias); + + $node->save(); + $this->assertEquals(1, count($node->get('path'))); + $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals('/this-is-not-the-path-you-are-looking-for', $node->get('path')->alias); + $node = Node::create([ 'title' => 'Testing create()', 'type' => 'foo', 'path' => ['alias' => '/foo'], ]); + // Test using isEmpty() first. $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals(1, count($node->get('path'))); $this->assertEquals('/foo', $node->get('path')->alias); $node->save(); $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals(1, count($node->get('path'))); $this->assertEquals('/foo', $node->get('path')->alias); $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId());