+--------------------------------------------------------------------+
*/
+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?
*
*
* @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.
*
* @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).
$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;
$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;
}