annotate core/modules/views/tests/src/Kernel/ViewsConfigDependenciesIntegrationTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\views\Kernel;
Chris@0 4
Chris@0 5 use Drupal\field\Entity\FieldConfig;
Chris@0 6 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 7 use Drupal\image\Entity\ImageStyle;
Chris@0 8 use Drupal\user\Entity\Role;
Chris@0 9 use Drupal\views\Entity\View;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Tests integration of views with other modules.
Chris@0 13 *
Chris@0 14 * @group views
Chris@0 15 */
Chris@0 16 class ViewsConfigDependenciesIntegrationTest extends ViewsKernelTestBase {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * {@inheritdoc}
Chris@0 20 */
Chris@0 21 public static $modules = ['field', 'file', 'image', 'entity_test', 'user', 'text'];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * {@inheritdoc}
Chris@0 25 */
Chris@0 26 public static $testViews = ['entity_test_fields'];
Chris@0 27
Chris@0 28 /**
Chris@0 29 * {@inheritdoc}
Chris@0 30 */
Chris@0 31 protected function setUp($import_test_views = TRUE) {
Chris@0 32 parent::setUp($import_test_views);
Chris@0 33
Chris@0 34 $this->installEntitySchema('user');
Chris@0 35 $this->installSchema('user', ['users_data']);
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Tests integration with image module.
Chris@0 40 */
Chris@0 41 public function testImage() {
Chris@0 42 /** @var \Drupal\image\ImageStyleInterface $style */
Chris@0 43 $style = ImageStyle::create(['name' => 'foo']);
Chris@0 44 $style->save();
Chris@0 45
Chris@0 46 // Create a new image field 'bar' to be used in 'entity_test_fields' view.
Chris@0 47 FieldStorageConfig::create([
Chris@0 48 'entity_type' => 'entity_test',
Chris@0 49 'field_name' => 'bar',
Chris@0 50 'type' => 'image',
Chris@0 51 ])->save();
Chris@0 52 FieldConfig::create([
Chris@0 53 'entity_type' => 'entity_test',
Chris@0 54 'bundle' => 'entity_test',
Chris@0 55 'field_name' => 'bar',
Chris@0 56 ])->save();
Chris@0 57
Chris@0 58 /** @var \Drupal\views\ViewEntityInterface $view */
Chris@0 59 $view = View::load('entity_test_fields');
Chris@0 60 $display =& $view->getDisplay('default');
Chris@0 61
Chris@0 62 // Add the 'bar' image field to 'entity_test_fields' view.
Chris@0 63 $display['display_options']['fields']['bar'] = [
Chris@0 64 'id' => 'bar',
Chris@0 65 'field' => 'bar',
Chris@0 66 'plugin_id' => 'field',
Chris@0 67 'table' => 'entity_test__bar',
Chris@0 68 'entity_type' => 'entity_test',
Chris@0 69 'entity_field' => 'bar',
Chris@0 70 'type' => 'image',
Chris@0 71 'settings' => ['image_style' => 'foo', 'image_link' => ''],
Chris@0 72 ];
Chris@0 73 $view->save();
Chris@0 74
Chris@0 75 $dependencies = $view->getDependencies() + ['config' => []];
Chris@0 76
Chris@0 77 // Checks that style 'foo' is a dependency of view 'entity_test_fields'.
Chris@0 78 $this->assertTrue(in_array('image.style.foo', $dependencies['config']));
Chris@0 79
Chris@0 80 // Delete the 'foo' image style.
Chris@0 81 $style->delete();
Chris@0 82
Chris@0 83 $view = View::load('entity_test_fields');
Chris@0 84
Chris@0 85 // Checks that the view has not been deleted too.
Chris@0 86 $this->assertNotNull(View::load('entity_test_fields'));
Chris@0 87
Chris@0 88 // Checks that the image field was removed from the View.
Chris@0 89 $display = $view->getDisplay('default');
Chris@0 90 $this->assertFalse(isset($display['display_options']['fields']['bar']));
Chris@0 91
Chris@0 92 // Checks that the view has been disabled.
Chris@0 93 $this->assertFalse($view->status());
Chris@0 94
Chris@0 95 $dependencies = $view->getDependencies() + ['config' => []];
Chris@0 96 // Checks that the dependency on style 'foo' has been removed.
Chris@0 97 $this->assertFalse(in_array('image.style.foo', $dependencies['config']));
Chris@0 98 }
Chris@0 99
Chris@0 100 /**
Chris@0 101 * Tests removing a config dependency that deletes the View.
Chris@0 102 */
Chris@0 103 public function testConfigRemovalRole() {
Chris@0 104 // Create a role we can add to the View and delete.
Chris@0 105 $role = Role::create([
Chris@0 106 'id' => 'dummy',
Chris@0 107 'label' => 'dummy',
Chris@0 108 ]);
Chris@0 109
Chris@0 110 $role->save();
Chris@0 111
Chris@0 112 /** @var \Drupal\views\ViewEntityInterface $view */
Chris@0 113 $view = View::load('entity_test_fields');
Chris@0 114 $display = &$view->getDisplay('default');
Chris@0 115
Chris@0 116 // Set the access to be restricted by the dummy role.
Chris@0 117 $display['display_options']['access'] = [
Chris@0 118 'type' => 'role',
Chris@0 119 'options' => [
Chris@0 120 'role' => [
Chris@0 121 $role->id() => $role->id(),
Chris@0 122 ],
Chris@0 123 ],
Chris@0 124 ];
Chris@0 125 $view->save();
Chris@0 126
Chris@0 127 // Check that the View now has a dependency on the Role.
Chris@0 128 $dependencies = $view->getDependencies() + ['config' => []];
Chris@0 129 $this->assertTrue(in_array('user.role.dummy', $dependencies['config']));
Chris@0 130
Chris@0 131 // Delete the role.
Chris@0 132 $role->delete();
Chris@0 133
Chris@0 134 $view = View::load('entity_test_fields');
Chris@0 135
Chris@0 136 // Checks that the view has been deleted too.
Chris@0 137 $this->assertNull($view);
Chris@0 138 }
Chris@0 139
Chris@0 140 /**
Chris@0 141 * Tests uninstalling a module that provides a base table for a View.
Chris@0 142 */
Chris@0 143 public function testConfigRemovalBaseTable() {
Chris@0 144 // Find all the entity types provided by the entity_test module and install
Chris@0 145 // the schema for them so we can uninstall them.
Chris@0 146 $entities = \Drupal::entityTypeManager()->getDefinitions();
Chris@0 147 foreach ($entities as $entity_type_id => $definition) {
Chris@0 148 if ($definition->getProvider() == 'entity_test') {
Chris@0 149 $this->installEntitySchema($entity_type_id);
Chris@0 150 };
Chris@0 151 }
Chris@0 152
Chris@0 153 // Check that removing the module that provides the base table for a View,
Chris@0 154 // deletes the View.
Chris@0 155 $this->assertNotNull(View::load('entity_test_fields'));
Chris@0 156 $this->container->get('module_installer')->uninstall(['entity_test']);
Chris@0 157 // Check that the View has been deleted.
Chris@0 158 $this->assertNull(View::load('entity_test_fields'));
Chris@0 159 }
Chris@0 160
Chris@0 161 }