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\Core\StringTranslation\TranslationInterface;
9 use Drupal\Tests\UnitTestCase;
12 * @coversDefaultClass \Drupal\Core\Entity\EntityType
15 class EntityTypeTest extends UnitTestCase {
18 * Sets up an EntityType object for a given set of values.
20 * @param array $definition
21 * An array of values to use for the EntityType.
23 * @return \Drupal\Core\Entity\EntityTypeInterface
25 protected function setUpEntityType($definition) {
27 'id' => 'example_entity_type',
29 return new EntityType($definition);
35 * @dataProvider providerTestGet
37 public function testGet(array $defintion, $key, $expected) {
38 $entity_type = $this->setUpEntityType($defintion);
39 $this->assertSame($expected, $entity_type->get($key));
46 * @dataProvider providerTestSet
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);
56 * Tests the getKeys() method.
58 * @dataProvider providerTestGetKeys
60 public function testGetKeys($entity_keys, $expected) {
61 $entity_type = $this->setUpEntityType(['entity_keys' => $entity_keys]);
63 'default_langcode' => 'default_langcode',
64 'revision_translation_affected' => 'revision_translation_affected',
66 $this->assertSame($expected, $entity_type->getKeys());
70 * Tests the getKey() method.
72 * @dataProvider providerTestGetKeys
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'));
81 * Tests the hasKey() method.
83 * @dataProvider providerTestGetKeys
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'));
93 * Provides test data for testGet.
95 public function providerTestGet() {
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'],
107 * Provides test data for testSet.
109 public function providerTestSet() {
113 ['provider', 'test'],
114 ['something_additional', NULL],
115 ['something_additional', ''],
116 ['something_additional', 'additional'],
121 * Provides test data.
123 public function providerTestGetKeys() {
125 [[], ['revision' => '', 'bundle' => '', 'langcode' => '']],
126 [['id' => 'id'], ['id' => 'id', 'revision' => '', 'bundle' => '', 'langcode' => '']],
127 [['bundle' => 'bundle'], ['bundle' => 'bundle', 'revision' => '', 'langcode' => '']],
132 * Tests the isInternal() method.
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());
144 * Tests the isRevisionable() method.
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());
156 * Tests the getHandler() method.
158 public function testGetHandler() {
159 $controller = $this->getTestHandlerClass();
160 $entity_type = $this->setUpEntityType([
162 'storage' => $controller,
164 'default' => $controller,
168 $this->assertSame($controller, $entity_type->getHandlerClass('storage'));
169 $this->assertSame($controller, $entity_type->getHandlerClass('form', 'default'));
173 * Tests the getStorageClass() method.
175 public function testGetStorageClass() {
176 $controller = $this->getTestHandlerClass();
177 $entity_type = $this->setUpEntityType([
179 'storage' => $controller,
182 $this->assertSame($controller, $entity_type->getStorageClass());
186 * Tests the setStorageClass() method.
188 public function testSetStorageClass() {
189 $controller = $this->getTestHandlerClass();
190 $entity_type = $this->setUpEntityType([]);
191 $this->assertSame($entity_type, $entity_type->setStorageClass($controller));
195 * Tests the getListBuilderClass() method.
197 public function testGetListBuilderClass() {
198 $controller = $this->getTestHandlerClass();
199 $entity_type = $this->setUpEntityType([
201 'list_builder' => $controller,
204 $this->assertSame($controller, $entity_type->getListBuilderClass());
208 * Tests the getAccessControlClass() method.
210 public function testGetAccessControlClass() {
211 $controller = $this->getTestHandlerClass();
212 $entity_type = $this->setUpEntityType([
214 'access' => $controller,
217 $this->assertSame($controller, $entity_type->getAccessControlClass());
221 * Tests the getFormClass() method.
223 public function testGetFormClass() {
224 $controller = $this->getTestHandlerClass();
225 $operation = 'default';
226 $entity_type = $this->setUpEntityType([
229 $operation => $controller,
233 $this->assertSame($controller, $entity_type->getFormClass($operation));
237 * Tests the hasFormClasses() method.
239 public function testHasFormClasses() {
240 $controller = $this->getTestHandlerClass();
241 $operation = 'default';
242 $entity_type1 = $this->setUpEntityType([
245 $operation => $controller,
249 $entity_type2 = $this->setUpEntityType([
252 $this->assertTrue($entity_type1->hasFormClasses());
253 $this->assertFalse($entity_type2->hasFormClasses());
257 * Tests the getViewBuilderClass() method.
259 public function testGetViewBuilderClass() {
260 $controller = $this->getTestHandlerClass();
261 $entity_type = $this->setUpEntityType([
263 'view_builder' => $controller,
266 $this->assertSame($controller, $entity_type->getViewBuilderClass());
270 * @covers ::__construct
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]);
280 * @covers ::getOriginalClass
282 public function testgetOriginalClassUnchanged() {
283 $class = $this->randomMachineName();
284 $entity_type = $this->setUpEntityType(['class' => $class]);
285 $this->assertEquals($class, $entity_type->getOriginalClass());
290 * @covers ::getOriginalClass
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());
302 public function testId() {
303 $id = $this->randomMachineName(32);
304 $entity_type = $this->setUpEntityType(['id' => $id]);
305 $this->assertEquals($id, $entity_type->id());
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());
316 $label = $this->randomMachineName();
317 $entity_type = $this->setUpEntityType(['label' => $label]);
318 $this->assertSame($label, $entity_type->getLabel());
322 * @covers ::getGroupLabel
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());
329 $default_label = $this->randomMachineName();
330 $entity_type = $this->setUpEntityType(['group_label' => $default_label]);
331 $this->assertSame($default_label, $entity_type->getGroupLabel());
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());
339 * @covers ::getCollectionLabel
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());
349 * @covers ::getSingularLabel
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());
359 * @covers ::getSingularLabel
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());
368 * @covers ::getPluralLabel
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());
378 * @covers ::getPluralLabel
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());
387 * @covers ::getCountLabel
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));
398 * @covers ::getCountLabel
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));
409 * Tests the ::getBundleLabel() method.
411 * @covers ::getBundleLabel
412 * @dataProvider providerTestGetBundleLabel
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());
421 * Provides test data for ::testGetBundleLabel().
423 public function providerTestGetBundleLabel() {
425 [['label' => 'Entity Label Foo'], 'Entity Label Foo bundle'],
426 [['bundle_label' => 'Bundle Label Bar'], 'Bundle Label Bar'],
431 * Gets a mock controller class name.
434 * A mock controller class name.
436 protected function getTestHandlerClass() {
437 return get_class($this->getMockForAbstractClass('Drupal\Core\Entity\EntityHandlerBase'));
441 * @covers ::setLinkTemplate
443 public function testSetLinkTemplateWithInvalidPath() {
444 $entity_type = $this->setUpEntityType(['id' => $this->randomMachineName()]);
445 $this->setExpectedException(\InvalidArgumentException::class);
446 $entity_type->setLinkTemplate('test', 'invalid-path');
450 * Tests the constraint methods.
452 * @covers ::getConstraints
453 * @covers ::setConstraints
454 * @covers ::addConstraint
456 public function testConstraintMethods() {
459 'EntityChanged' => [],
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());
471 * Asserts there on no public properties on the object instance.
473 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
475 protected function assertNoPublicProperties(EntityTypeInterface $entity_type) {
476 $reflection = new \ReflectionObject($entity_type);
477 $this->assertEmpty($reflection->getProperties(\ReflectionProperty::IS_PUBLIC));
481 * Tests that the EntityType object can be serialized.
483 public function testIsSerializable() {
484 $entity_type = $this->setUpEntityType([]);
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';
492 $entity_type->setStringTranslation($translation_service);
493 $entity_type = unserialize(serialize($entity_type));
495 $this->assertEquals('example_entity_type', $entity_type->id());