ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
HarvesterTest.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;
48
49class HarvesterTest extends TestCase
50{
51 protected function getSettings(
52 array $types = [],
53 array $copyright_ids = [],
54 int $harvesting_target_ref_id = 0,
55 int $exposed_source_ref_id = 0
57 return new class (
58 $types,
59 $copyright_ids,
60 $harvesting_target_ref_id,
61 $exposed_source_ref_id
62 ) extends NullSettings {
63 public function __construct(
64 protected array $types,
65 protected array $copyright_ids,
66 protected int $harvesting_target_ref_id,
67 protected int $exposed_source_ref_id
68 ) {
69 }
70
71 public function getObjectTypesSelectedForHarvesting(): array
72 {
73 return $this->types;
74 }
75
76 public function getCopyrightEntryIDsSelectedForHarvesting(): array
77 {
78 return $this->copyright_ids;
79 }
80
81 public function getContainerRefIDForHarvesting(): int
82 {
83 return $this->harvesting_target_ref_id;
84 }
85
86 public function getContainerRefIDForExposing(): int
87 {
88 return $this->exposed_source_ref_id;
89 }
90 };
91 }
92
97 protected function getObjectHandler(
98 array $deleted_obj_ids = [],
99 int $valid_source_container = 0,
100 array $obj_ids_referenced_in_container = [],
101 ?int $throw_error_on_deletion_ref_id = null,
102 ?int $throw_error_on_ref_creation_obj_id = null
103 ): ObjectHandler {
104 return new class (
105 $deleted_obj_ids,
106 $valid_source_container,
107 $obj_ids_referenced_in_container,
108 $throw_error_on_deletion_ref_id,
109 $throw_error_on_ref_creation_obj_id
110 ) extends NullObjectHandler {
111 public array $exposed_ref_creations = [];
112 public array $exposed_ref_deletions = [];
113
114 public function __construct(
115 protected array $deleted_obj_ids,
116 protected int $valid_target_container,
117 protected array $obj_ids_referenced_in_container,
118 protected ?int $throw_error_on_deletion_ref_id = null,
119 protected ?int $throw_error_on_ref_creation_obj_id = null
120 ) {
121 }
122
123 public function referenceObjectInTargetContainer(int $obj_id, int $container_ref_id): int
124 {
125 if ($obj_id === $this->throw_error_on_ref_creation_obj_id) {
126 throw new \ilMDOERHarvesterException('error');
127 }
128 $new_ref_id = (int) ($container_ref_id . $obj_id);
129 $this->exposed_ref_creations[] = [
130 'obj_id' => $obj_id,
131 'container_ref_id' => $container_ref_id,
132 'new_ref_id' => $new_ref_id
133 ];
134 return $new_ref_id;
135 }
136
137 public function getObjectReferenceIDInContainer(int $obj_id, int $container_ref_id): ?int
138 {
139 if (in_array($obj_id, $this->obj_ids_referenced_in_container)) {
140 return (int) ($container_ref_id . $obj_id);
141 }
142 return null;
143 }
144
145 public function isObjectDeleted(int $obj_id): bool
146 {
147 return in_array($obj_id, $this->deleted_obj_ids);
148 }
149
150 public function deleteReference(int $ref_id): void
151 {
152 if ($ref_id === $this->throw_error_on_deletion_ref_id) {
153 throw new \ilMDOERHarvesterException('error');
154 }
155 $this->exposed_ref_deletions[] = $ref_id;
156 }
157
158 public function getTypeOfReferencedObject(int $ref_id): string
159 {
160 return 'type_' . $ref_id;
161 }
162 };
163 }
164
165 protected function getExportHandler(
166 ?int $throw_exception_for_id = null,
167 int ...$already_have_export_obj_ids
168 ): ExportHandler {
169 return new class ($throw_exception_for_id, $already_have_export_obj_ids) extends NullExportHandler {
170 public array $exposed_created_exports_obj_ids = [];
171
172 public function __construct(
173 protected ?int $throw_exception_for_id,
174 protected array $already_have_export_obj_ids
175 ) {
176 }
177
178 public function hasPublicAccessExport(int $obj_id): bool
179 {
180 if ($this->throw_exception_for_id === $obj_id) {
181 throw new \Exception('error');
182 }
183 return in_array($obj_id, $this->already_have_export_obj_ids);
184 }
185
186 public function createPublicAccessExport(int $obj_id): void
187 {
188 if ($this->throw_exception_for_id === $obj_id) {
189 throw new \Exception('error');
190 }
191 $this->exposed_created_exports_obj_ids[] = $obj_id;
192 }
193 };
194 }
195
199 protected function getStatusRepository(
200 array $currently_harvested = [],
201 array $blocked_obj_ids = [],
202 bool $throw_error = false
203 ): StatusRepository {
204 return new class ($currently_harvested, $blocked_obj_ids, $throw_error) extends NullStatusRepository {
205 public array $exposed_deletions = [];
206 public array $exposed_creations = [];
207
208 public function __construct(
209 protected array $currently_harvested,
210 protected array $blocked_obj_ids,
211 protected bool $throw_error
212 ) {
213 }
214
215 public function getAllHarvestedObjIDs(): \Generator
216 {
217 if ($this->throw_error === true) {
218 throw new \ilMDOERHarvesterException('error');
219 }
220 yield from array_keys($this->currently_harvested);
221 }
222
223 public function filterOutBlockedObjects(int ...$obj_ids): \Generator
224 {
225 foreach ($obj_ids as $obj_id) {
226 if (!in_array($obj_id, $this->blocked_obj_ids)) {
227 yield $obj_id;
228 }
229 }
230 }
231
232 public function getHarvestRefID(int $obj_id): int
233 {
234 return $this->currently_harvested[$obj_id];
235 }
236
237 public function deleteHarvestRefID(int $obj_id): void
238 {
239 $this->exposed_deletions[] = $obj_id;
240 }
241
242 public function setHarvestRefID(int $obj_id, int $harvested_ref_id): void
243 {
244 $this->exposed_creations[] = [
245 'obj_id' => $obj_id,
246 'href_id' => $harvested_ref_id
247 ];
248 }
249 };
250 }
251
257 protected function getExposedRecordRepository(
258 array $returned_records = [],
259 array $deleted_records = []
260 ): ExposedRecordRepository {
261 return new class ($returned_records, $deleted_records) extends NullExposedRecordRepository {
262 public array $exposed_deletions = [];
263 public array $exposed_updates = [];
264 public array $exposed_creations = [];
265
266 public function __construct(
267 protected array $returned_records,
268 protected array $deleted_records
269 ) {
270 }
271
272 public function getRecords(
273 ?\DateTimeImmutable $from = null,
274 ?\DateTimeImmutable $until = null,
275 ?int $limit = null,
276 ?int $offset = null
277 ): \Generator {
278 foreach ($this->returned_records as $obj_id => $metadata) {
279 $is_deleted = in_array($obj_id, $this->deleted_records);
280 yield new class ($obj_id, $is_deleted, $metadata) extends NullRecord {
281 public function __construct(
282 protected int $obj_id,
283 protected bool $is_deleted,
284 protected string $metadata
285 ) {
286 }
287
288 public function infos(): RecordInfosInterface
289 {
290 return new class ($this->obj_id, $this->is_deleted) extends NullRecordInfos {
291 public function __construct(
292 protected int $obj_id,
293 protected bool $is_deleted
294 ) {
295 }
296
297 public function objID(): int
298 {
299 return $this->obj_id;
300 }
301
302 public function isDeleted(): bool
303 {
304 return $this->is_deleted;
305 }
306 };
307 }
308
309 public function metadata(): \DOMDocument
310 {
311 $xml = new \DOMDocument();
312 $xml->loadXML($this->metadata);
313 return $xml;
314 }
315 };
316 }
317 }
318
319 public function deleteRecordsMarkedAsDeletedOlderThan(\DateInterval $interval): void
320 {
321 $this->exposed_deletions[] = ['interval' => $interval];
322 }
323
324 public function updateRecord(int $obj_id, bool $is_deleted, ?\DOMDocument $metadata): void
325 {
326 $this->exposed_updates[] = [
327 'obj_id' => $obj_id,
328 'deleted' => $is_deleted,
329 'metadata' => $metadata?->saveXML()
330 ];
331 }
332
333 public function createRecord(int $obj_id, string $identifier, \DOMDocument $metadata): void
334 {
335 $this->exposed_creations[] = [
336 'obj_id' => $obj_id,
337 'identifier' => $identifier,
338 'metadata' => $metadata->saveXML()
339 ];
340 }
341 };
342 }
343
344 protected function getSearchFactory(int ...$search_result_obj_ids): SearchFactory
345 {
346 return new class ($search_result_obj_ids) extends NullFactory {
347 public array $exposed_search_params;
348
349 public function __construct(public array $search_result_obj_ids)
350 {
351 }
352
353 public function get(): SearcherInterface
354 {
355 return new class ($this) extends NullSearcher {
356 protected array $types = [];
357 protected bool $restricted_to_repository = false;
358
359 public function __construct(protected SearchFactory $factory)
360 {
361 }
362
363 public function withRestrictionToRepositoryObjects(bool $restricted): SearcherInterface
364 {
365 $clone = clone $this;
366 $clone->restricted_to_repository = $restricted;
367 return $clone;
368 }
369
370 public function withAdditionalTypeFilter(string $type): SearcherInterface
371 {
372 $clone = clone $this;
373 $clone->types[] = $type;
374 return $clone;
375 }
376
377 public function search(
378 LOMRepository $lom_repository,
379 int $first_entry_id,
380 int ...$further_entry_ids
381 ): \Generator {
382 $this->factory->exposed_search_params[] = [
383 'restricted' => $this->restricted_to_repository,
384 'types' => $this->types,
385 'entries' => [$first_entry_id, ...$further_entry_ids]
386 ];
387 foreach ($this->factory->search_result_obj_ids as $obj_id) {
388 yield new class ($obj_id) extends NullRessourceID {
389 public function __construct(protected int $obj_id)
390 {
391 }
392
393 public function objID(): int
394 {
395 return $this->obj_id;
396 }
397 };
398 }
399 }
400 };
401 }
402 };
403 }
404
408 protected function getXMLWriter(array $returned_md = []): SimpleDCXMLWriter
409 {
410 return new class ($returned_md) extends NullWriter {
411 public array $exposed_params = [];
412
413 public function __construct(protected array $returned_md)
414 {
415 }
416
417 public function writeSimpleDCMetaData(int $obj_id, int $ref_id, string $type): \DOMDocument
418 {
419 $this->exposed_params[] = [
420 'obj_id' => $obj_id,
421 'ref_id' => $ref_id,
422 'type' => $type
423 ];
424
425 $xml = new \DOMDocument();
426 $xml->loadXML($this->returned_md[$obj_id]);
427 return $xml;
428 }
429 };
430 }
431
432 protected function getNullLogger(): \ilLogger
433 {
434 return $this->createMock(\ilLogger::class);
435 }
436
438 {
439 return new class () extends NullWrapper {
440 public int $exposed_status;
441 public string $exposed_message;
442
443 public function withMessage(string $message): WrapperInterface
444 {
445 $clone = clone $this;
446 $clone->exposed_message = $message;
447 return $clone;
448 }
449
450 public function withStatus(int $status): WrapperInterface
451 {
452 $clone = clone $this;
453 $clone->exposed_status = $status;
454 return $clone;
455 }
456 };
457 }
458
460 {
461 $harvester = new Harvester(
462 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
463 $object_handler = $this->getObjectHandler(),
464 $this->getExportHandler(),
465 $status_repo = $this->getStatusRepository([32 => 12332, 45 => 12345]),
466 $this->getExposedRecordRepository(),
467 $search_factory = $this->getSearchFactory(45),
468 new NullLOMRepository(),
469 $this->getXMLWriter(),
470 $this->getNullLogger()
471 );
472
473 $result = $harvester->run($this->getCronResultWrapper());
474
475 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
476 $this->assertSame(
477 'Deleted 1 deprecated references.<br>' .
478 'Created 0 new references.<br>' .
479 'Created, updated, or deleted 0 exposed records.',
480 $result->exposed_message
481 );
482 $this->assertSame(
483 [['restricted' => true, 'types' => ['type', 'second type'], 'entries' => [12, 5]]],
484 $search_factory->exposed_search_params
485 );
486 $this->assertSame([32], $status_repo->exposed_deletions);
487 $this->assertSame([12332], $object_handler->exposed_ref_deletions);
488 }
489
491 {
492 $harvester = new Harvester(
493 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
494 $object_handler = $this->getObjectHandler(),
495 $this->getExportHandler(),
496 $status_repo = $this->getStatusRepository([32 => 12332, 45 => 12345], [32]),
497 $this->getExposedRecordRepository(),
498 $this->getSearchFactory(45, 32),
499 new NullLOMRepository(),
500 $this->getXMLWriter(),
501 $this->getNullLogger()
502 );
503
504 $result = $harvester->run($this->getCronResultWrapper());
505
506 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
507 $this->assertSame(
508 'Deleted 1 deprecated references.<br>' .
509 'Created 0 new references.<br>' .
510 'Created, updated, or deleted 0 exposed records.',
511 $result->exposed_message
512 );
513 $this->assertSame([32], $status_repo->exposed_deletions);
514 $this->assertSame([12332], $object_handler->exposed_ref_deletions);
515 }
516
518 {
519 $harvester = new Harvester(
520 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
521 $object_handler = $this->getObjectHandler([32]),
522 $this->getExportHandler(),
523 $status_repo = $this->getStatusRepository([32 => 12332, 45 => 12345]),
524 $this->getExposedRecordRepository(),
525 $this->getSearchFactory(45, 32),
526 new NullLOMRepository(),
527 $this->getXMLWriter(),
528 $this->getNullLogger()
529 );
530
531 $result = $harvester->run($this->getCronResultWrapper());
532
533 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
534 $this->assertSame(
535 'Deleted 1 deprecated references.<br>' .
536 'Created 0 new references.<br>' .
537 'Created, updated, or deleted 0 exposed records.',
538 $result->exposed_message
539 );
540 $this->assertSame([32], $status_repo->exposed_deletions);
541 $this->assertSame([12332], $object_handler->exposed_ref_deletions);
542 }
543
545 {
546 $harvester = new Harvester(
547 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
548 $object_handler = $this->getObjectHandler([], 0, [], 12345),
549 $this->getExportHandler(),
550 $status_repo = $this->getStatusRepository([32 => 12332, 45 => 12345, 67 => 12367]),
551 $this->getExposedRecordRepository(),
552 $this->getSearchFactory(),
553 new NullLOMRepository(),
554 $this->getXMLWriter(),
555 $this->getNullLogger()
556 );
557
558 $result = $harvester->run($this->getCronResultWrapper());
559
560 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
561 $this->assertSame(
562 'Deleted 2 deprecated references.<br>' .
563 'Created 0 new references.<br>' .
564 'Created, updated, or deleted 0 exposed records.',
565 $result->exposed_message
566 );
567 $this->assertSame([32, 67], $status_repo->exposed_deletions);
568 $this->assertSame([12332, 12367], $object_handler->exposed_ref_deletions);
569 }
570
571 public function testRunHarvestObject(): void
572 {
573 $harvester = new Harvester(
574 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
575 $object_handler = $this->getObjectHandler(),
576 $export_handler = $this->getExportHandler(),
577 $status_repo = $this->getStatusRepository([32 => 12332]),
578 $this->getExposedRecordRepository(),
579 $search_factory = $this->getSearchFactory(32, 45),
580 new NullLOMRepository(),
581 $this->getXMLWriter(),
582 $this->getNullLogger()
583 );
584
585 $result = $harvester->run($this->getCronResultWrapper());
586
587 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
588 $this->assertSame(
589 'Deleted 0 deprecated references.<br>' .
590 'Created 1 new references.<br>' .
591 'Created, updated, or deleted 0 exposed records.',
592 $result->exposed_message
593 );
594 $this->assertSame(
595 [[
596 'restricted' => true,
597 'types' => ['type', 'second type'],
598 'entries' => [12, 5]
599 ]],
600 $search_factory->exposed_search_params
601 );
602 $this->assertSame(
603 [['obj_id' => 45, 'href_id' => 12345]],
604 $status_repo->exposed_creations
605 );
606 $this->assertSame(
607 [['obj_id' => 45, 'container_ref_id' => 123, 'new_ref_id' => 12345]],
608 $object_handler->exposed_ref_creations
609 );
610 $this->assertSame([45], $export_handler->exposed_created_exports_obj_ids);
611 }
612
613 public function testRunDoNotHarvestBlockedObject(): void
614 {
615 $harvester = new Harvester(
616 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
617 $object_handler = $this->getObjectHandler(),
618 $export_handler = $this->getExportHandler(),
619 $status_repo = $this->getStatusRepository([32 => 12332], [45]),
620 $this->getExposedRecordRepository(),
621 $this->getSearchFactory(32, 45),
622 new NullLOMRepository(),
623 $this->getXMLWriter(),
624 $this->getNullLogger()
625 );
626
627 $result = $harvester->run($this->getCronResultWrapper());
628
629 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_NO_ACTION, $result->exposed_status);
630 $this->assertSame(
631 'Deleted 0 deprecated references.<br>' .
632 'Created 0 new references.<br>' .
633 'Created, updated, or deleted 0 exposed records.',
634 $result->exposed_message
635 );
636 $this->assertEmpty($status_repo->exposed_creations);
637 $this->assertEmpty($object_handler->exposed_ref_creations);
638 $this->assertEmpty($export_handler->exposed_created_exports_obj_ids);
639 }
640
641 public function testRunDoNotHarvestDeletedObject(): void
642 {
643 $harvester = new Harvester(
644 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
645 $object_handler = $this->getObjectHandler([45]),
646 $export_handler = $this->getExportHandler(),
647 $status_repo = $this->getStatusRepository([32 => 12332]),
648 $this->getExposedRecordRepository(),
649 $this->getSearchFactory(32, 45),
650 new NullLOMRepository(),
651 $this->getXMLWriter(),
652 $this->getNullLogger()
653 );
654
655 $result = $harvester->run($this->getCronResultWrapper());
656
657 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_NO_ACTION, $result->exposed_status);
658 $this->assertSame(
659 'Deleted 0 deprecated references.<br>' .
660 'Created 0 new references.<br>' .
661 'Created, updated, or deleted 0 exposed records.',
662 $result->exposed_message
663 );
664 $this->assertEmpty($status_repo->exposed_creations);
665 $this->assertEmpty($object_handler->exposed_ref_creations);
666 $this->assertEmpty($export_handler->exposed_created_exports_obj_ids);
667 }
668
670 {
671 $harvester = new Harvester(
672 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
673 $object_handler = $this->getObjectHandler(),
674 $export_handler = $this->getExportHandler(),
675 $status_repo = $this->getStatusRepository([32 => 12332, 45 => 12345]),
676 $this->getExposedRecordRepository(),
677 $this->getSearchFactory(32, 45),
678 new NullLOMRepository(),
679 $this->getXMLWriter(),
680 $this->getNullLogger()
681 );
682
683 $result = $harvester->run($this->getCronResultWrapper());
684
685 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_NO_ACTION, $result->exposed_status);
686 $this->assertSame(
687 'Deleted 0 deprecated references.<br>' .
688 'Created 0 new references.<br>' .
689 'Created, updated, or deleted 0 exposed records.',
690 $result->exposed_message
691 );
692 $this->assertEmpty($status_repo->exposed_creations);
693 $this->assertEmpty($object_handler->exposed_ref_creations);
694 $this->assertEmpty($export_handler->exposed_created_exports_obj_ids);
695 }
696
698 {
699 $harvester = new Harvester(
700 $this->getSettings(['type', 'second type'], [12, 5], 0, 456),
701 $object_handler = $this->getObjectHandler(),
702 $export_handler = $this->getExportHandler(),
703 $status_repo = $this->getStatusRepository([32 => 12332]),
704 $this->getExposedRecordRepository(),
705 $this->getSearchFactory(32, 45),
706 new NullLOMRepository(),
707 $this->getXMLWriter(),
708 $this->getNullLogger()
709 );
710
711 $result = $harvester->run($this->getCronResultWrapper());
712
713 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_NO_ACTION, $result->exposed_status);
714 $this->assertSame(
715 'Deleted 0 deprecated references.<br>' .
716 'Created 0 new references.<br>' .
717 'Created, updated, or deleted 0 exposed records.',
718 $result->exposed_message
719 );
720 $this->assertEmpty($status_repo->exposed_creations);
721 $this->assertEmpty($object_handler->exposed_ref_creations);
722 $this->assertEmpty($export_handler->exposed_created_exports_obj_ids);
723 }
724
726 {
727 $harvester = new Harvester(
728 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
729 $object_handler = $this->getObjectHandler([], 0, [], null, 45),
730 $export_handler = $this->getExportHandler(),
731 $status_repo = $this->getStatusRepository(),
732 $this->getExposedRecordRepository(),
733 $this->getSearchFactory(32, 45, 67),
734 new NullLOMRepository(),
735 $this->getXMLWriter(),
736 $this->getNullLogger()
737 );
738
739 $result = $harvester->run($this->getCronResultWrapper());
740
741 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
742 $this->assertSame(
743 'Deleted 0 deprecated references.<br>' .
744 'Created 2 new references.<br>' .
745 'Created, updated, or deleted 0 exposed records.',
746 $result->exposed_message
747 );
748 $this->assertSame(
749 [
750 ['obj_id' => 32, 'href_id' => 12332],
751 ['obj_id' => 67, 'href_id' => 12367]
752 ],
753 $status_repo->exposed_creations
754 );
755 $this->assertSame(
756 [
757 ['obj_id' => 32, 'container_ref_id' => 123, 'new_ref_id' => 12332],
758 ['obj_id' => 67, 'container_ref_id' => 123, 'new_ref_id' => 12367]
759 ],
760 $object_handler->exposed_ref_creations
761 );
762 $this->assertSame([32, 67], $export_handler->exposed_created_exports_obj_ids);
763 }
764
766 {
767 $harvester = new Harvester(
768 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
769 $object_handler = $this->getObjectHandler(),
770 $export_handler = $this->getExportHandler(null, 45),
771 $status_repo = $this->getStatusRepository([32 => 12332]),
772 $this->getExposedRecordRepository(),
773 $search_factory = $this->getSearchFactory(32, 45),
774 new NullLOMRepository(),
775 $this->getXMLWriter(),
776 $this->getNullLogger()
777 );
778
779 $result = $harvester->run($this->getCronResultWrapper());
780
781 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
782 $this->assertSame(
783 'Deleted 0 deprecated references.<br>' .
784 'Created 1 new references.<br>' .
785 'Created, updated, or deleted 0 exposed records.',
786 $result->exposed_message
787 );
788 $this->assertSame(
789 [[
790 'restricted' => true,
791 'types' => ['type', 'second type'],
792 'entries' => [12, 5]
793 ]],
794 $search_factory->exposed_search_params
795 );
796 $this->assertSame(
797 [['obj_id' => 45, 'href_id' => 12345]],
798 $status_repo->exposed_creations
799 );
800 $this->assertSame(
801 [['obj_id' => 45, 'container_ref_id' => 123, 'new_ref_id' => 12345]],
802 $object_handler->exposed_ref_creations
803 );
804 $this->assertEmpty($export_handler->exposed_created_exports_obj_ids);
805 }
806
808 {
809 $harvester = new Harvester(
810 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
811 $object_handler = $this->getObjectHandler(),
812 $export_handler = $this->getExportHandler(45),
813 $status_repo = $this->getStatusRepository(),
814 $this->getExposedRecordRepository(),
815 $this->getSearchFactory(32, 45, 67),
816 new NullLOMRepository(),
817 $this->getXMLWriter(),
818 $this->getNullLogger()
819 );
820
821 $result = $harvester->run($this->getCronResultWrapper());
822
823 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
824 $this->assertSame(
825 'Deleted 0 deprecated references.<br>' .
826 'Created 3 new references.<br>' .
827 'Created, updated, or deleted 0 exposed records.',
828 $result->exposed_message
829 );
830 $this->assertSame(
831 [
832 ['obj_id' => 32, 'href_id' => 12332],
833 ['obj_id' => 45, 'href_id' => 12345],
834 ['obj_id' => 67, 'href_id' => 12367]
835 ],
836 $status_repo->exposed_creations
837 );
838 $this->assertSame(
839 [
840 ['obj_id' => 32, 'container_ref_id' => 123, 'new_ref_id' => 12332],
841 ['obj_id' => 45, 'container_ref_id' => 123, 'new_ref_id' => 12345],
842 ['obj_id' => 67, 'container_ref_id' => 123, 'new_ref_id' => 12367]
843 ],
844 $object_handler->exposed_ref_creations
845 );
846 $this->assertSame([32, 67], $export_handler->exposed_created_exports_obj_ids);
847 }
848
850 {
851 $harvester = new Harvester(
852 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
853 $this->getObjectHandler([], 456, [32, 45]),
854 $this->getExportHandler(),
855 $this->getStatusRepository([32 => 12332]),
856 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>', 45 => '<el>45</el>']),
857 $search_factory = $this->getSearchFactory(32),
858 new NullLOMRepository(),
859 $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45</el>']),
860 $this->getNullLogger()
861 );
862
863 $result = $harvester->run($this->getCronResultWrapper());
864
865 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
866 $this->assertSame(
867 'Deleted 0 deprecated references.<br>' .
868 'Created 0 new references.<br>' .
869 'Created, updated, or deleted 1 exposed records.',
870 $result->exposed_message
871 );
872 $this->assertSame(
873 [[
874 'restricted' => true,
875 'types' => ['type', 'second type'],
876 'entries' => [12, 5]
877 ]],
878 $search_factory->exposed_search_params
879 );
880 $this->assertEmpty($record_repo->exposed_creations);
881 $this->assertSame(
882 [[
883 'obj_id' => 45,
884 'deleted' => true,
885 'metadata' => null
886 ]],
887 $record_repo->exposed_updates
888 );
889 $this->assertCount(1, $record_repo->exposed_deletions);
890 }
891
893 {
894 $harvester = new Harvester(
895 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
896 $this->getObjectHandler([], 456, [32, 45]),
897 $this->getExportHandler(),
898 $this->getStatusRepository([32 => 12332]),
899 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>', 45 => ''], [45]),
900 $search_factory = $this->getSearchFactory(32),
901 new NullLOMRepository(),
902 $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45</el>']),
903 $this->getNullLogger()
904 );
905
906 $result = $harvester->run($this->getCronResultWrapper());
907
908 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_NO_ACTION, $result->exposed_status);
909 $this->assertSame(
910 'Deleted 0 deprecated references.<br>' .
911 'Created 0 new references.<br>' .
912 'Created, updated, or deleted 0 exposed records.',
913 $result->exposed_message
914 );
915 $this->assertSame(
916 [[
917 'restricted' => true,
918 'types' => ['type', 'second type'],
919 'entries' => [12, 5]
920 ]],
921 $search_factory->exposed_search_params
922 );
923 $this->assertEmpty($record_repo->exposed_creations);
924 $this->assertEmpty($record_repo->exposed_updates);
925 $this->assertCount(1, $record_repo->exposed_deletions);
926 }
927
929 {
930 $harvester = new Harvester(
931 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
932 $this->getObjectHandler([], 456, [32, 45]),
933 $this->getExportHandler(),
934 $this->getStatusRepository([32 => 12332], [45]),
935 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>', 45 => '<el>45</el>']),
936 $this->getSearchFactory(32, 45),
937 new NullLOMRepository(),
938 $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45</el>']),
939 $this->getNullLogger()
940 );
941
942 $result = $harvester->run($this->getCronResultWrapper());
943
944 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
945 $this->assertSame(
946 'Deleted 0 deprecated references.<br>' .
947 'Created 0 new references.<br>' .
948 'Created, updated, or deleted 1 exposed records.',
949 $result->exposed_message
950 );
951 $this->assertEmpty($record_repo->exposed_creations);
952 $this->assertSame(
953 [[
954 'obj_id' => 45,
955 'deleted' => true,
956 'metadata' => null
957 ]],
958 $record_repo->exposed_updates
959 );
960 $this->assertCount(1, $record_repo->exposed_deletions);
961 }
962
964 {
965 $harvester = new Harvester(
966 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
967 $this->getObjectHandler([45], 456, [32, 45]),
968 $this->getExportHandler(),
969 $this->getStatusRepository([32 => 12332]),
970 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>', 45 => '<el>45</el>']),
971 $this->getSearchFactory(32, 45),
972 new NullLOMRepository(),
973 $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45</el>']),
974 $this->getNullLogger()
975 );
976
977 $result = $harvester->run($this->getCronResultWrapper());
978
979 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
980 $this->assertSame(
981 'Deleted 0 deprecated references.<br>' .
982 'Created 0 new references.<br>' .
983 'Created, updated, or deleted 1 exposed records.',
984 $result->exposed_message
985 );
986 $this->assertEmpty($record_repo->exposed_creations);
987 $this->assertSame(
988 [[
989 'obj_id' => 45,
990 'deleted' => true,
991 'metadata' => null
992 ]],
993 $record_repo->exposed_updates
994 );
995 $this->assertCount(1, $record_repo->exposed_deletions);
996 }
997
999 {
1000 $harvester = new Harvester(
1001 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
1002 $this->getObjectHandler([], 456, [32]),
1003 $this->getExportHandler(),
1004 $this->getStatusRepository([32 => 12332, 45 => 12345]),
1005 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>', 45 => '<el>45</el>']),
1006 $this->getSearchFactory(32, 45),
1007 new NullLOMRepository(),
1008 $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45</el>']),
1009 $this->getNullLogger()
1010 );
1011
1012 $result = $harvester->run($this->getCronResultWrapper());
1013
1014 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
1015 $this->assertSame(
1016 'Deleted 0 deprecated references.<br>' .
1017 'Created 0 new references.<br>' .
1018 'Created, updated, or deleted 1 exposed records.',
1019 $result->exposed_message
1020 );
1021 $this->assertEmpty($record_repo->exposed_creations);
1022 $this->assertSame(
1023 [[
1024 'obj_id' => 45,
1025 'deleted' => true,
1026 'metadata' => null
1027 ]],
1028 $record_repo->exposed_updates
1029 );
1030 $this->assertCount(1, $record_repo->exposed_deletions);
1031 }
1032
1033 public function testRunUpdateExposedRecord(): void
1034 {
1035 $harvester = new Harvester(
1036 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
1037 $this->getObjectHandler([], 456, [32, 45]),
1038 $this->getExportHandler(),
1039 $this->getStatusRepository([32 => 12332, 45 => 12345]),
1040 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>', 45 => '<el>45</el>']),
1041 $this->getSearchFactory(32, 45),
1042 new NullLOMRepository(),
1043 $writer = $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45 changed</el>']),
1044 $this->getNullLogger()
1045 );
1046
1047 $result = $harvester->run($this->getCronResultWrapper());
1048
1049 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
1050 $this->assertSame(
1051 'Deleted 0 deprecated references.<br>' .
1052 'Created 0 new references.<br>' .
1053 'Created, updated, or deleted 1 exposed records.',
1054 $result->exposed_message
1055 );
1056 $this->assertEmpty($record_repo->exposed_creations);
1057 $this->assertCount(1, $record_repo->exposed_updates);
1058 $this->assertSame(45, $record_repo->exposed_updates[0]['obj_id']);
1059 $this->assertSame(false, $record_repo->exposed_updates[0]['deleted']);
1060 $this->assertXmlStringEqualsXmlString(
1061 '<el>45 changed</el>',
1062 $record_repo->exposed_updates[0]['metadata']
1063 );
1064 $this->assertEquals(
1065 [
1066 ['obj_id' => 32, 'ref_id' => 45632, 'type' => 'type_45632'],
1067 ['obj_id' => 45, 'ref_id' => 45645, 'type' => 'type_45645']
1068 ],
1069 $writer->exposed_params
1070 );
1071 $this->assertCount(1, $record_repo->exposed_deletions);
1072 }
1073
1075 {
1076 $harvester = new Harvester(
1077 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
1078 $this->getObjectHandler([], 456, [32, 45]),
1079 $this->getExportHandler(),
1080 $this->getStatusRepository([32 => 12332, 45 => 12345]),
1081 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>', 45 => ''], [45]),
1082 $this->getSearchFactory(32, 45),
1083 new NullLOMRepository(),
1084 $writer = $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45 changed</el>']),
1085 $this->getNullLogger()
1086 );
1087
1088 $result = $harvester->run($this->getCronResultWrapper());
1089
1090 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
1091 $this->assertSame(
1092 'Deleted 0 deprecated references.<br>' .
1093 'Created 0 new references.<br>' .
1094 'Created, updated, or deleted 1 exposed records.',
1095 $result->exposed_message
1096 );
1097 $this->assertEmpty($record_repo->exposed_creations);
1098 $this->assertCount(1, $record_repo->exposed_updates);
1099 $this->assertSame(45, $record_repo->exposed_updates[0]['obj_id']);
1100 $this->assertSame(false, $record_repo->exposed_updates[0]['deleted']);
1101 $this->assertXmlStringEqualsXmlString(
1102 '<el>45 changed</el>',
1103 $record_repo->exposed_updates[0]['metadata']
1104 );
1105 $this->assertEquals(
1106 [
1107 ['obj_id' => 32, 'ref_id' => 45632, 'type' => 'type_45632'],
1108 ['obj_id' => 45, 'ref_id' => 45645, 'type' => 'type_45645']
1109 ],
1110 $writer->exposed_params
1111 );
1112 $this->assertCount(1, $record_repo->exposed_deletions);
1113 }
1114
1115 public function testRunCreateNewExposedRecord(): void
1116 {
1117 $harvester = new Harvester(
1118 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
1119 $this->getObjectHandler([], 456, [32, 45]),
1120 $this->getExportHandler(),
1121 $this->getStatusRepository([32 => 12332, 45 => 12345]),
1122 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>']),
1123 $this->getSearchFactory(32, 45),
1124 new NullLOMRepository(),
1125 $writer = $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45 new</el>']),
1126 $this->getNullLogger()
1127 );
1128
1129 $result = $harvester->run($this->getCronResultWrapper());
1130
1131 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_OK, $result->exposed_status);
1132 $this->assertSame(
1133 'Deleted 0 deprecated references.<br>' .
1134 'Created 0 new references.<br>' .
1135 'Created, updated, or deleted 1 exposed records.',
1136 $result->exposed_message
1137 );
1138 $this->assertEmpty($record_repo->exposed_updates);
1139 $this->assertCount(1, $record_repo->exposed_creations);
1140 $this->assertSame(45, $record_repo->exposed_creations[0]['obj_id']);
1141 $this->assertSame('il__type_45645_45', $record_repo->exposed_creations[0]['identifier']);
1142 $this->assertXmlStringEqualsXmlString(
1143 '<el>45 new</el>',
1144 $record_repo->exposed_creations[0]['metadata']
1145 );
1146 $this->assertEquals(
1147 [
1148 ['obj_id' => 32, 'ref_id' => 45632, 'type' => 'type_45632'],
1149 ['obj_id' => 45, 'ref_id' => 45645, 'type' => 'type_45645']
1150 ],
1151 $writer->exposed_params
1152 );
1153 $this->assertCount(1, $record_repo->exposed_deletions);
1154 }
1155
1157 {
1158 $harvester = new Harvester(
1159 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
1160 $this->getObjectHandler([], 456, [32, 45]),
1161 $this->getExportHandler(),
1162 $this->getStatusRepository([32 => 12332], [45]),
1163 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>']),
1164 $this->getSearchFactory(32, 45),
1165 new NullLOMRepository(),
1166 $writer = $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45 new</el>']),
1167 $this->getNullLogger()
1168 );
1169
1170 $result = $harvester->run($this->getCronResultWrapper());
1171
1172 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_NO_ACTION, $result->exposed_status);
1173 $this->assertSame(
1174 'Deleted 0 deprecated references.<br>' .
1175 'Created 0 new references.<br>' .
1176 'Created, updated, or deleted 0 exposed records.',
1177 $result->exposed_message
1178 );
1179 $this->assertEmpty($record_repo->exposed_updates);
1180 $this->assertEmpty($record_repo->exposed_creations);
1181 $this->assertCount(1, $record_repo->exposed_deletions);
1182 }
1183
1185 {
1186 $harvester = new Harvester(
1187 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
1188 $this->getObjectHandler([45], 456, [32, 45]),
1189 $this->getExportHandler(),
1190 $this->getStatusRepository([32 => 12332]),
1191 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>']),
1192 $this->getSearchFactory(32, 45),
1193 new NullLOMRepository(),
1194 $writer = $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45 new</el>']),
1195 $this->getNullLogger()
1196 );
1197
1198 $result = $harvester->run($this->getCronResultWrapper());
1199
1200 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_NO_ACTION, $result->exposed_status);
1201 $this->assertSame(
1202 'Deleted 0 deprecated references.<br>' .
1203 'Created 0 new references.<br>' .
1204 'Created, updated, or deleted 0 exposed records.',
1205 $result->exposed_message
1206 );
1207 $this->assertEmpty($record_repo->exposed_updates);
1208 $this->assertEmpty($record_repo->exposed_creations);
1209 $this->assertCount(1, $record_repo->exposed_deletions);
1210 }
1211
1213 {
1214 $harvester = new Harvester(
1215 $this->getSettings(['type', 'second type'], [12, 5], 123, 456),
1216 $this->getObjectHandler([], 456, [32]),
1217 $this->getExportHandler(),
1218 $this->getStatusRepository([32 => 12332, 45 => 12345]),
1219 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>']),
1220 $this->getSearchFactory(32, 45),
1221 new NullLOMRepository(),
1222 $writer = $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45 new</el>']),
1223 $this->getNullLogger()
1224 );
1225
1226 $result = $harvester->run($this->getCronResultWrapper());
1227
1228 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_NO_ACTION, $result->exposed_status);
1229 $this->assertSame(
1230 'Deleted 0 deprecated references.<br>' .
1231 'Created 0 new references.<br>' .
1232 'Created, updated, or deleted 0 exposed records.',
1233 $result->exposed_message
1234 );
1235 $this->assertEmpty($record_repo->exposed_updates);
1236 $this->assertEmpty($record_repo->exposed_creations);
1237 $this->assertCount(1, $record_repo->exposed_deletions);
1238 }
1239
1241 {
1242 $harvester = new Harvester(
1243 $this->getSettings(['type', 'second type'], [12, 5], 123, 0),
1244 $this->getObjectHandler([], 456, [32, 45]),
1245 $this->getExportHandler(),
1246 $this->getStatusRepository([32 => 12332, 45 => 12345]),
1247 $record_repo = $this->getExposedRecordRepository([32 => '<el>32</el>']),
1248 $this->getSearchFactory(32, 45),
1249 new NullLOMRepository(),
1250 $writer = $this->getXMLWriter([32 => '<el>32</el>', 45 => '<el>45 new</el>']),
1251 $this->getNullLogger()
1252 );
1253
1254 $result = $harvester->run($this->getCronResultWrapper());
1255
1256 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_NO_ACTION, $result->exposed_status);
1257 $this->assertSame(
1258 'Deleted 0 deprecated references.<br>' .
1259 'Created 0 new references.<br>' .
1260 'Created, updated, or deleted 0 exposed records.',
1261 $result->exposed_message
1262 );
1263 $this->assertEmpty($record_repo->exposed_updates);
1264 $this->assertEmpty($record_repo->exposed_creations);
1265 $this->assertCount(1, $record_repo->exposed_deletions);
1266 }
1267
1268 public function testRunWithUnforeseenError(): void
1269 {
1270 $harvester = new Harvester(
1271 $this->getSettings(['type', 'second type'], [12, 5]),
1272 $object_handler = $this->getObjectHandler(),
1273 $this->getExportHandler(),
1274 $status_repo = $this->getStatusRepository([], [], true),
1275 $this->getExposedRecordRepository(),
1276 $search_factory = $this->getSearchFactory(),
1277 new NullLOMRepository(),
1278 $this->getXMLWriter(),
1279 $this->getNullLogger()
1280 );
1281
1282 $result = $harvester->run($this->getCronResultWrapper());
1283
1284 $this->assertSame(\ILIAS\Cron\Job\JobResult::STATUS_FAIL, $result->exposed_status);
1285 $this->assertSame(
1286 'error',
1287 $result->exposed_message
1288 );
1289 }
1290}
factory()
getSettings(array $types=[], array $copyright_ids=[], int $harvesting_target_ref_id=0, int $exposed_source_ref_id=0)
getExposedRecordRepository(array $returned_records=[], array $deleted_records=[])
Records are passed as array via obj_id => metadata-xml as string.
getSearchFactory(int ... $search_result_obj_ids)
getStatusRepository(array $currently_harvested=[], array $blocked_obj_ids=[], bool $throw_error=false)
Currently harvested objects are passed as obj_id => href_id.
getObjectHandler(array $deleted_obj_ids=[], int $valid_source_container=0, array $obj_ids_referenced_in_container=[], ?int $throw_error_on_deletion_ref_id=null, ?int $throw_error_on_ref_creation_obj_id=null)
Returned ref_ids are always given by concatenation of target ref_id and obj_id.
getExportHandler(?int $throw_exception_for_id=null, int ... $already_have_export_obj_ids)
getXMLWriter(array $returned_md=[])
Metadata is passed as array via obj_id => metadata-xml as string.
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
Component logger with individual log levels by component id.
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
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.