ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
PublisherTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
33use ILIAS\MetaData\OERHarvester\ExposedRecords\NullRepository as NullExposedRecordRepository;
47use ILIAS\MetaData\Repository\NullRepository as NullLOMRepository;
49
50class PublisherTest extends TestCase
51{
52 protected function getSettings(
53 int $editorial_ref_id = 0,
54 int $publishing_ref_id = 0,
55 bool $automatic_publishing_enabled = false,
57 return new class (
58 $editorial_ref_id,
59 $publishing_ref_id,
60 $automatic_publishing_enabled
61 ) extends NullSettings {
62 public function __construct(
63 protected int $editorial_ref_id,
64 protected int $publishing_ref_id,
65 protected bool $automatic_publishing_enabled
66 ) {
67 }
68
69 public function isAutomaticPublishingEnabled(): bool
70 {
71 return $this->automatic_publishing_enabled;
72 }
73
74 public function getContainerRefIDForEditorialStep(): int
75 {
76 return $this->editorial_ref_id;
77 }
78
79 public function getContainerRefIDForPublishing(): int
80 {
81 return $this->publishing_ref_id;
82 }
83 };
84 }
85
89 protected function getObjectHandler(
90 array $exclusive_ref_ids = [],
91 ): ObjectHandler {
92 return new class ($exclusive_ref_ids) extends NullObjectHandler {
93 public array $exposed_ref_creations = [];
94 public array $exposed_ref_deletions = [];
95
96 public function __construct(
97 protected array $exclusive_ref_ids
98 ) {
99 }
100
101 public function referenceObjectInTargetContainer(int $obj_id, int $container_ref_id): int
102 {
103 $new_ref_id = (int) ($container_ref_id . $obj_id);
104 $this->exposed_ref_creations[] = [
105 'obj_id' => $obj_id,
106 'container_ref_id' => $container_ref_id,
107 'new_ref_id' => $new_ref_id
108 ];
109 return $new_ref_id;
110 }
111
112 public function isOnlyReference(int $ref_id): bool
113 {
114 return in_array($ref_id, $this->exclusive_ref_ids);
115 }
116
117 public function deleteReference(int $ref_id): void
118 {
119 $this->exposed_ref_deletions[] = $ref_id;
120 }
121 };
122 }
123
124 protected function getExportHandler(
125 int ...$already_have_export_obj_ids
126 ): ExportHandler {
127 return new class ($already_have_export_obj_ids) extends NullExportHandler {
128 public array $exposed_created_exports_obj_ids = [];
129
130 public function __construct(
131 protected array $already_have_export_obj_ids
132 ) {
133 }
134
135 public function hasPublicAccessExport(int $obj_id): bool
136 {
137 return in_array($obj_id, $this->already_have_export_obj_ids);
138 }
139
140 public function createPublicAccessExport(int $obj_id): void
141 {
142 $this->exposed_created_exports_obj_ids[] = $obj_id;
143 }
144 };
145 }
146
150 protected function getStatusRepository(
151 array $currently_harvested = []
152 ): StatusRepository {
153 return new class ($currently_harvested) extends NullStatusRepository {
154 public array $exposed_blocks = [];
155 public array $exposed_deletions = [];
156 public array $exposed_creations = [];
157
158 public function __construct(
159 protected array $currently_harvested
160 ) {
161 }
162
163 public function setHarvestingBlocked(int $obj_id, bool $blocked): void
164 {
165 $this->exposed_blocks[] = [
166 'obj_id' => $obj_id,
167 'blocked' => $blocked
168 ];
169 }
170
171 public function getHarvestRefID(int $obj_id): int
172 {
173 return $this->currently_harvested[$obj_id];
174 }
175
176 public function deleteHarvestRefID(int $obj_id): void
177 {
178 $this->exposed_deletions[] = $obj_id;
179 }
180
181 public function setHarvestRefID(int $obj_id, int $harvested_ref_id): void
182 {
183 $this->exposed_creations[] = [
184 'obj_id' => $obj_id,
185 'href_id' => $harvested_ref_id
186 ];
187 }
188 };
189 }
190
191 protected function getExposedRecordRepository(
192 int ...$has_record_obj_ids,
193 ): ExposedRecordRepository {
194 return new class ($has_record_obj_ids) extends NullExposedRecordRepository {
195 public array $exposed_deletions = [];
196 public array $exposed_updates = [];
197 public array $exposed_creations = [];
198
199 public function __construct(
200 protected array $has_record_obj_ids
201 ) {
202 }
203
204 public function doesRecordExistForObjID(int $obj_id): bool
205 {
206 return in_array($obj_id, $this->has_record_obj_ids);
207 }
208
209 public function updateRecord(int $obj_id, bool $is_deleted, ?\DOMDocument $metadata): void
210 {
211 $this->exposed_updates[] = [
212 'obj_id' => $obj_id,
213 'deleted' => $is_deleted,
214 'metadata' => $metadata?->saveXML()
215 ];
216 }
217
218 public function createRecord(int $obj_id, string $identifier, \DOMDocument $metadata): void
219 {
220 $this->exposed_creations[] = [
221 'obj_id' => $obj_id,
222 'identifier' => $identifier,
223 'metadata' => $metadata->saveXML()
224 ];
225 }
226 };
227 }
228
232 protected function getXMLWriter(array $returned_md = []): SimpleDCXMLWriter
233 {
234 return new class ($returned_md) extends NullWriter {
235 public array $exposed_params = [];
236
237 public function __construct(protected array $returned_md)
238 {
239 }
240
241 public function writeSimpleDCMetaData(int $obj_id, int $ref_id, string $type): \DOMDocument
242 {
243 $this->exposed_params[] = [
244 'obj_id' => $obj_id,
245 'ref_id' => $ref_id,
246 'type' => $type
247 ];
248
249 $xml = new \DOMDocument();
250 $xml->loadXML($this->returned_md[$obj_id]);
251 return $xml;
252 }
253 };
254 }
255
256 protected function getNullAccess(): \ilAccess
257 {
258 return $this->createMock(\ilAccess::class);
259 }
260
261 public function testBlock(): void
262 {
263 $publisher = new Publisher(
264 $this->getExposedRecordRepository(),
265 $status_repo = $this->getStatusRepository(),
266 $this->getObjectHandler(),
267 $this->getExportHandler(),
268 $this->getSettings(),
269 $this->getXMLWriter(),
270 $this->getNullAccess()
271 );
272
273 $publisher->block(123);
274
275 $this->assertSame(
276 [[
277 'obj_id' => 123,
278 'blocked' => true
279 ]],
280 $status_repo->exposed_blocks
281 );
282 }
283
284 public function testUnblock(): void
285 {
286 $publisher = new Publisher(
287 $this->getExposedRecordRepository(),
288 $status_repo = $this->getStatusRepository(),
289 $this->getObjectHandler(),
290 $this->getExportHandler(),
291 $this->getSettings(),
292 $this->getXMLWriter(),
293 $this->getNullAccess()
294 );
295
296 $publisher->unblock(123);
297
298 $this->assertSame([['obj_id' => 123, 'blocked' => false]], $status_repo->exposed_blocks);
299 }
300
301 public function testPublish(): void
302 {
303 $md = <<<XML
304<?xml version="1.0"?>
305<md>metadata</md>
306
307XML;
308 $publisher = new Publisher(
309 $exposed_repo = $this->getExposedRecordRepository(),
310 $status_repo = $this->getStatusRepository(),
311 $object_handler = $this->getObjectHandler(),
312 $export_handler = $this->getExportHandler(),
313 $this->getSettings(0, 456),
314 $this->getXMLWriter([123 => $md]),
315 $this->getNullAccess()
316 );
317
318 $publisher->publish(123, 'type');
319
320 $this->assertSame(
321 [[
322 'obj_id' => 123,
323 'container_ref_id' => 456,
324 'new_ref_id' => 456123
325 ]],
326 $object_handler->exposed_ref_creations
327 );
328 $this->assertSame(
329 [[
330 'obj_id' => 123,
331 'href_id' => 456123
332 ]],
333 $status_repo->exposed_creations
334 );
335 $this->assertSame(
336 [123],
337 $export_handler->exposed_created_exports_obj_ids
338 );
339 $this->assertSame(
340 [[
341 'obj_id' => 123,
342 'identifier' => 'il__type_123',
343 'metadata' => $md
344 ]],
345 $exposed_repo->exposed_creations
346 );
347 }
348
350 {
351 $md = <<<XML
352<?xml version="1.0"?>
353<md>metadata</md>
354
355XML;
356 $publisher = new Publisher(
357 $exposed_repo = $this->getExposedRecordRepository(),
358 $status_repo = $this->getStatusRepository(),
359 $object_handler = $this->getObjectHandler(),
360 $export_handler = $this->getExportHandler(123),
361 $this->getSettings(0, 456),
362 $this->getXMLWriter([123 => $md]),
363 $this->getNullAccess()
364 );
365
366 $publisher->publish(123, 'type');
367
368 $this->assertSame(
369 [[
370 'obj_id' => 123,
371 'container_ref_id' => 456,
372 'new_ref_id' => 456123
373 ]],
374 $object_handler->exposed_ref_creations
375 );
376 $this->assertSame(
377 [[
378 'obj_id' => 123,
379 'href_id' => 456123
380 ]],
381 $status_repo->exposed_creations
382 );
383 $this->assertEmpty($export_handler->exposed_created_exports_obj_ids);
384 $this->assertSame(
385 [[
386 'obj_id' => 123,
387 'identifier' => 'il__type_123',
388 'metadata' => $md
389 ]],
390 $exposed_repo->exposed_creations
391 );
392 }
393
395 {
396 $md = <<<XML
397<?xml version="1.0"?>
398<md>metadata</md>
399
400XML;
401 $publisher = new Publisher(
402 $exposed_repo = $this->getExposedRecordRepository(123),
403 $status_repo = $this->getStatusRepository(),
404 $object_handler = $this->getObjectHandler(),
405 $export_handler = $this->getExportHandler(),
406 $this->getSettings(0, 456),
407 $this->getXMLWriter([123 => $md]),
408 $this->getNullAccess()
409 );
410
411 $publisher->publish(123, 'type');
412
413 $this->assertSame(
414 [[
415 'obj_id' => 123,
416 'container_ref_id' => 456,
417 'new_ref_id' => 456123
418 ]],
419 $object_handler->exposed_ref_creations
420 );
421 $this->assertSame(
422 [[
423 'obj_id' => 123,
424 'href_id' => 456123
425 ]],
426 $status_repo->exposed_creations
427 );
428 $this->assertSame(
429 [123],
430 $export_handler->exposed_created_exports_obj_ids
431 );
432 $this->assertSame(
433 [[
434 'obj_id' => 123,
435 'deleted' => false,
436 'metadata' => $md
437 ]],
438 $exposed_repo->exposed_updates
439 );
440 }
441
442 public function testWithdraw(): void
443 {
444 $publisher = new Publisher(
445 $exposed_repo = $this->getExposedRecordRepository(),
446 $status_repo = $this->getStatusRepository([123 => 456123]),
447 $object_handler = $this->getObjectHandler(),
448 $this->getExportHandler(),
449 $this->getSettings(),
450 $this->getXMLWriter(),
451 $this->getNullAccess()
452 );
453
454 $publisher->withdraw(123);
455
456 $this->assertSame(
457 [456123],
458 $object_handler->exposed_ref_deletions
459 );
460 $this->assertSame(
461 [123],
462 $status_repo->exposed_deletions
463 );
464 $this->assertSame(
465 [[
466 'obj_id' => 123,
467 'deleted' => true,
468 'metadata' => null
469 ]],
470 $exposed_repo->exposed_updates
471 );
472 $this->assertEmpty($status_repo->exposed_blocks);
473 }
474
475 public function testWithdrawWithOnlyOneReference(): void
476 {
477 $publisher = new Publisher(
478 $exposed_repo = $this->getExposedRecordRepository(),
479 $status_repo = $this->getStatusRepository([123 => 456123]),
480 $object_handler = $this->getObjectHandler([456123]),
481 $this->getExportHandler(),
482 $this->getSettings(),
483 $this->getXMLWriter(),
484 $this->getNullAccess()
485 );
486
487 $publisher->withdraw(123);
488
489 $this->assertEmpty($object_handler->exposed_ref_deletions);
490 $this->assertSame(
491 [123],
492 $status_repo->exposed_deletions
493 );
494 $this->assertSame(
495 [[
496 'obj_id' => 123,
497 'deleted' => true,
498 'metadata' => null
499 ]],
500 $exposed_repo->exposed_updates
501 );
502 $this->assertEmpty($status_repo->exposed_blocks);
503 }
504
506 {
507 $publisher = new Publisher(
508 $exposed_repo = $this->getExposedRecordRepository(),
509 $status_repo = $this->getStatusRepository([123 => 456123]),
510 $object_handler = $this->getObjectHandler(),
511 $this->getExportHandler(),
512 $this->getSettings(0, 0, true),
513 $this->getXMLWriter(),
514 $this->getNullAccess()
515 );
516
517 $publisher->withdraw(123);
518
519 $this->assertSame(
520 [456123],
521 $object_handler->exposed_ref_deletions
522 );
523 $this->assertSame(
524 [123],
525 $status_repo->exposed_deletions
526 );
527 $this->assertSame(
528 [[
529 'obj_id' => 123,
530 'deleted' => true,
531 'metadata' => null
532 ]],
533 $exposed_repo->exposed_updates
534 );
535 $this->assertSame(
536 [[
537 'obj_id' => 123,
538 'blocked' => true
539 ]],
540 $status_repo->exposed_blocks
541 );
542 }
543
544 public function testSubmit(): void
545 {
546 $publisher = new Publisher(
547 $this->getExposedRecordRepository(),
548 $status_repo = $this->getStatusRepository(),
549 $object_handler = $this->getObjectHandler(),
550 $export_handler = $this->getExportHandler(),
551 $this->getSettings(456),
552 $this->getXMLWriter(),
553 $this->getNullAccess()
554 );
555
556 $publisher->submit(123);
557
558 $this->assertSame(
559 [[
560 'obj_id' => 123,
561 'container_ref_id' => 456,
562 'new_ref_id' => 456123
563 ]],
564 $object_handler->exposed_ref_creations
565 );
566 $this->assertSame(
567 [[
568 'obj_id' => 123,
569 'href_id' => 456123
570 ]],
571 $status_repo->exposed_creations
572 );
573 $this->assertSame(
574 [123],
575 $export_handler->exposed_created_exports_obj_ids
576 );
577 }
578
580 {
581 $publisher = new Publisher(
582 $this->getExposedRecordRepository(),
583 $status_repo = $this->getStatusRepository(),
584 $object_handler = $this->getObjectHandler(),
585 $export_handler = $this->getExportHandler(123),
586 $this->getSettings(456),
587 $this->getXMLWriter(),
588 $this->getNullAccess()
589 );
590
591 $publisher->submit(123);
592
593 $this->assertSame(
594 [[
595 'obj_id' => 123,
596 'container_ref_id' => 456,
597 'new_ref_id' => 456123
598 ]],
599 $object_handler->exposed_ref_creations
600 );
601 $this->assertSame(
602 [[
603 'obj_id' => 123,
604 'href_id' => 456123
605 ]],
606 $status_repo->exposed_creations
607 );
608 $this->assertEmpty($export_handler->exposed_created_exports_obj_ids);
609 }
610
611 public function testAccept(): void
612 {
613 $md = <<<XML
614<?xml version="1.0"?>
615<md>metadata</md>
616
617XML;
618 $publisher = new Publisher(
619 $exposed_repo = $this->getExposedRecordRepository(),
620 $status_repo = $this->getStatusRepository([123 => 456123]),
621 $object_handler = $this->getObjectHandler(),
622 $this->getExportHandler(),
623 $this->getSettings(456, 789),
624 $this->getXMLWriter([123 => $md]),
625 $this->getNullAccess()
626 );
627
628 $publisher->accept(123, 'type');
629
630 $this->assertSame(
631 [[
632 'obj_id' => 123,
633 'container_ref_id' => 789,
634 'new_ref_id' => 789123
635 ]],
636 $object_handler->exposed_ref_creations
637 );
638 $this->assertSame(
639 [456123],
640 $object_handler->exposed_ref_deletions
641 );
642 $this->assertSame(
643 [[
644 'obj_id' => 123,
645 'href_id' => 789123
646 ]],
647 $status_repo->exposed_creations
648 );
649 $this->assertSame(
650 [[
651 'obj_id' => 123,
652 'identifier' => 'il__type_123',
653 'metadata' => $md
654 ]],
655 $exposed_repo->exposed_creations
656 );
657 }
658
660 {
661 $md = <<<XML
662<?xml version="1.0"?>
663<md>metadata</md>
664
665XML;
666 $publisher = new Publisher(
667 $exposed_repo = $this->getExposedRecordRepository(123),
668 $status_repo = $this->getStatusRepository([123 => 456123]),
669 $object_handler = $this->getObjectHandler(),
670 $this->getExportHandler(),
671 $this->getSettings(456, 789),
672 $this->getXMLWriter([123 => $md]),
673 $this->getNullAccess()
674 );
675
676 $publisher->accept(123, 'type');
677
678 $this->assertSame(
679 [[
680 'obj_id' => 123,
681 'container_ref_id' => 789,
682 'new_ref_id' => 789123
683 ]],
684 $object_handler->exposed_ref_creations
685 );
686 $this->assertSame(
687 [456123],
688 $object_handler->exposed_ref_deletions
689 );
690 $this->assertSame(
691 [[
692 'obj_id' => 123,
693 'href_id' => 789123
694 ]],
695 $status_repo->exposed_creations
696 );
697 $this->assertSame(
698 [[
699 'obj_id' => 123,
700 'deleted' => false,
701 'metadata' => $md
702 ]],
703 $exposed_repo->exposed_updates
704 );
705 }
706
707 public function testReject(): void
708 {
709 $publisher = new Publisher(
710 $this->getExposedRecordRepository(),
711 $status_repo = $this->getStatusRepository([123 => 456123]),
712 $object_handler = $this->getObjectHandler(),
713 $this->getExportHandler(),
714 $this->getSettings(),
715 $this->getXMLWriter(),
716 $this->getNullAccess()
717 );
718
719 $publisher->reject(123);
720
721 $this->assertSame(
722 [456123],
723 $object_handler->exposed_ref_deletions
724 );
725 $this->assertSame(
726 [123],
727 $status_repo->exposed_deletions
728 );
729 $this->assertEmpty($status_repo->exposed_blocks);
730 }
731
732 public function testRejectWithOnlyOneReference(): void
733 {
734 $publisher = new Publisher(
735 $this->getExposedRecordRepository(),
736 $status_repo = $this->getStatusRepository([123 => 456123]),
737 $object_handler = $this->getObjectHandler([456123]),
738 $this->getExportHandler(),
739 $this->getSettings(),
740 $this->getXMLWriter(),
741 $this->getNullAccess()
742 );
743
744 $publisher->reject(123);
745
746 $this->assertEmpty($object_handler->exposed_ref_deletions);
747 $this->assertSame(
748 [123],
749 $status_repo->exposed_deletions
750 );
751 $this->assertEmpty($status_repo->exposed_blocks);
752 }
753
754 public function testRejectWithAutomaticPublishing(): void
755 {
756 $publisher = new Publisher(
757 $this->getExposedRecordRepository(),
758 $status_repo = $this->getStatusRepository([123 => 456123]),
759 $object_handler = $this->getObjectHandler(),
760 $this->getExportHandler(),
761 $this->getSettings(0, 0, true),
762 $this->getXMLWriter(),
763 $this->getNullAccess()
764 );
765
766 $publisher->reject(123);
767
768 $this->assertSame(
769 [456123],
770 $object_handler->exposed_ref_deletions
771 );
772 $this->assertSame(
773 [123],
774 $status_repo->exposed_deletions
775 );
776 $this->assertSame(
777 [[
778 'obj_id' => 123,
779 'blocked' => true
780 ]],
781 $status_repo->exposed_blocks
782 );
783 }
784}
getXMLWriter(array $returned_md=[])
Metadata is passed as array via obj_id => metadata-xml as string.
getExportHandler(int ... $already_have_export_obj_ids)
getSettings(int $editorial_ref_id=0, int $publishing_ref_id=0, bool $automatic_publishing_enabled=false,)
getObjectHandler(array $exclusive_ref_ids=[],)
Returned ref_ids are always given by concatenation of target ref_id and obj_id.
getStatusRepository(array $currently_harvested=[])
Currently harvested objects are passed as obj_id => href_id.
getExposedRecordRepository(int ... $has_record_obj_ids,)
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
Class ilAccessHandler Checks access for ILIAS objects.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:66
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc