diff --git a/automatic_updates.services.yml b/automatic_updates.services.yml index d0168241b455acd54a764d3ac948f428d69430ae..56fd9b4068e09eb9db1a0733ef4694a4ef03d87e 100644 --- a/automatic_updates.services.yml +++ b/automatic_updates.services.yml @@ -91,3 +91,9 @@ services: - '@extension.list.theme' tags: - { name: readiness_checker, category: warning} + automatic_updates.php_sapi: + class: Drupal\automatic_updates\ReadinessChecker\PhpSapi + arguments: + - '@state' + tags: + - { name: readiness_checker, category: warning} diff --git a/src/ReadinessChecker/PhpSapi.php b/src/ReadinessChecker/PhpSapi.php new file mode 100644 index 0000000000000000000000000000000000000000..fc1bce35178fa9bb078cf4e03c53b38281b65d3b --- /dev/null +++ b/src/ReadinessChecker/PhpSapi.php @@ -0,0 +1,43 @@ +<?php + +namespace Drupal\automatic_updates\ReadinessChecker; + +use Drupal\Core\State\StateInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; + +/** + * Warn if PHP SAPI changes between checker executions. + */ +class PhpSapi implements ReadinessCheckerInterface { + use StringTranslationTrait; + + /** + * The state service. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + /** + * PhpSapi constructor. + * + * @param \Drupal\Core\State\StateInterface $state + * The state service. + */ + public function __construct(StateInterface $state) { + $this->state = $state; + } + + /** + * {@inheritdoc} + */ + public function run() { + $messages = []; + $php_sapi = $this->state->get('automatic_updates.php_sapi', PHP_SAPI); + if ($php_sapi !== PHP_SAPI) { + $messages[] = $this->t('PHP changed from running as "@previous" to "@current". This can lead to inconsistent and misleading results.', ['@previous' => $php_sapi, '@current' => PHP_SAPI]); + } + return $messages; + } + +} diff --git a/tests/src/Kernel/ReadinessChecker/PhpSapiTest.php b/tests/src/Kernel/ReadinessChecker/PhpSapiTest.php new file mode 100644 index 0000000000000000000000000000000000000000..3eb397519ef2b34c6b64906692cded03ada75232 --- /dev/null +++ b/tests/src/Kernel/ReadinessChecker/PhpSapiTest.php @@ -0,0 +1,35 @@ +<?php + +namespace Drupal\Tests\automatic_updates\Kernel\ReadinessChecker; + +use Drupal\KernelTests\KernelTestBase; + +/** + * Tests what happens when PHP SAPI changes from one value to another. + * + * @group automatic_updates + */ +class PhpSapiTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = [ + 'automatic_updates', + ]; + + /** + * Tests the functionality of supported PHP version readiness checks. + */ + public function testSupportedPhpVersion() { + $messages = $this->container->get('automatic_updates.php_sapi')->run(); + $this->assertEmpty($messages); + $messages = $this->container->get('automatic_updates.php_sapi')->run(); + $this->assertEmpty($messages); + + $this->container->get('state')->set('automatic_updates.php_sapi', 'foo'); + $messages = $this->container->get('automatic_updates.php_sapi')->run(); + $this->assertEquals('PHP changed from running as "foo" to "cli". This can lead to inconsistent and misleading results.', $messages[0]); + } + +}