diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-08-27 10:27:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-27 10:27:34 +0200 |
commit | af41d5ab5d2bd3ba64d052c997bc6afa6966a63c (patch) | |
tree | 8fad2829c55f94022e359fa8914e11f80a2afc2a /tests/front/controller/visitor/ShaarliVisitorControllerTest.php | |
parent | b8e3630f2ecd142d397b1b062a346a667bb78595 (diff) | |
parent | 0c6fdbe12bbbb336348666b14b82096f24d5858b (diff) | |
download | Shaarli-af41d5ab5d2bd3ba64d052c997bc6afa6966a63c.tar.gz Shaarli-af41d5ab5d2bd3ba64d052c997bc6afa6966a63c.tar.zst Shaarli-af41d5ab5d2bd3ba64d052c997bc6afa6966a63c.zip |
Merge pull request #1511 from ArthurHoaro/wip-slim-routing
Diffstat (limited to 'tests/front/controller/visitor/ShaarliVisitorControllerTest.php')
-rw-r--r-- | tests/front/controller/visitor/ShaarliVisitorControllerTest.php | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/tests/front/controller/visitor/ShaarliVisitorControllerTest.php b/tests/front/controller/visitor/ShaarliVisitorControllerTest.php new file mode 100644 index 00000000..316ce49c --- /dev/null +++ b/tests/front/controller/visitor/ShaarliVisitorControllerTest.php | |||
@@ -0,0 +1,215 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Shaarli\Bookmark\BookmarkFilter; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | /** | ||
13 | * Class ShaarliControllerTest | ||
14 | * | ||
15 | * This class is used to test default behavior of ShaarliVisitorController abstract class. | ||
16 | * It uses a dummy non abstract controller. | ||
17 | */ | ||
18 | class ShaarliVisitorControllerTest extends TestCase | ||
19 | { | ||
20 | use FrontControllerMockHelper; | ||
21 | |||
22 | /** @var LoginController */ | ||
23 | protected $controller; | ||
24 | |||
25 | /** @var mixed[] List of variable assigned to the template */ | ||
26 | protected $assignedValues; | ||
27 | |||
28 | /** @var Request */ | ||
29 | protected $request; | ||
30 | |||
31 | public function setUp(): void | ||
32 | { | ||
33 | $this->createContainer(); | ||
34 | |||
35 | $this->controller = new class($this->container) extends ShaarliVisitorController | ||
36 | { | ||
37 | public function assignView(string $key, $value): ShaarliVisitorController | ||
38 | { | ||
39 | return parent::assignView($key, $value); | ||
40 | } | ||
41 | |||
42 | public function render(string $template): string | ||
43 | { | ||
44 | return parent::render($template); | ||
45 | } | ||
46 | |||
47 | public function redirectFromReferer( | ||
48 | Request $request, | ||
49 | Response $response, | ||
50 | array $loopTerms = [], | ||
51 | array $clearParams = [], | ||
52 | string $anchor = null | ||
53 | ): Response { | ||
54 | return parent::redirectFromReferer($request, $response, $loopTerms, $clearParams, $anchor); | ||
55 | } | ||
56 | }; | ||
57 | $this->assignedValues = []; | ||
58 | |||
59 | $this->request = $this->createMock(Request::class); | ||
60 | } | ||
61 | |||
62 | public function testAssignView(): void | ||
63 | { | ||
64 | $this->assignTemplateVars($this->assignedValues); | ||
65 | |||
66 | $self = $this->controller->assignView('variableName', 'variableValue'); | ||
67 | |||
68 | static::assertInstanceOf(ShaarliVisitorController::class, $self); | ||
69 | static::assertSame('variableValue', $this->assignedValues['variableName']); | ||
70 | } | ||
71 | |||
72 | public function testRender(): void | ||
73 | { | ||
74 | $this->assignTemplateVars($this->assignedValues); | ||
75 | |||
76 | $this->container->bookmarkService | ||
77 | ->method('count') | ||
78 | ->willReturnCallback(function (string $visibility): int { | ||
79 | return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10; | ||
80 | }) | ||
81 | ; | ||
82 | |||
83 | $this->container->pluginManager | ||
84 | ->method('executeHooks') | ||
85 | ->willReturnCallback(function (string $hook, array &$data, array $params): array { | ||
86 | return $data[$hook] = $params; | ||
87 | }); | ||
88 | $this->container->pluginManager->method('getErrors')->willReturn(['error']); | ||
89 | |||
90 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | ||
91 | |||
92 | $render = $this->controller->render('templateName'); | ||
93 | |||
94 | static::assertSame('templateName', $render); | ||
95 | |||
96 | static::assertSame(10, $this->assignedValues['linkcount']); | ||
97 | static::assertSame(5, $this->assignedValues['privateLinkcount']); | ||
98 | static::assertSame(['error'], $this->assignedValues['plugin_errors']); | ||
99 | |||
100 | static::assertSame('templateName', $this->assignedValues['plugins_includes']['render_includes']['target']); | ||
101 | static::assertTrue($this->assignedValues['plugins_includes']['render_includes']['loggedin']); | ||
102 | static::assertSame('templateName', $this->assignedValues['plugins_header']['render_header']['target']); | ||
103 | static::assertTrue($this->assignedValues['plugins_header']['render_header']['loggedin']); | ||
104 | static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']); | ||
105 | static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']); | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * Test redirectFromReferer() - Default behaviour | ||
110 | */ | ||
111 | public function testRedirectFromRefererDefault(): void | ||
112 | { | ||
113 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
114 | |||
115 | $response = new Response(); | ||
116 | |||
117 | $result = $this->controller->redirectFromReferer($this->request, $response); | ||
118 | |||
119 | static::assertSame(302, $result->getStatusCode()); | ||
120 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * Test redirectFromReferer() - With a loop term not matched in the referer | ||
125 | */ | ||
126 | public function testRedirectFromRefererWithUnmatchedLoopTerm(): void | ||
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($this->request, $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->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
144 | |||
145 | $response = new Response(); | ||
146 | |||
147 | $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'controller']); | ||
148 | |||
149 | static::assertSame(302, $result->getStatusCode()); | ||
150 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default | ||
155 | */ | ||
156 | public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void | ||
157 | { | ||
158 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
159 | |||
160 | $response = new Response(); | ||
161 | |||
162 | $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'other']); | ||
163 | |||
164 | static::assertSame(302, $result->getStatusCode()); | ||
165 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | ||
166 | } | ||
167 | |||
168 | /** | ||
169 | * Test redirectFromReferer() - With a loop term matching the referer in its query value | ||
170 | * -> we do not block redirection for query parameter values. | ||
171 | */ | ||
172 | public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void | ||
173 | { | ||
174 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
175 | |||
176 | $response = new Response(); | ||
177 | |||
178 | $result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'param']); | ||
179 | |||
180 | static::assertSame(302, $result->getStatusCode()); | ||
181 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | ||
182 | } | ||
183 | |||
184 | /** | ||
185 | * Test redirectFromReferer() - With a loop term matching the referer in its domain name | ||
186 | * -> we do not block redirection for shaarli's hosts | ||
187 | */ | ||
188 | public function testRedirectFromRefererWithLoopTermInDomain(): void | ||
189 | { | ||
190 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
191 | |||
192 | $response = new Response(); | ||
193 | |||
194 | $result = $this->controller->redirectFromReferer($this->request, $response, ['shaarli']); | ||
195 | |||
196 | static::assertSame(302, $result->getStatusCode()); | ||
197 | static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location')); | ||
198 | } | ||
199 | |||
200 | /** | ||
201 | * Test redirectFromReferer() - With a loop term matching a query parameter AND clear this query param | ||
202 | * -> the param should be cleared before checking if it matches the redir loop terms | ||
203 | */ | ||
204 | public function testRedirectFromRefererWithMatchingClearedParam(): void | ||
205 | { | ||
206 | $this->container->environment['HTTP_REFERER'] = 'http://shaarli.tld/subfolder/controller?query=param&other=2'; | ||
207 | |||
208 | $response = new Response(); | ||
209 | |||
210 | $result = $this->controller->redirectFromReferer($this->request, $response, ['query'], ['query']); | ||
211 | |||
212 | static::assertSame(302, $result->getStatusCode()); | ||
213 | static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location')); | ||
214 | } | ||
215 | } | ||