EntityTypedDataDefinitionTest.php in Zircon Profile 8
File
core/modules/system/src/Tests/Entity/EntityTypedDataDefinitionTest.php
View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
use Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\DataReferenceDefinition;
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
use Drupal\Core\TypedData\ListDataDefinitionInterface;
use Drupal\simpletest\KernelTestBase;
class EntityTypedDataDefinitionTest extends KernelTestBase {
protected $typedDataManager;
public static $modules = array(
'filter',
'text',
'node',
'user',
);
protected function setUp() {
parent::setup();
$this->typedDataManager = $this->container
->get('typed_data_manager');
}
public function testFields() {
$field_definition = BaseFieldDefinition::create('integer');
$this
->assertTrue($field_definition instanceof ListDataDefinitionInterface);
$this
->assertFalse($field_definition instanceof ComplexDataDefinitionInterface);
$field_item_definition = $field_definition
->getItemDefinition();
$this
->assertFalse($field_item_definition instanceof ListDataDefinitionInterface);
$this
->assertTrue($field_item_definition instanceof ComplexDataDefinitionInterface);
$this
->assertEqual(array_keys($field_item_definition
->getPropertyDefinitions()), array(
'value',
));
$this
->assertEqual($field_item_definition
->getPropertyDefinition('value')
->getDataType(), 'integer');
$this
->assertEqual($field_item_definition
->getMainPropertyName(), 'value');
$this
->assertNull($field_item_definition
->getPropertyDefinition('invalid'));
$this
->assertTrue($field_definition instanceof FieldDefinitionInterface);
$this
->assertEqual(array_keys($field_definition
->getPropertyDefinitions()), array(
'value',
));
$this
->assertEqual($field_definition
->getPropertyDefinition('value')
->getDataType(), 'integer');
$this
->assertEqual($field_definition
->getMainPropertyName(), 'value');
$this
->assertNull($field_definition
->getPropertyDefinition('invalid'));
$field_item = $this->typedDataManager
->createDataDefinition('field_item:integer');
$this
->assertFalse($field_item instanceof ListDataDefinitionInterface);
$this
->assertTrue($field_item instanceof ComplexDataDefinitionInterface);
$this
->assertEqual(serialize($field_item_definition), serialize($field_item));
$field_definition2 = $this->typedDataManager
->createListDataDefinition('field_item:integer');
$this
->assertTrue($field_definition2 instanceof ListDataDefinitionInterface);
$this
->assertFalse($field_definition2 instanceof ComplexDataDefinitionInterface);
$this
->assertEqual(serialize($field_definition), serialize($field_definition2));
}
public function testEntities() {
$entity_definition = EntityDataDefinition::create('node');
$this
->assertFalse($entity_definition instanceof ListDataDefinitionInterface);
$this
->assertTrue($entity_definition instanceof ComplexDataDefinitionInterface);
$field_definitions = $entity_definition
->getPropertyDefinitions();
$this
->assertEqual(serialize($field_definitions), serialize(\Drupal::entityManager()
->getBaseFieldDefinitions('node')));
$this
->assertEqual($entity_definition
->getPropertyDefinition('title')
->getItemDefinition()
->getDataType(), 'field_item:string');
$this
->assertNull($entity_definition
->getMainPropertyName());
$this
->assertNull($entity_definition
->getPropertyDefinition('invalid'));
$entity_definition2 = $this->typedDataManager
->createDataDefinition('entity:node');
$this
->assertFalse($entity_definition2 instanceof ListDataDefinitionInterface);
$this
->assertTrue($entity_definition2 instanceof ComplexDataDefinitionInterface);
$this
->assertEqual(serialize($entity_definition), serialize($entity_definition2));
$this
->assertEqual($this->typedDataManager
->createDataDefinition('entity'), EntityDataDefinition::create());
$this
->assertEqual($this->typedDataManager
->createDataDefinition('entity:node'), EntityDataDefinition::create('node'));
$entity_definition = EntityDataDefinition::create('node_type');
$this
->assertEqual(array(), $entity_definition
->getPropertyDefinitions());
}
public function testEntityReferences() {
$reference_definition = DataReferenceDefinition::create('entity');
$this
->assertTrue($reference_definition instanceof DataReferenceDefinitionInterface);
$this
->assertEqual($reference_definition
->getTargetDefinition()
->getDataType(), 'entity');
$this
->assertTrue($reference_definition
->getTargetDefinition() instanceof EntityDataDefinitionInterface);
$reference_definition2 = $this->typedDataManager
->createDataDefinition('entity_reference');
$this
->assertTrue($reference_definition2 instanceof DataReferenceDefinitionInterface);
$this
->assertEqual($reference_definition2, $reference_definition);
}
}