This repository was archived by the owner on May 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationVersionLifecycleSchemaTest.php
More file actions
236 lines (213 loc) · 8.33 KB
/
Copy pathApplicationVersionLifecycleSchemaTest.php
File metadata and controls
236 lines (213 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* Unit test for the ApplicationVersion lifecycle declared in
* `lib/Settings/openbuilt_register.json` (openbuilt#10 task 5.2).
*
* Per ADR-031 (schema-declarative business logic), the
* `applicationVersion` schema carries an `x-openregister-lifecycle`
* block describing the `draft → published → archived → draft (reopen)`
* state machine. OR's TransitionEngine executes the transitions at
* runtime; this test asserts the *contract* is well-formed:
*
* - initial state is `draft`
* - the three named states are exactly draft/published/archived
* - the three transitions are publish (draft→published),
* archive (published→archived), reopen (archived→draft)
* - the publish transition declares an `upsert_relation` action
* targeting `openbuilt/built-app-route` (BuiltAppRoute upkeep)
*
* A real end-to-end transition test requires booted Nextcloud +
* OpenRegister with a Postgres / MySQL backend (see openbuilt#10
* task 5.2 note "Requires container-bound NC bootstrap"). That
* integration scope is tracked separately; this test guards the
* declarative contract that anchors it.
*
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Test
* @package OCA\OpenBuilt\Tests\Unit
*
* @author Conduction Development Team <dev@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @version GIT: <git-id>
*
* @link https://conduction.nl
*/
declare(strict_types=1);
namespace OCA\OpenBuilt\Tests\Unit;
use PHPUnit\Framework\TestCase;
/**
* Tests the declarative lifecycle of ApplicationVersion (openbuilt#10).
*/
class ApplicationVersionLifecycleSchemaTest extends TestCase
{
/**
* Decoded register-seed payload, lazily loaded.
*
* @var array<string, mixed>|null
*/
private static ?array $registerSeed = null;
/**
* Load + cache the canonical register seed.
*
* @return array<string, mixed>
*/
private function registerSeed(): array
{
if (self::$registerSeed === null) {
$path = __DIR__.'/../../lib/Settings/openbuilt_register.json';
self::assertFileExists($path, 'register seed file must be present');
$raw = file_get_contents($path);
$decoded = json_decode($raw, true);
self::assertIsArray($decoded, 'register seed must be a JSON object');
self::$registerSeed = $decoded;
}
return self::$registerSeed;
}//end registerSeed()
/**
* Pull the ApplicationVersion schema block out of the register seed.
*
* @return array<string, mixed>
*/
private function applicationVersionSchema(): array
{
$seed = $this->registerSeed();
// The seed is OpenAPI-shaped; schemas live under components.schemas
// and the version schema is keyed `ApplicationVersion` (PascalCase).
self::assertArrayHasKey('components', $seed);
self::assertArrayHasKey('schemas', $seed['components']);
$schemas = $seed['components']['schemas'];
self::assertIsArray($schemas);
self::assertArrayHasKey(
'ApplicationVersion',
$schemas,
'register seed must define an ApplicationVersion schema'
);
$schema = $schemas['ApplicationVersion'];
self::assertIsArray($schema);
return $schema;
}//end applicationVersionSchema()
/**
* Pull out the lifecycle declaration.
*
* @return array<string, mixed>
*/
private function lifecycle(): array
{
$schema = $this->applicationVersionSchema();
// `x-openregister-lifecycle` sits at the schema level (sibling
// of `properties`), not inside the properties map.
self::assertArrayHasKey(
'x-openregister-lifecycle',
$schema,
'ApplicationVersion must declare x-openregister-lifecycle'
);
$lifecycle = $schema['x-openregister-lifecycle'];
self::assertIsArray($lifecycle, 'lifecycle block must be an object');
return $lifecycle;
}//end lifecycle()
/**
* REQ-OBV-LC-1 — initial state is draft.
*
* @return void
*/
public function testInitialStateIsDraft(): void
{
$lifecycle = $this->lifecycle();
self::assertSame('status', $lifecycle['field'] ?? null);
self::assertSame('draft', $lifecycle['initial'] ?? null);
}//end testInitialStateIsDraft()
/**
* REQ-OBV-LC-2 — three named states exist: draft, published, archived.
*
* @return void
*/
public function testStateSetIsDraftPublishedArchived(): void
{
$lifecycle = $this->lifecycle();
self::assertArrayHasKey('states', $lifecycle);
self::assertIsArray($lifecycle['states']);
$expected = ['draft', 'published', 'archived'];
$actual = array_keys($lifecycle['states']);
sort($expected);
sort($actual);
self::assertSame($expected, $actual);
}//end testStateSetIsDraftPublishedArchived()
/**
* REQ-OBV-LC-3 — three transitions: publish, archive, reopen.
*
* @return void
*/
public function testThreeTransitionsAreDeclared(): void
{
$lifecycle = $this->lifecycle();
$transitions = ($lifecycle['transitions'] ?? []);
self::assertIsArray($transitions);
self::assertCount(3, $transitions, 'expected exactly 3 transitions (publish/archive/reopen)');
$byName = [];
foreach ($transitions as $t) {
self::assertIsArray($t);
self::assertArrayHasKey('name', $t);
$byName[$t['name']] = $t;
}
self::assertSame(['publish', 'archive', 'reopen'], array_keys($byName));
self::assertSame('draft', $byName['publish']['from']);
self::assertSame('published', $byName['publish']['to']);
self::assertSame('published', $byName['archive']['from']);
self::assertSame('archived', $byName['archive']['to']);
self::assertSame('archived', $byName['reopen']['from']);
self::assertSame('draft', $byName['reopen']['to']);
}//end testThreeTransitionsAreDeclared()
/**
* REQ-OBV-LC-4 — publish fires the upsert_relation that keeps
* BuiltAppRoute in sync. This is the declarative replacement for
* the old ApplicationVersionSnapshotListener (per ADR-031).
*
* @return void
*/
public function testPublishUpsertsBuiltAppRoute(): void
{
$lifecycle = $this->lifecycle();
$transition = null;
foreach ($lifecycle['transitions'] as $t) {
if (($t['name'] ?? null) === 'publish') {
$transition = $t;
break;
}
}
self::assertIsArray($transition, 'publish transition must exist');
self::assertArrayHasKey('on_transition', $transition);
self::assertArrayHasKey('upsert_relation', $transition['on_transition']);
$upsert = $transition['on_transition']['upsert_relation'];
self::assertSame('openbuilt/built-app-route', $upsert['schema'] ?? null);
// The slug-keyed match ensures the route survives republishes
// (one row per Application slug).
self::assertArrayHasKey('match', $upsert);
self::assertArrayHasKey('slug', $upsert['match']);
self::assertArrayHasKey('payload', $upsert);
self::assertArrayHasKey('slug', $upsert['payload']);
self::assertArrayHasKey('applicationUuid', $upsert['payload']);
}//end testPublishUpsertsBuiltAppRoute()
/**
* Sanity guard — a disallowed transition (e.g. draft → archived
* directly) is NOT declared. OR's TransitionEngine rejects undefined
* transitions; the test catches accidental schema drift that would
* widen the state machine.
*
* @return void
*/
public function testDisallowedTransitionIsAbsent(): void
{
$lifecycle = $this->lifecycle();
$pairs = array_map(
static fn (array $t): string => sprintf('%s->%s', ($t['from'] ?? '?'), ($t['to'] ?? '?')),
$lifecycle['transitions']
);
self::assertNotContains('draft->archived', $pairs);
self::assertNotContains('published->draft', $pairs);
self::assertNotContains('archived->published', $pairs);
}//end testDisallowedTransitionIsAbsent()
}//end class