4027647abfd267ce691a6e16c47011f0908bb546
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Entity / EntityTypeTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Entity;
4
5 use Drupal\Core\Entity\EntityType;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\StringTranslation\TranslatableMarkup;
8 use Drupal\Core\StringTranslation\TranslationInterface;
9 use Drupal\Tests\UnitTestCase;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Entity\EntityType
13  * @group Entity
14  */
15 class EntityTypeTest extends UnitTestCase {
16
17   /**
18    * Sets up an EntityType object for a given set of values.
19    *
20    * @param array $definition
21    *   An array of values to use for the EntityType.
22    *
23    * @return \Drupal\Core\Entity\EntityTypeInterface
24    */
25   protected function setUpEntityType($definition) {
26     $definition += [
27       'id' => 'example_entity_type',
28     ];
29     return new EntityType($definition);
30   }
31
32   /**
33    * @covers ::get
34    *
35    * @dataProvider providerTestGet
36    */
37   public function testGet(array $defintion, $key, $expected) {
38     $entity_type = $this->setUpEntityType($defintion);
39     $this->assertSame($expected, $entity_type->get($key));
40   }
41
42   /**
43    * @covers ::set
44    * @covers ::get
45    *
46    * @dataProvider providerTestSet
47    */
48   public function testSet($key, $value) {
49     $entity_type = $this->setUpEntityType([]);
50     $this->assertInstanceOf('Drupal\Core\Entity\EntityTypeInterface', $entity_type->set($key, $value));
51     $this->assertSame($value, $entity_type->get($key));
52     $this->assertNoPublicProperties($entity_type);
53   }
54
55   /**
56    * Tests the getKeys() method.
57    *
58    * @dataProvider providerTestGetKeys
59    */
60   public function testGetKeys($entity_keys, $expected) {
61     $entity_type = $this->setUpEntityType(['entity_keys' => $entity_keys]);
62     $expected += [
63       'default_langcode' => 'default_langcode',
64       'revision_translation_affected' => 'revision_translation_affected',
65     ];
66     $this->assertSame($expected, $entity_type->getKeys());
67   }
68
69   /**
70    * Tests the getKey() method.
71    *
72    * @dataProvider providerTestGetKeys
73    */
74   public function testGetKey($entity_keys, $expected) {
75     $entity_type = $this->setUpEntityType(['entity_keys' => $entity_keys]);
76     $this->assertSame($expected['bundle'], $entity_type->getKey('bundle'));
77     $this->assertSame(FALSE, $entity_type->getKey('bananas'));
78   }
79
80   /**
81    * Tests the hasKey() method.
82    *
83    * @dataProvider providerTestGetKeys
84    */
85   public function testHasKey($entity_keys, $expected) {
86     $entity_type = $this->setUpEntityType(['entity_keys' => $entity_keys]);
87     $this->assertSame(!empty($expected['bundle']), $entity_type->hasKey('bundle'));
88     $this->assertSame(!empty($expected['id']), $entity_type->hasKey('id'));
89     $this->assertSame(FALSE, $entity_type->hasKey('bananas'));
90   }
91
92   /**
93    * Provides test data for testGet.
94    */
95   public function providerTestGet() {
96     return [
97       [[], 'provider', NULL],
98       [['provider' => ''], 'provider', ''],
99       [['provider' => 'test'], 'provider', 'test'],
100       [[], 'something_additional', NULL],
101       [['something_additional' => ''], 'something_additional', ''],
102       [['something_additional' => 'additional'], 'something_additional', 'additional'],
103     ];
104   }
105
106   /**
107    * Provides test data for testSet.
108    */
109   public function providerTestSet() {
110     return [
111       ['provider', NULL],
112       ['provider', ''],
113       ['provider', 'test'],
114       ['something_additional', NULL],
115       ['something_additional', ''],
116       ['something_additional', 'additional'],
117     ];
118   }
119
120   /**
121    * Provides test data.
122    */
123   public function providerTestGetKeys() {
124     return [
125       [[], ['revision' => '', 'bundle' => '', 'langcode' => '']],
126       [['id' => 'id'], ['id' => 'id', 'revision' => '', 'bundle' => '', 'langcode' => '']],
127       [['bundle' => 'bundle'], ['bundle' => 'bundle', 'revision' => '', 'langcode' => '']],
128     ];
129   }
130
131   /**
132    * Tests the isInternal() method.
133    */
134   public function testIsInternal() {
135     $entity_type = $this->setUpEntityType(['internal' => TRUE]);
136     $this->assertTrue($entity_type->isInternal());
137     $entity_type = $this->setUpEntityType(['internal' => FALSE]);
138     $this->assertFalse($entity_type->isInternal());
139     $entity_type = $this->setUpEntityType([]);
140     $this->assertFalse($entity_type->isInternal());
141   }
142
143   /**
144    * Tests the isRevisionable() method.
145    */
146   public function testIsRevisionable() {
147     $entity_type = $this->setUpEntityType(['entity_keys' => ['id' => 'id']]);
148     $this->assertFalse($entity_type->isRevisionable());
149     $entity_type = $this->setUpEntityType(['entity_keys' => ['id' => 'id', 'revision' => FALSE]]);
150     $this->assertFalse($entity_type->isRevisionable());
151     $entity_type = $this->setUpEntityType(['entity_keys' => ['id' => 'id', 'revision' => TRUE]]);
152     $this->assertTrue($entity_type->isRevisionable());
153   }
154
155   /**
156    * Tests the getHandler() method.
157    */
158   public function testGetHandler() {
159     $controller = $this->getTestHandlerClass();
160     $entity_type = $this->setUpEntityType([
161       'handlers' => [
162         'storage' => $controller,
163         'form' => [
164           'default' => $controller,
165         ],
166       ],
167     ]);
168     $this->assertSame($controller, $entity_type->getHandlerClass('storage'));
169     $this->assertSame($controller, $entity_type->getHandlerClass('form', 'default'));
170   }
171
172   /**
173    * Tests the getStorageClass() method.
174    */
175   public function testGetStorageClass() {
176     $controller = $this->getTestHandlerClass();
177     $entity_type = $this->setUpEntityType([
178       'handlers' => [
179         'storage' => $controller,
180       ],
181     ]);
182     $this->assertSame($controller, $entity_type->getStorageClass());
183   }
184
185   /**
186    * Tests the setStorageClass() method.
187    */
188   public function testSetStorageClass() {
189     $controller = $this->getTestHandlerClass();
190     $entity_type = $this->setUpEntityType([]);
191     $this->assertSame($entity_type, $entity_type->setStorageClass($controller));
192   }
193
194   /**
195    * Tests the getListBuilderClass() method.
196    */
197   public function testGetListBuilderClass() {
198     $controller = $this->getTestHandlerClass();
199     $entity_type = $this->setUpEntityType([
200       'handlers' => [
201         'list_builder' => $controller,
202       ],
203     ]);
204     $this->assertSame($controller, $entity_type->getListBuilderClass());
205   }
206
207   /**
208    * Tests the getAccessControlClass() method.
209    */
210   public function testGetAccessControlClass() {
211     $controller = $this->getTestHandlerClass();
212     $entity_type = $this->setUpEntityType([
213       'handlers' => [
214         'access' => $controller,
215       ],
216     ]);
217     $this->assertSame($controller, $entity_type->getAccessControlClass());
218   }
219
220   /**
221    * Tests the getFormClass() method.
222    */
223   public function testGetFormClass() {
224     $controller = $this->getTestHandlerClass();
225     $operation = 'default';
226     $entity_type = $this->setUpEntityType([
227       'handlers' => [
228         'form' => [
229           $operation => $controller,
230         ],
231       ],
232     ]);
233     $this->assertSame($controller, $entity_type->getFormClass($operation));
234   }
235
236   /**
237    * Tests the hasFormClasses() method.
238    */
239   public function testHasFormClasses() {
240     $controller = $this->getTestHandlerClass();
241     $operation = 'default';
242     $entity_type1 = $this->setUpEntityType([
243       'handlers' => [
244         'form' => [
245           $operation => $controller,
246         ],
247       ],
248     ]);
249     $entity_type2 = $this->setUpEntityType([
250       'handlers' => [],
251     ]);
252     $this->assertTrue($entity_type1->hasFormClasses());
253     $this->assertFalse($entity_type2->hasFormClasses());
254   }
255
256   /**
257    * Tests the getViewBuilderClass() method.
258    */
259   public function testGetViewBuilderClass() {
260     $controller = $this->getTestHandlerClass();
261     $entity_type = $this->setUpEntityType([
262       'handlers' => [
263         'view_builder' => $controller,
264       ],
265     ]);
266     $this->assertSame($controller, $entity_type->getViewBuilderClass());
267   }
268
269   /**
270    * @covers ::__construct
271    */
272   public function testIdExceedsMaxLength() {
273     $id = $this->randomMachineName(33);
274     $message = 'Attempt to create an entity type with an ID longer than 32 characters: ' . $id;
275     $this->setExpectedException('Drupal\Core\Entity\Exception\EntityTypeIdLengthException', $message);
276     $this->setUpEntityType(['id' => $id]);
277   }
278
279   /**
280    * @covers ::getOriginalClass
281    */
282   public function testgetOriginalClassUnchanged() {
283     $class = $this->randomMachineName();
284     $entity_type = $this->setUpEntityType(['class' => $class]);
285     $this->assertEquals($class, $entity_type->getOriginalClass());
286   }
287
288   /**
289    * @covers ::setClass
290    * @covers ::getOriginalClass
291    */
292   public function testgetOriginalClassChanged() {
293     $class = $this->randomMachineName();
294     $entity_type = $this->setUpEntityType(['class' => $class]);
295     $entity_type->setClass($this->randomMachineName());
296     $this->assertEquals($class, $entity_type->getOriginalClass());
297   }
298
299   /**
300    * @covers ::id
301    */
302   public function testId() {
303     $id = $this->randomMachineName(32);
304     $entity_type = $this->setUpEntityType(['id' => $id]);
305     $this->assertEquals($id, $entity_type->id());
306   }
307
308   /**
309    * @covers ::getLabel
310    */
311   public function testGetLabel() {
312     $translatable_label = new TranslatableMarkup($this->randomMachineName());
313     $entity_type = $this->setUpEntityType(['label' => $translatable_label]);
314     $this->assertSame($translatable_label, $entity_type->getLabel());
315
316     $label = $this->randomMachineName();
317     $entity_type = $this->setUpEntityType(['label' => $label]);
318     $this->assertSame($label, $entity_type->getLabel());
319   }
320
321   /**
322    * @covers ::getGroupLabel
323    */
324   public function testGetGroupLabel() {
325     $translatable_group_label = new TranslatableMarkup($this->randomMachineName());
326     $entity_type = $this->setUpEntityType(['group_label' => $translatable_group_label]);
327     $this->assertSame($translatable_group_label, $entity_type->getGroupLabel());
328
329     $default_label = $this->randomMachineName();
330     $entity_type = $this->setUpEntityType(['group_label' => $default_label]);
331     $this->assertSame($default_label, $entity_type->getGroupLabel());
332
333     $default_label = new TranslatableMarkup('Other', [], ['context' => 'Entity type group']);
334     $entity_type = $this->setUpEntityType(['group_label' => $default_label]);
335     $this->assertSame($default_label, $entity_type->getGroupLabel());
336   }
337
338   /**
339    * @covers ::getCollectionLabel
340    */
341   public function testGetCollectionLabel() {
342     $translatable_label = new TranslatableMarkup('Entity test collection', [], [], $this->getStringTranslationStub());
343     $entity_type = $this->setUpEntityType(['label_collection' => $translatable_label]);
344     $entity_type->setStringTranslation($this->getStringTranslationStub());
345     $this->assertEquals('Entity test collection', $entity_type->getCollectionLabel());
346   }
347
348   /**
349    * @covers ::getSingularLabel
350    */
351   public function testGetSingularLabel() {
352     $translatable_label = new TranslatableMarkup('entity test singular', [], [], $this->getStringTranslationStub());
353     $entity_type = $this->setUpEntityType(['label_singular' => $translatable_label]);
354     $entity_type->setStringTranslation($this->getStringTranslationStub());
355     $this->assertEquals('entity test singular', $entity_type->getSingularLabel());
356   }
357
358   /**
359    * @covers ::getSingularLabel
360    */
361   public function testGetSingularLabelDefault() {
362     $entity_type = $this->setUpEntityType(['label' => 'Entity test Singular']);
363     $entity_type->setStringTranslation($this->getStringTranslationStub());
364     $this->assertEquals('entity test singular', $entity_type->getSingularLabel());
365   }
366
367   /**
368    * @covers ::getPluralLabel
369    */
370   public function testGetPluralLabel() {
371     $translatable_label = new TranslatableMarkup('entity test plural', [], [], $this->getStringTranslationStub());
372     $entity_type = $this->setUpEntityType(['label_plural' => $translatable_label]);
373     $entity_type->setStringTranslation($this->getStringTranslationStub());
374     $this->assertEquals('entity test plural', $entity_type->getPluralLabel());
375   }
376
377   /**
378    * @covers ::getPluralLabel
379    */
380   public function testGetPluralLabelDefault() {
381     $entity_type = $this->setUpEntityType(['label' => 'Entity test Plural']);
382     $entity_type->setStringTranslation($this->getStringTranslationStub());
383     $this->assertEquals('entity test plural entities', $entity_type->getPluralLabel());
384   }
385
386   /**
387    * @covers ::getCountLabel
388    */
389   public function testGetCountLabel() {
390     $entity_type = $this->setUpEntityType(['label_count' => ['singular' => 'one entity test', 'plural' => '@count entity test']]);
391     $entity_type->setStringTranslation($this->getStringTranslationStub());
392     $this->assertEquals('one entity test', $entity_type->getCountLabel(1));
393     $this->assertEquals('2 entity test', $entity_type->getCountLabel(2));
394     $this->assertEquals('200 entity test', $entity_type->getCountLabel(200));
395   }
396
397   /**
398    * @covers ::getCountLabel
399    */
400   public function testGetCountLabelDefault() {
401     $entity_type = $this->setUpEntityType(['label' => 'Entity test Plural']);
402     $entity_type->setStringTranslation($this->getStringTranslationStub());
403     $this->assertEquals('1 entity test plural', $entity_type->getCountLabel(1));
404     $this->assertEquals('2 entity test plural entities', $entity_type->getCountLabel(2));
405     $this->assertEquals('200 entity test plural entities', $entity_type->getCountLabel(200));
406   }
407
408   /**
409    * Tests the ::getBundleLabel() method.
410    *
411    * @covers ::getBundleLabel
412    * @dataProvider providerTestGetBundleLabel
413    */
414   public function testGetBundleLabel($definition, $expected) {
415     $entity_type = $this->setUpEntityType($definition);
416     $entity_type->setStringTranslation($this->getStringTranslationStub());
417     $this->assertEquals($expected, $entity_type->getBundleLabel());
418   }
419
420   /**
421    * Provides test data for ::testGetBundleLabel().
422    */
423   public function providerTestGetBundleLabel() {
424     return [
425       [['label' => 'Entity Label Foo'], 'Entity Label Foo bundle'],
426       [['bundle_label' => 'Bundle Label Bar'], 'Bundle Label Bar'],
427     ];
428   }
429
430   /**
431    * Gets a mock controller class name.
432    *
433    * @return string
434    *   A mock controller class name.
435    */
436   protected function getTestHandlerClass() {
437     return get_class($this->getMockForAbstractClass('Drupal\Core\Entity\EntityHandlerBase'));
438   }
439
440   /**
441    * @covers ::setLinkTemplate
442    */
443   public function testSetLinkTemplateWithInvalidPath() {
444     $entity_type = $this->setUpEntityType(['id' => $this->randomMachineName()]);
445     $this->setExpectedException(\InvalidArgumentException::class);
446     $entity_type->setLinkTemplate('test', 'invalid-path');
447   }
448
449   /**
450    * Tests the constraint methods.
451    *
452    * @covers ::getConstraints
453    * @covers ::setConstraints
454    * @covers ::addConstraint
455    */
456   public function testConstraintMethods() {
457     $definition = [
458       'constraints' => [
459         'EntityChanged' => [],
460       ],
461     ];
462     $entity_type = $this->setUpEntityType($definition);
463     $this->assertEquals($definition['constraints'], $entity_type->getConstraints());
464     $entity_type->addConstraint('Test');
465     $this->assertEquals($definition['constraints'] + ['Test' => NULL], $entity_type->getConstraints());
466     $entity_type->setConstraints([]);
467     $this->assertEquals([], $entity_type->getConstraints());
468   }
469
470   /**
471    * Asserts there on no public properties on the object instance.
472    *
473    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
474    */
475   protected function assertNoPublicProperties(EntityTypeInterface $entity_type) {
476     $reflection = new \ReflectionObject($entity_type);
477     $this->assertEmpty($reflection->getProperties(\ReflectionProperty::IS_PUBLIC));
478   }
479
480   /**
481    * Tests that the EntityType object can be serialized.
482    */
483   public function testIsSerializable() {
484     $entity_type = $this->setUpEntityType([]);
485
486     $translation = $this->prophesize(TranslationInterface::class);
487     $translation->willImplement(\Serializable::class);
488     $translation->serialize()->willThrow(\Exception::class);
489     $translation_service = $translation->reveal();
490     $translation_service->_serviceId = 'string_translation';
491
492     $entity_type->setStringTranslation($translation_service);
493     $entity_type = unserialize(serialize($entity_type));
494
495     $this->assertEquals('example_entity_type', $entity_type->id());
496   }
497
498 }