From f11199559808cb8fc4ebdde57d1b0b7231675861 Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Thu, 7 Dec 2023 11:18:33 -0400 Subject: [PATCH] Adding test --- .../Controllers/Api/BookmarkController.php | 23 ++++------- routes/api.php | 2 +- tests/Feature/Api/BookmarkTest.php | 38 ++++++++----------- 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/BookmarkController.php b/ProcessMaker/Http/Controllers/Api/BookmarkController.php index 48ea2a0cad..16f994cc12 100644 --- a/ProcessMaker/Http/Controllers/Api/BookmarkController.php +++ b/ProcessMaker/Http/Controllers/Api/BookmarkController.php @@ -29,9 +29,10 @@ public function index(Request $request) } // Get the processes $processes = $processes - ->select('processes.*') + ->select('processes.*', 'bookmark.id as bookmark_id') ->leftJoin('user_process_bookmarks as bookmark', 'bookmark.process_id', '=', 'processes.id') ->where('bookmark.user_id', $user->id) + ->orderBy('processes.name', 'asc') ->get() ->collect(); @@ -41,12 +42,11 @@ public function index(Request $request) public function store(Request $request, Process $process) { $bookmark = new Bookmark(); - $bookmark->fill([ - 'process_id' => $process->id, - 'user_id' => Auth::user()->id - ]); try { - $bookmark->saveOrFail(); + $bookmark->updateOrCreate([ + 'process_id' => $process->id, + 'user_id' => Auth::user()->id + ]); } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()], 400); } @@ -54,17 +54,10 @@ public function store(Request $request, Process $process) return new ApiResource($bookmark->refresh()); } - public function destroy(Request $request, Process $process) + public function destroy(Bookmark $bookmark) { - // Get the current user - if ($request->user_id !== Auth::user()->id) { - abort(403); - } - // Delete bookmark - Bookmark::where('process_id', $process->id) - ->where('user_id', $request->user_id) - ->delete(); + $bookmark->delete(); return response([], 204); } diff --git a/routes/api.php b/routes/api.php index 3f6523e155..ffeec381da 100644 --- a/routes/api.php +++ b/routes/api.php @@ -140,7 +140,7 @@ // Process Bookmark Route::get('process_bookmarks', [BookmarkController::class, 'index'])->name('process_bookmarks.index')->middleware('can:view-processes'); Route::post('process_bookmarks/{process}', [BookmarkController::class, 'store'])->name('process_bookmarks.store')->middleware('can:edit-processes'); - Route::delete('process_bookmarks/{process}', [BookmarkController::class, 'destroy'])->name('process_bookmarks.destroy')->middleware('can:edit-processes'); + Route::delete('process_bookmarks/{bookmark}', [BookmarkController::class, 'destroy'])->name('process_bookmarks.destroy')->middleware('can:edit-processes'); // Process Categories Route::get('process_categories', [ProcessCategoryController::class, 'index'])->name('process_categories.index')->middleware('can:view-process-categories'); diff --git a/tests/Feature/Api/BookmarkTest.php b/tests/Feature/Api/BookmarkTest.php index 8821a0a3ce..03007a929a 100644 --- a/tests/Feature/Api/BookmarkTest.php +++ b/tests/Feature/Api/BookmarkTest.php @@ -30,11 +30,11 @@ public function testGetBookmark() { // Create a fake $bookmark = Bookmark::factory()->count(10)->create(); - $user = User::factory()->create(); // Call the api GET $response = $this->apiCall('GET', self::API_TEST_URL); // Validate the header status code $response->assertStatus(200); + $this->assertNotEmpty($response['data']); } /** @@ -47,39 +47,31 @@ public function testStoreBookmark() // Call the api POST $response = $this->apiCall('POST', self::API_TEST_URL .'/'. $process->id, []); // Validate the header status code - $response->assertStatus(201); - } - - /** - * Test delete bookmark with error 403 - */ - public function testDeleteBookmark403() - { - // Create a fake - $process = Process::factory()->create(); - // Call the api DELETE - $response = $this->apiCall('DELETE', self::API_TEST_URL .'/'. $process->id, []); + $response->assertStatus(200); + // Tried to save the same register twice + $user = Auth::user(); + // Call the api POST + $response = $this->apiCall('POST', self::API_TEST_URL .'/'. $process->id, []); // Validate the header status code - $response->assertStatus(403); + $response->assertStatus(200); + // Check if is only one register per user + $bookmark = Bookmark::where('process_id', $process->id)->where('user_id', $user->id)->get()->toArray(); + $this->assertCount(1, $bookmark); } - /** * Test delete bookmark */ public function testDeleteBookmark() { // Create a fake - $process = Process::factory()->create(); - $user = User::factory()->create(); - Auth::login($user); - // Call the api POST - $this->apiCall('POST', self::API_TEST_URL .'/'. $process->id, []); + $bookmark = Bookmark::factory()->create(); // Call the api DELETE - $response = $this->apiCall('DELETE', self::API_TEST_URL .'/'. $process->id, [ - 'user_id' => $user->id - ]); + $response = $this->apiCall('DELETE', self::API_TEST_URL .'/'. $bookmark->id); // Validate the header status code $response->assertStatus(204); + // Review if the item was deleted + $bookmark = Bookmark::where('id', $bookmark->id)->get()->toArray(); + $this->assertCount(0, $bookmark); } }