3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use Shaarli\Bookmark\BookmarkFilter
;
10 use Slim\Http\Response
;
13 * Class ShaarliControllerTest
15 * This class is used to test default behavior of ShaarliVisitorController abstract class.
16 * It uses a dummy non abstract controller.
18 class ShaarliVisitorControllerTest
extends TestCase
20 use FrontControllerMockHelper
;
22 /** @var LoginController */
23 protected $controller;
25 /** @var mixed[] List of variable assigned to the template */
26 protected $assignedValues;
31 public function setUp(): void
33 $this->createContainer();
35 $this->controller
= new class($this->container
) extends ShaarliVisitorController
37 public function assignView(string $key, $value): ShaarliVisitorController
39 return parent
::assignView($key, $value);
42 public function render(string $template): string
44 return parent
::render($template);
47 public function redirectFromReferer(
50 array $loopTerms = [],
51 array $clearParams = [],
54 return parent
::redirectFromReferer($request, $response, $loopTerms, $clearParams, $anchor);
57 $this->assignedValues
= [];
59 $this->request
= $this->createMock(Request
::class);
62 public function testAssignView(): void
64 $this->assignTemplateVars($this->assignedValues
);
66 $self = $this->controller
->assignView('variableName', 'variableValue');
68 static::assertInstanceOf(ShaarliVisitorController
::class, $self);
69 static::assertSame('variableValue', $this->assignedValues
['variableName']);
72 public function testRender(): void
74 $this->assignTemplateVars($this->assignedValues
);
76 $this->container
->bookmarkService
78 ->willReturnCallback(function (string $visibility): int {
79 return $visibility === BookmarkFilter
::$PRIVATE ? 5 : 10;
83 $this->container
->pluginManager
84 ->method('executeHooks')
85 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
86 return $data[$hook] = $params;
88 $this->container
->pluginManager
->method('getErrors')->willReturn(['error']);
90 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
92 $render = $this->controller
->render('templateName');
94 static::assertSame('templateName', $render);
96 static::assertSame('templateName', $this->assignedValues
['_PAGE_']);
97 static::assertSame('templateName', $this->assignedValues
['template']);
99 static::assertSame(10, $this->assignedValues
['linkcount']);
100 static::assertSame(5, $this->assignedValues
['privateLinkcount']);
101 static::assertSame(['error'], $this->assignedValues
['plugin_errors']);
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']);
112 * Test redirectFromReferer() - Default behaviour
114 public function testRedirectFromRefererDefault(): void
116 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
118 $response = new Response();
120 $result = $this->controller
->redirectFromReferer($this->request
, $response);
122 static::assertSame(302, $result->getStatusCode());
123 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
127 * Test redirectFromReferer() - With a loop term not matched in the referer
129 public function testRedirectFromRefererWithUnmatchedLoopTerm(): void
131 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
133 $response = new Response();
135 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope']);
137 static::assertSame(302, $result->getStatusCode());
138 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
142 * Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default
144 public function testRedirectFromRefererWithMatchingLoopTermInPath(): void
146 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
148 $response = new Response();
150 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'controller']);
152 static::assertSame(302, $result->getStatusCode());
153 static::assertSame(['/subfolder/'], $result->getHeader('location'));
157 * Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default
159 public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void
161 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
163 $response = new Response();
165 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'other']);
167 static::assertSame(302, $result->getStatusCode());
168 static::assertSame(['/subfolder/'], $result->getHeader('location'));
172 * Test redirectFromReferer() - With a loop term matching the referer in its query value
173 * -> we do not block redirection for query parameter values.
175 public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void
177 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
179 $response = new Response();
181 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'param']);
183 static::assertSame(302, $result->getStatusCode());
184 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
188 * Test redirectFromReferer() - With a loop term matching the referer in its domain name
189 * -> we do not block redirection for shaarli's hosts
191 public function testRedirectFromRefererWithLoopTermInDomain(): void
193 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
195 $response = new Response();
197 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['shaarli']);
199 static::assertSame(302, $result->getStatusCode());
200 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
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
207 public function testRedirectFromRefererWithMatchingClearedParam(): void
209 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
211 $response = new Response();
213 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['query'], ['query']);
215 static::assertSame(302, $result->getStatusCode());
216 static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));
220 * Test redirectFromReferer() - From another domain -> we ignore the given referrer.
222 public function testRedirectExternalReferer(): void
224 $this->container
->environment
['HTTP_REFERER'] = 'http://other.domain.tld/controller?query=param&other=2';
226 $response = new Response();
228 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['query'], ['query']);
230 static::assertSame(302, $result->getStatusCode());
231 static::assertSame(['/subfolder/'], $result->getHeader('location'));
235 * Test redirectFromReferer() - From another domain -> we ignore the given referrer.
237 public function testRedirectExternalRefererExplicitDomainName(): void
239 $this->container
->environment
['SERVER_NAME'] = 'my.shaarli.tld';
240 $this->container
->environment
['HTTP_REFERER'] = 'http://your.shaarli.tld/controller?query=param&other=2';
242 $response = new Response();
244 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['query'], ['query']);
246 static::assertSame(302, $result->getStatusCode());
247 static::assertSame(['/subfolder/'], $result->getHeader('location'));