View source
<?php
namespace Drupal\Tests\views\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\image\Entity\ImageStyle;
use Drupal\user\Entity\Role;
use Drupal\views\Entity\View;
class ViewsConfigDependenciesIntegrationTest extends ViewsKernelTestBase {
protected static $modules = [
'field',
'file',
'image',
'entity_test',
'user',
'text',
];
public static $testViews = [
'entity_test_fields',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('user');
$this
->installSchema('user', [
'users_data',
]);
}
public function testImage() {
$style = ImageStyle::create([
'name' => 'foo',
]);
$style
->save();
FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'bar',
'type' => 'image',
])
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'field_name' => 'bar',
])
->save();
$view = View::load('entity_test_fields');
$display =& $view
->getDisplay('default');
$display['display_options']['fields']['bar'] = [
'id' => 'bar',
'field' => 'bar',
'plugin_id' => 'field',
'table' => 'entity_test__bar',
'entity_type' => 'entity_test',
'entity_field' => 'bar',
'type' => 'image',
'settings' => [
'image_style' => 'foo',
'image_link' => '',
],
];
$view
->save();
$dependencies = $view
->getDependencies() + [
'config' => [],
];
$this
->assertContains('image.style.foo', $dependencies['config']);
$style
->delete();
$view = View::load('entity_test_fields');
$this
->assertNotNull(View::load('entity_test_fields'));
$display = $view
->getDisplay('default');
$this
->assertFalse(isset($display['display_options']['fields']['bar']));
$this
->assertFalse($view
->status());
$dependencies = $view
->getDependencies() + [
'config' => [],
];
$this
->assertNotContains('image.style.foo', $dependencies['config']);
}
public function testConfigRemovalRole() {
$role = Role::create([
'id' => 'dummy',
'label' => 'dummy',
]);
$role
->save();
$view = View::load('entity_test_fields');
$display =& $view
->getDisplay('default');
$display['display_options']['access'] = [
'type' => 'role',
'options' => [
'role' => [
$role
->id() => $role
->id(),
],
],
];
$view
->save();
$dependencies = $view
->getDependencies() + [
'config' => [],
];
$this
->assertContains('user.role.dummy', $dependencies['config']);
$role
->delete();
$view = View::load('entity_test_fields');
$this
->assertNull($view);
}
public function testConfigRemovalBaseTable() {
$entities = \Drupal::entityTypeManager()
->getDefinitions();
foreach ($entities as $entity_type_id => $definition) {
if ($definition
->getProvider() == 'entity_test') {
$this
->installEntitySchema($entity_type_id);
}
}
$this
->assertNotNull(View::load('entity_test_fields'));
$this->container
->get('module_installer')
->uninstall([
'entity_test',
]);
$this
->assertNull(View::load('entity_test_fields'));
}
}