Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\KernelTests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\FileCache\FileCacheFactory;
|
Chris@0
|
6 use Drupal\Core\Database\Database;
|
Chris@0
|
7 use org\bovigo\vfs\vfsStream;
|
Chris@0
|
8 use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * @coversDefaultClass \Drupal\KernelTests\KernelTestBase
|
Chris@0
|
12 *
|
Chris@0
|
13 * @group PHPUnit
|
Chris@0
|
14 * @group Test
|
Chris@0
|
15 * @group KernelTests
|
Chris@0
|
16 */
|
Chris@0
|
17 class KernelTestBaseTest extends KernelTestBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * @covers ::setUpBeforeClass
|
Chris@0
|
21 */
|
Chris@0
|
22 public function testSetUpBeforeClass() {
|
Chris@0
|
23 // Note: PHPUnit automatically restores the original working directory.
|
Chris@0
|
24 $this->assertSame(realpath(__DIR__ . '/../../../../'), getcwd());
|
Chris@0
|
25 }
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * @covers ::bootEnvironment
|
Chris@0
|
29 */
|
Chris@0
|
30 public function testBootEnvironment() {
|
Chris@0
|
31 $this->assertRegExp('/^test\d{8}$/', $this->databasePrefix);
|
Chris@0
|
32 $this->assertStringStartsWith('vfs://root/sites/simpletest/', $this->siteDirectory);
|
Chris@0
|
33 $this->assertEquals([
|
Chris@0
|
34 'root' => [
|
Chris@0
|
35 'sites' => [
|
Chris@0
|
36 'simpletest' => [
|
Chris@0
|
37 substr($this->databasePrefix, 4) => [
|
Chris@0
|
38 'files' => [
|
Chris@0
|
39 'config' => [
|
Chris@0
|
40 'sync' => [],
|
Chris@0
|
41 ],
|
Chris@0
|
42 ],
|
Chris@0
|
43 ],
|
Chris@0
|
44 ],
|
Chris@0
|
45 ],
|
Chris@0
|
46 ],
|
Chris@0
|
47 ], vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure());
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * @covers ::getDatabaseConnectionInfo
|
Chris@0
|
52 */
|
Chris@0
|
53 public function testGetDatabaseConnectionInfoWithOutManualSetDbUrl() {
|
Chris@0
|
54 $options = $this->container->get('database')->getConnectionOptions();
|
Chris@0
|
55 $this->assertSame($this->databasePrefix, $options['prefix']['default']);
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * @covers ::setUp
|
Chris@0
|
60 */
|
Chris@0
|
61 public function testSetUp() {
|
Chris@0
|
62 $this->assertTrue($this->container->has('request_stack'));
|
Chris@0
|
63 $this->assertTrue($this->container->initialized('request_stack'));
|
Chris@0
|
64 $request = $this->container->get('request_stack')->getCurrentRequest();
|
Chris@0
|
65 $this->assertNotEmpty($request);
|
Chris@0
|
66 $this->assertEquals('/', $request->getPathInfo());
|
Chris@0
|
67
|
Chris@0
|
68 $this->assertSame($request, \Drupal::request());
|
Chris@0
|
69
|
Chris@0
|
70 $this->assertEquals($this, $GLOBALS['conf']['container_service_providers']['test']);
|
Chris@0
|
71
|
Chris@0
|
72 $GLOBALS['destroy-me'] = TRUE;
|
Chris@0
|
73 $this->assertArrayHasKey('destroy-me', $GLOBALS);
|
Chris@0
|
74
|
Chris@0
|
75 $database = $this->container->get('database');
|
Chris@0
|
76 $database->schema()->createTable('foo', [
|
Chris@0
|
77 'fields' => [
|
Chris@0
|
78 'number' => [
|
Chris@0
|
79 'type' => 'int',
|
Chris@0
|
80 'unsigned' => TRUE,
|
Chris@0
|
81 'not null' => TRUE,
|
Chris@0
|
82 ],
|
Chris@0
|
83 ],
|
Chris@0
|
84 ]);
|
Chris@0
|
85 $this->assertTrue($database->schema()->tableExists('foo'));
|
Chris@0
|
86
|
Chris@0
|
87 // Ensure that the database tasks have been run during set up. Neither MySQL
|
Chris@0
|
88 // nor SQLite make changes that are testable.
|
Chris@0
|
89 if ($database->driver() == 'pgsql') {
|
Chris@0
|
90 $this->assertEquals('on', $database->query("SHOW standard_conforming_strings")->fetchField());
|
Chris@0
|
91 $this->assertEquals('escape', $database->query("SHOW bytea_output")->fetchField());
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 $this->assertNotNull(FileCacheFactory::getPrefix());
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 /**
|
Chris@0
|
98 * @covers ::setUp
|
Chris@0
|
99 * @depends testSetUp
|
Chris@0
|
100 */
|
Chris@0
|
101 public function testSetUpDoesNotLeak() {
|
Chris@0
|
102 $this->assertArrayNotHasKey('destroy-me', $GLOBALS);
|
Chris@0
|
103
|
Chris@0
|
104 // Ensure that we have a different database prefix.
|
Chris@0
|
105 $schema = $this->container->get('database')->schema();
|
Chris@0
|
106 $this->assertFalse($schema->tableExists('foo'));
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * @covers ::register
|
Chris@0
|
111 */
|
Chris@0
|
112 public function testRegister() {
|
Chris@0
|
113 // Verify that this container is identical to the actual container.
|
Chris@0
|
114 $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->container);
|
Chris@0
|
115 $this->assertSame($this->container, \Drupal::getContainer());
|
Chris@0
|
116
|
Chris@0
|
117 // The request service should never exist.
|
Chris@0
|
118 $this->assertFalse($this->container->has('request'));
|
Chris@0
|
119
|
Chris@0
|
120 // Verify that there is a request stack.
|
Chris@0
|
121 $request = $this->container->get('request_stack')->getCurrentRequest();
|
Chris@0
|
122 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $request);
|
Chris@0
|
123 $this->assertSame($request, \Drupal::request());
|
Chris@0
|
124
|
Chris@0
|
125 // Trigger a container rebuild.
|
Chris@0
|
126 $this->enableModules(['system']);
|
Chris@0
|
127
|
Chris@0
|
128 // Verify that this container is identical to the actual container.
|
Chris@0
|
129 $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->container);
|
Chris@0
|
130 $this->assertSame($this->container, \Drupal::getContainer());
|
Chris@0
|
131
|
Chris@0
|
132 // The request service should never exist.
|
Chris@0
|
133 $this->assertFalse($this->container->has('request'));
|
Chris@0
|
134
|
Chris@0
|
135 // Verify that there is a request stack (and that it persisted).
|
Chris@0
|
136 $new_request = $this->container->get('request_stack')->getCurrentRequest();
|
Chris@0
|
137 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $new_request);
|
Chris@0
|
138 $this->assertSame($new_request, \Drupal::request());
|
Chris@0
|
139 $this->assertSame($request, $new_request);
|
Chris@14
|
140
|
Chris@14
|
141 // Ensure getting the router.route_provider does not trigger a deprecation
|
Chris@14
|
142 // message that errors.
|
Chris@14
|
143 $this->container->get('router.route_provider');
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * Tests whether the fixture allows us to install modules and configuration.
|
Chris@0
|
148 *
|
Chris@0
|
149 * @see ::testSubsequentContainerIsolation()
|
Chris@0
|
150 */
|
Chris@0
|
151 public function testContainerIsolation() {
|
Chris@0
|
152 $this->enableModules(['system', 'user']);
|
Chris@0
|
153 $this->assertNull($this->installConfig('user'));
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * Tests whether the fixture can re-install modules and configuration.
|
Chris@0
|
158 *
|
Chris@0
|
159 * @depends testContainerIsolation
|
Chris@0
|
160 */
|
Chris@0
|
161 public function testSubsequentContainerIsolation() {
|
Chris@0
|
162 $this->enableModules(['system', 'user']);
|
Chris@0
|
163 $this->assertNull($this->installConfig('user'));
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 /**
|
Chris@0
|
167 * @covers ::render
|
Chris@0
|
168 */
|
Chris@0
|
169 public function testRender() {
|
Chris@0
|
170 $type = 'processed_text';
|
Chris@0
|
171 $element_info = $this->container->get('element_info');
|
Chris@0
|
172 $this->assertSame(['#defaults_loaded' => TRUE], $element_info->getInfo($type));
|
Chris@0
|
173
|
Chris@0
|
174 $this->enableModules(['filter']);
|
Chris@0
|
175
|
Chris@0
|
176 $this->assertNotSame($element_info, $this->container->get('element_info'));
|
Chris@0
|
177 $this->assertNotEmpty($this->container->get('element_info')->getInfo($type));
|
Chris@0
|
178
|
Chris@0
|
179 $build = [
|
Chris@0
|
180 '#type' => 'html_tag',
|
Chris@0
|
181 '#tag' => 'h3',
|
Chris@0
|
182 '#value' => 'Inner',
|
Chris@0
|
183 ];
|
Chris@0
|
184 $expected = "<h3>Inner</h3>\n";
|
Chris@0
|
185
|
Chris@0
|
186 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
|
Chris@0
|
187 $output = \Drupal::service('renderer')->renderRoot($build);
|
Chris@0
|
188 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
|
Chris@0
|
189
|
Chris@0
|
190 $this->assertEquals($expected, $build['#markup']);
|
Chris@0
|
191 $this->assertEquals($expected, $output);
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@0
|
195 * @covers ::render
|
Chris@0
|
196 */
|
Chris@0
|
197 public function testRenderWithTheme() {
|
Chris@0
|
198 $this->enableModules(['system']);
|
Chris@0
|
199
|
Chris@0
|
200 $build = [
|
Chris@0
|
201 '#type' => 'textfield',
|
Chris@0
|
202 '#name' => 'test',
|
Chris@0
|
203 ];
|
Chris@0
|
204 $expected = '/' . preg_quote('<input type="text" name="test"', '/') . '/';
|
Chris@0
|
205
|
Chris@0
|
206 $this->assertArrayNotHasKey('theme', $GLOBALS);
|
Chris@0
|
207 $output = \Drupal::service('renderer')->renderRoot($build);
|
Chris@0
|
208 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
|
Chris@0
|
209
|
Chris@0
|
210 $this->assertRegExp($expected, (string) $build['#children']);
|
Chris@0
|
211 $this->assertRegExp($expected, (string) $output);
|
Chris@0
|
212 }
|
Chris@0
|
213
|
Chris@0
|
214 /**
|
Chris@0
|
215 * @covers ::bootKernel
|
Chris@0
|
216 */
|
Chris@0
|
217 public function testFileDefaultScheme() {
|
Chris@0
|
218 $this->assertEquals('public', file_default_scheme());
|
Chris@0
|
219 $this->assertEquals('public', \Drupal::config('system.file')->get('default_scheme'));
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 /**
|
Chris@0
|
223 * Tests the assumption that local time is in 'Australia/Sydney'.
|
Chris@0
|
224 */
|
Chris@0
|
225 public function testLocalTimeZone() {
|
Chris@0
|
226 // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
|
Chris@0
|
227 $this->assertEquals('Australia/Sydney', date_default_timezone_get());
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 /**
|
Chris@0
|
231 * Tests that a test method is skipped when it requires a module not present.
|
Chris@0
|
232 *
|
Chris@0
|
233 * In order to catch checkRequirements() regressions, we have to make a new
|
Chris@0
|
234 * test object and run checkRequirements() here.
|
Chris@0
|
235 *
|
Chris@0
|
236 * @covers ::checkRequirements
|
Chris@0
|
237 * @covers ::checkModuleRequirements
|
Chris@0
|
238 */
|
Chris@0
|
239 public function testMethodRequiresModule() {
|
Chris@0
|
240 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleMethodTest.php';
|
Chris@0
|
241
|
Chris@0
|
242 $stub_test = new KernelMissingDependentModuleMethodTest();
|
Chris@0
|
243 // We have to setName() to the method name we're concerned with.
|
Chris@0
|
244 $stub_test->setName('testRequiresModule');
|
Chris@0
|
245
|
Chris@0
|
246 // We cannot use $this->setExpectedException() because PHPUnit would skip
|
Chris@0
|
247 // the test before comparing the exception type.
|
Chris@0
|
248 try {
|
Chris@0
|
249 $stub_test->publicCheckRequirements();
|
Chris@0
|
250 $this->fail('Missing required module throws skipped test exception.');
|
Chris@0
|
251 }
|
Chris@0
|
252 catch (\PHPUnit_Framework_SkippedTestError $e) {
|
Chris@0
|
253 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage());
|
Chris@0
|
254 }
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * Tests that a test case is skipped when it requires a module not present.
|
Chris@0
|
259 *
|
Chris@0
|
260 * In order to catch checkRequirements() regressions, we have to make a new
|
Chris@0
|
261 * test object and run checkRequirements() here.
|
Chris@0
|
262 *
|
Chris@0
|
263 * @covers ::checkRequirements
|
Chris@0
|
264 * @covers ::checkModuleRequirements
|
Chris@0
|
265 */
|
Chris@0
|
266 public function testRequiresModule() {
|
Chris@0
|
267 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleTest.php';
|
Chris@0
|
268
|
Chris@0
|
269 $stub_test = new KernelMissingDependentModuleTest();
|
Chris@0
|
270 // We have to setName() to the method name we're concerned with.
|
Chris@0
|
271 $stub_test->setName('testRequiresModule');
|
Chris@0
|
272
|
Chris@0
|
273 // We cannot use $this->setExpectedException() because PHPUnit would skip
|
Chris@0
|
274 // the test before comparing the exception type.
|
Chris@0
|
275 try {
|
Chris@0
|
276 $stub_test->publicCheckRequirements();
|
Chris@0
|
277 $this->fail('Missing required module throws skipped test exception.');
|
Chris@0
|
278 }
|
Chris@0
|
279 catch (\PHPUnit_Framework_SkippedTestError $e) {
|
Chris@0
|
280 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage());
|
Chris@0
|
281 }
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 /**
|
Chris@0
|
285 * {@inheritdoc}
|
Chris@0
|
286 */
|
Chris@0
|
287 protected function tearDown() {
|
Chris@0
|
288 parent::tearDown();
|
Chris@0
|
289
|
Chris@0
|
290 // Check that all tables of the test instance have been deleted. At this
|
Chris@0
|
291 // point the original database connection is restored so we need to prefix
|
Chris@0
|
292 // the tables.
|
Chris@0
|
293 $connection = Database::getConnection();
|
Chris@0
|
294 if ($connection->databaseType() != 'sqlite') {
|
Chris@0
|
295 $tables = $connection->schema()->findTables($this->databasePrefix . '%');
|
Chris@0
|
296 $this->assertTrue(empty($tables), 'All test tables have been removed.');
|
Chris@0
|
297 }
|
Chris@0
|
298 else {
|
Chris@0
|
299 $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
|
300 ':type' => 'table',
|
Chris@0
|
301 ':table_name' => '%',
|
Chris@0
|
302 ':pattern' => 'sqlite_%',
|
Chris@0
|
303 ])->fetchAllKeyed(0, 0);
|
Chris@0
|
304
|
Chris@0
|
305 $this->assertTrue(empty($result), 'All test tables have been removed.');
|
Chris@0
|
306 }
|
Chris@0
|
307 }
|
Chris@0
|
308
|
Chris@17
|
309 /**
|
Chris@17
|
310 * Ensures KernelTestBase tests can access modules in profiles.
|
Chris@17
|
311 */
|
Chris@17
|
312 public function testProfileModules() {
|
Chris@17
|
313 $this->assertFileExists('core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.info.yml');
|
Chris@17
|
314 $this->assertSame(
|
Chris@17
|
315 'core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.info.yml',
|
Chris@17
|
316 \Drupal::service('extension.list.module')->getPathname('demo_umami_content')
|
Chris@17
|
317 );
|
Chris@17
|
318 }
|
Chris@17
|
319
|
Chris@0
|
320 }
|