diff --git a/core/lib/Drupal/Core/Installer/CheckInstalledTrait.php b/core/lib/Drupal/Core/Installer/CheckInstalledTrait.php index 2894a76..821352a 100644 --- a/core/lib/Drupal/Core/Installer/CheckInstalledTrait.php +++ b/core/lib/Drupal/Core/Installer/CheckInstalledTrait.php @@ -14,6 +14,15 @@ trait CheckInstalledTrait { /** + * Returns whether the current PHP process runs on CLI. + * + * @return bool + */ + protected function isCli() { + return PHP_SAPI === 'cli'; + } + + /** * Determines if an exception handler should redirect to the installer. * * @param \Exception $exception @@ -29,27 +38,28 @@ * Drupal is not installed yet, or FALSE otherwise. */ protected function shouldRedirectToInstaller(\Exception $exception, Connection $connection = NULL) { - // To avoid unnecessary queries, only act if the exception is one that is - // expected to occur when Drupal has not yet been installed. This includes - // NotFoundHttpException because an uninstalled site won't have route - // information available yet and therefore can return 404 errors. - if (!($exception instanceof \PDOException || $exception instanceof DatabaseException || $exception instanceof NotFoundHttpException)) { + // Never redirect on the command line. + if ($this->isCli()) { return FALSE; } - // Never redirect on the command line. - if (PHP_SAPI === 'cli') { + // Never redirect if we're already in the installer. + if (drupal_installation_attempted()) { return FALSE; } // If the database wasn't found, assume the user hasn't entered it properly - // and redirect to the installer. + // and redirect to the installer. This check needs to come first because a + // DatabaseNotFoundException is also an instance of DatabaseException. if ($exception instanceof DatabaseNotFoundException) { return TRUE; } - // Never redirect if we're already in the installer. - if (drupal_installation_attempted()) { + // To avoid unnecessary queries, only act if the exception is one that is + // expected to occur when Drupal has not yet been installed. This includes + // NotFoundHttpException because an uninstalled site won't have route + // information available yet and therefore can return 404 errors. + if (!($exception instanceof \PDOException || $exception instanceof DatabaseException || $exception instanceof NotFoundHttpException)) { return FALSE; } diff --git a/core/tests/Drupal/Tests/Core/Installer/CheckInstalledTraitTest.php b/core/tests/Drupal/Tests/Core/Installer/CheckInstalledTraitTest.php new file mode 100644 index 0000000..abb7f20 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Installer/CheckInstalledTraitTest.php @@ -0,0 +1,128 @@ +getMockBuilder('Drupal\Core\Installer\CheckInstalledTrait') + ->setMethods(array('isCli')) + ->getMockForTrait(); + + // Make sure that the method thinks we are not using the cli. + $trait->expects($this->any()) + ->method('isCli') + ->willReturn(FALSE); + + // Un-protect the method using reflection. + $method_ref = new \ReflectionMethod($trait, 'shouldRedirectToInstaller'); + $method_ref->setAccessible(TRUE); + + // Mock the database connection info. + $db = $this->getMockForAbstractClass('Drupal\Core\Database\Database'); + $property_ref = new \ReflectionProperty($db, 'databaseInfo'); + $property_ref->setAccessible(true); + $property_ref->setValue($db, ['default' => $connection_info]); + + if ($connection) { + // Mock the database connection. + $connection = $this->getMockBuilder('Drupal\Core\Database\Connection') + ->disableOriginalConstructor() + ->setMethods(array('schema')) + ->getMockForAbstractClass(); + + if ($connection_info) { + // Mock the database schema class. + $schema = $this->getMockBuilder('Drupal\Core\Database\Schema') + ->disableOriginalConstructor() + ->setMethods(array('tableExists')) + ->getMockForAbstractClass(); + + $schema->expects($this->any()) + ->method('tableExists') + ->with('sessions') + ->willReturn($session_table_exists); + + $connection->expects($this->any()) + ->method('schema') + ->willReturn($schema); + } + } + else { + // Set the database connection if there is none. + $connection = NULL; + } + + // Call shouldRedirectToInstaller. + $this->assertSame($expected, $method_ref->invoke($trait, $e, $connection)); + } + } + +}