3 namespace Drupal\Tests\entity\Kernel;
5 use Drupal\entity_module_bundle_plugin_test\Entity\EntityTestBundlePlugin;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests the bundle plugin API.
13 class BundlePluginTest extends KernelTestBase {
18 public static $modules = [
26 protected function setUp() {
29 $this->installSchema('system', 'router');
31 // Install the modules properly. Putting them into static::$modules doesn't trigger the install
32 // hooks, like hook_modules_installed, so entity_modules_installed is not triggered().
33 /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
34 $module_installer = \Drupal::service('module_installer');
35 $module_installer->install(['entity_module_bundle_plugin_test', 'entity_module_bundle_plugin_examples_test']);
39 * Tests the bundle plugins.
41 public function testPluginBundles() {
42 $bundled_entity_types = entity_get_bundle_plugin_entity_types();
43 /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
44 $entity_type = $bundled_entity_types['entity_test_bundle_plugin'];
45 $this->assertEquals('entity_test_bundle_plugin', $entity_type->id());
46 $this->assertTrue($entity_type->hasHandlerClass('bundle_plugin'));
48 /** @var \Drupal\Core\Entity\EntityTypeBundleInfo $entity_type_bundle_info */
49 $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
50 $bundle_info = $entity_type_bundle_info->getBundleInfo('entity_test_bundle_plugin');
51 $this->assertEquals(2, count($bundle_info));
52 $this->assertArrayHasKey('first', $bundle_info);
53 $this->assertArrayHasKey('second', $bundle_info);
54 $this->assertEquals('First', $bundle_info['first']['label']);
55 $this->assertEquals('Some description', $bundle_info['first']['description']);
57 /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
58 $entity_field_manager = \Drupal::service('entity_field.manager');
59 $field_storage_definitions = $entity_field_manager->getFieldStorageDefinitions('entity_test_bundle_plugin');
60 $this->assertArrayHasKey('first_mail', $field_storage_definitions);
61 $this->assertArrayHasKey('second_mail', $field_storage_definitions);
62 $first_field_definitions = $entity_field_manager->getFieldDefinitions('entity_test_bundle_plugin', 'first');
63 $this->assertArrayHasKey('first_mail', $first_field_definitions);
64 $this->assertArrayNotHasKey('second_mail', $first_field_definitions);
65 $second_field_definitions = $entity_field_manager->getFieldDefinitions('entity_test_bundle_plugin', 'second');
66 $this->assertArrayNotHasKey('first_mail', $second_field_definitions);
67 $this->assertArrayHasKey('second_mail', $second_field_definitions);
69 $first_entity = EntityTestBundlePlugin::create([
71 'first_mail' => 'admin@test.com',
73 $first_entity->save();
74 $first_entity = EntityTestBundlePlugin::load($first_entity->id());
75 $this->assertEquals('admin@test.com', $first_entity->first_mail->value);
77 $second_entity = EntityTestBundlePlugin::create([
79 'second_mail' => 'admin@example.com',
81 $second_entity->save();
82 $second_entity = EntityTestBundlePlugin::load($second_entity->id());
83 $this->assertEquals('admin@example.com', $second_entity->second_mail->value);
85 // Also test entity queries.
86 $result = \Drupal::entityTypeManager()->getStorage('entity_test_bundle_plugin')
88 ->condition('second_mail', 'admin@example.com')
90 $this->assertEquals([$second_entity->id() => $second_entity->id()], $result);
92 $result = \Drupal::entityTypeManager()->getStorage('entity_test_bundle_plugin')
94 ->condition('type', 'first')
96 $this->assertEquals([$first_entity->id() => $first_entity->id()], $result);
101 * Tests the uninstallation for a bundle provided by a module.
103 public function testBundlePluginModuleUninstallation() {
104 /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
105 $module_installer = \Drupal::service('module_installer');
107 // One should be possible to uninstall without any actual content.
108 $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_examples_test']);
109 $this->assertEmpty($violations);
111 $first_entity = EntityTestBundlePlugin::create([
113 'first_mail' => 'admin@test.com',
115 $first_entity->save();
116 $second_entity = EntityTestBundlePlugin::create([
118 'second_mail' => 'admin@example.com',
120 $second_entity->save();
122 $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_examples_test']);
123 $this->assertCount(1, $violations);
124 $this->assertCount(1, $violations['entity_module_bundle_plugin_examples_test']);
125 $this->assertEquals('There is data for the bundle Second on the entity type Entity test bundle plugin. Please remove all content before uninstalling the module.', $violations['entity_module_bundle_plugin_examples_test'][0]);
127 $second_entity->delete();
129 // The first entity is defined by entity_module_bundle_plugin_test, so it should be possible
130 // to uninstall the module providing the second bundle plugin.
131 $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_examples_test']);
132 $this->assertEmpty($violations);
134 $module_installer->uninstall(['entity_module_bundle_plugin_examples_test']);
136 // The first entity is provided by entity_module_bundle_plugin_test which we cannot uninstall,
137 // until all the entities are deleted.
138 $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_test']);
139 $this->assertNotEmpty($violations);
141 $first_entity->delete();
142 $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_test']);
143 $this->assertEmpty($violations);