ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
IdentifierHandlerTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
27use ILIAS\MetaData\Paths\NullFactory as NullPathFactory;
32use ILIAS\MetaData\Paths\NullBuilder as NullPathBuilder;
35
36class IdentifierHandlerTest extends TestCase
37{
38 protected function getSet(): SetInterface
39 {
40 return new class () extends NullSet {
41 public array $prepared_changes = [];
42 };
43 }
44
45 protected function getRessourceID(int $obj_id, int $sub_id, string $type): RessourceIDInterface
46 {
47 return new class ($obj_id, $sub_id, $type) extends NullRessourceID {
48 public function __construct(
49 protected int $obj_id,
50 protected int $sub_id,
51 protected string $type
52 ) {
53 }
54
55 public function objID(): int
56 {
57 return $this->obj_id;
58 }
59
60 public function subID(): int
61 {
62 return $this->sub_id;
63 }
64
65 public function type(): string
66 {
67 return $this->type;
68 }
69 };
70 }
71
73 {
74 $manipulator = new class () extends NullManipulator {
75 public function prepareCreateOrUpdate(
76 SetInterface $set,
78 string ...$values
79 ): SetInterface {
80 $set = clone $set;
81 $set->prepared_changes[] = [
82 'path' => $path->toString(),
83 'values' => $values
84 ];
85 return $set;
86 }
87
88 public function prepareDelete(SetInterface $set, PathInterface $path): SetInterface
89 {
90 $set = clone $set;
91 $set->prepared_changes[] = ['delete should not be prepared!'];
92 return $set;
93 }
94
95 public function prepareForceCreate(
96 SetInterface $set,
98 string ...$values
99 ): SetInterface {
100 $set = clone $set;
101 $set->prepared_changes[] = ['force create should not be prepared!'];
102 return $set;
103 }
104 };
105
106 $builder = new class () extends NullPathBuilder {
107 protected string $path_string = '~start~';
108
109 public function withNextStep(string $name, bool $add_as_first = false): PathBuilder
110 {
111 $builder = clone $this;
112 if ($add_as_first) {
113 $name .= '[added as first]';
114 }
115 $builder->path_string .= '%' . $name;
116 return $builder;
117 }
118
119 public function withAdditionalFilterAtCurrentStep(FilterType $type, string ...$values): PathBuilder
120 {
121 $builder = clone $this;
122 $builder->path_string .= '{' . $type->value . ':' . implode('><', $values) . '}';
123 return $builder;
124 }
125
126 public function get(): PathInterface
127 {
128 return new class ($this->path_string) extends NullPath {
129 public function __construct(protected string $path_string)
130 {
131 }
132
133 public function toString(): string
134 {
135 return $this->path_string;
136 }
137 };
138 }
139 };
140
141 $path_factory = new class ($builder) extends NullPathFactory {
142 public function __construct(protected PathBuilder $builder)
143 {
144 }
145
146 public function custom(): PathBuilder
147 {
148 return $this->builder;
149 }
150 };
151
152 return new class ($manipulator, $path_factory) extends IdentifierHandler {
153 protected function getInstallID(): string
154 {
155 return 'MockInstID';
156 }
157 };
158 }
159
160 public function testPrepareUpdateOfIdentifier(): void
161 {
162 $set = $this->getSet();
163 $ressource_id = $this->getRessourceID(78, 983, 'TargetType');
164 $identifier_handler = $this->getIdentifierHandler();
165
166 $prepared_set = $identifier_handler->prepareUpdateOfIdentifier($set, $ressource_id);
167
168 $expected_entry_changes = [
169 'path' => '~start~%general%identifier{index:0}%entry',
170 'values' => ['il_MockInstID_TargetType_983']
171 ];
172 $expected_catalog_changes = [
173 'path' => '~start~%general%identifier{index:0}%catalog',
174 'values' => ['ILIAS']
175 ];
176 $prepared_changes = $prepared_set->prepared_changes;
177 $this->assertCount(2, $prepared_changes);
178 $this->assertContains($expected_entry_changes, $prepared_changes);
179 $this->assertContains($expected_catalog_changes, $prepared_changes);
180 }
181
183 {
184 $set = $this->getSet();
185 $ressource_id = $this->getRessourceID(78, 0, 'TargetType');
186 $identifier_handler = $this->getIdentifierHandler();
187
188 $prepared_set = $identifier_handler->prepareUpdateOfIdentifier($set, $ressource_id);
189
190 $expected_entry_changes = [
191 'path' => '~start~%general%identifier{index:0}%entry',
192 'values' => ['il_MockInstID_TargetType_78']
193 ];
194 $expected_catalog_changes = [
195 'path' => '~start~%general%identifier{index:0}%catalog',
196 'values' => ['ILIAS']
197 ];
198 $prepared_changes = $prepared_set->prepared_changes;
199 $this->assertCount(2, $prepared_changes);
200 $this->assertContains($expected_entry_changes, $prepared_changes);
201 $this->assertContains($expected_catalog_changes, $prepared_changes);
202 }
203}
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
$path
Definition: ltiservices.php:30
FilterType
Values should always be all lowercase.
Definition: FilterType.php:27