diff --git a/app/Http/Controllers/Backend/WikiController.php b/app/Http/Controllers/Backend/WikiController.php index 8fccf778..b8e23761 100644 --- a/app/Http/Controllers/Backend/WikiController.php +++ b/app/Http/Controllers/Backend/WikiController.php @@ -7,28 +7,22 @@ use Illuminate\Http\Request; class WikiController extends Controller { - private static $with = ['wikiDb', 'wikiQueryserviceNamespace', 'settings']; - public function getWikiForDomain(Request $request): \Illuminate\Http\JsonResponse { - $domain = $request->input('domain'); + $validated = $request->validate([ + 'domain' => 'required|string', + ]); - // XXX: this same logic is in quickstatements.php and platform api WikiController backend + $domain = $validated['domain']; try { - if ($domain === 'localhost' || $domain === 'mediawiki') { - // If just using localhost then just get the first undeleted wiki - $result = Wiki::with(self::$with)->first(); - } else { - // TODO don't select the timestamps and redundant info for the settings? - $result = Wiki::where('domain', $domain)->with(self::$with)->first(); - } - } catch (\Exception $ex) { - return response()->json($ex->getMessage(), 500); + $wiki = Wiki::with(['wikiDb', 'wikiQueryserviceNamespace', 'settings'])->firstWhere('domain', $domain); + } catch (\Exception $e) { + return response()->json($e->getMessage(), 500); } - if (!$result) { + if (!$wiki) { return response()->json(['error' => 'Not found'], 404); } - return response()->json(['data' => $result], 200); + return response()->json(['data' => $wiki], 200); } } diff --git a/tests/Routes/Backend/WikiControllerTest.php b/tests/Routes/Backend/WikiControllerTest.php new file mode 100644 index 00000000..9c2a0e43 --- /dev/null +++ b/tests/Routes/Backend/WikiControllerTest.php @@ -0,0 +1,53 @@ +create(['domain' => $domain]); + WikiDb::create([ + 'name' => $domain, + 'user' => 'someUser', + 'password' => 'somePassword', + 'prefix' => 'somePrefix', + 'version' => 'someVersion', + 'wiki_id' => $wiki->id, + ]); + } + + public function testGetWikiDomainSuccess() { + $wikiDomain = 'coffeebase.wikibase.cloud'; + + $this->createWiki($wikiDomain); + + $this->get("{$this->route}?domain={$wikiDomain}") + ->assertStatus(200) + ->assertJsonPath('data.domain', $wikiDomain) + ->assertJsonStructure([ + 'data' => [ + 'domain', + 'sitename', + 'wiki_queryservice_namespace', + ], + ]); + } + + public function testGetWikiDomainMissingWikiDomain() { + $this->getJson("{$this->route}") + ->assertStatus(422); + } + + public function testGetWikiDomainWikiNotFound() { + $this->getJson("{$this->route}?domain=somewiki") + ->assertStatus(404); + } +}