3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use PHPUnit\Framework\TestCase
;
8 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(10, $this->assignedValues
['linkcount']);
97 static::assertSame(5, $this->assignedValues
['privateLinkcount']);
98 static::assertSame(['error'], $this->assignedValues
['plugin_errors']);
99 static::assertSame('/subfolder', $this->assignedValues
['base_path']);
100 static::assertSame('/subfolder/tpl/default', $this->assignedValues
['asset_path']);
102 static::assertSame('templateName', $this->assignedValues
['plugins_includes']['render_includes']['target']);
103 static::assertTrue($this->assignedValues
['plugins_includes']['render_includes']['loggedin']);
104 static::assertSame('templateName', $this->assignedValues
['plugins_header']['render_header']['target']);
105 static::assertTrue($this->assignedValues
['plugins_header']['render_header']['loggedin']);
106 static::assertSame('templateName', $this->assignedValues
['plugins_footer']['render_footer']['target']);
107 static::assertTrue($this->assignedValues
['plugins_footer']['render_footer']['loggedin']);
111 * Test redirectFromReferer() - Default behaviour
113 public function testRedirectFromRefererDefault(): void
115 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
117 $response = new Response();
119 $result = $this->controller
->redirectFromReferer($this->request
, $response);
121 static::assertSame(302, $result->getStatusCode());
122 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
126 * Test redirectFromReferer() - With a loop term not matched in the referer
128 public function testRedirectFromRefererWithUnmatchedLoopTerm(): void
130 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
132 $response = new Response();
134 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope']);
136 static::assertSame(302, $result->getStatusCode());
137 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
141 * Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default
143 public function testRedirectFromRefererWithMatchingLoopTermInPath(): void
145 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
147 $response = new Response();
149 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'controller']);
151 static::assertSame(302, $result->getStatusCode());
152 static::assertSame(['/subfolder/'], $result->getHeader('location'));
156 * Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default
158 public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void
160 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
162 $response = new Response();
164 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'other']);
166 static::assertSame(302, $result->getStatusCode());
167 static::assertSame(['/subfolder/'], $result->getHeader('location'));
171 * Test redirectFromReferer() - With a loop term matching the referer in its query value
172 * -> we do not block redirection for query parameter values.
174 public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void
176 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
178 $response = new Response();
180 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'param']);
182 static::assertSame(302, $result->getStatusCode());
183 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
187 * Test redirectFromReferer() - With a loop term matching the referer in its domain name
188 * -> we do not block redirection for shaarli's hosts
190 public function testRedirectFromRefererWithLoopTermInDomain(): void
192 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
194 $response = new Response();
196 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['shaarli']);
198 static::assertSame(302, $result->getStatusCode());
199 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
203 * Test redirectFromReferer() - With a loop term matching a query parameter AND clear this query param
204 * -> the param should be cleared before checking if it matches the redir loop terms
206 public function testRedirectFromRefererWithMatchingClearedParam(): void
208 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
210 $response = new Response();
212 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['query'], ['query']);
214 static::assertSame(302, $result->getStatusCode());
215 static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));