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]);
62 'default_langcode' => 'default_langcode',
63 'revision_translation_affected' => 'revision_translation_affected',
65 $this->assertSame($expected, $entity_type->getKeys());
69 * Tests the getKey() method.
71 * @dataProvider providerTestGetKeys
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'));
80 * Tests the hasKey() method.
82 * @dataProvider providerTestGetKeys
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'));
92 * Provides test data for testGet.
94 public function providerTestGet() {
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'],
106 * Provides test data for testSet.
108 public function providerTestSet() {
112 ['provider', 'test'],
113 ['something_additional', NULL],
114 ['something_additional', ''],
115 ['something_additional', 'additional'],
120 * Provides test data.
122 public function providerTestGetKeys() {
124 [[], ['revision' => '', 'bundle' => '', 'langcode' => '']],
125 [['id' => 'id'], ['id' => 'id', 'revision' => '', 'bundle' => '', 'langcode' => '']],
126 [['bundle' => 'bundle'], ['bundle' => 'bundle', 'revision' => '', 'langcode' => '']],
131 * Tests the isInternal() method.
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());
143 * Tests the isRevisionable() method.
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());
155 * Tests the getHandler() method.
157 public function testGetHandler() {
158 $controller = $this->getTestHandlerClass();
159 $entity_type = $this->setUpEntityType([
161 'storage' => $controller,
163 'default' => $controller,
167 $this->assertSame($controller, $entity_type->getHandlerClass('storage'));
168 $this->assertSame($controller, $entity_type->getHandlerClass('form', 'default'));
172 * Tests the getStorageClass() method.
174 public function testGetStorageClass() {
175 $controller = $this->getTestHandlerClass();
176 $entity_type = $this->setUpEntityType([
178 'storage' => $controller,
181 $this->assertSame($controller, $entity_type->getStorageClass());
185 * Tests the setStorageClass() method.
187 public function testSetStorageClass() {
188 $controller = $this->getTestHandlerClass();
189 $entity_type = $this->setUpEntityType([]);
190 $this->assertSame($entity_type, $entity_type->setStorageClass($controller));
194 * Tests the getListBuilderClass() method.
196 public function testGetListBuilderClass() {
197 $controller = $this->getTestHandlerClass();
198 $entity_type = $this->setUpEntityType([
200 'list_builder' => $controller,
203 $this->assertSame($controller, $entity_type->getListBuilderClass());
207 * Tests the getAccessControlClass() method.
209 public function testGetAccessControlClass() {
210 $controller = $this->getTestHandlerClass();
211 $entity_type = $this->setUpEntityType([
213 'access' => $controller,
216 $this->assertSame($controller, $entity_type->getAccessControlClass());
220 * Tests the getFormClass() method.
222 public function testGetFormClass() {
223 $controller = $this->getTestHandlerClass();
224 $operation = 'default';
225 $entity_type = $this->setUpEntityType([
228 $operation => $controller,
232 $this->assertSame($controller, $entity_type->getFormClass($operation));
236 * Tests the hasFormClasses() method.
238 public function testHasFormClasses() {
239 $controller = $this->getTestHandlerClass();
240 $operation = 'default';
241 $entity_type1 = $this->setUpEntityType([
244 $operation => $controller,
248 $entity_type2 = $this->setUpEntityType([
251 $this->assertTrue($entity_type1->hasFormClasses());
252 $this->assertFalse($entity_type2->hasFormClasses());
256 * Tests the getViewBuilderClass() method.
258 public function testGetViewBuilderClass() {
259 $controller = $this->getTestHandlerClass();
260 $entity_type = $this->setUpEntityType([
262 'view_builder' => $controller,
265 $this->assertSame($controller, $entity_type->getViewBuilderClass());
269 * @covers ::__construct
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]);
279 * @covers ::getOriginalClass
281 public function testgetOriginalClassUnchanged() {
282 $class = $this->randomMachineName();
283 $entity_type = $this->setUpEntityType(['class' => $class]);
284 $this->assertEquals($class, $entity_type->getOriginalClass());
289 * @covers ::getOriginalClass
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());
301 public function testId() {
302 $id = $this->randomMachineName(32);
303 $entity_type = $this->setUpEntityType(['id' => $id]);
304 $this->assertEquals($id, $entity_type->id());
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());
315 $label = $this->randomMachineName();
316 $entity_type = $this->setUpEntityType(['label' => $label]);
317 $this->assertSame($label, $entity_type->getLabel());
321 * @covers ::getGroupLabel
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());
328 $default_label = $this->randomMachineName();
329 $entity_type = $this->setUpEntityType(['group_label' => $default_label]);
330 $this->assertSame($default_label, $entity_type->getGroupLabel());
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());
338 * @covers ::getCollectionLabel
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());
348 * @covers ::getSingularLabel
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());
358 * @covers ::getSingularLabel
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());
367 * @covers ::getPluralLabel
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());
377 * @covers ::getPluralLabel
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());
386 * @covers ::getCountLabel
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));
397 * @covers ::getCountLabel
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));
408 * Tests the ::getBundleLabel() method.
410 * @covers ::getBundleLabel
411 * @dataProvider providerTestGetBundleLabel
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());
420 * Provides test data for ::testGetBundleLabel().
422 public function providerTestGetBundleLabel() {
424 [['label' => 'Entity Label Foo'], 'Entity Label Foo bundle'],
425 [['bundle_label' => 'Bundle Label Bar'], 'Bundle Label Bar'],
430 * Gets a mock controller class name.
433 * A mock controller class name.
435 protected function getTestHandlerClass() {
436 return get_class($this->getMockForAbstractClass('Drupal\Core\Entity\EntityHandlerBase'));
440 * @covers ::setLinkTemplate
442 public function testSetLinkTemplateWithInvalidPath() {
443 $entity_type = $this->setUpEntityType(['id' => $this->randomMachineName()]);
444 $this->setExpectedException(\InvalidArgumentException::class);
445 $entity_type->setLinkTemplate('test', 'invalid-path');
449 * Tests the constraint methods.
451 * @covers ::getConstraints
452 * @covers ::setConstraints
453 * @covers ::addConstraint
455 public function testConstraintMethods() {
458 'EntityChanged' => [],
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());
470 * Asserts there on no public properties on the object instance.
472 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
474 protected function assertNoPublicProperties(EntityTypeInterface $entity_type) {
475 $reflection = new \ReflectionObject($entity_type);
476 $this->assertEmpty($reflection->getProperties(\ReflectionProperty::IS_PUBLIC));