Skip to content

Commit

Permalink
Merge pull request #640 from pinkary-project/feat/improve-user-purge
Browse files Browse the repository at this point in the history
Feat: improve user purge
  • Loading branch information
nunomaduro authored Sep 20, 2024
2 parents fbe855d + 2c67a73 commit 9a24fe7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 3 additions & 1 deletion app/Observers/QuestionObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
38 changes: 38 additions & 0 deletions tests/Unit/Models/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use App\Models\Question;
use App\Models\User;

test('to array', function () {
Expand Down Expand Up @@ -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);
});
16 changes: 14 additions & 2 deletions tests/Unit/Observers/QuestionObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Expand All @@ -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);
Expand All @@ -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 () {
Expand All @@ -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',
]);
Expand All @@ -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);
Expand All @@ -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 () {
Expand Down

0 comments on commit 9a24fe7

Please sign in to comment.