|
| 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