Files
proton/tests/NoteCreatedNotificationTest.php
T

50 lines
2.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
/**
* Пример «событие -> уведомление»: config/events.php подписывает
* NoteCreatedNotification на note.created, см. app/Notifications/NoteCreatedNotification.php.
*/
use App\Models\Note;
use Rsgrinko\Proton\Access\Viewer;
use Rsgrinko\Proton\Events\Events;
use Rsgrinko\Proton\Models\Role;
use Rsgrinko\Proton\Models\User;
use Rsgrinko\Proton\Models\UserNotification;
test('note.created заводит уведомление тем, кто видит чужие данные', function (): void {
$admin = User::register('note_notify_admin_' . bin2hex(random_bytes(3)), 'секрет123', [
'email' => 'note_notify' . bin2hex(random_bytes(3)) . '@example.com',
'role_id' => Role::admin()?->id() ?? 0,
]);
assertTrue(Viewer::fromUser($admin)->can(\Rsgrinko\Proton\Access\Permission::DATA_ALL), 'у встроенного администратора право есть само собой');
$before = UserNotification::unreadFor($admin->id());
$note = Note::create(['title' => 'Заметка для примера уведомления']);
try {
// note.created объявляет контроллер после успешного сохранения
// (NotesController::store()), а не сама модель — зовём событие так же,
// как это делает реальный запрос POST /notes
Events::fire('note.created', ['note' => $note]);
assertSame($before + 1, UserNotification::unreadFor($admin->id()));
$notification = UserNotification::query()
->where('user_id', $admin->id())
->orderBy('id', 'desc')
->first();
assertNotNull($notification);
assertContains((string) $note->title, (string) $notification->raw('title'));
} finally {
$note->forceDelete();
UserNotification::query()->where('user_id', $admin->id())->forceDelete();
$admin->forceDelete();
}
});