|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Domains\Discussions\Tests\Feature; |
| 4 | + |
| 5 | +use Domains\Accounts\Database\Factories\UserFactory; |
| 6 | +use Domains\Accounts\Enums\AccountTypeEnum; |
| 7 | +use Domains\Accounts\Models\User; |
| 8 | +use Domains\Discussions\Database\Factories\QuestionFactory; |
| 9 | +use Domains\Discussions\Models\Question; |
| 10 | +use Faker\Factory; |
| 11 | +use Faker\Generator; |
| 12 | +use Illuminate\Http\Response; |
| 13 | +use Illuminate\Support\Carbon; |
| 14 | +use Laravel\Lumen\Testing\DatabaseMigrations; |
| 15 | +use Tests\TestCase; |
| 16 | + |
| 17 | +class QuestionsDeleteTest extends TestCase |
| 18 | +{ |
| 19 | + use DatabaseMigrations; |
| 20 | + |
| 21 | + private Generator $faker; |
| 22 | + private User $user; |
| 23 | + private Question $question; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $this->faker = Factory::create(); |
| 30 | + $this->user = UserFactory::new()->create(); |
| 31 | + $this->question = QuestionFactory::new(['author_id' => $this->user->id])->create(); |
| 32 | + |
| 33 | + Carbon::setTestNow(); |
| 34 | + } |
| 35 | + |
| 36 | + /** @test */ |
| 37 | + public function it_soft_deletes_a_question_i_own(): void |
| 38 | + { |
| 39 | + $response = $this->actingAs($this->user) |
| 40 | + ->delete(route('discussions.questions.delete', ['questionId' => $this->question->id])); |
| 41 | + |
| 42 | + $this->assertResponseStatus(Response::HTTP_NO_CONTENT); |
| 43 | + self::assertTrue($response->response->isEmpty()); |
| 44 | + $this->seeInDatabase('questions', [ |
| 45 | + 'id' => $this->question->id, |
| 46 | + 'updated_at' => Carbon::now(), |
| 47 | + 'deleted_at' => Carbon::now(), |
| 48 | + ]); |
| 49 | + } |
| 50 | + |
| 51 | + /** @test */ |
| 52 | + public function it_allows_admin_to_soft_delete_another_users_question(): void |
| 53 | + { |
| 54 | + $response = $this->actingAs(UserFactory::new()->withRole(AccountTypeEnum::ADMIN)->make()) |
| 55 | + ->delete(route('discussions.questions.update', ['questionId' => $this->question->id])); |
| 56 | + |
| 57 | + $this->assertResponseStatus(Response::HTTP_NO_CONTENT); |
| 58 | + self::assertTrue($response->response->isEmpty()); |
| 59 | + $this->seeInDatabase('questions', [ |
| 60 | + 'id' => $this->question->id, |
| 61 | + 'updated_at' => Carbon::now(), |
| 62 | + 'deleted_at' => Carbon::now(), |
| 63 | + ]); |
| 64 | + } |
| 65 | + |
| 66 | + /** @test */ |
| 67 | + public function it_forbids_a_non_admin_to_soft_delete_a_question_he_doesnt_own(): void |
| 68 | + { |
| 69 | + $this->actingAs(UserFactory::new()->make()) |
| 70 | + ->delete(route('discussions.questions.update', ['questionId' => $this->question->id])); |
| 71 | + |
| 72 | + $this->assertResponseStatus(Response::HTTP_FORBIDDEN); |
| 73 | + $this->seeInDatabase('questions', [ |
| 74 | + 'id' => $this->question->id, |
| 75 | + 'updated_at' => $this->question->updated_at, |
| 76 | + 'deleted_at' => null, |
| 77 | + ]); |
| 78 | + } |
| 79 | +} |
0 commit comments