diff --git a/ProcessMaker/Http/Controllers/Api/V1_1/ClipboardController.php b/ProcessMaker/Http/Controllers/Api/V1_1/ClipboardController.php index 96a62cb40a..f7bcfd7b35 100644 --- a/ProcessMaker/Http/Controllers/Api/V1_1/ClipboardController.php +++ b/ProcessMaker/Http/Controllers/Api/V1_1/ClipboardController.php @@ -30,7 +30,12 @@ public function show(int $clipboardId): Resource public function showByUserId(): Resource { $userId = Auth::id(); - $clipboard = Clipboard::where('user_id', $userId)->firstOrFail(); + $clipboard = Clipboard::firstOrCreate( + ['user_id' => $userId], + ['config' => [], 'type' => 'FORM'] + ); + // JsonResource responds with 201 for new models, but this GET should remain 200. + $clipboard->wasRecentlyCreated = false; return new Resource($clipboard); } diff --git a/tests/Feature/Api/V1_1/ClipboardControllerTest.php b/tests/Feature/Api/V1_1/ClipboardControllerTest.php new file mode 100644 index 0000000000..374b9b0133 --- /dev/null +++ b/tests/Feature/Api/V1_1/ClipboardControllerTest.php @@ -0,0 +1,60 @@ +user->id)->delete(); + + $response = $this->apiCall('GET', '/api/1.1/clipboard/get_by_user'); + + $response->assertStatus(200) + ->assertJson([ + 'user_id' => $this->user->id, + 'type' => 'FORM', + 'config' => [], + ]); + + $this->assertDatabaseHas('clipboards', [ + 'user_id' => $this->user->id, + 'type' => 'FORM', + ]); + $this->assertSame(1, Clipboard::where('user_id', $this->user->id)->count()); + } + + public function test_get_by_user_returns_existing_clipboard_without_creating_duplicate(): void + { + Clipboard::where('user_id', $this->user->id)->delete(); + + $clipboard = Clipboard::factory()->create([ + 'user_id' => $this->user->id, + 'config' => [ + [ + 'component' => 'FormInput', + 'config' => ['label' => 'Existing Clipboard Item'], + ], + ], + 'type' => 'FORM', + ]); + + $response = $this->apiCall('GET', '/api/1.1/clipboard/get_by_user'); + + $response->assertStatus(200) + ->assertJson([ + 'id' => $clipboard->id, + 'user_id' => $this->user->id, + 'type' => 'FORM', + 'config' => $clipboard->config, + ]); + + $this->assertSame(1, Clipboard::where('user_id', $this->user->id)->count()); + } +}