Merged in feature/PMCORE-3154 (pull request #7989)

PMCORE-3154

Approved-by: Julio Cesar Laura Avendaño
This commit is contained in:
Andrea Adamczyk
2021-07-22 16:12:26 +00:00
committed by Julio Cesar Laura Avendaño
4 changed files with 205 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\unit\workflow\src\ProcessMaker\BusinessModel\Cases;
namespace Tests\unit\workflow\engine\src\ProcessMaker\BusinessModel\Cases;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\unit\workflow\src\ProcessMaker\BusinessModel\Cases;
namespace Tests\unit\workflow\engine\src\ProcessMaker\BusinessModel\Cases;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;

View File

@@ -0,0 +1,157 @@
<?php
namespace Tests\unit\workflow\engine\src\ProcessMaker\Services\Api;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Luracast\Restler\Data\ApiMethodInfo;
use Luracast\Restler\Defaults;
use Luracast\Restler\HumanReadableCache;
use Maveriks\Extension\Restler;
use ProcessMaker\BusinessModel\Cases\Unassigned;
use ProcessMaker\Services\Api\Metrics;
use ReflectionClass;
use Tests\TestCase;
use Tests\unit\workflow\engine\src\ProcessMaker\BusinessModel\Cases\DraftTest;
use Tests\unit\workflow\engine\src\ProcessMaker\BusinessModel\Cases\InboxTest;
use Tests\unit\workflow\engine\src\ProcessMaker\BusinessModel\Cases\PausedTest;
use Tests\unit\workflow\engine\src\ProcessMaker\BusinessModel\Cases\UnassignedTest;
/**
* Class MetricsTest
*
* @coversDefaultClass @covers \ProcessMaker\Services\Api\Metrics
*/
class MetricsTest extends TestCase
{
use DatabaseTransactions;
/**
* Method set up.
*/
public function setUp()
{
parent::setUp();
}
/**
* Initialize Rest API.
* @param string $userUid
* @return Restler
*/
private function initializeRestApi(string $userUid)
{
//server
$reflection = new ReflectionClass('\ProcessMaker\Services\OAuth2\Server');
$reflectionPropertyUserId = $reflection->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);
}
}

View File

@@ -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());
}
}
}