From e8724946f3b74b45f0e7c92aee5f1aa99e28e947 Mon Sep 17 00:00:00 2001 From: toan Date: Thu, 18 Aug 2022 12:10:48 +0200 Subject: [PATCH] Refactor /mine to include wikis, the count and the limit Bug: T315441 --- app/Http/Controllers/WikisController.php | 14 ++++++---- tests/Routes/Wiki/MineTest.php | 34 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/WikisController.php b/app/Http/Controllers/WikisController.php index 081e0aa09..8eb15578d 100644 --- a/app/Http/Controllers/WikisController.php +++ b/app/Http/Controllers/WikisController.php @@ -9,10 +9,14 @@ class WikisController extends Controller { public function getWikisOwnedByCurrentUser(Request $request): \Illuminate\Http\Response { - return response( - $request->user() - ->managesWikis() - ->get() - ); + $wikis = $request->user()->managesWikis()->get(); + + return response( + [ + "wikis" => $wikis, + "count" => count($wikis), + "limit" => config('wbstack.wiki_max_per_user') + ] + ); } } diff --git a/tests/Routes/Wiki/MineTest.php b/tests/Routes/Wiki/MineTest.php index 1438048ec..aa06bd64a 100644 --- a/tests/Routes/Wiki/MineTest.php +++ b/tests/Routes/Wiki/MineTest.php @@ -4,10 +4,44 @@ use Tests\Routes\Traits\OptionsRequestAllowed; use Tests\TestCase; +use App\User; +use Illuminate\Support\Facades\Config; +use App\Wiki; +use App\WikiManager; +use Illuminate\Foundation\Testing\DatabaseTransactions; class MineTest extends TestCase { protected $route = 'wiki/mine'; use OptionsRequestAllowed; + use DatabaseTransactions; + + public function testMineDefault() + { + Config::set('wbstack.wiki_max_per_user', false); + + $user = User::factory()->create(['verified' => true]); + $this->actingAs($user, 'api') + ->json('POST', $this->route, []) + ->assertStatus(200) + ->assertJsonFragment([ "wikis" => [], "count" => 0, "limit" => false]); + } + + public function testMineWithWikis() + { + Config::set('wbstack.wiki_max_per_user', 1); + + $user = User::factory()->create(['verified' => true]); + $wiki = Wiki::factory()->create(); + WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); + + $content = $this->actingAs($user, 'api') + ->json('POST', $this->route, []) + ->assertStatus(200) + ->assertJson([ "wikis" => [], "count" => 1, "limit" => 1])->getContent(); + + + $this->assertEquals( $wiki->id, json_decode($content)->wikis[0]->id ); + } }