Files
Toiminnanohjaus/tests/Unit/Modules/Customers/Domain/PersonalIdentityCodeTest.php
petriandClaude Opus 5.5 e0b8f4970f Add customer contacts, billing units and consumer personal identity codes
Customer page and contacts/billing units (phase 1, item 2):
- New customer page (customers.show) with details, contacts and billing
  units; add and edit on their own pages, delete after confirmation.
  Creating or editing a customer now leads to this page.
- Contacts: name, title, phone, email, note. Billing units: name,
  billing address, e-invoice address (12-35 letters/digits), operator
  (required with an e-invoice address) and reference.
- Use cases check that the row belongs to the customer, so ids in the
  URL can't reach another customer's rows.
- EmailAddress moves from Users to Shared so both modules can use it.
- <x-confirm-dialog> (native <dialog>) replaces <x-modal>, whose title,
  close button and focus handling are not accessible; solid red buttons
  use red-700 via published NormalButtonColors for 4.5:1 contrast.

Personal identity code (henkilötunnus) for consumers who are invoiced:
- Validated (date, century sign, check character), stored encrypted
  with an HMAC blind index for duplicate checks; never in
  CustomerSummary, search, logs or Livewire state.
- Admins and management see it in full and may remove it; others enter
  it and see only the masked form. The form never shows the stored code.

Spec, architecture doc and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-23 18:11:06 +02:00

60 lines
2.5 KiB
PHP

<?php
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\CompanyCannotHavePersonalIdentityCode;
use App\Modules\Customers\Domain\Exceptions\InvalidPersonalIdentityCode;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Customers\Domain\ValueObjects\PersonalIdentityCode;
it('accepts valid personal identity codes of every century', function (string $code) {
expect((new PersonalIdentityCode($code))->value)->toBe($code);
})->with([
'1900s, hyphen' => '131052-308T',
'1900s, new sign Y' => '010594Y9032',
'1800s' => '010185+123B',
'2000s, leap day' => '290200A4561',
'2000s, new sign E' => '020502E902X',
]);
it('normalises spaces and lower case', function () {
expect((new PersonalIdentityCode(' 020502e902x '))->value)->toBe('020502E902X');
});
it('rejects a wrong check character, an impossible date or a wrong format', function (string $code) {
new PersonalIdentityCode($code);
})->throws(InvalidPersonalIdentityCode::class)->with([
'wrong check character' => '131052-308U',
'no such day (29.2.2001)' => '290201A4569',
'no such day (31.4.1952)' => '310452-308K',
'unknown century sign' => '131052G308T',
'too short' => '131052-30T',
'empty' => '',
]);
it('keeps the code out of the exception message', function () {
try {
new PersonalIdentityCode('131052-308U');
} catch (InvalidPersonalIdentityCode $exception) {
expect($exception->getMessage())->not->toContain('131052');
}
});
it('masks the date of birth', function () {
expect((new PersonalIdentityCode('131052-308T'))->masked())->toBe('••••••-308T');
});
it('gives a personal identity code only to a consumer', function () {
Customer::register(new CustomerName('Mainos Oy'), CustomerType::Company, null, new PersonalIdentityCode('131052-308T'));
})->throws(CompanyCannotHavePersonalIdentityCode::class);
it('keeps the stored code when no new one is given, and drops it when the customer becomes a company', function () {
$customer = Customer::register(new CustomerName('Maija'), CustomerType::Consumer, null, new PersonalIdentityCode('131052-308T'));
$customer->changeDetails(new CustomerName('Maija Meikäläinen'), CustomerType::Consumer, null);
expect($customer->personalIdentityCode()?->value)->toBe('131052-308T');
$customer->changeDetails(new CustomerName('Maijan Kuvaamo Oy'), CustomerType::Company, null);
expect($customer->personalIdentityCode())->toBeNull();
});