Skip to content

Commit 230dc67

Browse files
authored
Merge pull request #5896 from ConductionNL/quality/h16-purpose-read
[access] refactor(access-purpose): share the read of recording and checking an access purpose
2 parents ef9a084 + 198b248 commit 230dc67

2 files changed

Lines changed: 328 additions & 36 deletions

File tree

‎lib/Service/CaseAccessPurposeService.php‎

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,7 @@ public function record(string $caseId, string $readerId, string $purpose, string
111111
$note = trim($note);
112112
$this->assertPurpose(purpose: $purpose, note: $note);
113113

114-
$objectService = $this->settingsService->getObjectService();
115-
$mapper = $this->settingsService->getOpenRegisterClass(class: self::AUDIT_MAPPER);
116-
$register = $this->settingsService->getConfigValue('register');
117-
$caseSchema = $this->settingsService->getConfigValue('case_schema');
118-
$caseTypeSchema = $this->settingsService->getConfigValue('case_type_schema');
119-
if ($objectService === null || $mapper === null || $register === '' || $caseSchema === '' || $caseTypeSchema === '') {
120-
throw $this->notRecorded(previous: null);
121-
}
122-
123-
$case = $this->find(
124-
objectService: $objectService,
125-
id: $caseId,
126-
register: $register,
127-
schema: $caseSchema,
128-
refusal: new RefusedException(rule: 'case-not-readable', sentence: 'This case cannot be opened.', status: 404)
129-
);
130-
131-
$categories = $this->categoriesOf(objectService: $objectService, case: $case, register: $register, schema: $caseTypeSchema);
114+
['mapper' => $mapper, 'case' => $case, 'categories' => $categories] = $this->purposeRead(caseId: $caseId);
132115
if ($categories === []) {
133116
return ['recorded' => false, 'required' => false];
134117
}
@@ -164,24 +147,7 @@ public function record(string $caseId, string $readerId, string $purpose, string
164147
* @spec openspec/specs/case-access-purpose/spec.md#requirement-the-cases-own-handler-opens-it-without-the-dialog
165148
*/
166149
public function check(string $caseId, string $readerId): array {
167-
$objectService = $this->settingsService->getObjectService();
168-
$mapper = $this->settingsService->getOpenRegisterClass(class: self::AUDIT_MAPPER);
169-
$register = $this->settingsService->getConfigValue('register');
170-
$caseSchema = $this->settingsService->getConfigValue('case_schema');
171-
$caseTypeSchema = $this->settingsService->getConfigValue('case_type_schema');
172-
if ($objectService === null || $mapper === null || $register === '' || $caseSchema === '' || $caseTypeSchema === '') {
173-
throw $this->notRecorded(previous: null);
174-
}
175-
176-
$case = $this->find(
177-
objectService: $objectService,
178-
id: $caseId,
179-
register: $register,
180-
schema: $caseSchema,
181-
refusal: new RefusedException(rule: 'case-not-readable', sentence: 'This case cannot be opened.', status: 404)
182-
);
183-
184-
$categories = $this->categoriesOf(objectService: $objectService, case: $case, register: $register, schema: $caseTypeSchema);
150+
['mapper' => $mapper, 'case' => $case, 'categories' => $categories] = $this->purposeRead(caseId: $caseId);
185151
if ($categories === []) {
186152
return ['required' => false, 'recorded' => false];
187153
}
@@ -456,6 +422,76 @@ private function dataOf(object $entity): array {
456422
return [];
457423
}//end dataOf()
458424

425+
/**
426+
* The audit mapper, the case and its special data categories, read before a purpose is
427+
* recorded or checked.
428+
*
429+
* @param string $caseId The case.
430+
*
431+
* @return array{mapper: object, case: object, categories: array<int, string>} The three.
432+
*
433+
* @throws RefusedException When OpenRegister, the mapper, the register or a schema is missing,
434+
* the case cannot be opened (404), or its type cannot be read.
435+
*/
436+
private function purposeRead(string $caseId): array {
437+
$target = $this->purposeTarget();
438+
$case = $this->readableCase(target: $target, caseId: $caseId);
439+
$categories = $this->categoriesOf(
440+
objectService: $target['objectService'],
441+
case: $case,
442+
register: $target['register'],
443+
schema: $target['caseTypeSchema']
444+
);
445+
446+
return ['mapper' => $target['mapper'], 'case' => $case, 'categories' => $categories];
447+
}//end purposeRead()
448+
449+
/**
450+
* The case, or the refusal that it cannot be opened.
451+
*
452+
* @param array{objectService: object, register: string, caseSchema: string} $target Where cases are read.
453+
* @param string $caseId The case.
454+
*
455+
* @return object The case.
456+
*
457+
* @throws RefusedException When the case cannot be opened (404).
458+
*/
459+
private function readableCase(array $target, string $caseId): object {
460+
return $this->find(
461+
objectService: $target['objectService'],
462+
id: $caseId,
463+
register: $target['register'],
464+
schema: $target['caseSchema'],
465+
refusal: new RefusedException(rule: 'case-not-readable', sentence: 'This case cannot be opened.', status: 404)
466+
);
467+
}//end readableCase()
468+
469+
/**
470+
* The object service, the audit mapper, the register and the two schemas, or the not-recorded refusal.
471+
*
472+
* Read in that order; any of them missing (no service or mapper, an empty register or schema)
473+
* refuses before a case is read.
474+
*
475+
* @return array{objectService: object, mapper: object, register: string, caseSchema: string, caseTypeSchema: string}
476+
* The five.
477+
*
478+
* @throws RefusedException When any of them is missing.
479+
*/
480+
private function purposeTarget(): array {
481+
$target = [
482+
'objectService' => $this->settingsService->getObjectService(),
483+
'mapper' => $this->settingsService->getOpenRegisterClass(class: self::AUDIT_MAPPER),
484+
'register' => $this->settingsService->getConfigValue('register'),
485+
'caseSchema' => $this->settingsService->getConfigValue('case_schema'),
486+
'caseTypeSchema' => $this->settingsService->getConfigValue('case_type_schema'),
487+
];
488+
if (in_array(null, $target, true) === true || in_array('', $target, true) === true) {
489+
throw $this->notRecorded(previous: null);
490+
}
491+
492+
return $target;
493+
}//end purposeTarget()
494+
459495
/**
460496
* The refusal of a purpose that could not be recorded.
461497
*
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
<?php
2+
3+
/**
4+
* What recording and checking an access purpose read before they write.
5+
*
6+
* Golden over CaseAccessPurposeService::record() and check() before they
7+
* share their read: without OpenRegister, the audit mapper, the register or
8+
* a schema, both refuse as not recorded and read nothing; a case the reader
9+
* cannot open is refused with 404; an unreadable case type refuses as
10+
* indeterminate; a case whose type asks no purpose answers that nothing was
11+
* recorded, each in its own answer shape. Nothing is written in any of these.
12+
*
13+
* @category Test
14+
* @package OCA\Dossiq\Tests\Unit\Service
15+
*
16+
* @author Conduction Development Team <info@conduction.nl>
17+
* @copyright 2026 Conduction B.V.
18+
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
19+
*
20+
* SPDX-License-Identifier: EUPL-1.2
21+
* SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl>
22+
*
23+
* @link https://conduction.nl
24+
*
25+
* @spec openspec/specs/case-access-purpose/spec.md#requirement-opening-a-sensitive-case-records-its-purpose
26+
*/
27+
28+
declare(strict_types=1);
29+
30+
namespace OCA\Dossiq\Tests\Unit\Service;
31+
32+
use OCA\Dossiq\Exception\RefusedException;
33+
use OCA\Dossiq\Service\CaseAccessPurposeService;
34+
use OCA\Dossiq\Service\People\CaseRoleHolder;
35+
use OCA\Dossiq\Service\SettingsService;
36+
use OCA\OpenRegister\Db\AuditTrail;
37+
use OCA\OpenRegister\Db\AuditTrail\ObjectAuditEvent;
38+
use OCA\OpenRegister\Db\ObjectEntity;
39+
use PHPUnit\Framework\TestCase;
40+
use Psr\Log\NullLogger;
41+
use RuntimeException;
42+
43+
/**
44+
* The shared read of record() and check(), and its refusals.
45+
*
46+
* @covers \OCA\Dossiq\Service\CaseAccessPurposeService
47+
*
48+
* @spec openspec/specs/case-access-purpose/spec.md#requirement-opening-a-sensitive-case-records-its-purpose
49+
*/
50+
class AccessPurposeReadGoldenTest extends TestCase {
51+
/**
52+
* The audit events written.
53+
*
54+
* @var array<int, array<int, mixed>>
55+
*/
56+
private array $written = [];
57+
58+
/**
59+
* The two gestures, called with a case id.
60+
*
61+
* @return array<string, array{0: string}>
62+
*/
63+
public static function gestures(): array {
64+
return ['record' => ['record'], 'check' => ['check']];
65+
}//end gestures()
66+
67+
/**
68+
* Without a piece of the configuration both refuse as not recorded, and no case is read.
69+
*
70+
* @param string $gesture The gesture.
71+
*
72+
* @dataProvider gestures
73+
*
74+
* @return void
75+
*/
76+
public function testAnUnconfiguredInstanceRefusesBeforeReading(string $gesture): void {
77+
foreach (['objectService', 'mapper', 'register', 'case_schema', 'case_type_schema'] as $missing) {
78+
$reads = 0;
79+
try {
80+
$this->call(gesture: $gesture, service: $this->service(missing: $missing, reads: $reads), caseId: 'case-1');
81+
self::fail('An unconfigured ' . $missing . ' must refuse.');
82+
} catch (RefusedException $e) {
83+
self::assertSame(expected: ['access-purpose-not-recorded', RefusedException::STATUS_INDETERMINATE, 0], actual: [$e->getRule(), $e->getStatus(), $reads]);
84+
}
85+
}
86+
87+
self::assertSame(expected: [], actual: $this->written);
88+
}//end testAnUnconfiguredInstanceRefusesBeforeReading()
89+
90+
/**
91+
* A case the reader cannot open is refused with 404.
92+
*
93+
* @param string $gesture The gesture.
94+
*
95+
* @dataProvider gestures
96+
*
97+
* @return void
98+
*/
99+
public function testAnUnreadableCaseIsNotFound(string $gesture): void {
100+
$reads = 0;
101+
try {
102+
$this->call(gesture: $gesture, service: $this->service(missing: '', reads: $reads), caseId: 'case-hidden');
103+
self::fail('An unreadable case must be refused.');
104+
} catch (RefusedException $e) {
105+
self::assertSame(expected: ['case-not-readable', 404, 1], actual: [$e->getRule(), $e->getStatus(), $reads]);
106+
}
107+
108+
self::assertSame(expected: [], actual: $this->written);
109+
}//end testAnUnreadableCaseIsNotFound()
110+
111+
/**
112+
* An unreadable case type refuses as indeterminate.
113+
*
114+
* @param string $gesture The gesture.
115+
*
116+
* @dataProvider gestures
117+
*
118+
* @return void
119+
*/
120+
public function testAnUnreadableCaseTypeIsIndeterminate(string $gesture): void {
121+
$reads = 0;
122+
try {
123+
$this->call(gesture: $gesture, service: $this->service(missing: '', reads: $reads), caseId: 'case-orphan');
124+
self::fail('An unreadable case type must refuse.');
125+
} catch (RefusedException $e) {
126+
self::assertSame(expected: RefusedException::STATUS_INDETERMINATE, actual: $e->getStatus());
127+
}
128+
129+
self::assertSame(expected: [], actual: $this->written);
130+
}//end testAnUnreadableCaseTypeIsIndeterminate()
131+
132+
/**
133+
* A case whose type asks no purpose records nothing, each gesture in its own answer shape.
134+
*
135+
* @return void
136+
*/
137+
public function testACaseThatAsksNoPurposeRecordsNothing(): void {
138+
$reads = 0;
139+
$service = $this->service(missing: '', reads: $reads);
140+
141+
self::assertSame(
142+
expected: [['recorded' => false, 'required' => false], ['required' => false, 'recorded' => false]],
143+
actual: [$this->call(gesture: 'record', service: $service, caseId: 'case-2'), $this->call(gesture: 'check', service: $service, caseId: 'case-2')]
144+
);
145+
self::assertSame(expected: [], actual: $this->written);
146+
}//end testACaseThatAsksNoPurposeRecordsNothing()
147+
148+
/**
149+
* Collect an audit event the mapper would write.
150+
*
151+
* @param ObjectAuditEvent $event The event.
152+
*
153+
* @return void
154+
*/
155+
public function collect(ObjectAuditEvent $event): void {
156+
$this->written[] = [$event->action, $event->context];
157+
}//end collect()
158+
159+
/**
160+
* Call one gesture as reader pieter, recording the handling purpose.
161+
*
162+
* @param string $gesture 'record' or 'check'.
163+
* @param CaseAccessPurposeService $service The service.
164+
* @param string $caseId The case.
165+
*
166+
* @return array<string, mixed> The answer.
167+
*/
168+
private function call(string $gesture, CaseAccessPurposeService $service, string $caseId): array {
169+
if ($gesture === 'record') {
170+
return $service->record(caseId: $caseId, readerId: 'pieter', purpose: 'handling', note: '');
171+
}
172+
173+
return $service->check(caseId: $caseId, readerId: 'pieter');
174+
}//end call()
175+
176+
/**
177+
* The service over an in-memory register, with one piece of the configuration missing.
178+
*
179+
* @param string $missing The missing piece ('' for none).
180+
* @param int $reads Counts the object reads.
181+
*
182+
* @return CaseAccessPurposeService The service.
183+
*/
184+
private function service(string $missing, int &$reads): CaseAccessPurposeService {
185+
$objects = [
186+
'case-1' => ['title' => 'Melding Jeugdwet', 'caseType' => 'ct-jw'],
187+
'ct-jw' => ['title' => 'Melding Jeugdwet', 'specialDataCategories' => ['health']],
188+
'case-2' => ['title' => 'Dakkapel', 'caseType' => 'ct-ov'],
189+
'ct-ov' => ['title' => 'Omgevingsvergunning'],
190+
'case-orphan' => ['title' => 'Wees', 'caseType' => 'ct-gone'],
191+
];
192+
$objectService = new class($objects, $reads) {
193+
/**
194+
* Constructor.
195+
*
196+
* @param array<string, array<string, mixed>> $objects The stored objects.
197+
* @param int $reads Counts the reads.
198+
*/
199+
public function __construct(private array $objects, private int &$reads) {
200+
}//end __construct()
201+
202+
/**
203+
* Find a stored object, or throw.
204+
*
205+
* @param string $id The id.
206+
* @param string $register The register.
207+
* @param string $schema The schema.
208+
*
209+
* @return object The entity.
210+
*/
211+
public function find(string $id, string $register, string $schema): object {
212+
$this->reads++;
213+
if (isset($this->objects[$id]) === false) {
214+
throw new RuntimeException('Object not found');
215+
}
216+
217+
$entity = new ObjectEntity();
218+
$entity->setUuid($id);
219+
$entity->setObject($this->objects[$id]);
220+
221+
return $entity;
222+
}//end find()
223+
};
224+
$test = $this;
225+
$mapper = new class($test) {
226+
/**
227+
* Constructor.
228+
*
229+
* @param AccessPurposeReadGoldenTest $test Collects the events.
230+
*/
231+
public function __construct(private AccessPurposeReadGoldenTest $test) {
232+
}//end __construct()
233+
234+
/**
235+
* Record an audit event.
236+
*
237+
* @param ObjectAuditEvent $event The event.
238+
*
239+
* @return AuditTrail The trail.
240+
*/
241+
public function recordObjectEvent(ObjectAuditEvent $event): AuditTrail {
242+
$this->test->collect(event: $event);
243+
244+
return new AuditTrail();
245+
}//end recordObjectEvent()
246+
};
247+
$config = ['register' => 'dossiq', 'case_schema' => 'case', 'case_type_schema' => 'caseType'];
248+
unset($config[$missing]);
249+
$settings = $this->createMock(originalClassName: SettingsService::class);
250+
$settings->method('getObjectService')->willReturn($missing === 'objectService' ? null : $objectService);
251+
$settings->method('getOpenRegisterClass')->willReturn($missing === 'mapper' ? null : $mapper);
252+
$settings->method('getConfigValue')->willReturnCallback(static fn (string $key, string $default = ''): string => ($config[$key] ?? $default));
253+
254+
return new CaseAccessPurposeService(settingsService: $settings, logger: new NullLogger(), roles: $this->createMock(originalClassName: CaseRoleHolder::class));
255+
}//end service()
256+
}//end class

0 commit comments

Comments
 (0)