orgA = Organisation::factory()->create(); $this->orgB = Organisation::factory()->create(); } public function test_form_schema_filters_by_organisation_route_parameter(): void { FormSchema::factory()->count(3)->create(['organisation_id' => $this->orgA->id]); FormSchema::factory()->count(2)->create(['organisation_id' => $this->orgB->id]); $this->actingAsOrgUser($this->orgA); $this->withRouteParameter('organisation', $this->orgA); $this->assertSame(3, FormSchema::query()->count()); $this->withRouteParameter('organisation', $this->orgB); $this->assertSame(2, FormSchema::query()->count()); } public function test_form_template_filters_by_organisation_route_parameter(): void { FormTemplate::factory()->count(2)->create(['organisation_id' => $this->orgA->id]); FormTemplate::factory()->count(4)->create(['organisation_id' => $this->orgB->id]); $this->withRouteParameter('organisation', $this->orgA); $this->assertSame(2, FormTemplate::query()->count()); $this->withRouteParameter('organisation', $this->orgB); $this->assertSame(4, FormTemplate::query()->count()); } public function test_form_field_library_filters_by_organisation_route_parameter(): void { FormFieldLibrary::factory()->count(1)->create(['organisation_id' => $this->orgA->id]); FormFieldLibrary::factory()->count(3)->create(['organisation_id' => $this->orgB->id]); $this->withRouteParameter('organisation', $this->orgA); $this->assertSame(1, FormFieldLibrary::query()->count()); } public function test_form_schema_webhook_is_scoped_via_fk_chain(): void { $schemaA = FormSchema::factory()->create(['organisation_id' => $this->orgA->id]); $schemaB = FormSchema::factory()->create(['organisation_id' => $this->orgB->id]); FormSchemaWebhook::factory()->count(2)->create(['form_schema_id' => $schemaA->id]); FormSchemaWebhook::factory()->count(3)->create(['form_schema_id' => $schemaB->id]); $this->withRouteParameter('organisation', $this->orgA); // Addendum Q2 / WS-4 Commit 3: FK-chain scope now filters direct // queries by the route's organisation via form_schemas. $this->assertSame(2, FormSchemaWebhook::query()->count()); $this->withRouteParameter('organisation', $this->orgB); $this->assertSame(3, FormSchemaWebhook::query()->count()); // Admin-wide lookups still work via withoutGlobalScope(). $this->assertSame( 5, FormSchemaWebhook::withoutGlobalScope(OrganisationScope::class)->count() ); } private function actingAsOrgUser(Organisation $org): void { $user = User::factory()->create(); $org->users()->attach($user, ['role' => 'org_member']); $this->actingAs($user); } private function withRouteParameter(string $name, mixed $value): void { $route = new Route(['GET'], '/_test', static fn () => null); $route->bind(request()); $route->setParameter($name, $value); request()->setRouteResolver(static fn () => $route); } }