namespace Drupal\Tests\field\Kernel\EntityReference;
use Drupal\comment\Entity\Comment;
+use Drupal\comment\Entity\CommentType;
use Drupal\Component\Render\FormattableMarkup;
-use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTestStringId;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
-use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
+use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
+use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
/**
* Tests the new entity API for the entity reference field type.
$this->vocabulary = Vocabulary::create([
'name' => $this->randomMachineName(),
- 'vid' => Unicode::strtolower($this->randomMachineName()),
+ 'vid' => mb_strtolower($this->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this->vocabulary->save();
]);
$this->term->save();
+ NodeType::create([
+ 'type' => $this->randomMachineName(),
+ ])->save();
+ CommentType::create([
+ 'id' => $this->randomMachineName(),
+ 'target_entity_type_id' => 'node',
+ ])->save();
+
$this->entityStringId = EntityTestStringId::create([
'id' => $this->randomMachineName(),
]);
$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');
+ $this->createEntityReferenceField('entity_test_string_id', 'entity_test_string_id', 'field_test_entity_test', 'Test content entity reference with string ID', 'entity_test');
}
/**
$this->entityValidateAndSave($entity);
}
+ /**
+ * Tests the ::generateSampleValue() method when it has a circular reference.
+ */
+ public function testGenerateSampleValueCircularReference() {
+ // Delete the existing entity.
+ $this->entityStringId->delete();
+
+ $entity_storage = \Drupal::entityTypeManager()->getStorage('entity_test');
+ $entity = $entity_storage->createWithSampleValues('entity_test');
+ $this->assertInstanceOf(EntityTestStringId::class, $entity->field_test_entity_test_string_id->entity);
+ $this->assertInstanceOf(EntityTest::class, $entity->field_test_entity_test_string_id->entity->field_test_entity_test->entity);
+ }
+
/**
* Tests referencing content entities with string IDs.
*/
// Make sure the computed term reflects updates to the term id.
$vocabulary2 = $vocabulary = Vocabulary::create([
'name' => $this->randomMachineName(),
- 'vid' => Unicode::strtolower($this->randomMachineName()),
+ 'vid' => mb_strtolower($this->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$vocabulary2->save();
* Tests that the 'handler' field setting stores the proper plugin ID.
*/
public function testSelectionHandlerSettings() {
- $field_name = Unicode::strtolower($this->randomMachineName());
+ $field_name = mb_strtolower($this->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'entity_reference',
'settings' => [
- 'target_type' => 'entity_test'
+ 'target_type' => 'entity_test',
],
]);
$field_storage->save();
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
// Publish the node and try again.
- $node->setPublished(TRUE);
+ $node->setPublished();
$errors = $entity->validate();
$this->assertEqual(0, count($errors));
$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->setPublished();
$saved_unpublished_node->save();
$errors = $entity->validate();
$this->assertEqual(1, count($errors));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
// Publish the last invalid node and try again.
- $unsaved_unpublished_node->setPublished(TRUE);
+ $unsaved_unpublished_node->setPublished();
$errors = $entity->validate();
$this->assertEqual(0, count($errors));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_comment.0.entity');
// Publish the comment and try again.
- $comment->setPublished(TRUE);
+ $comment->setPublished();
$errors = $entity->validate();
$this->assertEqual(0, count($errors));