ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ImporterAndResultTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
36
37class ImporterAndResultTest extends TestCase
38{
39 protected function getRepo(
40 bool $config_error = false,
41 string ...$already_existing_values
42 ): ControlledVocabsRepository {
43 return new class ($config_error, $already_existing_values) extends NullRepository {
44 public array $created_vocabs = [];
45 public array $created_values = [];
46
47 public function __construct(
48 protected bool $config_error,
49 protected array $already_existing_values
50 ) {
51 }
52
53 public function create(
54 SlotIdentifier $slot,
55 string $source
56 ): string {
57 if ($this->config_error) {
58 throw new \ilMDVocabulariesException('config error');
59 }
60 $new_id = 'new id ' . count($this->created_vocabs);
61 $this->created_vocabs[] = [
62 'id' => $new_id,
63 'slot' => $slot,
64 'source' => $source
65 ];
66 return $new_id;
67 }
68
69 public function findAlreadyExistingValues(
70 SlotIdentifier $slot,
71 string ...$values
72 ): \Generator {
73 yield from array_intersect($this->already_existing_values, $values);
74 }
75
76 public function addValueToVocabulary(
77 string $vocab_id,
78 string $value,
79 string $label = ''
80 ): void {
81 $this->created_values[] = [
82 'vocab id' => $vocab_id,
83 'value' => $value,
84 'label' => $label
85 ];
86 }
87 };
88 }
89
90 protected function getPathFactory(): PathFactory
91 {
92 return new class () extends NullFactory {
93 public function custom(): BuilderInterface
94 {
95 return new class () extends NullBuilder {
96 protected $path_string = '';
97
98 public function withNextStep(string $name, bool $add_as_first = false): BuilderInterface
99 {
100 $clone = clone $this;
101 $clone->path_string .= $name . ';';
102 return $clone;
103 }
104
105 public function withNextStepToSuperElement(bool $add_as_first = false): BuilderInterface
106 {
107 $clone = clone $this;
108 $clone->path_string .= 'SUPER;';
109 return $clone;
110 }
111
112 public function withRelative(bool $is_relative): BuilderInterface
113 {
114 $clone = clone $this;
115 if ($is_relative) {
116 $clone->path_string = 'RELATIVE!' . $this->path_string;
117 }
118 return $clone;
119 }
120
121 public function get(): PathInterface
122 {
123 if (str_contains($this->path_string, 'INVALID')) {
124 throw new \ilMDPathException('path invalid');
125 }
126 return new class ($this->path_string) extends NullPath {
127 public function __construct(protected string $path_string)
128 {
129 }
130
131 public function toString(): string
132 {
133 return $this->path_string;
134 }
135 };
136 }
137 };
138 }
139 };
140 }
141
142 protected function getSlotHandler(bool $invalid_slot = false): SlotHandler
143 {
144 return new class ($invalid_slot) extends NullSlotHandler {
145 public function __construct(protected bool $invalid_slot)
146 {
147 }
148
149 public array $exposed_paths_and_conditions = [];
150
151 public function identiferFromPathAndCondition(
152 PathInterface $path_to_element,
153 ?PathInterface $path_to_condition,
154 ?string $condition_value
155 ): SlotIdentifier {
156 if ($this->invalid_slot) {
158 }
159 $this->exposed_paths_and_conditions[] = [
160 'path' => $path_to_element->toString(),
161 'condition path' => $path_to_condition?->toString(),
162 'condition value' => $condition_value
163 ];
164 return SlotIdentifier::EDUCATIONAL_CONTEXT;
165 }
166 };
167 }
168
169 public function testImportMalformedXMLError(): void
170 {
171 $importer = new Importer(
172 $this->getPathFactory(),
173 $repo = $this->getRepo(),
174 $this->getSlotHandler()
175 );
176
177 $xml_string = 'asbduafduhsbdjfbsjfbjdbgfd532t7hubfjxd';
178
179 $result = $importer->import($xml_string);
180
181 $this->assertFalse($result->wasSuccessful());
182 $this->assertNotEmpty($result->getErrors());
183 $this->assertEmpty($repo->created_vocabs);
184 $this->assertEmpty($repo->created_values);
185 }
186
188 {
189 $importer = new Importer(
190 $this->getPathFactory(),
191 $repo = $this->getRepo(),
192 $this->getSlotHandler()
193 );
194
195 $xml_string = <<<XML
196<?xml version="1.0"?>
197<something>
198 <else>value</else>
199</something>
200XML;
201
202 $result = $importer->import($xml_string);
203
204 $this->assertFalse($result->wasSuccessful());
205 $this->assertNotEmpty($result->getErrors());
206 $this->assertEmpty($repo->created_vocabs);
207 $this->assertEmpty($repo->created_values);
208 }
209
211 {
212 $importer = new Importer(
213 $this->getPathFactory(),
214 $repo = $this->getRepo(),
215 $this->getSlotHandler()
216 );
217
218 $xml_string = <<<XML
219<?xml version="1.0"?>
220<vocabulary>
221 <appliesTo>
222 <pathToElement>
223 <step>step1</step>
224 <step>INVALID</step>
225 <step>step3</step>
226 </pathToElement>
227 </appliesTo>
228 <source>some source</source>
229 <values>
230 <value>value</value>
231 <value>different value</value>
232 <value>third value</value>
233 </values>
234</vocabulary>
235XML;
236
237 $result = $importer->import($xml_string);
238
239 $this->assertFalse($result->wasSuccessful());
240 $this->assertNotEmpty($result->getErrors());
241 $this->assertEmpty($repo->created_vocabs);
242 $this->assertEmpty($repo->created_values);
243 }
244
246 {
247 $importer = new Importer(
248 $this->getPathFactory(),
249 $repo = $this->getRepo(),
250 $this->getSlotHandler()
251 );
252
253 $xml_string = <<<XML
254<?xml version="1.0"?>
255<vocabulary>
256 <appliesTo>
257 <pathToElement>
258 <step>step1</step>
259 <step>step2</step>
260 </pathToElement>
261 <condition value="condition value">
262 <pathToElement>
263 <stepToSuper/>
264 <step>INVALID</step>
265 <step>step 3</step>
266 </pathToElement>
267 </condition>
268 </appliesTo>
269 <source>some source</source>
270 <values>
271 <value>value</value>
272 <value>different value</value>
273 <value>third value</value>
274 </values>
275</vocabulary>
276XML;
277
278 $result = $importer->import($xml_string);
279
280 $this->assertFalse($result->wasSuccessful());
281 $this->assertNotEmpty($result->getErrors());
282 $this->assertEmpty($repo->created_vocabs);
283 $this->assertEmpty($repo->created_values);
284 }
285
286 public function testImportInvalidSlotError(): void
287 {
288 $importer = new Importer(
289 $this->getPathFactory(),
290 $repo = $this->getRepo(),
291 $this->getSlotHandler(true)
292 );
293
294 $xml_string = <<<XML
295<?xml version="1.0"?>
296<vocabulary>
297 <appliesTo>
298 <pathToElement>
299 <step>step1</step>
300 <step>step2</step>
301 </pathToElement>
302 <condition value="condition value">
303 <pathToElement>
304 <stepToSuper/>
305 <step>step2</step>
306 <step>step3</step>
307 </pathToElement>
308 </condition>
309 </appliesTo>
310 <source>some source</source>
311 <values>
312 <value>value</value>
313 <value>different value</value>
314 <value>third value</value>
315 </values>
316</vocabulary>
317XML;
318
319 $result = $importer->import($xml_string);
320
321 $this->assertFalse($result->wasSuccessful());
322 $this->assertNotEmpty($result->getErrors());
323 $this->assertEmpty($repo->created_vocabs);
324 $this->assertEmpty($repo->created_values);
325 }
326
327 public function testImportDuplicateValuesError(): void
328 {
329 $importer = new Importer(
330 $this->getPathFactory(),
331 $repo = $this->getRepo(),
332 $this->getSlotHandler()
333 );
334
335 $xml_string = <<<XML
336<?xml version="1.0"?>
337<vocabulary>
338 <appliesTo>
339 <pathToElement>
340 <step>step1</step>
341 <step>step2</step>
342 </pathToElement>
343 </appliesTo>
344 <source>some source</source>
345 <values>
346 <value>value</value>
347 <value>duplicate value</value>
348 <value>duplicate value</value>
349 </values>
350</vocabulary>
351XML;
352
353 $result = $importer->import($xml_string);
354
355 $this->assertFalse($result->wasSuccessful());
356 $this->assertNotEmpty($result->getErrors());
357 $this->assertEmpty($repo->created_vocabs);
358 $this->assertEmpty($repo->created_values);
359 }
360
362 {
363 $importer = new Importer(
364 $this->getPathFactory(),
365 $repo = $this->getRepo(false, 'already exists', 'also already exists'),
366 $this->getSlotHandler()
367 );
368
369 $xml_string = <<<XML
370<?xml version="1.0"?>
371<vocabulary>
372 <appliesTo>
373 <pathToElement>
374 <step>step1</step>
375 <step>step2</step>
376 </pathToElement>
377 </appliesTo>
378 <source>some source</source>
379 <values>
380 <value>already exists</value>
381 <value>value</value>
382 <value>also already exists</value>
383 </values>
384</vocabulary>
385XML;
386
387 $result = $importer->import($xml_string);
388
389 $this->assertFalse($result->wasSuccessful());
390 $this->assertNotEmpty($result->getErrors());
391 $this->assertEmpty($repo->created_vocabs);
392 $this->assertEmpty($repo->created_values);
393 }
394
396 {
397 $importer = new Importer(
398 $this->getPathFactory(),
399 $repo = $this->getRepo(true),
400 $this->getSlotHandler()
401 );
402
403 $xml_string = <<<XML
404<?xml version="1.0"?>
405<vocabulary>
406 <appliesTo>
407 <pathToElement>
408 <step>step1</step>
409 <step>step2</step>
410 <step>step3</step>
411 </pathToElement>
412 </appliesTo>
413 <source>some source</source>
414 <values>
415 <value>value</value>
416 <value>different value</value>
417 <value>third value</value>
418 </values>
419</vocabulary>
420XML;
421
422 $result = $importer->import($xml_string);
423
424 $this->assertFalse($result->wasSuccessful());
425 $this->assertNotEmpty($result->getErrors());
426 $this->assertEmpty($repo->created_vocabs);
427 $this->assertEmpty($repo->created_values);
428 }
429
430 public function testImport(): void
431 {
432 $importer = new Importer(
433 $this->getPathFactory(),
434 $repo = $this->getRepo(),
435 $slots = $this->getSlotHandler()
436 );
437
438 $xml_string = <<<XML
439<?xml version="1.0"?>
440<vocabulary>
441 <appliesTo>
442 <pathToElement>
443 <step>step1</step>
444 <step>step2</step>
445 <step>step3</step>
446 </pathToElement>
447 </appliesTo>
448 <source>some source</source>
449 <values>
450 <value>value</value>
451 <value>different value</value>
452 <value>third value</value>
453 </values>
454</vocabulary>
455XML;
456
457 $result = $importer->import($xml_string);
458
459 $this->assertTrue($result->wasSuccessful());
460 $this->assertEmpty($result->getErrors());
461 $this->assertSame(
462 [[
463 'id' => 'new id 0',
464 'slot' => SlotIdentifier::EDUCATIONAL_CONTEXT,
465 'source' => 'some source',
466 ]],
467 $repo->created_vocabs
468 );
469 $this->assertSame(
470 [[
471 'path' => 'step1;step2;step3;',
472 'condition path' => null,
473 'condition value' => null
474 ]],
475 $slots->exposed_paths_and_conditions
476 );
477 $this->assertSame(
478 [
479 ['vocab id' => 'new id 0', 'value' => 'value', 'label' => ''],
480 ['vocab id' => 'new id 0', 'value' => 'different value', 'label' => ''],
481 ['vocab id' => 'new id 0', 'value' => 'third value', 'label' => '']
482 ],
483 $repo->created_values
484 );
485 }
486
487 public function testImportWithLabels(): void
488 {
489 $importer = new Importer(
490 $this->getPathFactory(),
491 $repo = $this->getRepo(),
492 $slots = $this->getSlotHandler()
493 );
494
495 $xml_string = <<<XML
496<?xml version="1.0"?>
497<vocabulary>
498 <appliesTo>
499 <pathToElement>
500 <step>step1</step>
501 <step>step2</step>
502 <step>step3</step>
503 </pathToElement>
504 </appliesTo>
505 <source>some source</source>
506 <values>
507 <value label="label">value</value>
508 <value label="different label">different value</value>
509 <value label="third label">third value</value>
510 </values>
511</vocabulary>
512XML;
513
514 $result = $importer->import($xml_string);
515
516 $this->assertTrue($result->wasSuccessful());
517 $this->assertEmpty($result->getErrors());
518 $this->assertSame(
519 [[
520 'id' => 'new id 0',
521 'slot' => SlotIdentifier::EDUCATIONAL_CONTEXT,
522 'source' => 'some source',
523 ]],
524 $repo->created_vocabs
525 );
526 $this->assertSame(
527 [[
528 'path' => 'step1;step2;step3;',
529 'condition path' => null,
530 'condition value' => null
531 ]],
532 $slots->exposed_paths_and_conditions
533 );
534 $this->assertSame(
535 [
536 ['vocab id' => 'new id 0', 'value' => 'value', 'label' => 'label'],
537 ['vocab id' => 'new id 0', 'value' => 'different value', 'label' => 'different label'],
538 ['vocab id' => 'new id 0', 'value' => 'third value', 'label' => 'third label']
539 ],
540 $repo->created_values
541 );
542 }
543
544 public function testImportWithCondition(): void
545 {
546 $importer = new Importer(
547 $this->getPathFactory(),
548 $repo = $this->getRepo(),
549 $slots = $this->getSlotHandler()
550 );
551
552 $xml_string = <<<XML
553<?xml version="1.0"?>
554<vocabulary>
555 <appliesTo>
556 <pathToElement>
557 <step>step1</step>
558 <step>step2</step>
559 <step>step3</step>
560 </pathToElement>
561 <condition value="condition value">
562 <pathToElement>
563 <stepToSuper/>
564 <step>condstep1</step>
565 <step>condstep2</step>
566 </pathToElement>
567 </condition>
568 </appliesTo>
569 <source>some source</source>
570 <values>
571 <value>value</value>
572 <value>different value</value>
573 <value>third value</value>
574 </values>
575</vocabulary>
576XML;
577
578 $result = $importer->import($xml_string);
579
580 $this->assertTrue($result->wasSuccessful());
581 $this->assertEmpty($result->getErrors());
582 $this->assertSame(
583 [[
584 'id' => 'new id 0',
585 'slot' => SlotIdentifier::EDUCATIONAL_CONTEXT,
586 'source' => 'some source',
587 ]],
588 $repo->created_vocabs
589 );
590 $this->assertSame(
591 [[
592 'path' => 'step1;step2;step3;',
593 'condition path' => 'RELATIVE!SUPER;condstep1;condstep2;',
594 'condition value' => 'condition value'
595 ]],
596 $slots->exposed_paths_and_conditions
597 );
598 $this->assertSame(
599 [
600 ['vocab id' => 'new id 0', 'value' => 'value', 'label' => ''],
601 ['vocab id' => 'new id 0', 'value' => 'different value', 'label' => ''],
602 ['vocab id' => 'new id 0', 'value' => 'third value', 'label' => '']
603 ],
604 $repo->created_values
605 );
606 }
607}
getRepo(bool $config_error=false, string ... $already_existing_values)
__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
if(!file_exists('../ilias.ini.php'))