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@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 /**
|
Chris@0
|
143 * Tests whether the fixture allows us to install modules and configuration.
|
Chris@0
|
144 *
|
Chris@0
|
145 * @see ::testSubsequentContainerIsolation()
|
Chris@0
|
146 */
|
Chris@0
|
147 public function testContainerIsolation() {
|
Chris@0
|
148 $this->enableModules(['system', 'user']);
|
Chris@0
|
149 $this->assertNull($this->installConfig('user'));
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * Tests whether the fixture can re-install modules and configuration.
|
Chris@0
|
154 *
|
Chris@0
|
155 * @depends testContainerIsolation
|
Chris@0
|
156 */
|
Chris@0
|
157 public function testSubsequentContainerIsolation() {
|
Chris@0
|
158 $this->enableModules(['system', 'user']);
|
Chris@0
|
159 $this->assertNull($this->installConfig('user'));
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * @covers ::render
|
Chris@0
|
164 */
|
Chris@0
|
165 public function testRender() {
|
Chris@0
|
166 $type = 'processed_text';
|
Chris@0
|
167 $element_info = $this->container->get('element_info');
|
Chris@0
|
168 $this->assertSame(['#defaults_loaded' => TRUE], $element_info->getInfo($type));
|
Chris@0
|
169
|
Chris@0
|
170 $this->enableModules(['filter']);
|
Chris@0
|
171
|
Chris@0
|
172 $this->assertNotSame($element_info, $this->container->get('element_info'));
|
Chris@0
|
173 $this->assertNotEmpty($this->container->get('element_info')->getInfo($type));
|
Chris@0
|
174
|
Chris@0
|
175 $build = [
|
Chris@0
|
176 '#type' => 'html_tag',
|
Chris@0
|
177 '#tag' => 'h3',
|
Chris@0
|
178 '#value' => 'Inner',
|
Chris@0
|
179 ];
|
Chris@0
|
180 $expected = "<h3>Inner</h3>\n";
|
Chris@0
|
181
|
Chris@0
|
182 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
|
Chris@0
|
183 $output = \Drupal::service('renderer')->renderRoot($build);
|
Chris@0
|
184 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
|
Chris@0
|
185
|
Chris@0
|
186 $this->assertEquals($expected, $build['#markup']);
|
Chris@0
|
187 $this->assertEquals($expected, $output);
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 /**
|
Chris@0
|
191 * @covers ::render
|
Chris@0
|
192 */
|
Chris@0
|
193 public function testRenderWithTheme() {
|
Chris@0
|
194 $this->enableModules(['system']);
|
Chris@0
|
195
|
Chris@0
|
196 $build = [
|
Chris@0
|
197 '#type' => 'textfield',
|
Chris@0
|
198 '#name' => 'test',
|
Chris@0
|
199 ];
|
Chris@0
|
200 $expected = '/' . preg_quote('<input type="text" name="test"', '/') . '/';
|
Chris@0
|
201
|
Chris@0
|
202 $this->assertArrayNotHasKey('theme', $GLOBALS);
|
Chris@0
|
203 $output = \Drupal::service('renderer')->renderRoot($build);
|
Chris@0
|
204 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
|
Chris@0
|
205
|
Chris@0
|
206 $this->assertRegExp($expected, (string) $build['#children']);
|
Chris@0
|
207 $this->assertRegExp($expected, (string) $output);
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 /**
|
Chris@0
|
211 * @covers ::bootKernel
|
Chris@0
|
212 */
|
Chris@0
|
213 public function testFileDefaultScheme() {
|
Chris@0
|
214 $this->assertEquals('public', file_default_scheme());
|
Chris@0
|
215 $this->assertEquals('public', \Drupal::config('system.file')->get('default_scheme'));
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 /**
|
Chris@0
|
219 * Tests the assumption that local time is in 'Australia/Sydney'.
|
Chris@0
|
220 */
|
Chris@0
|
221 public function testLocalTimeZone() {
|
Chris@0
|
222 // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
|
Chris@0
|
223 $this->assertEquals('Australia/Sydney', date_default_timezone_get());
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 /**
|
Chris@0
|
227 * Tests that a test method is skipped when it requires a module not present.
|
Chris@0
|
228 *
|
Chris@0
|
229 * In order to catch checkRequirements() regressions, we have to make a new
|
Chris@0
|
230 * test object and run checkRequirements() here.
|
Chris@0
|
231 *
|
Chris@0
|
232 * @covers ::checkRequirements
|
Chris@0
|
233 * @covers ::checkModuleRequirements
|
Chris@0
|
234 */
|
Chris@0
|
235 public function testMethodRequiresModule() {
|
Chris@0
|
236 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleMethodTest.php';
|
Chris@0
|
237
|
Chris@0
|
238 $stub_test = new KernelMissingDependentModuleMethodTest();
|
Chris@0
|
239 // We have to setName() to the method name we're concerned with.
|
Chris@0
|
240 $stub_test->setName('testRequiresModule');
|
Chris@0
|
241
|
Chris@0
|
242 // We cannot use $this->setExpectedException() because PHPUnit would skip
|
Chris@0
|
243 // the test before comparing the exception type.
|
Chris@0
|
244 try {
|
Chris@0
|
245 $stub_test->publicCheckRequirements();
|
Chris@0
|
246 $this->fail('Missing required module throws skipped test exception.');
|
Chris@0
|
247 }
|
Chris@0
|
248 catch (\PHPUnit_Framework_SkippedTestError $e) {
|
Chris@0
|
249 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage());
|
Chris@0
|
250 }
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 /**
|
Chris@0
|
254 * Tests that a test case is skipped when it requires a module not present.
|
Chris@0
|
255 *
|
Chris@0
|
256 * In order to catch checkRequirements() regressions, we have to make a new
|
Chris@0
|
257 * test object and run checkRequirements() here.
|
Chris@0
|
258 *
|
Chris@0
|
259 * @covers ::checkRequirements
|
Chris@0
|
260 * @covers ::checkModuleRequirements
|
Chris@0
|
261 */
|
Chris@0
|
262 public function testRequiresModule() {
|
Chris@0
|
263 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleTest.php';
|
Chris@0
|
264
|
Chris@0
|
265 $stub_test = new KernelMissingDependentModuleTest();
|
Chris@0
|
266 // We have to setName() to the method name we're concerned with.
|
Chris@0
|
267 $stub_test->setName('testRequiresModule');
|
Chris@0
|
268
|
Chris@0
|
269 // We cannot use $this->setExpectedException() because PHPUnit would skip
|
Chris@0
|
270 // the test before comparing the exception type.
|
Chris@0
|
271 try {
|
Chris@0
|
272 $stub_test->publicCheckRequirements();
|
Chris@0
|
273 $this->fail('Missing required module throws skipped test exception.');
|
Chris@0
|
274 }
|
Chris@0
|
275 catch (\PHPUnit_Framework_SkippedTestError $e) {
|
Chris@0
|
276 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage());
|
Chris@0
|
277 }
|
Chris@0
|
278 }
|
Chris@0
|
279
|
Chris@0
|
280 /**
|
Chris@0
|
281 * {@inheritdoc}
|
Chris@0
|
282 */
|
Chris@0
|
283 protected function tearDown() {
|
Chris@0
|
284 parent::tearDown();
|
Chris@0
|
285
|
Chris@0
|
286 // Check that all tables of the test instance have been deleted. At this
|
Chris@0
|
287 // point the original database connection is restored so we need to prefix
|
Chris@0
|
288 // the tables.
|
Chris@0
|
289 $connection = Database::getConnection();
|
Chris@0
|
290 if ($connection->databaseType() != 'sqlite') {
|
Chris@0
|
291 $tables = $connection->schema()->findTables($this->databasePrefix . '%');
|
Chris@0
|
292 $this->assertTrue(empty($tables), 'All test tables have been removed.');
|
Chris@0
|
293 }
|
Chris@0
|
294 else {
|
Chris@0
|
295 $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
|
296 ':type' => 'table',
|
Chris@0
|
297 ':table_name' => '%',
|
Chris@0
|
298 ':pattern' => 'sqlite_%',
|
Chris@0
|
299 ])->fetchAllKeyed(0, 0);
|
Chris@0
|
300
|
Chris@0
|
301 $this->assertTrue(empty($result), 'All test tables have been removed.');
|
Chris@0
|
302 }
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 }
|