]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/visitor/ShaarliPublicControllerTest.php
Test ShaarliAdminController
[github/shaarli/Shaarli.git] / tests / front / controller / visitor / ShaarliPublicControllerTest.php
CommitLineData
0498b209
A
1<?php
2
3declare(strict_types=1);
4
2899ebb5 5namespace Shaarli\Front\Controller\Visitor;
0498b209
A
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkFilter;
2899ebb5 9use Slim\Http\Request;
af290059 10use Slim\Http\Response;
2899ebb5 11use Slim\Http\Uri;
0498b209
A
12
13/**
14 * Class ShaarliControllerTest
15 *
16 * This class is used to test default behavior of ShaarliController abstract class.
17 * It uses a dummy non abstract controller.
18 */
fdedbfd4 19class ShaarliPublicControllerTest extends TestCase
0498b209 20{
dd09ec52 21 use FrontControllerMockHelper;
0498b209
A
22
23 /** @var LoginController */
24 protected $controller;
25
26 /** @var mixed[] List of variable assigned to the template */
27 protected $assignedValues;
28
2899ebb5
A
29 /** @var Request */
30 protected $request;
31
0498b209
A
32 public function setUp(): void
33 {
dd09ec52
A
34 $this->createContainer();
35
2899ebb5 36 $this->controller = new class($this->container) extends ShaarliVisitorController
0498b209 37 {
2899ebb5 38 public function assignView(string $key, $value): ShaarliVisitorController
0498b209
A
39 {
40 return parent::assignView($key, $value);
41 }
42
43 public function render(string $template): string
44 {
45 return parent::render($template);
46 }
af290059
A
47
48 public function redirectFromReferer(
2899ebb5 49 Request $request,
af290059
A
50 Response $response,
51 array $loopTerms = [],
52 array $clearParams = []
53 ): Response {
2899ebb5 54 return parent::redirectFromReferer($request, $response, $loopTerms, $clearParams);
af290059 55 }
0498b209
A
56 };
57 $this->assignedValues = [];
2899ebb5
A
58
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');
63
64 return $uri;
65 });
0498b209
A
66 }
67
68 public function testAssignView(): void
69 {
dd09ec52
A
70 $this->assignTemplateVars($this->assignedValues);
71
0498b209
A
72 $self = $this->controller->assignView('variableName', 'variableValue');
73
2899ebb5 74 static::assertInstanceOf(ShaarliVisitorController::class, $self);
0498b209
A
75 static::assertSame('variableValue', $this->assignedValues['variableName']);
76 }
77
78 public function testRender(): void
79 {
dd09ec52
A
80 $this->assignTemplateVars($this->assignedValues);
81
82 $this->container->bookmarkService
83 ->method('count')
84 ->willReturnCallback(function (string $visibility): int {
85 return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
86 })
87 ;
88
89 $this->container->pluginManager
90 ->method('executeHooks')
91 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
92 return $data[$hook] = $params;
93 });
94 $this->container->pluginManager->method('getErrors')->willReturn(['error']);
95
96 $this->container->loginManager->method('isLoggedIn')->willReturn(true);
97
0498b209
A
98 $render = $this->controller->render('templateName');
99
100 static::assertSame('templateName', $render);
101
102 static::assertSame(10, $this->assignedValues['linkcount']);
103 static::assertSame(5, $this->assignedValues['privateLinkcount']);
104 static::assertSame(['error'], $this->assignedValues['plugin_errors']);
105
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']);
112 }
af290059
A
113
114 /**
115 * Test redirectFromReferer() - Default behaviour
116 */
117 public function testRedirectFromRefererDefault(): void
118 {
af290059
A
119 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
120
121 $response = new Response();
122
2899ebb5 123 $result = $this->controller->redirectFromReferer($this->request, $response);
af290059
A
124
125 static::assertSame(302, $result->getStatusCode());
126 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
127 }
128
129 /**
130 * Test redirectFromReferer() - With a loop term not matched in the referer
131 */
132 public function testRedirectFromRefererWithUnmatchedLoopTerm(): void
133 {
af290059
A
134 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
135
136 $response = new Response();
137
2899ebb5 138 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope']);
af290059
A
139
140 static::assertSame(302, $result->getStatusCode());
141 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
142 }
143
144 /**
145 * Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default
146 */
147 public function testRedirectFromRefererWithMatchingLoopTermInPath(): void
148 {
af290059
A
149 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
150
151 $response = new Response();
152
2899ebb5 153 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'controller']);
af290059
A
154
155 static::assertSame(302, $result->getStatusCode());
2899ebb5 156 static::assertSame(['/subfolder'], $result->getHeader('location'));
af290059
A
157 }
158
159 /**
160 * Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default
161 */
162 public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void
163 {
af290059
A
164 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
165
166 $response = new Response();
167
2899ebb5 168 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'other']);
af290059
A
169
170 static::assertSame(302, $result->getStatusCode());
2899ebb5 171 static::assertSame(['/subfolder'], $result->getHeader('location'));
af290059
A
172 }
173
174 /**
175 * Test redirectFromReferer() - With a loop term matching the referer in its query value
176 * -> we do not block redirection for query parameter values.
177 */
178 public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void
179 {
af290059
A
180 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
181
182 $response = new Response();
183
2899ebb5 184 $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'param']);
af290059
A
185
186 static::assertSame(302, $result->getStatusCode());
187 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
188 }
189
190 /**
191 * Test redirectFromReferer() - With a loop term matching the referer in its domain name
192 * -> we do not block redirection for shaarli's hosts
193 */
194 public function testRedirectFromRefererWithLoopTermInDomain(): void
195 {
af290059
A
196 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
197
198 $response = new Response();
199
2899ebb5 200 $result = $this->controller->redirectFromReferer($this->request, $response, ['shaarli']);
af290059
A
201
202 static::assertSame(302, $result->getStatusCode());
203 static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
204 }
205
206 /**
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
209 */
210 public function testRedirectFromRefererWithMatchingClearedParam(): void
211 {
af290059
A
212 $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2';
213
214 $response = new Response();
215
2899ebb5 216 $result = $this->controller->redirectFromReferer($this->request, $response, ['query'], ['query']);
af290059
A
217
218 static::assertSame(302, $result->getStatusCode());
219 static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));
220 }
0498b209 221}