ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
CopyrightHelperTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
39 use ILIAS\MetaData\Search\Clauses\NullFactory as NullSearchClauseFactory;
51 
52 class CopyrightHelperTest extends TestCase
53 {
54  protected function getSettings(bool $selection_active): SettingsInterface
55  {
56  return new class ($selection_active) extends NullSettings {
57  public function __construct(protected bool $selection_active)
58  {
59  }
60 
61  public function isCopyrightSelectionActive(): bool
62  {
63  return $this->selection_active;
64  }
65  };
66  }
67 
68  protected function getPathFactory(): PathFactory
69  {
70  return new class () extends NullPathFactory {
71  public function custom(): BuilderInterface
72  {
73  return new class () extends NullBuilder {
74  protected string $path = '';
75 
76  public function withNextStep(string $name, bool $add_as_first = false): BuilderInterface
77  {
78  $clone = clone $this;
79  $clone->path .= '>' . $name;
80  return $clone;
81  }
82 
83  public function get(): PathInterface
84  {
85  return new class ($this->path) extends NullPath {
86  public function __construct(protected string $path)
87  {
88  }
89 
90  public function toString(): string
91  {
92  return $this->path;
93  }
94  };
95  }
96  };
97  }
98  };
99  }
100 
101  protected function getCopyrightEntry(int $id): EntryInterface
102  {
103  return new class ($id) extends NullEntry {
104  public function __construct(protected int $id)
105  {
106  }
107 
108  public function id(): int
109  {
110  return $this->id;
111  }
112  };
113  }
114 
119  protected function getCopyrightRepository(
120  array $entry_ids,
121  array $outdated_entry_ids,
122  int $default_entry_id
124  $entries = [];
125  foreach ($entry_ids as $id) {
126  $entries[] = $this->getCopyrightEntry($id);
127  }
128 
129  return new class ($entries, $outdated_entry_ids, $default_entry_id) extends NullRepository {
130  public function __construct(
131  protected array $entries,
132  protected array $outdated_entry_ids,
133  protected int $default_entry_id
134  ) {
135  }
136 
137  public function getAllEntries(): \Generator
138  {
139  yield from $this->entries;
140  }
141 
142  public function getActiveEntries(): \Generator
143  {
144  foreach ($this->entries as $entry) {
145  if (!in_array($entry->id(), $this->outdated_entry_ids)) {
146  yield $entry;
147  }
148  }
149  }
150 
151  public function getDefaultEntry(): EntryInterface
152  {
153  foreach ($this->entries as $entry) {
154  if ($entry->id() === $this->default_entry_id) {
155  return $entry;
156  }
157  }
158  throw new \ilMDServicesException('Default entry id not found.');
159  }
160 
161  public function getEntry(int $id): EntryInterface
162  {
163  foreach ($this->entries as $entry) {
164  if ($entry->id() === $id) {
165  return $entry;
166  }
167  }
168  throw new \ilMDServicesException('Entry id not found.');
169  }
170  };
171  }
172 
174  {
175  return new class () extends NullHandler {
176  public function isIdentifierValid(string $identifier): bool
177  {
178  return str_contains($identifier, 'valid_');
179  }
180 
181  public function parseEntryIDFromIdentifier(string $identifier): int
182  {
183  if ($this->isIdentifierValid($identifier)) {
184  return (int) str_replace('valid_', '', $identifier);
185  }
186  return 0;
187  }
188  };
189  }
190 
191  protected function getClauseFactory(): SearchClauseFactory
192  {
193  return new class () extends NullSearchClauseFactory {
194  public function getBasicClause(
196  Mode $mode,
197  string $value,
198  bool $is_mode_negated = false
199  ): ClauseInterface {
200  $search_data = '"' . $path->toString() . '" ' .
201  ($is_mode_negated ? 'not ' : '') .
202  $mode->value . ' "' . $value . '"';
203  return new class ($search_data) extends NullClause {
204  public function __construct(public string $exposed_search_data)
205  {
206  }
207  };
208  }
209 
210  public function getJoinedClauses(
211  Operator $operator,
212  ClauseInterface $first_clause,
213  ClauseInterface ...$further_clauses
214  ): ClauseInterface {
215  $clauses_data = [];
216  foreach ([$first_clause, ...$further_clauses] as $clause) {
217  $clauses_data[] = $clause->exposed_search_data;
218  }
219  $search_data = implode(' ' . $operator->value . ' ', $clauses_data);
220  return new class ($search_data) extends NullClause {
221  public function __construct(public string $exposed_search_data)
222  {
223  }
224  };
225  }
226  };
227  }
228 
229  protected function getReader(string $raw_copyright): ReaderInterface
230  {
231  return new class ($raw_copyright) extends NullReader {
232  public function __construct(protected string $raw_copyright)
233  {
234  }
235 
236  public function firstData(PathInterface $path): DataInterface
237  {
238  if ($path->toString() !== '>rights>description>string') {
239  throw new \ilMDServicesException('Wrong Path!');
240  }
241  return new class ($this->raw_copyright) extends NullData {
242  public function __construct(protected string $raw_copyright)
243  {
244  }
245 
246  public function value(): string
247  {
248  return $this->raw_copyright;
249  }
250  };
251  }
252  };
253  }
254 
255  protected function getManipulator(): ManipulatorInterface
256  {
257  return new class () extends NullManipulator {
258  public array $prepared_creates_or_updates = [];
259 
260  public function prepareCreateOrUpdate(PathInterface $path, string ...$values): ManipulatorInterface
261  {
262  $clone = clone $this;
263  $clone->prepared_creates_or_updates[] = [
264  'path' => $path->toString(),
265  'values' => $values,
266  ];
267  return $clone;
268  }
269  };
270  }
271 
272  protected function getCopyrightHelper(
275  ): CopyrightHelper {
276  return new class (
277  $settings,
278  $this->getPathFactory(),
280  $this->getIdentifierHandler(),
281  new NullRenderer(),
282  $this->getClauseFactory()
283  ) extends CopyrightHelper {
285  {
286  return new class ((string) $entry->id()) extends NullCopyright {
287  public function __construct(public string $exposed_id)
288  {
289  }
290  };
291  }
292 
294  {
295  return new class ('null') extends NullCopyright {
296  public function __construct(public string $exposed_id)
297  {
298  }
299  };
300  }
301  };
302  }
303 
304  public function testIsCopyrightSelectionActiveTrue(): void
305  {
306  $helper = $this->getCopyrightHelper(
307  $this->getSettings(true),
308  $this->getCopyrightRepository([], [], 0)
309  );
310 
311  $this->assertTrue($helper->isCopyrightSelectionActive());
312  }
313 
314  public function testIsCopyrightSelectionActiveFalse(): void
315  {
316  $helper = $this->getCopyrightHelper(
317  $this->getSettings(false),
318  $this->getCopyrightRepository([], [], 0)
319  );
320 
321  $this->assertFalse($helper->isCopyrightSelectionActive());
322  }
323 
324  public function testHasPresetCopyrightTrue(): void
325  {
326  $helper = $this->getCopyrightHelper(
327  $this->getSettings(true),
328  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
329  );
330  $reader = $this->getReader('valid_5');
331 
332  $this->assertTrue($helper->hasPresetCopyright($reader));
333  }
334 
335  public function testHasPresetCopyrightTrueEmpty(): void
336  {
337  $helper = $this->getCopyrightHelper(
338  $this->getSettings(true),
339  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
340  );
341  $reader = $this->getReader('');
342 
343  $this->assertTrue($helper->hasPresetCopyright($reader));
344  }
345 
346  public function testHasPresetCopyrightFalse(): void
347  {
348  $helper = $this->getCopyrightHelper(
349  $this->getSettings(true),
350  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
351  );
352  $reader = $this->getReader('something else');
353 
354  $this->assertFalse($helper->hasPresetCopyright($reader));
355  }
356 
358  {
359  $helper = $this->getCopyrightHelper(
360  $this->getSettings(false),
361  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
362  );
363  $reader = $this->getReader('valid_5');
364 
365  $this->assertFalse($helper->hasPresetCopyright($reader));
366  }
367 
368  public function testReadPresetCopyright(): void
369  {
370  $helper = $this->getCopyrightHelper(
371  $this->getSettings(true),
372  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
373  );
374  $reader = $this->getReader('valid_5');
375 
376  $this->assertSame('5', $helper->readPresetCopyright($reader)->exposed_id);
377  }
378 
380  {
381  $helper = $this->getCopyrightHelper(
382  $this->getSettings(false),
383  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
384  );
385  $reader = $this->getReader('valid_5');
386 
387  $this->assertSame('null', $helper->readPresetCopyright($reader)->exposed_id);
388  }
389 
391  {
392  $helper = $this->getCopyrightHelper(
393  $this->getSettings(true),
394  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
395  );
396  $reader = $this->getReader('something else');
397 
398  $this->assertSame('null', $helper->readPresetCopyright($reader)->exposed_id);
399  }
400 
402  {
403  $helper = $this->getCopyrightHelper(
404  $this->getSettings(true),
405  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
406  );
407  $reader = $this->getReader('');
408 
409  $this->assertSame('13', $helper->readPresetCopyright($reader)->exposed_id);
410  }
411 
412  public function testReadCustomCopyright(): void
413  {
414  $helper = $this->getCopyrightHelper(
415  $this->getSettings(true),
416  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
417  );
418  $reader = $this->getReader('custom info about the copyright');
419 
420  $this->assertSame(
421  'custom info about the copyright',
422  $helper->readCustomCopyright($reader)
423  );
424  }
425 
427  {
428  $helper = $this->getCopyrightHelper(
429  $this->getSettings(true),
430  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
431  );
432  $reader = $this->getReader('valid_5');
433 
434  $this->assertSame(
435  '',
436  $helper->readCustomCopyright($reader)
437  );
438  }
439 
441  {
442  $helper = $this->getCopyrightHelper(
443  $this->getSettings(false),
444  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
445  );
446  $reader = $this->getReader('valid_5');
447 
448  $this->assertSame(
449  '',
450  $helper->readCustomCopyright($reader)
451  );
452  }
453 
455  {
456  $helper = $this->getCopyrightHelper(
457  $this->getSettings(false),
458  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
459  );
460  $manipulator = $this->getManipulator();
461 
462  $manipulator = $helper->prepareCreateOrUpdateOfCopyrightFromPreset(
463  $manipulator,
464  'valid_5'
465  );
466 
467  $this->assertSame(
468  [['path' => '>rights>description>string', 'values' => ['valid_5']]],
469  $manipulator->prepared_creates_or_updates
470  );
471  }
472 
474  {
475  $helper = $this->getCopyrightHelper(
476  $this->getSettings(false),
477  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
478  );
479  $manipulator = $this->getManipulator();
480 
481  $manipulator = $helper->prepareCreateOrUpdateOfCustomCopyright(
482  $manipulator,
483  'custom info about the copyright'
484  );
485 
486  $this->assertSame(
487  [['path' => '>rights>description>string', 'values' => ['custom info about the copyright']]],
488  $manipulator->prepared_creates_or_updates
489  );
490  }
491 
492  public function testGetAllCopyrightPresets(): void
493  {
494  $helper = $this->getCopyrightHelper(
495  $this->getSettings(true),
496  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
497  );
498 
499  $presets = $helper->getAllCopyrightPresets();
500 
501  $this->assertSame('13', $presets->current()->exposed_id);
502  $presets->next();
503  $this->assertSame('77', $presets->current()->exposed_id);
504  $presets->next();
505  $this->assertSame('932', $presets->current()->exposed_id);
506  $presets->next();
507  $this->assertSame('5', $presets->current()->exposed_id);
508  $presets->next();
509  $this->assertNull($presets->current());
510  }
511 
513  {
514  $helper = $this->getCopyrightHelper(
515  $this->getSettings(false),
516  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
517  );
518 
519  $presets = $helper->getAllCopyrightPresets();
520 
521  $this->assertNull($presets->current());
522  }
523 
524  public function testGetNonOutdatedCopyrightPresets(): void
525  {
526  $helper = $this->getCopyrightHelper(
527  $this->getSettings(true),
528  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
529  );
530 
531  $presets = $helper->getNonOutdatedCopyrightPresets();
532 
533  $this->assertSame('13', $presets->current()->exposed_id);
534  $presets->next();
535  $this->assertSame('932', $presets->current()->exposed_id);
536  $presets->next();
537  $this->assertSame('5', $presets->current()->exposed_id);
538  $presets->next();
539  $this->assertNull($presets->current());
540  }
541 
543  {
544  $helper = $this->getCopyrightHelper(
545  $this->getSettings(false),
546  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
547  );
548 
549  $presets = $helper->getNonOutdatedCopyrightPresets();
550 
551  $this->assertNull($presets->current());
552  }
553 
554  public function testGetCopyrightSearchClause(): void
555  {
556  $helper = $this->getCopyrightHelper(
557  $this->getSettings(true),
558  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
559  );
560 
561  $clause = $helper->getCopyrightSearchClause('valid_5');
562 
563  $this->assertSame(
564  '">rights>description>string" equals "valid_5"',
565  $clause->exposed_search_data
566  );
567  }
568 
570  {
571  $helper = $this->getCopyrightHelper(
572  $this->getSettings(true),
573  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
574  );
575 
576  $clause = $helper->getCopyrightSearchClause(
577  'valid_5',
578  'valid_77',
579  'something else'
580  );
581 
582  $this->assertSame(
583  '">rights>description>string" equals "valid_5" or ' .
584  '">rights>description>string" equals "valid_77" or ' .
585  '">rights>description>string" equals "something else"',
586  $clause->exposed_search_data
587  );
588  }
589 
591  {
592  $helper = $this->getCopyrightHelper(
593  $this->getSettings(true),
594  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
595  );
596 
597  $clause = $helper->getCopyrightSearchClause('valid_13');
598 
599  $this->assertSame(
600  '">rights>description>string" equals "valid_13" or ' .
601  '">rights>description>string" equals ""',
602  $clause->exposed_search_data
603  );
604  }
605 
607  {
608  $helper = $this->getCopyrightHelper(
609  $this->getSettings(false),
610  $this->getCopyrightRepository([13, 77, 932, 5], [77], 13)
611  );
612 
613  $clause = $helper->getCopyrightSearchClause('valid_13');
614 
615  $this->assertSame(
616  '">rights>description>string" equals "valid_13"',
617  $clause->exposed_search_data
618  );
619  }
620 }
__construct(SettingsInterface $settings, PathFactory $path_factory, CopyrightRepository $copyright_repo, IdentifierHandler $identifier_handler, RendererInterface $renderer, SearchClauseFactory $search_clause_factory)
$path
Definition: ltiservices.php:29
isCopyrightSelectionActive()
If copyright selection is not active, there are no copyright presets to choose from, but copyright information can still be added manually to the LOM of objects.
getCopyrightHelper(SettingsInterface $settings, CopyrightRepository $copyright_repo)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getCopyrightRepository(array $entry_ids, array $outdated_entry_ids, int $default_entry_id)
custom()
expected output: > ILIAS shows a base horizontal bar chart but customized with e.g.
Definition: custom.php:33