diff --git a/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionWithAutocreateInterface.php b/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionWithAutocreateInterface.php index f81d5fa..7010b91 100644 --- a/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionWithAutocreateInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionWithAutocreateInterface.php @@ -37,8 +37,8 @@ public function createNewEntity($entity_type_id, $bundle, $label, $uid); * Validates which newly created entities can be referenced. * * This method should replicate the logic implemented by - * validateReferenceableEntities(), but applied to newly created entities that - * have not been saved yet. + * \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface::validateReferenceableEntities(), + * but applied to newly created entities that have not been saved yet. * * @param \Drupal\Core\Entity\EntityInterface[] $entities * An array of entities to check. diff --git a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php index 4f207e6..b053170 100644 --- a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php +++ b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php @@ -59,8 +59,9 @@ public function validateReferenceableNewEntities(array $entities) { $entities = parent::validateReferenceableNewEntities($entities); // Mirror the conditions checked in buildEntityQuery(). if (!$this->currentUser->hasPermission('administer comments')) { - $entities = array_filter($entities, function ($entity) { - return $entity->status->value === CommentInterface::PUBLISHED; + $entities = array_filter($entities, function ($comment) { + /** @var \Drupal\comment\CommentInterface $comment */ + return $comment->isPublished(); }); } return $entities; diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php index f8a9348..f629ce8 100644 --- a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php +++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php @@ -7,6 +7,8 @@ namespace Drupal\field\Tests\EntityReference; +use Drupal\comment\Entity\Comment; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\Unicode; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemInterface; @@ -17,8 +19,11 @@ use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Tests\FieldUnitTestBase; +use Drupal\file\Entity\File; +use Drupal\node\Entity\Node; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; +use Drupal\user\Entity\User; /** @@ -35,7 +40,7 @@ class EntityReferenceItemTest extends FieldUnitTestBase { * * @var array */ - public static $modules = ['taxonomy', 'text', 'filter', 'views', 'field']; + public static $modules = ['node', 'comment', 'file', 'taxonomy', 'text', 'filter', 'views', 'field']; /** * The taxonomy vocabulary to test with. @@ -66,6 +71,11 @@ protected function setUp() { $this->installEntitySchema('entity_test_string_id'); $this->installEntitySchema('taxonomy_term'); + $this->installEntitySchema('node'); + $this->installEntitySchema('comment'); + $this->installEntitySchema('file'); + + $this->installSchema('comment', ['comment_entity_statistics']); $this->vocabulary = entity_create('taxonomy_vocabulary', array( 'name' => $this->randomMachineName(), @@ -90,6 +100,10 @@ protected function setUp() { $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_entity_test_string_id', 'Test content entity reference with string ID', 'entity_test_string_id'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary'); + $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_node', 'Test node entity reference', 'node'); + $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_user', 'Test user entity reference', 'user'); + $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_comment', 'Test comment entity reference', 'comment'); + $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_file', 'Test file entity reference', 'file'); } /** @@ -333,9 +347,9 @@ public function testSelectionHandlerSettings() { } /** - * Tests validation constraint. + * Tests ValidReferenceConstraint with newly created and unsaved entities. */ - public function testValidation() { + public function testAutocreateValidation() { // The term entity is unsaved here. $term = Term::create(array( 'name' => $this->randomMachineName(), @@ -367,6 +381,100 @@ public function testValidation() { $entity->save(); $errors = $entity->validate(); $this->assertEqual(0, count($errors)); + + // Test with an unpublished and unsaved node. + $title = $this->randomString(); + $node = Node::create([ + 'title' => $title, + 'type' => 'node', + 'status' => NODE_NOT_PUBLISHED, + ]); + + $entity = EntityTest::create([ + 'field_test_node' => [ + 'entity' => $node, + ], + ]); + + $errors = $entity->validate(); + $this->assertEqual(1, count($errors)); + $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $title])); + $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity'); + + // Publish the node and try again. + $node->setPublished(TRUE); + $errors = $entity->validate(); + $this->assertEqual(0, count($errors)); + + // Test with an unpublished and unsaved comment. + $title = $this->randomString(); + $comment = Comment::create([ + 'subject' => $title, + 'comment_type' => 'comment', + 'status' => 0, + ]); + + $entity = EntityTest::create([ + 'field_test_comment' => [ + 'entity' => $comment, + ], + ]); + + $errors = $entity->validate(); + $this->assertEqual(1, count($errors)); + $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'comment', '%label' => $title])); + $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_comment.0.entity'); + + // Publish the comment and try again. + $comment->setPublished(TRUE); + $errors = $entity->validate(); + $this->assertEqual(0, count($errors)); + + // Test with an inactive and unsaved user. + $name = $this->randomString(); + $user = User::create([ + 'name' => $name, + 'status' => 0, + ]); + + $entity = EntityTest::create([ + 'field_test_user' => [ + 'entity' => $user, + ], + ]); + + $errors = $entity->validate(); + $this->assertEqual(1, count($errors)); + $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'user', '%label' => $name])); + $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_user.0.entity'); + + // Activate the user and try again. + $user->activate(); + $errors = $entity->validate(); + $this->assertEqual(0, count($errors)); + + // Test with a temporary and unsaved file. + $filename = $this->randomMachineName() . '.txt'; + $file = File::create([ + 'filename' => $filename, + 'status' => 0, + ]); + + $entity = EntityTest::create([ + 'field_test_file' => [ + 'entity' => $file, + ], + ]); + + $errors = $entity->validate(); + $this->assertEqual(1, count($errors)); + $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'file', '%label' => $filename])); + $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_file.0.entity'); + + // Set the file as permanent and try again. + $file->setPermanent(); + $errors = $entity->validate(); + $this->assertEqual(0, count($errors)); } } diff --git a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php index 0055a4a..a42fa60 100644 --- a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php +++ b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php @@ -68,8 +68,9 @@ public function validateReferenceableNewEntities(array $entities) { $entities = parent::validateReferenceableNewEntities($entities); // Mirror the conditions checked in buildEntityQuery(). if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) { - $entities = array_filter($entities, function ($entity) { - return $entity->status->value === NODE_PUBLISHED; + $entities = array_filter($entities, function ($node) { + /** @var \Drupal\node\NodeInterface $node */ + return $node->isPublished(); }); } return $entities; diff --git a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php index 318a36e..3964f29 100644 --- a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php +++ b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php @@ -190,13 +190,15 @@ public function validateReferenceableNewEntities(array $entities) { $entities = parent::validateReferenceableNewEntities($entities); // Mirror the conditions checked in buildEntityQuery(). if (!empty($this->configuration['handler_settings']['filter']['role'])) { - $entities = array_filter($entities, function ($entity) { - return !empty(array_intersect($entity->getRoles(), $this->configuration['handler_settings']['filter']['role'])); + $entities = array_filter($entities, function ($user) { + /** @var \Drupal\user\UserInterface $user */ + return !empty(array_intersect($user->getRoles(), $this->configuration['handler_settings']['filter']['role'])); }); } if (!$this->currentUser->hasPermission('administer users')) { - $entities = array_filter($entities, function ($entity) { - return $entity->status->value === 1; + $entities = array_filter($entities, function ($user) { + /** @var \Drupal\user\UserInterface $user */ + return $user->isActive(); }); } return $entities;