View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class ValidReferenceConstraintValidatorTest extends EntityKernelTestBase {
use EntityReferenceTestTrait;
use ContentTypeCreationTrait;
protected $typedData;
public static $modules = [
'field',
'node',
'user',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('user', [
'users_data',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installConfig('node');
$this->typedData = $this->container
->get('typed_data_manager');
$this
->createContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->createContentType([
'type' => 'page',
'name' => 'Basic page',
]);
}
public function testValidation() {
$entity = $this
->createUser();
$definition = BaseFieldDefinition::create('entity_reference')
->setSettings([
'target_type' => 'user',
]);
$typed_data = $this->typedData
->create($definition, [
'target_id' => $entity
->id(),
]);
$violations = $typed_data
->validate();
$this
->assertEquals(0, $violations
->count(), 'Validation passed for correct value.');
$typed_data = $this->typedData
->create($definition, [
'target_id' => NULL,
]);
$violations = $typed_data
->validate();
$this
->assertEquals(0, $violations
->count(), 'Validation passed for correct value.');
$typed_data = $this->typedData
->create($definition, [
'target_id' => $entity
->id(),
]);
$entity
->delete();
$violations = $typed_data
->validate();
$this
->assertGreaterThan(0, $violations
->count(), 'Validation failed for incorrect value.');
$violation = $violations[0];
$this
->assertEqual($violation
->getMessage(), t('The referenced entity (%type: %id) does not exist.', [
'%type' => 'user',
'%id' => $entity
->id(),
]), 'The message for invalid value is correct.');
$this
->assertEqual($violation
->getRoot(), $typed_data, 'Violation root is correct.');
}
public function testPreExistingItemsValidation() {
$role_with_access = Role::create([
'id' => 'role_with_access',
]);
$role_with_access
->grantPermission('access content');
$role_with_access
->grantPermission('bypass node access');
$role_with_access
->save();
$role_without_access = Role::create([
'id' => 'role_without_access',
]);
$role_without_access
->grantPermission('access content');
$role_without_access
->save();
$user_with_access = User::create([
'roles' => [
'role_with_access',
],
]);
$user_without_access = User::create([
'roles' => [
'role_without_access',
],
]);
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test', 'Field test', 'node', 'default', [
'target_bundles' => [
'article',
'page',
],
], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$published_node = Node::create([
'title' => 'Test published node',
'type' => 'article',
'status' => NodeInterface::PUBLISHED,
]);
$published_node
->save();
$unpublished_node = Node::create([
'title' => 'Test unpublished node',
'type' => 'article',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$unpublished_node
->save();
$different_bundle_node = Node::create([
'title' => 'Test page node',
'type' => 'page',
'status' => NodeInterface::PUBLISHED,
]);
$different_bundle_node
->save();
$deleted_node = Node::create([
'title' => 'Test deleted node',
'type' => 'article',
'status' => NodeInterface::PUBLISHED,
]);
$deleted_node
->save();
$referencing_entity = EntityTest::create([
'field_test' => [
[
'entity' => $published_node,
],
[
'entity' => $unpublished_node,
],
[
'entity' => $different_bundle_node,
],
[
'entity' => $deleted_node,
],
],
]);
$this->container
->get('account_switcher')
->switchTo($user_with_access);
$violations = $referencing_entity->field_test
->validate();
$this
->assertCount(0, $violations);
$this->container
->get('account_switcher')
->switchTo($user_without_access);
$violations = $referencing_entity->field_test
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals(t('This entity (%type: %id) cannot be referenced.', [
'%type' => 'node',
'%id' => $unpublished_node
->id(),
]), $violations[0]
->getMessage());
$referencing_entity
->save();
$this->container
->get('account_switcher')
->switchTo($user_with_access);
$violations = $referencing_entity->field_test
->validate();
$this
->assertCount(0, $violations);
$this->container
->get('account_switcher')
->switchTo($user_without_access);
$violations = $referencing_entity->field_test
->validate();
$this
->assertCount(0, $violations);
$referencing_entity->name->value = $this
->randomString();
$referencing_entity
->save();
$this
->assertEquals($published_node
->id(), $referencing_entity->field_test[0]->target_id);
$this
->assertEquals($unpublished_node
->id(), $referencing_entity->field_test[1]->target_id);
$this
->assertEquals($different_bundle_node
->id(), $referencing_entity->field_test[2]->target_id);
$this
->assertEquals($deleted_node
->id(), $referencing_entity->field_test[3]->target_id);
$violations = $referencing_entity->field_test
->validate();
$this
->assertCount(0, $violations);
$field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test');
$field
->setSetting('handler_settings', [
'target_bundles' => [
'article',
],
]);
$field
->save();
$referencing_entity = $this
->reloadEntity($referencing_entity);
$violations = $referencing_entity->field_test
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals(t('This entity (%type: %id) cannot be referenced.', [
'%type' => 'node',
'%id' => $different_bundle_node
->id(),
]), $violations[0]
->getMessage());
$deleted_node
->delete();
$violations = $referencing_entity->field_test
->validate();
$this
->assertCount(2, $violations);
$this
->assertEquals(t('This entity (%type: %id) cannot be referenced.', [
'%type' => 'node',
'%id' => $different_bundle_node
->id(),
]), $violations[0]
->getMessage());
$this
->assertEquals(t('The referenced entity (%type: %id) does not exist.', [
'%type' => 'node',
'%id' => $deleted_node
->id(),
]), $violations[1]
->getMessage());
}
}