-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eee6908
commit 13b32a2
Showing
7 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Answer; | ||
use App\Models\AnswerRecord; | ||
use Illuminate\Http\RedirectResponse; | ||
|
||
use function redirect; | ||
|
||
class AnswerRecordController extends Controller | ||
{ | ||
public function answer(AnswerRecord $answerRecord, Answer $answer): RedirectResponse | ||
{ | ||
$answerRecord->answer()->associate($answer)->save(); | ||
|
||
return redirect() | ||
->back() | ||
->with("success", "Answer updated"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Policies; | ||
|
||
use App\Models\Answer; | ||
use App\Models\AnswerRecord; | ||
use App\Models\User; | ||
|
||
class AnswerRecordPolicy | ||
{ | ||
public function answer(User $user, AnswerRecord $answerRecord, Answer $answer): bool | ||
{ | ||
$submission = $answerRecord->quizSubmission; | ||
|
||
return !$submission->isClosed && $submission->user_id === $user->id && $answerRecord->question_id === $answer->question_id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Answer; | ||
use App\Models\User; | ||
use Carbon\Carbon; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
|
||
class AnswerRecordTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected User $user; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->user = User::factory()->create(); | ||
} | ||
|
||
public function testUserCanAnswerQuestion(): void | ||
{ | ||
$answer = Answer::factory()->locked()->create(); | ||
$submission = $answer->question->quiz->createSubmission($this->user); | ||
$record = $submission->answerRecords[0]; | ||
|
||
$this->actingAs($this->user) | ||
->from("/") | ||
->patch("/answers/{$record->id}/{$answer->id}") | ||
->assertRedirect("/"); | ||
|
||
$this->assertDatabaseHas("answer_records", [ | ||
"id" => $record->id, | ||
"answer_id" => $answer->id, | ||
]); | ||
} | ||
|
||
public function testUserCannotAnswerQuestionThatNotExisted(): void | ||
{ | ||
$answer = Answer::factory()->locked()->create(); | ||
|
||
$this->actingAs($this->user) | ||
->patch("/answers/0/{$answer->id}") | ||
->assertStatus(404); | ||
} | ||
|
||
public function testUserCannotAnswerQuestionThatIsNotTheirs(): void | ||
{ | ||
$user = User::factory()->create(); | ||
$answer = Answer::factory()->locked()->create(); | ||
$submission = $answer->question->quiz->createSubmission($user); | ||
$record = $submission->answerRecords[0]; | ||
|
||
$this->actingAs($this->user) | ||
->from("/") | ||
->patch("/answers/{$record->id}/{$answer->id}") | ||
->assertStatus(403); | ||
|
||
$this->assertDatabaseMissing("answer_records", [ | ||
"id" => $record->id, | ||
"answer_id" => $answer->id, | ||
]); | ||
} | ||
|
||
public function testUserCannotAnswerQuestionThatBelongsToClosedSubmission(): void | ||
{ | ||
$answer = Answer::factory()->locked()->create(); | ||
$submission = $answer->question->quiz->createSubmission($this->user); | ||
$record = $submission->answerRecords[0]; | ||
|
||
$submission->closed_at = Carbon::now(); | ||
$submission->save(); | ||
|
||
$this->actingAs($this->user) | ||
->from("/") | ||
->patch("/answers/{$record->id}/{$answer->id}") | ||
->assertStatus(403); | ||
|
||
$this->assertDatabaseMissing("answer_records", [ | ||
"id" => $record->id, | ||
"answer_id" => $answer->id, | ||
]); | ||
} | ||
|
||
public function testUserCannotAnswerQuestionWithAnswerThatNotExist(): void | ||
{ | ||
$answer = Answer::factory()->locked()->create(); | ||
$submission = $answer->question->quiz->createSubmission($this->user); | ||
$record = $submission->answerRecords[0]; | ||
|
||
$this->actingAs($this->user) | ||
->from("/") | ||
->patch("/answers/{$record->id}/4") | ||
->assertStatus(404); | ||
|
||
$this->assertDatabaseMissing("answer_records", [ | ||
"id" => $record->id, | ||
"answer_id" => $answer->id, | ||
]); | ||
} | ||
|
||
public function testUserCannotAnswerQuestionWithAnswerNotAssignedToIt(): void | ||
{ | ||
$answer = Answer::factory()->locked()->create(); | ||
$submission = $answer->question->quiz->createSubmission($this->user); | ||
$record = $submission->answerRecords[0]; | ||
|
||
$answer1 = Answer::factory()->locked()->create(); | ||
|
||
$this->actingAs($this->user) | ||
->from("/") | ||
->patch("/answers/{$record->id}/{$answer1->id}") | ||
->assertStatus(403); | ||
|
||
$this->assertDatabaseMissing("answer_records", [ | ||
"id" => $record->id, | ||
"answer_id" => $answer1->id, | ||
]); | ||
} | ||
} |