KernelTestBaseTest.php 12.5 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\simpletest\KernelTestBase;
11 12

/**
13
 * Tests KernelTestBase functionality.
14 15
 *
 * @group simpletest
16
 */
17
class KernelTestBaseTest extends KernelTestBase {
18 19 20 21 22 23

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

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

36 37 38 39 40 41 42 43 44 45
# 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);

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

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

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

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

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

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

    // Verify that the module does not exist yet.
79
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists($module), "$module module not found.");
80
    $list = array_keys(\Drupal::moduleHandler()->getModuleList());
81
    $this->assertFalse(in_array($module, $list), "$module module not found in the extension handler's module list.");
82 83
    $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.");
84 85

    // Enable the module.
86
    $this->enableModules(array($module));
87 88

    // Verify that the module exists.
89
    $this->assertTrue(\Drupal::moduleHandler()->moduleExists($module), "$module module found.");
90
    $list = array_keys(\Drupal::moduleHandler()->getModuleList());
91
    $this->assertTrue(in_array($module, $list), "$module module found in the extension handler's module list.");
92 93
    $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.");
94 95 96 97 98 99
  }

  /**
   * Tests expected installation behavior of enableModules().
   */
  function testEnableModulesInstall() {
100 101
    $module = 'module_test';
    $table = 'module_test';
102 103

    // Verify that the module does not exist yet.
104
    $this->assertFalse(\Drupal::moduleHandler()->moduleExists($module), "$module module not found.");
105
    $list = array_keys(\Drupal::moduleHandler()->getModuleList());
106
    $this->assertFalse(in_array($module, $list), "$module module not found in the extension handler's module list.");
107 108
    $list = \Drupal::moduleHandler()->getImplementations('hook_info');
    $this->assertFalse(in_array($module, $list), "{$module}_hook_info() in \Drupal::moduleHandler()->getImplementations() not found.");
109 110 111

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

112
    // Install the module.
113
    \Drupal::service('module_installer')->install(array($module));
114 115

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

    $this->assertTrue(db_table_exists($table), "'$table' database table found.");
123
    $schema = drupal_get_module_schema($module, $table);
124 125 126 127
    $this->assertTrue($schema, "'$table' table schema found.");
  }

  /**
128
   * Tests installing modules with DependencyInjection services.
129 130 131
   */
  function testEnableModulesInstallContainer() {
    // Install Node module.
132
    $this->enableModules(array('user', 'field', 'node'));
133

134
    $this->installEntitySchema('node', array('node', 'node_field_data'));
135
    // Perform an entity query against node.
136
    $query = \Drupal::entityQuery('node');
137 138 139 140 141 142 143 144 145 146 147 148
    // 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';
149
    $table = 'entity_test_example';
150 151 152 153 154
    // 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.
155
    $schema = drupal_get_module_schema($module, $table);
156 157
    $this->assertTrue($schema, "'$table' table schema found.");

158 159 160 161 162 163 164 165 166 167
    // 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.");
168
    $schema = drupal_get_module_schema($module, $table);
169 170
    $this->assertFalse($schema, "'$table' table schema not found.");

171 172 173 174 175 176 177 178 179 180 181
    // 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.");
182 183
    $schema = drupal_get_module_schema($module, $table);
    $this->assertTrue($schema, "'$table' table schema found.");
184 185

    // Verify that the same table can be installed after enabling the module.
186
    $this->enableModules(array($module));
187 188
    $this->installSchema($module, $table);
    $this->assertTrue(db_table_exists($table), "'$table' database table found.");
189
    $schema = drupal_get_module_schema($module, $table);
190 191 192
    $this->assertTrue($schema, "'$table' table schema found.");
  }

193 194 195 196 197 198 199 200 201 202 203 204
  /**
   * 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.");
  }

205
  /**
206 207 208
   * Tests expected behavior of installConfig().
   */
  function testInstallConfig() {
209 210
    // The user module has configuration that depends on system.
    $this->enableModules(array('system'));
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    $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'));
227
    $this->assertTrue($this->config('user.settings')->get('register'));
228 229 230
  }

  /**
231
   * Tests that the module list is retained after enabling/installing/disabling.
232 233
   */
  function testEnableModulesFixedList() {
234
    // Install system module.
235
    $this->container->get('module_installer')->install(array('system', 'menu_link_content'));
236
    $entity_manager = \Drupal::entityManager();
237

238 239
    // entity_test is loaded via $modules; its entity type should exist.
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
240
    $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
241 242

    // Load some additional modules; entity_test should still exist.
243
    $this->enableModules(array('field', 'text', 'entity_test'));
244
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
245
    $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
246 247

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

252
    // Uninstall one of those modules; entity_test should still exist.
253
    $this->container->get('module_installer')->uninstall(array('field_test'));
254
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
255
    $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test'));
256 257

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

262
    // Reactivate the previously uninstalled module.
263
    $this->enableModules(array('field_test'));
264

265
    // Create a field.
266
    entity_create('entity_view_display', array(
267 268
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
269
      'mode' => 'default',
270
    ));
271
    $field_storage = entity_create('field_storage_config', array(
272
      'field_name' => 'test_field',
273
      'entity_type' => 'entity_test',
274
      'type' => 'test_field'
275
    ));
276
    $field_storage->save();
277
    entity_create('field_config', array(
278
      'field_storage' => $field_storage,
279
      'bundle' => 'entity_test',
280
    ))->save();
281 282 283
  }

  /**
284
   * Tests that _theme() works right after loading a module.
285 286
   */
  function testEnableModulesTheme() {
287
    $original_element = $element = array(
288 289 290 291
      '#type' => 'container',
      '#markup' => 'Foo',
      '#attributes' => array(),
    );
292
    $this->enableModules(array('system'));
293
    // _theme() throws an exception if modules are not loaded yet.
294 295 296 297 298
    $this->assertTrue(drupal_render($element));

    $element = $original_element;
    $this->disableModules(array('entity_test'));
    $this->assertTrue(drupal_render($element));
299 300
  }

301 302 303 304
  /**
   * Tests that there is no theme by default.
   */
  function testNoThemeByDefault() {
305
    $themes = $this->config('core.extension')->get('theme');
306 307 308 309 310 311 312 313 314
    $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');
  }

315 316 317 318 319 320 321 322 323 324
  /**
   * 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());
  }

325
}