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