Skip to content

Commit 34f63a4

Browse files
authored
Create KER Object when a Wiki is made (#1069)
This creates a KnowledgeEquityResponse Object if one is provided at wiki creation time. It adds some validation for the content revieved over the network. No sanitisation has been applied to the freeTextResponse. This needs to be taken into account if we reuse this somewhere Bug: T419208
1 parent 5fe7723 commit 34f63a4

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

app/Http/Controllers/WikiController.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Jobs\MediawikiInit;
1111
use App\Jobs\ProvisionQueryserviceNamespaceJob;
1212
use App\Jobs\ProvisionWikiDbJob;
13+
use App\KnowledgeEquityResponse;
1314
use App\QueryserviceNamespace;
1415
use App\Wiki;
1516
use App\WikiDb;
@@ -21,6 +22,7 @@
2122
use Illuminate\Support\Facades\Config;
2223
use Illuminate\Support\Facades\DB;
2324
use Illuminate\Support\Str;
25+
use Illuminate\Validation\Rule;
2426

2527
class WikiController extends Controller {
2628
private $domainValidator;
@@ -55,6 +57,14 @@ public function create(Request $request): \Illuminate\Http\Response {
5557
'sitename' => 'required|min:3',
5658
'username' => 'required',
5759
'profile' => 'nullable|json',
60+
'knowledgeEquityResponse.selectedOption' => [
61+
'nullable',
62+
Rule::in('yes', 'no', 'unsure', 'unsaid'),
63+
],
64+
'knowledgeEquityResponse.freeTextResponse' => [
65+
'nullable',
66+
'max:3000',
67+
],
5868
]);
5969

6070
$rawProfile = false;
@@ -64,11 +74,13 @@ public function create(Request $request): \Illuminate\Http\Response {
6474
$profileValidator->validateWithBag('post');
6575
}
6676

77+
$rawKnowledgeEquityResponse = $request->input('knowledgeEquityResponse');
78+
6779
$wiki = null;
6880
$dbAssignment = null;
6981

7082
// TODO create with some sort of owner etc?
71-
DB::transaction(function () use ($user, $request, &$wiki, &$dbAssignment, $submittedDomain, $rawProfile) {
83+
DB::transaction(function () use ($user, $request, &$wiki, &$dbAssignment, $submittedDomain, $rawProfile, $rawKnowledgeEquityResponse) {
7284
$dbVersion = Config::get('wbstack.wiki_db_use_version');
7385
$wikiDbCondition = ['wiki_id' => null, 'version' => $dbVersion];
7486

@@ -158,6 +170,10 @@ public function create(Request $request): \Illuminate\Http\Response {
158170
if ($rawProfile) {
159171
WikiProfile::create(['wiki_id' => $wiki->id, ...$rawProfile]);
160172
}
173+
174+
if ($rawKnowledgeEquityResponse) {
175+
KnowledgeEquityResponse::create(['wiki_id' => $wiki->id, ...$rawKnowledgeEquityResponse]);
176+
}
161177
});
162178

163179
// TODO maybe always make these run in a certain order..?

tests/Routes/Wiki/CreateTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Jobs\ElasticSearchAliasInit;
66
use App\Jobs\MediawikiInit;
77
use App\Jobs\ProvisionWikiDbJob;
8+
use App\KnowledgeEquityResponse;
89
use App\QueryserviceNamespace;
910
use App\User;
1011
use App\Wiki;
@@ -310,4 +311,82 @@ public function testCreateWithProfileCreatesProfiles(): void {
310311
WikiProfile::where(['wiki_id' => $id])->count()
311312
);
312313
}
314+
315+
public function testCreateWithKERCreatesProfiles(): void {
316+
$this->createSQLandQSDBs();
317+
Queue::fake();
318+
$user = User::factory()->create(['verified' => true]);
319+
$response = $this->actingAs($user, 'api')
320+
->json(
321+
'POST',
322+
$this->route,
323+
[...self::defaultData, 'knowledgeEquityResponse' => [
324+
// This only tests the selectedOption since in the future this will become required while
325+
// the freeTextResponse will not.
326+
'selectedOption' => 'yes',
327+
]]
328+
);
329+
$id = $response->decodeResponseJson()['data']['id'];
330+
$this->assertEquals(1,
331+
KnowledgeEquityResponse::where(['wiki_id' => $id])->count()
332+
);
333+
}
334+
335+
public function testCreateWithKERRejectsIfSelectedOptionIsInvalid(): void {
336+
$this->createSQLandQSDBs();
337+
Queue::fake();
338+
$user = User::factory()->create(['verified' => true]);
339+
$response = $this->actingAs($user, 'api')
340+
->json(
341+
'POST',
342+
$this->route,
343+
[...self::defaultData, 'knowledgeEquityResponse' => [
344+
'selectedOption' => 'yeeeeeah',
345+
]]
346+
);
347+
$response->assertStatus(422);
348+
// This only tests for the existance of an error key.
349+
// Using JSON path to check the specific errors message
350+
// binds very tightly to the laravel implemention of validation and was hard to make work.
351+
$response->assertJsonStructure(['errors']);
352+
}
353+
354+
public function testCreateWithKERCreatesIf3000FreeTextResponse(): void {
355+
$this->createSQLandQSDBs();
356+
Queue::fake();
357+
$user = User::factory()->create(['verified' => true]);
358+
$response = $this->actingAs($user, 'api')
359+
->json(
360+
'POST',
361+
$this->route,
362+
[...self::defaultData, 'knowledgeEquityResponse' => [
363+
'selectedOption' => 'yes',
364+
'freeTextResponse' => str_repeat('a', 3000),
365+
]]
366+
);
367+
$id = $response->decodeResponseJson()['data']['id'];
368+
$this->assertEquals(1,
369+
KnowledgeEquityResponse::where(['wiki_id' => $id])->count()
370+
);
371+
}
372+
373+
public function testCreateWithKERErrorsIf3001FreeTextResponse(): void {
374+
$this->createSQLandQSDBs();
375+
Queue::fake();
376+
$user = User::factory()->create(['verified' => true]);
377+
$response = $this->actingAs($user, 'api')
378+
->json(
379+
'POST',
380+
$this->route,
381+
[...self::defaultData, 'knowledgeEquityResponse' => [
382+
'selectedOption' => 'yes',
383+
'freeTextResponse' => str_repeat('a', 3001),
384+
]]
385+
);
386+
$response->assertStatus(422);
387+
// This only tests for the existance of an error key.
388+
// Using JSON path to check the specific errors message
389+
// binds very tightly to the laravel implemention of validation and was hard to make work.
390+
$response->assertJsonStructure(['errors']);
391+
}
313392
}

0 commit comments

Comments
 (0)