diff --git a/ProcessMaker/Models/ProcessRequest.php b/ProcessMaker/Models/ProcessRequest.php index a9c6ad3865..bceeb109bc 100644 --- a/ProcessMaker/Models/ProcessRequest.php +++ b/ProcessMaker/Models/ProcessRequest.php @@ -211,19 +211,25 @@ public function __construct(array $argument = []) } /** - * Validation rules. + * Get the validation rules for process requests. * - * @param null $existing + * @param mixed|null $existing ID of existing process request * - * @return array + * @return array Array of validation rules for the process request fields: + * - name: Required string, max 100 chars, alpha spaces only + * - data: Required field + * - status: Must be one of: ACTIVE, COMPLETED, ERROR, CANCELED + * - process_id: Required and must exist in processes table + * - process_collaboration_id: Optional but must exist in process_collaborations table if provided + * - user_id: Optional but must exist in users table if provided */ public static function rules($existing = null) { $self = new self(); - $unique = Rule::unique($self->getConnectionName() . '.process_requests')->ignore($existing); + $nameRules = ['required', 'string', 'max:100', 'alpha_spaces']; return [ - 'name' => ['required', 'string', 'max:100', $unique, 'alpha_spaces'], + 'name' => $nameRules, 'data' => 'required', 'status' => 'in:ACTIVE,COMPLETED,ERROR,CANCELED', 'process_id' => 'required|exists:processes,id', @@ -986,6 +992,7 @@ public function getRequestAsArray() { $array = $this->toArray(); unset($array['process_version']['svg']); + return $array; } diff --git a/tests/Feature/Api/ProcessRequestsTest.php b/tests/Feature/Api/ProcessRequestsTest.php index ba847d98de..280ee9c97a 100644 --- a/tests/Feature/Api/ProcessRequestsTest.php +++ b/tests/Feature/Api/ProcessRequestsTest.php @@ -384,24 +384,85 @@ public function testUpdateProcessRequest() } /** - * Check that the validation wont allow duplicate requestnames + * Test updating process request status through various state transitions + * + * Tests the following status transitions: + * - ACTIVE -> CANCELED + * - CANCELED -> ACTIVE + * - ACTIVE -> ERROR + * - ERROR -> ACTIVE + * - ACTIVE -> CANCELED + * - CANCELED -> ERROR + * + * Each transition should return a 204 status code indicating success */ - public function testUpdateProcessRequestTitleExists() + public function testUpdateProcessRequestStatus() { + $process = Process::factory()->create(); $request1 = ProcessRequest::factory()->create([ 'name' => 'MyRequestName', + 'status' => 'ACTIVE', + 'process_id' => $process->id, + 'data' => ['foo' => 'baz'], ]); - $request2 = ProcessRequest::factory()->create(); + // Try to update status to CANCELED + $url = self::API_TEST_URL . '/' . $request1->id; + $response = $this->apiCall('PUT', $url, [ + 'status' => 'CANCELED', + ]); + + // Verify status was updated + $response->assertStatus(204); + + $response = $this->apiCall('PUT', $url, [ + 'status' => 'ACTIVE', + 'name' => 'MyRequestName', + 'process_id' => $process->id, + 'data' => ['foo' => 'baz'], + ]); - $url = self::API_TEST_URL . '/' . $request2->id; + // Verify status was updated + $response->assertStatus(204); $response = $this->apiCall('PUT', $url, [ + 'status' => 'ERROR', 'name' => 'MyRequestName', + 'process_id' => $process->id, + 'data' => ['foo' => 'baz'], ]); - //Validate the header status code - $response->assertStatus(422); - $response->assertSeeText('The Name has already been taken'); + // Verify status was updated + $response->assertStatus(204); + + $response = $this->apiCall('PUT', $url, [ + 'status' => 'ACTIVE', + 'name' => 'MyRequestName', + 'process_id' => $process->id, + 'data' => ['foo' => 'baz'], + ]); + + // Verify status was updated + $response->assertStatus(204); + + $response = $this->apiCall('PUT', $url, [ + 'status' => 'CANCELED', + 'name' => 'MyRequestName', + 'process_id' => $process->id, + 'data' => ['foo' => 'baz'], + ]); + + // Verify status was updated + $response->assertStatus(204); + + $response = $this->apiCall('PUT', $url, [ + 'status' => 'ERROR', + 'name' => 'MyRequestName', + 'process_id' => $process->id, + 'data' => ['foo' => 'baz'], + ]); + + // Verify status was updated + $response->assertStatus(204); } /**