Skip to content

Commit

Permalink
FOUR-12238
Browse files Browse the repository at this point in the history
  • Loading branch information
pmPaulis committed Dec 6, 2023
1 parent f4a9722 commit 0d01ffc
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 7 deletions.
20 changes: 14 additions & 6 deletions ProcessMaker/Http/Controllers/Api/BookmarkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace ProcessMaker\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Http\Resources\ApiResource;
use ProcessMaker\Http\Resources\ProcessCollection;
use ProcessMaker\Models\Bookmark;
use ProcessMaker\Models\Process;
Expand All @@ -16,8 +16,6 @@ public function index(Request $request)
{
// Get the user
$user = Auth::user();
// Get the order by to apply
$orderBy = $this->getRequestSortBy($request, 'name');
// Get the processes active
$processes = Process::nonSystem()->active();
// Filter pmql
Expand All @@ -34,7 +32,6 @@ public function index(Request $request)
->select('processes.*')
->leftJoin('user_process_bookmarks as bookmark', 'bookmark.process_id', '=', 'processes.id')
->where('bookmark.user_id', $user->id)
->orderBy(...$orderBy)
->get()
->collect();

Expand All @@ -53,11 +50,22 @@ public function store(Request $request, Process $process)
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 400);
}

return new ApiResource($bookmark->refresh());
}

public function destroy(Request $request, Process $process)
{
// Get the user
$user = Auth::user();
// 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();

return response([], 204);
}
}
1 change: 0 additions & 1 deletion ProcessMaker/Models/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use ProcessMaker\Models\ProcessMakerModel;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\User;

class Bookmark extends ProcessMakerModel
Expand Down
30 changes: 30 additions & 0 deletions database/factories/ProcessMaker/Models/BookmarkFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Database\Factories\ProcessMaker\Models;

use Illuminate\Database\Eloquent\Factories\Factory;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\User;

/**
* Bookmark factory
*/
class BookmarkFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'user_id' => function () {
return User::factory()->create()->getKey();
},
'process_id' => function () {
return Process::factory()->create()->getKey();
},
];
}
}
85 changes: 85 additions & 0 deletions tests/Feature/Api/BookmarkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Tests\Feature\Api;

use Illuminate\Support\Facades\Auth;
use ProcessMaker\Models\Bookmark;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\User;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;

class BookmarkTest extends TestCase
{
use RequestHelper;

const API_TEST_URL = '/process_bookmarks';

const STRUCTURE = [
'id',
'user_id',
'process_id',
'updated_at',
'created_at',
];

/**
* Test get bookmarks
*/
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);
}

/**
* Test store bookmarks
*/
public function testStoreBookmark()
{
// Create a fake
$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, []);
// Validate the header status code
$response->assertStatus(403);
}


/**
* 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, []);
// Call the api DELETE
$response = $this->apiCall('DELETE', self::API_TEST_URL .'/'. $process->id, [
'user_id' => $user->id
]);
// Validate the header status code
$response->assertStatus(204);
}
}

0 comments on commit 0d01ffc

Please sign in to comment.