ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
HttpPathBuilderTest.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
20use PHPUnit\Framework\TestCase;
21
22class HttpPathBuilderTest extends TestCase
23{
32 public static function environmentProvider(): Generator
33 {
34 yield 'Host matches configured HTTP path (no `allowed_hosts` configuration)' => [
35 'server_data' => [
36 'HTTP_HOST' => 'localhost',
37 'REQUEST_URI' => '/login.php',
38 ],
39 'http_path' => 'http://localhost',
40 'wsdl_path' => 'https://localhost/soap/server.php?wsdl=1',
41 'allowed_hosts' => '',
42 ];
43
44 yield 'Host matches configured WSDL path (no `allowed_hosts` configuration)' => [
45 'server_data' => [
46 'HTTP_HOST' => 'soap.ilias.de',
47 'REQUEST_URI' => '/login.php',
48 ],
49 'http_path' => 'https://test.ilias.de',
50 'wsdl_path' => 'https://soap.ilias.de/soap/server.php?wsdl=1',
51 'allowed_hosts' => '',
52 ];
53
54 yield 'Localhost is always allowed (no `allowed_hosts` configuration)' => [
55 'server_data' => [
56 'HTTP_HOST' => 'localhost',
57 'REQUEST_URI' => '/login.php',
58 ],
59 'http_path' => 'https://test.ilias.de',
60 'wsdl_path' => 'https://test.ilias.de/soap/server.php?wsdl=1',
61 'allowed_hosts' => '',
62 ];
63
64 yield 'Host is in `allowed_hosts` list' => [
65 'server_data' => [
66 'HTTP_HOST' => 'test2.ilias.de',
67 'REQUEST_URI' => '/login.php',
68 ],
69 'http_path' => 'https://test.ilias.de',
70 'wsdl_path' => 'https://test.ilias.de/soap/server.php?wsdl=1',
71 'allowed_hosts' => 'test2.ilias.de',
72 ];
73 }
74
78 #[\PHPUnit\Framework\Attributes\DataProvider('environmentProvider')]
80 array|ArrayAccess $server_data,
81 string $http_path,
82 string $wsdl_path,
83 string $allowed_hosts
84 ): void {
85 $path_builder = new \ILIAS\Init\Environment\HttpPathBuilder(
86 new \ILIAS\Data\Factory(),
87 $this->getSettingsMock($wsdl_path, $allowed_hosts),
88 $this->getHttpsMock(),
89 $this->getIniMock($http_path),
90 $server_data
91 );
92
93 $this->assertNotEmpty((string) $path_builder->build());
94 }
95
96 public function testUnknownHostWillRaiseException(): void
97 {
98 $this->expectException(RuntimeException::class);
99
100 $path_builder = new \ILIAS\Init\Environment\HttpPathBuilder(
101 new \ILIAS\Data\Factory(),
102 $this->getSettingsMock('https://test.ilias.de/soap/server.php?wsdl=1', 'test.ilias.de'),
103 $this->getHttpsMock(),
104 $this->getIniMock('https://test.ilias.de'),
105 [
106 'HTTP_HOST' => 'phishing.ilias.de',
107 'REQUEST_URI' => '/login.php',
108 ]
109 );
110
111 $path_builder->build();
112 }
113
114 private function getSettingsMock(
115 string $wsdl_path,
116 string $allowed_hosts
117 ): ilSetting&\PHPUnit\Framework\MockObject\MockObject {
118 $settings = $this
119 ->getMockBuilder(ilSetting::class)
120 ->disableOriginalConstructor()
121 ->onlyMethods(['get'])
122 ->getMock();
123 $settings->method('get')->willReturnCallback(
124 static fn(string $key, ?string $default = null): ?string => match ($key) {
125 'soap_wsdl_path' => $wsdl_path,
126 'allowed_hosts' => $allowed_hosts,
127 default => $default
128 }
129 );
130
131 return $settings;
132 }
133
134 private function getHttpsMock(): ilHttps&\PHPUnit\Framework\MockObject\MockObject
135 {
136 $https = $this
137 ->getMockBuilder(ilHTTPS::class)
138 ->disableOriginalConstructor()
139 ->onlyMethods(['isDetected'])
140 ->getMock();
141 $https->method('isDetected')->willReturn(true);
142
143 return $https;
144 }
145
146 private function getIniMock(string $http_path): ilIniFile&\PHPUnit\Framework\MockObject\MockObject
147 {
148 $ini = $this
149 ->getMockBuilder(ilIniFile::class)
150 ->disableOriginalConstructor()
151 ->onlyMethods(['readVariable'])
152 ->getMock();
153 $ini->method('readVariable')->willReturnCallback(
154 static fn(string $group, string $variable): string => match ($variable) {
155 'http_path' => $http_path,
156 default => ''
157 }
158 );
159
160 return $ini;
161 }
162}
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)
INIFile Parser Early access in init proceess! Avoid further dependencies like logging or other servic...
ILIAS Setting Class.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$ini
Definition: raiseError.php:20