ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
HttpPathBuilderTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
22
23final class HttpPathBuilderTest extends TestCase
24{
33 public static function environmentProvider(): Generator
34 {
35 yield 'Host matches configured HTTP path (no `allowed_hosts` configuration)' => [
36 'server_data' => [
37 'HTTP_HOST' => 'localhost',
38 'REQUEST_URI' => '/login.php',
39 ],
40 'http_path' => 'http://localhost',
41 'wsdl_path' => 'https://localhost/soap/server.php?wsdl=1',
42 'allowed_hosts' => '',
43 ];
44
45 yield 'Host matches configured WSDL path (no `allowed_hosts` configuration)' => [
46 'server_data' => [
47 'HTTP_HOST' => 'soap.ilias.de',
48 'REQUEST_URI' => '/login.php',
49 ],
50 'http_path' => 'https://test.ilias.de',
51 'wsdl_path' => 'https://soap.ilias.de/soap/server.php?wsdl=1',
52 'allowed_hosts' => '',
53 ];
54
55 yield 'Localhost is always allowed (no `allowed_hosts` configuration)' => [
56 'server_data' => [
57 'HTTP_HOST' => 'localhost',
58 'REQUEST_URI' => '/login.php',
59 ],
60 'http_path' => 'https://test.ilias.de',
61 'wsdl_path' => 'https://test.ilias.de/soap/server.php?wsdl=1',
62 'allowed_hosts' => '',
63 ];
64
65 yield 'Host is in `allowed_hosts` list' => [
66 'server_data' => [
67 'HTTP_HOST' => 'test2.ilias.de',
68 'REQUEST_URI' => '/login.php',
69 ],
70 'http_path' => 'https://test.ilias.de',
71 'wsdl_path' => 'https://test.ilias.de/soap/server.php?wsdl=1',
72 'allowed_hosts' => 'test2.ilias.de',
73 ];
74 }
75
79 #[\PHPUnit\Framework\Attributes\DataProvider('environmentProvider')]
81 array|ArrayAccess $server_data,
82 string $http_path,
83 string $wsdl_path,
84 string $allowed_hosts
85 ): void {
86 $path_builder = new \ILIAS\Init\Environment\HttpPathBuilder(
87 new \ILIAS\Data\Factory(),
88 $this->getSettingsMock($wsdl_path, $allowed_hosts),
89 $this->getHttpsMock(),
90 $this->getIniMock($http_path),
91 $server_data
92 );
93
94 $this->assertNotEmpty((string) $path_builder->build());
95 }
96
97 public function testUnknownHostWillRaiseException(): void
98 {
99 $this->expectException(RuntimeException::class);
100
101 $path_builder = new \ILIAS\Init\Environment\HttpPathBuilder(
102 new \ILIAS\Data\Factory(),
103 $this->getSettingsMock('https://test.ilias.de/soap/server.php?wsdl=1', 'test.ilias.de'),
104 $this->getHttpsMock(),
105 $this->getIniMock('https://test.ilias.de'),
106 [
107 'HTTP_HOST' => 'phishing.ilias.de',
108 'REQUEST_URI' => '/login.php',
109 ]
110 );
111
112 $path_builder->build();
113 }
114
116 {
117 $this->expectException(RuntimeException::class);
118 $this->expectExceptionMessage('reserved for asset delivery');
119
120 $path_builder = new \ILIAS\Init\Environment\HttpPathBuilder(
121 new \ILIAS\Data\Factory(),
122 // content host added to allowed_hosts so we reach the isolation check
123 $this->getSettingsMock('https://app.ilias.de/soap/server.php?wsdl=1', 'content.ilias.de'),
124 $this->getHttpsMock(),
125 $this->getIniMock('https://app.ilias.de'),
126 [
127 'HTTP_HOST' => 'content.ilias.de',
128 'REQUEST_URI' => '/login.php',
129 ],
130 \ILIAS\FileDelivery\Isolation\IsolationConfig::fromArray([
131 \ILIAS\FileDelivery\Isolation\IsolationConfig::KEY_ACTIVATED => true,
132 \ILIAS\FileDelivery\Isolation\IsolationConfig::KEY_CONTENT_DOMAIN => 'https://content.ilias.de',
133 \ILIAS\FileDelivery\Isolation\IsolationConfig::KEY_ILIAS_DOMAIN => 'https://app.ilias.de',
134 ])
135 );
136
137 $path_builder->build();
138 }
139
141 {
142 $path_builder = new \ILIAS\Init\Environment\HttpPathBuilder(
143 new \ILIAS\Data\Factory(),
144 $this->getSettingsMock('https://app.ilias.de/soap/server.php?wsdl=1', ''),
145 $this->getHttpsMock(),
146 $this->getIniMock('https://app.ilias.de'),
147 [
148 'HTTP_HOST' => 'app.ilias.de',
149 'REQUEST_URI' => '/login.php',
150 ],
151 \ILIAS\FileDelivery\Isolation\IsolationConfig::fromArray([
152 \ILIAS\FileDelivery\Isolation\IsolationConfig::KEY_ACTIVATED => true,
153 \ILIAS\FileDelivery\Isolation\IsolationConfig::KEY_CONTENT_DOMAIN => 'https://content.ilias.de',
154 \ILIAS\FileDelivery\Isolation\IsolationConfig::KEY_ILIAS_DOMAIN => 'https://app.ilias.de',
155 ])
156 );
157
158 $this->assertSame('app.ilias.de', $path_builder->build()->getHost());
159 }
160
162 {
163 // isolation disabled -> the content host is treated like any other host;
164 // since it is in allowed_hosts here, build() must succeed
165 $path_builder = new \ILIAS\Init\Environment\HttpPathBuilder(
166 new \ILIAS\Data\Factory(),
167 $this->getSettingsMock('https://app.ilias.de/soap/server.php?wsdl=1', 'content.ilias.de'),
168 $this->getHttpsMock(),
169 $this->getIniMock('https://app.ilias.de'),
170 [
171 'HTTP_HOST' => 'content.ilias.de',
172 'REQUEST_URI' => '/login.php',
173 ],
175 );
176
177 $this->assertSame('content.ilias.de', $path_builder->build()->getHost());
178 }
179
180 private function getSettingsMock(
181 string $wsdl_path,
182 string $allowed_hosts
183 ): ilSetting&\PHPUnit\Framework\MockObject\MockObject {
184 $settings = $this
185 ->getMockBuilder(ilSetting::class)
186 ->disableOriginalConstructor()
187 ->onlyMethods(['get'])
188 ->getMock();
189 $settings->method('get')->willReturnCallback(
190 static fn(string $key, ?string $default = null): ?string => match ($key) {
191 'soap_wsdl_path' => $wsdl_path,
192 'allowed_hosts' => $allowed_hosts,
193 default => $default
194 }
195 );
196
197 return $settings;
198 }
199
200 private function getHttpsMock(): ilHttps&\PHPUnit\Framework\MockObject\MockObject
201 {
202 $https = $this
203 ->getMockBuilder(ilHTTPS::class)
204 ->disableOriginalConstructor()
205 ->onlyMethods(['isDetected'])
206 ->getMock();
207 $https->method('isDetected')->willReturn(true);
208
209 return $https;
210 }
211
212 private function getIniMock(string $http_path): ilIniFile&\PHPUnit\Framework\MockObject\MockObject
213 {
214 $ini = $this
215 ->getMockBuilder(ilIniFile::class)
216 ->disableOriginalConstructor()
217 ->onlyMethods(['readVariable'])
218 ->getMock();
219 $ini->method('readVariable')->willReturnCallback(
220 static fn(string $group, string $variable): string => match ($variable) {
221 'http_path' => $http_path,
222 default => ''
223 }
224 );
225
226 return $ini;
227 }
228}
$settings
Definition: index.php:44
getIniMock(string $http_path)
getSettingsMock(string $wsdl_path, string $allowed_hosts)
testValidHostsTriggerNoExceptions(array|ArrayAccess $server_data, string $http_path, string $wsdl_path, string $allowed_hosts)
testContentDomainIsRejectedAsApplicationHostWhenIsolationActive()
INI file parser — compatibility wrapper.
ILIAS Setting Class.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.