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