3 namespace Drupal\KernelTests;
5 use Drupal\Component\FileCache\FileCacheFactory;
6 use Drupal\Core\Database\Database;
7 use org\bovigo\vfs\vfsStream;
8 use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
11 * @coversDefaultClass \Drupal\KernelTests\KernelTestBase
17 class KernelTestBaseTest extends KernelTestBase {
20 * @covers ::setUpBeforeClass
22 public function testSetUpBeforeClass() {
23 // Note: PHPUnit automatically restores the original working directory.
24 $this->assertSame(realpath(__DIR__ . '/../../../../'), getcwd());
28 * @covers ::bootEnvironment
30 public function testBootEnvironment() {
31 $this->assertRegExp('/^test\d{8}$/', $this->databasePrefix);
32 $this->assertStringStartsWith('vfs://root/sites/simpletest/', $this->siteDirectory);
37 substr($this->databasePrefix, 4) => [
47 ], vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure());
51 * @covers ::getDatabaseConnectionInfo
53 public function testGetDatabaseConnectionInfoWithOutManualSetDbUrl() {
54 $options = $this->container->get('database')->getConnectionOptions();
55 $this->assertSame($this->databasePrefix, $options['prefix']['default']);
61 public function testSetUp() {
62 $this->assertTrue($this->container->has('request_stack'));
63 $this->assertTrue($this->container->initialized('request_stack'));
64 $request = $this->container->get('request_stack')->getCurrentRequest();
65 $this->assertNotEmpty($request);
66 $this->assertEquals('/', $request->getPathInfo());
68 $this->assertSame($request, \Drupal::request());
70 $this->assertEquals($this, $GLOBALS['conf']['container_service_providers']['test']);
72 $GLOBALS['destroy-me'] = TRUE;
73 $this->assertArrayHasKey('destroy-me', $GLOBALS);
75 $database = $this->container->get('database');
76 $database->schema()->createTable('foo', [
85 $this->assertTrue($database->schema()->tableExists('foo'));
87 // Ensure that the database tasks have been run during set up. Neither MySQL
88 // nor SQLite make changes that are testable.
89 if ($database->driver() == 'pgsql') {
90 $this->assertEquals('on', $database->query("SHOW standard_conforming_strings")->fetchField());
91 $this->assertEquals('escape', $database->query("SHOW bytea_output")->fetchField());
94 $this->assertNotNull(FileCacheFactory::getPrefix());
101 public function testSetUpDoesNotLeak() {
102 $this->assertArrayNotHasKey('destroy-me', $GLOBALS);
104 // Ensure that we have a different database prefix.
105 $schema = $this->container->get('database')->schema();
106 $this->assertFalse($schema->tableExists('foo'));
112 public function testRegister() {
113 // Verify that this container is identical to the actual container.
114 $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->container);
115 $this->assertSame($this->container, \Drupal::getContainer());
117 // The request service should never exist.
118 $this->assertFalse($this->container->has('request'));
120 // Verify that there is a request stack.
121 $request = $this->container->get('request_stack')->getCurrentRequest();
122 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $request);
123 $this->assertSame($request, \Drupal::request());
125 // Trigger a container rebuild.
126 $this->enableModules(['system']);
128 // Verify that this container is identical to the actual container.
129 $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->container);
130 $this->assertSame($this->container, \Drupal::getContainer());
132 // The request service should never exist.
133 $this->assertFalse($this->container->has('request'));
135 // Verify that there is a request stack (and that it persisted).
136 $new_request = $this->container->get('request_stack')->getCurrentRequest();
137 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $new_request);
138 $this->assertSame($new_request, \Drupal::request());
139 $this->assertSame($request, $new_request);
141 // Ensure getting the router.route_provider does not trigger a deprecation
142 // message that errors.
143 $this->container->get('router.route_provider');
147 * Tests whether the fixture allows us to install modules and configuration.
149 * @see ::testSubsequentContainerIsolation()
151 public function testContainerIsolation() {
152 $this->enableModules(['system', 'user']);
153 $this->assertNull($this->installConfig('user'));
157 * Tests whether the fixture can re-install modules and configuration.
159 * @depends testContainerIsolation
161 public function testSubsequentContainerIsolation() {
162 $this->enableModules(['system', 'user']);
163 $this->assertNull($this->installConfig('user'));
169 public function testRender() {
170 $type = 'processed_text';
171 $element_info = $this->container->get('element_info');
172 $this->assertSame(['#defaults_loaded' => TRUE], $element_info->getInfo($type));
174 $this->enableModules(['filter']);
176 $this->assertNotSame($element_info, $this->container->get('element_info'));
177 $this->assertNotEmpty($this->container->get('element_info')->getInfo($type));
180 '#type' => 'html_tag',
184 $expected = "<h3>Inner</h3>\n";
186 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
187 $output = \Drupal::service('renderer')->renderRoot($build);
188 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
190 $this->assertEquals($expected, $build['#markup']);
191 $this->assertEquals($expected, $output);
197 public function testRenderWithTheme() {
198 $this->enableModules(['system']);
201 '#type' => 'textfield',
204 $expected = '/' . preg_quote('<input type="text" name="test"', '/') . '/';
206 $this->assertArrayNotHasKey('theme', $GLOBALS);
207 $output = \Drupal::service('renderer')->renderRoot($build);
208 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
210 $this->assertRegExp($expected, (string) $build['#children']);
211 $this->assertRegExp($expected, (string) $output);
215 * @covers ::bootKernel
217 public function testFileDefaultScheme() {
218 $this->assertEquals('public', file_default_scheme());
219 $this->assertEquals('public', \Drupal::config('system.file')->get('default_scheme'));
223 * Tests the assumption that local time is in 'Australia/Sydney'.
225 public function testLocalTimeZone() {
226 // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
227 $this->assertEquals('Australia/Sydney', date_default_timezone_get());
231 * Tests that a test method is skipped when it requires a module not present.
233 * In order to catch checkRequirements() regressions, we have to make a new
234 * test object and run checkRequirements() here.
236 * @covers ::checkRequirements
237 * @covers ::checkModuleRequirements
239 public function testMethodRequiresModule() {
240 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleMethodTest.php';
242 $stub_test = new KernelMissingDependentModuleMethodTest();
243 // We have to setName() to the method name we're concerned with.
244 $stub_test->setName('testRequiresModule');
246 // We cannot use $this->setExpectedException() because PHPUnit would skip
247 // the test before comparing the exception type.
249 $stub_test->publicCheckRequirements();
250 $this->fail('Missing required module throws skipped test exception.');
252 catch (\PHPUnit_Framework_SkippedTestError $e) {
253 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage());
258 * Tests that a test case is skipped when it requires a module not present.
260 * In order to catch checkRequirements() regressions, we have to make a new
261 * test object and run checkRequirements() here.
263 * @covers ::checkRequirements
264 * @covers ::checkModuleRequirements
266 public function testRequiresModule() {
267 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleTest.php';
269 $stub_test = new KernelMissingDependentModuleTest();
270 // We have to setName() to the method name we're concerned with.
271 $stub_test->setName('testRequiresModule');
273 // We cannot use $this->setExpectedException() because PHPUnit would skip
274 // the test before comparing the exception type.
276 $stub_test->publicCheckRequirements();
277 $this->fail('Missing required module throws skipped test exception.');
279 catch (\PHPUnit_Framework_SkippedTestError $e) {
280 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage());
287 protected function tearDown() {
290 // Check that all tables of the test instance have been deleted. At this
291 // point the original database connection is restored so we need to prefix
293 $connection = Database::getConnection();
294 if ($connection->databaseType() != 'sqlite') {
295 $tables = $connection->schema()->findTables($this->databasePrefix . '%');
296 $this->assertTrue(empty($tables), 'All test tables have been removed.');
299 $result = $connection->query("SELECT name FROM " . $this->databasePrefix . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [
301 ':table_name' => '%',
302 ':pattern' => 'sqlite_%',
303 ])->fetchAllKeyed(0, 0);
305 $this->assertTrue(empty($result), 'All test tables have been removed.');