Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityTypedDataDefinitionTest.php
index 5e755d95842e30f04dbfb62b014fad7404d6d461..ece711b1b03d42bb0bcc70d640361819748f2706 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace Drupal\KernelTests\Core\Entity;
 
+use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Entity\TypedData\EntityDataDefinition;
 use Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface;
 use Drupal\Core\Field\BaseFieldDefinition;
@@ -11,6 +13,7 @@ use Drupal\Core\TypedData\DataReferenceDefinition;
 use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
 use Drupal\Core\TypedData\ListDataDefinitionInterface;
 use Drupal\KernelTests\KernelTestBase;
+use Drupal\node\Entity\NodeType;
 
 /**
  * Tests deriving metadata of entity and field data types.
@@ -31,10 +34,16 @@ class EntityTypedDataDefinitionTest extends KernelTestBase {
    *
    * @var array
    */
-  public static $modules = ['filter', 'text', 'node', 'user'];
+  public static $modules = ['system', 'filter', 'text', 'node', 'user'];
 
   protected function setUp() {
     parent::setup();
+
+    NodeType::create([
+      'type' => 'article',
+      'name' => 'Article',
+    ])->save();
+
     $this->typedDataManager = $this->container->get('typed_data_manager');
   }
 
@@ -82,10 +91,15 @@ class EntityTypedDataDefinitionTest extends KernelTestBase {
    */
   public function testEntities() {
     $entity_definition = EntityDataDefinition::create('node');
+    $bundle_definition = EntityDataDefinition::create('node', 'article');
     // Entities are complex data.
     $this->assertFalse($entity_definition instanceof ListDataDefinitionInterface);
     $this->assertTrue($entity_definition instanceof ComplexDataDefinitionInterface);
 
+    // Entity definitions should inherit their labels from the entity type.
+    $this->assertEquals('Content', $entity_definition->getLabel());
+    $this->assertEquals('Article', $bundle_definition->getLabel());
+
     $field_definitions = $entity_definition->getPropertyDefinitions();
     // Comparison should ignore the internal static cache, so compare the
     // serialized objects instead.
@@ -126,4 +140,36 @@ class EntityTypedDataDefinitionTest extends KernelTestBase {
     $this->assertEqual(serialize($reference_definition2), serialize($reference_definition));
   }
 
+  /**
+   * Tests that an entity annotation can mark the data definition as internal.
+   *
+   * @dataProvider entityDefinitionIsInternalProvider
+   */
+  public function testEntityDefinitionIsInternal($internal, $expected) {
+    $entity_type_id = $this->randomMachineName();
+
+    $entity_type = $this->prophesize(EntityTypeInterface::class);
+    $entity_type->getLabel()->willReturn($this->randomString());
+    $entity_type->getConstraints()->willReturn([]);
+    $entity_type->isInternal()->willReturn($internal);
+
+    $entity_manager = $this->prophesize(EntityManagerInterface::class);
+    $entity_manager->getDefinitions()->willReturn([$entity_type_id => $entity_type->reveal()]);
+    $this->container->set('entity.manager', $entity_manager->reveal());
+
+    $entity_data_definition = EntityDataDefinition::create($entity_type_id);
+    $this->assertSame($expected, $entity_data_definition->isInternal());
+  }
+
+  /**
+   * Provides test cases for testEntityDefinitionIsInternal.
+   */
+  public function entityDefinitionIsInternalProvider() {
+    return [
+      'internal' => [TRUE, TRUE],
+      'external' => [FALSE, FALSE],
+      'undefined' => [NULL, FALSE],
+    ];
+  }
+
 }