Files
Toiminnanohjaus/tests/Feature/Modules/Notifications/NotificationsTest.php
petriandClaude Sonnet 5.5 f906f9a43f Add notifications: header bell, task start run and assignment notices
A new Notifications module shows a bell beside the profile menu with the
number of unseen notifications and a panel of the last 7 days; opening
it marks them seen, and each notification links to its job or task.

- A daily run at 14.00 (notifications:task-starts) tells the assignee
  and members of tasks that start by the next working day, skipping
  holidays and days off, done tasks and archived jobs.
- Jobs publishes PeopleAddedToTask and PeopleAddedToJob when someone
  else makes a user a task's assignee or member or gives them a role on
  a job; those users are notified at once. UpdateTask and ScheduleTask
  now take the acting user.
- A notification link to #task-<id> opens that task card on the job page.

Also: simplifying job fields per service unit (K31) moves to the end of
phase 4, and the workflow template browser test waits for Livewire's
progress bar before its last accessibility check.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 08:28:01 +02:00

166 lines
7.7 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Jobs\Application\Contracts\UpcomingTasks;
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\Notifications\Infrastructure\Persistence\Eloquent\NotificationModel;
use Carbon\CarbonImmutable;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
// Thursday afternoon in Finland: tasks starting on Friday are notified.
$this->travelTo(CarbonImmutable::parse('2026-11-26 14:00', 'Europe/Helsinki'));
$this->user = User::factory()->create();
$this->member = User::factory()->create();
$this->job = CustomerJobModel::factory()->create(['status' => 'order', 'number' => '2026-0042', 'name' => 'Messuvideo']);
});
function notificationFor(User $user, array $overrides = []): NotificationModel
{
static $sequence = 0;
$sequence++;
return NotificationModel::query()->create([
'user_id' => $user->id,
'type' => 'task_starts_soon',
'params' => ['task_id' => 7, 'task_title' => 'Kuvauspäivä', 'job_id' => 3, 'job_number' => '2026-0042', 'job_name' => 'Messuvideo', 'starts_on' => '2026-11-30'],
'dedupe_key' => 'test:'.$sequence,
'created_at' => now(),
...$overrides,
]);
}
it('finds tasks starting in the period with their assignee and members, without done tasks and archived jobs', function () {
$task = TaskModel::factory()->assignedTo([$this->user->id, $this->member->id])->create(['customer_job_id' => $this->job->id, 'title' => 'Kuvauspäivä', 'planned_from' => '2026-11-27', 'planned_to' => '2026-11-27']);
TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => $this->job->id, 'status' => 'done', 'planned_from' => '2026-11-27']);
TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => CustomerJobModel::factory()->create(['status' => 'archived'])->id, 'planned_from' => '2026-11-27']);
TaskModel::factory()->assignedTo([$this->user->id])->create(['customer_job_id' => $this->job->id, 'planned_from' => '2026-11-26']);
$tasks = app(UpcomingTasks::class)->startingBetween('2026-11-26', '2026-11-27');
expect($tasks)->toHaveCount(1)
->and($tasks[0]->taskId)->toBe($task->id)
->and($tasks[0]->jobNumber)->toBe('2026-0042')
->and($tasks[0]->plannedFrom)->toBe('2026-11-27')
->and($tasks[0]->recipientIds)->toBe([$this->user->id, $this->member->id]);
});
it('notifies with the daily command, scheduled at 14.00 Finnish time', function () {
$task = TaskModel::factory()->assignedTo([$this->user->id, $this->member->id])->create(['customer_job_id' => $this->job->id, 'title' => 'Kuvauspäivä', 'planned_from' => '2026-11-27']);
$this->artisan('notifications:task-starts')->expectsOutputToContain('2 notifications added.')->assertSuccessful();
$this->artisan('notifications:task-starts')->expectsOutputToContain('0 notifications added.');
expect(NotificationModel::query()->where('user_id', $this->member->id)->sole()->params['task_id'])->toBe($task->id);
$event = collect(app(Schedule::class)->events())->first(fn (Event $event): bool => str_contains((string) $event->command, 'notifications:task-starts'));
expect($event)->not->toBeNull()
->and($event->expression)->toBe('0 14 * * *')
->and($event->timezone)->toBe('Europe/Helsinki');
});
it('shows the unseen count of the user\'s own notifications of the last 7 days', function () {
notificationFor($this->user);
notificationFor($this->user, ['created_at' => now()->subDays(8)]);
notificationFor($this->member);
$this->actingAs($this->user);
Livewire::test('notifications::bell')
->assertSee('1 lukematon ilmoitus')
->assertSee('Tehtävä Kuvauspäivä (työ 2026-0042 Messuvideo) alkaa ma 30.11.2026.')
->assertSee(route('jobs.show', 3).'#task-7', false)
->assertSee('26.11.2026 klo 14.00');
});
it('marks the notifications seen when the panel is opened, keeping them marked new for now', function () {
$notification = notificationFor($this->user);
$this->actingAs($this->user);
Livewire::test('notifications::bell')
->call('open')
->assertSee('Ei lukemattomia ilmoituksia')
->assertSet('newIds', [$notification->id])
->assertSee('Uusi');
expect($notification->fresh()->seen_at)->not->toBeNull();
Livewire::test('notifications::bell')->assertDontSee('Uusi');
});
it('tells when there are no notifications, and in the user\'s language', function () {
$this->actingAs($this->user);
Livewire::test('notifications::bell')->assertSee('Ei ilmoituksia viimeiseltä 7 päivältä.');
notificationFor($this->member);
$this->member->forceFill(['locale' => 'en'])->save();
$this->actingAs($this->member);
app()->setLocale('en');
Livewire::test('notifications::bell')
->assertSee('Task Kuvauspäivä (job 2026-0042 Messuvideo) starts on Mon 30.11.2026.')
->assertSee('1 unread notification');
});
it('shows the bell in the header of signed-in pages', function () {
$this->actingAs($this->user);
$this->get(route('jobs.index'))
->assertOk()
->assertSee('data-notifications-bell', false);
});
it('notifies people at once when someone else adds them to a task, not the one who added', function () {
$marketing = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole();
DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $marketing->id]);
$this->actingAs($this->user);
Livewire::test('jobs::task-form', ['job' => $this->job->id])
->set('form.title', 'Mainoskuvat')
->set('form.work_type_id', (string) WorkTypeModel::query()->where('name', 'graafinen työ')->value('id'))
->set('form.member_user_ids', [(string) $this->member->id])
->call('save')
->assertHasNoErrors();
expect(NotificationModel::query()->where('user_id', $this->user->id)->count())->toBe(0)
->and(NotificationModel::query()->where('user_id', $this->member->id)->sole()->type->value)->toBe('task_assigned');
$this->actingAs($this->member);
Livewire::test('notifications::bell')
->assertSee('Sinut lisättiin tehtävän Mainoskuvat jäseneksi (työ 2026-0042 Messuvideo).')
->assertSee(route('jobs.show', $this->job->id).'#task-'.TaskModel::query()->sole()->id, false);
});
it('notifies people at once about their new roles on a job', function () {
$jobId = app(CreateCustomerJob::class)->execute(new CustomerJobData(
name: 'Esite',
customerId: CustomerModel::factory()->create()->id,
serviceUnitIds: [ServiceUnitModel::query()->value('id')],
responsibleUserId: $this->member->id,
kind: 'one_off',
status: 'order',
priority: 'normal',
memberUserIds: [$this->member->id, $this->user->id],
), $this->user->id);
$this->actingAs($this->member);
Livewire::test('notifications::bell')
->assertSee('Sinut lisättiin työn '.CustomerJobModel::query()->find($jobId)->number.' Esite vastuuhenkilöksi ja jäseneksi.')
->assertSee(route('jobs.show', $jobId), false);
expect(NotificationModel::query()->where('user_id', $this->user->id)->count())->toBe(0);
});