EntityHierarchyValidationTest.php in Entity Reference Hierarchy 8.2
File
tests/src/Kernel/EntityHierarchyValidationTest.php
View source
<?php
namespace Drupal\Tests\entity_hierarchy\Kernel;
use Drupal\Core\Entity\EntityInterface;
class EntityHierarchyValidationTest extends EntityHierarchyKernelTestBase {
public function testValidation() {
$this
->createUser();
$child = $this
->createTestEntity($this->parent
->id());
$this->parent->{self::FIELD_NAME}->target_id = $child
->id();
$this
->doTestViolations($child);
$this->parent->{self::FIELD_NAME}->target_id = $this->parent
->id();
$this
->doTestViolations($this->parent);
$grandchild = $this
->createTestEntity($child
->id());
$this->parent->{self::FIELD_NAME}->target_id = $grandchild
->id();
$this
->doTestViolations($grandchild);
}
protected function doTestViolations(EntityInterface $referencedEntity) {
$violations = $this->parent
->validate();
$this
->assertCount(1, $violations);
$violation = $violations[0];
$this
->assertEquals(sprintf('This entity (entity_test: %s) cannot be referenced as it is either a child or the same entity.', $referencedEntity
->id()), strip_tags($violation
->getMessage()));
$this
->assertEquals(self::FIELD_NAME . '.0.target_id', $violation
->getPropertyPath());
}
}