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