From 861ba037e587fa75fd4963577de06d2d1a6d7331 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 7 Feb 2023 13:08:32 +1300 Subject: [PATCH] dev/core#4117 Add is_current to UserJob, Search --- .../Provider/IsCurrentFieldSpecProvider.php | 36 ++++++++-- .../api/v4/Action/CurrentFilterTest.php | 65 +++++++++++++++---- 2 files changed, 84 insertions(+), 17 deletions(-) diff --git a/Civi/Api4/Service/Spec/Provider/IsCurrentFieldSpecProvider.php b/Civi/Api4/Service/Spec/Provider/IsCurrentFieldSpecProvider.php index c031c6c2f9..2d3dbb08a3 100644 --- a/Civi/Api4/Service/Spec/Provider/IsCurrentFieldSpecProvider.php +++ b/Civi/Api4/Service/Spec/Provider/IsCurrentFieldSpecProvider.php @@ -31,7 +31,7 @@ class IsCurrentFieldSpecProvider extends \Civi\Core\Service\AutoService implemen /** * @param \Civi\Api4\Service\Spec\RequestSpec $spec */ - public function modifySpec(RequestSpec $spec) { + public function modifySpec(RequestSpec $spec): void { $field = new FieldSpec('is_current', $spec->getEntity(), 'Boolean'); $field->setLabel(ts('Is Current')) ->setTitle(ts('Current')) @@ -39,27 +39,42 @@ class IsCurrentFieldSpecProvider extends \Civi\Core\Service\AutoService implemen ->setColumnName('is_current') ->setDescription(ts('Is active with a non-past end-date')) ->setType('Extra') - ->setSqlRenderer([__CLASS__, 'renderIsCurrentSql']); + ->setSqlRenderer([__CLASS__, $this->getRenderer($field->getEntity())]); $spec->addFieldSpec($field); } + /** + * Get the function to render the sql. + * + * @param string $entity + * + * @return string + */ + private function getRenderer(string $entity): string { + if (in_array($entity, ['UserJob', 'Search'])) { + return 'renderNonExpiredSql'; + } + return 'renderIsCurrentSql'; + } + /** * @param string $entity * @param string $action * * @return bool */ - public function applies($entity, $action) { + public function applies($entity, $action): bool { if ($action !== 'get') { return FALSE; } // 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'], TRUE); + return in_array($entity, ['Relationship', 'RelationshipCache', 'Event', 'Campaign', 'Search', 'UserJob'], TRUE); } /** * @param array $field + * * return string */ public static function renderIsCurrentSql(array $field): string { @@ -71,4 +86,17 @@ class IsCurrentFieldSpecProvider extends \Civi\Core\Service\AutoService implemen return "IF($isActive = 1 AND ($startDate <= '$todayStart' OR $startDate IS NULL) AND ($endDate >= '$todayEnd' OR $endDate IS NULL), '1', '0')"; } + /** + * Render the sql clause to filter on expires date. + * + * @param array $field + * + * return string + */ + public static function renderNonExpiredSql(array $field): string { + $endDate = substr_replace($field['sql_name'], 'expires_date', -11, -1); + $todayEnd = date('Ymd'); + return "IF($endDate >= '$todayEnd' OR $endDate IS NULL, 1, 0)"; + } + } diff --git a/tests/phpunit/api/v4/Action/CurrentFilterTest.php b/tests/phpunit/api/v4/Action/CurrentFilterTest.php index a2126fd80f..d0d1fddd78 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\UserJob; use Civi\Test\TransactionalInterface; /** @@ -29,37 +30,42 @@ use Civi\Test\TransactionalInterface; */ class CurrentFilterTest extends Api4TestBase implements TransactionalInterface { - public function testCurrentRelationship() { - $cid1 = Contact::create()->addValue('first_name', 'Bob1')->execute()->first()['id']; - $cid2 = Contact::create()->addValue('first_name', 'Bob2')->execute()->first()['id']; + /** + * Test relationship is_current checks start, end, active. + * + * @throws \CRM_Core_Exception + */ + public function testCurrentRelationship(): void { + $contactID1 = Contact::create()->addValue('first_name', 'Bob1')->execute()->first()['id']; + $contactID2 = Contact::create()->addValue('first_name', 'Bob2')->execute()->first()['id']; $current = Relationship::create()->setValues([ 'relationship_type_id' => 1, - 'contact_id_a' => $cid1, - 'contact_id_b' => $cid2, + 'contact_id_a' => $contactID1, + 'contact_id_b' => $contactID2, 'end_date' => 'now + 1 week', ])->execute()->first(); $indefinite = Relationship::create()->setValues([ 'relationship_type_id' => 2, - 'contact_id_a' => $cid1, - 'contact_id_b' => $cid2, + 'contact_id_a' => $contactID1, + 'contact_id_b' => $contactID2, ])->execute()->first(); $expiring = Relationship::create()->setValues([ 'relationship_type_id' => 3, - 'contact_id_a' => $cid1, - 'contact_id_b' => $cid2, + 'contact_id_a' => $contactID1, + 'contact_id_b' => $contactID2, 'end_date' => 'now', ])->execute()->first(); $past = Relationship::create()->setValues([ 'relationship_type_id' => 3, - 'contact_id_a' => $cid1, - 'contact_id_b' => $cid2, + 'contact_id_a' => $contactID1, + 'contact_id_b' => $contactID2, 'end_date' => 'now - 1 week', ])->execute()->first(); $inactive = Relationship::create()->setValues([ 'relationship_type_id' => 4, - 'contact_id_a' => $cid1, - 'contact_id_b' => $cid2, + 'contact_id_a' => $contactID1, + 'contact_id_b' => $contactID2, 'is_active' => 0, ])->execute()->first(); @@ -92,4 +98,37 @@ class CurrentFilterTest extends Api4TestBase implements TransactionalInterface { $this->assertArrayNotHasKey('is_current', $starGet); } + /** + * Test UserJob checks expires. + * + * @throws \CRM_Core_Exception + */ + public function testCurrentUserJob(): void { + $current = UserJob::create()->setValues([ + 'expires_date' => '+ 1 week', + 'status_id:name' => 'draft', + 'job_type:name' => 'contact_import', + ])->execute()->first(); + $indefinite = UserJob::create()->setValues([ + 'status_id:name' => 'draft', + 'job_type:name' => 'contact_import', + ])->execute()->first(); + $expired = UserJob::create()->setValues([ + 'expires_date' => '-1 week', + 'status_id:name' => 'draft', + 'job_type:name' => 'contact_import', + ])->execute()->first(); + + $getCurrent = (array) UserJob::get()->addWhere('is_current', '=', TRUE)->execute()->indexBy('id'); + $notCurrent = (array) UserJob::get()->addWhere('is_current', '=', FALSE)->execute()->indexBy('id'); + $getAll = (array) UserJob::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