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 isRevisionable() method.
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());
143 * Tests the getHandler() method.
145 public function testGetHandler() {
146 $controller = $this->getTestHandlerClass();
147 $entity_type = $this->setUpEntityType([
149 'storage' => $controller,
151 'default' => $controller,
155 $this->assertSame($controller, $entity_type->getHandlerClass('storage'));
156 $this->assertSame($controller, $entity_type->getHandlerClass('form', 'default'));
160 * Tests the getStorageClass() method.
162 public function testGetStorageClass() {
163 $controller = $this->getTestHandlerClass();
164 $entity_type = $this->setUpEntityType([
166 'storage' => $controller,
169 $this->assertSame($controller, $entity_type->getStorageClass());
173 * Tests the setStorageClass() method.
175 public function testSetStorageClass() {
176 $controller = $this->getTestHandlerClass();
177 $entity_type = $this->setUpEntityType([]);
178 $this->assertSame($entity_type, $entity_type->setStorageClass($controller));
182 * Tests the getListBuilderClass() method.
184 public function testGetListBuilderClass() {
185 $controller = $this->getTestHandlerClass();
186 $entity_type = $this->setUpEntityType([
188 'list_builder' => $controller,
191 $this->assertSame($controller, $entity_type->getListBuilderClass());
195 * Tests the getAccessControlClass() method.
197 public function testGetAccessControlClass() {
198 $controller = $this->getTestHandlerClass();
199 $entity_type = $this->setUpEntityType([
201 'access' => $controller,
204 $this->assertSame($controller, $entity_type->getAccessControlClass());
208 * Tests the getFormClass() method.
210 public function testGetFormClass() {
211 $controller = $this->getTestHandlerClass();
212 $operation = 'default';
213 $entity_type = $this->setUpEntityType([
216 $operation => $controller,
220 $this->assertSame($controller, $entity_type->getFormClass($operation));
224 * Tests the hasFormClasses() method.
226 public function testHasFormClasses() {
227 $controller = $this->getTestHandlerClass();
228 $operation = 'default';
229 $entity_type1 = $this->setUpEntityType([
232 $operation => $controller,
236 $entity_type2 = $this->setUpEntityType([
239 $this->assertTrue($entity_type1->hasFormClasses());
240 $this->assertFalse($entity_type2->hasFormClasses());
244 * Tests the getViewBuilderClass() method.
246 public function testGetViewBuilderClass() {
247 $controller = $this->getTestHandlerClass();
248 $entity_type = $this->setUpEntityType([
250 'view_builder' => $controller,
253 $this->assertSame($controller, $entity_type->getViewBuilderClass());
257 * @covers ::__construct
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]);
267 * @covers ::getOriginalClass
269 public function testgetOriginalClassUnchanged() {
270 $class = $this->randomMachineName();
271 $entity_type = $this->setUpEntityType(['class' => $class]);
272 $this->assertEquals($class, $entity_type->getOriginalClass());
277 * @covers ::getOriginalClass
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());
289 public function testId() {
290 $id = $this->randomMachineName(32);
291 $entity_type = $this->setUpEntityType(['id' => $id]);
292 $this->assertEquals($id, $entity_type->id());
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());
303 $label = $this->randomMachineName();
304 $entity_type = $this->setUpEntityType(['label' => $label]);
305 $this->assertSame($label, $entity_type->getLabel());
309 * @covers ::getGroupLabel
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());
316 $default_label = $this->randomMachineName();
317 $entity_type = $this->setUpEntityType(['group_label' => $default_label]);
318 $this->assertSame($default_label, $entity_type->getGroupLabel());
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());
326 * @covers ::getCollectionLabel
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());
336 * @covers ::getSingularLabel
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());
346 * @covers ::getSingularLabel
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());
355 * @covers ::getPluralLabel
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());
365 * @covers ::getPluralLabel
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());
374 * @covers ::getCountLabel
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));
385 * @covers ::getCountLabel
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));
396 * Gets a mock controller class name.
399 * A mock controller class name.
401 protected function getTestHandlerClass() {
402 return get_class($this->getMockForAbstractClass('Drupal\Core\Entity\EntityHandlerBase'));
406 * @covers ::setLinkTemplate
408 public function testSetLinkTemplateWithInvalidPath() {
409 $entity_type = $this->setUpEntityType(['id' => $this->randomMachineName()]);
410 $this->setExpectedException(\InvalidArgumentException::class);
411 $entity_type->setLinkTemplate('test', 'invalid-path');
415 * Tests the constraint methods.
417 * @covers ::getConstraints
418 * @covers ::setConstraints
419 * @covers ::addConstraint
421 public function testConstraintMethods() {
424 'EntityChanged' => [],
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());
436 * Asserts there on no public properties on the object instance.
438 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
440 protected function assertNoPublicProperties(EntityTypeInterface $entity_type) {
441 $reflection = new \ReflectionObject($entity_type);
442 $this->assertEmpty($reflection->getProperties(\ReflectionProperty::IS_PUBLIC));