diff options
Diffstat (limited to 'tests/front/ShaarliMiddlewareTest.php')
-rw-r--r-- | tests/front/ShaarliMiddlewareTest.php | 130 |
1 files changed, 125 insertions, 5 deletions
diff --git a/tests/front/ShaarliMiddlewareTest.php b/tests/front/ShaarliMiddlewareTest.php index 57be1002..81ea1344 100644 --- a/tests/front/ShaarliMiddlewareTest.php +++ b/tests/front/ShaarliMiddlewareTest.php | |||
@@ -8,7 +8,11 @@ use PHPUnit\Framework\TestCase; | |||
8 | use Shaarli\Config\ConfigManager; | 8 | use Shaarli\Config\ConfigManager; |
9 | use Shaarli\Container\ShaarliContainer; | 9 | use Shaarli\Container\ShaarliContainer; |
10 | use Shaarli\Front\Exception\LoginBannedException; | 10 | use Shaarli\Front\Exception\LoginBannedException; |
11 | use Shaarli\Front\Exception\UnauthorizedException; | ||
11 | use Shaarli\Render\PageBuilder; | 12 | use Shaarli\Render\PageBuilder; |
13 | use Shaarli\Render\PageCacheManager; | ||
14 | use Shaarli\Security\LoginManager; | ||
15 | use Shaarli\Updater\Updater; | ||
12 | use Slim\Http\Request; | 16 | use Slim\Http\Request; |
13 | use Slim\Http\Response; | 17 | use Slim\Http\Response; |
14 | use Slim\Http\Uri; | 18 | use Slim\Http\Uri; |
@@ -24,9 +28,16 @@ class ShaarliMiddlewareTest extends TestCase | |||
24 | public function setUp(): void | 28 | public function setUp(): void |
25 | { | 29 | { |
26 | $this->container = $this->createMock(ShaarliContainer::class); | 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 | |||
27 | $this->middleware = new ShaarliMiddleware($this->container); | 35 | $this->middleware = new ShaarliMiddleware($this->container); |
28 | } | 36 | } |
29 | 37 | ||
38 | /** | ||
39 | * Test middleware execution with valid controller call | ||
40 | */ | ||
30 | public function testMiddlewareExecution(): void | 41 | public function testMiddlewareExecution(): void |
31 | { | 42 | { |
32 | $request = $this->createMock(Request::class); | 43 | $request = $this->createMock(Request::class); |
@@ -49,7 +60,10 @@ class ShaarliMiddlewareTest extends TestCase | |||
49 | static::assertSame(418, $result->getStatusCode()); | 60 | static::assertSame(418, $result->getStatusCode()); |
50 | } | 61 | } |
51 | 62 | ||
52 | public function testMiddlewareExecutionWithException(): void | 63 | /** |
64 | * Test middleware execution with controller throwing a known front exception | ||
65 | */ | ||
66 | public function testMiddlewareExecutionWithFrontException(): void | ||
53 | { | 67 | { |
54 | $request = $this->createMock(Request::class); | 68 | $request = $this->createMock(Request::class); |
55 | $request->method('getUri')->willReturnCallback(function (): Uri { | 69 | $request->method('getUri')->willReturnCallback(function (): Uri { |
@@ -58,7 +72,7 @@ class ShaarliMiddlewareTest extends TestCase | |||
58 | 72 | ||
59 | return $uri; | 73 | return $uri; |
60 | }); | 74 | }); |
61 | 75 | ||
62 | $response = new Response(); | 76 | $response = new Response(); |
63 | $controller = function (): void { | 77 | $controller = function (): void { |
64 | $exception = new LoginBannedException(); | 78 | $exception = new LoginBannedException(); |
@@ -72,9 +86,6 @@ class ShaarliMiddlewareTest extends TestCase | |||
72 | }); | 86 | }); |
73 | $this->container->pageBuilder = $pageBuilder; | 87 | $this->container->pageBuilder = $pageBuilder; |
74 | 88 | ||
75 | $conf = $this->createMock(ConfigManager::class); | ||
76 | $this->container->conf = $conf; | ||
77 | |||
78 | /** @var Response $result */ | 89 | /** @var Response $result */ |
79 | $result = $this->middleware->__invoke($request, $response, $controller); | 90 | $result = $this->middleware->__invoke($request, $response, $controller); |
80 | 91 | ||
@@ -82,4 +93,113 @@ class ShaarliMiddlewareTest extends TestCase | |||
82 | static::assertSame(401, $result->getStatusCode()); | 93 | static::assertSame(401, $result->getStatusCode()); |
83 | static::assertContains('error', (string) $result->getBody()); | 94 | static::assertContains('error', (string) $result->getBody()); |
84 | } | 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 | } | ||
85 | } | 205 | } |