aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/visitor/ShaarliPublicControllerTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-22 13:20:31 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit2899ebb5b5e82890c877151f5c02045266ac9973 (patch)
tree0c4e2684c7f6d161f92a21181bfa4b2f78d6a82f /tests/front/controller/visitor/ShaarliPublicControllerTest.php
parentaf290059d10319e76d1e7d78b592cab99c26d91a (diff)
downloadShaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.gz
Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.zst
Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.zip
Initialize admin Slim controllers
- Reorganize visitor controllers - Fix redirection with Slim's requests base path - Fix daily links
Diffstat (limited to 'tests/front/controller/visitor/ShaarliPublicControllerTest.php')
-rw-r--r--tests/front/controller/visitor/ShaarliPublicControllerTest.php239
1 files changed, 239 insertions, 0 deletions
diff --git a/tests/front/controller/visitor/ShaarliPublicControllerTest.php b/tests/front/controller/visitor/ShaarliPublicControllerTest.php
new file mode 100644
index 00000000..e2e88da3
--- /dev/null
+++ b/tests/front/controller/visitor/ShaarliPublicControllerTest.php
@@ -0,0 +1,239 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Visitor;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkFilter;
9use Slim\Http\Request;
10use Slim\Http\Response;
11use Slim\Http\Uri;
12
13/**
14 * Class ShaarliControllerTest
15 *
16 * This class is used to test default behavior of ShaarliController abstract class.
17 * It uses a dummy non abstract controller.
18 */
19class ShaarliControllerTest extends TestCase
20{
21 use FrontControllerMockHelper;
22
23 /** @var LoginController */
24 protected $controller;
25
26 /** @var mixed[] List of variable assigned to the template */
27 protected $assignedValues;
28
29 /** @var Request */
30 protected $request;
31
32 public function setUp(): void
33 {
34 $this->createContainer();
35
36 $this->controller = new class($this->container) extends ShaarliVisitorController
37 {
38 public function assignView(string $key, $value): ShaarliVisitorController
39 {
40 return parent::assignView($key, $value);
41 }
42
43 public function render(string $template): string
44 {
45 return parent::render($template);
46 }
47
48 public function redirectFromReferer(
49 Request $request,
50 Response $response,
51 array $loopTerms = [],
52 array $clearParams = []
53 ): Response {
54 return parent::redirectFromReferer($request, $response, $loopTerms, $clearParams);
55 }
56 };
57 $this->assignedValues = [];
58
59 $this->request = $this->createMock(Request::class);
60 $this->request->method('getUri')->willReturnCallback(function (): Uri {
61 $uri = $this->createMock(Uri::class);
62 $uri->method('getBasePath')->willReturn('/subfolder');
63
64 return $uri;
65 });
66 }
67
68 public function testAssignView(): void
69 {
70 $this->createValidContainerMockSet();
71
72 $this->assignTemplateVars($this->assignedValues);
73
74 $self = $this->controller->assignView('variableName', 'variableValue');
75
76 static::assertInstanceOf(ShaarliVisitorController::class, $self);
77 static::assertSame('variableValue', $this->assignedValues['variableName']);
78 }
79
80 public function testRender(): void
81 {
82 $this->createValidContainerMockSet();
83
84 $this->assignTemplateVars($this->assignedValues);
85
86 $this->container->bookmarkService
87 ->method('count')
88 ->willReturnCallback(function (string $visibility): int {
89 return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
90 })
91 ;
92
93 $this->container->pluginManager
94 ->method('executeHooks')
95 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
96 return $data[$hook] = $params;
97 });
98 $this->container->pluginManager->method('getErrors')->willReturn(['error']);
99
100 $this->container->loginManager->method('isLoggedIn')->willReturn(true);
101
102 $render = $this->controller->render('templateName');
103
104 static::assertSame('templateName', $render);
105
106 static::assertSame(10, $this->assignedValues['linkcount']);
107 static::assertSame(5, $this->assignedValues['privateLinkcount']);
108 static::assertSame(['error'], $this->assignedValues['plugin_errors']);
109
110 static::assertSame('templateName', $this->assignedValues['plugins_includes']['render_includes']['target']);
111 static::assertTrue($this->assignedValues['plugins_includes']['render_includes']['loggedin']);
112 static::assertSame('templateName', $this->assignedValues['plugins_header']['render_header']['target']);
113 static::assertTrue($this->assignedValues['plugins_header']['render_header']['loggedin']);
114 static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']);
115 static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']);
116 }
117
118 /**
119 * Test redirectFromReferer() - Default behaviour
120 */
121 public function testRedirectFromRefererDefault(): void
122 {
123 $this->createValidContainerMockSet();
124
125 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
126
127 $response = new Response();
128
129 $result = $this->controller->redirectFromReferer($this->request, $response);
130
131 static::assertSame(302, $result->getStatusCode());
132 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
133 }
134
135 /**
136 * Test redirectFromReferer() - With a loop term not matched in the referer
137 */
138 public function testRedirectFromRefererWithUnmatchedLoopTerm(): void
139 {
140 $this->createValidContainerMockSet();
141
142 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
143
144 $response = new Response();
145
146 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope']);
147
148 static::assertSame(302, $result->getStatusCode());
149 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
150 }
151
152 /**
153 * Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default
154 */
155 public function testRedirectFromRefererWithMatchingLoopTermInPath(): void
156 {
157 $this->createValidContainerMockSet();
158
159 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
160
161 $response = new Response();
162
163 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'controller']);
164
165 static::assertSame(302, $result->getStatusCode());
166 static::assertSame(['/subfolder'], $result->getHeader('location'));
167 }
168
169 /**
170 * Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default
171 */
172 public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void
173 {
174 $this->createValidContainerMockSet();
175
176 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
177
178 $response = new Response();
179
180 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'other']);
181
182 static::assertSame(302, $result->getStatusCode());
183 static::assertSame(['/subfolder'], $result->getHeader('location'));
184 }
185
186 /**
187 * Test redirectFromReferer() - With a loop term matching the referer in its query value
188 * -> we do not block redirection for query parameter values.
189 */
190 public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void
191 {
192 $this->createValidContainerMockSet();
193
194 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
195
196 $response = new Response();
197
198 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'param']);
199
200 static::assertSame(302, $result->getStatusCode());
201 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
202 }
203
204 /**
205 * Test redirectFromReferer() - With a loop term matching the referer in its domain name
206 * -> we do not block redirection for shaarli's hosts
207 */
208 public function testRedirectFromRefererWithLoopTermInDomain(): void
209 {
210 $this->createValidContainerMockSet();
211
212 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
213
214 $response = new Response();
215
216 $result = $this->controller->redirectFromReferer($this->request, $response, ['shaarli']);
217
218 static::assertSame(302, $result->getStatusCode());
219 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
220 }
221
222 /**
223 * Test redirectFromReferer() - With a loop term matching a query parameter AND clear this query param
224 * -> the param should be cleared before checking if it matches the redir loop terms
225 */
226 public function testRedirectFromRefererWithMatchingClearedParam(): void
227 {
228 $this->createValidContainerMockSet();
229
230 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
231
232 $response = new Response();
233
234 $result = $this->controller->redirectFromReferer($this->request, $response, ['query'], ['query']);
235
236 static::assertSame(302, $result->getStatusCode());
237 static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));
238 }
239}