|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Response; |
|
6
|
|
|
use Nyholm\Psr7\ServerRequest; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Yiisoft\Aliases\Aliases; |
|
12
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
|
13
|
|
|
use Yiisoft\Yii\Web\Exception\BadUriPrefixException; |
|
14
|
|
|
use Yiisoft\Yii\Web\Middleware\SubFolder; |
|
15
|
|
|
|
|
16
|
|
|
class SubFolderTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
protected string $urlGeneratorUriPrefix; |
|
19
|
|
|
protected Aliases $aliases; |
|
20
|
|
|
protected ?ServerRequestInterface $lastRequest; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->urlGeneratorUriPrefix = ''; |
|
25
|
|
|
$this->lastRequest = null; |
|
26
|
|
|
$this->aliases = new Aliases(['@web' => '/default/web']); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testDefault(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$request = $this->createRequest($uri = '/', $script = '/index.php'); |
|
32
|
|
|
$mw = $this->createMiddleware(); |
|
33
|
|
|
|
|
34
|
|
|
$this->process($mw, $request); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertEquals('/default/web', $this->aliases->get('@web')); |
|
37
|
|
|
$this->assertEquals('', $this->urlGeneratorUriPrefix); |
|
38
|
|
|
$this->assertEquals($uri, $this->getRequestPath()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testCustomPrefix(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$request = $this->createRequest($uri = '/custom_public/index.php?test', $script = '/index.php'); |
|
44
|
|
|
$mw = $this->createMiddleware(); |
|
45
|
|
|
$mw->prefix = '/custom_public'; |
|
46
|
|
|
|
|
47
|
|
|
$this->process($mw, $request); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertEquals('/custom_public', $this->aliases->get('@web')); |
|
50
|
|
|
$this->assertEquals('/custom_public', $this->urlGeneratorUriPrefix); |
|
51
|
|
|
$this->assertEquals('/index.php', $this->getRequestPath()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testAutoPrefix(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$request = $this->createRequest($uri = '/public/', $script = '/public/index.php'); |
|
57
|
|
|
$mw = $this->createMiddleware(); |
|
58
|
|
|
|
|
59
|
|
|
$this->process($mw, $request); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertEquals('/public', $this->aliases->get('@web')); |
|
62
|
|
|
$this->assertEquals('/public', $this->urlGeneratorUriPrefix); |
|
63
|
|
|
$this->assertEquals('/', $this->getRequestPath()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testAutoPrefixLogn(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$prefix = '/root/php/dev-server/project-42/index_html/public/web'; |
|
69
|
|
|
$uri = "{$prefix}/"; |
|
70
|
|
|
$script = "{$prefix}/index.php"; |
|
71
|
|
|
$request = $this->createRequest($uri, $script); |
|
72
|
|
|
$mw = $this->createMiddleware(); |
|
73
|
|
|
|
|
74
|
|
|
$this->process($mw, $request); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals($prefix, $this->aliases->get('@web')); |
|
77
|
|
|
$this->assertEquals($prefix, $this->urlGeneratorUriPrefix); |
|
78
|
|
|
$this->assertEquals('/', $this->getRequestPath()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testAutoPrefixAndUriWithoutTrailingSlash(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$request = $this->createRequest($uri = '/public', $script = '/public/index.php'); |
|
84
|
|
|
$mw = $this->createMiddleware(); |
|
85
|
|
|
|
|
86
|
|
|
$this->process($mw, $request); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertEquals('/public', $this->aliases->get('@web')); |
|
89
|
|
|
$this->assertEquals('/public', $this->urlGeneratorUriPrefix); |
|
90
|
|
|
$this->assertEquals('/', $this->getRequestPath()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testAutoPrefixFullUrl(): void |
|
94
|
|
|
{ |
|
95
|
|
|
$request = $this->createRequest($uri = '/public/index.php?test', $script = '/public/index.php'); |
|
96
|
|
|
$mw = $this->createMiddleware(); |
|
97
|
|
|
|
|
98
|
|
|
$this->process($mw, $request); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals('/public', $this->aliases->get('@web')); |
|
101
|
|
|
$this->assertEquals('/public', $this->urlGeneratorUriPrefix); |
|
102
|
|
|
$this->assertEquals('/index.php', $this->getRequestPath()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testFailedAutoPrefix(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$request = $this->createRequest($uri = '/web/index.php', $script = '/public/index.php'); |
|
108
|
|
|
$mw = $this->createMiddleware(); |
|
109
|
|
|
|
|
110
|
|
|
$this->process($mw, $request); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals('/default/web', $this->aliases->get('@web')); |
|
113
|
|
|
$this->assertEquals('', $this->urlGeneratorUriPrefix); |
|
114
|
|
|
$this->assertEquals($uri, $this->getRequestPath()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testCustomPrefixWithTrailingSlash(): void |
|
118
|
|
|
{ |
|
119
|
|
|
$request = $this->createRequest($uri = '/web/', $script = '/public/index.php'); |
|
120
|
|
|
$mw = $this->createMiddleware(); |
|
121
|
|
|
$mw->prefix = '/web/'; |
|
122
|
|
|
|
|
123
|
|
|
$this->expectException(BadUriPrefixException::class); |
|
124
|
|
|
$this->expectExceptionMessage('Wrong URI prefix value'); |
|
125
|
|
|
|
|
126
|
|
|
$this->process($mw, $request); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testCustomPrefixFromMiddleOfUri(): void |
|
130
|
|
|
{ |
|
131
|
|
|
$request = $this->createRequest($uri = '/web/middle/public', $script = '/public/index.php'); |
|
132
|
|
|
$mw = $this->createMiddleware(); |
|
133
|
|
|
$mw->prefix = '/middle'; |
|
134
|
|
|
|
|
135
|
|
|
$this->expectException(BadUriPrefixException::class); |
|
136
|
|
|
$this->expectExceptionMessage('URI prefix does not match'); |
|
137
|
|
|
|
|
138
|
|
|
$this->process($mw, $request); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testCustomPrefixDoesNotMatch(): void |
|
142
|
|
|
{ |
|
143
|
|
|
$request = $this->createRequest($uri = '/web/', $script = '/public/index.php'); |
|
144
|
|
|
$mw = $this->createMiddleware(); |
|
145
|
|
|
$mw->prefix = '/other_prefix'; |
|
146
|
|
|
|
|
147
|
|
|
$this->expectException(BadUriPrefixException::class); |
|
148
|
|
|
$this->expectExceptionMessage('URI prefix does not match'); |
|
149
|
|
|
|
|
150
|
|
|
$this->process($mw, $request); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testCustomPrefixDoesNotMatchCompletely(): void |
|
154
|
|
|
{ |
|
155
|
|
|
$request = $this->createRequest($uri = '/project1/web/', $script = '/public/index.php'); |
|
156
|
|
|
$mw = $this->createMiddleware(); |
|
157
|
|
|
$mw->prefix = '/project1/we'; |
|
158
|
|
|
|
|
159
|
|
|
$this->expectException(BadUriPrefixException::class); |
|
160
|
|
|
$this->expectExceptionMessage('URI prefix does not match completely'); |
|
161
|
|
|
|
|
162
|
|
|
$this->process($mw, $request); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function testAutoPrefixDoesNotMatchCompletely(): void |
|
166
|
|
|
{ |
|
167
|
|
|
$request = $this->createRequest($uri = '/public/web/', $script = '/pub/index.php'); |
|
168
|
|
|
$mw = $this->createMiddleware(); |
|
169
|
|
|
|
|
170
|
|
|
$this->process($mw, $request); |
|
171
|
|
|
|
|
172
|
|
|
$this->assertEquals('/default/web', $this->aliases->get('@web')); |
|
173
|
|
|
$this->assertEquals('', $this->urlGeneratorUriPrefix); |
|
174
|
|
|
$this->assertEquals($uri, $this->getRequestPath()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
private function process(SubFolder $middleware, ServerRequestInterface $request): ResponseInterface |
|
178
|
|
|
{ |
|
179
|
|
|
$handler = new class() implements RequestHandlerInterface { |
|
180
|
|
|
public ?ServerRequestInterface $request = null; |
|
181
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
182
|
|
|
{ |
|
183
|
|
|
$this->request = $request; |
|
184
|
|
|
return new Response(); |
|
185
|
|
|
} |
|
186
|
|
|
}; |
|
187
|
|
|
$this->lastRequest = &$handler->request; |
|
188
|
|
|
return $middleware->process($request, $handler); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
private function getRequestPath(): string |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->lastRequest->getUri()->getPath(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
private function createMiddleware(): SubFolder |
|
197
|
|
|
{ |
|
198
|
|
|
$urlGenerator = $this->createMock(UrlGeneratorInterface::class); |
|
199
|
|
|
$urlGenerator->method('setUriPrefix')->willReturnCallback(function ($prefix) { |
|
200
|
|
|
$this->urlGeneratorUriPrefix = $prefix; |
|
201
|
|
|
}); |
|
202
|
|
|
$urlGenerator->method('getUriPrefix')->willReturnReference($this->urlGeneratorUriPrefix); |
|
203
|
|
|
|
|
204
|
|
|
return new SubFolder($urlGenerator, $this->aliases); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
private function createRequest(string $uri = '/', string $scriptPath = '/'): ServerRequestInterface |
|
208
|
|
|
{ |
|
209
|
|
|
return new ServerRequest('get', $uri, [], null, '1.1', ['SCRIPT_NAME' => $scriptPath]); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|