From 806d38b214784824d5ee6b07c21e19b7ebb20a4b Mon Sep 17 00:00:00 2001 From: colemanw Date: Thu, 14 Sep 2023 21:10:44 -0400 Subject: [PATCH] CiviCase - Prevent recursion when reconciling managed activity/relationship types & fix test The createManagedRelationshipTypes and createManagedActivityTypes functions always use 'civicrm' as the module name, so this reconciliation can target just that module to prevent recursion. The ManagedCaseTypeTest was failing due to caching/timing issues. Try this... --- CRM/Case/BAO/CaseType.php | 6 +++-- CRM/Core/ManagedEntities.php | 9 ++++---- .../tests/mixin/ManagedCaseTypeTest.php | 22 +++++++++++++------ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/CRM/Case/BAO/CaseType.php b/CRM/Case/BAO/CaseType.php index 8021fa1ea9..63d3dba55a 100644 --- a/CRM/Case/BAO/CaseType.php +++ b/CRM/Case/BAO/CaseType.php @@ -57,10 +57,12 @@ class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType implements \Civi\Core\ $caseTypeName = (isset($params['name'])) ? $params['name'] : CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $params['id'], 'name', 'id', TRUE); - // function to format definition column + // Format definition column if (isset($params['definition']) && is_array($params['definition'])) { $params['definition'] = self::convertDefinitionToXML($caseTypeName, $params['definition']); - CRM_Core_ManagedEntities::scheduleReconciliation(); + // Ensure entities declared in the definition get created. + // @see CRM_Case_ManagedEntities + CRM_Core_ManagedEntities::scheduleReconciliation(['civicrm']); } $caseTypeDAO->copyValues($params); diff --git a/CRM/Core/ManagedEntities.php b/CRM/Core/ManagedEntities.php index 0c16cb2644..c6bc824419 100644 --- a/CRM/Core/ManagedEntities.php +++ b/CRM/Core/ManagedEntities.php @@ -43,14 +43,15 @@ class CRM_Core_ManagedEntities { /** * Perform an asynchronous reconciliation when the transaction ends. + * @param array|null $modules */ - public static function scheduleReconciliation() { + public static function scheduleReconciliation(array $modules = NULL) { CRM_Core_Transaction::addCallback( CRM_Core_Transaction::PHASE_POST_COMMIT, - function () { - CRM_Core_ManagedEntities::singleton(TRUE)->reconcile(); + function ($modules) { + CRM_Core_ManagedEntities::singleton(TRUE)->reconcile($modules); }, - [], + [$modules], 'ManagedEntities::reconcile' ); } diff --git a/mixin/mgd-php@1/example/tests/mixin/ManagedCaseTypeTest.php b/mixin/mgd-php@1/example/tests/mixin/ManagedCaseTypeTest.php index 9137c5a272..097ce106b5 100644 --- a/mixin/mgd-php@1/example/tests/mixin/ManagedCaseTypeTest.php +++ b/mixin/mgd-php@1/example/tests/mixin/ManagedCaseTypeTest.php @@ -24,13 +24,15 @@ class ManagedCaseTypeTest extends \PHPUnit\Framework\Assert { $this->assertEquals(TRUE, $items[0]['is_active']); $this->assertEquals(1, count($items)); - // FIXME: The example is partially disabled - because the full example causes a crash. - // Hence, these assertions don't yet pass. - // $actTypes = $cv->api4('OptionValue', 'get', [ - // 'where' => [['option_group_id:name', '=', 'activity_type'], ['name', '=', 'Nibble']], - // ]); - // $this->assertEquals('Nibble', $actTypes[0]['name'], 'ActivityType "Nibble" should be auto enabled. It\'s missing.'); - // $this->assertEquals(TRUE, $actTypes[0]['is_active'], 'ActivityType "Nibble" should be auto enabled. It\'s inactive.'); + // This is normally handled by `CRM_Case_BAO_CaseType` calling `CRM_Core_ManagedEntities::scheduleReconciliation` + // But due to timing issues with the E2E tests the scheduled reconciliation hasn't happened yet. + \Civi\Api4\Managed::reconcile(FALSE)->addModule('civicrm')->execute(); + + $actTypes = $cv->api4('OptionValue', 'get', [ + 'where' => [['option_group_id:name', '=', 'activity_type'], ['name', '=', 'Nibble']], + ]); + $this->assertEquals('Nibble', $actTypes[0]['name'], 'ActivityType "Nibble" should be auto enabled. It\'s missing.'); + $this->assertEquals(TRUE, $actTypes[0]['is_active'], 'ActivityType "Nibble" should be auto enabled. It\'s inactive.'); } public function testDisabled($cv): void { @@ -46,6 +48,12 @@ class ManagedCaseTypeTest extends \PHPUnit\Framework\Assert { $items = $cv->api4('CaseType', 'get', ['where' => [['name', '=', 'BunnyDance']]]); $this->assertEquals(0, count($items)); + // This is normally handled by `CRM_Case_BAO_CaseType` calling `CRM_Core_ManagedEntities::scheduleReconciliation` + // But static caching seems to interfere. + \Civi\Api4\Managed::reconcile(FALSE)->execute(); + \CRM_Core_PseudoConstant::flush(); + \Civi\Api4\Managed::reconcile(FALSE)->addModule('civicrm')->execute(); + $actTypes = $cv->api4('OptionValue', 'get', [ 'where' => [['option_group_id:name', '=', 'activity_type'], ['name', '=', 'Nibble']], ]); -- 2.25.1