Skip to content

Commit

Permalink
Add system check to warn if CiviReport disabled & logging enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jan 4, 2024
1 parent 8f0a829 commit e368d4e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
32 changes: 30 additions & 2 deletions CRM/Utils/Check/Component/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\Extension;
use Psr\Log\LogLevel;

/**
*
* @package CRM
Expand Down Expand Up @@ -200,7 +203,7 @@ public function checkDomainNameEmail($force = FALSE) {
return $messages;
}

list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE);
[$domainEmailName, $domainEmailAddress] = CRM_Core_BAO_Domain::getNameAndEmail(TRUE);
$domain = CRM_Core_BAO_Domain::getDomain();
$domainName = $domain->name;
$fixEmailUrl = CRM_Utils_System::url("civicrm/admin/options/from_email_address", "&reset=1");
Expand Down Expand Up @@ -766,7 +769,7 @@ public function checkComponents(): array {
$messages = [];

$setting = Civi::settings()->get('enable_components');
$exts = \Civi\Api4\Extension::get(FALSE)
$exts = Extension::get(FALSE)
->addWhere('key', 'LIKE', 'civi_%')
->addWhere('status', '=', 'installed')
->execute()
Expand Down Expand Up @@ -859,6 +862,31 @@ public function checkExtensionUpgrades() {
return [];
}

/**
* Checks if logging is enabled but Civi-report is not.
*
* @return CRM_Utils_Check_Message[]
* @throws \CRM_Core_Exception
*/
public function checkLoggingHasCiviReport(): array {
if (Civi::settings()->get('logging')) {
$isEnabledCiviReport = (bool) Extension::get(FALSE)
->addWhere('key', '=', 'civi_report')
->addWhere('status', '=', 'installed')
->execute()->countFetched();
return $isEnabledCiviReport ? [] : [
new CRM_Utils_Check_Message(
__FUNCTION__,
ts('You have enabled detailed logging but to display this in the change log tab CiviReport must be enabled'),
ts('CiviReport required to display detailed logging.'),
LogLevel::WARNING,
'fa-plug'
),
];
}
return [];
}

/**
* Checks if CiviCRM database version is up-to-date
* @return CRM_Utils_Check_Message[]
Expand Down
46 changes: 40 additions & 6 deletions tests/phpunit/CRM/Utils/Check/Component/EnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,57 @@
*/
class CRM_Utils_Check_Component_EnvTest extends CiviUnitTestCase {

public function setUp(): void {
parent::setUp();
$this->useTransaction();
public function tearDown(): void {
Civi::settings()->set('logging', FALSE);
parent::tearDown();
}

/**
* File check test should fail if reached maximum timeout.
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function testResourceUrlCheck(): void {
$check = new \CRM_Utils_Check_Component_Env();
$check = new CRM_Utils_Check_Component_Env();
$failRequest = $check->fileExists('https://civicrm.org', 0.001);
$successRequest = $check->fileExists('https://civicrm.org', 0);

$this->assertEquals(FALSE, $failRequest, 'Request should fail for minimum timeout.');
$this->assertEquals(TRUE, $successRequest, 'Request should not fail for infinite timeout.');
$this->assertFalse($failRequest, 'Request should fail for minimum timeout.');
$this->assertTrue($successRequest, 'Request should not fail for infinite timeout.');
}

/**
* Test the check that warns users if they have enabled logging but not Civi-report.
*
* The check should not be triggered if logging is not enabled or it
* is enabled and civi-report is enabled.
*
* @return void
*/
public function testLoggingWithReport(): void {
$this->callAPISuccess('Extension', 'disable', ['key' => 'civi_report']);
$this->assertFalse($this->checkChecks('checkLoggingHasCiviReport'));

Civi::settings()->set('logging', 1);
$check = $this->checkChecks('checkLoggingHasCiviReport');
$this->assertEquals('CiviReport required to display detailed logging.', $check['title']);

$this->callAPISuccess('Extension', 'install', ['key' => 'civi_report']);
$this->assertFalse($this->checkChecks('checkLoggingHasCiviReport'));
}

/**
* Check the checks are checking for the checky thing.
*
* @return bool|array
*/
public function checkChecks($checkName) {
$checks = $this->callAPISuccess('System', 'check');
foreach ($checks['values'] as $check) {
if ($check['name'] === $checkName) {
return $check;
}
}
return FALSE;
}

}

0 comments on commit e368d4e

Please sign in to comment.