]>
Commit | Line | Data |
---|---|---|
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 ShaarliPublicControllerTest 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->assignTemplateVars($this->assignedValues); | |
71 | ||
72 | $self = $this->controller->assignView('variableName', 'variableValue'); | |
73 | ||
74 | static::assertInstanceOf(ShaarliVisitorController::class, $self); | |
75 | static::assertSame('variableValue', $this->assignedValues['variableName']); | |
76 | } | |
77 | ||
78 | public function testRender(): void | |
79 | { | |
80 | $this->assignTemplateVars($this->assignedValues); | |
81 | ||
82 | $this->container->bookmarkService | |
83 | ->method('count') | |
84 | ->willReturnCallback(function (string $visibility): int { | |
85 | return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10; | |
86 | }) | |
87 | ; | |
88 | ||
89 | $this->container->pluginManager | |
90 | ->method('executeHooks') | |
91 | ->willReturnCallback(function (string $hook, array &$data, array $params): array { | |
92 | return $data[$hook] = $params; | |
93 | }); | |
94 | $this->container->pluginManager->method('getErrors')->willReturn(['error']); | |
95 | ||
96 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | |
97 | ||
98 | $render = $this->controller->render('templateName'); | |
99 | ||
100 | static::assertSame('templateName', $render); | |
101 | ||
102 | static::assertSame(10, $this->assignedValues['linkcount']); | |
103 | static::assertSame(5, $this->assignedValues['privateLinkcount']); | |
104 | static::assertSame(['error'], $this->assignedValues['plugin_errors']); | |
105 | ||
106 | static::assertSame('templateName', $this->assignedValues['plugins_includes']['render_includes']['target']); | |
107 | static::assertTrue($this->assignedValues['plugins_includes']['render_includes']['loggedin']); | |
108 | static::assertSame('templateName', $this->assignedValues['plugins_header']['render_header']['target']); | |
109 | static::assertTrue($this->assignedValues['plugins_header']['render_header']['loggedin']); | |
110 | static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']); | |
111 | static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Test redirectFromReferer() - Default behaviour | |
116 | */ | |
117 | public function testRedirectFromRefererDefault(): void | |
118 | { | |
119 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | |
120 | ||
121 | $response = new Response(); | |
122 | ||
123 | $result = $this->controller->redirectFromReferer($this->request, $response); | |
124 | ||
125 | static::assertSame(302, $result->getStatusCode()); | |
126 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Test redirectFromReferer() - With a loop term not matched in the referer | |
131 | */ | |
132 | public function testRedirectFromRefererWithUnmatchedLoopTerm(): void | |
133 | { | |
134 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | |
135 | ||
136 | $response = new Response(); | |
137 | ||
138 | $result = $this->controller->redirectFromReferer($this->request, $response, ['nope']); | |
139 | ||
140 | static::assertSame(302, $result->getStatusCode()); | |
141 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default | |
146 | */ | |
147 | public function testRedirectFromRefererWithMatchingLoopTermInPath(): void | |
148 | { | |
149 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | |
150 | ||
151 | $response = new Response(); | |
152 | ||
153 | $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'controller']); | |
154 | ||
155 | static::assertSame(302, $result->getStatusCode()); | |
156 | static::assertSame(['/subfolder'], $result->getHeader('location')); | |
157 | } | |
158 | ||
159 | /** | |
160 | * Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default | |
161 | */ | |
162 | public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void | |
163 | { | |
164 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | |
165 | ||
166 | $response = new Response(); | |
167 | ||
168 | $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'other']); | |
169 | ||
170 | static::assertSame(302, $result->getStatusCode()); | |
171 | static::assertSame(['/subfolder'], $result->getHeader('location')); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Test redirectFromReferer() - With a loop term matching the referer in its query value | |
176 | * -> we do not block redirection for query parameter values. | |
177 | */ | |
178 | public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void | |
179 | { | |
180 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | |
181 | ||
182 | $response = new Response(); | |
183 | ||
184 | $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'param']); | |
185 | ||
186 | static::assertSame(302, $result->getStatusCode()); | |
187 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | |
188 | } | |
189 | ||
190 | /** | |
191 | * Test redirectFromReferer() - With a loop term matching the referer in its domain name | |
192 | * -> we do not block redirection for shaarli's hosts | |
193 | */ | |
194 | public function testRedirectFromRefererWithLoopTermInDomain(): void | |
195 | { | |
196 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | |
197 | ||
198 | $response = new Response(); | |
199 | ||
200 | $result = $this->controller->redirectFromReferer($this->request, $response, ['shaarli']); | |
201 | ||
202 | static::assertSame(302, $result->getStatusCode()); | |
203 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | |
204 | } | |
205 | ||
206 | /** | |
207 | * Test redirectFromReferer() - With a loop term matching a query parameter AND clear this query param | |
208 | * -> the param should be cleared before checking if it matches the redir loop terms | |
209 | */ | |
210 | public function testRedirectFromRefererWithMatchingClearedParam(): void | |
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, ['query'], ['query']); | |
217 | ||
218 | static::assertSame(302, $result->getStatusCode()); | |
219 | static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location')); | |
220 | } | |
221 | } |