View source
<?php
namespace Drupal\Tests\entity\Kernel;
use Drupal\entity_module_bundle_plugin_test\Entity\EntityTestBundlePlugin;
use Drupal\KernelTests\KernelTestBase;
class BundlePluginTest extends KernelTestBase {
public static $modules = [
'system',
'entity',
];
protected function setUp() {
parent::setUp();
$module_installer = $this->container
->get('module_installer');
$module_installer
->install([
'entity_module_bundle_plugin_test',
'entity_module_bundle_plugin_examples_test',
]);
}
public function testPluginBundles() {
$bundled_entity_types = entity_get_bundle_plugin_entity_types();
$entity_type = $bundled_entity_types['entity_test_bundle_plugin'];
$this
->assertEquals('entity_test_bundle_plugin', $entity_type
->id());
$this
->assertTrue($entity_type
->hasHandlerClass('bundle_plugin'));
$entity_type_bundle_info = $this->container
->get('entity_type.bundle.info');
$bundle_info = $entity_type_bundle_info
->getBundleInfo('entity_test_bundle_plugin');
$this
->assertEquals(2, count($bundle_info));
$this
->assertArrayHasKey('first', $bundle_info);
$this
->assertArrayHasKey('second', $bundle_info);
$this
->assertEquals('First', $bundle_info['first']['label']);
$this
->assertEquals('Some description', $bundle_info['first']['description']);
$entity_field_manager = $this->container
->get('entity_field.manager');
$field_storage_definitions = $entity_field_manager
->getFieldStorageDefinitions('entity_test_bundle_plugin');
$this
->assertArrayHasKey('first_mail', $field_storage_definitions);
$this
->assertArrayHasKey('second_mail', $field_storage_definitions);
$first_field_definitions = $entity_field_manager
->getFieldDefinitions('entity_test_bundle_plugin', 'first');
$this
->assertArrayHasKey('first_mail', $first_field_definitions);
$this
->assertArrayNotHasKey('second_mail', $first_field_definitions);
$second_field_definitions = $entity_field_manager
->getFieldDefinitions('entity_test_bundle_plugin', 'second');
$this
->assertArrayNotHasKey('first_mail', $second_field_definitions);
$this
->assertArrayHasKey('second_mail', $second_field_definitions);
$first_entity = EntityTestBundlePlugin::create([
'type' => 'first',
'first_mail' => 'admin@test.com',
]);
$first_entity
->save();
$first_entity = EntityTestBundlePlugin::load($first_entity
->id());
$this
->assertEquals('admin@test.com', $first_entity->first_mail->value);
$second_entity = EntityTestBundlePlugin::create([
'type' => 'second',
'second_mail' => 'admin@example.com',
]);
$second_entity
->save();
$second_entity = EntityTestBundlePlugin::load($second_entity
->id());
$this
->assertEquals('admin@example.com', $second_entity->second_mail->value);
$result = $this->container
->get('entity_type.manager')
->getStorage('entity_test_bundle_plugin')
->getQuery()
->condition('second_mail', 'admin@example.com')
->execute();
$this
->assertEquals([
$second_entity
->id() => $second_entity
->id(),
], $result);
$result = $this->container
->get('entity_type.manager')
->getStorage('entity_test_bundle_plugin')
->getQuery()
->condition('type', 'first')
->execute();
$this
->assertEquals([
$first_entity
->id() => $first_entity
->id(),
], $result);
}
public function testBundlePluginModuleUninstallation() {
$module_installer = $this->container
->get('module_installer');
$violations = $module_installer
->validateUninstall([
'entity_module_bundle_plugin_examples_test',
]);
$this
->assertEmpty($violations);
$first_entity = EntityTestBundlePlugin::create([
'type' => 'first',
'first_mail' => 'admin@test.com',
]);
$first_entity
->save();
$second_entity = EntityTestBundlePlugin::create([
'type' => 'second',
'second_mail' => 'admin@example.com',
]);
$second_entity
->save();
$violations = $module_installer
->validateUninstall([
'entity_module_bundle_plugin_examples_test',
]);
$this
->assertCount(1, $violations);
$this
->assertCount(1, $violations['entity_module_bundle_plugin_examples_test']);
$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]);
$second_entity
->delete();
$violations = $module_installer
->validateUninstall([
'entity_module_bundle_plugin_examples_test',
]);
$this
->assertEmpty($violations);
$module_installer
->uninstall([
'entity_module_bundle_plugin_examples_test',
]);
$violations = $module_installer
->validateUninstall([
'entity_module_bundle_plugin_test',
]);
$this
->assertNotEmpty($violations);
$first_entity
->delete();
$violations = $module_installer
->validateUninstall([
'entity_module_bundle_plugin_test',
]);
$this
->assertEmpty($violations);
}
}