3 namespace Drupal\Tests\Core\Entity\TypedData;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Entity\EntityFieldManagerInterface;
7 use Drupal\Core\Entity\EntityManager;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
10 use Drupal\Core\Field\BaseFieldDefinition;
11 use Drupal\Core\Language\LanguageInterface;
12 use Drupal\Core\TypedData\Exception\MissingDataException;
13 use Drupal\Core\TypedData\TypedDataManagerInterface;
14 use Drupal\Tests\UnitTestCase;
15 use Drupal\Core\Language\Language;
18 * @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
22 class EntityAdapterUnitTest extends UnitTestCase {
25 * The bundle used for testing.
32 * The content entity used for testing.
34 * @var \Drupal\Core\Entity\ContentEntityBase|\PHPUnit_Framework_MockObject_MockObject
39 * The content entity adapter under test.
41 * @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
43 protected $entityAdapter;
46 * The entity type used for testing.
48 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
50 protected $entityType;
53 * The entity manager used for testing.
55 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
57 protected $entityManager;
60 * The entity type manager used for testing.
62 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
64 protected $entityTypeManager;
68 * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
70 protected $entityFieldManager;
73 * The type ID of the entity under test.
77 protected $entityTypeId;
80 * The typed data manager used for testing.
82 * @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit_Framework_MockObject_MockObject
84 protected $typedDataManager;
87 * The field item list returned by the typed data manager.
89 * @var \Drupal\Core\Field\FieldItemListInterface|\PHPUnit_Framework_MockObject_MockObject
91 protected $fieldItemList;
94 * The field type manager used for testing.
96 * @var \Drupal\Core\Field\FieldTypePluginManager|\PHPUnit_Framework_MockObject_MockObject
98 protected $fieldTypePluginManager;
101 * The language manager.
103 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
105 protected $languageManager;
108 * The UUID generator used for testing.
110 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
124 * @var \Drupal\Core\Field\BaseFieldDefinition[]
126 protected $fieldDefinitions;
131 protected function setUp() {
135 'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
136 'defaultLangcode' => [LanguageInterface::LANGCODE_DEFAULT => 'en'],
138 $this->entityTypeId = $this->randomMachineName();
139 $this->bundle = $this->randomMachineName();
141 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
142 $this->entityType->expects($this->any())
144 ->will($this->returnValue([
149 $this->entityManager = new EntityManager();
151 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
152 $this->entityTypeManager->expects($this->any())
153 ->method('getDefinition')
154 ->with($this->entityTypeId)
155 ->will($this->returnValue($this->entityType));
157 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
159 $this->typedDataManager = $this->getMock(TypedDataManagerInterface::class);
160 $this->typedDataManager->expects($this->any())
161 ->method('getDefinition')
163 ->will($this->returnValue(['class' => '\Drupal\Core\Entity\Plugin\DataType\EntityAdapter']));
164 $this->typedDataManager->expects($this->any())
165 ->method('getDefaultConstraints')
168 $validation_constraint_manager = $this->getMockBuilder('\Drupal\Core\Validation\ConstraintManager')
169 ->disableOriginalConstructor()
171 $validation_constraint_manager->expects($this->any())
174 $this->typedDataManager->expects($this->any())
175 ->method('getValidationConstraintManager')
176 ->willReturn($validation_constraint_manager);
178 $not_specified = new Language(['id' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'locked' => TRUE]);
179 $this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface');
180 $this->languageManager->expects($this->any())
181 ->method('getLanguages')
182 ->will($this->returnValue([LanguageInterface::LANGCODE_NOT_SPECIFIED => $not_specified]));
184 $this->languageManager->expects($this->any())
185 ->method('getLanguage')
186 ->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
187 ->will($this->returnValue($not_specified));
189 $this->fieldTypePluginManager = $this->getMockBuilder('\Drupal\Core\Field\FieldTypePluginManager')
190 ->disableOriginalConstructor()
192 $this->fieldTypePluginManager->expects($this->any())
193 ->method('getDefaultStorageSettings')
194 ->will($this->returnValue([]));
195 $this->fieldTypePluginManager->expects($this->any())
196 ->method('getDefaultFieldSettings')
197 ->will($this->returnValue([]));
199 $this->fieldItemList = $this->getMock('\Drupal\Core\Field\FieldItemListInterface');
200 $this->fieldTypePluginManager->expects($this->any())
201 ->method('createFieldItemList')
202 ->willReturn($this->fieldItemList);
204 $this->entityFieldManager = $this->getMockForAbstractClass(EntityFieldManagerInterface::class);
206 $container = new ContainerBuilder();
207 $container->set('entity.manager', $this->entityManager);
208 $container->set('entity_type.manager', $this->entityTypeManager);
209 $container->set('entity_field.manager', $this->entityFieldManager);
210 $container->set('uuid', $this->uuid);
211 $container->set('typed_data_manager', $this->typedDataManager);
212 $container->set('language_manager', $this->languageManager);
213 $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
214 // Inject the container into entity.manager so it can defer to
215 // entity_type.manager and other services.
216 $this->entityManager->setContainer($container);
217 \Drupal::setContainer($container);
219 $this->fieldDefinitions = [
220 'id' => BaseFieldDefinition::create('integer'),
221 'revision_id' => BaseFieldDefinition::create('integer'),
223 $this->entityFieldManager->expects($this->any())
224 ->method('getFieldDefinitions')
225 ->with($this->entityTypeId, $this->bundle)
226 ->will($this->returnValue($this->fieldDefinitions));
228 $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle]);
230 $this->entityAdapter = EntityAdapter::createFromEntity($this->entity);
234 * @covers ::getConstraints
236 public function testGetConstraints() {
237 $this->assertInternalType('array', $this->entityAdapter->getConstraints());
243 public function testGetName() {
244 $this->assertNull($this->entityAdapter->getName());
250 public function testGetRoot() {
251 $this->assertSame(spl_object_hash($this->entityAdapter), spl_object_hash($this->entityAdapter->getRoot()));
255 * @covers ::getPropertyPath
257 public function testGetPropertyPath() {
258 $this->assertSame('', $this->entityAdapter->getPropertyPath());
262 * @covers ::getParent
264 public function testGetParent() {
265 $this->assertNull($this->entityAdapter->getParent());
269 * @covers ::setContext
271 public function testSetContext() {
272 $name = $this->randomMachineName();
273 $parent = $this->getMock('\Drupal\Core\TypedData\TraversableTypedDataInterface');
274 // Our mocked entity->setContext() returns NULL, so assert that.
275 $this->assertNull($this->entityAdapter->setContext($name, $parent));
276 $this->assertEquals($name, $this->entityAdapter->getName());
277 $this->assertEquals($parent, $this->entityAdapter->getParent());
283 public function testGetValue() {
284 $this->assertEquals($this->entity, $this->entityAdapter->getValue());
290 public function testSetValue() {
291 $this->entityAdapter->setValue(NULL);
292 $this->assertNull($this->entityAdapter->getValue());
298 public function testGet() {
299 $this->assertInstanceOf('\Drupal\Core\Field\FieldItemListInterface', $this->entityAdapter->get('id'));
305 public function testGetInvalidField() {
306 $this->setExpectedException(\InvalidArgumentException::class);
307 $this->entityAdapter->get('invalid');
313 public function testGetWithoutData() {
314 $this->entityAdapter->setValue(NULL);
315 $this->setExpectedException(MissingDataException::class);
316 $this->entityAdapter->get('id');
322 public function testSet() {
323 $id_items = [['value' => $this->id + 1]];
325 $this->fieldItemList->expects($this->once())
329 $this->entityAdapter->set('id', $id_items);
335 public function testSetWithoutData() {
336 $this->entityAdapter->setValue(NULL);
337 $id_items = [['value' => $this->id + 1]];
338 $this->setExpectedException(MissingDataException::class);
339 $this->entityAdapter->set('id', $id_items);
343 * @covers ::getProperties
345 public function testGetProperties() {
346 $fields = $this->entityAdapter->getProperties();
347 $this->assertInstanceOf('Drupal\Core\Field\FieldItemListInterface', $fields['id']);
348 $this->assertInstanceOf('Drupal\Core\Field\FieldItemListInterface', $fields['revision_id']);
354 public function testToArray() {
355 $array = $this->entityAdapter->toArray();
356 // Mock field objects return NULL values, so test keys only.
357 $this->assertArrayHasKey('id', $array);
358 $this->assertArrayHasKey('revision_id', $array);
359 $this->assertEquals(count($array), 2);
365 public function testToArrayWithoutData() {
366 $this->entityAdapter->setValue(NULL);
367 $this->setExpectedException(MissingDataException::class);
368 $this->entityAdapter->toArray();
374 public function testIsEmpty() {
375 $this->assertFalse($this->entityAdapter->isEmpty());
376 $this->entityAdapter->setValue(NULL);
377 $this->assertTrue($this->entityAdapter->isEmpty());
383 public function testOnChange() {
384 $entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
385 $entity->expects($this->once())
389 $this->entityAdapter->setValue($entity);
390 $this->entityAdapter->onChange('foo');
394 * @covers ::getDataDefinition
396 public function testGetDataDefinition() {
397 $definition = $this->entityAdapter->getDataDefinition();
398 $this->assertInstanceOf('\Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface', $definition);
399 $this->assertEquals($definition->getEntityTypeId(), $this->entityTypeId);
400 $this->assertEquals($definition->getBundles(), [$this->bundle]);
404 * @covers ::getString
406 public function testGetString() {
407 $entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
408 $entity->expects($this->once())
411 $this->entityAdapter->setValue($entity);
412 $this->assertEquals('foo', $this->entityAdapter->getString());
413 $this->entityAdapter->setValue(NULL);
414 $this->assertEquals('', $this->entityAdapter->getString());
418 * @covers ::applyDefaultValue
420 public function testApplyDefaultValue() {
421 // For each field on the entity the mock method has to be invoked once.
422 $this->fieldItemList->expects($this->exactly(2))
423 ->method('applyDefaultValue');
424 $this->entityAdapter->applyDefaultValue();
428 * @covers ::getIterator
430 public function testGetIterator() {
431 // Content entity test.
432 $iterator = $this->entityAdapter->getIterator();
433 $fields = iterator_to_array($iterator);
434 $this->assertArrayHasKey('id', $fields);
435 $this->assertArrayHasKey('revision_id', $fields);
436 $this->assertEquals(count($fields), 2);
438 $this->entityAdapter->setValue(NULL);
439 $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator());