3 declare(strict_types
=1);
5 namespace Shaarli\Front
;
7 use Shaarli\Config\ConfigManager
;
8 use Shaarli\Container\ShaarliContainer
;
9 use Shaarli\Front\Exception\LoginBannedException
;
10 use Shaarli\Front\Exception\UnauthorizedException
;
11 use Shaarli\Render\PageBuilder
;
12 use Shaarli\Render\PageCacheManager
;
13 use Shaarli\Security\LoginManager
;
15 use Shaarli\Updater\Updater
;
16 use Slim\Http\Request
;
17 use Slim\Http\Response
;
20 class ShaarliMiddlewareTest
extends TestCase
22 protected const TMP_MOCK_FILE
= '.tmp';
24 /** @var ShaarliContainer */
27 /** @var ShaarliMiddleware */
28 protected $middleware;
30 public function setUp(): void
32 $this->container
= $this->createMock(ShaarliContainer
::class);
34 touch(static::TMP_MOCK_FILE
);
36 $this->container
->conf
= $this->createMock(ConfigManager
::class);
37 $this->container
->conf
->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE
);
39 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
41 $this->container
->environment
= ['REQUEST_URI' => 'http://shaarli/subfolder/path'];
43 $this->middleware
= new ShaarliMiddleware($this->container
);
46 public function tearDown(): void
48 unlink(static::TMP_MOCK_FILE
);
52 * Test middleware execution with valid controller call
54 public function testMiddlewareExecution(): void
56 $request = $this->createMock(Request
::class);
57 $request->method('getUri')->willReturnCallback(function (): Uri
{
58 $uri = $this->createMock(Uri
::class);
59 $uri->method('getBasePath')->willReturn('/subfolder');
64 $response = new Response();
65 $controller = function (Request
$request, Response
$response): Response
{
66 return $response->withStatus(418); // I'm a tea pot
69 /** @var Response $result */
70 $result = $this->middleware
->__invoke($request, $response, $controller);
72 static::assertInstanceOf(Response
::class, $result);
73 static::assertSame(418, $result->getStatusCode());
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.
80 public function testMiddlewareExecutionWithFrontException(): void
82 $request = $this->createMock(Request
::class);
83 $request->method('getUri')->willReturnCallback(function (): Uri
{
84 $uri = $this->createMock(Uri
::class);
85 $uri->method('getBasePath')->willReturn('/subfolder');
90 $response = new Response();
91 $controller = function (): void {
92 $exception = new LoginBannedException();
97 $pageBuilder = $this->createMock(PageBuilder
::class);
98 $pageBuilder->method('render')->willReturnCallback(function (string $message): string {
101 $this->container
->pageBuilder
= $pageBuilder;
103 $this->expectException(LoginBannedException
::class);
105 $this->middleware
->__invoke($request, $response, $controller);
109 * Test middleware execution with controller throwing a not authorized exception
110 * The middle should send a redirection response to the login page.
112 public function testMiddlewareExecutionWithUnauthorizedException(): void
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');
122 $response = new Response();
123 $controller = function (): void {
124 throw new UnauthorizedException();
127 /** @var Response $result */
128 $result = $this->middleware
->__invoke($request, $response, $controller);
130 static::assertSame(302, $result->getStatusCode());
132 '/subfolder/login?returnurl=' . urlencode('http://shaarli/subfolder/path'),
133 $result->getHeader('location')[0]
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.
141 public function testMiddlewareExecutionWithServerException(): void
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');
151 $dummyException = new class() extends \Exception
{};
153 $response = new Response();
154 $controller = function () use ($dummyException): void {
155 throw $dummyException;
159 $this->container
->pageBuilder
= $this->createMock(PageBuilder
::class);
160 $this->container
->pageBuilder
->method('render')->willReturnCallback(function (string $message): string {
163 $this->container
->pageBuilder
165 ->willReturnCallback(function (string $key, string $value) use (&$parameters): void {
166 $parameters[$key] = $value;
170 $this->expectException(get_class($dummyException));
172 $this->middleware
->__invoke($request, $response, $controller);
175 public function testMiddlewareExecutionWithUpdates(): void
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');
185 $response = new Response();
186 $controller = function (Request
$request, Response
$response): Response
{
187 return $response->withStatus(418); // I'm a tea pot
190 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
191 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
193 $this->container
->conf
= $this->createMock(ConfigManager
::class);
194 $this->container
->conf
->method('get')->willReturnCallback(function (string $key): string {
197 $this->container
->conf
->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE
);
199 $this->container
->pageCacheManager
= $this->createMock(PageCacheManager
::class);
200 $this->container
->pageCacheManager
->expects(static::once())->method('invalidateCaches');
202 $this->container
->updater
= $this->createMock(Updater
::class);
203 $this->container
->updater
204 ->expects(static::once())
206 ->willReturn(['update123'])
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)
215 /** @var Response $result */
216 $result = $this->middleware
->__invoke($request, $response, $controller);
218 static::assertInstanceOf(Response
::class, $result);
219 static::assertSame(418, $result->getStatusCode());