diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/DraftTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/DraftTest.php index 9f0035ff5..41238d060 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/DraftTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/DraftTest.php @@ -1,6 +1,6 @@ getProperty('userId'); + $reflectionPropertyUserId->setAccessible(true); + $reflectionPropertyUserId->setValue($userUid); + + $reflectionPropertyDSN = $reflection->getProperty('dsn'); + $reflectionPropertyDSN->setAccessible(true); + $reflectionPropertyDSN->setValue('mysql:host=' . env('DB_HOST') . ';dbname=' . env('DB_DATABASE')); + + $reflectionPropertyUserName = $reflection->getProperty('dbUser'); + $reflectionPropertyUserName->setAccessible(true); + $reflectionPropertyUserName->setValue(env('DB_USERNAME')); + + $reflectionPropertyPassword = $reflection->getProperty('dbPassword'); + $reflectionPropertyPassword->setAccessible(true); + $reflectionPropertyPassword->setValue(env('DB_PASSWORD')); + + //application + Defaults::$cacheDirectory = PATH_DB . config('system.workspace') . PATH_SEP; + HumanReadableCache::$cacheDir = PATH_DB . config('system.workspace') . PATH_SEP; + + $rest = new Restler(true); + $rest->setFlagMultipart(false); + $rest->setAPIVersion('1.0'); + $rest->addAuthenticationClass('ProcessMaker\\Services\\OAuth2\\Server', ''); + $rest->addAuthenticationClass('ProcessMaker\\Policies\\AccessControl'); + $rest->addAuthenticationClass('ProcessMaker\\Policies\\ControlUnderUpdating'); + + $rest->apiMethodInfo = new ApiMethodInfo(); + return $rest; + } + + /** + * Tests the getCountersList method with empty lists + * + * @test + */ + public function it_tests_get_counters_list_method_empty_lists() + { + $user = factory(\ProcessMaker\Model\User::class)->create(); + $this->initializeRestApi($user->USR_UID); + + $metrics = new Metrics(); + $res = $metrics->getCountersList(); + + $this->assertEquals(0, $res[0]['Total']); + $this->assertEquals(0, $res[1]['Total']); + $this->assertEquals(0, $res[2]['Total']); + $this->assertEquals(0, $res[3]['Total']); + } + + /** + * Tests the getCountersList method + * + * @test + */ + public function it_tests_get_counters_list_method_inbox() + { + $inbox = new InboxTest(); + $user = $inbox->createMultipleInbox(10); + $this->initializeRestApi($user->USR_UID); + $metrics = new Metrics(); + $res = $metrics->getCountersList(); + $this->assertEquals(10, $res[0]['Total']); + } + + /** + * Tests the getCountersList method + * + * @test + */ + public function it_tests_get_counters_list_method_draft() + { + $draft = new DraftTest(); + $user = $draft->createManyDraft(10); + $this->initializeRestApi($user->USR_UID); + $metrics = new Metrics(); + $res = $metrics->getCountersList(); + $this->assertNotEmpty($res); + } + + /** + * Tests the getCountersList method + * + * @test + */ + public function it_tests_get_counters_list_method_paused() + { + $paused = new PausedTest(); + $user = $paused->createMultiplePaused(5); + $this->initializeRestApi($user->USR_UID); + $metrics = new Metrics(); + $res = $metrics->getCountersList(); + $this->assertEquals(5, $res[2]['Total']); + } + + /** + * Tests the getCountersList method + * + * @test + */ + public function it_tests_get_counters_list_method_unassigned() + { + $unassignedTest = new UnassignedTest(); + $cases = $unassignedTest->createMultipleUnassigned(3); + $unassigned = new Unassigned(); + $unassigned->setUserId($cases->USR_ID); + $unassigned->setUserUid($cases->USR_UID); + $this->initializeRestApi($cases->USR_UID); + $metrics = new Metrics(); + $res = $metrics->getCountersList(); + $this->assertNotEmpty($res); + } +} diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Metrics.php b/workflow/engine/src/ProcessMaker/Services/Api/Metrics.php index c8a8d1700..ba5f1b82e 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Metrics.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Metrics.php @@ -7,6 +7,7 @@ use ProcessMaker\BusinessModel\Cases\Draft; use ProcessMaker\BusinessModel\Cases\Inbox; use ProcessMaker\BusinessModel\Cases\Paused; use ProcessMaker\BusinessModel\Cases\Unassigned; +use ProcessMaker\Model\User; use ProcessMaker\Services\Api; use RBAC; @@ -107,4 +108,49 @@ class Metrics extends Api throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } + + /** + * Get total of cases per list + * + * @url /list-total-cases + * + * @return array + * + * @throws RestException + */ + public function getCountersList() + { + try { + $usrUid = $this->getUserId(); + $properties['user'] = !empty($usrUid) ? User::getId($usrUid) : 0; + + $listInbox = new Inbox(); + $listInbox->setProperties($properties); + + $listDraft = new Draft(); + $listDraft->setProperties($properties); + + $listPaused = new Paused(); + $listPaused->setProperties($properties); + + $listUnassigned = new Unassigned(); + $listUnassigned->setProperties($properties); + + $casesInbox = $listInbox->getCounter(); + $casesDraft = $listDraft->getCounter(); + $casesPaused = $listPaused->getCounter(); + $casesUnassigned = $listUnassigned->getCounter(); + + $result = [ + ['List Name' => 'Inbox', 'Total' => $casesInbox, 'Color' => 'green'], + ['List Name' => 'Draft', 'Total' => $casesDraft, 'Color' => 'yellow'], + ['List Name' => 'Paused', 'Total' => $casesPaused, 'Color' => 'blue'], + ['List Name' => 'Unassigned', 'Total' => $casesUnassigned, 'Color' => 'gray'] + ]; + + return $result; + } catch (Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } }