From b4628ea6514112a1204debddc2a4e1d5d4604f81 Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 6 Oct 2019 20:13:54 +0200 Subject: [PATCH] Heed is_active value if set to zero Note this is done BEFORE doing checks in case it is for performance reasons --- CRM/Utils/Check/Component.php | 57 +++++++++++++++++++++++- tests/phpunit/api/v3/SystemCheckTest.php | 26 ++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/CRM/Utils/Check/Component.php b/CRM/Utils/Check/Component.php index f07f2923fc..0c07a93953 100644 --- a/CRM/Utils/Check/Component.php +++ b/CRM/Utils/Check/Component.php @@ -25,6 +25,7 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\StatusPreference; /** * @@ -33,6 +34,36 @@ */ 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). diff --git a/tests/phpunit/api/v3/SystemCheckTest.php b/tests/phpunit/api/v3/SystemCheckTest.php index f2dfc32a81..0204d4d735 100644 --- a/tests/phpunit/api/v3/SystemCheckTest.php +++ b/tests/phpunit/api/v3/SystemCheckTest.php @@ -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; } -- 2.25.1