From d9942d1738f5dc61bb03064206ccc4cc84ae30c0 Mon Sep 17 00:00:00 2001 From: Juan Claude Date: Wed, 11 Nov 2026 16:22:34 +0000 Subject: [PATCH 1/2] test(access-purpose): pin what recording and checking a purpose read before they share the read Assisted-by: ClaudeCode:claude-opus-5-5 --- .../Service/AccessPurposeReadGoldenTest.php | 256 ++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 tests/Unit/Service/AccessPurposeReadGoldenTest.php diff --git a/tests/Unit/Service/AccessPurposeReadGoldenTest.php b/tests/Unit/Service/AccessPurposeReadGoldenTest.php new file mode 100644 index 0000000000..25f3de3583 --- /dev/null +++ b/tests/Unit/Service/AccessPurposeReadGoldenTest.php @@ -0,0 +1,256 @@ + + * @copyright 2026 Conduction B.V. + * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * SPDX-License-Identifier: EUPL-1.2 + * SPDX-FileCopyrightText: 2026 Conduction B.V. + * + * @link https://conduction.nl + * + * @spec openspec/specs/case-access-purpose/spec.md#requirement-opening-a-sensitive-case-records-its-purpose + */ + +declare(strict_types=1); + +namespace OCA\Dossiq\Tests\Unit\Service; + +use OCA\Dossiq\Exception\RefusedException; +use OCA\Dossiq\Service\CaseAccessPurposeService; +use OCA\Dossiq\Service\People\CaseRoleHolder; +use OCA\Dossiq\Service\SettingsService; +use OCA\OpenRegister\Db\AuditTrail; +use OCA\OpenRegister\Db\AuditTrail\ObjectAuditEvent; +use OCA\OpenRegister\Db\ObjectEntity; +use PHPUnit\Framework\TestCase; +use Psr\Log\NullLogger; +use RuntimeException; + +/** + * The shared read of record() and check(), and its refusals. + * + * @covers \OCA\Dossiq\Service\CaseAccessPurposeService + * + * @spec openspec/specs/case-access-purpose/spec.md#requirement-opening-a-sensitive-case-records-its-purpose + */ +class AccessPurposeReadGoldenTest extends TestCase { + /** + * The audit events written. + * + * @var array> + */ + private array $written = []; + + /** + * The two gestures, called with a case id. + * + * @return array + */ + public static function gestures(): array { + return ['record' => ['record'], 'check' => ['check']]; + }//end gestures() + + /** + * Without a piece of the configuration both refuse as not recorded, and no case is read. + * + * @param string $gesture The gesture. + * + * @dataProvider gestures + * + * @return void + */ + public function testAnUnconfiguredInstanceRefusesBeforeReading(string $gesture): void { + foreach (['objectService', 'mapper', 'register', 'case_schema', 'case_type_schema'] as $missing) { + $reads = 0; + try { + $this->call(gesture: $gesture, service: $this->service(missing: $missing, reads: $reads), caseId: 'case-1'); + self::fail('An unconfigured ' . $missing . ' must refuse.'); + } catch (RefusedException $e) { + self::assertSame(expected: ['access-purpose-not-recorded', RefusedException::STATUS_INDETERMINATE, 0], actual: [$e->getRule(), $e->getStatus(), $reads]); + } + } + + self::assertSame(expected: [], actual: $this->written); + }//end testAnUnconfiguredInstanceRefusesBeforeReading() + + /** + * A case the reader cannot open is refused with 404. + * + * @param string $gesture The gesture. + * + * @dataProvider gestures + * + * @return void + */ + public function testAnUnreadableCaseIsNotFound(string $gesture): void { + $reads = 0; + try { + $this->call(gesture: $gesture, service: $this->service(missing: '', reads: $reads), caseId: 'case-hidden'); + self::fail('An unreadable case must be refused.'); + } catch (RefusedException $e) { + self::assertSame(expected: ['case-not-readable', 404, 1], actual: [$e->getRule(), $e->getStatus(), $reads]); + } + + self::assertSame(expected: [], actual: $this->written); + }//end testAnUnreadableCaseIsNotFound() + + /** + * An unreadable case type refuses as indeterminate. + * + * @param string $gesture The gesture. + * + * @dataProvider gestures + * + * @return void + */ + public function testAnUnreadableCaseTypeIsIndeterminate(string $gesture): void { + $reads = 0; + try { + $this->call(gesture: $gesture, service: $this->service(missing: '', reads: $reads), caseId: 'case-orphan'); + self::fail('An unreadable case type must refuse.'); + } catch (RefusedException $e) { + self::assertSame(expected: RefusedException::STATUS_INDETERMINATE, actual: $e->getStatus()); + } + + self::assertSame(expected: [], actual: $this->written); + }//end testAnUnreadableCaseTypeIsIndeterminate() + + /** + * A case whose type asks no purpose records nothing, each gesture in its own answer shape. + * + * @return void + */ + public function testACaseThatAsksNoPurposeRecordsNothing(): void { + $reads = 0; + $service = $this->service(missing: '', reads: $reads); + + self::assertSame( + expected: [['recorded' => false, 'required' => false], ['required' => false, 'recorded' => false]], + actual: [$this->call(gesture: 'record', service: $service, caseId: 'case-2'), $this->call(gesture: 'check', service: $service, caseId: 'case-2')] + ); + self::assertSame(expected: [], actual: $this->written); + }//end testACaseThatAsksNoPurposeRecordsNothing() + + /** + * Collect an audit event the mapper would write. + * + * @param ObjectAuditEvent $event The event. + * + * @return void + */ + public function collect(ObjectAuditEvent $event): void { + $this->written[] = [$event->action, $event->context]; + }//end collect() + + /** + * Call one gesture as reader pieter, recording the handling purpose. + * + * @param string $gesture 'record' or 'check'. + * @param CaseAccessPurposeService $service The service. + * @param string $caseId The case. + * + * @return array The answer. + */ + private function call(string $gesture, CaseAccessPurposeService $service, string $caseId): array { + if ($gesture === 'record') { + return $service->record(caseId: $caseId, readerId: 'pieter', purpose: 'handling', note: ''); + } + + return $service->check(caseId: $caseId, readerId: 'pieter'); + }//end call() + + /** + * The service over an in-memory register, with one piece of the configuration missing. + * + * @param string $missing The missing piece ('' for none). + * @param int $reads Counts the object reads. + * + * @return CaseAccessPurposeService The service. + */ + private function service(string $missing, int &$reads): CaseAccessPurposeService { + $objects = [ + 'case-1' => ['title' => 'Melding Jeugdwet', 'caseType' => 'ct-jw'], + 'ct-jw' => ['title' => 'Melding Jeugdwet', 'specialDataCategories' => ['health']], + 'case-2' => ['title' => 'Dakkapel', 'caseType' => 'ct-ov'], + 'ct-ov' => ['title' => 'Omgevingsvergunning'], + 'case-orphan' => ['title' => 'Wees', 'caseType' => 'ct-gone'], + ]; + $objectService = new class($objects, $reads) { + /** + * Constructor. + * + * @param array> $objects The stored objects. + * @param int $reads Counts the reads. + */ + public function __construct(private array $objects, private int &$reads) { + }//end __construct() + + /** + * Find a stored object, or throw. + * + * @param string $id The id. + * @param string $register The register. + * @param string $schema The schema. + * + * @return object The entity. + */ + public function find(string $id, string $register, string $schema): object { + $this->reads++; + if (isset($this->objects[$id]) === false) { + throw new RuntimeException('Object not found'); + } + + $entity = new ObjectEntity(); + $entity->setUuid($id); + $entity->setObject($this->objects[$id]); + + return $entity; + }//end find() + }; + $test = $this; + $mapper = new class($test) { + /** + * Constructor. + * + * @param AccessPurposeReadGoldenTest $test Collects the events. + */ + public function __construct(private AccessPurposeReadGoldenTest $test) { + }//end __construct() + + /** + * Record an audit event. + * + * @param ObjectAuditEvent $event The event. + * + * @return AuditTrail The trail. + */ + public function recordObjectEvent(ObjectAuditEvent $event): AuditTrail { + $this->test->collect(event: $event); + + return new AuditTrail(); + }//end recordObjectEvent() + }; + $config = ['register' => 'dossiq', 'case_schema' => 'case', 'case_type_schema' => 'caseType']; + unset($config[$missing]); + $settings = $this->createMock(originalClassName: SettingsService::class); + $settings->method('getObjectService')->willReturn($missing === 'objectService' ? null : $objectService); + $settings->method('getOpenRegisterClass')->willReturn($missing === 'mapper' ? null : $mapper); + $settings->method('getConfigValue')->willReturnCallback(static fn (string $key, string $default = ''): string => ($config[$key] ?? $default)); + + return new CaseAccessPurposeService(settingsService: $settings, logger: new NullLogger(), roles: $this->createMock(originalClassName: CaseRoleHolder::class)); + }//end service() +}//end class From 198b248253bcf7c6b28401da8a675289b34c0062 Mon Sep 17 00:00:00 2001 From: Juan Claude Date: Wed, 11 Nov 2026 16:24:27 +0000 Subject: [PATCH 2/2] refactor(access-purpose): share the read of recording and checking an access purpose record() and check() repeated the same read: the object service, the audit mapper, the register and both schemas (refused as not recorded when any is missing), the case (404 when it cannot be opened) and its special data categories. Both now call one private purposeRead(), split into purposeTarget() and readableCase(); the checks still run in the same order before anything is written. Assisted-by: ClaudeCode:claude-opus-5-5 --- lib/Service/CaseAccessPurposeService.php | 108 +++++++++++++++-------- 1 file changed, 72 insertions(+), 36 deletions(-) diff --git a/lib/Service/CaseAccessPurposeService.php b/lib/Service/CaseAccessPurposeService.php index 9a26109257..d0267eed5a 100644 --- a/lib/Service/CaseAccessPurposeService.php +++ b/lib/Service/CaseAccessPurposeService.php @@ -111,24 +111,7 @@ public function record(string $caseId, string $readerId, string $purpose, string $note = trim($note); $this->assertPurpose(purpose: $purpose, note: $note); - $objectService = $this->settingsService->getObjectService(); - $mapper = $this->settingsService->getOpenRegisterClass(class: self::AUDIT_MAPPER); - $register = $this->settingsService->getConfigValue('register'); - $caseSchema = $this->settingsService->getConfigValue('case_schema'); - $caseTypeSchema = $this->settingsService->getConfigValue('case_type_schema'); - if ($objectService === null || $mapper === null || $register === '' || $caseSchema === '' || $caseTypeSchema === '') { - throw $this->notRecorded(previous: null); - } - - $case = $this->find( - objectService: $objectService, - id: $caseId, - register: $register, - schema: $caseSchema, - refusal: new RefusedException(rule: 'case-not-readable', sentence: 'This case cannot be opened.', status: 404) - ); - - $categories = $this->categoriesOf(objectService: $objectService, case: $case, register: $register, schema: $caseTypeSchema); + ['mapper' => $mapper, 'case' => $case, 'categories' => $categories] = $this->purposeRead(caseId: $caseId); if ($categories === []) { return ['recorded' => false, 'required' => false]; } @@ -164,24 +147,7 @@ public function record(string $caseId, string $readerId, string $purpose, string * @spec openspec/specs/case-access-purpose/spec.md#requirement-the-cases-own-handler-opens-it-without-the-dialog */ public function check(string $caseId, string $readerId): array { - $objectService = $this->settingsService->getObjectService(); - $mapper = $this->settingsService->getOpenRegisterClass(class: self::AUDIT_MAPPER); - $register = $this->settingsService->getConfigValue('register'); - $caseSchema = $this->settingsService->getConfigValue('case_schema'); - $caseTypeSchema = $this->settingsService->getConfigValue('case_type_schema'); - if ($objectService === null || $mapper === null || $register === '' || $caseSchema === '' || $caseTypeSchema === '') { - throw $this->notRecorded(previous: null); - } - - $case = $this->find( - objectService: $objectService, - id: $caseId, - register: $register, - schema: $caseSchema, - refusal: new RefusedException(rule: 'case-not-readable', sentence: 'This case cannot be opened.', status: 404) - ); - - $categories = $this->categoriesOf(objectService: $objectService, case: $case, register: $register, schema: $caseTypeSchema); + ['mapper' => $mapper, 'case' => $case, 'categories' => $categories] = $this->purposeRead(caseId: $caseId); if ($categories === []) { return ['required' => false, 'recorded' => false]; } @@ -456,6 +422,76 @@ private function dataOf(object $entity): array { return []; }//end dataOf() + /** + * The audit mapper, the case and its special data categories, read before a purpose is + * recorded or checked. + * + * @param string $caseId The case. + * + * @return array{mapper: object, case: object, categories: array} The three. + * + * @throws RefusedException When OpenRegister, the mapper, the register or a schema is missing, + * the case cannot be opened (404), or its type cannot be read. + */ + private function purposeRead(string $caseId): array { + $target = $this->purposeTarget(); + $case = $this->readableCase(target: $target, caseId: $caseId); + $categories = $this->categoriesOf( + objectService: $target['objectService'], + case: $case, + register: $target['register'], + schema: $target['caseTypeSchema'] + ); + + return ['mapper' => $target['mapper'], 'case' => $case, 'categories' => $categories]; + }//end purposeRead() + + /** + * The case, or the refusal that it cannot be opened. + * + * @param array{objectService: object, register: string, caseSchema: string} $target Where cases are read. + * @param string $caseId The case. + * + * @return object The case. + * + * @throws RefusedException When the case cannot be opened (404). + */ + private function readableCase(array $target, string $caseId): object { + return $this->find( + objectService: $target['objectService'], + id: $caseId, + register: $target['register'], + schema: $target['caseSchema'], + refusal: new RefusedException(rule: 'case-not-readable', sentence: 'This case cannot be opened.', status: 404) + ); + }//end readableCase() + + /** + * The object service, the audit mapper, the register and the two schemas, or the not-recorded refusal. + * + * Read in that order; any of them missing (no service or mapper, an empty register or schema) + * refuses before a case is read. + * + * @return array{objectService: object, mapper: object, register: string, caseSchema: string, caseTypeSchema: string} + * The five. + * + * @throws RefusedException When any of them is missing. + */ + private function purposeTarget(): array { + $target = [ + 'objectService' => $this->settingsService->getObjectService(), + 'mapper' => $this->settingsService->getOpenRegisterClass(class: self::AUDIT_MAPPER), + 'register' => $this->settingsService->getConfigValue('register'), + 'caseSchema' => $this->settingsService->getConfigValue('case_schema'), + 'caseTypeSchema' => $this->settingsService->getConfigValue('case_type_schema'), + ]; + if (in_array(null, $target, true) === true || in_array('', $target, true) === true) { + throw $this->notRecorded(previous: null); + } + + return $target; + }//end purposeTarget() + /** * The refusal of a purpose that could not be recorded. *