Phase 3 of the tenant-marking series. The same concept was written four ways, and the Doctrine filter arriving in phase 4 keys on the field name — so the tables using a different spelling would have been skipped silently, which is exactly the leak this work exists to prevent. - discount_rules: owner_type/owner_id renamed to entity_type/entity_id. Pure rename, no data moves. - doctor_secretaries: owner_type plus a nullable clinic_id replaced by the shared pair. The environment now comes from the clinic argument alone, so the inconsistent combination (owner_type='clinic', clinic_id=NULL) can no longer be constructed, and the redundant constructor parameter is gone. - user_active_context: added db_type, so resolving an environment is one lookup instead of "try clinics, then try doctors". Filled from the type already present in available_contexts. - entity_type is VARCHAR(10) in all twenty tenant tables; four of them were 20. Behaviour change, the only one in this series: the doctor_secretaries unique key went from (doctor_id, secretary_id, owner_type) to (doctor_id, secretary_id, entity_type, entity_id). With clinic_id outside the key, one secretary could not be assigned to the same doctor in two clinics — the second row collided on owner_type='clinic'. The duplicate check in SecretaryController had the same blind spot and would have rejected the request before the database saw it; both are fixed together. Correcting an assumption from the phase-3 plan: mobile_verification_otp.entity_type really is a tenant pair. NotificationMobileController validates the target against ['doctor','clinic'] and stores that entity's id, so the column was normalised with the rest rather than treated as unrelated. TenantOwnedTrait gained assignTenantPair() for callers that resolved the pair as scalars and hold no entity — building an EntityContext from scalars would produce one where isClinic() is true but ->clinic is null, breaking consumers silently. tests/ApiTestCase::createUser now retries on a duplicate mobile. db_test is never reset and already holds ~38k users, so the 9-digit random draw collided often enough to fail unrelated tests a few percent of runs. Tests: 830 passing. PHPStan reports no new errors on the changed files. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
188 lines
7.1 KiB
PHP
188 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Shared;
|
|
|
|
use App\Auth\Entity\User;
|
|
use App\Auth\Entity\UserActiveContext;
|
|
use App\Clinic\Entity\Clinic;
|
|
use App\Clinic\Entity\ClinicDoctorPermission;
|
|
use App\Discount\Entity\DiscountRule;
|
|
use App\Doctor\Entity\Doctor;
|
|
use App\Shared\Context\EntityContext;
|
|
use App\Tests\ApiTestCase;
|
|
|
|
/**
|
|
* پس از یکسانسازی املای tenant، هر نقش باید فقط دادهٔ محیط خودش را ببیند.
|
|
*
|
|
* روی discount-rules سنجیده میشود چون تنها جدولی بود که با owner_type نوشته شده
|
|
* بود و تا فاز ۳ از هر فیلتر مبتنی بر entity_type جا میماند.
|
|
*/
|
|
class TenantIsolationMatrixTest extends ApiTestCase
|
|
{
|
|
private const LIST_URL = '/api/v1/admin/discount-rules';
|
|
|
|
private function makeDoctor(?User $user = null): Doctor
|
|
{
|
|
$doctor = new Doctor($user ?? $this->createUser(['ROLE_DOCTOR']), 'دکتر ماتریس');
|
|
$this->em->persist($doctor);
|
|
$this->em->flush();
|
|
|
|
return $doctor;
|
|
}
|
|
|
|
private function makeClinic(?User $owner = null): Clinic
|
|
{
|
|
$clinic = new Clinic($owner ?? $this->createUser(['ROLE_CLINIC']));
|
|
$clinic->setName('کلینیک ماتریس');
|
|
$this->em->persist($clinic);
|
|
$this->em->flush();
|
|
|
|
return $clinic;
|
|
}
|
|
|
|
/** عضویت بهعلاوهٔ مجوز discounts.view که در DEFAULT_PERMISSIONS خاموش است. */
|
|
private function joinClinic(Clinic $clinic, Doctor $doctor): void
|
|
{
|
|
$clinic->getDoctors()->add($doctor);
|
|
|
|
$permission = new ClinicDoctorPermission($clinic, $doctor);
|
|
$permission->mergePermissions(['resources' => ['discounts' => ['view' => true]]]);
|
|
$this->em->persist($permission);
|
|
$this->em->flush();
|
|
}
|
|
|
|
private function setClinicContext(User $user, Clinic $clinic): void
|
|
{
|
|
$this->em->persist(new UserActiveContext($user, $clinic->getUuid(), EntityContext::TYPE_CLINIC));
|
|
$this->em->flush();
|
|
}
|
|
|
|
private function rule(string $entityType, int $entityId, string $name): DiscountRule
|
|
{
|
|
$rule = new DiscountRule($entityType, $entityId, $name, DiscountRule::TYPE_INVOICE_AMOUNT);
|
|
$this->em->persist($rule);
|
|
$this->em->flush();
|
|
|
|
return $rule;
|
|
}
|
|
|
|
/**
|
|
* پاسخ این endpoint با success(['data' => …]) ساخته میشود، پس یک لایه
|
|
* تودرتوی اضافه دارد — همان دام double-nesting که در CLAUDE.md ثبت شده.
|
|
*
|
|
* @return string[]
|
|
*/
|
|
private function listedNames(User $user): array
|
|
{
|
|
$res = $this->authJson('GET', self::LIST_URL, $user);
|
|
self::assertSame(200, $this->responseCode());
|
|
|
|
return array_column($res['data']['data'] ?? [], 'name');
|
|
}
|
|
|
|
public function testIndependentDoctorSeesOnlyItsOwnRules(): void
|
|
{
|
|
$mine = $this->makeDoctor();
|
|
$other = $this->makeDoctor();
|
|
$clinic = $this->makeClinic();
|
|
|
|
$this->rule('doctor', $mine->getId(), 'مالِ من');
|
|
$this->rule('doctor', $other->getId(), 'مالِ پزشک دیگر');
|
|
$this->rule('clinic', $clinic->getId(), 'مالِ کلینیک');
|
|
|
|
$names = $this->listedNames($mine->getUser());
|
|
|
|
self::assertContains('مالِ من', $names);
|
|
self::assertNotContains('مالِ پزشک دیگر', $names);
|
|
self::assertNotContains('مالِ کلینیک', $names);
|
|
}
|
|
|
|
public function testClinicManagerSeesOnlyItsClinicRules(): void
|
|
{
|
|
$clinic = $this->makeClinic();
|
|
$otherClinic = $this->makeClinic();
|
|
$doctor = $this->makeDoctor();
|
|
|
|
$this->rule('clinic', $clinic->getId(), 'کلینیک خودم');
|
|
$this->rule('clinic', $otherClinic->getId(), 'کلینیک دیگر');
|
|
$this->rule('doctor', $doctor->getId(), 'مطب یک پزشک');
|
|
|
|
$names = $this->listedNames($clinic->getUser());
|
|
|
|
self::assertContains('کلینیک خودم', $names);
|
|
self::assertNotContains('کلینیک دیگر', $names);
|
|
self::assertNotContains('مطب یک پزشک', $names);
|
|
}
|
|
|
|
/** پزشکِ عضو در محیط کلینیک، قوانین کلینیک را میبیند نه مطب شخصیاش. */
|
|
public function testMemberDoctorInClinicContextSeesClinicRules(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$clinic = $this->makeClinic();
|
|
$this->joinClinic($clinic, $doctor);
|
|
$this->setClinicContext($doctor->getUser(), $clinic);
|
|
|
|
$this->rule('clinic', $clinic->getId(), 'قانون کلینیک');
|
|
$this->rule('doctor', $doctor->getId(), 'قانون مطب شخصی');
|
|
|
|
$names = $this->listedNames($doctor->getUser());
|
|
|
|
self::assertContains('قانون کلینیک', $names);
|
|
self::assertNotContains('قانون مطب شخصی', $names);
|
|
}
|
|
|
|
/** همان پزشک بیرون از محیط کلینیک، فقط مطب شخصی. */
|
|
public function testMemberDoctorOutsideClinicContextSeesOwnPracticeRules(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$clinic = $this->makeClinic();
|
|
$this->joinClinic($clinic, $doctor);
|
|
|
|
$this->rule('clinic', $clinic->getId(), 'قانون کلینیک');
|
|
$this->rule('doctor', $doctor->getId(), 'قانون مطب شخصی');
|
|
|
|
$names = $this->listedNames($doctor->getUser());
|
|
|
|
self::assertContains('قانون مطب شخصی', $names);
|
|
self::assertNotContains('قانون کلینیک', $names);
|
|
}
|
|
|
|
/** پزشکی که مالک کلینیک هم هست: محیط فعال تعیینکننده است، نه نقش. */
|
|
public function testDoctorWhoOwnsClinicSeesClinicRulesInClinicContext(): void
|
|
{
|
|
$user = $this->createUser(['ROLE_DOCTOR', 'ROLE_CLINIC']);
|
|
$doctor = $this->makeDoctor($user);
|
|
$clinic = $this->makeClinic($user);
|
|
$this->joinClinic($clinic, $doctor);
|
|
$this->setClinicContext($user, $clinic);
|
|
|
|
$this->rule('clinic', $clinic->getId(), 'قانون کلینیکِ خودش');
|
|
$this->rule('doctor', $doctor->getId(), 'قانون مطبِ خودش');
|
|
|
|
$names = $this->listedNames($user);
|
|
|
|
self::assertContains('قانون کلینیکِ خودش', $names);
|
|
self::assertNotContains('قانون مطبِ خودش', $names);
|
|
}
|
|
|
|
/** ❌ بدون توکن، لیست اصلاً باز نمیشود. */
|
|
public function testAnonymousCannotListRules(): void
|
|
{
|
|
$this->client->request('GET', self::LIST_URL);
|
|
|
|
self::assertSame(401, $this->client->getResponse()->getStatusCode());
|
|
}
|
|
|
|
/** ⚠️ محیط حلنشده: کاربر بدون نقش پنل، نه خطای ۵۰۰ میگیرد نه دادهٔ کسی را. */
|
|
public function testUserWithoutAPanelRoleGetsNoRules(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$this->rule('doctor', $doctor->getId(), 'قانون یک پزشک');
|
|
|
|
$stranger = $this->createUser(['ROLE_USER']);
|
|
$this->authJson('GET', self::LIST_URL, $stranger);
|
|
|
|
self::assertContains($this->responseCode(), [403, 404], 'دسترسی رد شود، نه خطای سرور');
|
|
}
|
|
}
|