ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
URLBuilderTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\UI;
22 
24 
25 class URLBuilderTest extends TestCase
26 {
27  private readonly \ILIAS\Data\URI $URI_COMPLETE;
28 
29  protected function setUp(): void
30  {
31  $this->URI_COMPLETE = new \ILIAS\Data\URI('https://www.ilias.de/foo/bar?var1=42&var2=foo#12345');
32  }
33 
34  public function testGetUrl(): void
35  {
36  $url = new URLBuilder($this->URI_COMPLETE);
37  $this->assertInstanceOf(\ILIAS\Data\URI::class, $url->buildURI());
38  $this->assertEquals('https://www.ilias.de/foo/bar?var1=42&var2=foo#12345', (string) $url->buildURI());
39  }
40 
41  public function testAcquireParam(): void
42  {
43  $url = new URLBuilder($this->URI_COMPLETE);
44  $result = $url->acquireParameter(['test'], 'title');
45  $this->assertInstanceOf(URLBuilder::class, $result[0]);
46  $this->assertEquals(
47  'https://www.ilias.de/foo/bar?var1=42&var2=foo&test' . URLBuilder::SEPARATOR . 'title=#12345',
48  (string) $result[0]->buildURI()
49  );
50  $this->assertInstanceOf(URLBuilderToken::class, $result[1]);
51  $this->assertEquals('test' . URLBuilder::SEPARATOR . 'title', $result[1]->getName());
52  $this->assertNotEmpty($result[1]->getToken());
53  }
54 
55  public function testAcquireParamWithLongNamespace(): void
56  {
57  $url = new URLBuilder($this->URI_COMPLETE);
58  $result = $url->acquireParameter(['test', 'object', 'metadata'], 'title');
59  $this->assertInstanceOf(URLBuilder::class, $result[0]);
60  $this->assertEquals(
61  'https://www.ilias.de/foo/bar?var1=42&var2=foo&test' . URLBuilder::SEPARATOR .
62  'object' . URLBuilder::SEPARATOR .
63  'metadata' . URLBuilder::SEPARATOR .
64  'title=#12345',
65  (string) $result[0]->buildURI()
66  );
67  $this->assertInstanceOf(URLBuilderToken::class, $result[1]);
68  $this->assertEquals(
69  'test' . URLBuilder::SEPARATOR .
70  'object' . URLBuilder::SEPARATOR .
71  'metadata' . URLBuilder::SEPARATOR .
72  'title',
73  $result[1]->getName()
74  );
75  $this->assertNotEmpty($result[1]->getToken());
76  }
77 
78  public function testAcquireParamWithValue(): void
79  {
80  $url = new URLBuilder($this->URI_COMPLETE);
81  $sep = URLBuilder::SEPARATOR;
82  $result = $url->acquireParameter(['test'], 'title', 'bar');
83  $this->assertInstanceOf(URLBuilder::class, $result[0]);
84  $this->assertEquals(
85  'https://www.ilias.de/foo/bar?var1=42&var2=foo&test' . $sep . 'title=bar#12345',
86  (string) $result[0]->buildURI()
87  );
88  $this->assertInstanceOf(URLBuilderToken::class, $result[1]);
89  $this->assertEquals('test' . $sep . 'title', $result[1]->getName());
90  $this->assertNotEmpty($result[1]->getToken());
91  }
92 
93  public function testAcquireParamWithSameName(): void
94  {
95  $url = new URLBuilder($this->URI_COMPLETE);
96  $sep = URLBuilder::SEPARATOR;
97  $result = $url->acquireParameter(['test'], 'title', 'foo');
98  $this->assertEquals(
99  'https://www.ilias.de/foo/bar?var1=42&var2=foo&test' . $sep . 'title=foo#12345',
100  (string) $result[0]->buildURI()
101  );
102  $this->assertEquals('test' . $sep . 'title', $result[1]->getName());
103 
104  $result2 = $result[0]->acquireParameter(['notatest'], 'title', 'bar');
105  $this->assertEquals(
106  'https://www.ilias.de/foo/bar?var1=42&var2=foo&test' . $sep . 'title=foo&notatest' . $sep . 'title=bar#12345',
107  (string) $result2[0]->buildURI()
108  );
109  $this->assertEquals('notatest' . $sep . 'title', $result2[1]->getName());
110  $this->assertNotEquals($result[1]->getToken(), $result2[1]->getToken());
111  }
112 
113  public function testWriteParam(): void
114  {
115  $url = new URLBuilder($this->URI_COMPLETE);
116  $sep = URLBuilder::SEPARATOR;
117  $result = $url->acquireParameter(['test'], 'title', 'bar');
118  $this->assertEquals(
119  'https://www.ilias.de/foo/bar?var1=42&var2=foo&test' . $sep . 'title=bar#12345',
120  (string) $result[0]->buildURI()
121  );
122 
123  $url = $result[0]->withParameter($result[1], 'foobar');
124  $this->assertInstanceOf(URLBuilder::class, $url);
125  $this->assertEquals(
126  'https://www.ilias.de/foo/bar?var1=42&var2=foo&test' . $sep . 'title=foobar#12345',
127  (string) $url->buildURI()
128  );
129 
130  $url = $result[0]->withParameter($result[1], ['foo', 'bar']);
131  $this->assertInstanceOf(URLBuilder::class, $url);
132  $this->assertEquals(
133  'https://www.ilias.de/foo/bar?var1=42&var2=foo'
134  . '&test' . $sep . urlencode('title[]') . '=foo'
135  . '&test' . $sep . urlencode('title[]') . '=bar'
136  . '#12345',
137  (string) $url->buildURI()
138  );
139  }
140 
141  public function testDeleteParam(): void
142  {
143  $url = new URLBuilder($this->URI_COMPLETE);
144  $sep = URLBuilder::SEPARATOR;
145  $result = $url->acquireParameter(['test'], 'title', 'bar');
146  $this->assertEquals(
147  'https://www.ilias.de/foo/bar?var1=42&var2=foo&test' . $sep . 'title=bar#12345',
148  (string) $result[0]->buildURI()
149  );
150 
151  $url = $result[0]->deleteParameter($result[1]);
152  $this->assertEquals('https://www.ilias.de/foo/bar?var1=42&var2=foo#12345', (string) $url->buildURI());
153  }
154 
155  public function testUrlTooLong(): void
156  {
157  $url = new URLBuilder($this->URI_COMPLETE);
158  $result = $url->acquireParameter(['test'], 'title', random_bytes(URLBuilder::URL_MAX_LENGTH));
159  $this->expectException(\LengthException::class);
160  $output = $result[0]->buildURI();
161  }
162 
163  public function testRemoveAndAddFragment(): void
164  {
165  $url = new URLBuilder($this->URI_COMPLETE);
166  $url = $url->withFragment(''); // set fragment to empty
167  $this->assertInstanceOf(URLBuilder::class, $url);
168  $this->assertEquals('https://www.ilias.de/foo/bar?var1=42&var2=foo', (string) $url->buildURI());
169  $url = $url->withFragment(null); // unset fragment, use fragment from base URL if present
170  $this->assertInstanceOf(URLBuilder::class, $url);
171  $this->assertEquals('https://www.ilias.de/foo/bar?var1=42&var2=foo#12345', (string) $url->buildURI());
172  $url = $url->withFragment('54321'); // set fragment to value
173  $this->assertInstanceOf(URLBuilder::class, $url);
174  $this->assertEquals('https://www.ilias.de/foo/bar?var1=42&var2=foo#54321', (string) $url->buildURI());
175  }
176 
177  public function testWithUri(): void
178  {
179  $url = new URLBuilder($this->URI_COMPLETE);
180  $result = $url->acquireParameter(['test'], 'title', 'bar');
181  $url = $result[0]->withURI(
182  new \ILIAS\Data\URI('http://test.ilias.de/bar/foo?test' . URLBuilder::SEPARATOR . 'title=foo&var1=46#12345')
183  );
184  $this->assertEquals(
185  'http://test.ilias.de/bar/foo?test' . URLBuilder::SEPARATOR . 'title=bar&var1=46#12345',
186  (string) $url->buildURI()
187  );
188  }
189 
190  public function testRenderTokens(): void
191  {
192  $url = new URLBuilder($this->URI_COMPLETE);
193 
194  // One parameter
195  $result1 = $url->acquireParameter(['test', 'object'], 'title', 'bar');
196  $url = $result1[0];
197  $expected_token = 'new Map([["' . $result1[1]->getName() . '",'
198  . 'new il.UI.core.URLBuilderToken(["test","object"], "title", "' . $result1[1]->getToken() . '")]])';
199  $this->assertEquals($expected_token, $url->renderTokens([$result1[1]]));
200 
201  // Two parameters, but just rendered with one
202  $result2 = $url->acquireParameter(['test'], 'description', 'foo');
203  $url = $result2[0];
204  $this->assertEquals($expected_token, $url->renderTokens([$result1[1]]));
205 
206  // Two parameters with full render
207  $expected_token = 'new Map([["' . $result1[1]->getName() . '",'
208  . 'new il.UI.core.URLBuilderToken(["test","object"], "title", "' . $result1[1]->getToken() . '")],'
209  . '["' . $result2[1]->getName() . '",'
210  . 'new il.UI.core.URLBuilderToken(["test"], "description", "' . $result2[1]->getToken() . '")]])';
211  $this->assertEquals($expected_token, $url->renderTokens([$result1[1], $result2[1]]));
212  }
213 
214  public function testRenderObject(): void
215  {
216  $url = new URLBuilder($this->URI_COMPLETE);
217 
218  // One parameter
219  $result1 = $url->acquireParameter(['test', 'object'], 'title', 'bar');
220  $url = $result1[0];
221  $expected_token = 'new Map([["' . $result1[1]->getName() . '",'
222  . 'new il.UI.core.URLBuilderToken(["test","object"], "title", "' . $result1[1]->getToken() . '")]])';
223  $expected_object = 'new il.UI.core.URLBuilder(new URL("https://www.ilias.de/foo/bar?var1=42&var2=foo&'
224  . 'test' . URLBuilder::SEPARATOR . 'object' . URLBuilder::SEPARATOR . 'title=bar#12345"), ' . $expected_token . ')';
225  $this->assertEquals($expected_object, $url->renderObject([$result1[1]]));
226 
227  // Two parameters, but just rendered with one
228  $result2 = $url->acquireParameter(['test'], 'description', 'foo');
229  $url = $result2[0];
230  $expected_object = 'new il.UI.core.URLBuilder(new URL("https://www.ilias.de/foo/bar?var1=42&var2=foo&'
231  . 'test' . URLBuilder::SEPARATOR . 'object' . URLBuilder::SEPARATOR . 'title=bar&'
232  . 'test' . URLBuilder::SEPARATOR . 'description=foo#12345"), ' . $expected_token . ')';
233  $this->assertEquals($expected_object, $url->renderObject([$result1[1]]));
234 
235  // Two parameters with full render
236  $expected_token = 'new Map([["' . $result1[1]->getName() . '",'
237  . 'new il.UI.core.URLBuilderToken(["test","object"], "title", "' . $result1[1]->getToken() . '")],'
238  . '["' . $result2[1]->getName() . '",'
239  . 'new il.UI.core.URLBuilderToken(["test"], "description", "' . $result2[1]->getToken() . '")]])';
240  $expected_object = 'new il.UI.core.URLBuilder(new URL("https://www.ilias.de/foo/bar?var1=42&var2=foo&'
241  . 'test' . URLBuilder::SEPARATOR . 'object' . URLBuilder::SEPARATOR . 'title=bar&'
242  . 'test' . URLBuilder::SEPARATOR . 'description=foo#12345"), ' . $expected_token . ')';
243  $this->assertEquals($expected_object, $url->renderObject([$result1[1], $result2[1]]));
244  }
245 }
const URL_MAX_LENGTH
A maximum length of 2048 characters should be safe to use in most browsers, even though longer URLs w...
Definition: URLBuilder.php:46
Interface Observer Contains several chained tasks and infos about them.
const SEPARATOR
Separator for parts of a parameter&#39;s namespace.
Definition: URLBuilder.php:50
$url
Definition: shib_logout.php:66
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
readonly ILIAS Data URI $URI_COMPLETE
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
URLBuilder.
Definition: URLBuilder.php:40