mirror of
https://github.com/flarum/core.git
synced 2025-07-25 18:51:40 +02:00
Add Notification extender beforeSending method (#2533)
This commit is contained in:
@@ -14,10 +14,14 @@ use Flarum\Notification\Blueprint\BlueprintInterface;
|
||||
use Flarum\Notification\Driver\NotificationDriverInterface;
|
||||
use Flarum\Notification\Notification;
|
||||
use Flarum\Notification\NotificationSyncer;
|
||||
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||
use Flarum\Tests\integration\TestCase;
|
||||
use Flarum\User\User;
|
||||
|
||||
class NotificationTest extends TestCase
|
||||
{
|
||||
use RetrievesAuthorizedUsers;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
@@ -119,23 +123,61 @@ class NotificationTest extends TestCase
|
||||
$this->assertContains('secondCustomDriver', $blueprints[SecondCustomNotificationType::class]);
|
||||
$this->assertEmpty($blueprints[ThirdCustomNotificationType::class]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function notification_before_sending_callback_works_if_added()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Notification)
|
||||
->type(CustomNotificationType::class, 'customNotificationTypeSerializer')
|
||||
->driver('customNotificationDriver', CustomNotificationDriver::class)
|
||||
->beforeSending(function ($blueprint, $users) {
|
||||
if ($blueprint instanceof CustomNotificationType) {
|
||||
unset($users[1]);
|
||||
}
|
||||
|
||||
return $users;
|
||||
})
|
||||
);
|
||||
|
||||
$this->prepareDatabase([
|
||||
'users' => [
|
||||
$this->adminUser(),
|
||||
$this->normalUser(),
|
||||
['id' => 3, 'username' => 'hani']
|
||||
],
|
||||
]);
|
||||
|
||||
$this->app();
|
||||
|
||||
$users = User::whereIn('id', [1, 2, 3])->get()->all();
|
||||
|
||||
$notificationSyncer = $this->app()->getContainer()->make(NotificationSyncer::class);
|
||||
$notificationSyncer->sync(new CustomNotificationType(), $users);
|
||||
|
||||
$this->assertEquals('potato', $users[0]->username);
|
||||
$this->assertEquals('normal', $users[1]->username);
|
||||
$this->assertEquals('potato', $users[2]->username);
|
||||
}
|
||||
}
|
||||
|
||||
class CustomNotificationType implements BlueprintInterface
|
||||
{
|
||||
public function getFromUser()
|
||||
{
|
||||
// ...
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getSubject()
|
||||
{
|
||||
// ...
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getData()
|
||||
{
|
||||
// ...
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function getType()
|
||||
@@ -169,7 +211,9 @@ class CustomNotificationDriver implements NotificationDriverInterface
|
||||
{
|
||||
public function send(BlueprintInterface $blueprint, array $users): void
|
||||
{
|
||||
// ...
|
||||
foreach ($users as $user) {
|
||||
$user->username = 'potato';
|
||||
}
|
||||
}
|
||||
|
||||
public function registerType(string $blueprintClass, array $driversEnabledByDefault): void
|
||||
|
Reference in New Issue
Block a user