3 namespace Drupal\Tests\Core\Entity\TypedData;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
7 use Drupal\Core\Field\BaseFieldDefinition;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\Core\TypedData\Exception\MissingDataException;
10 use Drupal\Core\TypedData\TypedDataManagerInterface;
11 use Drupal\Tests\UnitTestCase;
12 use Drupal\Core\Language\Language;
15 * @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
19 class EntityAdapterUnitTest extends UnitTestCase {
22 * The bundle used for testing.
29 * The entity used for testing.
31 * @var \Drupal\Core\Entity\ContentEntityBase|\PHPUnit_Framework_MockObject_MockObject
36 * The entity adapter under test.
38 * @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
40 protected $entityAdapter;
43 * The entity type used for testing.
45 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
47 protected $entityType;
50 * The entity manager used for testing.
52 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
54 protected $entityManager;
57 * The type ID of the entity under test.
61 protected $entityTypeId;
64 * The typed data manager used for testing.
66 * @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit_Framework_MockObject_MockObject
68 protected $typedDataManager;
71 * The field item list returned by the typed data manager.
73 * @var \Drupal\Core\Field\FieldItemListInterface|\PHPUnit_Framework_MockObject_MockObject
75 protected $fieldItemList;
78 * The field type manager used for testing.
80 * @var \Drupal\Core\Field\FieldTypePluginManager|\PHPUnit_Framework_MockObject_MockObject
82 protected $fieldTypePluginManager;
85 * The language manager.
87 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
89 protected $languageManager;
92 * The UUID generator used for testing.
94 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
108 * @var \Drupal\Core\Field\BaseFieldDefinition[]
110 protected $fieldDefinitions;
115 protected function setUp() {
119 'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
120 'defaultLangcode' => [LanguageInterface::LANGCODE_DEFAULT => 'en'],
122 $this->entityTypeId = $this->randomMachineName();
123 $this->bundle = $this->randomMachineName();
125 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
126 $this->entityType->expects($this->any())
128 ->will($this->returnValue([
133 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
134 $this->entityManager->expects($this->any())
135 ->method('getDefinition')
136 ->with($this->entityTypeId)
137 ->will($this->returnValue($this->entityType));
139 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
141 $this->typedDataManager = $this->getMock(TypedDataManagerInterface::class);
142 $this->typedDataManager->expects($this->any())
143 ->method('getDefinition')
145 ->will($this->returnValue(['class' => '\Drupal\Core\Entity\Plugin\DataType\EntityAdapter']));
146 $this->typedDataManager->expects($this->any())
147 ->method('getDefaultConstraints')
150 $validation_constraint_manager = $this->getMockBuilder('\Drupal\Core\Validation\ConstraintManager')
151 ->disableOriginalConstructor()
153 $validation_constraint_manager->expects($this->any())
156 $this->typedDataManager->expects($this->any())
157 ->method('getValidationConstraintManager')
158 ->willReturn($validation_constraint_manager);
160 $not_specified = new Language(['id' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'locked' => TRUE]);
161 $this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface');
162 $this->languageManager->expects($this->any())
163 ->method('getLanguages')
164 ->will($this->returnValue([LanguageInterface::LANGCODE_NOT_SPECIFIED => $not_specified]));
166 $this->languageManager->expects($this->any())
167 ->method('getLanguage')
168 ->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
169 ->will($this->returnValue($not_specified));
171 $this->fieldTypePluginManager = $this->getMockBuilder('\Drupal\Core\Field\FieldTypePluginManager')
172 ->disableOriginalConstructor()
174 $this->fieldTypePluginManager->expects($this->any())
175 ->method('getDefaultStorageSettings')
176 ->will($this->returnValue([]));
177 $this->fieldTypePluginManager->expects($this->any())
178 ->method('getDefaultFieldSettings')
179 ->will($this->returnValue([]));
181 $this->fieldItemList = $this->getMock('\Drupal\Core\Field\FieldItemListInterface');
182 $this->fieldTypePluginManager->expects($this->any())
183 ->method('createFieldItemList')
184 ->willReturn($this->fieldItemList);
186 $container = new ContainerBuilder();
187 $container->set('entity.manager', $this->entityManager);
188 $container->set('uuid', $this->uuid);
189 $container->set('typed_data_manager', $this->typedDataManager);
190 $container->set('language_manager', $this->languageManager);
191 $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
192 \Drupal::setContainer($container);
194 $this->fieldDefinitions = [
195 'id' => BaseFieldDefinition::create('integer'),
196 'revision_id' => BaseFieldDefinition::create('integer'),
199 $this->entityManager->expects($this->any())
200 ->method('getFieldDefinitions')
201 ->with($this->entityTypeId, $this->bundle)
202 ->will($this->returnValue($this->fieldDefinitions));
203 $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle]);
205 $this->entityAdapter = EntityAdapter::createFromEntity($this->entity);
209 * @covers ::getConstraints
211 public function testGetConstraints() {
212 $this->assertInternalType('array', $this->entityAdapter->getConstraints());
218 public function testGetName() {
219 $this->assertNull($this->entityAdapter->getName());
225 public function testGetRoot() {
226 $this->assertSame(spl_object_hash($this->entityAdapter), spl_object_hash($this->entityAdapter->getRoot()));
230 * @covers ::getPropertyPath
232 public function testGetPropertyPath() {
233 $this->assertSame('', $this->entityAdapter->getPropertyPath());
237 * @covers ::getParent
239 public function testGetParent() {
240 $this->assertNull($this->entityAdapter->getParent());
244 * @covers ::setContext
246 public function testSetContext() {
247 $name = $this->randomMachineName();
248 $parent = $this->getMock('\Drupal\Core\TypedData\TraversableTypedDataInterface');
249 // Our mocked entity->setContext() returns NULL, so assert that.
250 $this->assertNull($this->entityAdapter->setContext($name, $parent));
251 $this->assertEquals($name, $this->entityAdapter->getName());
252 $this->assertEquals($parent, $this->entityAdapter->getParent());
258 public function testGetValue() {
259 $this->assertEquals($this->entity, $this->entityAdapter->getValue());
265 public function testSetValue() {
266 $this->entityAdapter->setValue(NULL);
267 $this->assertNull($this->entityAdapter->getValue());
273 public function testGet() {
274 $this->assertInstanceOf('\Drupal\Core\Field\FieldItemListInterface', $this->entityAdapter->get('id'));
280 public function testGetInvalidField() {
281 $this->setExpectedException(\InvalidArgumentException::class);
282 $this->entityAdapter->get('invalid');
288 public function testGetWithoutData() {
289 $this->entityAdapter->setValue(NULL);
290 $this->setExpectedException(MissingDataException::class);
291 $this->entityAdapter->get('id');
297 public function testSet() {
298 $id_items = [['value' => $this->id + 1]];
300 $this->fieldItemList->expects($this->once())
304 $this->entityAdapter->set('id', $id_items);
310 public function testSetWithoutData() {
311 $this->entityAdapter->setValue(NULL);
312 $id_items = [['value' => $this->id + 1]];
313 $this->setExpectedException(MissingDataException::class);
314 $this->entityAdapter->set('id', $id_items);
318 * @covers ::getProperties
320 public function testGetProperties() {
321 $fields = $this->entityAdapter->getProperties();
322 $this->assertInstanceOf('Drupal\Core\Field\FieldItemListInterface', $fields['id']);
323 $this->assertInstanceOf('Drupal\Core\Field\FieldItemListInterface', $fields['revision_id']);
329 public function testToArray() {
330 $array = $this->entityAdapter->toArray();
331 // Mock field objects return NULL values, so test keys only.
332 $this->assertArrayHasKey('id', $array);
333 $this->assertArrayHasKey('revision_id', $array);
334 $this->assertEquals(count($array), 2);
340 public function testToArrayWithoutData() {
341 $this->entityAdapter->setValue(NULL);
342 $this->setExpectedException(MissingDataException::class);
343 $this->entityAdapter->toArray();
349 public function testIsEmpty() {
350 $this->assertFalse($this->entityAdapter->isEmpty());
351 $this->entityAdapter->setValue(NULL);
352 $this->assertTrue($this->entityAdapter->isEmpty());
358 public function testOnChange() {
359 $entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
360 $entity->expects($this->once())
364 $this->entityAdapter->setValue($entity);
365 $this->entityAdapter->onChange('foo');
369 * @covers ::getDataDefinition
371 public function testGetDataDefinition() {
372 $definition = $this->entityAdapter->getDataDefinition();
373 $this->assertInstanceOf('\Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface', $definition);
374 $this->assertEquals($definition->getEntityTypeId(), $this->entityTypeId);
375 $this->assertEquals($definition->getBundles(), [$this->bundle]);
379 * @covers ::getString
381 public function testGetString() {
382 $entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
383 $entity->expects($this->once())
386 $this->entityAdapter->setValue($entity);
387 $this->assertEquals('foo', $this->entityAdapter->getString());
388 $this->entityAdapter->setValue(NULL);
389 $this->assertEquals('', $this->entityAdapter->getString());
393 * @covers ::applyDefaultValue
395 public function testApplyDefaultValue() {
396 // For each field on the entity the mock method has to be invoked once.
397 $this->fieldItemList->expects($this->exactly(2))
398 ->method('applyDefaultValue');
399 $this->entityAdapter->applyDefaultValue();
403 * @covers ::getIterator
405 public function testGetIterator() {
406 $iterator = $this->entityAdapter->getIterator();
407 $fields = iterator_to_array($iterator);
408 $this->assertArrayHasKey('id', $fields);
409 $this->assertArrayHasKey('revision_id', $fields);
410 $this->assertEquals(count($fields), 2);
412 $this->entityAdapter->setValue(NULL);
413 $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator());