From e398a65108660716e3dcdd3beb94d8daa31e2ab8 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 14 Feb 2023 16:28:11 +1300 Subject: [PATCH] Add test to is_current on SavedSearch, fix --- .../Provider/IsCurrentFieldSpecProvider.php | 4 +-- .../api/v4/Action/CurrentFilterTest.php | 33 ++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Civi/Api4/Service/Spec/Provider/IsCurrentFieldSpecProvider.php b/Civi/Api4/Service/Spec/Provider/IsCurrentFieldSpecProvider.php index 2d3dbb08a3..29ce637ad8 100644 --- a/Civi/Api4/Service/Spec/Provider/IsCurrentFieldSpecProvider.php +++ b/Civi/Api4/Service/Spec/Provider/IsCurrentFieldSpecProvider.php @@ -51,7 +51,7 @@ class IsCurrentFieldSpecProvider extends \Civi\Core\Service\AutoService implemen * @return string */ private function getRenderer(string $entity): string { - if (in_array($entity, ['UserJob', 'Search'])) { + if (in_array($entity, ['UserJob', 'SavedSearch'])) { return 'renderNonExpiredSql'; } return 'renderIsCurrentSql'; @@ -69,7 +69,7 @@ class IsCurrentFieldSpecProvider extends \Civi\Core\Service\AutoService implemen } // TODO: If we wanted this to not be a hard-coded list, we could always return TRUE here // and then in the `modifySpec` function check for the 3 fields `is_active`, `start_date`, and `end_date` - return in_array($entity, ['Relationship', 'RelationshipCache', 'Event', 'Campaign', 'Search', 'UserJob'], TRUE); + return in_array($entity, ['Relationship', 'RelationshipCache', 'Event', 'Campaign', 'SavedSearch', 'UserJob'], TRUE); } /** diff --git a/tests/phpunit/api/v4/Action/CurrentFilterTest.php b/tests/phpunit/api/v4/Action/CurrentFilterTest.php index d0d1fddd78..43093ef3e4 100644 --- a/tests/phpunit/api/v4/Action/CurrentFilterTest.php +++ b/tests/phpunit/api/v4/Action/CurrentFilterTest.php @@ -22,6 +22,7 @@ namespace api\v4\Action; use Civi\Api4\Relationship; use api\v4\Api4TestBase; use Civi\Api4\Contact; +use Civi\Api4\SavedSearch; use Civi\Api4\UserJob; use Civi\Test\TransactionalInterface; @@ -125,7 +126,37 @@ class CurrentFilterTest extends Api4TestBase implements TransactionalInterface { $this->assertTrue($getAll[$current['id']]['is_current']); $this->assertTrue($getAll[$indefinite['id']]['is_current']); - $this->assertFALSE($getAll[$expired['id']]['is_current']); + $this->assertFalse($getAll[$expired['id']]['is_current']); + + $this->assertEquals([$current['id'], $indefinite['id']], array_keys($getCurrent)); + $this->assertEquals([$expired['id']], array_keys($notCurrent)); + } + + /** + * Test saved search api checks expires_date. + * + * @throws \CRM_Core_Exception + */ + public function testCurrentSavedSearch(): void { + $current = SavedSearch::create()->setValues([ + 'expires_date' => '+ 1 week', + 'name' => 'current', + ])->execute()->first(); + $indefinite = SavedSearch::create()->setValues([ + 'name' => 'never expires', + ])->execute()->first(); + $expired = SavedSearch::create()->setValues([ + 'expires_date' => '-1 week', + 'name' => 'expired', + ])->execute()->first(); + + $getCurrent = (array) SavedSearch::get()->addWhere('is_current', '=', TRUE)->execute()->indexBy('id'); + $notCurrent = (array) SavedSearch::get()->addWhere('is_current', '=', FALSE)->execute()->indexBy('id'); + $getAll = (array) SavedSearch::get()->addSelect('is_current')->execute()->indexBy('id'); + + $this->assertTrue($getAll[$current['id']]['is_current']); + $this->assertTrue($getAll[$indefinite['id']]['is_current']); + $this->assertFalse($getAll[$expired['id']]['is_current']); $this->assertEquals([$current['id'], $indefinite['id']], array_keys($getCurrent)); $this->assertEquals([$expired['id']], array_keys($notCurrent)); -- 2.25.1