KernelTestBaseTest.php 8.0 KB

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