KernelTestBaseTest.php 14.7 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\simpletest\Tests\KernelTestBaseTest.
6
7
8
9
 */

namespace Drupal\simpletest\Tests;

10
use Drupal\Core\Database\Database;
11
use Drupal\simpletest\KernelTestBase;
12
use Drupal\field\Entity\FieldStorageConfig;
13
use Drupal\Core\Entity\Entity\EntityViewDisplay;
14
15

/**
16
 * Tests KernelTestBase functionality.
17
18
 *
 * @group simpletest
19
 */
20
class KernelTestBaseTest extends KernelTestBase {
21
22
23
24
25
26

  /**
   * Modules to enable.
   *
   * @var array
   */
27
  public static $modules = array('entity_test');
28

29
30
31
32
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
33
    $php = <<<'EOS'
34
<?php
35
36
37
# Make sure that the $test_class variable is defined when this file is included.
if ($test_class) {
}
38

39
40
41
42
43
44
45
46
47
48
# Define a function to be able to check that this file was loaded with
# function_exists().
if (!function_exists('simpletest_test_stub_settings_function')) {
  function simpletest_test_stub_settings_function() {}
}
EOS;

    $settings_testing_file = $this->siteDirectory . '/settings.testing.php';
    file_put_contents($settings_testing_file, $php);

49
    $original_container = $this->originalContainer;
50
51
52
53
    parent::setUp();
    $this->assertNotIdentical(\Drupal::getContainer(), $original_container, 'KernelTestBase test creates a new container.');
  }

54
55
56
57
  /**
   * Tests expected behavior of setUp().
   */
  function testSetUp() {
58
    $modules = array('entity_test');
59
60
61
    $table = 'entity_test';

    // Verify that specified $modules have been loaded.
62
    $this->assertTrue(function_exists('entity_test_entity_bundle_info'), 'entity_test.module was loaded.');
63
    // Verify that there is a fixed module list.
64
    $this->assertIdentical(array_keys(\Drupal::moduleHandler()->getModuleList()), $modules);
65
66
    $this->assertIdentical(\Drupal::moduleHandler()->getImplementations('entity_bundle_info'), ['entity_test']);
    $this->assertIdentical(\Drupal::moduleHandler()->getImplementations('entity_type_alter'), ['entity_test']);
67
68
69

    // Verify that no modules have been installed.
    $this->assertFalse(db_table_exists($table), "'$table' database table not found.");
70
71
72

    // Verify that the settings.testing.php got taken into account.
    $this->assertTrue(function_exists('simpletest_test_stub_settings_function'));
73
74
75
76
77
78
79
80

    // Ensure that the database tasks have been run during set up. Neither MySQL
    // nor SQLite make changes that are testable.
    $database = $this->container->get('database');
    if ($database->driver() == 'pgsql') {
      $this->assertEqual('on', $database->query("SHOW standard_conforming_strings")->fetchField());
      $this->assertEqual('escape', $database->query("SHOW bytea_output")->fetchField());
    }
81
82
83
84
85
86
87
88
89
  }

  /**
   * Tests expected load behavior of enableModules().
   */
  function testEnableModulesLoad() {
    $module = 'field_test';

    // Verify that the module does not exist yet.
90
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists($module), "$module module not found.");
91
    $list = array_keys(\Drupal::moduleHandler()->getModuleList());
92
    $this->assertFalse(in_array($module, $list), "$module module not found in the extension handler's module list.");
93
94
    $list = \Drupal::moduleHandler()->getImplementations('entity_display_build_alter');
    $this->assertFalse(in_array($module, $list), "{$module}_entity_display_build_alter() in \Drupal::moduleHandler()->getImplementations() not found.");
95
96

    // Enable the module.
97
    $this->enableModules(array($module));
98
99

    // Verify that the module exists.
100
    $this->assertTrue(\Drupal::moduleHandler()->moduleExists($module), "$module module found.");
101
    $list = array_keys(\Drupal::moduleHandler()->getModuleList());
102
    $this->assertTrue(in_array($module, $list), "$module module found in the extension handler's module list.");
103
104
    $list = \Drupal::moduleHandler()->getImplementations('query_efq_table_prefixing_test_alter');
    $this->assertTrue(in_array($module, $list), "{$module}_query_efq_table_prefixing_test_alter() in \Drupal::moduleHandler()->getImplementations() found.");
105
106
107
108
109
110
  }

  /**
   * Tests expected installation behavior of enableModules().
   */
  function testEnableModulesInstall() {
111
112
    $module = 'module_test';
    $table = 'module_test';
113
114

    // Verify that the module does not exist yet.
115
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists($module), "$module module not found.");
116
    $list = array_keys(\Drupal::moduleHandler()->getModuleList());
117
    $this->assertFalse(in_array($module, $list), "$module module not found in the extension handler's module list.");
118
119
    $list = \Drupal::moduleHandler()->getImplementations('hook_info');
    $this->assertFalse(in_array($module, $list), "{$module}_hook_info() in \Drupal::moduleHandler()->getImplementations() not found.");
120
121
122

    $this->assertFalse(db_table_exists($table), "'$table' database table not found.");

123
    // Install the module.
124
    \Drupal::service('module_installer')->install(array($module));
125
126

    // Verify that the enabled module exists.
127
    $this->assertTrue(\Drupal::moduleHandler()->moduleExists($module), "$module module found.");
128
    $list = array_keys(\Drupal::moduleHandler()->getModuleList());
129
    $this->assertTrue(in_array($module, $list), "$module module found in the extension handler's module list.");
130
131
    $list = \Drupal::moduleHandler()->getImplementations('hook_info');
    $this->assertTrue(in_array($module, $list), "{$module}_hook_info() in \Drupal::moduleHandler()->getImplementations() found.");
132
133

    $this->assertTrue(db_table_exists($table), "'$table' database table found.");
134
    $schema = drupal_get_module_schema($module, $table);
135
136
137
138
    $this->assertTrue($schema, "'$table' table schema found.");
  }

  /**
139
   * Tests installing modules with DependencyInjection services.
140
141
142
   */
  function testEnableModulesInstallContainer() {
    // Install Node module.
143
    $this->enableModules(array('user', 'field', 'node'));
144

145
    $this->installEntitySchema('node', array('node', 'node_field_data'));
146
    // Perform an entity query against node.
147
    $query = \Drupal::entityQuery('node');
148
149
150
151
152
153
154
155
156
157
158
159
    // Disable node access checks, since User module is not enabled.
    $query->accessCheck(FALSE);
    $query->condition('nid', 1);
    $query->execute();
    $this->pass('Entity field query was executed.');
  }

  /**
   * Tests expected behavior of installSchema().
   */
  function testInstallSchema() {
    $module = 'entity_test';
160
    $table = 'entity_test_example';
161
162
163
164
165
    // Verify that we can install a table from the module schema.
    $this->installSchema($module, $table);
    $this->assertTrue(db_table_exists($table), "'$table' database table found.");

    // Verify that the schema is known to Schema API.
166
    $schema = drupal_get_module_schema($module, $table);
167
168
    $this->assertTrue($schema, "'$table' table schema found.");

169
170
171
172
173
174
175
176
177
178
    // Verify that a unknown table from an enabled module throws an error.
    $table = 'unknown_entity_test_table';
    try {
      $this->installSchema($module, $table);
      $this->fail('Exception for non-retrievable schema found.');
    }
    catch (\Exception $e) {
      $this->pass('Exception for non-retrievable schema found.');
    }
    $this->assertFalse(db_table_exists($table), "'$table' database table not found.");
179
    $schema = drupal_get_module_schema($module, $table);
180
181
    $this->assertFalse($schema, "'$table' table schema not found.");

182
183
184
185
186
187
188
189
190
191
192
    // Verify that a table from a unknown module cannot be installed.
    $module = 'database_test';
    $table = 'test';
    try {
      $this->installSchema($module, $table);
      $this->fail('Exception for non-retrievable schema found.');
    }
    catch (\Exception $e) {
      $this->pass('Exception for non-retrievable schema found.');
    }
    $this->assertFalse(db_table_exists($table), "'$table' database table not found.");
193
194
    $schema = drupal_get_module_schema($module, $table);
    $this->assertTrue($schema, "'$table' table schema found.");
195
196

    // Verify that the same table can be installed after enabling the module.
197
    $this->enableModules(array($module));
198
199
    $this->installSchema($module, $table);
    $this->assertTrue(db_table_exists($table), "'$table' database table found.");
200
    $schema = drupal_get_module_schema($module, $table);
201
202
203
    $this->assertTrue($schema, "'$table' table schema found.");
  }

204
205
206
207
208
209
210
211
212
213
214
215
  /**
   * Tests expected behavior of installEntitySchema().
   */
  function testInstallEntitySchema() {
    $entity = 'entity_test';
    // The entity_test Entity has a field that depends on the User module.
    $this->enableModules(array('user'));
    // Verity that the entity schema is created properly.
    $this->installEntitySchema($entity);
    $this->assertTrue(db_table_exists($entity), "'$entity' database table found.");
  }

216
  /**
217
218
219
   * Tests expected behavior of installConfig().
   */
  function testInstallConfig() {
220
221
    // The user module has configuration that depends on system.
    $this->enableModules(array('system'));
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    $module = 'user';

    // Verify that default config can only be installed for enabled modules.
    try {
      $this->installConfig(array($module));
      $this->fail('Exception for non-enabled module found.');
    }
    catch (\Exception $e) {
      $this->pass('Exception for non-enabled module found.');
    }
    $this->assertFalse($this->container->get('config.storage')->exists('user.settings'));

    // Verify that default config can be installed.
    $this->enableModules(array('user'));
    $this->installConfig(array('user'));
    $this->assertTrue($this->container->get('config.storage')->exists('user.settings'));
238
    $this->assertTrue($this->config('user.settings')->get('register'));
239
240
241
  }

  /**
242
   * Tests that the module list is retained after enabling/installing/disabling.
243
244
   */
  function testEnableModulesFixedList() {
245
    // Install system module.
246
    $this->container->get('module_installer')->install(array('system', 'menu_link_content'));
247
    $entity_manager = \Drupal::entityManager();
248

249
250
    // entity_test is loaded via $modules; its entity type should exist.
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
251
    $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
252
253

    // Load some additional modules; entity_test should still exist.
254
    $this->enableModules(array('field', 'text', 'entity_test'));
255
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
256
    $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
257
258

    // Install some other modules; entity_test should still exist.
259
    $this->container->get('module_installer')->install(array('user', 'field', 'field_test'), FALSE);
260
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
261
    $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
262

263
    // Uninstall one of those modules; entity_test should still exist.
264
    $this->container->get('module_installer')->uninstall(array('field_test'));
265
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
266
    $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
267
268

    // Set the weight of a module; entity_test should still exist.
269
    module_set_weight('field', -1);
270
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
271
    $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
272

273
    // Reactivate the previously uninstalled module.
274
    $this->enableModules(array('field_test'));
275

276
    // Create a field.
277
    $display = EntityViewDisplay::create(array(
278
279
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
280
      'mode' => 'default',
281
    ));
282
    $field_storage = FieldStorageConfig::create(array(
283
      'field_name' => 'test_field',
284
      'entity_type' => 'entity_test',
285
      'type' => 'test_field'
286
    ));
287
    $field_storage->save();
288
    entity_create('field_config', array(
289
      'field_storage' => $field_storage,
290
      'bundle' => 'entity_test',
291
    ))->save();
292
293
294
  }

  /**
295
   * Tests that ThemeManager works right after loading a module.
296
297
   */
  function testEnableModulesTheme() {
298
299
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');
300
    $original_element = $element = array(
301
302
303
304
      '#type' => 'container',
      '#markup' => 'Foo',
      '#attributes' => array(),
    );
305
    $this->enableModules(array('system'));
306
307
    // \Drupal\Core\Theme\ThemeManager::render() throws an exception if modules
    // are not loaded yet.
308
    $this->assertTrue($renderer->renderRoot($element));
309
310
311

    $element = $original_element;
    $this->disableModules(array('entity_test'));
312
    $this->assertTrue($renderer->renderRoot($element));
313
314
  }

315
316
317
318
  /**
   * Tests that there is no theme by default.
   */
  function testNoThemeByDefault() {
319
    $themes = $this->config('core.extension')->get('theme');
320
321
322
323
324
325
326
327
328
    $this->assertEqual($themes, array());

    $extensions = $this->container->get('config.storage')->read('core.extension');
    $this->assertEqual($extensions['theme'], array());

    $active_theme = $this->container->get('theme.manager')->getActiveTheme();
    $this->assertEqual($active_theme->getName(), 'core');
  }

329
330
331
332
333
334
335
336
337
338
  /**
   * Tests that drupal_get_profile() returns NULL.
   *
   * As the currently active installation profile is used when installing
   * configuration, for example, this is essential to ensure test isolation.
   */
  public function testDrupalGetProfile() {
    $this->assertNull(drupal_get_profile());
  }

339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
  /**
   * {@inheritdoc}
   */
  public function run(array $methods = array()) {
    parent::run($methods);

    // Check that all tables of the test instance have been deleted. At this
    // point the original database connection is restored so we need to prefix
    // the tables.
    $connection = Database::getConnection();
    if ($connection->databaseType() != 'sqlite') {
      $tables = $connection->schema()->findTables($this->databasePrefix . '%');
      $this->assertTrue(empty($tables), 'All test tables have been removed.');
    }
    else {
      // We don't have the test instance connection anymore so we have to
      // re-attach its database and then use the same query as
      // \Drupal\Core\Database\Driver\sqlite\Schema::findTables().
      // @see \Drupal\Core\Database\Driver\sqlite\Connection::__construct()
      $info = Database::getConnectionInfo();
      $connection->query('ATTACH DATABASE :database AS :prefix', [
        ':database' => $info['default']['database'] . '-' . $this->databasePrefix,
        ':prefix' => $this->databasePrefix
      ]);

      $result = $connection->query("SELECT name FROM " . $this->databasePrefix . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", array(
        ':type' => 'table',
        ':table_name' => '%',
        ':pattern' => 'sqlite_%',
      ))->fetchAllKeyed(0, 0);

      $this->assertTrue(empty($result), 'All test tables have been removed.');
    }
  }

374
}