Skip to content

Commit

Permalink
Adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
pmPaulis committed Dec 7, 2023
1 parent 0d01ffc commit 5b43825
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 43 deletions.
23 changes: 8 additions & 15 deletions ProcessMaker/Http/Controllers/Api/BookmarkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -41,30 +42,22 @@ 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);
}

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);
}
Expand Down
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
56 changes: 29 additions & 27 deletions tests/Feature/Api/BookmarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,60 @@ class BookmarkTest extends TestCase
*/
public function testGetBookmark()
{
// Create a fake
$bookmark = Bookmark::factory()->count(10)->create();
$user = User::factory()->create();
// Create data
Bookmark::factory()->count(10)->create();
// Call the api GET
$response = $this->apiCall('GET', self::API_TEST_URL);
// Validate the header status code
$response->assertStatus(200);
$this->assertEmpty($response['data']);
// Create data related with the auth user
$user = Auth::user();
Bookmark::factory()->count(10)->create([
'user_id' => $user->id
]);
// Call the api GET
$response = $this->apiCall('GET', self::API_TEST_URL);
// Validate the header status code
$response->assertStatus(200);
$this->assertNotEmpty($response['data']);
}

/**
* Test store bookmarks
*/
public function testStoreBookmark()
{
// Create a fake
// Create data
$process = Process::factory()->create();
// 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, []);
// Create data
$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);
}
}

0 comments on commit 5b43825

Please sign in to comment.