8ec7c5497a107164c047d6e1a9b98a7e2904f07b
[yaffs-website] / web / core / tests / Drupal / KernelTests / KernelTestBaseTest.php
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  * @group PHPUnit
13  * @group Test
14  * @group KernelTests
15  */
16 class KernelTestBaseTest extends KernelTestBase {
17
18   /**
19    * @covers ::setUpBeforeClass
20    */
21   public function testSetUpBeforeClass() {
22     // Note: PHPUnit automatically restores the original working directory.
23     $this->assertSame(realpath(__DIR__ . '/../../../../'), getcwd());
24   }
25
26   /**
27    * @covers ::bootEnvironment
28    */
29   public function testBootEnvironment() {
30     $this->assertRegExp('/^test\d{8}$/', $this->databasePrefix);
31     $this->assertStringStartsWith('vfs://root/sites/simpletest/', $this->siteDirectory);
32     $this->assertEquals([
33       'root' => [
34         'sites' => [
35           'simpletest' => [
36             substr($this->databasePrefix, 4) => [
37               'files' => [
38                 'config' => [
39                   'sync' => [],
40                 ],
41               ],
42             ],
43           ],
44         ],
45       ],
46     ], vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure());
47   }
48
49   /**
50    * @covers ::getDatabaseConnectionInfo
51    */
52   public function testGetDatabaseConnectionInfoWithOutManualSetDbUrl() {
53     $options = $this->container->get('database')->getConnectionOptions();
54     $this->assertSame($this->databasePrefix, $options['prefix']['default']);
55   }
56
57   /**
58    * @covers ::setUp
59    */
60   public function testSetUp() {
61     $this->assertTrue($this->container->has('request_stack'));
62     $this->assertTrue($this->container->initialized('request_stack'));
63     $request = $this->container->get('request_stack')->getCurrentRequest();
64     $this->assertNotEmpty($request);
65     $this->assertEquals('/', $request->getPathInfo());
66
67     $this->assertSame($request, \Drupal::request());
68
69     $this->assertEquals($this, $GLOBALS['conf']['container_service_providers']['test']);
70
71     $GLOBALS['destroy-me'] = TRUE;
72     $this->assertArrayHasKey('destroy-me', $GLOBALS);
73
74     $database = $this->container->get('database');
75     $database->schema()->createTable('foo', [
76       'fields' => [
77         'number' => [
78           'type' => 'int',
79           'unsigned' => TRUE,
80           'not null' => TRUE,
81         ],
82       ],
83     ]);
84     $this->assertTrue($database->schema()->tableExists('foo'));
85
86     // Ensure that the database tasks have been run during set up. Neither MySQL
87     // nor SQLite make changes that are testable.
88     if ($database->driver() == 'pgsql') {
89       $this->assertEquals('on', $database->query("SHOW standard_conforming_strings")->fetchField());
90       $this->assertEquals('escape', $database->query("SHOW bytea_output")->fetchField());
91     }
92
93     $this->assertNotNull(FileCacheFactory::getPrefix());
94   }
95
96   /**
97    * @covers ::setUp
98    * @depends testSetUp
99    */
100   public function testSetUpDoesNotLeak() {
101     $this->assertArrayNotHasKey('destroy-me', $GLOBALS);
102
103     // Ensure that we have a different database prefix.
104     $schema = $this->container->get('database')->schema();
105     $this->assertFalse($schema->tableExists('foo'));
106   }
107
108   /**
109    * @covers ::register
110    */
111   public function testRegister() {
112     // Verify that this container is identical to the actual container.
113     $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->container);
114     $this->assertSame($this->container, \Drupal::getContainer());
115
116     // The request service should never exist.
117     $this->assertFalse($this->container->has('request'));
118
119     // Verify that there is a request stack.
120     $request = $this->container->get('request_stack')->getCurrentRequest();
121     $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $request);
122     $this->assertSame($request, \Drupal::request());
123
124     // Trigger a container rebuild.
125     $this->enableModules(['system']);
126
127     // Verify that this container is identical to the actual container.
128     $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->container);
129     $this->assertSame($this->container, \Drupal::getContainer());
130
131     // The request service should never exist.
132     $this->assertFalse($this->container->has('request'));
133
134     // Verify that there is a request stack (and that it persisted).
135     $new_request = $this->container->get('request_stack')->getCurrentRequest();
136     $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $new_request);
137     $this->assertSame($new_request, \Drupal::request());
138     $this->assertSame($request, $new_request);
139   }
140
141   /**
142    * Tests whether the fixture allows us to install modules and configuration.
143    *
144    * @see ::testSubsequentContainerIsolation()
145    */
146   public function testContainerIsolation() {
147     $this->enableModules(['system', 'user']);
148     $this->assertNull($this->installConfig('user'));
149   }
150
151   /**
152    * Tests whether the fixture can re-install modules and configuration.
153    *
154    * @depends testContainerIsolation
155    */
156   public function testSubsequentContainerIsolation() {
157     $this->enableModules(['system', 'user']);
158     $this->assertNull($this->installConfig('user'));
159   }
160
161   /**
162    * @covers ::render
163    */
164   public function testRender() {
165     $type = 'processed_text';
166     $element_info = $this->container->get('element_info');
167     $this->assertSame(['#defaults_loaded' => TRUE], $element_info->getInfo($type));
168
169     $this->enableModules(['filter']);
170
171     $this->assertNotSame($element_info, $this->container->get('element_info'));
172     $this->assertNotEmpty($this->container->get('element_info')->getInfo($type));
173
174     $build = [
175       '#type' => 'html_tag',
176       '#tag' => 'h3',
177       '#value' => 'Inner',
178     ];
179     $expected = "<h3>Inner</h3>\n";
180
181     $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
182     $output = \Drupal::service('renderer')->renderRoot($build);
183     $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
184
185     $this->assertEquals($expected, $build['#children']);
186     $this->assertEquals($expected, $output);
187   }
188
189   /**
190    * @covers ::render
191    */
192   public function testRenderWithTheme() {
193     $this->enableModules(['system']);
194
195     $build = [
196       '#type' => 'textfield',
197       '#name' => 'test',
198     ];
199     $expected = '/' . preg_quote('<input type="text" name="test"', '/') . '/';
200
201     $this->assertArrayNotHasKey('theme', $GLOBALS);
202     $output = \Drupal::service('renderer')->renderRoot($build);
203     $this->assertEquals('core', \Drupal::theme()->getActiveTheme()->getName());
204
205     $this->assertRegExp($expected, (string) $build['#children']);
206     $this->assertRegExp($expected, (string) $output);
207   }
208
209   /**
210    * @covers ::bootKernel
211    */
212   public function testFileDefaultScheme() {
213     $this->assertEquals('public', file_default_scheme());
214     $this->assertEquals('public', \Drupal::config('system.file')->get('default_scheme'));
215   }
216
217   /**
218    * {@inheritdoc}
219    */
220   protected function tearDown() {
221     parent::tearDown();
222
223     // Check that all tables of the test instance have been deleted. At this
224     // point the original database connection is restored so we need to prefix
225     // the tables.
226     $connection = Database::getConnection();
227     if ($connection->databaseType() != 'sqlite') {
228       $tables = $connection->schema()->findTables($this->databasePrefix . '%');
229       $this->assertTrue(empty($tables), 'All test tables have been removed.');
230     }
231     else {
232       $result = $connection->query("SELECT name FROM " . $this->databasePrefix . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [
233         ':type' => 'table',
234         ':table_name' => '%',
235         ':pattern' => 'sqlite_%',
236       ])->fetchAllKeyed(0, 0);
237
238       $this->assertTrue(empty($result), 'All test tables have been removed.');
239     }
240   }
241
242 }