FarmEntityBundleFieldTest.php in farmOS 2.x
File
modules/core/entity/tests/src/Functional/FarmEntityBundleFieldTest.php
View source
<?php
namespace Drupal\Tests\farm_entity\Functional;
use Drupal\Tests\farm_test\Functional\FarmBrowserTestBase;
class FarmEntityBundleFieldTest extends FarmBrowserTestBase {
protected $entityFieldManager;
protected $entityTypeManager;
protected $database;
protected $moduleInstaller;
protected static $modules = [
'farm_entity',
'farm_entity_test',
'farm_entity_bundle_fields_test',
];
protected function setUp() : void {
parent::setUp();
$this->entityFieldManager = $this->container
->get('entity_field.manager');
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->database = $this->container
->get('database');
$this->moduleInstaller = $this->container
->get('module_installer');
}
public function testBundleFieldPostponedInstall() {
$result = $this->moduleInstaller
->install([
'farm_entity_contrib_test',
], TRUE);
$this
->assertTrue($result);
$this->entityFieldManager
->clearCachedFieldDefinitions();
$fields = $this->entityFieldManager
->getFieldDefinitions('log', 'test');
$this
->assertArrayHasKey('test_contrib_hook_bundle_field', $fields);
$this
->assertFieldStorageDefinitionExists('log', 'test_contrib_hook_bundle_field');
$installed_contrib_field_storage_definition = $this->entityFieldManager
->getFieldStorageDefinitions('log')['test_contrib_hook_bundle_field'];
$result = $this->moduleInstaller
->uninstall([
'farm_entity_contrib_test',
]);
$this
->assertTrue($result);
$this->entityFieldManager
->clearCachedFieldDefinitions();
$fields = $this->entityFieldManager
->getFieldDefinitions('log', 'test');
$this
->assertArrayNotHasKey('test_contrib_hook_bundle_field', $fields);
$this
->assertFieldStorageDefinitionExists('log', 'test_contrib_hook_bundle_field', FALSE);
$table_mapping = $this->entityTypeManager
->getStorage('log')
->getTableMapping();
$table = $table_mapping
->getDedicatedDataTableName($installed_contrib_field_storage_definition);
$this
->assertFalse($this->database
->schema()
->tableExists($table));
}
public function testBundlePluginModuleUninstallation() {
$this->moduleInstaller
->uninstall([
'farm_entity_bundle_fields_test',
]);
$this->entityFieldManager
->clearCachedFieldDefinitions();
$test_fields = [
'second_plan_field' => FALSE,
'asset' => TRUE,
'log' => TRUE,
];
foreach ($test_fields as $field_name => $exists) {
$this
->assertFieldStorageDefinitionExists('plan', $field_name, $exists);
}
}
protected function assertFieldStorageDefinitionExists(string $entity_type, string $field_name, bool $exists = TRUE) {
$field_storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($entity_type);
$this
->assertEquals($exists, array_key_exists($field_name, $field_storage_definitions));
if ($exists) {
$table_mapping = $this->entityTypeManager
->getStorage($entity_type)
->getTableMapping();
$table = $table_mapping
->getDedicatedDataTableName($field_storage_definitions[$field_name]);
$this
->assertTrue($this->database
->schema()
->tableExists($table));
}
}
}