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);
143 * Tests whether the fixture allows us to install modules and configuration.
145 * @see ::testSubsequentContainerIsolation()
147 public function testContainerIsolation() {
148 $this->enableModules(['system', 'user']);
149 $this->assertNull($this->installConfig('user'));
153 * Tests whether the fixture can re-install modules and configuration.
155 * @depends testContainerIsolation
157 public function testSubsequentContainerIsolation() {
158 $this->enableModules(['system', 'user']);
159 $this->assertNull($this->installConfig('user'));
165 public function testRender() {
166 $type = 'processed_text';
167 $element_info = $this->container->get('element_info');
168 $this->assertSame(['#defaults_loaded' => TRUE], $element_info->getInfo($type));
170 $this->enableModules(['filter']);
172 $this->assertNotSame($element_info, $this->container->get('element_info'));
173 $this->assertNotEmpty($this->container->get('element_info')->getInfo($type));
176 '#type' => 'html_tag',
180 $expected = "<h3>Inner</h3>\n";
182 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
183 $output = \Drupal::service('renderer')->renderRoot($build);
184 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
186 $this->assertEquals($expected, $build['#markup']);
187 $this->assertEquals($expected, $output);
193 public function testRenderWithTheme() {
194 $this->enableModules(['system']);
197 '#type' => 'textfield',
200 $expected = '/' . preg_quote('<input type="text" name="test"', '/') . '/';
202 $this->assertArrayNotHasKey('theme', $GLOBALS);
203 $output = \Drupal::service('renderer')->renderRoot($build);
204 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
206 $this->assertRegExp($expected, (string) $build['#children']);
207 $this->assertRegExp($expected, (string) $output);
211 * @covers ::bootKernel
213 public function testFileDefaultScheme() {
214 $this->assertEquals('public', file_default_scheme());
215 $this->assertEquals('public', \Drupal::config('system.file')->get('default_scheme'));
219 * Tests the assumption that local time is in 'Australia/Sydney'.
221 public function testLocalTimeZone() {
222 // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
223 $this->assertEquals('Australia/Sydney', date_default_timezone_get());
227 * Tests that a test method is skipped when it requires a module not present.
229 * In order to catch checkRequirements() regressions, we have to make a new
230 * test object and run checkRequirements() here.
232 * @covers ::checkRequirements
233 * @covers ::checkModuleRequirements
235 public function testMethodRequiresModule() {
236 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleMethodTest.php';
238 $stub_test = new KernelMissingDependentModuleMethodTest();
239 // We have to setName() to the method name we're concerned with.
240 $stub_test->setName('testRequiresModule');
242 // We cannot use $this->setExpectedException() because PHPUnit would skip
243 // the test before comparing the exception type.
245 $stub_test->publicCheckRequirements();
246 $this->fail('Missing required module throws skipped test exception.');
248 catch (\PHPUnit_Framework_SkippedTestError $e) {
249 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage());
254 * Tests that a test case is skipped when it requires a module not present.
256 * In order to catch checkRequirements() regressions, we have to make a new
257 * test object and run checkRequirements() here.
259 * @covers ::checkRequirements
260 * @covers ::checkModuleRequirements
262 public function testRequiresModule() {
263 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleTest.php';
265 $stub_test = new KernelMissingDependentModuleTest();
266 // We have to setName() to the method name we're concerned with.
267 $stub_test->setName('testRequiresModule');
269 // We cannot use $this->setExpectedException() because PHPUnit would skip
270 // the test before comparing the exception type.
272 $stub_test->publicCheckRequirements();
273 $this->fail('Missing required module throws skipped test exception.');
275 catch (\PHPUnit_Framework_SkippedTestError $e) {
276 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage());
283 protected function tearDown() {
286 // Check that all tables of the test instance have been deleted. At this
287 // point the original database connection is restored so we need to prefix
289 $connection = Database::getConnection();
290 if ($connection->databaseType() != 'sqlite') {
291 $tables = $connection->schema()->findTables($this->databasePrefix . '%');
292 $this->assertTrue(empty($tables), 'All test tables have been removed.');
295 $result = $connection->query("SELECT name FROM " . $this->databasePrefix . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [
297 ':table_name' => '%',
298 ':pattern' => 'sqlite_%',
299 ])->fetchAllKeyed(0, 0);
301 $this->assertTrue(empty($result), 'All test tables have been removed.');