]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/visitor/ShaarliPublicControllerTest.php
Initialize admin Slim controllers
[github/shaarli/Shaarli.git] / tests / front / controller / visitor / ShaarliPublicControllerTest.php
CommitLineData
0498b209
A
1<?php
2
3declare(strict_types=1);
4
2899ebb5 5namespace Shaarli\Front\Controller\Visitor;
0498b209
A
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkFilter;
2899ebb5 9use Slim\Http\Request;
af290059 10use Slim\Http\Response;
2899ebb5 11use Slim\Http\Uri;
0498b209
A
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{
dd09ec52 21 use FrontControllerMockHelper;
0498b209
A
22
23 /** @var LoginController */
24 protected $controller;
25
26 /** @var mixed[] List of variable assigned to the template */
27 protected $assignedValues;
28
2899ebb5
A
29 /** @var Request */
30 protected $request;
31
0498b209
A
32 public function setUp(): void
33 {
dd09ec52
A
34 $this->createContainer();
35
2899ebb5 36 $this->controller = new class($this->container) extends ShaarliVisitorController
0498b209 37 {
2899ebb5 38 public function assignView(string $key, $value): ShaarliVisitorController
0498b209
A
39 {
40 return parent::assignView($key, $value);
41 }
42
43 public function render(string $template): string
44 {
45 return parent::render($template);
46 }
af290059
A
47
48 public function redirectFromReferer(
2899ebb5 49 Request $request,
af290059
A
50 Response $response,
51 array $loopTerms = [],
52 array $clearParams = []
53 ): Response {
2899ebb5 54 return parent::redirectFromReferer($request, $response, $loopTerms, $clearParams);
af290059 55 }
0498b209
A
56 };
57 $this->assignedValues = [];
2899ebb5
A
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 });
0498b209
A
66 }
67
68 public function testAssignView(): void
69 {
70 $this->createValidContainerMockSet();
71
dd09ec52
A
72 $this->assignTemplateVars($this->assignedValues);
73
0498b209
A
74 $self = $this->controller->assignView('variableName', 'variableValue');
75
2899ebb5 76 static::assertInstanceOf(ShaarliVisitorController::class, $self);
0498b209
A
77 static::assertSame('variableValue', $this->assignedValues['variableName']);
78 }
79
80 public function testRender(): void
81 {
82 $this->createValidContainerMockSet();
83
dd09ec52
A
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
0498b209
A
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 }
af290059
A
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
2899ebb5 129 $result = $this->controller->redirectFromReferer($this->request, $response);
af290059
A
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
2899ebb5 146 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope']);
af290059
A
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
2899ebb5 163 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'controller']);
af290059
A
164
165 static::assertSame(302, $result->getStatusCode());
2899ebb5 166 static::assertSame(['/subfolder'], $result->getHeader('location'));
af290059
A
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
2899ebb5 180 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'other']);
af290059
A
181
182 static::assertSame(302, $result->getStatusCode());
2899ebb5 183 static::assertSame(['/subfolder'], $result->getHeader('location'));
af290059
A
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
2899ebb5 198 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'param']);
af290059
A
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
2899ebb5 216 $result = $this->controller->redirectFromReferer($this->request, $response, ['shaarli']);
af290059
A
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
2899ebb5 234 $result = $this->controller->redirectFromReferer($this->request, $response, ['query'], ['query']);
af290059
A
235
236 static::assertSame(302, $result->getStatusCode());
237 static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));
238 }
0498b209 239}