Skip to content

Commit

Permalink
name routes
Browse files Browse the repository at this point in the history
  • Loading branch information
AmonDeShir committed Aug 19, 2024
1 parent f7c26f8 commit cae7f91
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 118 deletions.
2 changes: 1 addition & 1 deletion app/Http/Middleware/EnsureQuizIsNotAlreadyStarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function handle(Request $request, Closure $next): Response|RedirectRespon
$submission = QuizSubmission::query()->where(["quiz_id" => $quiz->id, "user_id" => $user->id])->first();

if ($submission) {
return redirect("/submissions/{$submission->id}/");
return redirect(route("submissions.show", $submission->id));
}

return $next($request);
Expand Down
44 changes: 22 additions & 22 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@
Route::get("/", fn(): Response => inertia("Home"));

Route::group(["prefix" => "admin"], function (): void {
Route::get("/quizzes", [QuizController::class, "index"]);
Route::post("/quizzes", [QuizController::class, "store"]);
Route::get("/quizzes/{quiz}", [QuizController::class, "show"]);
Route::patch("/quizzes/{quiz}", [QuizController::class, "update"])->can("update,quiz");
Route::delete("/quizzes/{quiz}", [QuizController::class, "destroy"])->can("delete,quiz");
Route::post("/quizzes/{quiz}/clone/", [QuizController::class, "clone"]);
Route::get("/quizzes", [QuizController::class, "index"])->name("admin.quizzes.index");
Route::post("/quizzes", [QuizController::class, "store"])->name("admin.quizzes.store");
Route::get("/quizzes/{quiz}", [QuizController::class, "show"])->name("admin.quizzes.show");
Route::patch("/quizzes/{quiz}", [QuizController::class, "update"])->can("update,quiz")->name("admin.quizzes.update");
Route::delete("/quizzes/{quiz}", [QuizController::class, "destroy"])->can("delete,quiz")->name("admin.quizzes.destroy");
Route::post("/quizzes/{quiz}/clone/", [QuizController::class, "clone"])->name("admin.quizzes.clone");

Route::get("/quizzes/{quiz}/questions", [QuizQuestionController::class, "index"]);
Route::post("/quizzes/{quiz}/questions", [QuizQuestionController::class, "store"])->can("create," . Question::class . ",quiz");
Route::get("/questions/{question}", [QuizQuestionController::class, "show"]);
Route::patch("/questions/{question}", [QuizQuestionController::class, "update"])->can("update,question");
Route::delete("/questions/{question}", [QuizQuestionController::class, "destroy"])->can("delete,question");
Route::post("/questions/{question}/clone/{quiz}", [QuizQuestionController::class, "clone"])->can("clone,question,quiz");
Route::get("/quizzes/{quiz}/questions", [QuizQuestionController::class, "index"])->name("admin.questions.index");
Route::post("/quizzes/{quiz}/questions", [QuizQuestionController::class, "store"])->can("create," . Question::class . ",quiz")->name("admin.questions.store");
Route::get("/questions/{question}", [QuizQuestionController::class, "show"])->name("admin.questions.show");
Route::patch("/questions/{question}", [QuizQuestionController::class, "update"])->can("update,question")->name("admin.questions.update");
Route::delete("/questions/{question}", [QuizQuestionController::class, "destroy"])->can("delete,question")->name("admin.questions.destroy");
Route::post("/questions/{question}/clone/{quiz}", [QuizQuestionController::class, "clone"])->can("clone,question,quiz")->name("admin.questions.clone");

Route::get("/questions/{question}/answers", [QuestionAnswerController::class, "index"]);
Route::post("/questions/{question}/answers", [QuestionAnswerController::class, "store"])->can("create," . Answer::class . ",question");
Route::get("/answers/{answer}", [QuestionAnswerController::class, "show"]);
Route::patch("/answers/{answer}", [QuestionAnswerController::class, "update"])->can("update,answer");
Route::delete("/answers/{answer}", [QuestionAnswerController::class, "destroy"])->can("delete,answer");
Route::post("/answers/{answer}/clone/{question}", [QuestionAnswerController::class, "clone"])->can("clone,answer,question");
Route::post("/answers/{answer}/correct", [QuestionAnswerController::class, "markAsCorrect"])->can("update,answer");
Route::post("/answers/{answer}/invalid", [QuestionAnswerController::class, "markAsInvalid"])->can("update,answer");
Route::get("/questions/{question}/answers", [QuestionAnswerController::class, "index"])->name("admin.answers.index");
Route::post("/questions/{question}/answers", [QuestionAnswerController::class, "store"])->can("create," . Answer::class . ",question")->name("admin.answers.store");
Route::get("/answers/{answer}", [QuestionAnswerController::class, "show"])->name("admin.answers.show");
Route::patch("/answers/{answer}", [QuestionAnswerController::class, "update"])->can("update,answer")->name("admin.answers.update");
Route::delete("/answers/{answer}", [QuestionAnswerController::class, "destroy"])->can("delete,answer")->name("admin.answers.destroy");
Route::post("/answers/{answer}/clone/{question}", [QuestionAnswerController::class, "clone"])->can("clone,answer,question")->name("admin.answers.clone");
Route::post("/answers/{answer}/correct", [QuestionAnswerController::class, "markAsCorrect"])->can("update,answer")->name("admin.answers.correct");
Route::post("/answers/{answer}/invalid", [QuestionAnswerController::class, "markAsInvalid"])->can("update,answer")->name("admin.answers.invalid");
});

Route::post("/quizzes/{quiz}/start", [QuizController::class, "createSubmission"])->middleware(EnsureQuizIsNotAlreadyStarted::class)->can("submit,quiz");
Route::get("/submissions/{quizSubmission}/", [QuizSubmissionController::class, "show"])->can("view,quizSubmission");
Route::post("/quizzes/{quiz}/start", [QuizController::class, "createSubmission"])->middleware(EnsureQuizIsNotAlreadyStarted::class)->can("submit,quiz")->name("quizzes.start");
Route::get("/submissions/{quizSubmission}/", [QuizSubmissionController::class, "show"])->can("view,quizSubmission")->name("submissions.show");
66 changes: 33 additions & 33 deletions tests/Feature/AnswerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testUserCanViewQuestionAnswers(): void
$this->assertDatabaseCount("answers", 10);

$this->actingAs($this->user)
->get("/admin/questions/{$question->id}/answers")
->get(route("admin.answers.index", $question->id))
->assertInertia(
fn(Assert $page) => $page
->component("Answer/Index")
Expand All @@ -42,7 +42,7 @@ public function testUserCanViewQuestionAnswers(): void

public function testUserCannotViewAnswersOfQuestionThatNotExisted(): void
{
$this->actingAs($this->user)->get("/admin/questions/1/answers")
$this->actingAs($this->user)->get(route("admin.answers.index", 1))
->assertStatus(404);
}

Expand All @@ -53,7 +53,7 @@ public function testUserCanViewSingleAnswer(): void
$this->assertDatabaseCount("answers", 1);

$this->actingAs($this->user)
->get("/admin/answers/{$answer->id}")
->get(route("admin.answers.show", $answer->id))
->assertInertia(
fn(Assert $page) => $page
->component("Answer/Show")
Expand All @@ -68,7 +68,7 @@ public function testUserCanViewLockedAnswer(): void
$this->assertDatabaseCount("answers", 1);

$this->actingAs($this->user)
->get("/admin/answers/{$answer->id}")
->get(route("admin.answers.show", $answer->id))
->assertInertia(
fn(Assert $page) => $page
->component("Answer/Show")
Expand All @@ -79,7 +79,7 @@ public function testUserCanViewLockedAnswer(): void

public function testUserCannotViewAnswerThatNotExisted(): void
{
$this->actingAs($this->user)->get("/admin/answers/1")
$this->actingAs($this->user)->get(route("admin.answers.show", 1))
->assertStatus(404);
}

Expand All @@ -89,7 +89,7 @@ public function testUserCanCreateAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/questions/{$question->id}/answers", ["text" => "Example answer"])
->post(route("admin.answers.store", $question->id), ["text" => "Example answer"])
->assertRedirect("/quizzes");

$this->assertDatabaseHas("answers", [
Expand All @@ -104,15 +104,15 @@ public function testUserCanCreateMultipleAnswers(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/questions/{$question->id}/answers", ["text" => "Example answer 1"])
->post(route("admin.answers.store", $question->id), ["text" => "Example answer 1"])
->assertRedirect("/quizzes");

$this->from("/quizzes")
->post("/admin/questions/{$question->id}/answers", ["text" => "Example answer 2"])
->post(route("admin.answers.store", $question->id), ["text" => "Example answer 2"])
->assertRedirect("/quizzes");

$this->from("/quizzes")
->post("/admin/questions/{$question->id}/answers", ["text" => "Example answer 3"])
->post(route("admin.answers.store", $question->id), ["text" => "Example answer 3"])
->assertRedirect("/quizzes");

$this->assertDatabaseHas("answers", ["text" => "Example answer 1"]);
Expand All @@ -124,7 +124,7 @@ public function testUserCannotCreateAnswerToQuestionThatNotExisted(): void
{
$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/questions/1/answers", ["text" => "Example answer"])
->post(route("admin.answers.store", 1), ["text" => "Example answer"])
->assertStatus(404);

$this->assertDatabaseMissing("answers", [
Expand All @@ -138,7 +138,7 @@ public function testUserCannotCreateAnswerToQuestionThatIsLocked(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/questions/{$question->id}/answers", ["text" => "Example answer 1"])
->post(route("admin.answers.store", $question->id), ["text" => "Example answer 1"])
->assertStatus(403);

$this->assertDatabaseMissing("answers", [
Expand All @@ -152,11 +152,11 @@ public function testUserCannotCreateInvalidAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/questions/{$question->id}/answers", [])
->post(route("admin.answers.store", $question->id), [])
->assertRedirect("/quizzes")->assertSessionHasErrors(["text"]);

$this->from("/quizzes")
->post("/admin/questions/{$question->id}/answers", ["text" => false])
->post(route("admin.answers.store", $question->id), ["text" => false])
->assertRedirect("/quizzes")->assertSessionHasErrors(["text"]);

$this->assertDatabaseCount("answers", 0);
Expand All @@ -168,7 +168,7 @@ public function testUserCanEditAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->patch("/admin/answers/{$answer->id}", ["text" => "New answer"])
->patch(route("admin.answers.update", $answer->id), ["text" => "New answer"])
->assertRedirect("/quizzes");

$this->assertDatabaseHas("answers", ["text" => "New answer"]);
Expand All @@ -178,7 +178,7 @@ public function testUserCannotEditAnswerThatNotExisted(): void
{
$this->actingAs($this->user)
->from("/quizzes")
->patch("/admin/answers/1", ["text" => "New answer"])
->patch(route("admin.answers.update", 1), ["text" => "New answer"])
->assertStatus(404);
}

Expand All @@ -188,11 +188,11 @@ public function testUserCannotMakeInvalidEdit(): void

$this->actingAs($this->user)
->from("/quizzes")
->patch("/admin/answers/{$answer->id}", [])
->patch(route("admin.answers.update", $answer->id), [])
->assertRedirect("/quizzes")->assertSessionHasErrors(["text"]);

$this->from("/quizzes")
->patch("/admin/answers/{$answer->id}", ["text" => true])
->patch(route("admin.answers.update", $answer->id), ["text" => true])
->assertRedirect("/quizzes")->assertSessionHasErrors(["text"]);

$this->assertDatabaseHas("answers", ["text" => "Old answer"]);
Expand All @@ -204,7 +204,7 @@ public function testUserCannotEditLockedAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->patch("/admin/answers/{$answer->id}", ["text" => "New answer"])
->patch(route("admin.answers.update", $answer->id), ["text" => "New answer"])
->assertStatus(403);

$this->assertDatabaseHas("answers", ["text" => "Old answer"]);
Expand All @@ -216,7 +216,7 @@ public function testUserCanDeleteAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->delete("/admin/answers/{$answer->id}")
->delete(route("admin.answers.destroy", $answer->id))
->assertRedirect("/quizzes");

$this->assertDatabaseMissing("answers", ["text" => "answer"]);
Expand All @@ -228,7 +228,7 @@ public function testUserCannotDeleteLockedAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->delete("/admin/answers/{$answer->id}")
->delete(route("admin.answers.destroy", $answer->id))
->assertStatus(403);

$this->assertDatabaseHas("answers", ["text" => "answer"]);
Expand All @@ -238,7 +238,7 @@ public function testUserCannotDeleteAnswerThatNotExisted(): void
{
$this->actingAs($this->user)
->from("/quizzes")
->delete("/admin/answers/1")
->delete(route("admin.answers.destroy", 1))
->assertStatus(404);
}

Expand All @@ -248,7 +248,7 @@ public function testUserCanMarkAnswerAsCorrect(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answer->id}/correct")
->post(route("admin.answers.correct", $answer->id))
->assertRedirect("/quizzes");

$this->assertDatabaseHas("questions", ["correct_answer_id" => $answer->id]);
Expand All @@ -267,7 +267,7 @@ public function testUserCanChangeCorrectAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answerB->id}/correct")
->post(route("admin.answers.correct", $answerB->id))
->assertRedirect("/quizzes");

$this->assertDatabaseHas("questions", ["correct_answer_id" => $answerB->id]);
Expand All @@ -285,7 +285,7 @@ public function testUserCanDeleteCorrectAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->delete("/admin/answers/{$answer->id}")
->delete(route("admin.answers.destroy", $answer->id))
->assertRedirect("/quizzes");

$this->assertDatabaseHas("questions", ["correct_answer_id" => null]);
Expand All @@ -304,7 +304,7 @@ public function testUserCannotChangeCorrectAnswerInLockedQuestion(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answerB->id}/correct")
->post(route("admin.answers.correct", $answerA->id))
->assertStatus(403);

$this->assertDatabaseHas("questions", ["correct_answer_id" => $answerA->id]);
Expand All @@ -322,7 +322,7 @@ public function testUserCanChangeCorrectAnswerToInvalid(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answer->id}/invalid")
->post(route("admin.answers.invalid", $answer->id))
->assertRedirect("/quizzes");

$this->assertDatabaseHas("questions", ["correct_answer_id" => $answer->id]);
Expand All @@ -340,7 +340,7 @@ public function testUserCannotChangeCorrectAnswerToInvalidInLockedQuestion(): vo

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answer->id}/invalid")
->post(route("admin.answers.invalid", $answer->id))
->assertStatus(403);

$this->assertDatabaseHas("questions", ["correct_answer_id" => $answer->id]);
Expand All @@ -356,7 +356,7 @@ public function testUserCanCopyAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answer->id}/clone/{$questionB->id}")
->post(route("admin.answers.clone", ["answer" => $answer->id, "question" => $questionB->id]))
->assertRedirect("/quizzes");

$this->assertDatabaseHas("answers", ["question_id" => $questionB->id]);
Expand All @@ -372,7 +372,7 @@ public function testUserCanCopyLockedAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answer->id}/clone/{$questionB->id}")
->post(route("admin.answers.clone", ["answer" => $answer->id, "question" => $questionB->id]))
->assertRedirect("/quizzes");

$this->assertDatabaseHas("answers", ["question_id" => $questionB->id]);
Expand All @@ -388,7 +388,7 @@ public function testUserCannotCopyAnswerToLockedQuestion(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answer->id}/clone/{$questionB->id}")
->post(route("admin.answers.clone", ["answer" => $answer->id, "question" => $questionB->id]))
->assertStatus(403);

$this->assertDatabaseHas("answers", ["question_id" => $questionA->id]);
Expand All @@ -408,7 +408,7 @@ public function testUserCanCopyCorrectAnswer(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answer->id}/clone/{$questionB->id}")
->post(route("admin.answers.clone", ["answer" => $answer->id, "question" => $questionB->id]))
->assertRedirect("/quizzes");

$this->assertDatabaseHas("answers", ["question_id" => $questionB->id]);
Expand All @@ -422,7 +422,7 @@ public function testUserCannotCopyAnswerThatNotExisted(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/2/clone/{$question->id}")
->post(route("admin.answers.clone", ["answer" => 2, "question" => $question->id]))
->assertStatus(404);
}

Expand All @@ -432,7 +432,7 @@ public function testUserCannotCopyAnswerToQuestionThatNotExisted(): void

$this->actingAs($this->user)
->from("/quizzes")
->post("/admin/answers/{$answer->id}/clone/2")
->post(route("admin.answers.clone", ["answer" => $answer->id, "question" => 2]))
->assertStatus(404);
}
}
Loading

0 comments on commit cae7f91

Please sign in to comment.