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
;
14 * Class ShaarliControllerTest
16 * This class is used to test default behavior of ShaarliController abstract class.
17 * It uses a dummy non abstract controller.
19 class ShaarliControllerTest
extends TestCase
21 use FrontControllerMockHelper
;
23 /** @var LoginController */
24 protected $controller;
26 /** @var mixed[] List of variable assigned to the template */
27 protected $assignedValues;
32 public function setUp(): void
34 $this->createContainer();
36 $this->controller
= new class($this->container
) extends ShaarliVisitorController
38 public function assignView(string $key, $value): ShaarliVisitorController
40 return parent
::assignView($key, $value);
43 public function render(string $template): string
45 return parent
::render($template);
48 public function redirectFromReferer(
51 array $loopTerms = [],
52 array $clearParams = []
54 return parent
::redirectFromReferer($request, $response, $loopTerms, $clearParams);
57 $this->assignedValues
= [];
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');
68 public function testAssignView(): void
70 $this->createValidContainerMockSet();
72 $this->assignTemplateVars($this->assignedValues
);
74 $self = $this->controller
->assignView('variableName', 'variableValue');
76 static::assertInstanceOf(ShaarliVisitorController
::class, $self);
77 static::assertSame('variableValue', $this->assignedValues
['variableName']);
80 public function testRender(): void
82 $this->createValidContainerMockSet();
84 $this->assignTemplateVars($this->assignedValues
);
86 $this->container
->bookmarkService
88 ->willReturnCallback(function (string $visibility): int {
89 return $visibility === BookmarkFilter
::$PRIVATE ? 5 : 10;
93 $this->container
->pluginManager
94 ->method('executeHooks')
95 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
96 return $data[$hook] = $params;
98 $this->container
->pluginManager
->method('getErrors')->willReturn(['error']);
100 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
102 $render = $this->controller
->render('templateName');
104 static::assertSame('templateName', $render);
106 static::assertSame(10, $this->assignedValues
['linkcount']);
107 static::assertSame(5, $this->assignedValues
['privateLinkcount']);
108 static::assertSame(['error'], $this->assignedValues
['plugin_errors']);
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']);
119 * Test redirectFromReferer() - Default behaviour
121 public function testRedirectFromRefererDefault(): void
123 $this->createValidContainerMockSet();
125 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
127 $response = new Response();
129 $result = $this->controller
->redirectFromReferer($this->request
, $response);
131 static::assertSame(302, $result->getStatusCode());
132 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
136 * Test redirectFromReferer() - With a loop term not matched in the referer
138 public function testRedirectFromRefererWithUnmatchedLoopTerm(): void
140 $this->createValidContainerMockSet();
142 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
144 $response = new Response();
146 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope']);
148 static::assertSame(302, $result->getStatusCode());
149 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
153 * Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default
155 public function testRedirectFromRefererWithMatchingLoopTermInPath(): void
157 $this->createValidContainerMockSet();
159 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
161 $response = new Response();
163 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'controller']);
165 static::assertSame(302, $result->getStatusCode());
166 static::assertSame(['/subfolder'], $result->getHeader('location'));
170 * Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default
172 public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void
174 $this->createValidContainerMockSet();
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', 'other']);
182 static::assertSame(302, $result->getStatusCode());
183 static::assertSame(['/subfolder'], $result->getHeader('location'));
187 * Test redirectFromReferer() - With a loop term matching the referer in its query value
188 * -> we do not block redirection for query parameter values.
190 public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void
192 $this->createValidContainerMockSet();
194 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
196 $response = new Response();
198 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'param']);
200 static::assertSame(302, $result->getStatusCode());
201 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
205 * Test redirectFromReferer() - With a loop term matching the referer in its domain name
206 * -> we do not block redirection for shaarli's hosts
208 public function testRedirectFromRefererWithLoopTermInDomain(): void
210 $this->createValidContainerMockSet();
212 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
214 $response = new Response();
216 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['shaarli']);
218 static::assertSame(302, $result->getStatusCode());
219 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
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
226 public function testRedirectFromRefererWithMatchingClearedParam(): void
228 $this->createValidContainerMockSet();
230 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
232 $response = new Response();
234 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['query'], ['query']);
236 static::assertSame(302, $result->getStatusCode());
237 static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));