3 namespace Drupal\Tests\Core\Entity;
5 use Drupal\Core\Entity\EntityType;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\StringTranslation\TranslatableMarkup;
8 use Drupal\Tests\UnitTestCase;
11 * @coversDefaultClass \Drupal\Core\Entity\EntityType
14 class EntityTypeTest extends UnitTestCase {
17 * Sets up an EntityType object for a given set of values.
19 * @param array $definition
20 * An array of values to use for the EntityType.
22 * @return \Drupal\Core\Entity\EntityTypeInterface
24 protected function setUpEntityType($definition) {
26 'id' => 'example_entity_type',
28 return new EntityType($definition);
34 * @dataProvider providerTestGet
36 public function testGet(array $defintion, $key, $expected) {
37 $entity_type = $this->setUpEntityType($defintion);
38 $this->assertSame($expected, $entity_type->get($key));
45 * @dataProvider providerTestSet
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);
55 * Tests the getKeys() method.
57 * @dataProvider providerTestGetKeys
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());
65 * Tests the getKey() method.
67 * @dataProvider providerTestGetKeys
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'));
76 * Tests the hasKey() method.
78 * @dataProvider providerTestGetKeys
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'));
88 * Provides test data for testGet.
90 public function providerTestGet() {
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'],
102 * Provides test data for testSet.
104 public function providerTestSet() {
108 ['provider', 'test'],
109 ['something_additional', NULL],
110 ['something_additional', ''],
111 ['something_additional', 'additional'],
116 * Provides test data.
118 public function providerTestGetKeys() {
120 [[], ['revision' => '', 'bundle' => '', 'langcode' => '']],
121 [['id' => 'id'], ['id' => 'id', 'revision' => '', 'bundle' => '', 'langcode' => '']],
122 [['bundle' => 'bundle'], ['bundle' => 'bundle', 'revision' => '', 'langcode' => '']],
127 * Tests the isRevisionable() method.
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());
139 * Tests the getHandler() method.
141 public function testGetHandler() {
142 $controller = $this->getTestHandlerClass();
143 $entity_type = $this->setUpEntityType([
145 'storage' => $controller,
147 'default' => $controller,
151 $this->assertSame($controller, $entity_type->getHandlerClass('storage'));
152 $this->assertSame($controller, $entity_type->getHandlerClass('form', 'default'));
156 * Tests the getStorageClass() method.
158 public function testGetStorageClass() {
159 $controller = $this->getTestHandlerClass();
160 $entity_type = $this->setUpEntityType([
162 'storage' => $controller,
165 $this->assertSame($controller, $entity_type->getStorageClass());
169 * Tests the setStorageClass() method.
171 public function testSetStorageClass() {
172 $controller = $this->getTestHandlerClass();
173 $entity_type = $this->setUpEntityType([]);
174 $this->assertSame($entity_type, $entity_type->setStorageClass($controller));
178 * Tests the getListBuilderClass() method.
180 public function testGetListBuilderClass() {
181 $controller = $this->getTestHandlerClass();
182 $entity_type = $this->setUpEntityType([
184 'list_builder' => $controller,
187 $this->assertSame($controller, $entity_type->getListBuilderClass());
191 * Tests the getAccessControlClass() method.
193 public function testGetAccessControlClass() {
194 $controller = $this->getTestHandlerClass();
195 $entity_type = $this->setUpEntityType([
197 'access' => $controller,
200 $this->assertSame($controller, $entity_type->getAccessControlClass());
204 * Tests the getFormClass() method.
206 public function testGetFormClass() {
207 $controller = $this->getTestHandlerClass();
208 $operation = 'default';
209 $entity_type = $this->setUpEntityType([
212 $operation => $controller,
216 $this->assertSame($controller, $entity_type->getFormClass($operation));
220 * Tests the hasFormClasses() method.
222 public function testHasFormClasses() {
223 $controller = $this->getTestHandlerClass();
224 $operation = 'default';
225 $entity_type1 = $this->setUpEntityType([
228 $operation => $controller,
232 $entity_type2 = $this->setUpEntityType([
235 $this->assertTrue($entity_type1->hasFormClasses());
236 $this->assertFalse($entity_type2->hasFormClasses());
240 * Tests the getViewBuilderClass() method.
242 public function testGetViewBuilderClass() {
243 $controller = $this->getTestHandlerClass();
244 $entity_type = $this->setUpEntityType([
246 'view_builder' => $controller,
249 $this->assertSame($controller, $entity_type->getViewBuilderClass());
253 * @covers ::__construct
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]);
263 * @covers ::getOriginalClass
265 public function testgetOriginalClassUnchanged() {
266 $class = $this->randomMachineName();
267 $entity_type = $this->setUpEntityType(['class' => $class]);
268 $this->assertEquals($class, $entity_type->getOriginalClass());
273 * @covers ::getOriginalClass
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());
285 public function testId() {
286 $id = $this->randomMachineName(32);
287 $entity_type = $this->setUpEntityType(['id' => $id]);
288 $this->assertEquals($id, $entity_type->id());
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());
299 $label = $this->randomMachineName();
300 $entity_type = $this->setUpEntityType(['label' => $label]);
301 $this->assertSame($label, $entity_type->getLabel());
305 * @covers ::getGroupLabel
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());
312 $default_label = $this->randomMachineName();
313 $entity_type = $this->setUpEntityType(['group_label' => $default_label]);
314 $this->assertSame($default_label, $entity_type->getGroupLabel());
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());
322 * @covers ::getCollectionLabel
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());
332 * @covers ::getSingularLabel
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());
342 * @covers ::getSingularLabel
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());
351 * @covers ::getPluralLabel
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());
361 * @covers ::getPluralLabel
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());
370 * @covers ::getCountLabel
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));
381 * @covers ::getCountLabel
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));
392 * Gets a mock controller class name.
395 * A mock controller class name.
397 protected function getTestHandlerClass() {
398 return get_class($this->getMockForAbstractClass('Drupal\Core\Entity\EntityHandlerBase'));
402 * @covers ::setLinkTemplate
404 public function testSetLinkTemplateWithInvalidPath() {
405 $entity_type = $this->setUpEntityType(['id' => $this->randomMachineName()]);
406 $this->setExpectedException(\InvalidArgumentException::class);
407 $entity_type->setLinkTemplate('test', 'invalid-path');
411 * Tests the constraint methods.
413 * @covers ::getConstraints
414 * @covers ::setConstraints
415 * @covers ::addConstraint
417 public function testConstraintMethods() {
420 'EntityChanged' => [],
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());
432 * Asserts there on no public properties on the object instance.
434 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
436 protected function assertNoPublicProperties(EntityTypeInterface $entity_type) {
437 $reflection = new \ReflectionObject($entity_type);
438 $this->assertEmpty($reflection->getProperties(\ReflectionProperty::IS_PUBLIC));