ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CopyrightHandlerTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
24use ILIAS\MetaData\Copyright\RepositoryInterface as CopyrightRepository;
37
38class CopyrightHandlerTest extends TestCase
39{
40 protected function getURI(string $link): URI
41 {
42 $url = $this->createMock(URI::class);
43 $url->method('__toString')->willReturn($link);
44 return $url;
45 }
46
47 protected function getCopyrightEntry(
48 int $id,
49 string $full_name,
50 ?string $link,
51 bool $default = false
53 $url = is_null($link) ? null : $this->getURI($link);
54
55 return new class ($id, $full_name, $url, $default) extends NullEntry {
56 public function __construct(
57 protected int $id,
58 protected string $full_name,
59 protected ?URI $url,
60 protected bool $default
61 ) {
62 }
63
64 public function id(): int
65 {
66 return $this->id;
67 }
68
69 public function isDefault(): bool
70 {
71 return $this->default;
72 }
73
74 public function copyrightData(): CopyrightDataInterface
75 {
76 return new class ($this->full_name, $this->url) extends NullCopyrightData {
77 public function __construct(
78 protected string $full_name,
79 protected ?URI $url
80 ) {
81 }
82
83 public function fullName(): string
84 {
85 return $this->full_name;
86 }
87
88 public function link(): ?URI
89 {
90 return $this->url;
91 }
92 };
93 }
94 };
95 }
96
97 protected function getCopyrightRepository(EntryInterface ...$entries): CopyrightRepository
98 {
99 return new class ($entries) extends NullRepository {
100 public function __construct(protected array $entries)
101 {
102 }
103
104 public function getAllEntries(): \Generator
105 {
106 yield from $this->entries;
107 }
108
109 public function getEntry(int $id): EntryInterface
110 {
111 foreach ($this->entries as $entry) {
112 if ($entry->id() === $id) {
113 return $entry;
114 }
115 }
116 return new NullEntry();
117 }
118
119 public function getDefaultEntry(): EntryInterface
120 {
121 foreach ($this->entries as $entry) {
122 if ($entry->isDefault()) {
123 return $entry;
124 }
125 }
126 return new NullEntry();
127 }
128 };
129 }
130
131 protected function getIdentifierHandler(): IdentifierHandler
132 {
133 return new class () extends NullHandler {
134 public function isIdentifierValid(string $identifier): bool
135 {
136 return str_contains($identifier, 'valid_identifier_');
137 }
138
139 public function parseEntryIDFromIdentifier(string $identifier): int
140 {
141 return (int) str_replace('valid_identifier_', '', $identifier);
142 }
143
144 public function buildIdentifierFromEntryID(int $entry_id): string
145 {
146 return 'valid_identifier_' . $entry_id;
147 }
148 };
149 }
150
151 protected function getSettings(bool $selection_active): SettingsInterface
152 {
153 return new class ($selection_active) extends NullSettings {
154 public function __construct(protected bool $selection_active)
155 {
156 }
157
158 public function isCopyrightSelectionActive(): bool
159 {
160 return $this->selection_active;
161 }
162 };
163 }
164
166 {
168 $this->getCopyrightRepository(),
169 $this->getIdentifierHandler(),
170 $this->getSettings(true)
171 );
172
173 $this->assertTrue($handler->isCopyrightSelectionActive());
174 }
175
177 {
179 $this->getCopyrightRepository(),
180 $this->getIdentifierHandler(),
181 $this->getSettings(false)
182 );
183
184 $this->assertFalse($handler->isCopyrightSelectionActive());
185 }
186
187 public function testCopyrightAsString(): void
188 {
189 $entries = [
190 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
191 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
192 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
193 ];
195 $this->getCopyrightRepository(...$entries),
196 $this->getIdentifierHandler(),
197 $this->getSettings(true)
198 );
199
200 $this->assertSame(
201 'http://www.example2.com',
202 $handler->copyrightAsString('valid_identifier_55')
203 );
204 }
205
207 {
208 $entries = [
209 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
210 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
211 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
212 ];
214 $this->getCopyrightRepository(...$entries),
215 $this->getIdentifierHandler(),
216 $this->getSettings(false)
217 );
218
219 $this->assertSame(
220 'valid_identifier_55',
221 $handler->copyrightAsString('valid_identifier_55')
222 );
223 }
224
226 {
227 $entries = [
228 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
229 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
230 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
231 ];
233 $this->getCopyrightRepository(...$entries),
234 $this->getIdentifierHandler(),
235 $this->getSettings(true)
236 );
237
238 $this->assertSame(
239 'something invalid',
240 $handler->copyrightAsString('something invalid')
241 );
242 }
243
245 {
246 $entries = [
247 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
248 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
249 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
250 ];
252 $this->getCopyrightRepository(...$entries),
253 $this->getIdentifierHandler(),
254 $this->getSettings(true)
255 );
256
257 $this->assertSame(
258 '',
259 $handler->copyrightAsString('valid_identifier_678')
260 );
261 }
262
264 {
265 $entries = [
266 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
267 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
268 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
269 ];
271 $this->getCopyrightRepository(...$entries),
272 $this->getIdentifierHandler(),
273 $this->getSettings(true)
274 );
275
276 $this->assertSame(
277 'something invalid',
278 $handler->copyrightForExport('something invalid')
279 );
280 }
281
283 {
284 $entries = [
285 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
286 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
287 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
288 ];
290 $this->getCopyrightRepository(...$entries),
291 $this->getIdentifierHandler(),
292 $this->getSettings(true)
293 );
294
295 $this->assertSame(
296 '',
297 $handler->copyrightForExport('valid_identifier_678')
298 );
299 }
300
302 {
303 $entries = [
304 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
305 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
306 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
307 ];
309 $this->getCopyrightRepository(...$entries),
310 $this->getIdentifierHandler(),
311 $this->getSettings(false)
312 );
313
314 $this->assertSame(
315 'valid_identifier_55',
316 $handler->copyrightForExport('valid_identifier_55')
317 );
318 }
319
320 public function testCopyrightForExportEmpty(): void
321 {
322 $entries = [
323 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
324 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com', true),
325 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
326 ];
328 $this->getCopyrightRepository(...$entries),
329 $this->getIdentifierHandler(),
330 $this->getSettings(true)
331 );
332
333 $this->assertSame(
334 'http://www.example2.com',
335 $handler->copyrightForExport('')
336 );
337 }
338
339 public function testCopyrightForExportHasLink(): void
340 {
341 $entries = [
342 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
343 $this->getCopyrightEntry(55, '', 'http://www.example2.com'),
344 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
345 ];
347 $this->getCopyrightRepository(...$entries),
348 $this->getIdentifierHandler(),
349 $this->getSettings(true)
350 );
351
352 $this->assertSame(
353 'http://www.example2.com',
354 $handler->copyrightForExport('valid_identifier_55')
355 );
356 }
357
358 public function testCopyrightForExportHasFullName(): void
359 {
360 $entries = [
361 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
362 $this->getCopyrightEntry(55, 'second entry', null),
363 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
364 ];
366 $this->getCopyrightRepository(...$entries),
367 $this->getIdentifierHandler(),
368 $this->getSettings(true)
369 );
370
371 $this->assertSame(
372 'second entry',
373 $handler->copyrightForExport('valid_identifier_55')
374 );
375 }
376
378 {
379 $entries = [
380 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
381 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
382 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
383 ];
385 $this->getCopyrightRepository(...$entries),
386 $this->getIdentifierHandler(),
387 $this->getSettings(true)
388 );
389
390 $this->assertSame(
391 'http://www.example2.com',
392 $handler->copyrightForExport('valid_identifier_55')
393 );
394 }
395
397 {
398 $entries = [
399 $this->getCopyrightEntry(13, 'first entry', 'https://www.example1.com'),
400 $this->getCopyrightEntry(55, '', null),
401 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
402 ];
404 $this->getCopyrightRepository(...$entries),
405 $this->getIdentifierHandler(),
406 $this->getSettings(true)
407 );
408
409 $this->assertSame(
410 '',
411 $handler->copyrightForExport('valid_identifier_55')
412 );
413 }
414
415 public function testCopyrightFromExportNoMatches(): void
416 {
417 $entries = [
418 $this->getCopyrightEntry(13, 'first entry', null),
419 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
420 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
421 ];
423 $this->getCopyrightRepository(...$entries),
424 $this->getIdentifierHandler(),
425 $this->getSettings(true)
426 );
427
428 $this->assertSame(
429 'just some text',
430 $handler->copyrightFromExport('just some text')
431 );
432 }
433
435 {
436 $entries = [
437 $this->getCopyrightEntry(13, 'first entry', null),
438 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
439 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
440 ];
442 $this->getCopyrightRepository(...$entries),
443 $this->getIdentifierHandler(),
444 $this->getSettings(true)
445 );
446
447 $this->assertSame(
448 'https://www.nonmatching.com',
449 $handler->copyrightFromExport('https://www.nonmatching.com')
450 );
451 $this->assertSame(
452 'http://www.nonmatching.com',
453 $handler->copyrightFromExport('http://www.nonmatching.com')
454 );
455 }
456
458 {
459 $entries = [
460 $this->getCopyrightEntry(13, 'first entry', null),
461 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
462 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
463 ];
465 $this->getCopyrightRepository(...$entries),
466 $this->getIdentifierHandler(),
467 $this->getSettings(true)
468 );
469
470 $this->assertSame(
471 'just some text which contains first entry',
472 $handler->copyrightFromExport('just some text which contains first entry')
473 );
474 }
475
477 {
478 $entries = [
479 $this->getCopyrightEntry(13, 'first entry', null),
480 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
481 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
482 ];
484 $this->getCopyrightRepository(...$entries),
485 $this->getIdentifierHandler(),
486 $this->getSettings(false)
487 );
488
489 $this->assertSame(
490 'some text containing http://www.example2.com',
491 $handler->copyrightFromExport('some text containing http://www.example2.com')
492 );
493 }
494
496 {
497 $entries = [
498 $this->getCopyrightEntry(13, 'first entry', null),
499 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
500 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
501 ];
503 $this->getCopyrightRepository(...$entries),
504 $this->getIdentifierHandler(),
505 $this->getSettings(true)
506 );
507
508 $this->assertSame(
509 'valid_identifier_13',
510 $handler->copyrightFromExport('first entry')
511 );
512 $this->assertSame(
513 'valid_identifier_55',
514 $handler->copyrightFromExport('second entry')
515 );
516 }
517
519 {
520 $entries = [
521 $this->getCopyrightEntry(13, 'first entry', null),
522 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
523 $this->getCopyrightEntry(123, 'second entry', 'https://www.example3.com/something')
524 ];
526 $this->getCopyrightRepository(...$entries),
527 $this->getIdentifierHandler(),
528 $this->getSettings(true)
529 );
530
531 $this->assertSame(
532 'valid_identifier_55',
533 $handler->copyrightFromExport('second entry')
534 );
535 }
536
538 {
539 $entries = [
540 $this->getCopyrightEntry(13, 'first entry', null),
541 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
542 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
543 ];
545 $this->getCopyrightRepository(...$entries),
546 $this->getIdentifierHandler(),
547 $this->getSettings(true)
548 );
549
550 $this->assertSame(
551 'valid_identifier_55',
552 $handler->copyrightFromExport('some text containing http://www.example2.com')
553 );
554 }
555
557 {
558 $entries = [
559 $this->getCopyrightEntry(13, 'first entry', null),
560 $this->getCopyrightEntry(55, 'second entry', 'https://www.example3.com/something'),
561 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
562 ];
564 $this->getCopyrightRepository(...$entries),
565 $this->getIdentifierHandler(),
566 $this->getSettings(true)
567 );
568
569 $this->assertSame(
570 'valid_identifier_55',
571 $handler->copyrightFromExport('some text containing https://www.example3.com/something')
572 );
573 }
574
576 {
577 $entries = [
578 $this->getCopyrightEntry(13, 'first entry', null),
579 $this->getCopyrightEntry(55, 'second entry', 'http://www.example2.com'),
580 $this->getCopyrightEntry(123, 'third entry', 'https://www.example3.com/something')
581 ];
583 $this->getCopyrightRepository(...$entries),
584 $this->getIdentifierHandler(),
585 $this->getSettings(true)
586 );
587
588 $this->assertSame(
589 'valid_identifier_55',
590 $handler->copyrightFromExport('some text containing https://www.example2.com')
591 );
592 }
593}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
getCopyrightEntry(int $id, string $full_name, ?string $link, bool $default=false)
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
link(string $caption, string $href, bool $new_viewport=false)
$handler
Definition: oai.php:29
$url
Definition: shib_logout.php:68