InstallerRedirectTraitTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Drupal\KernelTests\Core\Installer;
  3. use Drupal\Core\Database\Connection;
  4. use Drupal\Core\Database\Database;
  5. use Drupal\Core\Database\DatabaseExceptionWrapper;
  6. use Drupal\Core\Database\DatabaseNotFoundException;
  7. use Drupal\Core\Database\Schema;
  8. use Drupal\Core\Installer\InstallerRedirectTrait;
  9. use Drupal\KernelTests\KernelTestBase;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. /**
  12. * @coversDefaultClass \Drupal\Core\Installer\InstallerRedirectTrait
  13. *
  14. * @group Installer
  15. */
  16. class InstallerRedirectTraitTest extends KernelTestBase {
  17. /**
  18. * Data provider for testShouldRedirectToInstaller().
  19. *
  20. * @return array
  21. * - Expected result from shouldRedirectToInstaller().
  22. * - Exceptions to be handled by shouldRedirectToInstaller()
  23. * - Whether or not there is a database connection.
  24. * - Whether or not there is database connection info.
  25. * - Whether or not there exists a sessions table in the database.
  26. */
  27. public function providerShouldRedirectToInstaller() {
  28. return [
  29. [TRUE, DatabaseNotFoundException::class, FALSE, FALSE],
  30. [TRUE, DatabaseNotFoundException::class, TRUE, FALSE],
  31. [TRUE, DatabaseNotFoundException::class, FALSE, TRUE],
  32. [TRUE, DatabaseNotFoundException::class, TRUE, TRUE],
  33. [TRUE, DatabaseNotFoundException::class, TRUE, TRUE, FALSE],
  34. [TRUE, \PDOException::class, FALSE, FALSE],
  35. [TRUE, \PDOException::class, TRUE, FALSE],
  36. [FALSE, \PDOException::class, FALSE, TRUE],
  37. [FALSE, \PDOException::class, TRUE, TRUE],
  38. [TRUE, \PDOException::class, TRUE, TRUE, FALSE],
  39. [TRUE, DatabaseExceptionWrapper::class, FALSE, FALSE],
  40. [TRUE, DatabaseExceptionWrapper::class, TRUE, FALSE],
  41. [FALSE, DatabaseExceptionWrapper::class, FALSE, TRUE],
  42. [FALSE, DatabaseExceptionWrapper::class, TRUE, TRUE],
  43. [TRUE, DatabaseExceptionWrapper::class, TRUE, TRUE, FALSE],
  44. [TRUE, NotFoundHttpException::class, FALSE, FALSE],
  45. [TRUE, NotFoundHttpException::class, TRUE, FALSE],
  46. [FALSE, NotFoundHttpException::class, FALSE, TRUE],
  47. [FALSE, NotFoundHttpException::class, TRUE, TRUE],
  48. [TRUE, NotFoundHttpException::class, TRUE, TRUE, FALSE],
  49. [FALSE, \Exception::class, FALSE, FALSE],
  50. [FALSE, \Exception::class, TRUE, FALSE],
  51. [FALSE, \Exception::class, FALSE, TRUE],
  52. [FALSE, \Exception::class, TRUE, TRUE],
  53. [FALSE, \Exception::class, TRUE, TRUE, FALSE],
  54. ];
  55. }
  56. /**
  57. * @covers ::shouldRedirectToInstaller
  58. * @dataProvider providerShouldRedirectToInstaller
  59. */
  60. public function testShouldRedirectToInstaller($expected, $exception, $connection, $connection_info, $session_table_exists = TRUE) {
  61. try {
  62. throw new $exception();
  63. }
  64. catch (\Exception $e) {
  65. // Mock the trait.
  66. $trait = $this->getMockBuilder(InstallerRedirectTrait::class)
  67. ->setMethods(['isCli'])
  68. ->getMockForTrait();
  69. // Make sure that the method thinks we are not using the cli.
  70. $trait->expects($this->any())
  71. ->method('isCli')
  72. ->willReturn(FALSE);
  73. // Un-protect the method using reflection.
  74. $method_ref = new \ReflectionMethod($trait, 'shouldRedirectToInstaller');
  75. $method_ref->setAccessible(TRUE);
  76. // Mock the database connection info.
  77. $db = $this->getMockForAbstractClass(Database::class);
  78. $property_ref = new \ReflectionProperty($db, 'databaseInfo');
  79. $property_ref->setAccessible(TRUE);
  80. $property_ref->setValue($db, ['default' => $connection_info]);
  81. if ($connection) {
  82. // Mock the database connection.
  83. $connection = $this->getMockBuilder(Connection::class)
  84. ->disableOriginalConstructor()
  85. ->setMethods(['schema'])
  86. ->getMockForAbstractClass();
  87. if ($connection_info) {
  88. // Mock the database schema class.
  89. $schema = $this->getMockBuilder(Schema::class)
  90. ->disableOriginalConstructor()
  91. ->setMethods(['tableExists'])
  92. ->getMockForAbstractClass();
  93. $schema->expects($this->any())
  94. ->method('tableExists')
  95. ->with('sessions')
  96. ->willReturn($session_table_exists);
  97. $connection->expects($this->any())
  98. ->method('schema')
  99. ->willReturn($schema);
  100. }
  101. }
  102. else {
  103. // Set the database connection if there is none.
  104. $connection = NULL;
  105. }
  106. // Call shouldRedirectToInstaller.
  107. $this->assertSame($expected, $method_ref->invoke($trait, $e, $connection));
  108. }
  109. }
  110. }