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_not_globally_scoped(): 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); // Direct queries leak across orgs — exact reason the docblock warns // never to query FormSchemaWebhook::query() without an eager constraint. $this->assertSame(5, FormSchemaWebhook::query()->count()); // Going through the schema relation respects OrganisationScope on the parent. $this->assertCount(2, $schemaA->fresh()->webhooks); $this->assertCount(3, $schemaB->fresh()->webhooks); } 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); } }