Add sample tests. Change userUid argument to be properly userId. Do some safe checks in search.
This commit is contained in:
committed by
Paula Quispe
parent
9fa4c1750a
commit
17e6ef3441
@@ -7,7 +7,14 @@ $factory->define(\ProcessMaker\Model\Delegation::class, function(Faker $faker) {
|
|||||||
$app = factory(\ProcessMaker\Model\Application::class)->create();
|
$app = factory(\ProcessMaker\Model\Application::class)->create();
|
||||||
$process = \ProcessMaker\Model\Process::where('PRO_UID', $app->PRO_UID)->first();
|
$process = \ProcessMaker\Model\Process::where('PRO_UID', $app->PRO_UID)->first();
|
||||||
$task = $process->tasks->first();
|
$task = $process->tasks->first();
|
||||||
$user = \ProcessMaker\Model\User::all()->random();
|
|
||||||
|
// Grab a user if random
|
||||||
|
$users = \ProcessMaker\Model\User::all();
|
||||||
|
if(!count($users)) {
|
||||||
|
$user = factory(\ProcessMaker\Model\User::class)->create();
|
||||||
|
} else{
|
||||||
|
$user = $users->random();
|
||||||
|
}
|
||||||
return [
|
return [
|
||||||
'APP_UID' => $app->APP_UID,
|
'APP_UID' => $app->APP_UID,
|
||||||
'DEL_INDEX' => 1,
|
'DEL_INDEX' => 1,
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
namespace Tests\unit\workflow\src\ProcessMaker\Model;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use ProcessMaker\Model\User;
|
||||||
|
use ProcessMaker\Model\Process;
|
||||||
|
use ProcessMaker\Model\Delegation;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class DelegationTest extends TestCase
|
||||||
|
{
|
||||||
|
use DatabaseTransactions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This checks to make sure pagination is working properly
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function it_should_return_pages_of_data()
|
||||||
|
{
|
||||||
|
factory(\ProcessMaker\Model\User::class,100)->create();
|
||||||
|
factory(\ProcessMaker\Model\Process::class,10)->create();
|
||||||
|
factory(Delegation::class, 51)->create();
|
||||||
|
// Get first page, which is 25
|
||||||
|
$results = Delegation::search(null, 0, 25);
|
||||||
|
$this->assertCount(25, $results['data']);
|
||||||
|
// Get second page, which is 25 results
|
||||||
|
$results = Delegation::search(null, 25, 25);
|
||||||
|
$this->assertCount(25, $results['data']);
|
||||||
|
// Get third page, which is only 1 result
|
||||||
|
$results = Delegation::search(null, 50, 25);
|
||||||
|
$this->assertCount(1, $results['data']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This ensures searching for a valid user works
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function it_should_return_one_result_for_specified_user()
|
||||||
|
{
|
||||||
|
factory(\ProcessMaker\Model\User::class,100)->create();
|
||||||
|
factory(\ProcessMaker\Model\Process::class,10)->create();
|
||||||
|
// Create our unique user, with a unique username
|
||||||
|
$user = factory(\ProcessMaker\Model\User::class)->create([
|
||||||
|
'USR_USERNAME' => 'testcaseuser'
|
||||||
|
]);
|
||||||
|
// Create a new delegation, but for this specific user
|
||||||
|
factory(Delegation::class)->create([
|
||||||
|
'USR_UID' => $user->USR_UID,
|
||||||
|
'USR_ID' => $user->id
|
||||||
|
]);
|
||||||
|
// Now fetch results, and assume delegation count is 1 and the user points to our user
|
||||||
|
$results = Delegation::search($user->id);
|
||||||
|
$this->assertCount(1, $results['data']);
|
||||||
|
$this->assertEquals('testcaseuser', $results['data'][0]['USRCR_USR_USERNAME']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function it_should_have_data_match_certain_schema()
|
||||||
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function it_should_sort_by_case_id()
|
||||||
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function it_should_sort_by_user()
|
||||||
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -51,7 +51,7 @@ class Delegation extends Model
|
|||||||
* The query is related to advanced search with different filters
|
* The query is related to advanced search with different filters
|
||||||
* We can search by process, status of case, category of process, users, delegate date from and to
|
* We can search by process, status of case, category of process, users, delegate date from and to
|
||||||
*
|
*
|
||||||
* @param string $userUid
|
* @param integer $userId The USR_ID to search for (Note, this is no longer the USR_UID)
|
||||||
* @param integer $start for the pagination
|
* @param integer $start for the pagination
|
||||||
* @param integer $limit for the pagination
|
* @param integer $limit for the pagination
|
||||||
* @param string $search
|
* @param string $search
|
||||||
@@ -68,9 +68,10 @@ class Delegation extends Model
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public static function search(
|
public static function search(
|
||||||
$userUid,
|
$userId = null,
|
||||||
$start = null,
|
// Default pagination values
|
||||||
$limit = null,
|
$start = 0,
|
||||||
|
$limit = 25,
|
||||||
$search = null,
|
$search = null,
|
||||||
$process = null,
|
$process = null,
|
||||||
$status = null,
|
$status = null,
|
||||||
@@ -83,10 +84,6 @@ class Delegation extends Model
|
|||||||
) {
|
) {
|
||||||
$search = trim($search);
|
$search = trim($search);
|
||||||
|
|
||||||
// Default pagination values
|
|
||||||
$start = $start ? $start : 0;
|
|
||||||
$limit = $limit ? $limit : 25;
|
|
||||||
|
|
||||||
// Start the query builder, selecting our base attributes
|
// Start the query builder, selecting our base attributes
|
||||||
$selectColumns = [
|
$selectColumns = [
|
||||||
'APPLICATION.APP_NUMBER',
|
'APPLICATION.APP_NUMBER',
|
||||||
@@ -178,14 +175,14 @@ class Delegation extends Model
|
|||||||
|
|
||||||
// Add join for user, but only for certain scenarios as sorting
|
// Add join for user, but only for certain scenarios as sorting
|
||||||
if ($sort == 'APP_CURRENT_USER') {
|
if ($sort == 'APP_CURRENT_USER') {
|
||||||
$query->join('USERS', function ($join) use ($userUid) {
|
$query->join('USERS', function ($join) use ($userId) {
|
||||||
$join->on('APP_DELEGATION.USR_ID', '=', 'USERS.USR_ID');
|
$join->on('APP_DELEGATION.USR_ID', '=', 'USERS.USR_ID');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search for specified user
|
// Search for specified user
|
||||||
if ($userUid) {
|
if ($userId) {
|
||||||
$query->where('APP_DELEGATION.USR_ID', $userUid);
|
$query->where('APP_DELEGATION.USR_ID', $userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search for specified process
|
// Search for specified process
|
||||||
@@ -241,24 +238,26 @@ class Delegation extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add any sort if needed
|
// Add any sort if needed
|
||||||
switch ($sort) {
|
if($sort) {
|
||||||
case 'APP_NUMBER':
|
switch ($sort) {
|
||||||
$query->orderBy('APP_DELEGATION.APP_NUMBER', $dir);
|
case 'APP_NUMBER':
|
||||||
break;
|
$query->orderBy('APP_DELEGATION.APP_NUMBER', $dir);
|
||||||
case 'APP_PRO_TITLE':
|
break;
|
||||||
// We can do this because we joined the process table if sorting by it
|
case 'APP_PRO_TITLE':
|
||||||
$query->orderBy('PROCESS.PRO_TITLE', $dir);
|
// We can do this because we joined the process table if sorting by it
|
||||||
break;
|
$query->orderBy('PROCESS.PRO_TITLE', $dir);
|
||||||
case 'APP_TAS_TITLE':
|
break;
|
||||||
$query->orderBy('TASK.TAS_TITLE', $dir);
|
case 'APP_TAS_TITLE':
|
||||||
break;
|
$query->orderBy('TASK.TAS_TITLE', $dir);
|
||||||
case 'APP_CURRENT_USER':
|
break;
|
||||||
// We can do this because we joined the user table if sorting by it
|
case 'APP_CURRENT_USER':
|
||||||
$query->orderBy('USERS.USR_LASTNAME', $dir);
|
// We can do this because we joined the user table if sorting by it
|
||||||
$query->orderBy('USERS.USR_FIRSTNAME', $dir);
|
$query->orderBy('USERS.USR_LASTNAME', $dir);
|
||||||
break;
|
$query->orderBy('USERS.USR_FIRSTNAME', $dir);
|
||||||
default:
|
break;
|
||||||
$query->orderBy($sort, $dir);
|
default:
|
||||||
|
$query->orderBy($sort, $dir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add pagination to the query
|
// Add pagination to the query
|
||||||
|
|||||||
Reference in New Issue
Block a user