Files
clinicpro/tests/Shared/NumericFieldNormalizerTest.php
T
hamedandClaude Opus 5 04d3222559 feat(resource): admin UI for resources, types, skills and pools, plus real API docs
Four pages on the existing design system: a resources list whose branch/type/skill/
status filters live in the URL and go straight to the server, and three supporting
pages for types, skills and pools. Filtering client-side over a list the server had
already filtered would have been a second source of truth, so the page does neither.

The pool members dialog only offers resources from the pool's own branch and type —
the same rule the server enforces with 422, applied early so the user never reaches
the error. Skill assignment and pool membership are both full replacements, and both
say so in the dialog, because a partial-looking save that silently drops rows is
worse than an explicit one.

Wiring that was missing: deactivating a staff member through
PATCH /api/v1/staff/{uuid}/toggle now closes their resource too. Without it an
inactive operator would still have shown up in availability search. It is an explicit
call rather than a Doctrine lifecycle callback, since callbacks do not fire for
getArrayResult() — which is how every admin list is built — and that asymmetry is
its own bug. The reverse does not hold: closing a resource does not deactivate the
person, who may be purely administrative.

docs/api/resource.md documents all sixteen endpoints with responses captured from
real curl runs against ddev, including the 422 bodies for person-capacity and
non-scalar attributes. staff.md gains a "relationship to resources" section stating
that job_title is not a skill. tenancy.md contrasts these aggregate children —
whose roots do carry a tenant pair — with the branch_working_hours case from task 01,
where the root was global and the classification was wrong.

Also fixed a pre-existing flaky test: NumericFieldNormalizerTest guarded its random
mobile against collision on the never-reset db_test but not its random national code,
so a full-suite run could fail with 422 and close the EntityManager, taking an
unrelated test down with it. Both are now guarded, and the assertion prints the
server's response instead of a bare "422 is not 201".

Verified: phpunit 1119 tests / 3113 assertions green; slot-mode frozen contract green;
phpstan 14 errors before and after, none in touched files; tsc clean; vitest 88 files
/ 617 tests green.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-30 17:51:38 +03:30

121 lines
5.3 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
namespace App\Tests\Shared;
use App\Auth\Entity\User;
use App\Doctor\Entity\Doctor;
use App\Secretary\Entity\DoctorSecretary;
use App\Shared\Util\PersianText;
use App\Tests\ApiTestCase;
/**
* Persian/Arabic digits sent by any client must never reach the database.
* The admin SPA normalizes at the input, but nobat724_front and clinic-pro-tauri
* hit the same endpoints, so the request layer is the real guarantee.
*/
class NumericFieldNormalizerTest extends ApiTestCase
{
private function toPersianDigits(string $latin): string
{
return str_replace(range('0', '9'), ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'], $latin);
}
public function testDigitsHelperTranslatesWithoutStripping(): void
{
self::assertSame('09123456789', PersianText::digits('۰۹۱۲۳۴۵۶۷۸۹'));
self::assertSame('0912', PersianText::digits('٠٩١٢'));
self::assertSame('IR12-34', PersianText::digits('IR۱۲-۳۴'), 'letters and separators survive');
self::assertSame('', PersianText::digits(''));
}
public function testSecretaryCreatedWithPersianDigitsIsStoredLatin(): void
{
$owner = $this->createUser(['ROLE_USER', 'ROLE_DOCTOR']);
$doctor = new Doctor($owner, 'دکتر تست');
$this->em->persist($doctor);
$this->em->flush();
// db_test پاک نمی‌شود؛ شماره باید تازه باشد وگرنه endpoint با «تکراری» رد می‌کند.
do {
$persianMobile = '۰۹' . str_pad((string) random_int(0, 999_999_999), 9, '۰', STR_PAD_LEFT);
$latinMobile = PersianText::digits($persianMobile);
} while ($this->em->getRepository(User::class)->findOneBy(['mobileNumber' => $latinMobile]) !== null);
// کد ملی هم همین‌طور. ثابت‌بودنش این تست را flaky می‌کرد: در اجرای کامل سوئیت،
// بسته به ترتیب اجرا، ردیفِ اجرای قبلی باعث ۴۲۲ «تکراری» می‌شد و در اجرای تنها
// سبز بود.
// مثل شماره، یکتایی‌اش هم باید سنجیده شود: تصادفی‌بودن تنهایی کافی نیست و در
// اجرای کامل سوئیت روی db_testِ انباشته، برخورد ۴۲۲ «تکراری» می‌داد.
do {
$persianNationalCode = str_pad(
$this->toPersianDigits((string) random_int(0, 9_999_999_999)),
10,
'۰',
STR_PAD_LEFT,
);
$latinNationalCode = PersianText::digits($persianNationalCode);
} while (
$this->em->getRepository(DoctorSecretary::class)
->findOneBy(['nationalCode' => $latinNationalCode]) !== null
);
$body = $this->authJson('POST', '/api/v1/secretary', $owner, [
'doctor_uuid' => $doctor->getUuid(),
'mobile_number' => $persianMobile,
'name' => 'منشی تست',
'national_code' => $persianNationalCode,
]);
// بدنه در پیام می‌آید: «۴۲۲ به‌جای ۲۰۱» بدون دلیلِ سرور قابل دیباگ نیست.
self::assertSame(
201,
$this->responseCode(),
'Persian digits must not break validation: ' . json_encode($body, JSON_UNESCAPED_UNICODE),
);
$this->em->clear();
$created = $this->em->getRepository(User::class)->findOneBy(['mobileNumber' => $latinMobile]);
self::assertNotNull($created, 'user is stored under the latin mobile');
$rel = $this->em->getRepository(DoctorSecretary::class)->findOneBy(['secretary' => $created]);
self::assertSame(PersianText::digits($persianNationalCode), $rel->getNationalCode());
}
public function testNestedArraysAreNormalized(): void
{
$user = $this->createUser(['ROLE_USER', 'ROLE_DOCTOR']);
$this->authJson('PUT', '/api/v1/insurance-pricing', $user, [
'free_visit_price_rials' => '۵۰۰۰۰۰',
'insurances' => [
['insurance_id' => 1, 'patient_share_rials' => '۱۲۳۴۵'],
],
]);
// پروفایل پزشک ممکن است بیمه‌ای نداشته باشد؛ مهم این است که ارقام فارسی
// باعث خطای اعتبارسنجی یا NaN نشوند.
self::assertNotSame(500, $this->responseCode(), 'nested persian digits must not blow up');
}
public function testNonNumericKeysKeepPersianDigits(): void
{
$owner = $this->createUser(['ROLE_USER', 'ROLE_DOCTOR']);
$doctor = new Doctor($owner, 'دکتر تست');
$this->em->persist($doctor);
$this->em->flush();
$mobile = '09' . str_pad((string) random_int(0, 999_999_999), 9, '0', STR_PAD_LEFT);
$this->authJson('POST', '/api/v1/secretary', $owner, [
'doctor_uuid' => $doctor->getUuid(),
'mobile_number' => $mobile,
'name' => 'منشی شماره ۲',
]);
self::assertSame(201, $this->responseCode());
$this->em->clear();
$created = $this->em->getRepository(User::class)->findOneBy(['mobileNumber' => $mobile]);
self::assertStringContainsString('۲', $created->getRealName(), 'name is not a numeric field');
}
}