Heed is_active value if set to zero
authoreileen <emcnaughton@wikimedia.org>
Sun, 6 Oct 2019 18:13:54 +0000 (20:13 +0200)
committereileen <emcnaughton@wikimedia.org>
Mon, 7 Oct 2019 08:41:32 +0000 (10:41 +0200)
Note this is done BEFORE doing checks in case it is for performance reasons

CRM/Utils/Check/Component.php
tests/phpunit/api/v3/SystemCheckTest.php

index f07f2923fc9a6f05a60fa806913e520beb8edaf1..0c07a9395334cf6462860964931825574655d7c7 100644 (file)
@@ -25,6 +25,7 @@
  +--------------------------------------------------------------------+
  */
 
+use Civi\Api4\StatusPreference;
 
 /**
  *
  */
 abstract class CRM_Utils_Check_Component {
 
+  /**
+   * @var array
+   */
+  public $checksConfig = [];
+
+  /**
+   * Get the configured status checks.
+   *
+   * @return array
+   *
+   * @throws \API_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
+   */
+  public function getChecksConfig() {
+    if (empty($this->checksConfig)) {
+      $this->checksConfig = Civi::cache('checks')->get('checksConfig', []);
+      if (empty($this->checksConfig)) {
+        $this->checksConfig = StatusPreference::get()->setCheckPermissions(FALSE)->execute()->indexBy('name');
+      }
+    }
+    return $this->checksConfig;
+  }
+
+  /**
+   * @param array $checksConfig
+   */
+  public function setChecksConfig(array $checksConfig) {
+    $this->checksConfig = $checksConfig;
+  }
+
   /**
    * Should these checks be run?
    *
@@ -47,17 +78,40 @@ abstract class CRM_Utils_Check_Component {
    *
    * @return array
    *   [CRM_Utils_Check_Message]
+   *
+   * @throws \API_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
    */
   public function checkAll() {
     $messages = [];
     foreach (get_class_methods($this) as $method) {
-      if ($method !== 'checkAll' && strpos($method, 'check') === 0) {
+      // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
+      if ($method !== 'checkAll' && strpos($method, 'check') === 0 && !$this->isDisabled($method)) {
         $messages = array_merge($messages, $this->$method());
       }
     }
     return $messages;
   }
 
+  /**
+   * Is the specified check disabled.
+   *
+   * @param string $method
+   *
+   * @return bool
+   *
+   * @throws \API_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
+   */
+  public function isDisabled($method) {
+
+    $checks = $this->getChecksConfig();
+    if (!empty($checks[$method])) {
+      return (bool) empty($checks[$method]['is_active']);
+    }
+    return FALSE;
+  }
+
   /**
    * Check if file exists on given URL.
    *
@@ -65,6 +119,7 @@ abstract class CRM_Utils_Check_Component {
    * @param float|bool $timeoutOverride
    *
    * @return bool
+   * @throws \GuzzleHttp\Exception\GuzzleException
    */
   public function fileExists($url, $timeoutOverride = FALSE) {
     // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
index f2dfc32a81509cd58b75254c341908577d4697e1..0204d4d73583d93c3a92d29ff6ae3c618748a05c 100644 (file)
@@ -87,10 +87,34 @@ class api_v3_SystemCheckTest extends CiviUnitTestCase {
     $this->assertEquals($testedCheck['is_visible'], '0', 'in line ' . __LINE__);
   }
 
+  /**
+   * Disabled items should never show up.
+   *
+   * @param int $version
+   *
+   * @dataProvider versionThreeAndFour
+   */
+  public function testIsInactive($version) {
+    $this->_apiversion = $version;
+    $this->callAPISuccess('StatusPreference', 'create', [
+      'name' => 'checkDefaultMailbox',
+      'is_active' => 0,
+    ]);
+    $result = $this->callAPISuccess('System', 'check', [])['values'];
+    foreach ($result as $check) {
+      if ($check['name'] === 'checkDefaultMailbox') {
+        $this->fail('Check should have been skipped');
+      }
+    }
+  }
+
   /**
    * Items hushed through tomorrow shouldn't show up.
+   *
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \Exception
    */
   public function testSystemCheckHushFuture($version) {
     $this->_apiversion = $version;
@@ -103,7 +127,7 @@ class api_v3_SystemCheckTest extends CiviUnitTestCase {
     $statusPreference = $this->callAPISuccess('StatusPreference', 'create', $this->_params);
     $result = $this->callAPISuccess('System', 'check', []);
     foreach ($result['values'] as $check) {
-      if ($check['name'] == 'checkDefaultMailbox') {
+      if ($check['name'] === 'checkDefaultMailbox') {
         $testedCheck = $check;
         break;
       }