View source
<?php
namespace Drupal\Tests\Core\Entity\TypedData;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Language\Language;
class EntityAdapterUnitTest extends UnitTestCase {
protected $bundle;
protected $entity;
protected $entityAdapter;
protected $entityType;
protected $entityTypeManager;
protected $entityFieldManager;
protected $entityTypeId;
protected $typedDataManager;
protected $fieldItemList;
protected $fieldTypePluginManager;
protected $languageManager;
protected $uuid;
protected $id;
protected $fieldDefinitions;
protected function setUp() {
$this->id = 1;
$values = [
'id' => $this->id,
'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
'defaultLangcode' => [
LanguageInterface::LANGCODE_DEFAULT => 'en',
],
];
$this->entityTypeId = $this
->randomMachineName();
$this->bundle = $this
->randomMachineName();
$this->entityType = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$this->entityType
->expects($this
->any())
->method('getKeys')
->will($this
->returnValue([
'id' => 'id',
'uuid' => 'uuid',
]));
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->entityTypeManager
->expects($this
->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this
->returnValue($this->entityType));
$this->uuid = $this
->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
$this->typedDataManager = $this
->createMock(TypedDataManagerInterface::class);
$this->typedDataManager
->expects($this
->any())
->method('getDefinition')
->with('entity')
->will($this
->returnValue([
'class' => '\\Drupal\\Core\\Entity\\Plugin\\DataType\\EntityAdapter',
]));
$this->typedDataManager
->expects($this
->any())
->method('getDefaultConstraints')
->willReturn([]);
$validation_constraint_manager = $this
->getMockBuilder('\\Drupal\\Core\\Validation\\ConstraintManager')
->disableOriginalConstructor()
->getMock();
$validation_constraint_manager
->expects($this
->any())
->method('create')
->willReturn([]);
$this->typedDataManager
->expects($this
->any())
->method('getValidationConstraintManager')
->willReturn($validation_constraint_manager);
$not_specified = new Language([
'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'locked' => TRUE,
]);
$this->languageManager = $this
->createMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
$this->languageManager
->expects($this
->any())
->method('getLanguages')
->will($this
->returnValue([
LanguageInterface::LANGCODE_NOT_SPECIFIED => $not_specified,
]));
$this->languageManager
->expects($this
->any())
->method('getLanguage')
->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
->will($this
->returnValue($not_specified));
$this->fieldTypePluginManager = $this
->getMockBuilder('\\Drupal\\Core\\Field\\FieldTypePluginManager')
->disableOriginalConstructor()
->getMock();
$this->fieldTypePluginManager
->expects($this
->any())
->method('getDefaultStorageSettings')
->will($this
->returnValue([]));
$this->fieldTypePluginManager
->expects($this
->any())
->method('getDefaultFieldSettings')
->will($this
->returnValue([]));
$this->fieldItemList = $this
->createMock('\\Drupal\\Core\\Field\\FieldItemListInterface');
$this->fieldTypePluginManager
->expects($this
->any())
->method('createFieldItemList')
->willReturn($this->fieldItemList);
$this->entityFieldManager = $this
->getMockForAbstractClass(EntityFieldManagerInterface::class);
$container = new ContainerBuilder();
$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);
\Drupal::setContainer($container);
$this->fieldDefinitions = [
'id' => BaseFieldDefinition::create('integer'),
'revision_id' => BaseFieldDefinition::create('integer'),
];
$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);
}
public function testGetConstraints() {
$this
->assertIsArray($this->entityAdapter
->getConstraints());
}
public function testGetName() {
$this
->assertNull($this->entityAdapter
->getName());
}
public function testGetRoot() {
$this
->assertSame(spl_object_hash($this->entityAdapter), spl_object_hash($this->entityAdapter
->getRoot()));
}
public function testGetPropertyPath() {
$this
->assertSame('', $this->entityAdapter
->getPropertyPath());
}
public function testGetParent() {
$this
->assertNull($this->entityAdapter
->getParent());
}
public function testSetContext() {
$name = $this
->randomMachineName();
$parent = $this
->createMock('\\Drupal\\Core\\TypedData\\TraversableTypedDataInterface');
$this
->assertNull($this->entityAdapter
->setContext($name, $parent));
$this
->assertEquals($name, $this->entityAdapter
->getName());
$this
->assertEquals($parent, $this->entityAdapter
->getParent());
}
public function testGetValue() {
$this
->assertEquals($this->entity, $this->entityAdapter
->getValue());
}
public function testGetEntity() {
$this
->assertSame($this->entity, $this->entityAdapter
->getEntity());
}
public function testSetValue() {
$this->entityAdapter
->setValue(NULL);
$this
->assertNull($this->entityAdapter
->getValue());
}
public function testGet() {
$this
->assertInstanceOf('\\Drupal\\Core\\Field\\FieldItemListInterface', $this->entityAdapter
->get('id'));
}
public function testGetInvalidField() {
$this
->expectException(\InvalidArgumentException::class);
$this->entityAdapter
->get('invalid');
}
public function testGetWithoutData() {
$this->entityAdapter
->setValue(NULL);
$this
->expectException(MissingDataException::class);
$this->entityAdapter
->get('id');
}
public function testSet() {
$id_items = [
[
'value' => $this->id + 1,
],
];
$this->fieldItemList
->expects($this
->once())
->method('setValue')
->with($id_items);
$this->entityAdapter
->set('id', $id_items);
}
public function testSetWithoutData() {
$this->entityAdapter
->setValue(NULL);
$id_items = [
[
'value' => $this->id + 1,
],
];
$this
->expectException(MissingDataException::class);
$this->entityAdapter
->set('id', $id_items);
}
public function testGetProperties() {
$fields = $this->entityAdapter
->getProperties();
$this
->assertInstanceOf('Drupal\\Core\\Field\\FieldItemListInterface', $fields['id']);
$this
->assertInstanceOf('Drupal\\Core\\Field\\FieldItemListInterface', $fields['revision_id']);
}
public function testToArray() {
$array = $this->entityAdapter
->toArray();
$this
->assertArrayHasKey('id', $array);
$this
->assertArrayHasKey('revision_id', $array);
$this
->assertCount(2, $array);
}
public function testToArrayWithoutData() {
$this->entityAdapter
->setValue(NULL);
$this
->expectException(MissingDataException::class);
$this->entityAdapter
->toArray();
}
public function testIsEmpty() {
$this
->assertFalse($this->entityAdapter
->isEmpty());
$this->entityAdapter
->setValue(NULL);
$this
->assertTrue($this->entityAdapter
->isEmpty());
}
public function testOnChange() {
$entity = $this
->createMock('\\Drupal\\Core\\Entity\\ContentEntityInterface');
$entity
->expects($this
->once())
->method('onChange')
->with('foo')
->willReturn(NULL);
$this->entityAdapter
->setValue($entity);
$this->entityAdapter
->onChange('foo');
}
public function testGetDataDefinition() {
$definition = $this->entityAdapter
->getDataDefinition();
$this
->assertInstanceOf('\\Drupal\\Core\\Entity\\TypedData\\EntityDataDefinitionInterface', $definition);
$this
->assertEquals($definition
->getEntityTypeId(), $this->entityTypeId);
$this
->assertEquals($definition
->getBundles(), [
$this->bundle,
]);
}
public function testGetString() {
$entity = $this
->createMock('\\Drupal\\Core\\Entity\\ContentEntityInterface');
$entity
->expects($this
->once())
->method('label')
->willReturn('foo');
$this->entityAdapter
->setValue($entity);
$this
->assertEquals('foo', $this->entityAdapter
->getString());
$this->entityAdapter
->setValue(NULL);
$this
->assertEquals('', $this->entityAdapter
->getString());
}
public function testApplyDefaultValue() {
$this->fieldItemList
->expects($this
->exactly(2))
->method('applyDefaultValue');
$this->entityAdapter
->applyDefaultValue();
}
public function testGetIterator() {
$iterator = $this->entityAdapter
->getIterator();
$fields = iterator_to_array($iterator);
$this
->assertArrayHasKey('id', $fields);
$this
->assertArrayHasKey('revision_id', $fields);
$this
->assertCount(2, $fields);
$this->entityAdapter
->setValue(NULL);
$this
->assertEquals(new \ArrayIterator([]), $this->entityAdapter
->getIterator());
}
}