Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Entity / TypedData / EntityAdapterUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Entity\TypedData;
4
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;
13
14 /**
15  * @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
16  * @group Entity
17  * @group TypedData
18  */
19 class EntityAdapterUnitTest extends UnitTestCase {
20
21   /**
22    * The bundle used for testing.
23    *
24    * @var string
25    */
26   protected $bundle;
27
28   /**
29    * The entity used for testing.
30    *
31    * @var \Drupal\Core\Entity\ContentEntityBase|\PHPUnit_Framework_MockObject_MockObject
32    */
33   protected $entity;
34
35   /**
36    * The entity adapter under test.
37    *
38    * @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter
39    */
40   protected $entityAdapter;
41
42   /**
43    * The entity type used for testing.
44    *
45    * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
46    */
47   protected $entityType;
48
49   /**
50    * The entity manager used for testing.
51    *
52    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
53    */
54   protected $entityManager;
55
56   /**
57    * The type ID of the entity under test.
58    *
59    * @var string
60    */
61   protected $entityTypeId;
62
63   /**
64    * The typed data manager used for testing.
65    *
66    * @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit_Framework_MockObject_MockObject
67    */
68   protected $typedDataManager;
69
70   /**
71    * The field item list returned by the typed data manager.
72    *
73    * @var \Drupal\Core\Field\FieldItemListInterface|\PHPUnit_Framework_MockObject_MockObject
74    */
75   protected $fieldItemList;
76
77   /**
78    * The field type manager used for testing.
79    *
80    * @var \Drupal\Core\Field\FieldTypePluginManager|\PHPUnit_Framework_MockObject_MockObject
81    */
82   protected $fieldTypePluginManager;
83
84   /**
85    * The language manager.
86    *
87    * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
88    */
89   protected $languageManager;
90
91   /**
92    * The UUID generator used for testing.
93    *
94    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
95    */
96   protected $uuid;
97
98   /**
99    * The entity ID.
100    *
101    * @var int
102    */
103   protected $id;
104
105   /**
106    * Field definitions.
107    *
108    * @var \Drupal\Core\Field\BaseFieldDefinition[]
109    */
110   protected $fieldDefinitions;
111
112   /**
113    * {@inheritdoc}
114    */
115   protected function setUp() {
116     $this->id = 1;
117     $values = [
118       'id' => $this->id,
119       'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
120       'defaultLangcode' => [LanguageInterface::LANGCODE_DEFAULT => 'en'],
121     ];
122     $this->entityTypeId = $this->randomMachineName();
123     $this->bundle = $this->randomMachineName();
124
125     $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
126     $this->entityType->expects($this->any())
127       ->method('getKeys')
128       ->will($this->returnValue([
129         'id' => 'id',
130         'uuid' => 'uuid',
131     ]));
132
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));
138
139     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
140
141     $this->typedDataManager = $this->getMock(TypedDataManagerInterface::class);
142     $this->typedDataManager->expects($this->any())
143       ->method('getDefinition')
144       ->with('entity')
145       ->will($this->returnValue(['class' => '\Drupal\Core\Entity\Plugin\DataType\EntityAdapter']));
146     $this->typedDataManager->expects($this->any())
147       ->method('getDefaultConstraints')
148       ->willReturn([]);
149
150     $validation_constraint_manager = $this->getMockBuilder('\Drupal\Core\Validation\ConstraintManager')
151       ->disableOriginalConstructor()
152       ->getMock();
153     $validation_constraint_manager->expects($this->any())
154       ->method('create')
155       ->willReturn([]);
156     $this->typedDataManager->expects($this->any())
157       ->method('getValidationConstraintManager')
158       ->willReturn($validation_constraint_manager);
159
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]));
165
166     $this->languageManager->expects($this->any())
167       ->method('getLanguage')
168       ->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
169       ->will($this->returnValue($not_specified));
170
171     $this->fieldTypePluginManager = $this->getMockBuilder('\Drupal\Core\Field\FieldTypePluginManager')
172       ->disableOriginalConstructor()
173       ->getMock();
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([]));
180
181     $this->fieldItemList = $this->getMock('\Drupal\Core\Field\FieldItemListInterface');
182     $this->fieldTypePluginManager->expects($this->any())
183       ->method('createFieldItemList')
184       ->willReturn($this->fieldItemList);
185
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);
193
194     $this->fieldDefinitions = [
195       'id' => BaseFieldDefinition::create('integer'),
196       'revision_id' => BaseFieldDefinition::create('integer'),
197     ];
198
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]);
204
205     $this->entityAdapter = EntityAdapter::createFromEntity($this->entity);
206   }
207
208   /**
209    * @covers ::getConstraints
210    */
211   public function testGetConstraints() {
212     $this->assertInternalType('array', $this->entityAdapter->getConstraints());
213   }
214
215   /**
216    * @covers ::getName
217    */
218   public function testGetName() {
219     $this->assertNull($this->entityAdapter->getName());
220   }
221
222   /**
223    * @covers ::getRoot
224    */
225   public function testGetRoot() {
226     $this->assertSame(spl_object_hash($this->entityAdapter), spl_object_hash($this->entityAdapter->getRoot()));
227   }
228
229   /**
230    * @covers ::getPropertyPath
231    */
232   public function testGetPropertyPath() {
233     $this->assertSame('', $this->entityAdapter->getPropertyPath());
234   }
235
236   /**
237    * @covers ::getParent
238    */
239   public function testGetParent() {
240     $this->assertNull($this->entityAdapter->getParent());
241   }
242
243   /**
244    * @covers ::setContext
245    */
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());
253   }
254
255   /**
256    * @covers ::getValue
257    */
258   public function testGetValue() {
259     $this->assertEquals($this->entity, $this->entityAdapter->getValue());
260   }
261
262   /**
263    * @covers ::setValue
264    */
265   public function testSetValue() {
266     $this->entityAdapter->setValue(NULL);
267     $this->assertNull($this->entityAdapter->getValue());
268   }
269
270   /**
271    * @covers ::get
272    */
273   public function testGet() {
274     $this->assertInstanceOf('\Drupal\Core\Field\FieldItemListInterface', $this->entityAdapter->get('id'));
275   }
276
277   /**
278    * @covers ::get
279    */
280   public function testGetInvalidField() {
281     $this->setExpectedException(\InvalidArgumentException::class);
282     $this->entityAdapter->get('invalid');
283   }
284
285   /**
286    * @covers ::get
287    */
288   public function testGetWithoutData() {
289     $this->entityAdapter->setValue(NULL);
290     $this->setExpectedException(MissingDataException::class);
291     $this->entityAdapter->get('id');
292   }
293
294   /**
295    * @covers ::set
296    */
297   public function testSet() {
298     $id_items = [ ['value' => $this->id + 1] ];
299
300     $this->fieldItemList->expects($this->once())
301       ->method('setValue')
302       ->with($id_items);
303
304     $this->entityAdapter->set('id', $id_items);
305   }
306
307   /**
308    * @covers ::set
309    */
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);
315   }
316
317   /**
318  * @covers ::getProperties
319  */
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']);
324   }
325
326   /**
327    * @covers ::toArray
328    */
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);
335   }
336
337   /**
338    * @covers ::toArray
339    */
340   public function testToArrayWithoutData() {
341     $this->entityAdapter->setValue(NULL);
342     $this->setExpectedException(MissingDataException::class);
343     $this->entityAdapter->toArray();
344   }
345
346   /**
347    * @covers ::isEmpty
348    */
349   public function testIsEmpty() {
350     $this->assertFalse($this->entityAdapter->isEmpty());
351     $this->entityAdapter->setValue(NULL);
352     $this->assertTrue($this->entityAdapter->isEmpty());
353   }
354
355   /**
356    * @covers ::onChange
357    */
358   public function testOnChange() {
359     $entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
360     $entity->expects($this->once())
361       ->method('onChange')
362       ->with('foo')
363       ->willReturn(NULL);
364     $this->entityAdapter->setValue($entity);
365     $this->entityAdapter->onChange('foo');
366   }
367
368   /**
369    * @covers ::getDataDefinition
370    */
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 ]);
376   }
377
378   /**
379    * @covers ::getString
380    */
381   public function testGetString() {
382     $entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
383     $entity->expects($this->once())
384       ->method('label')
385       ->willReturn('foo');
386     $this->entityAdapter->setValue($entity);
387     $this->assertEquals('foo', $this->entityAdapter->getString());
388     $this->entityAdapter->setValue(NULL);
389     $this->assertEquals('', $this->entityAdapter->getString());
390   }
391
392   /**
393    * @covers ::applyDefaultValue
394    */
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();
400   }
401
402   /**
403    * @covers ::getIterator
404    */
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);
411
412     $this->entityAdapter->setValue(NULL);
413     $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator());
414   }
415
416 }