diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-22 13:20:31 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 2899ebb5b5e82890c877151f5c02045266ac9973 (patch) | |
tree | 0c4e2684c7f6d161f92a21181bfa4b2f78d6a82f /tests/front/controller/ShaarliControllerTest.php | |
parent | af290059d10319e76d1e7d78b592cab99c26d91a (diff) | |
download | Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.gz Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.zst Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.zip |
Initialize admin Slim controllers
- Reorganize visitor controllers
- Fix redirection with Slim's requests base path
- Fix daily links
Diffstat (limited to 'tests/front/controller/ShaarliControllerTest.php')
-rw-r--r-- | tests/front/controller/ShaarliControllerTest.php | 225 |
1 files changed, 0 insertions, 225 deletions
diff --git a/tests/front/controller/ShaarliControllerTest.php b/tests/front/controller/ShaarliControllerTest.php deleted file mode 100644 index a6011b49..00000000 --- a/tests/front/controller/ShaarliControllerTest.php +++ /dev/null | |||
@@ -1,225 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Shaarli\Bookmark\BookmarkFilter; | ||
9 | use Slim\Http\Response; | ||
10 | |||
11 | /** | ||
12 | * Class ShaarliControllerTest | ||
13 | * | ||
14 | * This class is used to test default behavior of ShaarliController abstract class. | ||
15 | * It uses a dummy non abstract controller. | ||
16 | */ | ||
17 | class ShaarliControllerTest extends TestCase | ||
18 | { | ||
19 | use FrontControllerMockHelper; | ||
20 | |||
21 | /** @var LoginController */ | ||
22 | protected $controller; | ||
23 | |||
24 | /** @var mixed[] List of variable assigned to the template */ | ||
25 | protected $assignedValues; | ||
26 | |||
27 | public function setUp(): void | ||
28 | { | ||
29 | $this->createContainer(); | ||
30 | |||
31 | $this->controller = new class($this->container) extends ShaarliController | ||
32 | { | ||
33 | public function assignView(string $key, $value): ShaarliController | ||
34 | { | ||
35 | return parent::assignView($key, $value); | ||
36 | } | ||
37 | |||
38 | public function render(string $template): string | ||
39 | { | ||
40 | return parent::render($template); | ||
41 | } | ||
42 | |||
43 | public function redirectFromReferer( | ||
44 | Response $response, | ||
45 | array $loopTerms = [], | ||
46 | array $clearParams = [] | ||
47 | ): Response { | ||
48 | return parent::redirectFromReferer($response, $loopTerms, $clearParams); | ||
49 | } | ||
50 | }; | ||
51 | $this->assignedValues = []; | ||
52 | } | ||
53 | |||
54 | public function testAssignView(): void | ||
55 | { | ||
56 | $this->createValidContainerMockSet(); | ||
57 | |||
58 | $this->assignTemplateVars($this->assignedValues); | ||
59 | |||
60 | $self = $this->controller->assignView('variableName', 'variableValue'); | ||
61 | |||
62 | static::assertInstanceOf(ShaarliController::class, $self); | ||
63 | static::assertSame('variableValue', $this->assignedValues['variableName']); | ||
64 | } | ||
65 | |||
66 | public function testRender(): void | ||
67 | { | ||
68 | $this->createValidContainerMockSet(); | ||
69 | |||
70 | $this->assignTemplateVars($this->assignedValues); | ||
71 | |||
72 | $this->container->bookmarkService | ||
73 | ->method('count') | ||
74 | ->willReturnCallback(function (string $visibility): int { | ||
75 | return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10; | ||
76 | }) | ||
77 | ; | ||
78 | |||
79 | $this->container->pluginManager | ||
80 | ->method('executeHooks') | ||
81 | ->willReturnCallback(function (string $hook, array &$data, array $params): array { | ||
82 | return $data[$hook] = $params; | ||
83 | }); | ||
84 | $this->container->pluginManager->method('getErrors')->willReturn(['error']); | ||
85 | |||
86 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | ||
87 | |||
88 | $render = $this->controller->render('templateName'); | ||
89 | |||
90 | static::assertSame('templateName', $render); | ||
91 | |||
92 | static::assertSame(10, $this->assignedValues['linkcount']); | ||
93 | static::assertSame(5, $this->assignedValues['privateLinkcount']); | ||
94 | static::assertSame(['error'], $this->assignedValues['plugin_errors']); | ||
95 | |||
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']); | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * Test redirectFromReferer() - Default behaviour | ||
106 | */ | ||
107 | public function testRedirectFromRefererDefault(): void | ||
108 | { | ||
109 | $this->createValidContainerMockSet(); | ||
110 | |||
111 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
112 | |||
113 | $response = new Response(); | ||
114 | |||
115 | $result = $this->controller->redirectFromReferer($response); | ||
116 | |||
117 | static::assertSame(302, $result->getStatusCode()); | ||
118 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * Test redirectFromReferer() - With a loop term not matched in the referer | ||
123 | */ | ||
124 | public function testRedirectFromRefererWithUnmatchedLoopTerm(): void | ||
125 | { | ||
126 | $this->createValidContainerMockSet(); | ||
127 | |||
128 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
129 | |||
130 | $response = new Response(); | ||
131 | |||
132 | $result = $this->controller->redirectFromReferer($response, ['nope']); | ||
133 | |||
134 | static::assertSame(302, $result->getStatusCode()); | ||
135 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default | ||
140 | */ | ||
141 | public function testRedirectFromRefererWithMatchingLoopTermInPath(): void | ||
142 | { | ||
143 | $this->createValidContainerMockSet(); | ||
144 | |||
145 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
146 | |||
147 | $response = new Response(); | ||
148 | |||
149 | $result = $this->controller->redirectFromReferer($response, ['nope', 'controller']); | ||
150 | |||
151 | static::assertSame(302, $result->getStatusCode()); | ||
152 | static::assertSame(['./'], $result->getHeader('location')); | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default | ||
157 | */ | ||
158 | public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void | ||
159 | { | ||
160 | $this->createValidContainerMockSet(); | ||
161 | |||
162 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
163 | |||
164 | $response = new Response(); | ||
165 | |||
166 | $result = $this->controller->redirectFromReferer($response, ['nope', 'other']); | ||
167 | |||
168 | static::assertSame(302, $result->getStatusCode()); | ||
169 | static::assertSame(['./'], $result->getHeader('location')); | ||
170 | } | ||
171 | |||
172 | /** | ||
173 | * Test redirectFromReferer() - With a loop term matching the referer in its query value | ||
174 | * -> we do not block redirection for query parameter values. | ||
175 | */ | ||
176 | public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void | ||
177 | { | ||
178 | $this->createValidContainerMockSet(); | ||
179 | |||
180 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
181 | |||
182 | $response = new Response(); | ||
183 | |||
184 | $result = $this->controller->redirectFromReferer($response, ['nope', 'param']); | ||
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 | { | ||
196 | $this->createValidContainerMockSet(); | ||
197 | |||
198 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
199 | |||
200 | $response = new Response(); | ||
201 | |||
202 | $result = $this->controller->redirectFromReferer($response, ['shaarli']); | ||
203 | |||
204 | static::assertSame(302, $result->getStatusCode()); | ||
205 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | ||
206 | } | ||
207 | |||
208 | /** | ||
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 | ||
211 | */ | ||
212 | public function testRedirectFromRefererWithMatchingClearedParam(): void | ||
213 | { | ||
214 | $this->createValidContainerMockSet(); | ||
215 | |||
216 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
217 | |||
218 | $response = new Response(); | ||
219 | |||
220 | $result = $this->controller->redirectFromReferer($response, ['query'], ['query']); | ||
221 | |||
222 | static::assertSame(302, $result->getStatusCode()); | ||
223 | static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location')); | ||
224 | } | ||
225 | } | ||