From 4256828110fed2e858f75ba3c7571c45320a1f16 Mon Sep 17 00:00:00 2001 From: Dominic Tubach Date: Wed, 12 Feb 2025 16:15:34 +0100 Subject: [PATCH] Load funding case from database for update event Previously the funding case was fetched from cache so previous and current funding case in `FundingCasePreUpdateEvent` and `FundingCaseUpdatedEvent` where the same object. --- Civi/Funding/FundingCase/FundingCaseManager.php | 13 ++++++++++++- .../Funding/FundingCase/FundingCaseManagerTest.php | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Civi/Funding/FundingCase/FundingCaseManager.php b/Civi/Funding/FundingCase/FundingCaseManager.php index de14195cf..46b2b1387 100644 --- a/Civi/Funding/FundingCase/FundingCaseManager.php +++ b/Civi/Funding/FundingCase/FundingCaseManager.php @@ -229,8 +229,11 @@ public function getOrCreate(array $allowedExistingStatusList, int $contactId, ar return $fundingCase ?? $this->create($contactId, $values); } + /** + * @throws \CRM_Core_Exception + */ public function update(FundingCaseEntity $fundingCase): void { - $previousFundingCase = $this->get($fundingCase->getId()); + $previousFundingCase = $this->getWithoutCache($fundingCase->getId()); Assert::notNull($previousFundingCase, 'Funding case could not be loaded'); if ($fundingCase->getModificationDate() == $previousFundingCase->getModificationDate()) { $fundingCase->setModificationDate(new \DateTime(date('Y-m-d H:i:s'))); @@ -315,4 +318,12 @@ private function getFundingCaseFromApiResultOrNull(Result $result): ?FundingCase return $fundingCases[0] ?? NULL; } + /** + * @throws \CRM_Core_Exception + */ + private function getWithoutCache(int $id): ?FundingCaseEntity { + // @phpstan-ignore argument.type + return FundingCaseEntity::fromArrayOrNull($this->api4->getEntity(FundingCase::getEntityName(), $id)); + } + } diff --git a/tests/phpunit/Civi/Funding/FundingCase/FundingCaseManagerTest.php b/tests/phpunit/Civi/Funding/FundingCase/FundingCaseManagerTest.php index b9c62c9c9..211b3a203 100644 --- a/tests/phpunit/Civi/Funding/FundingCase/FundingCaseManagerTest.php +++ b/tests/phpunit/Civi/Funding/FundingCase/FundingCaseManagerTest.php @@ -409,7 +409,8 @@ public function testUpdate(): void { \CRM_Core_Session::singleton()->set('userID', $contact['id']); FundingCaseContactRelationFixture::addContact($contact['id'], $fundingCase->getId(), ['test_permission']); - $updatedFundingCase = FundingCaseEntity::fromArray($fundingCase->toArray()); + $updatedFundingCase = $this->fundingCaseManager->get($fundingCase->getId()); + static::assertNotNull($updatedFundingCase); $updatedFundingCase->setStatus('updated'); // Reload to have permission fields required for the event object comparison.