Skip to content
Snippets Groups Projects
Commit 240e1997 authored by Ted Bowman's avatar Ted Bowman
Browse files

Issue #3400146: Require the OpenSSL extension

parent 1edb15aa
No related branches found
No related tags found
1 merge request!984Issue #3400146 updated to error
......@@ -6,6 +6,9 @@ namespace Drupal\package_manager\Validator;
use Drupal\Component\FileSystem\FileSystem as DrupalFilesystem;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\PreOperationStageEvent;
use Drupal\package_manager\Event\StatusCheckEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......@@ -59,17 +62,17 @@ class PhpExtensionsValidator implements EventSubscriberInterface {
}
/**
* Flags a warning if the OpenSSL extension is not installed.
* Flags an error if the OpenSSL extension is not installed.
*
* @param \Drupal\package_manager\Event\StatusCheckEvent $event
* @param \Drupal\package_manager\Event\PreOperationStageEvent $event
* The event object.
*/
public function validateOpenSsl(StatusCheckEvent $event): void {
public function validateOpenSsl(PreOperationStageEvent $event): void {
if (!$this->isExtensionLoaded('openssl')) {
$message = $this->t('The OpenSSL extension is not enabled, which is a security risk. See <a href=":url">the PHP documentation</a> for information on how to enable this extension.', [
':url' => 'https://www.php.net/manual/en/openssl.installation.php',
]);
$event->addWarning([$message]);
$event->addError([$message]);
}
}
......@@ -97,6 +100,8 @@ class PhpExtensionsValidator implements EventSubscriberInterface {
['validateXdebug'],
['validateOpenSsl'],
],
PreCreateEvent::class => ['validateOpenSsl'],
PreApplyEvent::class => ['validateOpenSsl'],
];
}
......
......@@ -4,6 +4,9 @@ declare(strict_types = 1);
namespace Drupal\Tests\package_manager\Kernel;
use Drupal\package_manager\Event\PostCreateEvent;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\ValidationResult;
/**
......@@ -20,37 +23,32 @@ class PhpExtensionsValidatorTest extends PackageManagerKernelTestBase {
* The test cases.
*/
public function providerPhpExtensionsValidation(): array {
$openssl_error = ValidationResult::createError([
t('The OpenSSL extension is not enabled, which is a security risk. See <a href="https://www.php.net/manual/en/openssl.installation.php">the PHP documentation</a> for information on how to enable this extension.'),
]);
$xdebug_warning = ValidationResult::createWarning([
t('Xdebug is enabled, which may have a negative performance impact on Package Manager and any modules that use it.'),
]);
return [
'xdebug enabled, openssl installed' => [
['xdebug', 'openssl'],
[
ValidationResult::createWarning([
t('Xdebug is enabled, which may have a negative performance impact on Package Manager and any modules that use it.'),
]),
],
[$xdebug_warning],
[],
],
'xdebug enabled, openssl not installed' => [
['xdebug'],
[
ValidationResult::createWarning([
t('Xdebug is enabled, which may have a negative performance impact on Package Manager and any modules that use it.'),
]),
ValidationResult::createWarning([
t('The OpenSSL extension is not enabled, which is a security risk. See <a href="https://www.php.net/manual/en/openssl.installation.php">the PHP documentation</a> for information on how to enable this extension.'),
]),
],
[$xdebug_warning, $openssl_error],
[$openssl_error],
],
'xdebug disabled, openssl installed' => [
['openssl'],
[],
[],
],
'xdebug disabled, openssl not installed' => [
[],
[
ValidationResult::createWarning([
t('The OpenSSL extension is not enabled, which is a security risk. See <a href="https://www.php.net/manual/en/openssl.installation.php">the PHP documentation</a> for information on how to enable this extension.'),
]),
],
[$openssl_error],
[$openssl_error],
],
];
}
......@@ -61,17 +59,29 @@ class PhpExtensionsValidatorTest extends PackageManagerKernelTestBase {
* @param string[] $loaded_extensions
* The names of the PHP extensions that the validator should think are
* loaded.
* @param \Drupal\package_manager\ValidationResult[] $expected_results
* The expected validation results.
* @param \Drupal\package_manager\ValidationResult[] $expected_status_check_results
* The expected validation results during the status check event.
* @param \Drupal\package_manager\ValidationResult[] $expected_life_cycle_results
* The expected validation results during pre-create and pre-apply event.
*
* @dataProvider providerPhpExtensionsValidation
*/
public function testPhpExtensionsValidation(array $loaded_extensions, array $expected_results): void {
public function testPhpExtensionsValidation(array $loaded_extensions, array $expected_status_check_results, array $expected_life_cycle_results): void {
$state = $this->container->get('state');
// @see \Drupal\package_manager\Validator\PhpExtensionsValidator::isExtensionLoaded()
$this->container->get('state')
->set('package_manager_loaded_php_extensions', $loaded_extensions);
$state->set('package_manager_loaded_php_extensions', $loaded_extensions);
$this->assertStatusCheckResults($expected_results);
$this->assertStatusCheckResults($expected_status_check_results);
$this->assertResults($expected_life_cycle_results, PreCreateEvent::class);
// To test pre-apply delete the loaded extensions in state which will allow
// the pre-create event to run without a validation error.
$state->delete('package_manager_loaded_php_extensions');
// On post-create set the loaded extensions in state so that the pre-apply
// event will have the expected validation error.
$this->addEventTestListener(function () use ($state, $loaded_extensions) {
$state->set('package_manager_loaded_php_extensions', $loaded_extensions);
}, PostCreateEvent::class);
$this->assertResults($expected_life_cycle_results, PreApplyEvent::class);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment