Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\simpletest\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Database\Database;
|
Chris@0
|
6 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
7 use Drupal\simpletest\KernelTestBase;
|
Chris@0
|
8 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
9 use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Tests KernelTestBase functionality.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group simpletest
|
Chris@0
|
15 */
|
Chris@0
|
16 class KernelTestBaseTest extends KernelTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Modules to enable.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var array
|
Chris@0
|
22 */
|
Chris@0
|
23 public static $modules = ['entity_test'];
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * {@inheritdoc}
|
Chris@0
|
27 */
|
Chris@0
|
28 protected function setUp() {
|
Chris@0
|
29 $php = <<<'EOS'
|
Chris@0
|
30 <?php
|
Chris@0
|
31 # Make sure that the $test_class variable is defined when this file is included.
|
Chris@0
|
32 if ($test_class) {
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 # Define a function to be able to check that this file was loaded with
|
Chris@0
|
36 # function_exists().
|
Chris@0
|
37 if (!function_exists('simpletest_test_stub_settings_function')) {
|
Chris@0
|
38 function simpletest_test_stub_settings_function() {}
|
Chris@0
|
39 }
|
Chris@0
|
40 EOS;
|
Chris@0
|
41
|
Chris@0
|
42 $settings_testing_file = $this->siteDirectory . '/settings.testing.php';
|
Chris@0
|
43 file_put_contents($settings_testing_file, $php);
|
Chris@0
|
44
|
Chris@0
|
45 $original_container = $this->originalContainer;
|
Chris@0
|
46 parent::setUp();
|
Chris@0
|
47 $this->assertNotIdentical(\Drupal::getContainer(), $original_container, 'KernelTestBase test creates a new container.');
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Tests expected behavior of setUp().
|
Chris@0
|
52 */
|
Chris@0
|
53 public function testSetUp() {
|
Chris@0
|
54 $modules = ['entity_test'];
|
Chris@0
|
55 $table = 'entity_test';
|
Chris@0
|
56
|
Chris@0
|
57 // Verify that specified $modules have been loaded.
|
Chris@0
|
58 $this->assertTrue(function_exists('entity_test_entity_bundle_info'), 'entity_test.module was loaded.');
|
Chris@0
|
59 // Verify that there is a fixed module list.
|
Chris@0
|
60 $this->assertIdentical(array_keys(\Drupal::moduleHandler()->getModuleList()), $modules);
|
Chris@0
|
61 $this->assertIdentical(\Drupal::moduleHandler()->getImplementations('entity_bundle_info'), ['entity_test']);
|
Chris@0
|
62 $this->assertIdentical(\Drupal::moduleHandler()->getImplementations('entity_type_alter'), ['entity_test']);
|
Chris@0
|
63
|
Chris@0
|
64 // Verify that no modules have been installed.
|
Chris@17
|
65 $this->assertFalse(Database::getConnection()->schema()->tableExists($table), "'$table' database table not found.");
|
Chris@0
|
66
|
Chris@0
|
67 // Verify that the settings.testing.php got taken into account.
|
Chris@0
|
68 $this->assertTrue(function_exists('simpletest_test_stub_settings_function'));
|
Chris@0
|
69
|
Chris@0
|
70 // Ensure that the database tasks have been run during set up. Neither MySQL
|
Chris@0
|
71 // nor SQLite make changes that are testable.
|
Chris@0
|
72 $database = $this->container->get('database');
|
Chris@0
|
73 if ($database->driver() == 'pgsql') {
|
Chris@0
|
74 $this->assertEqual('on', $database->query("SHOW standard_conforming_strings")->fetchField());
|
Chris@0
|
75 $this->assertEqual('escape', $database->query("SHOW bytea_output")->fetchField());
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Tests expected load behavior of enableModules().
|
Chris@0
|
81 */
|
Chris@0
|
82 public function testEnableModulesLoad() {
|
Chris@0
|
83 $module = 'field_test';
|
Chris@0
|
84
|
Chris@0
|
85 // Verify that the module does not exist yet.
|
Chris@0
|
86 $this->assertFalse(\Drupal::moduleHandler()->moduleExists($module), "$module module not found.");
|
Chris@0
|
87 $list = array_keys(\Drupal::moduleHandler()->getModuleList());
|
Chris@0
|
88 $this->assertFalse(in_array($module, $list), "$module module not found in the extension handler's module list.");
|
Chris@0
|
89 $list = \Drupal::moduleHandler()->getImplementations('entity_display_build_alter');
|
Chris@0
|
90 $this->assertFalse(in_array($module, $list), "{$module}_entity_display_build_alter() in \Drupal::moduleHandler()->getImplementations() not found.");
|
Chris@0
|
91
|
Chris@0
|
92 // Enable the module.
|
Chris@0
|
93 $this->enableModules([$module]);
|
Chris@0
|
94
|
Chris@0
|
95 // Verify that the module exists.
|
Chris@0
|
96 $this->assertTrue(\Drupal::moduleHandler()->moduleExists($module), "$module module found.");
|
Chris@0
|
97 $list = array_keys(\Drupal::moduleHandler()->getModuleList());
|
Chris@0
|
98 $this->assertTrue(in_array($module, $list), "$module module found in the extension handler's module list.");
|
Chris@0
|
99 $list = \Drupal::moduleHandler()->getImplementations('query_efq_table_prefixing_test_alter');
|
Chris@0
|
100 $this->assertTrue(in_array($module, $list), "{$module}_query_efq_table_prefixing_test_alter() in \Drupal::moduleHandler()->getImplementations() found.");
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Tests expected installation behavior of enableModules().
|
Chris@0
|
105 */
|
Chris@0
|
106 public function testEnableModulesInstall() {
|
Chris@0
|
107 $module = 'module_test';
|
Chris@0
|
108 $table = 'module_test';
|
Chris@0
|
109
|
Chris@0
|
110 // Verify that the module does not exist yet.
|
Chris@0
|
111 $this->assertFalse(\Drupal::moduleHandler()->moduleExists($module), "$module module not found.");
|
Chris@0
|
112 $list = array_keys(\Drupal::moduleHandler()->getModuleList());
|
Chris@0
|
113 $this->assertFalse(in_array($module, $list), "$module module not found in the extension handler's module list.");
|
Chris@0
|
114 $list = \Drupal::moduleHandler()->getImplementations('hook_info');
|
Chris@0
|
115 $this->assertFalse(in_array($module, $list), "{$module}_hook_info() in \Drupal::moduleHandler()->getImplementations() not found.");
|
Chris@0
|
116
|
Chris@17
|
117 $this->assertFalse(Database::getConnection()->schema()->tableExists($table), "'$table' database table not found.");
|
Chris@0
|
118
|
Chris@0
|
119 // Install the module.
|
Chris@0
|
120 \Drupal::service('module_installer')->install([$module]);
|
Chris@0
|
121
|
Chris@0
|
122 // Verify that the enabled module exists.
|
Chris@0
|
123 $this->assertTrue(\Drupal::moduleHandler()->moduleExists($module), "$module module found.");
|
Chris@0
|
124 $list = array_keys(\Drupal::moduleHandler()->getModuleList());
|
Chris@0
|
125 $this->assertTrue(in_array($module, $list), "$module module found in the extension handler's module list.");
|
Chris@0
|
126 $list = \Drupal::moduleHandler()->getImplementations('hook_info');
|
Chris@0
|
127 $this->assertTrue(in_array($module, $list), "{$module}_hook_info() in \Drupal::moduleHandler()->getImplementations() found.");
|
Chris@0
|
128
|
Chris@17
|
129 $this->assertTrue(Database::getConnection()->schema()->tableExists($table), "'$table' database table found.");
|
Chris@0
|
130 $schema = drupal_get_module_schema($module, $table);
|
Chris@0
|
131 $this->assertTrue($schema, "'$table' table schema found.");
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * Tests installing modules with DependencyInjection services.
|
Chris@0
|
136 */
|
Chris@0
|
137 public function testEnableModulesInstallContainer() {
|
Chris@0
|
138 // Install Node module.
|
Chris@0
|
139 $this->enableModules(['user', 'field', 'node']);
|
Chris@0
|
140
|
Chris@0
|
141 $this->installEntitySchema('node', ['node', 'node_field_data']);
|
Chris@0
|
142 // Perform an entity query against node.
|
Chris@0
|
143 $query = \Drupal::entityQuery('node');
|
Chris@0
|
144 // Disable node access checks, since User module is not enabled.
|
Chris@0
|
145 $query->accessCheck(FALSE);
|
Chris@0
|
146 $query->condition('nid', 1);
|
Chris@0
|
147 $query->execute();
|
Chris@0
|
148 $this->pass('Entity field query was executed.');
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * Tests expected behavior of installSchema().
|
Chris@0
|
153 */
|
Chris@0
|
154 public function testInstallSchema() {
|
Chris@0
|
155 $module = 'entity_test';
|
Chris@0
|
156 $table = 'entity_test_example';
|
Chris@0
|
157 // Verify that we can install a table from the module schema.
|
Chris@0
|
158 $this->installSchema($module, $table);
|
Chris@17
|
159 $this->assertTrue(Database::getConnection()->schema()->tableExists($table), "'$table' database table found.");
|
Chris@0
|
160
|
Chris@0
|
161 // Verify that the schema is known to Schema API.
|
Chris@0
|
162 $schema = drupal_get_module_schema($module, $table);
|
Chris@0
|
163 $this->assertTrue($schema, "'$table' table schema found.");
|
Chris@0
|
164
|
Chris@0
|
165 // Verify that a unknown table from an enabled module throws an error.
|
Chris@0
|
166 $table = 'unknown_entity_test_table';
|
Chris@0
|
167 try {
|
Chris@0
|
168 $this->installSchema($module, $table);
|
Chris@0
|
169 $this->fail('Exception for non-retrievable schema found.');
|
Chris@0
|
170 }
|
Chris@0
|
171 catch (\Exception $e) {
|
Chris@0
|
172 $this->pass('Exception for non-retrievable schema found.');
|
Chris@0
|
173 }
|
Chris@17
|
174 $this->assertFalse(Database::getConnection()->schema()->tableExists($table), "'$table' database table not found.");
|
Chris@0
|
175 $schema = drupal_get_module_schema($module, $table);
|
Chris@0
|
176 $this->assertFalse($schema, "'$table' table schema not found.");
|
Chris@0
|
177
|
Chris@0
|
178 // Verify that a table from a unknown module cannot be installed.
|
Chris@0
|
179 $module = 'database_test';
|
Chris@0
|
180 $table = 'test';
|
Chris@0
|
181 try {
|
Chris@0
|
182 $this->installSchema($module, $table);
|
Chris@0
|
183 $this->fail('Exception for non-retrievable schema found.');
|
Chris@0
|
184 }
|
Chris@0
|
185 catch (\Exception $e) {
|
Chris@0
|
186 $this->pass('Exception for non-retrievable schema found.');
|
Chris@0
|
187 }
|
Chris@17
|
188 $this->assertFalse(Database::getConnection()->schema()->tableExists($table), "'$table' database table not found.");
|
Chris@0
|
189 $schema = drupal_get_module_schema($module, $table);
|
Chris@0
|
190 $this->assertTrue($schema, "'$table' table schema found.");
|
Chris@0
|
191
|
Chris@0
|
192 // Verify that the same table can be installed after enabling the module.
|
Chris@0
|
193 $this->enableModules([$module]);
|
Chris@0
|
194 $this->installSchema($module, $table);
|
Chris@17
|
195 $this->assertTrue(Database::getConnection()->schema()->tableExists($table), "'$table' database table found.");
|
Chris@0
|
196 $schema = drupal_get_module_schema($module, $table);
|
Chris@0
|
197 $this->assertTrue($schema, "'$table' table schema found.");
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Tests expected behavior of installEntitySchema().
|
Chris@0
|
202 */
|
Chris@0
|
203 public function testInstallEntitySchema() {
|
Chris@0
|
204 $entity = 'entity_test';
|
Chris@0
|
205 // The entity_test Entity has a field that depends on the User module.
|
Chris@0
|
206 $this->enableModules(['user']);
|
Chris@0
|
207 // Verity that the entity schema is created properly.
|
Chris@0
|
208 $this->installEntitySchema($entity);
|
Chris@17
|
209 $this->assertTrue(Database::getConnection()->schema()->tableExists($entity), "'$entity' database table found.");
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * Tests expected behavior of installConfig().
|
Chris@0
|
214 */
|
Chris@0
|
215 public function testInstallConfig() {
|
Chris@0
|
216 // The user module has configuration that depends on system.
|
Chris@0
|
217 $this->enableModules(['system']);
|
Chris@0
|
218 $module = 'user';
|
Chris@0
|
219
|
Chris@0
|
220 // Verify that default config can only be installed for enabled modules.
|
Chris@0
|
221 try {
|
Chris@0
|
222 $this->installConfig([$module]);
|
Chris@0
|
223 $this->fail('Exception for non-enabled module found.');
|
Chris@0
|
224 }
|
Chris@0
|
225 catch (\Exception $e) {
|
Chris@0
|
226 $this->pass('Exception for non-enabled module found.');
|
Chris@0
|
227 }
|
Chris@0
|
228 $this->assertFalse($this->container->get('config.storage')->exists('user.settings'));
|
Chris@0
|
229
|
Chris@0
|
230 // Verify that default config can be installed.
|
Chris@0
|
231 $this->enableModules(['user']);
|
Chris@0
|
232 $this->installConfig(['user']);
|
Chris@0
|
233 $this->assertTrue($this->container->get('config.storage')->exists('user.settings'));
|
Chris@0
|
234 $this->assertTrue($this->config('user.settings')->get('register'));
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 /**
|
Chris@0
|
238 * Tests that the module list is retained after enabling/installing/disabling.
|
Chris@0
|
239 */
|
Chris@0
|
240 public function testEnableModulesFixedList() {
|
Chris@0
|
241 // Install system module.
|
Chris@18
|
242 $this->container->get('module_installer')->install(['system', 'user', 'menu_link_content']);
|
Chris@0
|
243 $entity_manager = \Drupal::entityManager();
|
Chris@0
|
244
|
Chris@0
|
245 // entity_test is loaded via $modules; its entity type should exist.
|
Chris@0
|
246 $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
|
Chris@0
|
247 $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
|
Chris@0
|
248
|
Chris@0
|
249 // Load some additional modules; entity_test should still exist.
|
Chris@0
|
250 $this->enableModules(['field', 'text', 'entity_test']);
|
Chris@0
|
251 $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
|
Chris@0
|
252 $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
|
Chris@0
|
253
|
Chris@0
|
254 // Install some other modules; entity_test should still exist.
|
Chris@0
|
255 $this->container->get('module_installer')->install(['user', 'field', 'field_test'], FALSE);
|
Chris@0
|
256 $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
|
Chris@0
|
257 $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
|
Chris@0
|
258
|
Chris@0
|
259 // Uninstall one of those modules; entity_test should still exist.
|
Chris@0
|
260 $this->container->get('module_installer')->uninstall(['field_test']);
|
Chris@0
|
261 $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
|
Chris@0
|
262 $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
|
Chris@0
|
263
|
Chris@0
|
264 // Set the weight of a module; entity_test should still exist.
|
Chris@0
|
265 module_set_weight('field', -1);
|
Chris@0
|
266 $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
|
Chris@0
|
267 $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
|
Chris@0
|
268
|
Chris@0
|
269 // Reactivate the previously uninstalled module.
|
Chris@0
|
270 $this->enableModules(['field_test']);
|
Chris@0
|
271
|
Chris@0
|
272 // Create a field.
|
Chris@18
|
273 $this->installEntitySchema('entity_test');
|
Chris@0
|
274 $display = EntityViewDisplay::create([
|
Chris@0
|
275 'targetEntityType' => 'entity_test',
|
Chris@0
|
276 'bundle' => 'entity_test',
|
Chris@0
|
277 'mode' => 'default',
|
Chris@0
|
278 ]);
|
Chris@0
|
279 $field_storage = FieldStorageConfig::create([
|
Chris@0
|
280 'field_name' => 'test_field',
|
Chris@0
|
281 'entity_type' => 'entity_test',
|
Chris@17
|
282 'type' => 'test_field',
|
Chris@0
|
283 ]);
|
Chris@0
|
284 $field_storage->save();
|
Chris@0
|
285 FieldConfig::create([
|
Chris@0
|
286 'field_storage' => $field_storage,
|
Chris@0
|
287 'bundle' => 'entity_test',
|
Chris@0
|
288 ])->save();
|
Chris@0
|
289 }
|
Chris@0
|
290
|
Chris@0
|
291 /**
|
Chris@0
|
292 * Tests that ThemeManager works right after loading a module.
|
Chris@0
|
293 */
|
Chris@0
|
294 public function testEnableModulesTheme() {
|
Chris@0
|
295 /** @var \Drupal\Core\Render\RendererInterface $renderer */
|
Chris@0
|
296 $renderer = $this->container->get('renderer');
|
Chris@0
|
297 $original_element = $element = [
|
Chris@0
|
298 '#type' => 'container',
|
Chris@0
|
299 '#markup' => 'Foo',
|
Chris@0
|
300 '#attributes' => [],
|
Chris@0
|
301 ];
|
Chris@0
|
302 $this->enableModules(['system']);
|
Chris@0
|
303 // \Drupal\Core\Theme\ThemeManager::render() throws an exception if modules
|
Chris@0
|
304 // are not loaded yet.
|
Chris@0
|
305 $this->assertTrue($renderer->renderRoot($element));
|
Chris@0
|
306
|
Chris@0
|
307 $element = $original_element;
|
Chris@0
|
308 $this->disableModules(['entity_test']);
|
Chris@0
|
309 $this->assertTrue($renderer->renderRoot($element));
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 /**
|
Chris@0
|
313 * Tests that there is no theme by default.
|
Chris@0
|
314 */
|
Chris@0
|
315 public function testNoThemeByDefault() {
|
Chris@0
|
316 $themes = $this->config('core.extension')->get('theme');
|
Chris@0
|
317 $this->assertEqual($themes, []);
|
Chris@0
|
318
|
Chris@0
|
319 $extensions = $this->container->get('config.storage')->read('core.extension');
|
Chris@0
|
320 $this->assertEqual($extensions['theme'], []);
|
Chris@0
|
321
|
Chris@0
|
322 $active_theme = $this->container->get('theme.manager')->getActiveTheme();
|
Chris@0
|
323 $this->assertEqual($active_theme->getName(), 'core');
|
Chris@0
|
324 }
|
Chris@0
|
325
|
Chris@0
|
326 /**
|
Chris@0
|
327 * Tests that \Drupal::installProfile() returns NULL.
|
Chris@0
|
328 *
|
Chris@0
|
329 * As the currently active installation profile is used when installing
|
Chris@0
|
330 * configuration, for example, this is essential to ensure test isolation.
|
Chris@0
|
331 */
|
Chris@0
|
332 public function testDrupalGetProfile() {
|
Chris@0
|
333 $this->assertNull(\Drupal::installProfile());
|
Chris@0
|
334 }
|
Chris@0
|
335
|
Chris@0
|
336 /**
|
Chris@0
|
337 * {@inheritdoc}
|
Chris@0
|
338 */
|
Chris@0
|
339 public function run(array $methods = []) {
|
Chris@0
|
340 parent::run($methods);
|
Chris@0
|
341
|
Chris@0
|
342 // Check that all tables of the test instance have been deleted. At this
|
Chris@0
|
343 // point the original database connection is restored so we need to prefix
|
Chris@0
|
344 // the tables.
|
Chris@0
|
345 $connection = Database::getConnection();
|
Chris@0
|
346 if ($connection->databaseType() != 'sqlite') {
|
Chris@0
|
347 $tables = $connection->schema()->findTables($this->databasePrefix . '%');
|
Chris@0
|
348 $this->assertTrue(empty($tables), 'All test tables have been removed.');
|
Chris@0
|
349 }
|
Chris@0
|
350 else {
|
Chris@0
|
351 // We don't have the test instance connection anymore so we have to
|
Chris@0
|
352 // re-attach its database and then use the same query as
|
Chris@0
|
353 // \Drupal\Core\Database\Driver\sqlite\Schema::findTables().
|
Chris@0
|
354 // @see \Drupal\Core\Database\Driver\sqlite\Connection::__construct()
|
Chris@0
|
355 $info = Database::getConnectionInfo();
|
Chris@0
|
356 $connection->query('ATTACH DATABASE :database AS :prefix', [
|
Chris@0
|
357 ':database' => $info['default']['database'] . '-' . $this->databasePrefix,
|
Chris@17
|
358 ':prefix' => $this->databasePrefix,
|
Chris@0
|
359 ]);
|
Chris@0
|
360
|
Chris@0
|
361 $result = $connection->query("SELECT name FROM " . $this->databasePrefix . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [
|
Chris@0
|
362 ':type' => 'table',
|
Chris@0
|
363 ':table_name' => '%',
|
Chris@0
|
364 ':pattern' => 'sqlite_%',
|
Chris@0
|
365 ])->fetchAllKeyed(0, 0);
|
Chris@0
|
366
|
Chris@0
|
367 $this->assertTrue(empty($result), 'All test tables have been removed.');
|
Chris@0
|
368 }
|
Chris@0
|
369 }
|
Chris@0
|
370
|
Chris@0
|
371 }
|