Files
Toiminnanohjaus/tests/Unit/Modules/Jobs/Application/CustomerJobUseCasesTest.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

128 lines
5.8 KiB
PHP

<?php
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
use App\Modules\Jobs\Application\Services\JobDetailsFactory;
use App\Modules\Jobs\Application\UseCases\ChangeJobStatus;
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
use App\Modules\Jobs\Application\UseCases\UpdateCustomerJob;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Events\PeopleAddedToJob;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobReference;
use App\Modules\Users\Domain\Enums\Role;
use Tests\Fakes\FakeCatalogQueries;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeCustomerQueries;
use Tests\Fakes\FakeEventDispatcher;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\FakeUserAccountQueries;
use Tests\Fakes\InMemoryCustomerJobRepository;
beforeEach(function () {
$this->events = new FakeEventDispatcher;
$this->jobs = new InMemoryCustomerJobRepository;
$this->customers = new FakeCustomerQueries;
$this->catalog = new FakeCatalogQueries;
$this->users = new FakeUserAccountQueries;
$this->clock = new FakeClock(new DateTimeImmutable('2026-12-31'));
$transactions = new FakeTransactionManager;
$this->customers->customers = [1 => 'Mainos Oy', 2 => 'Toinen Oy'];
$this->customers->contacts = [[1, 5, 'Maija'], [2, 6, 'Toisen kontakti']];
$this->catalog->addServiceUnit(1, 'Mediatuotanto');
$this->catalog->addServiceUnit(2, 'Markkinointi', active: false);
$this->catalog->addServiceUnit(9, 'Kaikille yhteiset', shared: true);
$this->users->addUser(10, 'Eero');
$this->users->addUser(11, 'Aino', Role::Subcontractor);
$this->users->addUser(12, 'Pois', active: false);
$factory = new JobDetailsFactory($this->customers, $this->catalog, $this->users);
$this->create = new CreateCustomerJob($this->jobs, $this->jobs, $factory, $transactions, $this->clock, $this->events);
$this->update = new UpdateCustomerJob($this->jobs, $factory, $transactions, $this->clock, $this->events);
$this->changeStatus = new ChangeJobStatus($this->jobs, $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')));
});
/**
* @param array<string, mixed> $overrides
*/
function jobData(array $overrides = []): CustomerJobData
{
return new CustomerJobData(...array_merge([
'name' => 'Messuvideo',
'customerId' => 1,
'serviceUnitIds' => [1],
'responsibleUserId' => 10,
'kind' => 'one_off',
'status' => 'tentative',
'priority' => 'normal',
], $overrides));
}
it('numbers jobs per year and starts the history', function () {
$first = $this->create->execute(jobData(), 10);
$second = $this->create->execute(jobData(), 10);
$this->clock->today = new DateTimeImmutable('2027-01-01');
$nextYear = $this->create->execute(jobData(), 10);
expect($this->jobs->findById($first)?->number()->format())->toBe('2026-0001')
->and($this->jobs->findById($second)?->number()->format())->toBe('2026-0002')
->and($this->jobs->findById($nextYear)?->number()->format())->toBe('2027-0001')
->and($this->jobs->changedFields($first))->toBe(['created']);
});
it('rejects references that do not fit the job', function (array $overrides, string $field) {
try {
$this->create->execute(jobData($overrides), 10);
$this->fail('No exception');
} catch (InvalidJobReference $exception) {
expect($exception->field)->toBe($field);
}
})->with([
'unknown customer' => [['customerId' => 99], 'customer'],
'contact of another customer' => [['customerContactId' => 6], 'customer_contact'],
'inactive service unit' => [['serviceUnitIds' => [2]], 'service_units'],
'shared service unit' => [['serviceUnitIds' => [9]], 'service_units'],
'subcontractor as responsible' => [['responsibleUserId' => 11], 'responsible_user'],
'inactive member' => [['memberUserIds' => [12]], 'members'],
]);
it('keeps an inactive unit or user that is already on the job', function () {
$this->catalog->addServiceUnit(2, 'Markkinointi');
$this->users->addUser(12, 'Pois');
$jobId = $this->create->execute(jobData(['serviceUnitIds' => [1, 2], 'memberUserIds' => [12]]), 10);
$this->catalog->addServiceUnit(2, 'Markkinointi', active: false);
$this->users->addUser(12, 'Pois', active: false);
$this->update->execute($jobId, jobData(['name' => 'Messuvideo 2', 'serviceUnitIds' => [1, 2], 'memberUserIds' => [12]]), 10);
expect($this->jobs->findById($jobId)?->details()->name->value)->toBe('Messuvideo 2');
});
it('records each changed field, and nothing when nothing changed', function () {
$jobId = $this->create->execute(jobData(), 10);
$this->update->execute($jobId, jobData(['priority' => 'urgent', 'customerContactId' => 5, 'tags' => ['messut']]), 10);
$this->update->execute($jobId, jobData(['priority' => 'urgent', 'customerContactId' => 5, 'tags' => ['Messut']]), 10);
$this->changeStatus->execute($jobId, JobStatus::Order, 10);
expect($this->jobs->changedFields($jobId))->toBe(['created', 'priority', 'customer_contact', 'tags', 'status']);
});
it('publishes the new roles people got on a job', function () {
$jobId = $this->create->execute(jobData(['memberUserIds' => [11]]), 10);
$added = end($this->events->dispatched);
expect($added)->toBeInstanceOf(PeopleAddedToJob::class)
->and($added->jobId)->toBe($jobId)
->and($added->jobName)->toBe('Messuvideo')
->and($added->newRoles)->toBe([10 => ['responsible'], 11 => ['member']])
->and($added->actorUserId)->toBe(10);
$count = count($this->events->dispatched);
$this->update->execute($jobId, jobData(['memberUserIds' => [11], 'name' => 'Messuvideo 2']), 10);
expect($this->events->dispatched)->toHaveCount($count);
$this->update->execute($jobId, jobData(['memberUserIds' => [11], 'contactUserId' => 11]), 10);
expect(end($this->events->dispatched)->newRoles)->toBe([11 => ['contact']]);
});