]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/front/controller/visitor/ShaarliPublicControllerTest.php
e2e88da3f64d9d0372b49c2978c9d8deddca1d82
[github/shaarli/Shaarli.git] / tests / front / controller / visitor / ShaarliPublicControllerTest.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Front\Controller\Visitor;
6
7 use PHPUnit\Framework\TestCase;
8 use Shaarli\Bookmark\BookmarkFilter;
9 use Slim\Http\Request;
10 use Slim\Http\Response;
11 use 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 */
19 class 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 }