3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\BookmarkFilter
;
9 use Slim\Http\Response
;
12 * Class ShaarliControllerTest
14 * This class is used to test default behavior of ShaarliController abstract class.
15 * It uses a dummy non abstract controller.
17 class ShaarliControllerTest
extends TestCase
19 use FrontControllerMockHelper
;
21 /** @var LoginController */
22 protected $controller;
24 /** @var mixed[] List of variable assigned to the template */
25 protected $assignedValues;
27 public function setUp(): void
29 $this->createContainer();
31 $this->controller
= new class($this->container
) extends ShaarliController
33 public function assignView(string $key, $value): ShaarliController
35 return parent
::assignView($key, $value);
38 public function render(string $template): string
40 return parent
::render($template);
43 public function redirectFromReferer(
45 array $loopTerms = [],
46 array $clearParams = []
48 return parent
::redirectFromReferer($response, $loopTerms, $clearParams);
51 $this->assignedValues
= [];
54 public function testAssignView(): void
56 $this->createValidContainerMockSet();
58 $this->assignTemplateVars($this->assignedValues
);
60 $self = $this->controller
->assignView('variableName', 'variableValue');
62 static::assertInstanceOf(ShaarliController
::class, $self);
63 static::assertSame('variableValue', $this->assignedValues
['variableName']);
66 public function testRender(): void
68 $this->createValidContainerMockSet();
70 $this->assignTemplateVars($this->assignedValues
);
72 $this->container
->bookmarkService
74 ->willReturnCallback(function (string $visibility): int {
75 return $visibility === BookmarkFilter
::$PRIVATE ? 5 : 10;
79 $this->container
->pluginManager
80 ->method('executeHooks')
81 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
82 return $data[$hook] = $params;
84 $this->container
->pluginManager
->method('getErrors')->willReturn(['error']);
86 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
88 $render = $this->controller
->render('templateName');
90 static::assertSame('templateName', $render);
92 static::assertSame(10, $this->assignedValues
['linkcount']);
93 static::assertSame(5, $this->assignedValues
['privateLinkcount']);
94 static::assertSame(['error'], $this->assignedValues
['plugin_errors']);
96 static::assertSame('templateName', $this->assignedValues
['plugins_includes']['render_includes']['target']);
97 static::assertTrue($this->assignedValues
['plugins_includes']['render_includes']['loggedin']);
98 static::assertSame('templateName', $this->assignedValues
['plugins_header']['render_header']['target']);
99 static::assertTrue($this->assignedValues
['plugins_header']['render_header']['loggedin']);
100 static::assertSame('templateName', $this->assignedValues
['plugins_footer']['render_footer']['target']);
101 static::assertTrue($this->assignedValues
['plugins_footer']['render_footer']['loggedin']);
105 * Test redirectFromReferer() - Default behaviour
107 public function testRedirectFromRefererDefault(): void
109 $this->createValidContainerMockSet();
111 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
113 $response = new Response();
115 $result = $this->controller
->redirectFromReferer($response);
117 static::assertSame(302, $result->getStatusCode());
118 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
122 * Test redirectFromReferer() - With a loop term not matched in the referer
124 public function testRedirectFromRefererWithUnmatchedLoopTerm(): void
126 $this->createValidContainerMockSet();
128 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
130 $response = new Response();
132 $result = $this->controller
->redirectFromReferer($response, ['nope']);
134 static::assertSame(302, $result->getStatusCode());
135 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
139 * Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default
141 public function testRedirectFromRefererWithMatchingLoopTermInPath(): void
143 $this->createValidContainerMockSet();
145 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
147 $response = new Response();
149 $result = $this->controller
->redirectFromReferer($response, ['nope', 'controller']);
151 static::assertSame(302, $result->getStatusCode());
152 static::assertSame(['./'], $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->createValidContainerMockSet();
162 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
164 $response = new Response();
166 $result = $this->controller
->redirectFromReferer($response, ['nope', 'other']);
168 static::assertSame(302, $result->getStatusCode());
169 static::assertSame(['./'], $result->getHeader('location'));
173 * Test redirectFromReferer() - With a loop term matching the referer in its query value
174 * -> we do not block redirection for query parameter values.
176 public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void
178 $this->createValidContainerMockSet();
180 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
182 $response = new Response();
184 $result = $this->controller
->redirectFromReferer($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->createValidContainerMockSet();
198 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
200 $response = new Response();
202 $result = $this->controller
->redirectFromReferer($response, ['shaarli']);
204 static::assertSame(302, $result->getStatusCode());
205 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
209 * Test redirectFromReferer() - With a loop term matching a query parameter AND clear this query param
210 * -> the param should be cleared before checking if it matches the redir loop terms
212 public function testRedirectFromRefererWithMatchingClearedParam(): void
214 $this->createValidContainerMockSet();
216 $this->container
->environment
['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
218 $response = new Response();
220 $result = $this->controller
->redirectFromReferer($response, ['query'], ['query']);
222 static::assertSame(302, $result->getStatusCode());
223 static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));