users = new InMemoryUserAccountRepository; $this->passwordLinks = new FakePasswordLinkSender; $supervisorRule = new SupervisorRule($this->users); $transactions = new FakeTransactionManager; $this->createUser = new CreateUserAccount($this->users, $supervisorRule, $transactions, $this->passwordLinks); $this->updateUser = new UpdateUserAccount($this->users, $supervisorRule, $transactions); $this->adminId = $this->users->save(UserAccount::register(new PersonName('Anna Admin'), new EmailAddress('anna@example.test'), Role::Admin))->id(); $this->employeeId = $this->users->save(UserAccount::register(new PersonName('Eero Työntekijä'), new EmailAddress('eero@example.test'), Role::Employee))->id(); }); it('creates a user and sends a link for setting the password', function () { $userId = $this->createUser->execute(new UserAccountData('Maija Meikäläinen', 'Maija@Example.test', Role::Employee)); expect($this->users->findById($userId)?->email()->value)->toBe('maija@example.test') ->and($this->passwordLinks->sentTo)->toBe(['maija@example.test']); }); it('creates an intern with an end date and a supervisor', function () { $userId = $this->createUser->execute(new UserAccountData('Ida', 'ida@example.test', Role::Intern, true, '2027-05-31', $this->employeeId)); $intern = $this->users->findById($userId); expect($intern?->employmentEndsOn()?->format('Y-m-d'))->toBe('2027-05-31') ->and($intern?->supervisorId())->toBe($this->employeeId); }); it('rejects a supervisor who cannot supervise', function (string $case) { $supervisorId = match ($case) { 'admin' => $this->adminId, 'unknown' => 999, 'inactive' => $this->users->save(UserAccount::reconstitute(50, new PersonName('Pois'), new EmailAddress('pois@example.test'), Role::Employee, false, null, null))->id(), }; $this->createUser->execute(new UserAccountData('Ida', 'ida@example.test', Role::Intern, true, '2027-05-31', $supervisorId)); })->throws(InvalidSupervisor::class)->with(['admin', 'unknown', 'inactive']); it('rejects an email address that another user has', function () { expect(fn () => $this->createUser->execute(new UserAccountData('Toinen Eero', 'EERO@example.test', Role::Employee))) ->toThrow(EmailAlreadyInUse::class) ->and($this->passwordLinks->sentTo)->toBeEmpty(); }); it('does not let the last active admin be deactivated or demoted', function (bool $active, Role $role) { $this->updateUser->execute($this->adminId, new UserAccountData('Anna Admin', 'anna@example.test', $role, $active)); })->throws(LastAdminCannotBeRemoved::class)->with([ 'deactivated' => [false, Role::Admin], 'demoted' => [true, Role::Management], ]); it('lets an admin be demoted when another active admin remains', function () { $this->users->save(UserAccount::register(new PersonName('Toinen Admin'), new EmailAddress('toinen@example.test'), Role::Admin)); $this->updateUser->execute($this->adminId, new UserAccountData('Anna Admin', 'anna@example.test', Role::Management)); expect($this->users->findById($this->adminId)?->role())->toBe(Role::Management); }); it('fails to update an unknown user', function () { $this->updateUser->execute(999, new UserAccountData('X', 'x@example.test', Role::Employee)); })->throws(UserAccountNotFound::class); it('sends a password link to a stored user', function () { (new SendPasswordSetupLink($this->users, $this->passwordLinks))->execute($this->employeeId); expect($this->passwordLinks->sentTo)->toBe(['eero@example.test']); }); it('checks whether a user may log in today', function () { $internId = $this->createUser->execute(new UserAccountData('Ida', 'ida@example.test', Role::Intern, true, '2027-05-31', $this->employeeId)); $clock = new FakeClock(new DateTimeImmutable('2027-05-31')); $checkLogin = new CheckLoginAllowed($this->users, $clock); expect($checkLogin->execute($internId))->toBeTrue() ->and($checkLogin->execute(999))->toBeFalse(); $clock->today = new DateTimeImmutable('2027-06-01'); expect($checkLogin->execute($internId))->toBeFalse(); }); it('builds input from form data', function () { $data = UserAccountData::fromArray(['name' => 'Ida', 'email' => 'ida@example.test', 'role' => 'intern', 'active' => true, 'employment_ends_on' => '', 'supervisor_id' => '3']); expect($data->role)->toBe(Role::Intern) ->and($data->employmentEndsOn)->toBeNull() ->and($data->supervisorId)->toBe(3); });