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();
|
||||
$process = \ProcessMaker\Model\Process::where('PRO_UID', $app->PRO_UID)->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 [
|
||||
'APP_UID' => $app->APP_UID,
|
||||
'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
|
||||
* 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 $limit for the pagination
|
||||
* @param string $search
|
||||
@@ -68,9 +68,10 @@ class Delegation extends Model
|
||||
*/
|
||||
|
||||
public static function search(
|
||||
$userUid,
|
||||
$start = null,
|
||||
$limit = null,
|
||||
$userId = null,
|
||||
// Default pagination values
|
||||
$start = 0,
|
||||
$limit = 25,
|
||||
$search = null,
|
||||
$process = null,
|
||||
$status = null,
|
||||
@@ -83,10 +84,6 @@ class Delegation extends Model
|
||||
) {
|
||||
$search = trim($search);
|
||||
|
||||
// Default pagination values
|
||||
$start = $start ? $start : 0;
|
||||
$limit = $limit ? $limit : 25;
|
||||
|
||||
// Start the query builder, selecting our base attributes
|
||||
$selectColumns = [
|
||||
'APPLICATION.APP_NUMBER',
|
||||
@@ -178,14 +175,14 @@ class Delegation extends Model
|
||||
|
||||
// Add join for user, but only for certain scenarios as sorting
|
||||
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');
|
||||
});
|
||||
}
|
||||
|
||||
// Search for specified user
|
||||
if ($userUid) {
|
||||
$query->where('APP_DELEGATION.USR_ID', $userUid);
|
||||
if ($userId) {
|
||||
$query->where('APP_DELEGATION.USR_ID', $userId);
|
||||
}
|
||||
|
||||
// Search for specified process
|
||||
@@ -241,6 +238,7 @@ class Delegation extends Model
|
||||
}
|
||||
|
||||
// Add any sort if needed
|
||||
if($sort) {
|
||||
switch ($sort) {
|
||||
case 'APP_NUMBER':
|
||||
$query->orderBy('APP_DELEGATION.APP_NUMBER', $dir);
|
||||
@@ -260,6 +258,7 @@ class Delegation extends Model
|
||||
default:
|
||||
$query->orderBy($sort, $dir);
|
||||
}
|
||||
}
|
||||
|
||||
// Add pagination to the query
|
||||
$query = $query->offset($start)
|
||||
|
||||
Reference in New Issue
Block a user