From 741a424e4730618426adb0c19ca41b1774b9569b Mon Sep 17 00:00:00 2001 From: Andrea Adamczyk Date: Thu, 25 Nov 2021 15:08:32 -0400 Subject: [PATCH] PMCORE-3515 --- database/factories/ApplicationFactory.php | 12 + .../BusinessModel/Cases/DraftTest.php | 15 + .../BusinessModel/Cases/InboxTest.php | 33 +- .../BusinessModel/Cases/PausedTest.php | 16 + .../BusinessModel/Cases/UnassignedTest.php | 15 + .../src/ProcessMaker/Model/DelegationTest.php | 87 ++- .../ProcessMaker/Services/Api/MetricsTest.php | 566 ++++++++++++++++++ .../BusinessModel/Cases/AbstractCases.php | 24 +- .../BusinessModel/Cases/Draft.php | 12 + .../BusinessModel/Cases/Inbox.php | 12 + .../BusinessModel/Cases/Paused.php | 12 + .../BusinessModel/Cases/Unassigned.php | 12 + .../src/ProcessMaker/Model/Delegation.php | 58 ++ .../src/ProcessMaker/Services/Api/Metrics.php | 38 +- 14 files changed, 866 insertions(+), 46 deletions(-) diff --git a/database/factories/ApplicationFactory.php b/database/factories/ApplicationFactory.php index c545b3e11..74ba1720d 100644 --- a/database/factories/ApplicationFactory.php +++ b/database/factories/ApplicationFactory.php @@ -96,6 +96,18 @@ $factory->state(\ProcessMaker\Model\Application::class, 'draft', function (Faker ]; }); +$factory->state(\ProcessMaker\Model\Application::class, 'paused', function (Faker $faker) { + $user = factory(\ProcessMaker\Model\User::class)->create(); + + return [ + 'APP_NUMBER' => $faker->unique()->numberBetween(1000), + 'APP_STATUS_ID' => 1, + 'APP_STATUS' => 'PAUSED', + 'APP_INIT_USER' => $user->USR_UID, + 'APP_INIT_USER_ID' => $user->USR_ID, + ]; +}); + $factory->state(\ProcessMaker\Model\Application::class, 'completed', function (Faker $faker) { return [ 'APP_NUMBER' => $faker->unique()->numberBetween(1000), 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 427fce0ed..eb511e367 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/DraftTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/DraftTest.php @@ -28,6 +28,7 @@ class DraftTest extends TestCase public function setUp() { parent::setUp(); + Delegation::truncate(); } /** @@ -881,4 +882,18 @@ class DraftTest extends TestCase $res = $draft->getCasesRisk($process->PRO_ID, null, null, 'OVERDUE'); $this->assertCount(1, $res); } + + /** + * This tests the getCounterMetrics() method + * + * @covers \ProcessMaker\BusinessModel\Cases\Draft::getCounterMetrics() + * @test + */ + public function it_should_test_get_counter_metrics() + { + $this->createDraft(); + $draft = new Draft(); + $result = $draft->getCounterMetrics(); + $this->assertTrue($result > 0); + } } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/InboxTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/InboxTest.php index 13030d165..647bbd117 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/InboxTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/InboxTest.php @@ -31,6 +31,14 @@ class InboxTest extends TestCase public function setUp() { parent::setUp(); + Delegation::truncate(); + } + + /** + * Method tearDown + */ + public function tearDown() { + parent::tearDown(); } /** @@ -272,9 +280,14 @@ class InboxTest extends TestCase public function it_filter_by_thread_title() { // Create factories related to the to_do cases - $cases = $this->createInbox(); - $usrId = $cases->USR_ID; - $title = $cases->DEL_TITLE; + $delegation = factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2, + 'DEL_TITLE' => 'Test', + ]); + $usrId = $delegation->USR_ID; + $title = 'Test'; // We need to commit the records inserted because is needed for the "fulltext" index DB::commit(); // Create new Inbox object @@ -830,4 +843,18 @@ class InboxTest extends TestCase $res = $inbox->getCasesRisk($process->PRO_ID, null, null, "OVERDUE"); $this->assertCount(1, $res); } + + /** + * It tests the getCounterMetrics method + * + * @covers \ProcessMaker\BusinessModel\Cases\Inbox::getCounterMetrics() + * @test + */ + public function it_tests_get_counter_metrics() + { + $this->createInbox(); + $inbox = new Inbox(); + $res = $inbox->getCounterMetrics(); + $this->assertTrue($res > 0); + } } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/PausedTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/PausedTest.php index c73dee38d..c384325e8 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/PausedTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/PausedTest.php @@ -32,6 +32,7 @@ class PausedTest extends TestCase public function setUp() { parent::setUp(); + Delegation::truncate(); } /** @@ -911,4 +912,19 @@ class PausedTest extends TestCase $res = $paused->getCasesRisk($process1->PRO_ID, null, null, 'OVERDUE'); $this->assertCount(1, $res); } + + /** + * It tests the getCounterMetrics() method + * + * @covers \ProcessMaker\BusinessModel\Cases\Paused::getCounterMetrics() + * @test + */ + public function it_tests_get_counter_metrics() + { + $this->createMultiplePaused(3); + $paused = new Paused(); + + $res = $paused->getCounterMetrics(); + $this->assertTrue($res > 0); + } } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/UnassignedTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/UnassignedTest.php index e90207e3c..50db301dc 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/UnassignedTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/Cases/UnassignedTest.php @@ -34,6 +34,7 @@ class UnassignedTest extends TestCase public function setUp() { parent::setUp(); + Delegation::truncate(); } /** @@ -951,4 +952,18 @@ class UnassignedTest extends TestCase $res = $unassigned->getCasesRisk($process1->PRO_ID, null, null, 'OVERDUE'); $this->assertCount(1, $res); } + + /** + * This the getCounterMetrics method + * + * @covers \ProcessMaker\BusinessModel\Cases\Unassigned::getCounterMetrics() + * @test + */ + public function it_tests_get_counter_metrics() + { + $this->createSelfServiceUserOrGroup(); + $unassigned = new Unassigned; + $result = $unassigned->getCounterMetrics(); + $this->assertTrue($result > 0); + } } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/DelegationTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/DelegationTest.php index 8a2448bb5..be1fd91f6 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/DelegationTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/DelegationTest.php @@ -1154,7 +1154,11 @@ class DelegationTest extends TestCase */ public function it_should_search_and_filter_by_app_title() { - $delegations = factory(Delegation::class, 1)->states('foreign_keys')->create(); + $delegations = factory(Delegation::class, 1)->states('foreign_keys')->create([ + 'APP_NUMBER' => function () { + return factory(Application::class)->create()->APP_NUMBER; + } + ]); $title = $delegations->last()->DEL_TITLE; // We need to commit the records inserted because is needed for the "fulltext" index DB::commit(); @@ -3596,4 +3600,83 @@ class DelegationTest extends TestCase $res = $table->joinApplication()->participatedUser($table->USR_ID)->get(); $this->assertCount(1, $res); } -} + + /** + * Test the scopeInboxMetrics + * + * @covers \ProcessMaker\Model\Delegation::scopeInboxMetrics() + * @test + */ + public function it_tests_scope_inbox_metrics() + { + $application = factory(Application::class)->states('todo')->create(); + $table = factory(Delegation::class)->states('foreign_keys')->create([ + 'APP_NUMBER' => $application->APP_NUMBER, + 'APP_UID' => $application->APP_UID, + ]); + $res = $table->inboxMetrics()->get(); + $this->assertCount(1, $res); + } + + /** + * Test the scopeDraftMetrics + * + * @covers \ProcessMaker\Model\Delegation::scopeDraftMetrics() + * @test + */ + public function it_tests_scope_draft_metrics() + { + $application = factory(Application::class)->states('draft')->create(); + $table = factory(Delegation::class)->states('foreign_keys')->create([ + 'APP_NUMBER' => $application->APP_NUMBER, + 'APP_UID' => $application->APP_UID, + ]); + $res = $table->draftMetrics()->get(); + $this->assertCount(1, $res); + } + + /** + * Test the scopePausedMetrics + * + * @covers \ProcessMaker\Model\Delegation::scopePausedMetrics() + * @test + */ + public function it_tests_scope_paused_metrics() + { + $application = factory(Application::class)->states('paused')->create(); + $appDelay = factory(AppDelay::class)->states('paused_foreign_keys')->create([ + 'APP_NUMBER' => $application->APP_NUMBER, + 'APP_UID' => $application->APP_UID, + ]); + $table = factory(Delegation::class)->states('foreign_keys')->create([ + 'APP_NUMBER' => $application->APP_NUMBER, + 'APP_UID' => $application->APP_UID, + 'DEL_INDEX' => $appDelay->APP_DEL_INDEX, + ]); + $res = $table->pausedMetrics()->get(); + $this->assertCount(1, $res); + } + + /** + * Test the scopeSelfServiceMetrics + * + * @covers \ProcessMaker\Model\Delegation::scopeSelfServiceMetrics() + * @test + */ + public function it_tests_scope_self_service_metrics() + { + $application = factory(Application::class)->states('paused')->create(); + $task = factory(Task::class)->create([ + 'TAS_ASSIGN_TYPE' => 'SELF_SERVICE', + ]); + $delegation = factory(Delegation::class)->states('foreign_keys')->create([ + 'APP_NUMBER' => $application->APP_NUMBER, + 'APP_UID' => $application->APP_UID, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_ID' => 0, + ]); + $res = $delegation->selfServiceMetrics()->get(); + $this->assertCount(1, $res); + } +} \ No newline at end of file diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Services/Api/MetricsTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Services/Api/MetricsTest.php index a7491ee5e..9ebb33e06 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Services/Api/MetricsTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Services/Api/MetricsTest.php @@ -8,6 +8,13 @@ use Luracast\Restler\Defaults; use Luracast\Restler\HumanReadableCache; use Maveriks\Extension\Restler; use ProcessMaker\BusinessModel\Cases\Unassigned; +use ProcessMaker\Model\AppDelay; +use ProcessMaker\Model\Application as ApplicationModel; +use ProcessMaker\Model\Delegation; +use ProcessMaker\Model\Process; +use ProcessMaker\Model\Task; +use ProcessMaker\Model\TaskUser; +use ProcessMaker\Model\User; use ProcessMaker\Services\Api\Metrics; use ReflectionClass; use Tests\TestCase; @@ -31,6 +38,7 @@ class MetricsTest extends TestCase public function setUp() { parent::setUp(); + Delegation::truncate(); } /** * Initialize Rest API. @@ -80,6 +88,8 @@ class MetricsTest extends TestCase */ public function it_tests_get_counters_list_method_empty_lists() { + ApplicationModel::truncate(); + $user = factory(\ProcessMaker\Model\User::class)->create(); $this->initializeRestApi($user->USR_UID); @@ -154,4 +164,560 @@ class MetricsTest extends TestCase $res = $metrics->getCountersList(); $this->assertNotEmpty($res); } + + /** + * Tests the getProcessTotalCases method with inbox + * + * @test + */ + public function it_tests_get_process_total_cases_inbox() + { + factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2, + ]); + $metrics = new Metrics(); + $res = $metrics->getProcessTotalCases('inbox'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getProcessTotalCases method with draft + * + * @test + */ + public function it_tests_get_process_total_cases_draft() + { + $application = factory(ApplicationModel::class)->states('draft')->create(); + factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_INDEX' => 1, + 'USR_UID' => $application->APP_INIT_USER, + 'USR_ID' => $application->APP_INIT_USER_ID, + 'APP_UID' => $application->APP_UID, + 'APP_NUMBER' => $application->APP_NUMBER, + ]); + $metrics = new Metrics(); + $res = $metrics->getProcessTotalCases('draft'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getProcessTotalCases method with paused + * + * @test + */ + public function it_tests_get_process_total_cases_paused() + { + $process1 = factory(Process::class)->create( + ['PRO_CATEGORY' => '1'] + ); + $process2 = factory(Process::class)->create( + ['PRO_CATEGORY' => '2'] + ); + $user = factory(User::class)->create(); + $task = factory(Task::class)->create([ + 'TAS_ASSIGN_TYPE' => '', + 'TAS_GROUP_VARIABLE' => '', + 'PRO_UID' => $process1->PRO_UID, + 'TAS_TYPE' => 'NORMAL' + ]); + $application1 = factory(ApplicationModel::class)->create(); + $application2 = factory(ApplicationModel::class)->create(); + factory(Delegation::class)->create([ + 'APP_NUMBER' => $application1->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'CLOSED', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process1->PRO_ID, + 'PRO_UID' => $process1->PRO_UID, + 'DEL_PREVIOUS' => 0, + 'DEL_INDEX' => 1 + ]); + $delegation1 = factory(Delegation::class)->create([ + 'APP_NUMBER' => $application1->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'CLOSED', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process1->PRO_ID, + 'PRO_UID' => $process1->PRO_UID, + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2 + ]); + factory(Delegation::class)->create([ + 'APP_NUMBER' => $application2->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process2->PRO_ID, + 'PRO_UID' => $process2->PRO_UID, + 'DEL_PREVIOUS' => 0, + 'DEL_INDEX' => 1 + ]); + $delegation2 = factory(Delegation::class)->create([ + 'APP_NUMBER' => $application2->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process2->PRO_ID, + 'PRO_UID' => $process2->PRO_UID, + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2 + ]); + factory(AppDelay::class, 5)->create([ + 'APP_DELEGATION_USER' => $user->USR_UID, + 'PRO_UID' => $process2->PRO_UID, + 'APP_NUMBER' => $delegation1->APP_NUMBER, + 'APP_DEL_INDEX' => $delegation1->DEL_INDEX, + 'APP_DISABLE_ACTION_USER' => 0, + 'APP_TYPE' => 'PAUSE' + ]); + factory(AppDelay::class, 5)->create([ + 'APP_DELEGATION_USER' => $user->USR_UID, + 'PRO_UID' => $process2->PRO_UID, + 'APP_NUMBER' => $delegation2->APP_NUMBER, + 'APP_DEL_INDEX' => $delegation2->DEL_INDEX, + 'APP_DISABLE_ACTION_USER' => 0, + 'APP_TYPE' => 'PAUSE' + ]); + $metrics = new Metrics(); + $res = $metrics->getProcessTotalCases('paused'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getProcessTotalCases method with unassigned + * + * @test + */ + public function it_tests_get_process_total_cases_unassigned() + { + $user = factory(\ProcessMaker\Model\User::class)->create(); + $process = factory(Process::class)->create(); + $application = factory(ApplicationModel::class)->create([ + 'APP_STATUS_ID' => 2 + ]); + $task = factory(Task::class)->create([ + 'TAS_ASSIGN_TYPE' => 'SELF_SERVICE', + 'TAS_GROUP_VARIABLE' => '', + 'PRO_UID' => $process->PRO_UID, + 'PRO_ID' => $process->PRO_ID, + ]); + factory(TaskUser::class)->create([ + 'TAS_UID' => $task->TAS_UID, + 'USR_UID' => $user->USR_UID, + 'TU_RELATION' => 1, + 'TU_TYPE' => 1 + ]); + factory(Delegation::class)->create([ + 'APP_NUMBER' => $application->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'PRO_ID' => $process->PRO_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_ID' => 0, + 'DEL_DELEGATE_DATE' => date('Y-m-d H:i:s', strtotime("-1 year")) + ]); + $metrics = new Metrics(); + $res = $metrics->getProcessTotalCases('unassigned'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getTotalCasesByRange method with inbox + * + * @test + */ + public function it_tests_get_total_cases_by_range_inbox() + { + factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2, + ]); + $metrics = new Metrics(); + $res = $metrics->getTotalCasesByRange('inbox'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getTotalCasesByRange method with draft + * + * @test + */ + public function it_tests_get_total_cases_by_range_draft() + { + $application = factory(ApplicationModel::class)->states('draft')->create(); + factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_INDEX' => 1, + 'USR_UID' => $application->APP_INIT_USER, + 'USR_ID' => $application->APP_INIT_USER_ID, + 'APP_UID' => $application->APP_UID, + 'APP_NUMBER' => $application->APP_NUMBER, + ]); + $metrics = new Metrics(); + $res = $metrics->getTotalCasesByRange('draft'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getTotalCasesByRange method with paused + * + * @test + */ + public function it_tests_get_total_cases_by_range_paused() + { + $process1 = factory(Process::class)->create( + ['PRO_CATEGORY' => '1'] + ); + $process2 = factory(Process::class)->create( + ['PRO_CATEGORY' => '2'] + ); + $user = factory(User::class)->create(); + $task = factory(Task::class)->create([ + 'TAS_ASSIGN_TYPE' => '', + 'TAS_GROUP_VARIABLE' => '', + 'PRO_UID' => $process1->PRO_UID, + 'TAS_TYPE' => 'NORMAL' + ]); + $application1 = factory(ApplicationModel::class)->create(); + $application2 = factory(ApplicationModel::class)->create(); + factory(Delegation::class)->create([ + 'APP_NUMBER' => $application1->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'CLOSED', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process1->PRO_ID, + 'PRO_UID' => $process1->PRO_UID, + 'DEL_PREVIOUS' => 0, + 'DEL_INDEX' => 1 + ]); + $delegation1 = factory(Delegation::class)->create([ + 'APP_NUMBER' => $application1->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'CLOSED', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process1->PRO_ID, + 'PRO_UID' => $process1->PRO_UID, + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2 + ]); + factory(Delegation::class)->create([ + 'APP_NUMBER' => $application2->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process2->PRO_ID, + 'PRO_UID' => $process2->PRO_UID, + 'DEL_PREVIOUS' => 0, + 'DEL_INDEX' => 1 + ]); + $delegation2 = factory(Delegation::class)->create([ + 'APP_NUMBER' => $application2->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process2->PRO_ID, + 'PRO_UID' => $process2->PRO_UID, + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2 + ]); + factory(AppDelay::class, 5)->create([ + 'APP_DELEGATION_USER' => $user->USR_UID, + 'PRO_UID' => $process2->PRO_UID, + 'APP_NUMBER' => $delegation1->APP_NUMBER, + 'APP_DEL_INDEX' => $delegation1->DEL_INDEX, + 'APP_DISABLE_ACTION_USER' => 0, + 'APP_TYPE' => 'PAUSE' + ]); + factory(AppDelay::class, 5)->create([ + 'APP_DELEGATION_USER' => $user->USR_UID, + 'PRO_UID' => $process2->PRO_UID, + 'APP_NUMBER' => $delegation2->APP_NUMBER, + 'APP_DEL_INDEX' => $delegation2->DEL_INDEX, + 'APP_DISABLE_ACTION_USER' => 0, + 'APP_TYPE' => 'PAUSE' + ]); + $metrics = new Metrics(); + $res = $metrics->getTotalCasesByRange('paused'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getTotalCasesByRange method with unassigned + * + * @test + */ + public function it_tests_get_total_cases_by_range_unassigned() + { + $user = factory(\ProcessMaker\Model\User::class)->create(); + $process = factory(Process::class)->create(); + $application = factory(ApplicationModel::class)->create([ + 'APP_STATUS_ID' => 2 + ]); + $task = factory(Task::class)->create([ + 'TAS_ASSIGN_TYPE' => 'SELF_SERVICE', + 'TAS_GROUP_VARIABLE' => '', + 'PRO_UID' => $process->PRO_UID, + 'PRO_ID' => $process->PRO_ID, + ]); + factory(TaskUser::class)->create([ + 'TAS_UID' => $task->TAS_UID, + 'USR_UID' => $user->USR_UID, + 'TU_RELATION' => 1, + 'TU_TYPE' => 1 + ]); + factory(Delegation::class)->create([ + 'APP_NUMBER' => $application->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'PRO_ID' => $process->PRO_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_ID' => 0, + 'DEL_DELEGATE_DATE' => date('Y-m-d H:i:s', strtotime("-1 year")) + ]); + $metrics = new Metrics(); + $res = $metrics->getTotalCasesByRange('unassigned'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getCasesRiskByProcess method with inbox + * + * @test + */ + public function it_tests_get_cases_risk_by_process_inbox() + { + $process = factory(Process::class)->create(); + $delegation = factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2, + 'PRO_ID' => $process->PRO_ID, + 'DEL_RISK_DATE' => date('Y-m-d H:i:s'), + 'DEL_TASK_DUE_DATE' => date('Y-m-d H:i:s', strtotime("+1 hour")) + ]); + $metrics = new Metrics(); + $res = $metrics->getCasesRiskByProcess('inbox', $delegation->PRO_ID, null, null, 'AT_RISK'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getCasesRiskByProcess method with draft + * + * @test + */ + public function it_tests_get_cases_risk_by_process_draft() + { + $process = factory(Process::class)->create(); + $application = factory(ApplicationModel::class)->states('draft')->create(); + $delegation = factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_INDEX' => 1, + 'USR_UID' => $application->APP_INIT_USER, + 'USR_ID' => $application->APP_INIT_USER_ID, + 'APP_UID' => $application->APP_UID, + 'APP_NUMBER' => $application->APP_NUMBER, + 'PRO_ID' => $process->PRO_ID, + 'DEL_RISK_DATE' => date('Y-m-d H:i:s'), + 'DEL_TASK_DUE_DATE' => date('Y-m-d H:i:s', strtotime("+1 hour")) + ]); + $metrics = new Metrics(); + $res = $metrics->getCasesRiskByProcess('draft', $delegation->PRO_ID, null, null, 'AT_RISK'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getCasesRiskByProcess method with paused + * + * @test + */ + public function it_tests_get_cases_risk_by_process_paused() + { + $process1 = factory(Process::class)->create( + ['PRO_CATEGORY' => '1'] + ); + $process2 = factory(Process::class)->create( + ['PRO_CATEGORY' => '2'] + ); + $user = factory(User::class)->create(); + $task = factory(Task::class)->create([ + 'TAS_ASSIGN_TYPE' => '', + 'TAS_GROUP_VARIABLE' => '', + 'PRO_UID' => $process1->PRO_UID, + 'TAS_TYPE' => 'NORMAL' + ]); + $application1 = factory(ApplicationModel::class)->create(); + $application2 = factory(ApplicationModel::class)->create(); + factory(Delegation::class)->create([ + 'APP_NUMBER' => $application1->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'CLOSED', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process1->PRO_ID, + 'PRO_UID' => $process1->PRO_UID, + 'DEL_PREVIOUS' => 0, + 'DEL_INDEX' => 1, + 'DEL_RISK_DATE' => date('Y-m-d H:i:s'), + 'DEL_TASK_DUE_DATE' => date('Y-m-d H:i:s', strtotime("+1 hour")) + ]); + $delegation1 = factory(Delegation::class)->create([ + 'APP_NUMBER' => $application1->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'CLOSED', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process1->PRO_ID, + 'PRO_UID' => $process1->PRO_UID, + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2, + 'DEL_RISK_DATE' => date('Y-m-d H:i:s'), + 'DEL_TASK_DUE_DATE' => date('Y-m-d H:i:s', strtotime("+1 hour")) + ]); + factory(Delegation::class)->create([ + 'APP_NUMBER' => $application2->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process2->PRO_ID, + 'PRO_UID' => $process2->PRO_UID, + 'DEL_PREVIOUS' => 0, + 'DEL_INDEX' => 1, + 'DEL_RISK_DATE' => date('Y-m-d H:i:s'), + 'DEL_TASK_DUE_DATE' => date('Y-m-d H:i:s', strtotime("+1 hour")) + ]); + $delegation2 = factory(Delegation::class)->create([ + 'APP_NUMBER' => $application2->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_UID' => $user->USR_UID, + 'USR_ID' => $user->USR_ID, + 'PRO_ID' => $process2->PRO_ID, + 'PRO_UID' => $process2->PRO_UID, + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2, + 'DEL_RISK_DATE' => date('Y-m-d H:i:s'), + 'DEL_TASK_DUE_DATE' => date('Y-m-d H:i:s', strtotime("+1 hour")) + ]); + factory(AppDelay::class, 5)->create([ + 'APP_DELEGATION_USER' => $user->USR_UID, + 'PRO_UID' => $process2->PRO_UID, + 'APP_NUMBER' => $delegation1->APP_NUMBER, + 'APP_DEL_INDEX' => $delegation1->DEL_INDEX, + 'APP_DISABLE_ACTION_USER' => 0, + 'APP_TYPE' => 'PAUSE' + ]); + factory(AppDelay::class, 5)->create([ + 'APP_DELEGATION_USER' => $user->USR_UID, + 'PRO_UID' => $process2->PRO_UID, + 'APP_NUMBER' => $delegation2->APP_NUMBER, + 'APP_DEL_INDEX' => $delegation2->DEL_INDEX, + 'APP_DISABLE_ACTION_USER' => 0, + 'APP_TYPE' => 'PAUSE' + ]); + $metrics = new Metrics(); + $res = $metrics->getCasesRiskByProcess('paused', $delegation1->PRO_ID, null, null, 'AT_RISK'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getCasesRiskByProcess method with unassigned + * + * @test + */ + public function it_tests_get_cases_risk_by_process_unassigned() + { + $user = factory(\ProcessMaker\Model\User::class)->create(); + $process = factory(Process::class)->create(); + $application = factory(ApplicationModel::class)->create([ + 'APP_STATUS_ID' => 2 + ]); + $task = factory(Task::class)->create([ + 'TAS_ASSIGN_TYPE' => 'SELF_SERVICE', + 'TAS_GROUP_VARIABLE' => '', + 'PRO_UID' => $process->PRO_UID, + 'PRO_ID' => $process->PRO_ID, + ]); + factory(TaskUser::class)->create([ + 'TAS_UID' => $task->TAS_UID, + 'USR_UID' => $user->USR_UID, + 'TU_RELATION' => 1, + 'TU_TYPE' => 1 + ]); + $delegation = factory(Delegation::class)->create([ + 'APP_NUMBER' => $application->APP_NUMBER, + 'TAS_ID' => $task->TAS_ID, + 'PRO_ID' => $process->PRO_ID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'USR_ID' => 0, + 'DEL_DELEGATE_DATE' => date('Y-m-d H:i:s', strtotime("-1 year")), + 'DEL_RISK_DATE' => date('Y-m-d H:i:s'), + 'DEL_TASK_DUE_DATE' => date('Y-m-d H:i:s', strtotime("+1 hour")) + ]); + unset($RBAC); + $metrics = new Metrics(); + $res = $metrics->getCasesRiskByProcess('unassigned', $delegation->PRO_ID, null, null, 'AT_RISK'); + $this->assertNotEmpty($res); + } + + /** + * Tests the getProcessTotalCases method with exception + * + * @test + */ + public function it_tests_get_process_total_cases_exception() + { + factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2, + ]); + $metrics = new Metrics(); + $this->expectExceptionMessage("Undefined variable: list"); + $metrics->getProcessTotalCases(12, 123, "asda"); + } + + /** + * Tests the getTotalCasesByRange method with exception + * + * @test + */ + public function it_tests_get_total_cases_by_range_exception() + { + factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2, + ]); + $metrics = new Metrics(); + $this->expectExceptionMessage("Undefined variable: list"); + $metrics->getTotalCasesByRange(12, 123, "asda"); + } + + /** + * Tests the getCasesRiskByProcess method with exception + * + * @test + */ + public function it_tests_get_counters_list_exception() + { + factory(Delegation::class)->states('foreign_keys')->create([ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_PREVIOUS' => 1, + 'DEL_INDEX' => 2, + ]); + $metrics = new Metrics(); + $this->expectExceptionMessage("Undefined variable: list"); + $metrics->getCasesRiskByProcess(12, 123, "asda"); + } } diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCases.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCases.php index d3d4d8d30..6b684b7d5 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCases.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/AbstractCases.php @@ -1552,16 +1552,16 @@ class AbstractCases implements CasesInterface $list = end($listArray); switch ($list) { case 'Inbox': - $query->inbox($this->getUserId()); + $query->inboxMetrics(); break; case 'Draft': - $query->draft($this->getUserId()); + $query->draftMetrics(); break; case 'Paused': - $query->paused($this->getUserId()); + $query->pausedMetrics(); break; case 'Unassigned': - $query->selfService($this->getUserUid()); + $query->selfServiceMetrics(); break; } $query->joinProcess(); @@ -1604,16 +1604,16 @@ class AbstractCases implements CasesInterface $list = end($listArray); switch ($list) { case 'Inbox': - $query->inbox($this->getUserId()); + $query->inboxMetrics(); break; case 'Draft': - $query->draft($this->getUserId()); + $query->draftMetrics(); break; case 'Paused': - $query->paused($this->getUserId()); + $query->pausedMetrics(); break; case 'Unassigned': - $query->selfService($this->getUserUid()); + $query->selfServiceMetrics(); break; } $query->joinProcess(); @@ -1657,16 +1657,16 @@ class AbstractCases implements CasesInterface $list = end($listArray); switch ($list) { case 'Inbox': - $query->inbox($this->getUserId()); + $query->inboxMetrics(); break; case 'Draft': - $query->draft($this->getUserId()); + $query->draftMetrics(); break; case 'Paused': - $query->paused($this->getUserId()); + $query->pausedMetrics(); break; case 'Unassigned': - $query->selfService($this->getUserUid()); + $query->selfServiceMetrics(); break; } $query->joinProcess(); diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Draft.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Draft.php index 42e9e6720..60e9462f2 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Draft.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Draft.php @@ -244,4 +244,16 @@ class Draft extends AbstractCases 'total' => $count ]; } + + /** + * Count how many cases there are in DRAFT + * + * @return int + */ + public function getCounterMetrics() + { + $query = Delegation::query()->select(); + $query->draftMetrics(); + return $query->count(['APPLICATION.APP_NUMBER']); + } } diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Inbox.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Inbox.php index 3a7c95e64..337c09a5a 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Inbox.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Inbox.php @@ -260,4 +260,16 @@ class Inbox extends AbstractCases 'total' => $count ]; } + + /** + * Count how many cases there are in TO_DO + * + * @return int + */ + public function getCounterMetrics() + { + $query = Delegation::query()->select(); + $query->inboxMetrics(); + return $query->count(['APP_DELEGATION.APP_NUMBER']); + } } diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Paused.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Paused.php index fe9219f79..a0dbaa20e 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Paused.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Paused.php @@ -254,4 +254,16 @@ class Paused extends AbstractCases 'total' => $count ]; } + + /** + * Count how many cases there are in PAUSED + * + * @return int + */ + public function getCounterMetrics() + { + $query = Delegation::query()->select(); + $query->pausedMetrics(); + return $query->count(['APP_DELEGATION.APP_NUMBER']); + } } diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Unassigned.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Unassigned.php index 9bf328348..dbe41cfb4 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Unassigned.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Unassigned.php @@ -263,4 +263,16 @@ class Unassigned extends AbstractCases 'total' => $count ]; } + + /** + * Count how many cases there are in SELF_SERVICE + * + * @return int + */ + public function getCounterMetrics() + { + $query = Delegation::query()->select(); + $query->selfServiceMetrics(); + return $query->count(['APP_DELEGATION.APP_NUMBER']); + } } diff --git a/workflow/engine/src/ProcessMaker/Model/Delegation.php b/workflow/engine/src/ProcessMaker/Model/Delegation.php index 2519487bd..904af8e9d 100644 --- a/workflow/engine/src/ProcessMaker/Model/Delegation.php +++ b/workflow/engine/src/ProcessMaker/Model/Delegation.php @@ -1069,6 +1069,64 @@ class Delegation extends Model return $query; } + /** + * Scope the Inbox cases no matter the user + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeInboxMetrics($query) + { + $query->joinApplication(); + $query->status(Application::STATUS_TODO); + $query->threadOpen(); + return $query; + } + + /** + * Scope a draft cases no matter the user + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeDraftMetrics($query) + { + $query->joinApplication(); + $query->status(Application::STATUS_DRAFT); + $query->threadOpen(); + return $query; + } + + /** + * Scope paused cases list no matter the user + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopePausedMetrics($query) + { + $query->joinAppDelay('PAUSE'); + $query->joinApplication(); + return $query; + } + + /** + * Scope a self service cases no matter the user + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeSelfServiceMetrics($query) + { + $query->taskAssignType('SELF_SERVICE'); + $query->threadOpen()->withoutUserId(); + return $query; + } + /** * Get specific cases unassigned that the user can view * diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Metrics.php b/workflow/engine/src/ProcessMaker/Services/Api/Metrics.php index 583129b61..c9509015a 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Metrics.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Metrics.php @@ -3,11 +3,11 @@ namespace ProcessMaker\Services\Api; use Exception; +use Luracast\Restler\RestException; 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; @@ -46,8 +46,6 @@ class Metrics extends Api */ public function getProcessTotalCases($caseList, $category = null, $topTen = false, $processes = []) { - $usrUid = $this->getUserId(); - $usrId = !empty($usrUid) ? User::getId($usrUid) : 0; try { switch ($caseList) { case 'inbox': @@ -61,10 +59,8 @@ class Metrics extends Api break; case 'unassigned': $list = new Unassigned(); - $list->setUserUid($usrUid); break; } - $list->setUserId($usrId); $result = $list->getCountersByProcesses($category, $topTen, $processes); return $result; } catch (Exception $e) { @@ -91,8 +87,6 @@ class Metrics extends Api */ public function getTotalCasesByRange($caseList, $processId = null, $dateFrom = null, $dateTo = null, $groupBy = 'day') { - $usrUid = $this->getUserId(); - $usrId = !empty($usrUid) ? User::getId($usrUid) : 0; try { switch ($caseList) { case 'inbox': @@ -106,10 +100,8 @@ class Metrics extends Api break; case 'unassigned': $list = new Unassigned(); - $list->setUserUid($usrUid); break; } - $list->setUserId($usrId); $result = $list->getCountersByRange($processId, $dateFrom, $dateTo, $groupBy); return $result; } catch (Exception $e) { @@ -125,31 +117,21 @@ class Metrics extends Api * @return array * * @throws RestException + * + * @class AccessControl {@permission TASK_METRICS_VIEW} */ 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->setUserUid($usrUid); - $listDraft->setProperties($properties); - $listPaused = new Paused(); - $listPaused->setProperties($properties); - $listUnassigned = new Unassigned(); - $listUnassigned->setUserUid($usrUid); - $listUnassigned->setProperties($properties); - $casesInbox = $listInbox->getCounter(); - $casesDraft = $listDraft->getCounter(); - $casesPaused = $listPaused->getCounter(); - $casesUnassigned = $listUnassigned->getCounter(); + $casesInbox = $listInbox->getCounterMetrics(); + $casesDraft = $listDraft->getCounterMetrics(); + $casesPaused = $listPaused->getCounterMetrics(); + $casesUnassigned = $listUnassigned->getCounterMetrics(); $result = [ ['List Name' => 'Inbox', 'Total' => $casesInbox, 'Color' => 'green'], @@ -179,12 +161,12 @@ class Metrics extends Api * @return array * * @throws RestException + * + * @class AccessControl {@permission TASK_METRICS_VIEW} */ public function getCasesRiskByProcess($caseList = 'inbox', $process, $dateFrom = null, $dateTo = null, $riskStatus = 'ON_TIME', $topCases = null) { try { - $usrUid = $this->getUserId(); - $usrId = !empty($usrUid) ? User::getId($usrUid) : 0; switch ($caseList) { case 'inbox': $list = new Inbox(); @@ -197,10 +179,8 @@ class Metrics extends Api break; case 'unassigned': $list = new Unassigned(); - $list->setUserUid($usrUid); break; } - $list->setUserId($usrId); $result = $list->getCasesRisk($process, $dateFrom, $dateTo, $riskStatus, $topCases); return $result; } catch (Exception $e) {