KernelTestBaseTest.php 7.7 KB

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