Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / src / Kernel / EntityReference / EntityReferenceItemTest.php
index c0e58e04e9e5917a20001014862f9b2d032e7990..c35d1607bfdcfb108ceaa9a725d745cc42b4281f 100644 (file)
@@ -3,8 +3,8 @@
 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;
@@ -14,7 +14,7 @@ use Drupal\entity_test\Entity\EntityTest;
 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;
@@ -23,6 +23,7 @@ use Drupal\node\Entity\Node;
 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.
@@ -78,7 +79,7 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
 
     $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();
@@ -90,6 +91,14 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
     ]);
     $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(),
     ]);
@@ -103,6 +112,7 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
     $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');
   }
 
   /**
@@ -212,6 +222,19 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
     $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.
    */
@@ -259,7 +282,7 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
     // 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();
@@ -330,13 +353,13 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
    * 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();
@@ -428,7 +451,7 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
     $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));
 
@@ -478,7 +501,7 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
     $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));
@@ -486,7 +509,7 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
     $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));
 
@@ -510,7 +533,7 @@ class EntityReferenceItemTest extends FieldKernelTestBase {
     $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));