Mercurial > hg > isophonics-drupal-site
comparison core/tests/Drupal/KernelTests/KernelTestBaseTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\KernelTests; | |
4 | |
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; | |
9 | |
10 /** | |
11 * @coversDefaultClass \Drupal\KernelTests\KernelTestBase | |
12 * | |
13 * @group PHPUnit | |
14 * @group Test | |
15 * @group KernelTests | |
16 */ | |
17 class KernelTestBaseTest extends KernelTestBase { | |
18 | |
19 /** | |
20 * @covers ::setUpBeforeClass | |
21 */ | |
22 public function testSetUpBeforeClass() { | |
23 // Note: PHPUnit automatically restores the original working directory. | |
24 $this->assertSame(realpath(__DIR__ . '/../../../../'), getcwd()); | |
25 } | |
26 | |
27 /** | |
28 * @covers ::bootEnvironment | |
29 */ | |
30 public function testBootEnvironment() { | |
31 $this->assertRegExp('/^test\d{8}$/', $this->databasePrefix); | |
32 $this->assertStringStartsWith('vfs://root/sites/simpletest/', $this->siteDirectory); | |
33 $this->assertEquals([ | |
34 'root' => [ | |
35 'sites' => [ | |
36 'simpletest' => [ | |
37 substr($this->databasePrefix, 4) => [ | |
38 'files' => [ | |
39 'config' => [ | |
40 'sync' => [], | |
41 ], | |
42 ], | |
43 ], | |
44 ], | |
45 ], | |
46 ], | |
47 ], vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure()); | |
48 } | |
49 | |
50 /** | |
51 * @covers ::getDatabaseConnectionInfo | |
52 */ | |
53 public function testGetDatabaseConnectionInfoWithOutManualSetDbUrl() { | |
54 $options = $this->container->get('database')->getConnectionOptions(); | |
55 $this->assertSame($this->databasePrefix, $options['prefix']['default']); | |
56 } | |
57 | |
58 /** | |
59 * @covers ::setUp | |
60 */ | |
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()); | |
67 | |
68 $this->assertSame($request, \Drupal::request()); | |
69 | |
70 $this->assertEquals($this, $GLOBALS['conf']['container_service_providers']['test']); | |
71 | |
72 $GLOBALS['destroy-me'] = TRUE; | |
73 $this->assertArrayHasKey('destroy-me', $GLOBALS); | |
74 | |
75 $database = $this->container->get('database'); | |
76 $database->schema()->createTable('foo', [ | |
77 'fields' => [ | |
78 'number' => [ | |
79 'type' => 'int', | |
80 'unsigned' => TRUE, | |
81 'not null' => TRUE, | |
82 ], | |
83 ], | |
84 ]); | |
85 $this->assertTrue($database->schema()->tableExists('foo')); | |
86 | |
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()); | |
92 } | |
93 | |
94 $this->assertNotNull(FileCacheFactory::getPrefix()); | |
95 } | |
96 | |
97 /** | |
98 * @covers ::setUp | |
99 * @depends testSetUp | |
100 */ | |
101 public function testSetUpDoesNotLeak() { | |
102 $this->assertArrayNotHasKey('destroy-me', $GLOBALS); | |
103 | |
104 // Ensure that we have a different database prefix. | |
105 $schema = $this->container->get('database')->schema(); | |
106 $this->assertFalse($schema->tableExists('foo')); | |
107 } | |
108 | |
109 /** | |
110 * @covers ::register | |
111 */ | |
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()); | |
116 | |
117 // The request service should never exist. | |
118 $this->assertFalse($this->container->has('request')); | |
119 | |
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()); | |
124 | |
125 // Trigger a container rebuild. | |
126 $this->enableModules(['system']); | |
127 | |
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()); | |
131 | |
132 // The request service should never exist. | |
133 $this->assertFalse($this->container->has('request')); | |
134 | |
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); | |
140 } | |
141 | |
142 /** | |
143 * Tests whether the fixture allows us to install modules and configuration. | |
144 * | |
145 * @see ::testSubsequentContainerIsolation() | |
146 */ | |
147 public function testContainerIsolation() { | |
148 $this->enableModules(['system', 'user']); | |
149 $this->assertNull($this->installConfig('user')); | |
150 } | |
151 | |
152 /** | |
153 * Tests whether the fixture can re-install modules and configuration. | |
154 * | |
155 * @depends testContainerIsolation | |
156 */ | |
157 public function testSubsequentContainerIsolation() { | |
158 $this->enableModules(['system', 'user']); | |
159 $this->assertNull($this->installConfig('user')); | |
160 } | |
161 | |
162 /** | |
163 * @covers ::render | |
164 */ | |
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)); | |
169 | |
170 $this->enableModules(['filter']); | |
171 | |
172 $this->assertNotSame($element_info, $this->container->get('element_info')); | |
173 $this->assertNotEmpty($this->container->get('element_info')->getInfo($type)); | |
174 | |
175 $build = [ | |
176 '#type' => 'html_tag', | |
177 '#tag' => 'h3', | |
178 '#value' => 'Inner', | |
179 ]; | |
180 $expected = "<h3>Inner</h3>\n"; | |
181 | |
182 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName()); | |
183 $output = \Drupal::service('renderer')->renderRoot($build); | |
184 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName()); | |
185 | |
186 $this->assertEquals($expected, $build['#markup']); | |
187 $this->assertEquals($expected, $output); | |
188 } | |
189 | |
190 /** | |
191 * @covers ::render | |
192 */ | |
193 public function testRenderWithTheme() { | |
194 $this->enableModules(['system']); | |
195 | |
196 $build = [ | |
197 '#type' => 'textfield', | |
198 '#name' => 'test', | |
199 ]; | |
200 $expected = '/' . preg_quote('<input type="text" name="test"', '/') . '/'; | |
201 | |
202 $this->assertArrayNotHasKey('theme', $GLOBALS); | |
203 $output = \Drupal::service('renderer')->renderRoot($build); | |
204 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName()); | |
205 | |
206 $this->assertRegExp($expected, (string) $build['#children']); | |
207 $this->assertRegExp($expected, (string) $output); | |
208 } | |
209 | |
210 /** | |
211 * @covers ::bootKernel | |
212 */ | |
213 public function testFileDefaultScheme() { | |
214 $this->assertEquals('public', file_default_scheme()); | |
215 $this->assertEquals('public', \Drupal::config('system.file')->get('default_scheme')); | |
216 } | |
217 | |
218 /** | |
219 * Tests the assumption that local time is in 'Australia/Sydney'. | |
220 */ | |
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()); | |
224 } | |
225 | |
226 /** | |
227 * Tests that a test method is skipped when it requires a module not present. | |
228 * | |
229 * In order to catch checkRequirements() regressions, we have to make a new | |
230 * test object and run checkRequirements() here. | |
231 * | |
232 * @covers ::checkRequirements | |
233 * @covers ::checkModuleRequirements | |
234 */ | |
235 public function testMethodRequiresModule() { | |
236 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleMethodTest.php'; | |
237 | |
238 $stub_test = new KernelMissingDependentModuleMethodTest(); | |
239 // We have to setName() to the method name we're concerned with. | |
240 $stub_test->setName('testRequiresModule'); | |
241 | |
242 // We cannot use $this->setExpectedException() because PHPUnit would skip | |
243 // the test before comparing the exception type. | |
244 try { | |
245 $stub_test->publicCheckRequirements(); | |
246 $this->fail('Missing required module throws skipped test exception.'); | |
247 } | |
248 catch (\PHPUnit_Framework_SkippedTestError $e) { | |
249 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage()); | |
250 } | |
251 } | |
252 | |
253 /** | |
254 * Tests that a test case is skipped when it requires a module not present. | |
255 * | |
256 * In order to catch checkRequirements() regressions, we have to make a new | |
257 * test object and run checkRequirements() here. | |
258 * | |
259 * @covers ::checkRequirements | |
260 * @covers ::checkModuleRequirements | |
261 */ | |
262 public function testRequiresModule() { | |
263 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleTest.php'; | |
264 | |
265 $stub_test = new KernelMissingDependentModuleTest(); | |
266 // We have to setName() to the method name we're concerned with. | |
267 $stub_test->setName('testRequiresModule'); | |
268 | |
269 // We cannot use $this->setExpectedException() because PHPUnit would skip | |
270 // the test before comparing the exception type. | |
271 try { | |
272 $stub_test->publicCheckRequirements(); | |
273 $this->fail('Missing required module throws skipped test exception.'); | |
274 } | |
275 catch (\PHPUnit_Framework_SkippedTestError $e) { | |
276 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage()); | |
277 } | |
278 } | |
279 | |
280 /** | |
281 * {@inheritdoc} | |
282 */ | |
283 protected function tearDown() { | |
284 parent::tearDown(); | |
285 | |
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 | |
288 // the tables. | |
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.'); | |
293 } | |
294 else { | |
295 $result = $connection->query("SELECT name FROM " . $this->databasePrefix . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [ | |
296 ':type' => 'table', | |
297 ':table_name' => '%', | |
298 ':pattern' => 'sqlite_%', | |
299 ])->fetchAllKeyed(0, 0); | |
300 | |
301 $this->assertTrue(empty($result), 'All test tables have been removed.'); | |
302 } | |
303 } | |
304 | |
305 } |