Skip to content
Snippets Groups Projects
Unverified Commit 63dc97c7 authored by colemanw's avatar colemanw Committed by GitHub
Browse files

Merge pull request #25516 from eileenmcnaughton/expires

#4117 Add is_current to UserJob, Search
parents f9322f5c 861ba037
Branches
Tags
No related merge requests found
......@@ -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)";
}
}
......@@ -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));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment