diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/ApplicationTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/ApplicationTest.php index f4ef131b7..e39ad6519 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/ApplicationTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/ApplicationTest.php @@ -5,6 +5,7 @@ namespace Tests\unit\workflow\engine\src\ProcessMaker\Model; use Illuminate\Foundation\Testing\DatabaseTransactions; use ProcessMaker\Model\Application; use ProcessMaker\Model\Process; +use ProcessMaker\Model\User; use Tests\TestCase; /** @@ -16,10 +17,43 @@ class ApplicationTest extends TestCase { use DatabaseTransactions; + /** + * Test belongs to APP_CUR_USER + * + * @covers \ProcessMaker\Model\Application::currentUser() + * @test + */ + public function it_has_a_current_user() + { + $application = factory(Application::class)->create([ + 'APP_CUR_USER' => function () { + return factory(User::class)->create()->USR_UID; + } + ]); + $this->assertInstanceOf(User::class, $application->currentUser); + } + + /** + * Test belongs to APP_INIT_USER + * + * @covers \ProcessMaker\Model\Application::creatorUser() + * @test + */ + public function it_has_a_init_user() + { + $application = factory(Application::class)->create([ + 'APP_INIT_USER' => function () { + return factory(User::class)->create()->USR_UID; + } + ]); + $this->assertInstanceOf(User::class, $application->creatoruser); + } + /** * This checks if return the columns used * * @covers \ProcessMaker\Model\Application::getByProUid() + * @covers \ProcessMaker\Model\Application::scopeProUid() * @test */ public function it_return_cases_by_process() @@ -36,6 +70,7 @@ class ApplicationTest extends TestCase * This checks if return the columns used * * @covers \ProcessMaker\Model\Application::getCase() + * @covers \ProcessMaker\Model\Application::scopeAppUid() * @test */ public function it_return_case_information() @@ -45,4 +80,34 @@ class ApplicationTest extends TestCase $this->assertArrayHasKey('APP_STATUS', $result); $this->assertArrayHasKey('APP_INIT_USER', $result); } + + /** + * This checks if the columns was updated correctly + * + * @covers \ProcessMaker\Model\Application::updateColumns() + * @test + */ + public function it_update_columns() + { + // No column will be updated + $application = factory(Application::class)->create(); + $result = Application::updateColumns($application->APP_UID, []); + $this->isEmpty($result); + + // Tried to update APP_ROUTING_DATA + $application = factory(Application::class)->create(); + $result = Application::updateColumns($application->APP_UID, ['APP_ROUTING_DATA' => '']); + $this->assertArrayHasKey('APP_ROUTING_DATA', $result); + + // We can not update with a empty user + $application = factory(Application::class)->create(); + $result = Application::updateColumns($application->APP_UID, ['APP_CUR_USER' => '']); + $this->assertArrayNotHasKey('APP_CUR_USER', $result); + + // Tried to update APP_CUR_USER + $application = factory(Application::class)->create(); + $result = Application::updateColumns($application->APP_UID, ['APP_CUR_USER' => '00000000000000000000000000000001']); + $this->assertArrayHasKey('APP_CUR_USER', $result); + } + } diff --git a/workflow/engine/classes/Derivation.php b/workflow/engine/classes/Derivation.php index 1e92258f5..9e09638f5 100644 --- a/workflow/engine/classes/Derivation.php +++ b/workflow/engine/classes/Derivation.php @@ -1,20 +1,27 @@ appCurrentUser = $appFields['APP_CUR_USER']; + unset($appFields['APP_CUR_USER']); //We close the current derivation, then we'll try to derivate to each defined route $this->case->CloseCurrentDelegation( $currentDelegation['APP_UID'], $currentDelegation['DEL_INDEX'] ); @@ -1237,6 +1247,7 @@ class Derivation $nextDel['DEL_PRIORITY'] = 3; } + $newDelegationUser = ''; switch ($nextDel['TAS_ASSIGN_TYPE']) { case 'CANCEL_MI': case 'STATIC_MI': @@ -1266,12 +1277,14 @@ class Derivation $row = $criteriaMultiR->getRow(); $delPrevious = $row['DEL_PREVIOUS']; } + // Get the user that will create the new case + $newDelegationUser = $this->verifyCurrentUserInTask($nextDel, $aSP); // Create new delegation $iNewDelIndex = $this->case->newAppDelegation( $appFields['PRO_UID'], $currentDelegation['APP_UID'], $nextDel['TAS_UID'], - $this->verifyCurrentUserInTask($nextDel, $aSP), + $newDelegationUser, $currentDelegation['DEL_INDEX'], $nextDel['DEL_PRIORITY'], $delType, @@ -1297,10 +1310,15 @@ class Derivation } } - $application = new Application(); - $result = $application->update(['APP_UID' => $currentDelegation['APP_UID'], 'APP_ROUTING_DATA' => serialize($arrayRoutingData)]); + /** Update the table application */ + $applicationFields = [ + 'APP_ROUTING_DATA' => $arrayRoutingData, + 'APP_CUR_USER' => $newDelegationUser + ]; + $colUpdated = ModelApplication::updateColumns($currentDelegation['APP_UID'], $applicationFields); + $appFields['APP_CUR_USER'] = !empty($colUpdated['APP_CUR_USER']) ? $colUpdated['APP_CUR_USER'] : $this->appCurrentUser; - //We updated the information relate to APP_THREAD + // We updated the information relate to APP_THREAD $iAppThreadIndex = $appFields['DEL_THREAD']; $isUpdatedThread = false; if (isset($currentDelegation['ROUTE_TYPES']) && sizeof($currentDelegation['ROUTE_TYPES']) > 1) { diff --git a/workflow/engine/src/ProcessMaker/Model/Application.php b/workflow/engine/src/ProcessMaker/Model/Application.php index d34d2b91e..d9393bc57 100644 --- a/workflow/engine/src/ProcessMaker/Model/Application.php +++ b/workflow/engine/src/ProcessMaker/Model/Application.php @@ -16,20 +16,22 @@ class Application extends Model return $this->hasMany(Delegation::class, 'APP_UID', 'APP_UID'); } - public function parent() - { - return $this->hasOne(Application::class, 'APP_PARENT', 'APP_UID'); - } - public function currentUser() { - return $this->hasOne(User::class, 'APP_CUR_USER', 'USR_UID'); + return $this->belongsTo(User::class, 'APP_CUR_USER', 'USR_UID'); + } + + public function creatorUser() + { + return $this->belongsTo(User::class, 'APP_INIT_USER', 'USR_UID'); } /** * Scope for query to get the application by APP_UID. + * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $appUid + * * @return \Illuminate\Database\Eloquent\Builder */ public function scopeAppUid($query, $appUid) @@ -85,4 +87,26 @@ class Application extends Model return $firstElement; } + + /** + * Update properties + * + * @param string $appUid + * @param array $fields + * + * @return array + */ + public static function updateColumns($appUid, $fields) + { + $properties = []; + $properties['APP_ROUTING_DATA'] = !empty($fields['APP_ROUTING_DATA']) ? serialize($fields['APP_ROUTING_DATA']) : serialize([]); + + // This column will to update only when the thread is related to the user + if (!empty($fields['APP_CUR_USER'])) { + $properties['APP_CUR_USER'] = $fields['APP_CUR_USER']; + } + Application::query()->appUid($appUid)->update($properties); + + return $properties; + } }