ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
ServerUrlListTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use PHPUnit\Framework\TestCase;
25
26class ServerUrlListTest extends TestCase
27{
28 public function testFromStringEmptyYieldsEmptyList(): void
29 {
30 $list = ServerUrlList::fromString('');
31 $this->assertSame(0, $list->count());
32 $this->assertSame('', $list->toString());
33 $this->assertSame([], $list->getInvalidParts());
34 }
35
37 {
38 $list = ServerUrlList::fromString(' , , ');
39 $this->assertSame(0, $list->count());
40 $this->assertSame('', $list->toString());
41 $this->assertSame([], $list->getInvalidParts());
42 }
43
44 public function testFromStringSingleValidUri(): void
45 {
46 $list = ServerUrlList::fromString('ldap://host.example.com:389');
47 $this->assertSame(1, $list->count());
48 $this->assertSame('ldap://host.example.com:389', $list->getConnectionStringAtIndex(0));
49 $this->assertSame('', $list->getConnectionStringAtIndex(1));
50 $this->assertSame([], $list->getInvalidParts());
51 $this->assertSame('ldap://host.example.com:389', $list->toString());
52 }
53
55 {
56 $input = 'host.example.com:389';
57 $list = ServerUrlList::fromString($input);
58 $this->assertSame(1, $list->count());
59 $this->assertSame($input, $list->getConnectionStringAtIndex(0));
60 $this->assertSame([$input], $list->getInvalidParts());
61 $this->assertSame($input, $list->toString());
62 }
63
64 public function testFromStringLdapsPreserved(): void
65 {
66 $list = ServerUrlList::fromString('ldaps://secure.example.com:636');
67 $this->assertSame(1, $list->count());
68 $this->assertSame('ldaps://secure.example.com:636', $list->getConnectionStringAtIndex(0));
69 }
70
71 public function testFromStringMultipleValidUris(): void
72 {
73 $stored = 'ldap://a.example:389,ldaps://b.example:636';
74 $list = ServerUrlList::fromString($stored);
75 $this->assertSame(2, $list->count());
76 $this->assertSame('ldap://a.example:389', $list->getConnectionStringAtIndex(0));
77 $this->assertSame('ldaps://b.example:636', $list->getConnectionStringAtIndex(1));
78 $this->assertSame([], $list->getInvalidParts());
79 $this->assertSame($stored, $list->toString());
80 }
81
83 {
84 $invalid = 'ldap://';
85 $list = ServerUrlList::fromString($invalid);
86 $this->assertSame(1, $list->count());
87 $this->assertSame($invalid, $list->getConnectionStringAtIndex(0));
88 $this->assertSame([$invalid], $list->getInvalidParts());
89 $this->assertSame($invalid, $list->toString());
90 }
91
92 public function testFromStringMixedValidAndInvalid(): void
93 {
94 $invalid = 'ldap://';
95 $stored = 'ldap://ok:389,' . $invalid . ',ldaps://also:636';
96 $list = ServerUrlList::fromString($stored);
97 $this->assertSame(3, $list->count());
98 $this->assertSame('ldap://ok:389', $list->getConnectionStringAtIndex(0));
99 $this->assertSame($invalid, $list->getConnectionStringAtIndex(1));
100 $this->assertSame('ldaps://also:636', $list->getConnectionStringAtIndex(2));
101 $this->assertSame([$invalid], $list->getInvalidParts());
102 $this->assertSame($stored, $list->toString(), 'toString() must preserve order and use comma delimiter');
103 }
104
106 {
107 $list = ServerUrlList::fromString('ldap://host:389');
108 $this->assertSame('', $list->getConnectionStringAtIndex(-1));
109 }
110
112 {
113 $list = ServerUrlList::fromString('ldap://host:389');
114 $this->assertSame('', $list->getConnectionStringAtIndex(1));
115 $this->assertSame('', $list->getConnectionStringAtIndex(99));
116 }
117
119 {
120 $list = ServerUrlList::fromString('ldap://first:389,ldap://second:389');
121 $rotated = $list->rotate();
122 $this->assertNotSame($list, $rotated, 'rotate() must return new instance when count >= 2');
123 $this->assertSame(2, $rotated->count());
124 $this->assertSame('ldap://second:389', $rotated->getConnectionStringAtIndex(0));
125 $this->assertSame('ldap://first:389', $rotated->getConnectionStringAtIndex(1));
126 $this->assertSame('ldap://second:389,ldap://first:389', $rotated->toString());
127 }
128
130 {
131 $list = ServerUrlList::fromString('ldap://only:389');
132 $rotated = $list->rotate();
133 $this->assertSame($list, $rotated, 'rotate() must return this when count < 2');
134 $this->assertSame(1, $rotated->count());
135 $this->assertSame('ldap://only:389', $rotated->getConnectionStringAtIndex(0));
136 }
137
139 {
140 $list = ServerUrlList::fromString('');
141 $rotated = $list->rotate();
142 $this->assertSame($list, $rotated, 'rotate() must return this when count < 2');
143 $this->assertSame(0, $rotated->count());
144 $this->assertSame('', $rotated->toString());
145 }
146
148 {
149 $list = ServerUrlList::fromString('ldap://a:389,ldap://b:389');
150 $reordered = $list->withPrimaryAt(0);
151 $this->assertSame('ldap://a:389', $reordered->getConnectionStringAtIndex(0));
152 $this->assertSame('ldap://b:389', $reordered->getConnectionStringAtIndex(1));
153 $this->assertSame('ldap://a:389,ldap://b:389', $reordered->toString());
154 }
155
157 {
158 $list = ServerUrlList::fromString('ldap://a:389,ldap://b:389');
159 $reordered = $list->withPrimaryAt(1);
160 $this->assertNotSame($list, $reordered, 'withPrimaryAt() must return new instance when index valid');
161 $this->assertSame(2, $reordered->count());
162 $this->assertSame('ldap://b:389', $reordered->getConnectionStringAtIndex(0));
163 $this->assertSame('ldap://a:389', $reordered->getConnectionStringAtIndex(1));
164 $this->assertSame('ldap://b:389,ldap://a:389', $reordered->toString());
165 }
166
168 {
169 $list = ServerUrlList::fromString('ldap://a:389');
170 $reordered = $list->withPrimaryAt(5);
171 $this->assertSame($list, $reordered, 'withPrimaryAt() must return this when index out of range');
172 $this->assertSame('ldap://a:389', $reordered->getConnectionStringAtIndex(0));
173 }
174
176 {
177 $list = ServerUrlList::fromString('ldap://a:389,ldap://b:389');
178 $reordered = $list->withPrimaryAt(-1);
179 $this->assertSame($list, $reordered, 'withPrimaryAt() must return this when index negative');
180 $this->assertSame('ldap://a:389', $reordered->getConnectionStringAtIndex(0));
181 $this->assertSame('ldap://b:389', $reordered->getConnectionStringAtIndex(1));
182 }
183
185 {
186 $list = ServerUrlList::fromString('ldap://a:389,ldap://b:389');
187 $collected = [];
188 foreach ($list->validUrls() as $index => $uri) {
189 $collected[$index] = $uri;
190 }
191 $this->assertCount(2, $collected);
192 $this->assertInstanceOf(\ILIAS\Data\URI::class, $collected[0]);
193 $this->assertInstanceOf(\ILIAS\Data\URI::class, $collected[1]);
194 $this->assertSame('ldap://a:389', (string) $collected[0]);
195 $this->assertSame('ldap://b:389', (string) $collected[1]);
196 }
197
198 public function testValidUrlsSkipsInvalidParts(): void
199 {
200 $list = ServerUrlList::fromString('ldap://ok:389,ldap://');
201 $collected = [];
202 foreach ($list->validUrls() as $index => $uri) {
203 $collected[$index] = $uri;
204 }
205 $this->assertCount(1, $collected);
206 $this->assertArrayHasKey(0, $collected);
207 $this->assertInstanceOf(\ILIAS\Data\URI::class, $collected[0]);
208 $this->assertSame('ldap://ok:389', (string) $collected[0]);
209 }
210
212 {
213 $list = ServerUrlList::fromString('ldap://,ldap://');
214 $collected = [];
215 foreach ($list->validUrls() as $index => $uri) {
216 $collected[$index] = $uri;
217 }
218 $this->assertCount(0, $collected);
219 }
220
222 {
223 $invalid = 'ldap://';
224 $list = ServerUrlList::fromString('ldap://first:389,' . $invalid . ',ldap://third:389');
225 $this->assertSame(3, $list->count());
226
227 $indices = [];
228 foreach ($list->validUrls() as $index => $uri) {
229 $indices[] = $index;
230 }
231 $this->assertSame([0, 2], $indices);
232
233 $reordered = $list->withPrimaryAt(2);
234 $this->assertSame(3, $reordered->count());
235 $this->assertSame('ldap://third:389', $reordered->getConnectionStringAtIndex(0));
236 $this->assertSame('ldap://first:389', $reordered->getConnectionStringAtIndex(1));
237 $this->assertSame($invalid, $reordered->getConnectionStringAtIndex(2));
238 $this->assertSame('ldap://third:389,ldap://first:389,' . $invalid, $reordered->toString());
239
240 $reorderedInvalid = $list->withPrimaryAt(1);
241 $this->assertSame($invalid, $reorderedInvalid->getConnectionStringAtIndex(0));
242 $this->assertSame('ldap://first:389', $reorderedInvalid->getConnectionStringAtIndex(1));
243 $this->assertSame('ldap://third:389', $reorderedInvalid->getConnectionStringAtIndex(2));
244 }
245
247 {
248 $list = ServerUrlList::fromString('ldap://host:389');
249 $this->assertSame($list->toString(), (string) $list);
250 }
251
252 public function testConstructorWithEmptyArray(): void
253 {
254 $list = new ServerUrlList([]);
255 $this->assertSame(0, $list->count());
256 $this->assertSame('', $list->toString());
257 $this->assertSame([], $list->getInvalidParts());
258 }
259
261 {
262 $list = new ServerUrlList([
263 new \ILIAS\Data\URI('ldap://first:389'),
264 new \ILIAS\Data\URI('ldap://second:389')
265 ]);
266 $this->assertSame(2, $list->count());
267 $this->assertSame('ldap://first:389', $list->getConnectionStringAtIndex(0));
268 $this->assertSame('ldap://second:389', $list->getConnectionStringAtIndex(1));
269 }
270
275 {
276 $list = new ServerUrlList([
277 1 => new \ILIAS\Data\URI('ldap://first:389'),
278 2 => new \ILIAS\Data\URI('ldap://second:389')
279 ]);
280 $this->assertSame(2, $list->count());
281 $this->assertSame('ldap://first:389', $list->getConnectionStringAtIndex(0));
282 $this->assertSame('ldap://second:389', $list->getConnectionStringAtIndex(1));
283 $this->assertSame('', $list->getConnectionStringAtIndex(2));
284 }
285
290 {
291 $list = ServerUrlList::fromString('ldap://only:389 , , ');
292 $this->assertSame(1, $list->count());
293 $this->assertSame('ldap://only:389', $list->getConnectionStringAtIndex(0));
294 $this->assertSame('ldap://only:389', $list->toString());
295 }
296
301 {
302 $raw = 'not-a-valid-uri';
303 $list = ServerUrlList::fromString($raw);
304 $this->assertSame(1, $list->count());
305 $this->assertSame($raw, $list->getConnectionStringAtIndex(0));
306 $this->assertSame([$raw], $list->getInvalidParts());
307 }
308}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Value object representing a list of LDAP server URLs (primary plus fallbacks).
static fromString(string $stored)
Create from string representation (comma-separated, as stored in DB or form).
testFromStringSkipsEmptyPartsAfterTrim()
fromString must skip empty parts after trim (mutant that removes "continue" would add empty entries).
testConstructorWithNonSequentialKeysRebasesToZeroBasedIndices()
Constructor must reindex with array_values() so indices are 0-based (mutant that removes array_values...
testGetConnectionStringAtIndexReturnsRawStringForInvalidEntry()
getConnectionStringAtIndex must return string for raw (non-URI) entry.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.