Skip to content

Commit

Permalink
add quiz cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
AmonDeShir committed Aug 12, 2024
1 parent d189c2d commit 34551e0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/Http/Controllers/QuizController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ public function destroy(Quiz $quiz): RedirectResponse
->back()
->with("success", "Quiz deleted");
}

/**
* @throws AuthorizationException
*/
public function clone(Quiz $quiz): RedirectResponse
{
$quiz->clone();

return redirect()
->back()
->with("success", "Quiz cloned");
}
}
16 changes: 16 additions & 0 deletions app/Models/Quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Models;

use Carbon\Carbon;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -47,6 +48,21 @@ public function isLocked(): Attribute
return Attribute::get(fn(): bool => $this->locked_at !== null);
}

/**
* @throws AuthorizationException
*/
public function clone(): self
{
$quizCopy = $this->replicate();
$quizCopy->save();

foreach ($this->questions as $question) {
$question->cloneTo($quizCopy);
}

return $quizCopy;
}

protected function casts(): array
{
return [
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Route::get("/", fn(): Response => inertia("Welcome"));

Route::post("/quizzes/{quiz}/lock", [QuizController::class, "lock"]);
Route::post("/quizzes/{quiz}/clone/", [QuizController::class, "clone"]);
Route::post("/questions/{question}/clone/{quiz}", [QuizQuestionController::class, "clone"]);
Route::post("/answers/{answer}/correct", [QuestionAnswerController::class, "markAsCorrect"]);
Route::post("/answers/{answer}/invalid", [QuestionAnswerController::class, "markAsInvalid"]);
Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/QuizTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,52 @@ public function testUserCannotDeleteQuestionThatNotExisted(): void
->delete("/quizzes/1")
->assertStatus(404);
}

public function testUserCanCopyQuiz(): void
{
$user = User::factory()->create();
$quiz = Quiz::factory()->create();
$questions = Question::factory()->count(2)->create(["quiz_id" => $quiz->id]);

Answer::factory()->count(10)->create(["question_id" => $questions[0]->id]);
Answer::factory()->count(10)->create(["question_id" => $questions[1]->id]);

$this->assertDatabaseCount("quizzes", 1);
$this->assertDatabaseCount("questions", 2);
$this->assertDatabaseCount("answers", 20);

$this->actingAs($user)
->from("/")
->post("/quizzes/{$quiz->id}/clone")
->assertRedirect("/");

$this->assertDatabaseCount("quizzes", 2);
$this->assertDatabaseCount("questions", 4);
$this->assertDatabaseCount("answers", 40);
}

public function testUserCanCopyLockedQuiz(): void
{
$user = User::factory()->create();
$quiz = Quiz::factory()->locked()->create();

$this->assertDatabaseCount("quizzes", 1);

$this->actingAs($user)
->from("/")
->post("/quizzes/{$quiz->id}/clone")
->assertRedirect("/");

$this->assertDatabaseCount("quizzes", 2);
}

public function testUserCannotCopyQuizThatNotExisted(): void
{
$user = User::factory()->create();

$this->actingAs($user)
->from("/")
->post("/quizzes/2/clone")
->assertStatus(404);
}
}

0 comments on commit 34551e0

Please sign in to comment.