diff --git a/app/Http/Controllers/PublicWikiController.php b/app/Http/Controllers/PublicWikiController.php new file mode 100644 index 000000000..1f13861c0 --- /dev/null +++ b/app/Http/Controllers/PublicWikiController.php @@ -0,0 +1,55 @@ +query('per_page', null); + if ($perPage !== null) { + $perPage = intval($perPage); + } + return new PublicWikiCollection(Wiki::query()->paginate($perPage)); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request): \Illuminate\Http\JsonResponse + { + return response()->json(['message' => 'Method not allowed'], 405); + } + + /** + * Display the specified resource. + */ + public function show($id) + { + return new PublicWikiResource(Wiki::findOrFail($id)); + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, $id): \Illuminate\Http\JsonResponse + { + return response()->json(['message' => 'Method not allowed'], 405); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy($id): \Illuminate\Http\JsonResponse + { + return response()->json(['message' => 'Method not allowed'], 405); + } +} diff --git a/app/Http/Resources/PublicWikiCollection.php b/app/Http/Resources/PublicWikiCollection.php new file mode 100644 index 000000000..1418ceb4e --- /dev/null +++ b/app/Http/Resources/PublicWikiCollection.php @@ -0,0 +1,19 @@ + $this->id, + 'description' => $this->description, + 'domain' => $this->domain, + 'sitename' => $this->sitename, + 'wiki_site_stats' => $this->wikiSiteStats, + ]; + } +} diff --git a/app/Wiki.php b/app/Wiki.php index a323c10d7..5cf4189bc 100644 --- a/app/Wiki.php +++ b/app/Wiki.php @@ -57,6 +57,8 @@ class Wiki extends Model 'deleted_at', ]; + protected $perPage = 20; + public function wikiDbVersion() { /** diff --git a/database/factories/WikSiteStatsFactory.php b/database/factories/WikSiteStatsFactory.php new file mode 100644 index 000000000..9e07a8d63 --- /dev/null +++ b/database/factories/WikSiteStatsFactory.php @@ -0,0 +1,26 @@ +post('user/resetPassword', ['uses' => 'Auth\ResetPasswordController@reset']); $router->post('contact/sendMessage', ['uses' => 'ContactController@sendMessage']); + $router->apiResource('wiki', 'PublicWikiController'); + // Authed $router->group(['middleware' => ['auth:api']], function () use ($router) { diff --git a/tests/Routes/Wiki/PublicWikiTest.php b/tests/Routes/Wiki/PublicWikiTest.php new file mode 100644 index 000000000..4d8f79708 --- /dev/null +++ b/tests/Routes/Wiki/PublicWikiTest.php @@ -0,0 +1,118 @@ +delete(); + WikiSiteStats::query()->delete(); + } + + public function tearDown(): void { + Wiki::query()->delete(); + WikiSiteStats::query()->delete(); + parent::tearDown(); + } + + public function testEmpty() + { + $this->json('GET', $this->route.'/12') + ->assertStatus(404) + ->assertJsonStructure(['message']); + + $this->json('GET', $this->route) + ->assertStatus(200) + ->assertJsonPath('data', []); + } + + public function testBadMethods() + { + $wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']); + WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]); + + $this->json('DELETE', $this->route.'/'.$wiki->id) + ->assertStatus(405) + ->assertJsonStructure(['message']); + + $this->assertEquals(Wiki::where('id', $wiki->id)->count(), 1); + + $this->json('PATCH', $this->route.'/'.$wiki->id, ['domain' => 'foo.wikibase.cloud']) + ->assertStatus(405) + ->assertJsonStructure(['message']); + + $this->assertEquals(Wiki::where('id', $wiki->id)->first()->getAttribute('domain'), 'one.wikibase.cloud'); + + $this->json('POST', $this->route, ['domain' => 'create.wikibase.cloud']) + ->assertStatus(405) + ->assertJsonStructure(['message']); + } + + public function testGetOne() + { + $wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']); + WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]); + + Wiki::factory()->create(['domain' => 'two.wikibase.cloud']); + WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 66]); + + $this->json('GET', $this->route.'/'.$wiki->id) + ->assertStatus(200) + ->assertJsonPath('data.domain', 'one.wikibase.cloud') + ->assertJsonPath('data.wiki_site_stats.pages', 77); + } + + public function testGetAll() + { + $wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']); + WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]); + + $wiki = Wiki::factory()->create(['domain' => 'two.wikibase.cloud']); + WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 66]); + + $this->json('GET', $this->route) + ->assertStatus(200) + ->assertJsonPath('data.0.domain', 'one.wikibase.cloud') + ->assertJsonPath('data.0.wiki_site_stats.pages', 77) + ->assertJsonPath('data.1.domain', 'two.wikibase.cloud') + ->assertJsonPath('data.1.wiki_site_stats.pages', 66); + } + + public function testPagination() + { + $wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']); + WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]); + + $wiki = Wiki::factory()->create(['domain' => 'two.wikibase.cloud']); + WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 66]); + + $wiki = Wiki::factory()->create(['domain' => 'three.wikibase.cloud']); + WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 55]); + + $this->json('GET', $this->route.'?per_page=1') + ->assertStatus(200) + ->assertJsonPath('data.0.domain', 'one.wikibase.cloud') + ->assertJsonPath('data.0.wiki_site_stats.pages', 77) + ->assertJsonCount(1, 'data') + ->assertJsonPath('meta.total', 3); + + $this->json('GET', $this->route.'?per_page=1&page=3') + ->assertStatus(200) + ->assertJsonPath('data.0.domain', 'three.wikibase.cloud') + ->assertJsonPath('data.0.wiki_site_stats.pages', 55) + ->assertJsonCount(1, 'data') + ->assertJsonPath('meta.total', 3); + } +}