Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Installer / InstallerRedirectTraitTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Installer;
4
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;
13
14 /**
15  * @coversDefaultClass \Drupal\Core\Installer\InstallerRedirectTrait
16  *
17  * @group Installer
18  */
19 class InstallerRedirectTraitTest extends KernelTestBase {
20
21   /**
22    * Data provider for testShouldRedirectToInstaller().
23    *
24    * @return array
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.
30    */
31   public function providerShouldRedirectToInstaller() {
32     return [
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],
38
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],
44
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],
50
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],
56
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],
62     ];
63   }
64
65   /**
66    * @covers ::shouldRedirectToInstaller
67    * @dataProvider providerShouldRedirectToInstaller
68    */
69   public function testShouldRedirectToInstaller($expected, $exception, $connection, $connection_info, $session_table_exists = TRUE) {
70     try {
71       throw new $exception();
72     }
73     catch (\Exception $e) {
74       // Mock the trait.
75       $trait = $this->getMockBuilder(InstallerRedirectTrait::class)
76         ->setMethods(['isCli'])
77         ->getMockForTrait();
78
79       // Make sure that the method thinks we are not using the cli.
80       $trait->expects($this->any())
81         ->method('isCli')
82         ->willReturn(FALSE);
83
84       // Un-protect the method using reflection.
85       $method_ref = new \ReflectionMethod($trait, 'shouldRedirectToInstaller');
86       $method_ref->setAccessible(TRUE);
87
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]);
93
94       if ($connection) {
95         // Mock the database connection.
96         $connection = $this->getMockBuilder(Connection::class)
97           ->disableOriginalConstructor()
98           ->setMethods(['schema'])
99           ->getMockForAbstractClass();
100
101         if ($connection_info) {
102           // Mock the database schema class.
103           $schema = $this->getMockBuilder(Schema::class)
104             ->disableOriginalConstructor()
105             ->setMethods(['tableExists'])
106             ->getMockForAbstractClass();
107
108           $schema->expects($this->any())
109             ->method('tableExists')
110             ->with('sessions')
111             ->willReturn($session_table_exists);
112
113           $connection->expects($this->any())
114             ->method('schema')
115             ->willReturn($schema);
116         }
117       }
118       else {
119         // Set the database connection if there is none.
120         $connection = NULL;
121       }
122
123       // Call shouldRedirectToInstaller.
124       $this->assertSame($expected, $method_ref->invoke($trait, $e, $connection));
125     }
126   }
127
128 }