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