]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/ShaarliMiddlewareTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / front / ShaarliMiddlewareTest.php
CommitLineData
6c50a6cc
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front;
6
6c50a6cc
A
7use Shaarli\Config\ConfigManager;
8use Shaarli\Container\ShaarliContainer;
9use Shaarli\Front\Exception\LoginBannedException;
1a8ac737 10use Shaarli\Front\Exception\UnauthorizedException;
6c50a6cc 11use Shaarli\Render\PageBuilder;
1a8ac737
A
12use Shaarli\Render\PageCacheManager;
13use Shaarli\Security\LoginManager;
a5a9cf23 14use Shaarli\TestCase;
1a8ac737 15use Shaarli\Updater\Updater;
6c50a6cc
A
16use Slim\Http\Request;
17use Slim\Http\Response;
818b3193 18use Slim\Http\Uri;
6c50a6cc
A
19
20class ShaarliMiddlewareTest extends TestCase
21{
c4ad3d4f
A
22 protected const TMP_MOCK_FILE = '.tmp';
23
6c50a6cc
A
24 /** @var ShaarliContainer */
25 protected $container;
26
27 /** @var ShaarliMiddleware */
28 protected $middleware;
29
30 public function setUp(): void
31 {
32 $this->container = $this->createMock(ShaarliContainer::class);
1a8ac737 33
c4ad3d4f
A
34 touch(static::TMP_MOCK_FILE);
35
1a8ac737 36 $this->container->conf = $this->createMock(ConfigManager::class);
c4ad3d4f
A
37 $this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE);
38
1a8ac737
A
39 $this->container->loginManager = $this->createMock(LoginManager::class);
40
a8c11451
A
41 $this->container->environment = ['REQUEST_URI' => 'http://shaarli/subfolder/path'];
42
6c50a6cc
A
43 $this->middleware = new ShaarliMiddleware($this->container);
44 }
45
bedbb845 46 public function tearDown(): void
c4ad3d4f
A
47 {
48 unlink(static::TMP_MOCK_FILE);
49 }
50
1a8ac737
A
51 /**
52 * Test middleware execution with valid controller call
53 */
6c50a6cc
A
54 public function testMiddlewareExecution(): void
55 {
56 $request = $this->createMock(Request::class);
818b3193
A
57 $request->method('getUri')->willReturnCallback(function (): Uri {
58 $uri = $this->createMock(Uri::class);
59 $uri->method('getBasePath')->willReturn('/subfolder');
60
61 return $uri;
62 });
63
6c50a6cc
A
64 $response = new Response();
65 $controller = function (Request $request, Response $response): Response {
66 return $response->withStatus(418); // I'm a tea pot
67 };
68
69 /** @var Response $result */
70 $result = $this->middleware->__invoke($request, $response, $controller);
71
72 static::assertInstanceOf(Response::class, $result);
73 static::assertSame(418, $result->getStatusCode());
74 }
75
1a8ac737 76 /**
0c6fdbe1
A
77 * Test middleware execution with controller throwing a known front exception.
78 * The exception should be thrown to be later handled by the error handler.
1a8ac737
A
79 */
80 public function testMiddlewareExecutionWithFrontException(): void
6c50a6cc
A
81 {
82 $request = $this->createMock(Request::class);
818b3193
A
83 $request->method('getUri')->willReturnCallback(function (): Uri {
84 $uri = $this->createMock(Uri::class);
85 $uri->method('getBasePath')->willReturn('/subfolder');
86
87 return $uri;
88 });
1a8ac737 89
6c50a6cc
A
90 $response = new Response();
91 $controller = function (): void {
92 $exception = new LoginBannedException();
93
94 throw new $exception;
95 };
96
97 $pageBuilder = $this->createMock(PageBuilder::class);
98 $pageBuilder->method('render')->willReturnCallback(function (string $message): string {
99 return $message;
100 });
101 $this->container->pageBuilder = $pageBuilder;
102
0c6fdbe1 103 $this->expectException(LoginBannedException::class);
6c50a6cc 104
0c6fdbe1 105 $this->middleware->__invoke($request, $response, $controller);
6c50a6cc 106 }
1a8ac737
A
107
108 /**
109 * Test middleware execution with controller throwing a not authorized exception
0c6fdbe1 110 * The middle should send a redirection response to the login page.
1a8ac737
A
111 */
112 public function testMiddlewareExecutionWithUnauthorizedException(): void
113 {
114 $request = $this->createMock(Request::class);
115 $request->method('getUri')->willReturnCallback(function (): Uri {
116 $uri = $this->createMock(Uri::class);
117 $uri->method('getBasePath')->willReturn('/subfolder');
118
119 return $uri;
120 });
121
122 $response = new Response();
123 $controller = function (): void {
124 throw new UnauthorizedException();
125 };
126
127 /** @var Response $result */
128 $result = $this->middleware->__invoke($request, $response, $controller);
129
130 static::assertSame(302, $result->getStatusCode());
a8c11451
A
131 static::assertSame(
132 '/subfolder/login?returnurl=' . urlencode('http://shaarli/subfolder/path'),
133 $result->getHeader('location')[0]
134 );
1a8ac737
A
135 }
136
137 /**
0c6fdbe1
A
138 * Test middleware execution with controller throwing a not authorized exception.
139 * The exception should be thrown to be later handled by the error handler.
1a8ac737 140 */
0c6fdbe1 141 public function testMiddlewareExecutionWithServerException(): void
1a8ac737
A
142 {
143 $request = $this->createMock(Request::class);
144 $request->method('getUri')->willReturnCallback(function (): Uri {
145 $uri = $this->createMock(Uri::class);
146 $uri->method('getBasePath')->willReturn('/subfolder');
147
148 return $uri;
149 });
150
0c6fdbe1
A
151 $dummyException = new class() extends \Exception {};
152
1a8ac737 153 $response = new Response();
0c6fdbe1
A
154 $controller = function () use ($dummyException): void {
155 throw $dummyException;
1a8ac737
A
156 };
157
158 $parameters = [];
159 $this->container->pageBuilder = $this->createMock(PageBuilder::class);
160 $this->container->pageBuilder->method('render')->willReturnCallback(function (string $message): string {
161 return $message;
162 });
163 $this->container->pageBuilder
164 ->method('assign')
165 ->willReturnCallback(function (string $key, string $value) use (&$parameters): void {
166 $parameters[$key] = $value;
167 })
168 ;
169
0c6fdbe1 170 $this->expectException(get_class($dummyException));
1a8ac737 171
0c6fdbe1 172 $this->middleware->__invoke($request, $response, $controller);
1a8ac737
A
173 }
174
175 public function testMiddlewareExecutionWithUpdates(): void
176 {
177 $request = $this->createMock(Request::class);
178 $request->method('getUri')->willReturnCallback(function (): Uri {
179 $uri = $this->createMock(Uri::class);
180 $uri->method('getBasePath')->willReturn('/subfolder');
181
182 return $uri;
183 });
184
185 $response = new Response();
186 $controller = function (Request $request, Response $response): Response {
187 return $response->withStatus(418); // I'm a tea pot
188 };
189
190 $this->container->loginManager = $this->createMock(LoginManager::class);
191 $this->container->loginManager->method('isLoggedIn')->willReturn(true);
192
193 $this->container->conf = $this->createMock(ConfigManager::class);
194 $this->container->conf->method('get')->willReturnCallback(function (string $key): string {
195 return $key;
196 });
c4ad3d4f 197 $this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE);
1a8ac737
A
198
199 $this->container->pageCacheManager = $this->createMock(PageCacheManager::class);
200 $this->container->pageCacheManager->expects(static::once())->method('invalidateCaches');
201
202 $this->container->updater = $this->createMock(Updater::class);
203 $this->container->updater
204 ->expects(static::once())
205 ->method('update')
206 ->willReturn(['update123'])
207 ;
208 $this->container->updater->method('getDoneUpdates')->willReturn($updates = ['update123', 'other']);
209 $this->container->updater
210 ->expects(static::once())
211 ->method('writeUpdates')
212 ->with('resource.updates', $updates)
213 ;
214
215 /** @var Response $result */
216 $result = $this->middleware->__invoke($request, $response, $controller);
217
218 static::assertInstanceOf(Response::class, $result);
219 static::assertSame(418, $result->getStatusCode());
220 }
6c50a6cc 221}