diff --git a/ProcessMaker/Http/Controllers/TaskController.php b/ProcessMaker/Http/Controllers/TaskController.php index e238b88701..56b20522b4 100755 --- a/ProcessMaker/Http/Controllers/TaskController.php +++ b/ProcessMaker/Http/Controllers/TaskController.php @@ -281,7 +281,7 @@ public function updateVariable(HttpRequest $request, $abe_uuid) $abe = ProcessAbeRequestToken::where('uuid', $abe_uuid)->first(); // Check if the token is available if (!$abe) { - return $this->returnErrorResponse(__('Token not found'), 404); + return $this->returnErrorResponse(__('Token not found'), 404, __('This link could not be found.')); } // Review if the autentication is required if ($abe->require_login && Auth::user()->username === AnonymousUser::ANONYMOUS_USERNAME) { @@ -301,15 +301,15 @@ public function updateVariable(HttpRequest $request, $abe_uuid) return redirect('login')->withCookie($cookie); } if ($abe->is_answered) { - return $this->returnErrorResponse(__('This response has already been answered'), 200); + return $this->returnErrorResponse(__('This response has already been answered'), 200, __('This task has already been answered. No changes were made.')); } else { // Get the token related $task = ProcessRequestToken::find($abe->process_request_token_id); if (!$task) { - return $this->returnErrorResponse(__('Process request token not found'), 404); + return $this->returnErrorResponse(__('Process request token not found'), 404, __('This task could not be found.')); } else { if ($task->status === 'CLOSED') { - return $this->returnErrorResponse(__('Task already closed'), 404); + return $this->returnErrorResponse(__('Task already closed'), 404, __('This task is already closed.')); } // Update the data $data[$request->varName] = $request->varValue; @@ -340,10 +340,16 @@ public function updateVariable(HttpRequest $request, $abe_uuid) } } } catch (\Exception $e) { - return response()->json([ - 'message' => 'Error updating variable', - 'error' => $e->getMessage(), - ], 500); + report($e); + + if ($request->expectsJson()) { + return response()->json([ + 'message' => 'Error updating variable', + 'error' => $e->getMessage(), + ], 500); + } + + return $this->returnErrorResponse(__('An error occurred while processing your response.'), 500); } } @@ -365,12 +371,19 @@ private function showScreen($screenId) } /** - * Return response + * Return an ABE result in the format requested by the client. * @param string $message * @param int $status + * @param string|null $htmlMessage */ - public function returnErrorResponse(string $message, int $status) + public function returnErrorResponse(string $message, int $status, ?string $htmlMessage = null) { + if (!request()->expectsJson()) { + return response()->view('tasks.abe-response', [ + 'message' => $htmlMessage ?? $message, + ], $status); + } + return response()->json([ 'message' => $message, ], $status); diff --git a/resources/views/errors/base.blade.php b/resources/views/errors/base.blade.php index 4190c2ed11..56fb1a412d 100644 --- a/resources/views/errors/base.blade.php +++ b/resources/views/errors/base.blade.php @@ -12,6 +12,7 @@
@yield('message')
+ @section('buttons')
@@ -22,6 +23,7 @@
+ @show @endsection diff --git a/resources/views/tasks/abe-response.blade.php b/resources/views/tasks/abe-response.blade.php new file mode 100644 index 0000000000..907da6522f --- /dev/null +++ b/resources/views/tasks/abe-response.blade.php @@ -0,0 +1,10 @@ +@extends('errors.base') + +@section('title', __('Actions by Email')) + +@section('message') +

{{ __('Actions by Email') }}

+

{{ $message }}

+@endsection + +@section('buttons', '') diff --git a/tests/Feature/TaskActionByEmailResponseTest.php b/tests/Feature/TaskActionByEmailResponseTest.php new file mode 100644 index 0000000000..196bd86600 --- /dev/null +++ b/tests/Feature/TaskActionByEmailResponseTest.php @@ -0,0 +1,145 @@ + [404, 'Token not found', 'This link could not be found.'], + 'answered' => [200, 'This response has already been answered', 'This task has already been answered. No changes were made.'], + 'missing-task' => [404, 'Process request token not found', 'This task could not be found.'], + 'closed' => [404, 'Task already closed', 'This task is already closed.'], + 'submitted' => [200, 'Your response has been submitted.', 'Your response has been submitted.'], + 'exception' => [500, 'Error updating variable', 'An error occurred while processing your response.'], + ]; + $cases = []; + foreach ($results as $result => [$status, $jsonMessage, $htmlMessage]) { + foreach ([false, true] as $json) { + $cases[$result . ($json ? '-json' : '-html')] = [$result, $json, $status, $jsonMessage, $htmlMessage]; + } + } + + return $cases; + } + + #[DataProvider('resultCases')] + public function testAbeResultFormat(string $result, bool $json, int $status, string $jsonMessage, string $htmlMessage): void + { + $abe = $this->createAbe(); + if ($result === 'answered') { + $abe->update(['is_answered' => true]); + } elseif ($result === 'missing-task') { + $abe->update(['process_request_token_id' => null]); + } elseif ($result === 'closed') { + ProcessRequestToken::findOrFail($abe->process_request_token_id)->update(['status' => 'CLOSED']); + } + + if ($result === 'submitted') { + WorkflowManager::shouldReceive('completeTask')->once()->andReturnNull(); + } elseif ($result === 'exception') { + WorkflowManager::shouldReceive('completeTask')->once()->andThrow(new \RuntimeException('Internal diagnostic detail')); + } else { + WorkflowManager::shouldReceive('completeTask')->never(); + } + + $uuid = $result === 'missing-link' ? (string) Str::uuid() : $abe->uuid; + $url = '/tasks/update_variable/' . $uuid . '?varName=response&varValue=second'; + $this->actingAs($this->user, 'web'); + $response = $this->get($url, ['Accept' => $json ? 'application/json' : 'text/html']); + $response->assertStatus($status); + + if ($json) { + $expected = ['message' => $jsonMessage]; + if ($result === 'exception') { + $expected['error'] = 'Internal diagnostic detail'; + } + $response->assertExactJson($expected); + } else { + $response->assertViewIs('tasks.abe-response'); + $response->assertSee($htmlMessage); + $response->assertHeader('Content-Type', 'text/html; charset=utf-8'); + $response->assertDontSee('Internal diagnostic detail'); + $response->assertDontSee('javascript:history.back()', false); + $response->assertDontSee('class="buttons"', false); + } + } + + public static function sessionCases(): array + { + return ['authenticated' => [true], 'guest' => [false]]; + } + + #[DataProvider('sessionCases')] + public function testRepeatedAnswerDoesNotChangeOriginalState(bool $authenticated): void + { + $abe = $this->createAbe(); + $abe->update(['is_answered' => true, 'answered_at' => now()->subHour(), 'data' => json_encode(['response' => 'original']), 'user_id' => $this->user->id]); + $task = ProcessRequestToken::findOrFail($abe->process_request_token_id); + $task->update(['status' => 'CLOSED']); + $case = ProcessRequest::findOrFail($abe->process_request_id); + $before = [$abe->fresh()->getAttributes(), $task->fresh()->getAttributes(), $case->fresh()->getAttributes()]; + WorkflowManager::shouldReceive('completeTask')->never(); + + if ($authenticated) { + $this->actingAs($this->user, 'web'); + } else { + Auth::forgetGuards(); + } + $response = $this->get('/tasks/update_variable/' . $abe->uuid . '?varName=response&varValue=second'); + $response->assertOk()->assertViewIs('tasks.abe-response'); + $response->assertSee('This task has already been answered. No changes were made.'); + $this->assertSame($before, [$abe->fresh()->getAttributes(), $task->fresh()->getAttributes(), $case->fresh()->getAttributes()]); + } + + public function testGuestMustStillLogInWhenRequired(): void + { + $abe = $this->createAbe(); + $abe->update(['is_answered' => true, 'require_login' => true]); + Auth::forgetGuards(); + $response = $this->get('/tasks/update_variable/' . $abe->uuid . '?varName=response&varValue=second'); + $response->assertRedirect('login'); + $this->assertTrue((bool) $abe->fresh()->is_answered); + } + + public function testStandardNotFoundPageRetainsNavigation(): void + { + $html = view('errors.404')->render(); + $this->assertStringContainsString('class="buttons"', $html); + $this->assertStringContainsString('javascript:history.back()', $html); + } + + private function createAbe(): ProcessAbeRequestToken + { + $process = Process::factory()->create(['user_id' => $this->user->id]); + $case = ProcessRequest::factory()->create(['process_id' => $process->id, 'user_id' => $this->user->id]); + $task = ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $case->id, + 'user_id' => $this->user->id, + 'status' => 'ACTIVE', + 'is_self_service' => true, + ]); + + return ProcessAbeRequestToken::create([ + 'process_request_id' => $case->id, + 'process_request_token_id' => $task->id, + 'require_login' => false, + 'is_answered' => false, + ]); + } +} diff --git a/tests/Feature/TaskControllerTest.php b/tests/Feature/TaskControllerTest.php index 262d17ae3c..b0362fefce 100644 --- a/tests/Feature/TaskControllerTest.php +++ b/tests/Feature/TaskControllerTest.php @@ -143,7 +143,8 @@ public function testReturnMessageTokenNoFound() 'GET', 'tasks/update_variable/' . $token . '?varName=res&varValue=yes' ); - $response->assertSee('Token not found'); + $response->assertViewIs('tasks.abe-response'); + $response->assertSee('This link could not be found.'); $response->assertStatus(404); } }