KernelTestBaseTest.php 10 KB

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