Commit e5ec7496 authored by alexpott's avatar alexpott

Issue #2614408 by amateescu: Add test coverage for multiple invalid + mixed...

Issue #2614408 by amateescu: Add test coverage for multiple invalid + mixed existing/new entity reference validation
parent e53bf1fd
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTest; use Drupal\entity_test\Entity\EntityTest;
...@@ -76,6 +77,7 @@ protected function setUp() { ...@@ -76,6 +77,7 @@ protected function setUp() {
$this->installEntitySchema('file'); $this->installEntitySchema('file');
$this->installSchema('comment', ['comment_entity_statistics']); $this->installSchema('comment', ['comment_entity_statistics']);
$this->installSchema('node', ['node_access']);
$this->vocabulary = entity_create('taxonomy_vocabulary', array( $this->vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomMachineName(), 'name' => $this->randomMachineName(),
...@@ -100,7 +102,7 @@ protected function setUp() { ...@@ -100,7 +102,7 @@ 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_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_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_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_node', 'Test node entity reference', 'node', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_user', 'Test user entity reference', 'user'); $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_comment', 'Test comment entity reference', 'comment');
$this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_file', 'Test file entity reference', 'file'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_file', 'Test file entity reference', 'file');
...@@ -406,6 +408,64 @@ public function testAutocreateValidation() { ...@@ -406,6 +408,64 @@ public function testAutocreateValidation() {
$errors = $entity->validate(); $errors = $entity->validate();
$this->assertEqual(0, count($errors)); $this->assertEqual(0, count($errors));
// Test with a mix of valid and invalid nodes.
$unsaved_unpublished_node_title = $this->randomString();
$unsaved_unpublished_node = Node::create([
'title' => $unsaved_unpublished_node_title,
'type' => 'node',
'status' => NODE_NOT_PUBLISHED,
]);
$saved_unpublished_node_title = $this->randomString();
$saved_unpublished_node = Node::create([
'title' => $saved_unpublished_node_title,
'type' => 'node',
'status' => NODE_NOT_PUBLISHED,
]);
$saved_unpublished_node->save();
$saved_published_node_title = $this->randomString();
$saved_published_node = Node::create([
'title' => $saved_published_node_title,
'type' => 'node',
'status' => NODE_PUBLISHED,
]);
$saved_published_node->save();
$entity = EntityTest::create([
'field_test_node' => [
[
'entity' => $unsaved_unpublished_node,
],
[
'target_id' => $saved_unpublished_node->id(),
],
[
'target_id' => $saved_published_node->id(),
],
],
]);
$errors = $entity->validate();
$this->assertEqual(2, count($errors));
$this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $unsaved_unpublished_node_title]));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
$this->assertEqual($errors[1]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $saved_unpublished_node->id()]));
$this->assertEqual($errors[1]->getPropertyPath(), 'field_test_node.1.target_id');
// Publish one of the nodes and try again.
$saved_unpublished_node->setPublished(TRUE);
$saved_unpublished_node->save();
$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' => $unsaved_unpublished_node_title]));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
// Publish the last invalid node and try again.
$unsaved_unpublished_node->setPublished(TRUE);
$errors = $entity->validate();
$this->assertEqual(0, count($errors));
// Test with an unpublished and unsaved comment. // Test with an unpublished and unsaved comment.
$title = $this->randomString(); $title = $this->randomString();
$comment = Comment::create([ $comment = Comment::create([
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment