Mercurial > hg > cmmr2012-drupal-site
comparison core/tests/Drupal/KernelTests/KernelTestBaseTest.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
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 // Ensure getting the router.route_provider does not trigger a deprecation | |
142 // message that errors. | |
143 $this->container->get('router.route_provider'); | |
144 } | |
145 | |
146 /** | |
147 * Tests whether the fixture allows us to install modules and configuration. | |
148 * | |
149 * @see ::testSubsequentContainerIsolation() | |
150 */ | |
151 public function testContainerIsolation() { | |
152 $this->enableModules(['system', 'user']); | |
153 $this->assertNull($this->installConfig('user')); | |
154 } | |
155 | |
156 /** | |
157 * Tests whether the fixture can re-install modules and configuration. | |
158 * | |
159 * @depends testContainerIsolation | |
160 */ | |
161 public function testSubsequentContainerIsolation() { | |
162 $this->enableModules(['system', 'user']); | |
163 $this->assertNull($this->installConfig('user')); | |
164 } | |
165 | |
166 /** | |
167 * @covers ::render | |
168 */ | |
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)); | |
173 | |
174 $this->enableModules(['filter']); | |
175 | |
176 $this->assertNotSame($element_info, $this->container->get('element_info')); | |
177 $this->assertNotEmpty($this->container->get('element_info')->getInfo($type)); | |
178 | |
179 $build = [ | |
180 '#type' => 'html_tag', | |
181 '#tag' => 'h3', | |
182 '#value' => 'Inner', | |
183 ]; | |
184 $expected = "<h3>Inner</h3>\n"; | |
185 | |
186 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName()); | |
187 $output = \Drupal::service('renderer')->renderRoot($build); | |
188 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName()); | |
189 | |
190 $this->assertEquals($expected, $build['#markup']); | |
191 $this->assertEquals($expected, $output); | |
192 } | |
193 | |
194 /** | |
195 * @covers ::render | |
196 */ | |
197 public function testRenderWithTheme() { | |
198 $this->enableModules(['system']); | |
199 | |
200 $build = [ | |
201 '#type' => 'textfield', | |
202 '#name' => 'test', | |
203 ]; | |
204 $expected = '/' . preg_quote('<input type="text" name="test"', '/') . '/'; | |
205 | |
206 $this->assertArrayNotHasKey('theme', $GLOBALS); | |
207 $output = \Drupal::service('renderer')->renderRoot($build); | |
208 $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName()); | |
209 | |
210 $this->assertRegExp($expected, (string) $build['#children']); | |
211 $this->assertRegExp($expected, (string) $output); | |
212 } | |
213 | |
214 /** | |
215 * @covers ::bootKernel | |
216 */ | |
217 public function testFileDefaultScheme() { | |
218 $this->assertEquals('public', file_default_scheme()); | |
219 $this->assertEquals('public', \Drupal::config('system.file')->get('default_scheme')); | |
220 } | |
221 | |
222 /** | |
223 * Tests the assumption that local time is in 'Australia/Sydney'. | |
224 */ | |
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()); | |
228 } | |
229 | |
230 /** | |
231 * Tests that a test method is skipped when it requires a module not present. | |
232 * | |
233 * In order to catch checkRequirements() regressions, we have to make a new | |
234 * test object and run checkRequirements() here. | |
235 * | |
236 * @covers ::checkRequirements | |
237 * @covers ::checkModuleRequirements | |
238 */ | |
239 public function testMethodRequiresModule() { | |
240 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleMethodTest.php'; | |
241 | |
242 $stub_test = new KernelMissingDependentModuleMethodTest(); | |
243 // We have to setName() to the method name we're concerned with. | |
244 $stub_test->setName('testRequiresModule'); | |
245 | |
246 // We cannot use $this->setExpectedException() because PHPUnit would skip | |
247 // the test before comparing the exception type. | |
248 try { | |
249 $stub_test->publicCheckRequirements(); | |
250 $this->fail('Missing required module throws skipped test exception.'); | |
251 } | |
252 catch (\PHPUnit_Framework_SkippedTestError $e) { | |
253 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage()); | |
254 } | |
255 } | |
256 | |
257 /** | |
258 * Tests that a test case is skipped when it requires a module not present. | |
259 * | |
260 * In order to catch checkRequirements() regressions, we have to make a new | |
261 * test object and run checkRequirements() here. | |
262 * | |
263 * @covers ::checkRequirements | |
264 * @covers ::checkModuleRequirements | |
265 */ | |
266 public function testRequiresModule() { | |
267 require __DIR__ . '/../../fixtures/KernelMissingDependentModuleTest.php'; | |
268 | |
269 $stub_test = new KernelMissingDependentModuleTest(); | |
270 // We have to setName() to the method name we're concerned with. | |
271 $stub_test->setName('testRequiresModule'); | |
272 | |
273 // We cannot use $this->setExpectedException() because PHPUnit would skip | |
274 // the test before comparing the exception type. | |
275 try { | |
276 $stub_test->publicCheckRequirements(); | |
277 $this->fail('Missing required module throws skipped test exception.'); | |
278 } | |
279 catch (\PHPUnit_Framework_SkippedTestError $e) { | |
280 $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage()); | |
281 } | |
282 } | |
283 | |
284 /** | |
285 * {@inheritdoc} | |
286 */ | |
287 protected function tearDown() { | |
288 parent::tearDown(); | |
289 | |
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 | |
292 // the tables. | |
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.'); | |
297 } | |
298 else { | |
299 $result = $connection->query("SELECT name FROM " . $this->databasePrefix . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [ | |
300 ':type' => 'table', | |
301 ':table_name' => '%', | |
302 ':pattern' => 'sqlite_%', | |
303 ])->fetchAllKeyed(0, 0); | |
304 | |
305 $this->assertTrue(empty($result), 'All test tables have been removed.'); | |
306 } | |
307 } | |
308 | |
309 } |