ILIAS  trunk Revision v11.0_alpha-1744-gb0451eebef4
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
StandardTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
46 
47 class StandardTest extends TestCase
48 {
49  protected function getSet(array $set_as_array): SetInterface
50  {
51  $root = new class () extends NullElement {
52  public array $element_as_array;
53 
54  public function getSubElements(): \Generator
55  {
56  foreach ($this->element_as_array['subs'] as $sub_array) {
57  $sub = clone $this;
58  $sub->element_as_array = $sub_array;
59  yield $sub;
60  }
61  }
62 
63  public function getDefinition(): DefinitionInterface
64  {
65  return new class ($this->element_as_array) extends NullDefinition {
66  public function __construct(protected array $element_as_array)
67  {
68  }
69 
70  public function name(): string
71  {
72  return $this->element_as_array['name'];
73  }
74  };
75  }
76 
77  public function getData(): DataInterface
78  {
79  return new class ($this->element_as_array) extends NullData {
80  public function __construct(protected array $element_as_array)
81  {
82  }
83 
84  public function type(): Type
85  {
86  return $this->element_as_array['type'];
87  }
88 
89  public function value(): string
90  {
91  return $this->element_as_array['value'];
92  }
93  };
94  }
95  };
96  $root->element_as_array = $set_as_array;
97 
98  return new class ($root) extends NullSet {
99  public function __construct(protected ElementInterface $root)
100  {
101  }
102 
103  public function getRoot(): ElementInterface
104  {
105  return $this->root;
106  }
107  };
108  }
109 
110  protected function getStandardWriter(bool $cp_selection_active = false): Standard
111  {
112  $dictionary = new class () extends NullDictionary {
116  public function tagForElement(
117  BaseElementInterface $element,
119  ): ?TagInterface {
120  if (!isset($element->element_as_array['specials'])) {
121  return null;
122  }
123 
124  return new class ($element->element_as_array['specials']) extends NullTag {
125  public function __construct(
126  protected array $specials
127  ) {
128  }
129 
130  public function isExportedAsLangString(): bool
131  {
132  return in_array(SpecialCase::LANGSTRING, $this->specials);
133  }
134 
135  public function isTranslatedAsCopyright(): bool
136  {
137  return in_array(SpecialCase::COPYRIGHT, $this->specials);
138  }
139 
140  public function isOmitted(): bool
141  {
142  return in_array(SpecialCase::OMITTED, $this->specials);
143  }
144 
145  public function isExportedAsAttribute(): bool
146  {
147  return in_array(SpecialCase::AS_ATTRIBUTE, $this->specials);
148  }
149  };
150  }
151  };
152 
153  $copyright_handler = new class ($cp_selection_active) extends NullCopyrightHandler {
154  public function __construct(protected bool $cp_selection_active)
155  {
156  }
157 
158  public function copyrightForExport(string $copyright): string
159  {
160  return '~parsed:' . $copyright . '~';
161  }
162 
163  public function isCopyrightSelectionActive(): bool
164  {
165  return $this->cp_selection_active;
166  }
167  };
168 
169  $path_factory = new class () extends NullPathFactory {
170  public function custom(): BuilderInterface
171  {
172  return new class () extends NullBuilder {
173  protected array $path = [];
174 
175  public function withNextStep(string $name, bool $add_as_first = false): BuilderInterface
176  {
177  $clone = clone $this;
178  $clone->path[] = $name;
179  return $clone;
180  }
181 
182  public function get(): PathInterface
183  {
184  $string = implode('>', $this->path);
185  return new class ($string) extends NullPath {
186  public function __construct(protected string $string)
187  {
188  }
189 
190  public function toString(): string
191  {
192  return $this->string;
193  }
194  };
195  }
196  };
197  }
198  };
199 
200  $manipulator = new class () extends NullManipulator {
201  public function prepareCreateOrUpdate(
202  SetInterface $set,
204  string ...$values
205  ): SetInterface {
206  if (
207  $path->toString() !== 'rights>description>string' ||
208  count($values) !== 1
209  ) {
210  throw new \ilMDXMLException(
211  'Unexpected preparation, path: "' . $path->toString() .
212  '", value count: "' . count($values) . '"'
213  );
214  }
215 
216  /*
217  * Always insert the copyright when testing, the manipulator automatically
218  * takes care of the case where it is already there.
219  */
220  $insert_array = [
221  'name' => 'copyright',
222  'type' => Type::NULL,
223  'value' => '',
224  'subs' => [[
225  'name' => 'string',
226  'type' => Type::STRING,
227  'value' => '',
228  'subs' => [],
229  'specials' => [SpecialCase::COPYRIGHT]
230  ]]
231  ];
232 
233  $set->getRoot()->element_as_array['subs'][] = $insert_array;
234  return $set;
235  }
236  };
237 
238  return new Standard($dictionary, $copyright_handler, $path_factory, $manipulator);
239  }
240 
241  public function testWrite(): void
242  {
243  $set_array = [
244  'name' => 'el1',
245  'type' => Type::NULL,
246  'value' => '',
247  'subs' => [
248  [
249  'name' => 'el1.1',
250  'type' => Type::STRING,
251  'value' => 'val1.1',
252  'subs' => []
253  ],
254  [
255  'name' => 'el1.2',
256  'type' => Type::NULL,
257  'value' => '',
258  'subs' => [
259  [
260  'name' => 'el1.2.1',
261  'type' => Type::NON_NEG_INT,
262  'value' => 'val1.2.1',
263  'subs' => []
264  ],
265  [
266  'name' => 'el1.2.2',
267  'type' => Type::DURATION,
268  'value' => 'val1.2.2',
269  'subs' => []
270  ]
271  ]
272  ]
273  ]
274  ];
275 
276  $expected_xml = <<<XML
277 <?xml version="1.0"?>
278 <el1>
279  <el1.1>val1.1</el1.1>
280  <el1.2>
281  <el1.2.1>val1.2.1</el1.2.1>
282  <el1.2.2>val1.2.2</el1.2.2>
283  </el1.2>
284 </el1>
285 XML;
286 
287  $writer = $this->getStandardWriter();
288  $set = $this->getSet($set_array);
289  $xml = $writer->write($set);
290 
291  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
292  }
293 
294  public function testWriteWithDuplicateElementNames(): void
295  {
296  $set_array = [
297  'name' => 'el1',
298  'type' => Type::NULL,
299  'value' => '',
300  'subs' => [
301  [
302  'name' => 'name',
303  'type' => Type::STRING,
304  'value' => 'value1',
305  'subs' => []
306  ],
307  [
308  'name' => 'name',
309  'type' => Type::STRING,
310  'value' => 'value2',
311  'subs' => []
312  ]
313  ]
314  ];
315 
316  $expected_xml = <<<XML
317 <?xml version="1.0"?>
318 <el1>
319  <name>value1</name>
320  <name>value2</name>
321 </el1>
322 XML;
323 
324  $writer = $this->getStandardWriter();
325  $set = $this->getSet($set_array);
326  $xml = $writer->write($set);
327 
328  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
329  }
330 
331  public function testWriteWithLanguageNone(): void
332  {
333  $set_array = [
334  'name' => 'el1',
335  'type' => Type::NULL,
336  'value' => '',
337  'subs' => [
338  [
339  'name' => 'el1.1',
340  'type' => Type::LANG,
341  'value' => 'xx',
342  'subs' => []
343  ]
344  ]
345  ];
346 
347  $expected_xml = <<<XML
348 <?xml version="1.0"?>
349 <el1>
350  <el1.1>none</el1.1>
351 </el1>
352 XML;
353 
354  $writer = $this->getStandardWriter();
355  $set = $this->getSet($set_array);
356  $xml = $writer->write($set);
357 
358  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
359  }
360 
361  public function testWriteWithLangString(): void
362  {
363  $set_array = [
364  'name' => 'el1',
365  'type' => Type::NULL,
366  'value' => '',
367  'subs' => [
368  [
369  'name' => 'el1.1',
370  'type' => Type::NULL,
371  'value' => '',
372  'specials' => [SpecialCase::LANGSTRING],
373  'subs' => [
374  [
375  'name' => 'string',
376  'type' => Type::STRING,
377  'value' => 'some text',
378  'subs' => []
379  ],
380  [
381  'name' => 'language',
382  'type' => Type::LANG,
383  'value' => 'br',
384  'subs' => []
385  ]
386  ]
387  ]
388  ]
389  ];
390 
391  $expected_xml = <<<XML
392 <?xml version="1.0"?>
393 <el1>
394  <el1.1>
395  <string language="br">some text</string>
396  </el1.1>
397 </el1>
398 XML;
399 
400  $writer = $this->getStandardWriter();
401  $set = $this->getSet($set_array);
402  $xml = $writer->write($set);
403 
404  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
405  }
406 
407  public function testWriteWithLangStringNoLanguage(): void
408  {
409  $set_array = [
410  'name' => 'el1',
411  'type' => Type::NULL,
412  'value' => '',
413  'subs' => [
414  [
415  'name' => 'el1.1',
416  'type' => Type::NULL,
417  'value' => '',
418  'specials' => [SpecialCase::LANGSTRING],
419  'subs' => [
420  [
421  'name' => 'string',
422  'type' => Type::STRING,
423  'value' => 'some text',
424  'subs' => []
425  ]
426  ]
427  ]
428  ]
429  ];
430 
431  $expected_xml = <<<XML
432 <?xml version="1.0"?>
433 <el1>
434  <el1.1>
435  <string>some text</string>
436  </el1.1>
437 </el1>
438 XML;
439 
440  $writer = $this->getStandardWriter();
441  $set = $this->getSet($set_array);
442  $xml = $writer->write($set);
443 
444  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
445  }
446 
447  public function testWriteWithLangStringLanguageNone(): void
448  {
449  $set_array = [
450  'name' => 'el1',
451  'type' => Type::NULL,
452  'value' => '',
453  'subs' => [
454  [
455  'name' => 'el1.1',
456  'type' => Type::NULL,
457  'value' => '',
458  'specials' => [SpecialCase::LANGSTRING],
459  'subs' => [
460  [
461  'name' => 'string',
462  'type' => Type::STRING,
463  'value' => 'some text',
464  'subs' => []
465  ],
466  [
467  'name' => 'language',
468  'type' => Type::LANG,
469  'value' => 'xx',
470  'subs' => []
471  ]
472  ]
473  ]
474  ]
475  ];
476 
477  $expected_xml = <<<XML
478 <?xml version="1.0"?>
479 <el1>
480  <el1.1>
481  <string language="none">some text</string>
482  </el1.1>
483 </el1>
484 XML;
485 
486  $writer = $this->getStandardWriter();
487  $set = $this->getSet($set_array);
488  $xml = $writer->write($set);
489 
490  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
491  }
492 
493  public function testWriteWithLangStringNoString(): void
494  {
495  $set_array = [
496  'name' => 'el1',
497  'type' => Type::NULL,
498  'value' => '',
499  'subs' => [
500  [
501  'name' => 'el1.1',
502  'type' => Type::NULL,
503  'value' => '',
504  'specials' => [SpecialCase::LANGSTRING],
505  'subs' => [
506  [
507  'name' => 'language',
508  'type' => Type::LANG,
509  'value' => 'br',
510  'subs' => []
511  ]
512  ]
513  ]
514  ]
515  ];
516 
517  $expected_xml = <<<XML
518 <?xml version="1.0"?>
519 <el1>
520  <el1.1>
521  <string language="br"/>
522  </el1.1>
523 </el1>
524 XML;
525 
526  $writer = $this->getStandardWriter();
527  $set = $this->getSet($set_array);
528  $xml = $writer->write($set);
529 
530  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
531  }
532 
533  public function testWriteWithCopyright(): void
534  {
535  $set_array = [
536  'name' => 'el1',
537  'type' => Type::NULL,
538  'value' => '',
539  'subs' => [
540  [
541  'name' => 'cp',
542  'type' => Type::STRING,
543  'value' => 'some license',
544  'specials' => [SpecialCase::COPYRIGHT],
545  'subs' => []
546  ]
547  ]
548  ];
549 
550  $expected_xml = <<<XML
551 <?xml version="1.0"?>
552 <el1>
553  <cp>~parsed:some license~</cp>
554 </el1>
555 XML;
556 
557  $writer = $this->getStandardWriter();
558  $set = $this->getSet($set_array);
559  $xml = $writer->write($set);
560 
561  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
562  }
563 
564  public function testWriteCPSelectionActive(): void
565  {
566  $set_array = [
567  'name' => 'el1',
568  'type' => Type::NULL,
569  'value' => '',
570  'subs' => [
571  [
572  'name' => 'el2',
573  'type' => Type::STRING,
574  'value' => 'some value',
575  'subs' => []
576  ]
577  ]
578  ];
579 
580  $expected_xml = <<<XML
581 <?xml version="1.0"?>
582 <el1>
583  <el2>some value</el2>
584  <copyright><string>~parsed:~</string></copyright>
585 </el1>
586 XML;
587 
588  $writer = $this->getStandardWriter(true);
589  $set = $this->getSet($set_array);
590  $xml = $writer->write($set);
591 
592  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
593  }
594 
596  {
597  $set_array = [
598  'name' => 'el1',
599  'type' => Type::NULL,
600  'value' => '',
601  'subs' => [
602  [
603  'name' => 'el1.1',
604  'type' => Type::STRING,
605  'value' => 'val1.1',
606  'specials' => [SpecialCase::OMITTED],
607  'subs' => []
608  ]
609  ]
610  ];
611 
612  $expected_xml = <<<XML
613 <?xml version="1.0"?>
614 <el1/>
615 XML;
616 
617  $writer = $this->getStandardWriter();
618  $set = $this->getSet($set_array);
619  $xml = $writer->write($set);
620 
621  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
622  }
623 
624  public function testWriteWithOmittedContainerElement(): void
625  {
626  $set_array = [
627  'name' => 'el1',
628  'type' => Type::NULL,
629  'value' => '',
630  'subs' => [
631  [
632  'name' => 'el1.1',
633  'type' => Type::NULL,
634  'value' => '',
635  'specials' => [SpecialCase::OMITTED],
636  'subs' => [
637  [
638  'name' => 'el1.1.1',
639  'type' => Type::STRING,
640  'value' => 'val1.1.1',
641  'subs' => []
642  ],
643  [
644  'name' => 'el1.1.2',
645  'type' => Type::STRING,
646  'value' => 'val1.1.2',
647  'subs' => []
648  ]
649  ]
650  ]
651  ]
652  ];
653 
654  $expected_xml = <<<XML
655 <?xml version="1.0"?>
656 <el1/>
657 XML;
658 
659  $writer = $this->getStandardWriter();
660  $set = $this->getSet($set_array);
661  $xml = $writer->write($set);
662 
663  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
664  }
665 
666  public function testWriteWithExportedAsAttribute(): void
667  {
668  $set_array = [
669  'name' => 'el1',
670  'type' => Type::NULL,
671  'value' => '',
672  'subs' => [
673  [
674  'name' => 'el1.1',
675  'type' => Type::NULL,
676  'value' => '',
677  'subs' => [
678  [
679  'name' => 'el1.1.1',
680  'type' => Type::STRING,
681  'value' => 'val1.1.1',
682  'specials' => [SpecialCase::AS_ATTRIBUTE],
683  'subs' => []
684  ],
685  [
686  'name' => 'el1.1.2',
687  'type' => Type::STRING,
688  'value' => 'val1.1.2',
689  'subs' => []
690  ]
691  ]
692  ]
693  ]
694  ];
695 
696  $expected_xml = <<<XML
697 <?xml version="1.0"?>
698 <el1>
699  <el1.1 el1.1.1="val1.1.1">
700  <el1.1.2>val1.1.2</el1.1.2>
701  </el1.1>
702 </el1>
703 XML;
704 
705  $writer = $this->getStandardWriter();
706  $set = $this->getSet($set_array);
707  $xml = $writer->write($set);
708 
709  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
710  }
711 
712  public function testWriteWithoutDataCarryingElement(): void
713  {
714  $set_array = [
715  'name' => 'el1',
716  'type' => Type::NULL,
717  'value' => '',
718  'subs' => [
719  [
720  'name' => 'el1.1',
721  'type' => Type::NULL,
722  'value' => '',
723  'subs' => []
724  ],
725  [
726  'name' => 'el1.2',
727  'type' => Type::NULL,
728  'value' => '',
729  'subs' => []
730  ]
731  ]
732  ];
733 
734  $expected_xml = <<<XML
735 <?xml version="1.0"?>
736 <el1>
737  <el1.1/>
738  <el1.2/>
739 </el1>
740 XML;
741 
742  $writer = $this->getStandardWriter();
743  $set = $this->getSet($set_array);
744  $xml = $writer->write($set);
745 
746  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
747  }
748 
750  {
751  $set_array = [
752  'name' => 'el1',
753  'type' => Type::NULL,
754  'value' => '',
755  'subs' => [
756  [
757  'name' => 'el1.1',
758  'type' => Type::VOCAB_VALUE,
759  'value' => 'This contains !@#$%^&*(){}[]<>?=+\/|"\' a bunch of special characters.',
760  'subs' => []
761  ]
762  ]
763  ];
764 
765  $expected_xml = <<<XML
766 <?xml version="1.0"?>
767 <el1>
768  <el1.1>This contains !@#$%^&amp;*(){}[]&lt;&gt;?=+\/|"' a bunch of special characters.</el1.1>
769 </el1>
770 XML;
771 
772  $writer = $this->getStandardWriter();
773  $set = $this->getSet($set_array);
774  $xml = $writer->write($set);
775 
776  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
777  }
778 
779  public function testWriteWithLangStringContainingSpecialCharacters(): void
780  {
781  $set_array = [
782  'name' => 'el1',
783  'type' => Type::NULL,
784  'value' => '',
785  'subs' => [
786  [
787  'name' => 'el1.1',
788  'type' => Type::NULL,
789  'value' => '',
790  'specials' => [SpecialCase::LANGSTRING],
791  'subs' => [
792  [
793  'name' => 'string',
794  'type' => Type::STRING,
795  'value' => 'This contains !@#$%^&*(){}[]<>?=+\/|"\' a bunch of special characters.',
796  'subs' => []
797  ],
798  [
799  'name' => 'language',
800  'type' => Type::LANG,
801  'value' => 'br',
802  'subs' => []
803  ]
804  ]
805  ]
806  ]
807  ];
808 
809  $expected_xml = <<<XML
810 <?xml version="1.0"?>
811 <el1>
812  <el1.1>
813  <string language="br">This contains !@#$%^&amp;*(){}[]&lt;&gt;?=+\/|"' a bunch of special characters.</string>
814  </el1.1>
815 </el1>
816 XML;
817 
818  $writer = $this->getStandardWriter();
819  $set = $this->getSet($set_array);
820  $xml = $writer->write($set);
821 
822  $this->assertXmlStringEqualsXmlString($expected_xml, $xml->asXML());
823  }
824 }
$version
Definition: plugin.php:24
getStandardWriter(bool $cp_selection_active=false)
$path
Definition: ltiservices.php:29
getRoot()
Returns the root element of the metadata set.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
custom()
expected output: > ILIAS shows a base horizontal bar chart but customized with e.g.
Definition: custom.php:33
language()
description: > Example for rendring a language glyph.
Definition: language.php:41