Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Entity / TypedData / EntityAdapterUnitTest.php
index c64e507e2b40689252158b1f1636eb21b881eab7..8e909d24bd4fee4200db019bbeb860e35faa6fe6 100644 (file)
@@ -3,6 +3,9 @@
 namespace Drupal\Tests\Core\Entity\TypedData;
 
 use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Core\Entity\EntityFieldManagerInterface;
+use Drupal\Core\Entity\EntityManager;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\Core\Language\LanguageInterface;
@@ -26,19 +29,33 @@ class EntityAdapterUnitTest extends UnitTestCase {
   protected $bundle;
 
   /**
-   * The entity used for testing.
+   * The content entity used for testing.
    *
    * @var \Drupal\Core\Entity\ContentEntityBase|\PHPUnit_Framework_MockObject_MockObject
    */
   protected $entity;
 
   /**
-   * The entity adapter under test.
+   * The config entity used for testing.
+   *
+   * @var \Drupal\Core\Entity\ConfigtEntityBase|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $configEntity;
+
+  /**
+   * The content entity adapter under test.
    *
    * @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
    */
   protected $entityAdapter;
 
+  /**
+   * The config entity adapter under test.
+   *
+   * @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
+   */
+  protected $configEntityAdapter;
+
   /**
    * The entity type used for testing.
    *
@@ -53,6 +70,19 @@ class EntityAdapterUnitTest extends UnitTestCase {
    */
   protected $entityManager;
 
+  /**
+   * The entity type manager used for testing.
+   *
+   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $entityTypeManager;
+
+  /**
+   *
+   * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $entityFieldManager;
+
   /**
    * The type ID of the entity under test.
    *
@@ -130,8 +160,10 @@ class EntityAdapterUnitTest extends UnitTestCase {
         'uuid' => 'uuid',
     ]));
 
-    $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
-    $this->entityManager->expects($this->any())
+    $this->entityManager = new EntityManager();
+
+    $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
+    $this->entityTypeManager->expects($this->any())
       ->method('getDefinition')
       ->with($this->entityTypeId)
       ->will($this->returnValue($this->entityType));
@@ -183,26 +215,37 @@ class EntityAdapterUnitTest extends UnitTestCase {
       ->method('createFieldItemList')
       ->willReturn($this->fieldItemList);
 
+    $this->entityFieldManager = $this->getMockForAbstractClass(EntityFieldManagerInterface::class);
+
     $container = new ContainerBuilder();
     $container->set('entity.manager', $this->entityManager);
+    $container->set('entity_type.manager', $this->entityTypeManager);
+    $container->set('entity_field.manager', $this->entityFieldManager);
     $container->set('uuid', $this->uuid);
     $container->set('typed_data_manager', $this->typedDataManager);
     $container->set('language_manager', $this->languageManager);
     $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
+    // Inject the container into entity.manager so it can defer to
+    // entity_type.manager and other services.
+    $this->entityManager->setContainer($container);
     \Drupal::setContainer($container);
 
     $this->fieldDefinitions = [
       'id' => BaseFieldDefinition::create('integer'),
       'revision_id' => BaseFieldDefinition::create('integer'),
     ];
-
-    $this->entityManager->expects($this->any())
+    $this->entityFieldManager->expects($this->any())
       ->method('getFieldDefinitions')
       ->with($this->entityTypeId, $this->bundle)
       ->will($this->returnValue($this->fieldDefinitions));
+
     $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle]);
 
     $this->entityAdapter = EntityAdapter::createFromEntity($this->entity);
+
+    $this->configEntity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', [$values, $this->entityTypeId, $this->bundle]);
+
+    $this->configEntityAdapter = EntityAdapter::createFromEntity($this->configEntity);
   }
 
   /**
@@ -403,6 +446,7 @@ class EntityAdapterUnitTest extends UnitTestCase {
    * @covers ::getIterator
    */
   public function testGetIterator() {
+    // Content entity test.
     $iterator = $this->entityAdapter->getIterator();
     $fields = iterator_to_array($iterator);
     $this->assertArrayHasKey('id', $fields);
@@ -411,6 +455,11 @@ class EntityAdapterUnitTest extends UnitTestCase {
 
     $this->entityAdapter->setValue(NULL);
     $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator());
+
+    // Config entity test.
+    $iterator = $this->configEntityAdapter->getIterator();
+    $this->configEntityAdapter->setValue(NULL);
+    $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator());
   }
 
 }