Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/Jobs/SendNewPluginNotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Jobs;

use App\Models\Plugin;
use App\Models\User;
use App\Notifications\NewPluginAvailable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Notification;

class SendNewPluginNotifications implements ShouldQueue
{
use Queueable;

public function __construct(public Plugin $plugin) {}

public function handle(): void
{
$recipients = User::query()
->where('receives_new_plugin_notifications', true)
->where('id', '!=', $this->plugin->user_id)
->get();

Notification::send($recipients, new NewPluginAvailable($this->plugin));
}
}
10 changes: 2 additions & 8 deletions app/Models/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Enums\PluginTier;
use App\Enums\PluginType;
use App\Enums\PriceTier;
use App\Notifications\NewPluginAvailable;
use App\Jobs\SendNewPluginNotifications;
use App\Notifications\PluginApproved;
use App\Notifications\PluginRejected;
use App\Services\PluginSyncService;
Expand All @@ -21,7 +21,6 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Notification;

class Plugin extends Model
{
Expand Down Expand Up @@ -568,12 +567,7 @@ public function approve(int $approvedById): void
$this->user->notify(new PluginApproved($this));

if ($isFirstApproval) {
$recipients = User::query()
->where('receives_new_plugin_notifications', true)
->where('id', '!=', $this->user_id)
->get();

Notification::send($recipients, new NewPluginAvailable($this));
SendNewPluginNotifications::dispatch($this);
}

resolve(PluginSyncService::class)->sync($this);
Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/Jobs/SendNewPluginNotificationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Feature\Jobs;

use App\Jobs\SendNewPluginNotifications;
use App\Models\Plugin;
use App\Models\User;
use App\Notifications\NewPluginAvailable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;

class SendNewPluginNotificationsTest extends TestCase
{
use RefreshDatabase;

public function test_job_sends_notification_to_opted_in_users(): void
{
Notification::fake();

$author = User::factory()->create();
$optedIn = User::factory()->create(['receives_new_plugin_notifications' => true]);
$optedOut = User::factory()->create(['receives_new_plugin_notifications' => false]);

$plugin = Plugin::factory()->approved()->for($author)->create();

(new SendNewPluginNotifications($plugin))->handle();

Notification::assertSentTo($optedIn, NewPluginAvailable::class);
Notification::assertNotSentTo($optedOut, NewPluginAvailable::class);
}

public function test_job_does_not_notify_plugin_author(): void
{
Notification::fake();

$author = User::factory()->create(['receives_new_plugin_notifications' => true]);
$plugin = Plugin::factory()->approved()->for($author)->create();

(new SendNewPluginNotifications($plugin))->handle();

Notification::assertNotSentTo($author, NewPluginAvailable::class);
}
}
24 changes: 10 additions & 14 deletions tests/Feature/Notifications/NewPluginAvailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Tests\Feature\Notifications;

use App\Jobs\SendNewPluginNotifications;
use App\Models\Plugin;
use App\Models\User;
use App\Notifications\NewPluginAvailable;
use App\Services\PluginSyncService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Bus;
use Tests\TestCase;

class NewPluginAvailableTest extends TestCase
Expand All @@ -23,39 +24,34 @@ protected function setUp(): void
});
}

public function test_notification_is_sent_to_opted_in_users_on_first_approval(): void
public function test_notification_job_is_dispatched_on_first_approval(): void
{
Notification::fake();
Bus::fake(SendNewPluginNotifications::class);

$author = User::factory()->create();
$optedIn = User::factory()->create(['receives_new_plugin_notifications' => true]);
$optedOut = User::factory()->create(['receives_new_plugin_notifications' => false]);

$plugin = Plugin::factory()->pending()->for($author)->create();
$admin = User::factory()->create();

$plugin->approve($admin->id);

Notification::assertSentTo($optedIn, NewPluginAvailable::class);
Notification::assertNotSentTo($optedOut, NewPluginAvailable::class);
Notification::assertNotSentTo($author, NewPluginAvailable::class);
Bus::assertDispatched(SendNewPluginNotifications::class, function ($job) use ($plugin) {
return $job->plugin->id === $plugin->id;
});
}

public function test_notification_is_not_sent_on_re_approval(): void
public function test_notification_job_is_not_dispatched_on_re_approval(): void
{
Notification::fake();
Bus::fake(SendNewPluginNotifications::class);

$author = User::factory()->create();
$optedIn = User::factory()->create(['receives_new_plugin_notifications' => true]);

$plugin = Plugin::factory()->pending()->for($author)->create([
'approved_at' => now()->subDay(),
]);
$admin = User::factory()->create();

$plugin->approve($admin->id);

Notification::assertNotSentTo($optedIn, NewPluginAvailable::class);
Bus::assertNotDispatched(SendNewPluginNotifications::class);
}

public function test_via_returns_empty_array_when_user_opted_out(): void
Expand Down
Loading