3 namespace Drupal\KernelTests\Core\Installer;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Database\DatabaseExceptionWrapper;
8 use Drupal\Core\Database\DatabaseNotFoundException;
9 use Drupal\Core\Database\Schema;
10 use Drupal\Core\Installer\InstallerRedirectTrait;
11 use Drupal\KernelTests\KernelTestBase;
12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15 * @coversDefaultClass \Drupal\Core\Installer\InstallerRedirectTrait
19 class InstallerRedirectTraitTest extends KernelTestBase {
22 * Data provider for testShouldRedirectToInstaller().
25 * - Expected result from shouldRedirectToInstaller().
26 * - Exceptions to be handled by shouldRedirectToInstaller()
27 * - Whether or not there is a database connection.
28 * - Whether or not there is database connection info.
29 * - Whether or not there exists a sessions table in the database.
31 public function providerShouldRedirectToInstaller() {
33 [TRUE, DatabaseNotFoundException::class, FALSE, FALSE],
34 [TRUE, DatabaseNotFoundException::class, TRUE, FALSE],
35 [TRUE, DatabaseNotFoundException::class, FALSE, TRUE],
36 [TRUE, DatabaseNotFoundException::class, TRUE, TRUE],
37 [TRUE, DatabaseNotFoundException::class, TRUE, TRUE, FALSE],
39 [TRUE, \PDOException::class, FALSE, FALSE],
40 [TRUE, \PDOException::class, TRUE, FALSE],
41 [FALSE, \PDOException::class, FALSE, TRUE],
42 [FALSE, \PDOException::class, TRUE, TRUE],
43 [TRUE, \PDOException::class, TRUE, TRUE, FALSE],
45 [TRUE, DatabaseExceptionWrapper::class, FALSE, FALSE],
46 [TRUE, DatabaseExceptionWrapper::class, TRUE, FALSE],
47 [FALSE, DatabaseExceptionWrapper::class, FALSE, TRUE],
48 [FALSE, DatabaseExceptionWrapper::class, TRUE, TRUE],
49 [TRUE, DatabaseExceptionWrapper::class, TRUE, TRUE, FALSE],
51 [TRUE, NotFoundHttpException::class, FALSE, FALSE],
52 [TRUE, NotFoundHttpException::class, TRUE, FALSE],
53 [FALSE, NotFoundHttpException::class, FALSE, TRUE],
54 [FALSE, NotFoundHttpException::class, TRUE, TRUE],
55 [TRUE, NotFoundHttpException::class, TRUE, TRUE, FALSE],
57 [FALSE, \Exception::class, FALSE, FALSE],
58 [FALSE, \Exception::class, TRUE, FALSE],
59 [FALSE, \Exception::class, FALSE, TRUE],
60 [FALSE, \Exception::class, TRUE, TRUE],
61 [FALSE, \Exception::class, TRUE, TRUE, FALSE],
66 * @covers ::shouldRedirectToInstaller
67 * @dataProvider providerShouldRedirectToInstaller
69 public function testShouldRedirectToInstaller($expected, $exception, $connection, $connection_info, $session_table_exists = TRUE) {
71 throw new $exception();
73 catch (\Exception $e) {
75 $trait = $this->getMockBuilder(InstallerRedirectTrait::class)
76 ->setMethods(['isCli'])
79 // Make sure that the method thinks we are not using the cli.
80 $trait->expects($this->any())
84 // Un-protect the method using reflection.
85 $method_ref = new \ReflectionMethod($trait, 'shouldRedirectToInstaller');
86 $method_ref->setAccessible(TRUE);
88 // Mock the database connection info.
89 $db = $this->getMockForAbstractClass(Database::class);
90 $property_ref = new \ReflectionProperty($db, 'databaseInfo');
91 $property_ref->setAccessible(TRUE);
92 $property_ref->setValue($db, ['default' => $connection_info]);
95 // Mock the database connection.
96 $connection = $this->getMockBuilder(Connection::class)
97 ->disableOriginalConstructor()
98 ->setMethods(['schema'])
99 ->getMockForAbstractClass();
101 if ($connection_info) {
102 // Mock the database schema class.
103 $schema = $this->getMockBuilder(Schema::class)
104 ->disableOriginalConstructor()
105 ->setMethods(['tableExists'])
106 ->getMockForAbstractClass();
108 $schema->expects($this->any())
109 ->method('tableExists')
111 ->willReturn($session_table_exists);
113 $connection->expects($this->any())
115 ->willReturn($schema);
119 // Set the database connection if there is none.
123 // Call shouldRedirectToInstaller.
124 $this->assertSame($expected, $method_ref->invoke($trait, $e, $connection));