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->assignTemplateVars($this->assignedValues
);
72 $self = $this->controller
->assignView('variableName', 'variableValue');
74 static::assertInstanceOf(ShaarliVisitorController
::class, $self);
75 static::assertSame('variableValue', $this->assignedValues
['variableName']);
78 public function testRender(): void
80 $this->assignTemplateVars($this->assignedValues
);
82 $this->container
->bookmarkService
84 ->willReturnCallback(function (string $visibility): int {
85 return $visibility === BookmarkFilter
::$PRIVATE ? 5 : 10;
89 $this->container
->pluginManager
90 ->method('executeHooks')
91 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
92 return $data[$hook] = $params;
94 $this->container
->pluginManager
->method('getErrors')->willReturn(['error']);
96 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
98 $render = $this->controller
->render('templateName');
100 static::assertSame('templateName', $render);
102 static::assertSame(10, $this->assignedValues
['linkcount']);
103 static::assertSame(5, $this->assignedValues
['privateLinkcount']);
104 static::assertSame(['error'], $this->assignedValues
['plugin_errors']);
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']);
115 * Test redirectFromReferer() - Default behaviour
117 public function testRedirectFromRefererDefault(): void
119 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
121 $response = new Response();
123 $result = $this->controller
->redirectFromReferer($this->request
, $response);
125 static::assertSame(302, $result->getStatusCode());
126 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
130 * Test redirectFromReferer() - With a loop term not matched in the referer
132 public function testRedirectFromRefererWithUnmatchedLoopTerm(): void
134 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
136 $response = new Response();
138 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope']);
140 static::assertSame(302, $result->getStatusCode());
141 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
145 * Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default
147 public function testRedirectFromRefererWithMatchingLoopTermInPath(): void
149 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
151 $response = new Response();
153 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'controller']);
155 static::assertSame(302, $result->getStatusCode());
156 static::assertSame(['/subfolder'], $result->getHeader('location'));
160 * Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default
162 public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void
164 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
166 $response = new Response();
168 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'other']);
170 static::assertSame(302, $result->getStatusCode());
171 static::assertSame(['/subfolder'], $result->getHeader('location'));
175 * Test redirectFromReferer() - With a loop term matching the referer in its query value
176 * -> we do not block redirection for query parameter values.
178 public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void
180 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
182 $response = new Response();
184 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['nope', 'param']);
186 static::assertSame(302, $result->getStatusCode());
187 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
191 * Test redirectFromReferer() - With a loop term matching the referer in its domain name
192 * -> we do not block redirection for shaarli's hosts
194 public function testRedirectFromRefererWithLoopTermInDomain(): void
196 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
198 $response = new Response();
200 $result = $this->controller
->redirectFromReferer($this->request
, $response, ['shaarli']);
202 static::assertSame(302, $result->getStatusCode());
203 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
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
210 public function testRedirectFromRefererWithMatchingClearedParam(): void
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, ['query'], ['query']);
218 static::assertSame(302, $result->getStatusCode());
219 static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));