added extra conditions param to getCountOfCaseProvisions

This commit is contained in:
naomi 2022-06-05 14:54:27 +01:00
parent 737104d7bd
commit fb9737ca04
2 changed files with 35 additions and 1 deletions

View File

@ -211,6 +211,9 @@ class OCActor extends RevisionableContentEntityBase implements OCActorInterface
public function getCountOfCaseProvisions(array $conditionsToApplyToCaseProvisionQuery = []): int {
$query = \Drupal::entityQuery('oc_case_provision');
$query->condition('oc_provider', $this->id());
foreach($conditionsToApplyToCaseProvisionQuery as $condition) {
$query->condition($condition[0], $condition[1], $condition[2] ?? '=');
}
return count($query->execute());
}

View File

@ -15,7 +15,7 @@ class OCActorTest extends UnitTestCase{
]);
}
public function testGetCountOfCaseProvisions(): void{
public function testGetCountOfCaseProvisionsWithNoExtraConditions(): void{
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$actor = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\\OCActor')->disableOriginalConstructor()
@ -27,4 +27,35 @@ class OCActorTest extends UnitTestCase{
$count = $actor->getCountOfCaseProvisions();
$this->assertTrue($count == 4);
}
public function testGetCountOfCaseProvisionsWithExtraConditions(): void{
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$actor = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\\OCActor')->disableOriginalConstructor()
->onlyMethods(['id'])
->getMock();
$actor->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->exactly(2))->method('condition')->withConsecutive(
['oc_provider', 5],
['some_date_field', '2022-01-01', '<']);
$query->expects($this->once())->method('execute')->willReturn([1,2,3,4]);
$count = $actor->getCountOfCaseProvisions([['some_date_field', '2022-01-01', '<']]);
$this->assertTrue($count == 4);
}
public function testGetCountOfCaseProvisionsWithExtraConditionsWithAssumedEqualityOperator(): void{
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$actor = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\\OCActor')->disableOriginalConstructor()
->onlyMethods(['id'])
->getMock();
$actor->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->exactly(2))->method('condition')->withConsecutive(
['oc_provider', 5],
['some_date_field', '2022-01-01', '=']);
$query->expects($this->once())->method('execute')->willReturn([1,2,3,4]);
$count = $actor->getCountOfCaseProvisions([['some_date_field', '2022-01-01']]);
$this->assertTrue($count == 4);
}
}