diff --git a/app/Models/User.php b/app/Models/User.php index b520939f9..484a0d166 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -249,6 +249,11 @@ public function purge(): void $this->followers()->detach(); $this->following()->detach(); + $this->notifications()->delete(); + + $this->questionsReceived->each->delete(); + $this->questionsSent->each->delete(); + $this->delete(); } diff --git a/app/Observers/QuestionObserver.php b/app/Observers/QuestionObserver.php index 31944effe..3cfe6c524 100644 --- a/app/Observers/QuestionObserver.php +++ b/app/Observers/QuestionObserver.php @@ -80,10 +80,12 @@ public function deleted(Question $question): void $user->notifications()->whereJsonContains('data->question_id', $question->id)->delete(); }); - $question->loadMissing('children'); + $question->loadMissing(['children', 'descendants']); $question->children->each->delete(); + $question->descendants->each->delete(); + $question->hashtags()->detach(); } } diff --git a/tests/Unit/Models/UserTest.php b/tests/Unit/Models/UserTest.php index d78363916..c41705209 100644 --- a/tests/Unit/Models/UserTest.php +++ b/tests/Unit/Models/UserTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use App\Models\Question; use App\Models\User; test('to array', function () { @@ -160,3 +161,40 @@ $this->assertNull($user->fresh()); $this->assertDatabaseCount('links', 0); }); + +test('purge notifications with user', function () { + $user = User::factory()->create(); + Question::factory(2)->create([ + 'to_id' => $user->id, + ]); + $this->assertDatabaseCount('notifications', 2); + + $this->actingAs($user) + ->delete(route('profile.destroy'), [ + 'password' => 'password', + ]); + + $this->assertNull($user->fresh()); + $this->assertDatabaseCount('notifications', 0); +}); + +test('purge questions, comments and decendants with user', function () { + $user = User::factory()->hasQuestionsSent(2)->create(); + Question::factory() + ->hasChildren(2) + ->hasDescendants(2) + ->create([ + 'from_id' => $user->id, + 'to_id' => $user->id, + ]); + + $this->assertDatabaseCount('questions', 7); + + $this->actingAs($user) + ->delete(route('profile.destroy'), [ + 'password' => 'password', + ]); + + $this->assertNull($user->fresh()); + $this->assertDatabaseCount('questions', 0); +}); diff --git a/tests/Unit/Observers/QuestionObserverTest.php b/tests/Unit/Observers/QuestionObserverTest.php index e07abdaed..85b738303 100644 --- a/tests/Unit/Observers/QuestionObserverTest.php +++ b/tests/Unit/Observers/QuestionObserverTest.php @@ -197,7 +197,11 @@ $mentionedUser = User::factory()->create([ 'username' => 'johndoe', ]); - $question = Question::factory()->create(); + $question = Question::factory() + ->has(Question::factory()->sharedUpdate()->count(3)->state([ + 'answer' => 'descendant', + ]), 'descendants') + ->create(); $question->update([ 'answer' => 'My favourite developer is to @johndoe', ]); @@ -208,6 +212,7 @@ ->create(); expect($question->children()->count())->toBe(3); + expect($question->descendants()->count())->toBe(3); expect($question->to->notifications()->count())->toBe(0); expect($question->from->notifications()->count())->toBe(1); expect($mentionedUser->notifications()->count())->toBe(1); @@ -217,6 +222,7 @@ expect($question->from->fresh()->notifications()->count())->toBe(0); expect($mentionedUser->fresh()->notifications()->count())->toBe(0); expect($question->children()->count())->toBe(0); + expect($question->descendants()->count())->toBe(0); }); test('deleted', function () { @@ -242,7 +248,11 @@ 'username' => 'johndoe', ]); - $question = Question::factory()->create(); + $question = Question::factory() + ->has(Question::factory()->sharedUpdate()->count(3)->state([ + 'answer' => 'descendant', + ]), 'descendants') + ->create(); $question->update([ 'answer' => 'My favourite developer is to @johndoe', ]); @@ -257,6 +267,7 @@ expect($question->children()->count())->toBe(3); expect(Question::where('answer', 'grandchild')->count())->toBe(9); + expect(Question::where('answer', 'descendant')->count())->toBe(3); expect($question->to->notifications()->count())->toBe(0); expect($question->from->notifications()->count())->toBe(1); expect($mentionedUser->notifications()->count())->toBe(1); @@ -267,6 +278,7 @@ expect($mentionedUser->fresh()->notifications()->count())->toBe(0); expect($question->children()->count())->toBe(0); expect(Question::where('answer', 'grandchild')->count())->toBe(0); + expect(Question::where('answer', 'descendant')->count())->toBe(0); }); test('hashtags are synced when created', function () {