Security update for Core, with self-updated composer
[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 isRevisionable() method.
132    */
133   public function testIsRevisionable() {
134     $entity_type = $this->setUpEntityType(['entity_keys' => ['id' => 'id']]);
135     $this->assertFalse($entity_type->isRevisionable());
136     $entity_type = $this->setUpEntityType(['entity_keys' => ['id' => 'id', 'revision' => FALSE]]);
137     $this->assertFalse($entity_type->isRevisionable());
138     $entity_type = $this->setUpEntityType(['entity_keys' => ['id' => 'id', 'revision' => TRUE]]);
139     $this->assertTrue($entity_type->isRevisionable());
140   }
141
142   /**
143    * Tests the getHandler() method.
144    */
145   public function testGetHandler() {
146     $controller = $this->getTestHandlerClass();
147     $entity_type = $this->setUpEntityType([
148       'handlers' => [
149         'storage' => $controller,
150         'form' => [
151           'default' => $controller,
152         ],
153       ],
154     ]);
155     $this->assertSame($controller, $entity_type->getHandlerClass('storage'));
156     $this->assertSame($controller, $entity_type->getHandlerClass('form', 'default'));
157   }
158
159   /**
160    * Tests the getStorageClass() method.
161    */
162   public function testGetStorageClass() {
163     $controller = $this->getTestHandlerClass();
164     $entity_type = $this->setUpEntityType([
165       'handlers' => [
166         'storage' => $controller,
167       ],
168     ]);
169     $this->assertSame($controller, $entity_type->getStorageClass());
170   }
171
172   /**
173    * Tests the setStorageClass() method.
174    */
175   public function testSetStorageClass() {
176     $controller = $this->getTestHandlerClass();
177     $entity_type = $this->setUpEntityType([]);
178     $this->assertSame($entity_type, $entity_type->setStorageClass($controller));
179   }
180
181   /**
182    * Tests the getListBuilderClass() method.
183    */
184   public function testGetListBuilderClass() {
185     $controller = $this->getTestHandlerClass();
186     $entity_type = $this->setUpEntityType([
187       'handlers' => [
188         'list_builder' => $controller,
189       ],
190     ]);
191     $this->assertSame($controller, $entity_type->getListBuilderClass());
192   }
193
194   /**
195    * Tests the getAccessControlClass() method.
196    */
197   public function testGetAccessControlClass() {
198     $controller = $this->getTestHandlerClass();
199     $entity_type = $this->setUpEntityType([
200       'handlers' => [
201         'access' => $controller,
202       ],
203     ]);
204     $this->assertSame($controller, $entity_type->getAccessControlClass());
205   }
206
207   /**
208    * Tests the getFormClass() method.
209    */
210   public function testGetFormClass() {
211     $controller = $this->getTestHandlerClass();
212     $operation = 'default';
213     $entity_type = $this->setUpEntityType([
214       'handlers' => [
215         'form' => [
216           $operation => $controller,
217         ],
218       ],
219     ]);
220     $this->assertSame($controller, $entity_type->getFormClass($operation));
221   }
222
223   /**
224    * Tests the hasFormClasses() method.
225    */
226   public function testHasFormClasses() {
227     $controller = $this->getTestHandlerClass();
228     $operation = 'default';
229     $entity_type1 = $this->setUpEntityType([
230       'handlers' => [
231         'form' => [
232           $operation => $controller,
233         ],
234       ],
235     ]);
236     $entity_type2 = $this->setUpEntityType([
237       'handlers' => [],
238     ]);
239     $this->assertTrue($entity_type1->hasFormClasses());
240     $this->assertFalse($entity_type2->hasFormClasses());
241   }
242
243   /**
244    * Tests the getViewBuilderClass() method.
245    */
246   public function testGetViewBuilderClass() {
247     $controller = $this->getTestHandlerClass();
248     $entity_type = $this->setUpEntityType([
249       'handlers' => [
250         'view_builder' => $controller,
251       ],
252     ]);
253     $this->assertSame($controller, $entity_type->getViewBuilderClass());
254   }
255
256   /**
257    * @covers ::__construct
258    */
259   public function testIdExceedsMaxLength() {
260     $id = $this->randomMachineName(33);
261     $message = 'Attempt to create an entity type with an ID longer than 32 characters: ' . $id;
262     $this->setExpectedException('Drupal\Core\Entity\Exception\EntityTypeIdLengthException', $message);
263     $this->setUpEntityType(['id' => $id]);
264   }
265
266   /**
267    * @covers ::getOriginalClass
268    */
269   public function testgetOriginalClassUnchanged() {
270     $class = $this->randomMachineName();
271     $entity_type = $this->setUpEntityType(['class' => $class]);
272     $this->assertEquals($class, $entity_type->getOriginalClass());
273   }
274
275   /**
276    * @covers ::setClass
277    * @covers ::getOriginalClass
278    */
279   public function testgetOriginalClassChanged() {
280     $class = $this->randomMachineName();
281     $entity_type = $this->setUpEntityType(['class' => $class]);
282     $entity_type->setClass($this->randomMachineName());
283     $this->assertEquals($class, $entity_type->getOriginalClass());
284   }
285
286   /**
287    * @covers ::id
288    */
289   public function testId() {
290     $id = $this->randomMachineName(32);
291     $entity_type = $this->setUpEntityType(['id' => $id]);
292     $this->assertEquals($id, $entity_type->id());
293   }
294
295   /**
296    * @covers ::getLabel
297    */
298   public function testGetLabel() {
299     $translatable_label = new TranslatableMarkup($this->randomMachineName());
300     $entity_type = $this->setUpEntityType(['label' => $translatable_label]);
301     $this->assertSame($translatable_label, $entity_type->getLabel());
302
303     $label = $this->randomMachineName();
304     $entity_type = $this->setUpEntityType(['label' => $label]);
305     $this->assertSame($label, $entity_type->getLabel());
306   }
307
308   /**
309    * @covers ::getGroupLabel
310    */
311   public function testGetGroupLabel() {
312     $translatable_group_label = new TranslatableMarkup($this->randomMachineName());
313     $entity_type = $this->setUpEntityType(['group_label' => $translatable_group_label]);
314     $this->assertSame($translatable_group_label, $entity_type->getGroupLabel());
315
316     $default_label = $this->randomMachineName();
317     $entity_type = $this->setUpEntityType(['group_label' => $default_label]);
318     $this->assertSame($default_label, $entity_type->getGroupLabel());
319
320     $default_label = new TranslatableMarkup('Other', [], ['context' => 'Entity type group']);
321     $entity_type = $this->setUpEntityType(['group_label' => $default_label]);
322     $this->assertSame($default_label, $entity_type->getGroupLabel());
323   }
324
325   /**
326    * @covers ::getCollectionLabel
327    */
328   public function testGetCollectionLabel() {
329     $translatable_label = new TranslatableMarkup('Entity test collection', [], [], $this->getStringTranslationStub());
330     $entity_type = $this->setUpEntityType(['label_collection' => $translatable_label]);
331     $entity_type->setStringTranslation($this->getStringTranslationStub());
332     $this->assertEquals('Entity test collection', $entity_type->getCollectionLabel());
333   }
334
335   /**
336    * @covers ::getSingularLabel
337    */
338   public function testGetSingularLabel() {
339     $translatable_label = new TranslatableMarkup('entity test singular', [], [], $this->getStringTranslationStub());
340     $entity_type = $this->setUpEntityType(['label_singular' => $translatable_label]);
341     $entity_type->setStringTranslation($this->getStringTranslationStub());
342     $this->assertEquals('entity test singular', $entity_type->getSingularLabel());
343   }
344
345   /**
346    * @covers ::getSingularLabel
347    */
348   public function testGetSingularLabelDefault() {
349     $entity_type = $this->setUpEntityType(['label' => 'Entity test Singular']);
350     $entity_type->setStringTranslation($this->getStringTranslationStub());
351     $this->assertEquals('entity test singular', $entity_type->getSingularLabel());
352   }
353
354   /**
355    * @covers ::getPluralLabel
356    */
357   public function testGetPluralLabel() {
358     $translatable_label = new TranslatableMarkup('entity test plural', [], [], $this->getStringTranslationStub());
359     $entity_type = $this->setUpEntityType(['label_plural' => $translatable_label]);
360     $entity_type->setStringTranslation($this->getStringTranslationStub());
361     $this->assertEquals('entity test plural', $entity_type->getPluralLabel());
362   }
363
364   /**
365    * @covers ::getPluralLabel
366    */
367   public function testGetPluralLabelDefault() {
368     $entity_type = $this->setUpEntityType(['label' => 'Entity test Plural']);
369     $entity_type->setStringTranslation($this->getStringTranslationStub());
370     $this->assertEquals('entity test plural entities', $entity_type->getPluralLabel());
371   }
372
373   /**
374    * @covers ::getCountLabel
375    */
376   public function testGetCountLabel() {
377     $entity_type = $this->setUpEntityType(['label_count' => ['singular' => 'one entity test', 'plural' => '@count entity test']]);
378     $entity_type->setStringTranslation($this->getStringTranslationStub());
379     $this->assertEquals('one entity test', $entity_type->getCountLabel(1));
380     $this->assertEquals('2 entity test', $entity_type->getCountLabel(2));
381     $this->assertEquals('200 entity test', $entity_type->getCountLabel(200));
382   }
383
384   /**
385    * @covers ::getCountLabel
386    */
387   public function testGetCountLabelDefault() {
388     $entity_type = $this->setUpEntityType(['label' => 'Entity test Plural']);
389     $entity_type->setStringTranslation($this->getStringTranslationStub());
390     $this->assertEquals('1 entity test plural', $entity_type->getCountLabel(1));
391     $this->assertEquals('2 entity test plural entities', $entity_type->getCountLabel(2));
392     $this->assertEquals('200 entity test plural entities', $entity_type->getCountLabel(200));
393   }
394
395   /**
396    * Gets a mock controller class name.
397    *
398    * @return string
399    *   A mock controller class name.
400    */
401   protected function getTestHandlerClass() {
402     return get_class($this->getMockForAbstractClass('Drupal\Core\Entity\EntityHandlerBase'));
403   }
404
405   /**
406    * @covers ::setLinkTemplate
407    */
408   public function testSetLinkTemplateWithInvalidPath() {
409     $entity_type = $this->setUpEntityType(['id' => $this->randomMachineName()]);
410     $this->setExpectedException(\InvalidArgumentException::class);
411     $entity_type->setLinkTemplate('test', 'invalid-path');
412   }
413
414   /**
415    * Tests the constraint methods.
416    *
417    * @covers ::getConstraints
418    * @covers ::setConstraints
419    * @covers ::addConstraint
420    */
421   public function testConstraintMethods() {
422     $definition = [
423       'constraints' => [
424         'EntityChanged' => [],
425       ],
426     ];
427     $entity_type = $this->setUpEntityType($definition);
428     $this->assertEquals($definition['constraints'], $entity_type->getConstraints());
429     $entity_type->addConstraint('Test');
430     $this->assertEquals($definition['constraints'] + ['Test' => NULL], $entity_type->getConstraints());
431     $entity_type->setConstraints([]);
432     $this->assertEquals([], $entity_type->getConstraints());
433   }
434
435   /**
436    * Asserts there on no public properties on the object instance.
437    *
438    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
439    */
440   protected function assertNoPublicProperties(EntityTypeInterface $entity_type) {
441     $reflection = new \ReflectionObject($entity_type);
442     $this->assertEmpty($reflection->getProperties(\ReflectionProperty::IS_PUBLIC));
443   }
444
445 }