Phase 7 was scoped to guard aggregate children, which the Doctrine filter cannot reach. Measuring first — as the plan required — moved the target: all 22 children and their 20 repositories were already sound. Every list query anchors on its root, and ServiceItemRepository even joins service_sections and filters on the pair by hand. A repository-level guard would have found nothing. The real exposure was one layer up. Where a uuid arrives from a request body or query string, the entity it names is loaded by uuid alone, and the filter is no help: aggregate children have no tenant column, and a panel user who never chose an environment is not filtered at all. Three leaks, each proven by removing the fix and watching the new tests go red: - GET /api/v1/appointment-service-slots accepted service_item_uuids from any environment. Existence, bookable state and duration leaked through the error messages and the returned slots. The booking path in the same controller had guarded this since it was written; the slot path never did. - POST /api/v1/my/appointment attached service_section_uuid, service_item_uuid, staff_uuid and the service list without any check, and persisted them onto the appointment. A write, not just a read. - PatientService did the same in all three of its loops — pricing, session create, session update — so another environment's service price entered the invoice and its SessionService row was stored, staff included. TenantOwnershipChecker is the single place that answers "does this belong to the current environment?". It reads getEntityType()/getEntityId(), so ServiceItem now delegates that pair to its section: an aggregate child exposing the tenant it inherits. An entity that exposes no pair throws rather than returning false — silence here builds an always-closed guard, which is its own bug. TenantLookupInventoryTest keeps a per-file count of these lookups. It earned its place immediately: the first run found more sites than the manual grep had, and reviewing them turned up the third PatientService loop. StaffController looked unguarded until read properly — ownsStaff sits two lines below the null check. One assertion was wrong before it was right: the create-path test read `$session['services'] ?? []`, which passes vacuously. It now counts the stored rows through the repository, and fails without the fix. Tests: 879 passing. PHPStan unchanged at its 17 pre-existing errors. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
143 lines
5.4 KiB
PHP
143 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Shared;
|
|
|
|
use App\ClinicService\Entity\ServiceItem;
|
|
use App\ClinicService\Entity\ServiceSection;
|
|
use App\Clinic\Entity\Clinic;
|
|
use App\Doctor\Entity\Doctor;
|
|
use App\Shared\Context\EntityContext;
|
|
use App\Shared\Tenant\TenantOwnershipChecker;
|
|
use App\Staff\Entity\ClinicStaff;
|
|
use App\Tests\ApiTestCase;
|
|
|
|
/**
|
|
* تنها محافظِ موجودیتهایی که uuidشان از خودِ درخواست میآید و TenantFilter
|
|
* پوششان نمیدهد — یا چون فرزند aggregateاند یا چون کاربر محیطی انتخاب نکرده.
|
|
*/
|
|
class TenantOwnershipCheckerTest extends ApiTestCase
|
|
{
|
|
private function checker(): TenantOwnershipChecker
|
|
{
|
|
return static::getContainer()->get(TenantOwnershipChecker::class);
|
|
}
|
|
|
|
private function makeDoctor(): Doctor
|
|
{
|
|
$doctor = new Doctor($this->createUser(['ROLE_DOCTOR']), 'دکتر مالکیت');
|
|
$this->em->persist($doctor);
|
|
$this->em->flush();
|
|
|
|
return $doctor;
|
|
}
|
|
|
|
private function serviceFor(Doctor $doctor): ServiceItem
|
|
{
|
|
$section = new ServiceSection('doctor', $doctor->getId(), 'بخش');
|
|
$this->em->persist($section);
|
|
$item = new ServiceItem($section, 'سرویس', 0);
|
|
$this->em->persist($item);
|
|
$this->em->flush();
|
|
|
|
return $item;
|
|
}
|
|
|
|
public function testEntityOfTheSameEnvironmentBelongs(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
|
|
self::assertTrue(
|
|
$this->checker()->belongsTo(EntityContext::forDoctor($doctor), $this->serviceFor($doctor)),
|
|
);
|
|
}
|
|
|
|
public function testEntityOfAnotherEnvironmentDoesNot(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$foreign = $this->serviceFor($this->makeDoctor());
|
|
|
|
self::assertFalse($this->checker()->belongsTo(EntityContext::forDoctor($doctor), $foreign));
|
|
}
|
|
|
|
/** فرزند aggregate جفتش را از ریشه به ارث میبرد — ServiceItem از ServiceSection. */
|
|
public function testAggregateChildIsComparedThroughItsRoot(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$item = $this->serviceFor($doctor);
|
|
|
|
self::assertSame('doctor', $item->getEntityType());
|
|
self::assertSame($doctor->getId(), $item->getEntityId());
|
|
}
|
|
|
|
/** ❌ null یعنی «پیدا نشد» و همانقدر رد میشود که یک موجودیت بیگانه. */
|
|
public function testNullIsNeverOwned(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
|
|
self::assertFalse($this->checker()->belongsTo(EntityContext::forDoctor($doctor), null));
|
|
self::assertFalse($this->checker()->belongsToPair('doctor', $doctor->getId(), null));
|
|
}
|
|
|
|
/** ❌ محیط حلنشده هیچچیز را مالک نیست. */
|
|
public function testUnresolvedContextOwnsNothing(): void
|
|
{
|
|
$item = $this->serviceFor($this->makeDoctor());
|
|
|
|
self::assertFalse($this->checker()->belongsTo(EntityContext::unknown(), $item));
|
|
}
|
|
|
|
/**
|
|
* ❌ موجودیتی که جفت محیطش را expose نمیکند باید بلند خطا بدهد، نه اینکه
|
|
* بیصدا false برگرداند — سکوت اینجا یعنی گاردِ همیشه-بسته که باگ میسازد.
|
|
*/
|
|
public function testEntityWithoutATenantPairThrows(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->checker()->belongsTo(EntityContext::forDoctor($doctor), new \stdClass());
|
|
}
|
|
|
|
/** ⚠️ یک بیگانه در فهرست، کل فهرست را رد میکند. */
|
|
public function testOneForeignEntityRejectsTheWholeList(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$mine = $this->serviceFor($doctor);
|
|
$foreign = $this->serviceFor($this->makeDoctor());
|
|
$context = EntityContext::forDoctor($doctor);
|
|
|
|
self::assertTrue($this->checker()->allBelongTo($context, [$mine]));
|
|
self::assertFalse($this->checker()->allBelongTo($context, [$mine, $foreign]));
|
|
self::assertTrue($this->checker()->allBelongTo($context, []), 'فهرست خالی مانعی ندارد');
|
|
}
|
|
|
|
/** نوعِ محیط هم بخشی از هویت است: کلینیک ۵ با پزشک ۵ یکی نیست. */
|
|
public function testTypeIsPartOfTheIdentityNotJustTheId(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$item = $this->serviceFor($doctor);
|
|
|
|
$clinic = new Clinic($this->createUser(['ROLE_CLINIC']));
|
|
$clinic->setName('کلینیک');
|
|
$this->em->persist($clinic);
|
|
$this->em->flush();
|
|
|
|
self::assertFalse(
|
|
$this->checker()->belongsToPair('clinic', $doctor->getId(), $item),
|
|
'همان شناسه ولی نوع دیگر نباید مالک شمرده شود',
|
|
);
|
|
}
|
|
|
|
/** موجودیتهایی که خودشان جفت دارند (نه از ریشه) هم با همین checker سنجیده میشوند. */
|
|
public function testEntitiesCarryingTheirOwnPairWorkToo(): void
|
|
{
|
|
$doctor = $this->makeDoctor();
|
|
$staff = new ClinicStaff('doctor', $doctor->getId(), 'پرسنل');
|
|
$this->em->persist($staff);
|
|
$this->em->flush();
|
|
|
|
self::assertTrue($this->checker()->belongsTo(EntityContext::forDoctor($doctor), $staff));
|
|
self::assertFalse($this->checker()->belongsToPair('doctor', $doctor->getId() + 1, $staff));
|
|
}
|
|
}
|