Skip to content

Commit 331cd61

Browse files
authored
chore: Backend WikiController cleanup (#1013)
separated from #1010 - adds input validation - adds unit test - makes wiki retrieving safer - removes some special dev handling that is to my knowledge not needed anymore - IMO the routing in backend.php here also would need a cleanup, but that would break client use, so.. meh.
1 parent 99d2200 commit 331cd61

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

app/Http/Controllers/Backend/WikiController.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,22 @@
77
use Illuminate\Http\Request;
88

99
class WikiController extends Controller {
10-
private static $with = ['wikiDb', 'wikiQueryserviceNamespace', 'settings'];
11-
1210
public function getWikiForDomain(Request $request): \Illuminate\Http\JsonResponse {
13-
$domain = $request->input('domain');
11+
$validated = $request->validate([
12+
'domain' => 'required|string',
13+
]);
1414

15-
// XXX: this same logic is in quickstatements.php and platform api WikiController backend
15+
$domain = $validated['domain'];
1616
try {
17-
if ($domain === 'localhost' || $domain === 'mediawiki') {
18-
// If just using localhost then just get the first undeleted wiki
19-
$result = Wiki::with(self::$with)->first();
20-
} else {
21-
// TODO don't select the timestamps and redundant info for the settings?
22-
$result = Wiki::where('domain', $domain)->with(self::$with)->first();
23-
}
24-
} catch (\Exception $ex) {
25-
return response()->json($ex->getMessage(), 500);
17+
$wiki = Wiki::with(['wikiDb', 'wikiQueryserviceNamespace', 'settings'])->firstWhere('domain', $domain);
18+
} catch (\Exception $e) {
19+
return response()->json($e->getMessage(), 500);
2620
}
2721

28-
if (!$result) {
22+
if (!$wiki) {
2923
return response()->json(['error' => 'Not found'], 404);
3024
}
3125

32-
return response()->json(['data' => $result], 200);
26+
return response()->json(['data' => $wiki], 200);
3327
}
3428
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Tests\Routes\Backend;
4+
5+
use App\Wiki;
6+
use App\WikiDb;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Tests\TestCase;
9+
10+
class WikiControllerTest extends TestCase {
11+
use RefreshDatabase;
12+
13+
protected $route = '/backend/wiki/getWikiForDomain';
14+
15+
private function createWiki(string $domain) {
16+
$wiki = Wiki::factory()->create(['domain' => $domain]);
17+
WikiDb::create([
18+
'name' => $domain,
19+
'user' => 'someUser',
20+
'password' => 'somePassword',
21+
'prefix' => 'somePrefix',
22+
'version' => 'someVersion',
23+
'wiki_id' => $wiki->id,
24+
]);
25+
}
26+
27+
public function testGetWikiDomainSuccess() {
28+
$wikiDomain = 'coffeebase.wikibase.cloud';
29+
30+
$this->createWiki($wikiDomain);
31+
32+
$this->get("{$this->route}?domain={$wikiDomain}")
33+
->assertStatus(200)
34+
->assertJsonPath('data.domain', $wikiDomain)
35+
->assertJsonStructure([
36+
'data' => [
37+
'domain',
38+
'sitename',
39+
'wiki_queryservice_namespace',
40+
],
41+
]);
42+
}
43+
44+
public function testGetWikiDomainMissingWikiDomain() {
45+
$this->getJson("{$this->route}")
46+
->assertStatus(422);
47+
}
48+
49+
public function testGetWikiDomainWikiNotFound() {
50+
$this->getJson("{$this->route}?domain=somewiki")
51+
->assertStatus(404);
52+
}
53+
}

0 commit comments

Comments
 (0)