From f1f29b265795b8046bf0b231c5a69bdafb6735ee Mon Sep 17 00:00:00 2001
From: lucashedding <lucashedding@1463982.no-reply.drupal.org>
Date: Mon, 3 Jun 2019 15:42:24 -0500
Subject: [PATCH] Issue #3053712 by heddn, eiriksm, catch, mbaynton: Provide
 context of how checker service is run

---
 automatic_updates.services.yml                |  6 +++
 src/ReadinessChecker/PhpSapi.php              | 43 +++++++++++++++++++
 .../Kernel/ReadinessChecker/PhpSapiTest.php   | 35 +++++++++++++++
 3 files changed, 84 insertions(+)
 create mode 100644 src/ReadinessChecker/PhpSapi.php
 create mode 100644 tests/src/Kernel/ReadinessChecker/PhpSapiTest.php

diff --git a/automatic_updates.services.yml b/automatic_updates.services.yml
index d0168241b4..56fd9b4068 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 0000000000..fc1bce3517
--- /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 0000000000..3eb397519e
--- /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]);
+  }
+
+}
-- 
GitLab