ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ReaderTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
37
38class ReaderTest extends TestCase
39{
40 protected function getVocabulary(
41 bool $active,
42 string $id = '',
43 Type $type = Type::NULL
45 return new class ($active, $type, $id) extends NullVocabulary {
46 public function __construct(
47 protected bool $active,
48 protected Type $type,
49 protected string $id
50 ) {
51 }
52
53 public function isActive(): bool
54 {
55 return $this->active;
56 }
57
58 public function type(): Type
59 {
60 return $this->type;
61 }
62
63 public function id(): string
64 {
65 return $this->id;
66 }
67 };
68 }
69
73 public function getVocabsWithIDs(bool $active, string ...$ids): array
74 {
75 $result = [];
76 foreach ($ids as $id) {
77 $result[] = $this->getVocabulary($active, $id);
78 }
79 return $result;
80 }
81
82 protected function getCopyrightBridge(?VocabularyInterface $vocabulary = null): CopyrightBridge
83 {
84 return new class ($vocabulary) extends NullCopyrightBridge {
85 public function __construct(protected ?VocabularyInterface $vocabulary)
86 {
87 }
88
89 public function vocabulary(SlotIdentifier $slot): ?VocabularyInterface
90 {
91 return $this->vocabulary;
92 }
93 };
94 }
95
96 protected function getControlledRepo(VocabularyInterface ...$vocabularies): ControlledRepo
97 {
98 return new class ($vocabularies) extends NullControlledRepo {
99 public function __construct(protected array $vocabularies)
100 {
101 }
102
103 public function getVocabulary(string $vocab_id): VocabularyInterface
104 {
105 foreach ($this->vocabularies as $vocabulary) {
106 if ($vocab_id === $vocabulary->id()) {
107 return $vocabulary;
108 }
109 }
110 return new NullVocabulary();
111 }
112
113 public function getVocabulariesForSlots(SlotIdentifier ...$slots): \Generator
114 {
115 yield from $this->vocabularies;
116 }
117
118 public function getActiveVocabulariesForSlots(SlotIdentifier ...$slots): \Generator
119 {
120 foreach ($this->vocabularies as $vocabulary) {
121 if (!$vocabulary->isActive()) {
122 continue;
123 }
124 yield $vocabulary;
125 }
126 }
127 };
128 }
129
130 protected function getStandardRepo(VocabularyInterface ...$vocabularies): StandardRepo
131 {
132 return new class ($vocabularies) extends NullStandardRepo {
133 public function __construct(protected array $vocabularies)
134 {
135 }
136
137 public function getVocabulary(SlotIdentifier $slot): VocabularyInterface
138 {
139 foreach ($this->vocabularies as $vocabulary) {
140 if ($slot->value === $vocabulary->id()) {
141 return $vocabulary;
142 }
143 }
144 return new NullVocabulary();
145 }
146
147 public function getVocabularies(SlotIdentifier ...$slots): \Generator
148 {
149 yield from $this->vocabularies;
150 }
151
152 public function getActiveVocabularies(SlotIdentifier ...$slots): \Generator
153 {
154 foreach ($this->vocabularies as $vocabulary) {
155 if (!$vocabulary->isActive()) {
156 continue;
157 }
158 yield $vocabulary;
159 }
160 }
161 };
162 }
163
168 protected function assertVocabIDsMatch(array $ids, \Generator $vocabs): void
169 {
170 $actual_ids = [];
171 foreach ($vocabs as $vocabulary) {
172 $actual_ids[] = $vocabulary->id();
173 }
174 $this->assertSame($ids, $actual_ids);
175 }
176
177 public function testVocabularyInvalidVocabID(): void
178 {
179 $active_controlled_vocabs = $this->getVocabsWithIDs(true, 'contr active 1', 'contr active 2');
180 $inactive_controlled_vocabs = $this->getVocabsWithIDs(false, 'contr inactive 1', 'contr inactive 2');
181 $active_standard_vocabs = $this->getVocabsWithIDs(true, SlotIdentifier::GENERAL_COVERAGE->value);
182 $inactive_standard_vocabs = $this->getVocabsWithIDs(false, SlotIdentifier::RIGHTS_COST->value);
183
184 $reader = new Reader(
185 $this->getCopyrightBridge(),
186 $this->getControlledRepo(...$active_controlled_vocabs, ...$inactive_controlled_vocabs),
187 $this->getStandardRepo(...$active_standard_vocabs, ...$inactive_standard_vocabs)
188 );
189
190 $vocab = $reader->vocabulary('something entirely different');
191
192 $this->assertInstanceOf(NullVocabulary::class, $vocab);
193 }
194
196 {
197 $active_controlled_vocabs = $this->getVocabsWithIDs(true, 'contr active 1', 'contr active 2');
198 $inactive_controlled_vocabs = $this->getVocabsWithIDs(false, 'contr inactive 1', 'contr inactive 2');
199 $active_standard_vocabs = $this->getVocabsWithIDs(true, SlotIdentifier::GENERAL_COVERAGE->value);
200 $inactive_standard_vocabs = $this->getVocabsWithIDs(false, SlotIdentifier::RIGHTS_COST->value);
201
202 $reader = new Reader(
203 $this->getCopyrightBridge(),
204 $this->getControlledRepo(...$active_controlled_vocabs, ...$inactive_controlled_vocabs),
205 $this->getStandardRepo(...$active_standard_vocabs, ...$inactive_standard_vocabs)
206 );
207
208 $vocab = $reader->vocabulary(SlotIdentifier::RIGHTS_COST->value);
209
210 $this->assertSame(SlotIdentifier::RIGHTS_COST->value, $vocab->id());
211 }
213 {
214 $cp_vocab = $this->getVocabulary(true, SlotIdentifier::RIGHTS_DESCRIPTION->value);
215 $active_controlled_vocabs = $this->getVocabsWithIDs(true, 'contr active 1', 'contr active 2');
216 $inactive_controlled_vocabs = $this->getVocabsWithIDs(false, 'contr inactive 1', 'contr inactive 2');
217 $active_standard_vocabs = $this->getVocabsWithIDs(true, SlotIdentifier::GENERAL_COVERAGE->value);
218 $inactive_standard_vocabs = $this->getVocabsWithIDs(false, SlotIdentifier::RIGHTS_COST->value);
219
220 $reader = new Reader(
221 $this->getCopyrightBridge($cp_vocab),
222 $this->getControlledRepo(...$active_controlled_vocabs, ...$inactive_controlled_vocabs),
223 $this->getStandardRepo(...$active_standard_vocabs, ...$inactive_standard_vocabs)
224 );
225
226 $vocab = $reader->vocabulary(SlotIdentifier::RIGHTS_DESCRIPTION->value);
227
228 $this->assertSame(SlotIdentifier::RIGHTS_DESCRIPTION->value, $vocab->id());
229 }
230
232 {
233 $active_controlled_vocabs = $this->getVocabsWithIDs(true, 'contr active 1', 'contr active 2');
234 $inactive_controlled_vocabs = $this->getVocabsWithIDs(false, 'contr inactive 1', 'contr inactive 2');
235 $active_standard_vocabs = $this->getVocabsWithIDs(true, SlotIdentifier::GENERAL_COVERAGE->value);
236 $inactive_standard_vocabs = $this->getVocabsWithIDs(false, SlotIdentifier::RIGHTS_COST->value);
237
238 $reader = new Reader(
239 $this->getCopyrightBridge(),
240 $this->getControlledRepo(...$active_controlled_vocabs, ...$inactive_controlled_vocabs),
241 $this->getStandardRepo(...$active_standard_vocabs, ...$inactive_standard_vocabs)
242 );
243
244 $vocab = $reader->vocabulary('contr active 1');
245
246 $this->assertSame('contr active 1', $vocab->id());
247 }
248
249 public function testVocabulariesForSlots(): void
250 {
251 $cp_vocab = $this->getVocabulary(true, 'cp');
252 $active_controlled_vocabs = $this->getVocabsWithIDs(true, 'contr active 1', 'contr active 2');
253 $inactive_controlled_vocabs = $this->getVocabsWithIDs(false, 'contr inactive 1', 'contr inactive 2');
254 $active_standard_vocabs = $this->getVocabsWithIDs(true, 'stand active 1', 'stand active 2');
255 $inactive_standard_vocabs = $this->getVocabsWithIDs(false, 'stand inactive 1', 'stand inactive 2');
256
257 $reader = new Reader(
258 $this->getCopyrightBridge($cp_vocab),
259 $this->getControlledRepo(...$active_controlled_vocabs, ...$inactive_controlled_vocabs),
260 $this->getStandardRepo(...$active_standard_vocabs, ...$inactive_standard_vocabs)
261 );
262
263 $this->assertVocabIDsMatch(
264 [
265 'cp',
266 'contr active 1',
267 'contr active 2',
268 'contr inactive 1',
269 'contr inactive 2',
270 'stand active 1',
271 'stand active 2',
272 'stand inactive 1',
273 'stand inactive 2'
274 ],
275 $reader->vocabulariesForSlots(SlotIdentifier::RIGHTS_COST)
276 );
277 }
278
280 {
281 $active_controlled_vocabs = $this->getVocabsWithIDs(true, 'contr active 1', 'contr active 2');
282 $inactive_controlled_vocabs = $this->getVocabsWithIDs(false, 'contr inactive 1', 'contr inactive 2');
283 $active_standard_vocabs = $this->getVocabsWithIDs(true, 'stand active 1', 'stand active 2');
284 $inactive_standard_vocabs = $this->getVocabsWithIDs(false, 'stand inactive 1', 'stand inactive 2');
285
286 $reader = new Reader(
287 $this->getCopyrightBridge(),
288 $this->getControlledRepo(...$active_controlled_vocabs, ...$inactive_controlled_vocabs),
289 $this->getStandardRepo(...$active_standard_vocabs, ...$inactive_standard_vocabs)
290 );
291
292 $this->assertVocabIDsMatch(
293 [
294 'contr active 1',
295 'contr active 2',
296 'contr inactive 1',
297 'contr inactive 2',
298 'stand active 1',
299 'stand active 2',
300 'stand inactive 1',
301 'stand inactive 2'
302 ],
303 $reader->vocabulariesForSlots(SlotIdentifier::RIGHTS_COST)
304 );
305 }
306
307 public function testActiveVocabulariesForSlots(): void
308 {
309 $cp_vocab = $this->getVocabulary(true, 'cp');
310 $active_controlled_vocabs = $this->getVocabsWithIDs(true, 'contr active 1', 'contr active 2');
311 $inactive_controlled_vocabs = $this->getVocabsWithIDs(false, 'contr inactive 1', 'contr inactive 2');
312 $active_standard_vocabs = $this->getVocabsWithIDs(true, 'stand active 1', 'stand active 2');
313 $inactive_standard_vocabs = $this->getVocabsWithIDs(false, 'stand inactive 1', 'stand inactive 2');
314
315 $reader = new Reader(
316 $this->getCopyrightBridge($cp_vocab),
317 $this->getControlledRepo(...$active_controlled_vocabs, ...$inactive_controlled_vocabs),
318 $this->getStandardRepo(...$active_standard_vocabs, ...$inactive_standard_vocabs)
319 );
320
321 $this->assertVocabIDsMatch(
322 [
323 'cp',
324 'contr active 1',
325 'contr active 2',
326 'stand active 1',
327 'stand active 2'
328 ],
329 $reader->activeVocabulariesForSlots(SlotIdentifier::RIGHTS_COST)
330 );
331 }
332
334 {
335 $active_controlled_vocabs = $this->getVocabsWithIDs(true, 'contr active 1', 'contr active 2');
336 $inactive_controlled_vocabs = $this->getVocabsWithIDs(false, 'contr inactive 1', 'contr inactive 2');
337 $active_standard_vocabs = $this->getVocabsWithIDs(true, 'stand active 1', 'stand active 2');
338 $inactive_standard_vocabs = $this->getVocabsWithIDs(false, 'stand inactive 1', 'stand inactive 2');
339
340 $reader = new Reader(
341 $this->getCopyrightBridge(),
342 $this->getControlledRepo(...$active_controlled_vocabs, ...$inactive_controlled_vocabs),
343 $this->getStandardRepo(...$active_standard_vocabs, ...$inactive_standard_vocabs)
344 );
345
346 $this->assertVocabIDsMatch(
347 [
348 'contr active 1',
349 'contr active 2',
350 'stand active 1',
351 'stand active 2'
352 ],
353 $reader->activeVocabulariesForSlots(SlotIdentifier::RIGHTS_COST)
354 );
355 }
356
358 {
359 $cp_vocab = $this->getVocabulary(false, 'cp');
360 $active_controlled_vocabs = $this->getVocabsWithIDs(true, 'contr active 1', 'contr active 2');
361 $inactive_controlled_vocabs = $this->getVocabsWithIDs(false, 'contr inactive 1', 'contr inactive 2');
362 $active_standard_vocabs = $this->getVocabsWithIDs(true, 'stand active 1', 'stand active 2');
363 $inactive_standard_vocabs = $this->getVocabsWithIDs(false, 'stand inactive 1', 'stand inactive 2');
364
365 $reader = new Reader(
366 $this->getCopyrightBridge($cp_vocab),
367 $this->getControlledRepo(...$active_controlled_vocabs, ...$inactive_controlled_vocabs),
368 $this->getStandardRepo(...$active_standard_vocabs, ...$inactive_standard_vocabs)
369 );
370
371 $this->assertVocabIDsMatch(
372 [
373 'contr active 1',
374 'contr active 2',
375 'stand active 1',
376 'stand active 2'
377 ],
378 $reader->activeVocabulariesForSlots(SlotIdentifier::RIGHTS_COST)
379 );
380 }
381}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$vocab
getVocabsWithIDs(bool $active, string ... $ids)
Definition: ReaderTest.php:73
getStandardRepo(VocabularyInterface ... $vocabularies)
Definition: ReaderTest.php:130
getVocabulary(bool $active, string $id='', Type $type=Type::NULL)
Definition: ReaderTest.php:40
getControlledRepo(VocabularyInterface ... $vocabularies)
Definition: ReaderTest.php:96
getCopyrightBridge(?VocabularyInterface $vocabulary=null)
Definition: ReaderTest.php:82
assertVocabIDsMatch(array $ids, \Generator $vocabs)
Definition: ReaderTest.php:168
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc