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