diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCasesTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCasesTest.php index 0a2288db9..709c53ec5 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCasesTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCasesTest.php @@ -3,10 +3,12 @@ namespace Tests\unit\workflow\engine\src\ProcessMaker\BusinessModel\Cases; use G; +use Illuminate\Foundation\Testing\DatabaseTransactions; use ProcessMaker\BusinessModel\Cases\AbstractCases; use ProcessMaker\Model\Application; use ProcessMaker\Model\Process; use ProcessMaker\Model\ProcessCategory; +use ProcessMaker\Model\Task; use ProcessMaker\Model\User; use Tests\TestCase; @@ -15,6 +17,8 @@ use Tests\TestCase; */ class AbstractCasesTest extends TestCase { + use DatabaseTransactions; + /** * This check the getter and setter related to the category * @@ -52,6 +56,22 @@ class AbstractCasesTest extends TestCase $this->assertEquals($process->PRO_ID, $actual); } + /** + * This check the getter and setter related to the task + * + * @covers \ProcessMaker\BusinessModel\Cases\AbstractCases::setTaskId() + * @covers \ProcessMaker\BusinessModel\Cases\AbstractCases::getTaskId() + * @test + */ + public function it_return_set_get_task() + { + $task = factory(Task::class)->create(); + $absCases = new AbstractCases(); + $absCases->setTaskId($task->TAS_ID); + $actual = $absCases->getTaskId(); + $this->assertEquals($task->TAS_ID, $actual); + } + /** * This check the getter and setter related to the user * @@ -73,6 +93,61 @@ class AbstractCasesTest extends TestCase $this->assertEquals($users->USR_ID, $actual); } + /** + * This check the getter and setter related to the priority + * + * @covers \ProcessMaker\BusinessModel\Cases\AbstractCases::setPriority() + * @covers \ProcessMaker\BusinessModel\Cases\AbstractCases::getPriority() + * @test + */ + public function it_return_set_get_priority() + { + $absCases = new AbstractCases(); + $arguments = [1 => 'VL', 2 => 'L', 3 => 'N', 4 => 'H', 5 => 'VH']; + $index = array_rand($arguments); + $absCases->setPriority($index); + $actual = $absCases->getPriority(); + $this->assertEquals($index, $actual); + } + + /** + * This check the getter and setter related to the case number + * + * @covers \ProcessMaker\BusinessModel\Cases\AbstractCases::setCaseNumber() + * @covers \ProcessMaker\BusinessModel\Cases\AbstractCases::getCaseNumber() + * @test + */ + public function it_return_set_get_case_number() + { + $case = factory(Application::class)->create(); + $absCases = new AbstractCases(); + $absCases->setCaseNumber($case->APP_NUMBER); + $actual = $absCases->getCaseNumber(); + $this->assertEquals($case->APP_NUMBER, $actual); + } + + /** + * This check the getter and setter related to the range of case number + * + * @covers \ProcessMaker\BusinessModel\Cases\AbstractCases::setRangeCaseNumber() + * @covers \ProcessMaker\BusinessModel\Cases\AbstractCases::getFromCaseNumber() + * @covers \ProcessMaker\BusinessModel\Cases\AbstractCases::getToCaseNumber() + * @test + */ + public function it_return_set_get_range_case_number() + { + $case1 = factory(Application::class)->create(); + $case2 = factory(Application::class)->create([ + 'APP_NUMBER' => $case1->APP_NUMBER + 1 + ]); + $absCases = new AbstractCases(); + $absCases->setRangeCaseNumber($case1->APP_NUMBER, $case2->APP_NUMBER); + $from = $absCases->getFromCaseNumber(); + $to = $absCases->getToCaseNumber(); + $this->assertEquals($case1->APP_NUMBER, $from); + $this->assertEquals($case2->APP_NUMBER, $to); + } + /** * This check the getter and setter related to the search * @@ -306,7 +381,12 @@ class AbstractCasesTest extends TestCase $properties = [ 'category' => G::generateUniqueID(), 'process' => G::generateUniqueID(), + 'task' => rand(), 'user' => G::generateUniqueID(), + 'priority' => 1, + 'caseNumber' => rand(), + 'caseNumberFrom' => rand(), + 'caseNumberTo' => rand(), 'search' => G::generateUniqueID(), 'caseLink' => G::generateUniqueID(), 'appUidCheck' => [G::generateUniqueID()], @@ -323,8 +403,18 @@ class AbstractCasesTest extends TestCase $this->assertEquals($properties['category'], $actual); $actual = $absCases->getProcessUid(); $this->assertEquals($properties['process'], $actual); + $actual = $absCases->getTaskId(); + $this->assertEquals($properties['task'], $actual); $actual = $absCases->getUserUid(); $this->assertEquals($properties['user'], $actual); + $actual = $absCases->getPriority(); + $this->assertEquals($properties['priority'], $actual); + $actual = $absCases->getCaseNumber(); + $this->assertEquals($properties['caseNumber'], $actual); + $actual = $absCases->getFromCaseNumber(); + $this->assertEquals($properties['caseNumberFrom'], $actual); + $actual = $absCases->getToCaseNumber(); + $this->assertEquals($properties['caseNumberTo'], $actual); $actual = $absCases->getValueToSearch(); $this->assertEquals($properties['search'], $actual); $actual = $absCases->getCaseUid(); diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/SearchTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/SearchTest.php new file mode 100644 index 000000000..cdd3696df --- /dev/null +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/SearchTest.php @@ -0,0 +1,176 @@ +states('foreign_keys')->create(); + + return $delegation; + } + + /** + * It tests the getData method without filters + * + * @covers \ProcessMaker\BusinessModel\Cases\Search::getData() + * @test + */ + public function it_should_test_get_data_method_without_filters() + { + // Create factories related to the delegation cases + $cases = $this->createSearch(); + // Create new Search object + $search = new Search(); + $result = $search->getData(); + // This assert that the expected numbers of results are returned + $this->assertEquals(count($cases), count($result)); + } + + /** + * It tests the getData with case number + * + * @covers \ProcessMaker\BusinessModel\Cases\Search::getData() + * @test + */ + public function it_should_test_get_data_method_with_specific_case_number() + { + // Create factories related to the delegation cases + $cases = $this->createSearch(); + // Create new Search object + $search = new Search(); + $search->setCaseNumber($cases[0]->APP_NUMBER); + // Set order by column value + $search->setOrderByColumn('APP_DELEGATION.APP_NUMBER'); + $result = $search->getData(); + // This assert that the expected numbers of results are returned + $this->assertEquals($cases[0]->APP_NUMBER, $result[0]['APP_NUMBER']); + } + + /** + * It tests the getData with priority + * + * @covers \ProcessMaker\BusinessModel\Cases\Search::getData() + * @test + */ + public function it_should_test_get_data_method_with_specific_priority() + { + // Create factories related to the delegation cases + $cases = $this->createSearch(); + // Create new Search object + $search = new Search(); + $search->setPriority(3); + // Set order by column value + $search->setOrderByColumn('APP_DELEGATION.APP_NUMBER'); + $result = $search->getData(); + // This assert that the expected numbers of results are returned + $this->assertEquals(3, $result[0]['DEL_PRIORITY']); + } + + /** + * It tests the getData with process + * + * @covers \ProcessMaker\BusinessModel\Cases\Search::getData() + * @test + */ + public function it_should_test_get_data_method_with_specific_process() + { + // Create factories related to the delegation cases + $cases = $this->createSearch(); + // Create new Search object + $search = new Search(); + $search->setProcessId($cases[0]->PRO_ID); + // Set order by column value + $search->setOrderByColumn('APP_DELEGATION.APP_NUMBER'); + $result = $search->getData(); + // This assert that the expected numbers of results are returned + $this->assertEquals($cases[0]->PRO_ID, $result[0]['PRO_ID']); + } + + /** + * It tests the getData with task + * + * @covers \ProcessMaker\BusinessModel\Cases\Search::getData() + * @test + */ + public function it_should_test_get_data_method_with_specific_task() + { + // Create factories related to the delegation cases + $cases = $this->createSearch(); + // Create new Search object + $search = new Search(); + $search->setTaskId($cases[0]->TAS_ID); + // Set order by column value + $search->setOrderByColumn('APP_DELEGATION.APP_NUMBER'); + $result = $search->getData(); + // This assert that the expected numbers of results are returned + $this->assertEquals($cases[0]->TAS_ID, $result[0]['TAS_ID']); + } + + /** + * It tests the getData with user + * + * @covers \ProcessMaker\BusinessModel\Cases\Search::getData() + * @test + */ + public function it_should_test_get_data_method_with_specific_user() + { + // Create factories related to the delegation cases + $cases = $this->createSearch(); + // Create new Search object + $search = new Search(); + $search->setUserId($cases[0]->USR_ID); + // Set order by column value + $search->setOrderByColumn('APP_DELEGATION.APP_NUMBER'); + $result = $search->getData(); + // This assert that the expected numbers of results are returned + $this->assertEquals($cases[0]->USR_ID, $result[0]['USR_ID']); + } + + /** + * It tests the getCounter method + * + * @covers \ProcessMaker\BusinessModel\Cases\Search::getCounter() + * @test + */ + public function it_should_test_the_counter_for_search() + { + // Create factories related to the delegation cases + $cases = $this->createSearch(); + // Create new Search object + $search = new Search(); + // Set order by column value + $search->setOrderByColumn('APP_DELEGATION.APP_NUMBER'); + $total = $search->getCounter(); + $this->assertEquals(count($cases), $total);; + } +} \ No newline at end of file diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCases.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCases.php index 0fb8ab5ca..fbc7cc186 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCases.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCases.php @@ -16,6 +16,7 @@ class AbstractCases implements CasesInterface const ORDER_DIRECTIONS = ['DESC', 'ASC']; const CORRECT_CANCELED_STATUS = 'CANCELED'; const INCORRECT_CANCELED_STATUS = 'CANCELLED'; + const PRIORITIES = [1 => 'VL', 2 => 'L', 3 => 'N', 4 => 'H', 5 => 'VH']; // Filter by category from a process, know as "$category" in the old lists classes private $categoryUid = ''; @@ -26,6 +27,9 @@ class AbstractCases implements CasesInterface // Filter by process using the Id field private $processId = 0; + // Filter by task using the Id field + private $taskId = 0; + // Filter by user, know as "$user" in the old lists classes private $userUid = ''; @@ -44,6 +48,9 @@ class AbstractCases implements CasesInterface // Filter by risk status, know as "$filterStatus" in the old list "inbox" class private $riskStatus = ''; + // Filter by specific priority + private $priority = 0; + // Filter by case status, know as "$filterStatus" in the old "participated last" class private $caseStatus = ''; @@ -53,6 +60,10 @@ class AbstractCases implements CasesInterface // Filter by a specific case using case number private $caseNumber = 0; + // Filter by a specific range of case number + private $fromCaseNumber = 0; + private $toCaseNumber = 0; + // Filter by specific cases, know as "$appUidCheck" in the old lists classes private $casesUids = []; @@ -140,6 +151,26 @@ class AbstractCases implements CasesInterface return $this->processId; } + /** + * Set task Id value + * + * @param int $taskId + */ + public function setTaskId(int $taskId) + { + $this->taskId = $taskId; + } + + /** + * Get task Id value + * + * @return int + */ + public function getTaskId() + { + return $this->taskId; + } + /** * Set User Uid value * @@ -296,7 +327,7 @@ class AbstractCases implements CasesInterface } /** - * Get risk status + * Get risk value * * @return string */ @@ -305,6 +336,40 @@ class AbstractCases implements CasesInterface return $this->riskStatus; } + /** + * Set priority value + * + * @param int $priority + * + * @throws Exception + */ + public function setPriority(int $priority) + { + // Validate the priority value + if (!empty($priority)) { + if (!empty(self::PRIORITIES[$priority])) { + $priorityCode = $priority; + } else { + throw new Exception("Priority value {$priority} is not valid."); + } + } else { + // List all priorities + $priorityCode = 0; + } + + $this->priority = $priorityCode; + } + + /** + * Get priority status + * + * @return string + */ + public function getPriority() + { + return $this->priority; + } + /** * Set Case status * @@ -385,6 +450,38 @@ class AbstractCases implements CasesInterface return $this->caseNumber; } + /** + * Set range of Case Number + * + * @param int $from + * @param int $to + */ + public function setRangeCaseNumber(int $from, int $to) + { + $this->fromCaseNumber = $from; + $this->toCaseNumber = $to; + } + + /** + * Get from Case Number + * + * @return int + */ + public function getFromCaseNumber() + { + return $this->fromCaseNumber; + } + + /** + * Get to Case Number + * + * @return int + */ + public function getToCaseNumber() + { + return $this->toCaseNumber; + } + /** * Set Cases Uids * @@ -603,10 +700,26 @@ class AbstractCases implements CasesInterface $this->setProcessUid($properties['process']); } + if (!empty($properties['task'])) { + $this->setTaskId($properties['task']); + } + if (!empty($properties['user'])) { $this->setUserUid($properties['user']); } + if (!empty($properties['priority'])) { + $this->setPriority($properties['priority']); + } + + if (!empty($properties['caseNumber'])) { + $this->setCaseNumber($properties['caseNumber']); + } + + if (!empty($properties['caseNumberFrom']) && !empty($properties['caseNumberTo'])) { + $this->setRangeCaseNumber($properties['caseNumberFrom'], $properties['caseNumberTo']); + } + if (!empty($properties['search'])) { $this->setValueToSearch($properties['search']); } diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Search.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Search.php new file mode 100644 index 000000000..594701aad --- /dev/null +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Search.php @@ -0,0 +1,76 @@ +select(); + + // Filter by case number + $query->case($this->getCaseNumber()); + + // Filter by case number: from and to + if ($this->getFromCaseNumber() > 0 && $this->getToCaseNumber() > 0) { + $query->rangeOfCases($this->getFromCaseNumber(), $this->getToCaseNumber()); + } + + // @todo: Filter by case title, pending from other PRD + + // Filter by category + $query->categoryProcess($this->getCategoryUid()); + + // Filter by priority + if ($this->getPriority() > 0) { + $query->priority($this->getPriority()); + } + + // Filter by process + if (!empty($this->getProcessId())) { + $query->processId($this->getProcessId()); + } + + // Filter by user + if (!empty($this->getUserId())) { + $query->userId($this->getUserId()); + } + + // Filter by task + if (!empty($this->getTaskId())) { + $query->task($this->getTaskId()); + } + + // The order by clause + $query->orderBy($this->getOrderByColumn(), $this->getOrderDirection()); + + // The limit by clause + $query->offset($this->getOffset())->limit($this->getLimit()); + + //Execute the query + $results = $query->get(); + + //Return the values as an array format + return $results->values()->toArray(); + } + + /** + * Get the number of rows corresponding to the advanced search + * + * @return int + */ + public function getCounter() + { + $query = Delegation::query()->select(); + + // Return the number of rows + return $query->count(); + } +} \ No newline at end of file diff --git a/workflow/engine/src/ProcessMaker/Model/Delegation.php b/workflow/engine/src/ProcessMaker/Model/Delegation.php index f9b823e63..d7187b802 100644 --- a/workflow/engine/src/ProcessMaker/Model/Delegation.php +++ b/workflow/engine/src/ProcessMaker/Model/Delegation.php @@ -54,6 +54,18 @@ class Delegation extends Model return $this->belongsTo(Process::class, 'PRO_ID', 'PRO_ID'); } + /** + * Scope a query to only include specific priority + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param int $priority + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopePriority($query, int $priority) + { + return $query->where('DEL_PRIORITY', $priority); + } + /** * Scope a query to only include open threads * @@ -73,7 +85,7 @@ class Delegation extends Model * * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeIndex($query, $index) + public function scopeIndex($query, int $index) { return $query->where('DEL_INDEX', '=', $index); } @@ -177,6 +189,21 @@ class Delegation extends Model return $query->where('APP_DELEGATION.APP_NUMBER', '=', $appNumber); } + /** + * Scope a query to only include cases from a range + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param int $from + * @param int $to + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeRangeOfCases($query, int $from, int $to) + { + return $query->where('APP_DELEGATION.APP_NUMBER', '>=', $from) + ->where('APP_DELEGATION.APP_NUMBER', '<=', $to); + } + /** * Scope a query to only include specific cases * @@ -305,7 +332,7 @@ class Delegation extends Model * * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeTask($query, $task) + public function scopeTask($query, int $task) { return $query->where('APP_DELEGATION.TAS_ID', '=', $task); } @@ -1273,37 +1300,4 @@ class Delegation extends Model return $thread; } - - /** - * Helper to get the priority code from a given value - * - * @param int $priorityValue - * - * @return string - * - * @throws Exception - */ - public static function getPriorityCode($priorityValue) - { - if (!empty(self::PRIORITIES_MAP[$priorityValue])) { - $priorityCode = self::PRIORITIES_MAP[$priorityValue]; - } else { - throw new Exception("Priority value {$priorityValue} is not valid."); - } - return $priorityCode; - } - - /** - * Helper to get the priority label from a given value - * - * @param int $priorityValue - * - * @return string - */ - public static function getPriorityLabel($priorityValue) - { - $priorityCode = self::getPriorityCode($priorityValue); - - return G::LoadTranslation("ID_PRIORITY_{$priorityCode}"); - } }