]>
Commit | Line | Data |
---|---|---|
72caf4e8 A |
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 Shaarli\Bookmark\BookmarkServiceInterface; | |
10 | use Shaarli\Config\ConfigManager; | |
11 | use Shaarli\Container\ShaarliContainer; | |
12 | use Shaarli\Plugin\PluginManager; | |
13 | use Shaarli\Render\PageBuilder; | |
14 | use Shaarli\Security\LoginManager; | |
15 | use Shaarli\Security\SessionManager; | |
16 | use Slim\Http\Request; | |
17 | use Slim\Http\Response; | |
18 | ||
19 | class TagCloudControllerTest extends TestCase | |
20 | { | |
21 | /** @var ShaarliContainer */ | |
22 | protected $container; | |
23 | ||
24 | /** @var TagCloudController */ | |
25 | protected $controller; | |
26 | ||
27 | public function setUp(): void | |
28 | { | |
29 | $this->container = $this->createMock(ShaarliContainer::class); | |
30 | $this->controller = new TagCloudController($this->container); | |
31 | } | |
32 | ||
33 | public function testValidCloudControllerInvokeDefault(): void | |
34 | { | |
35 | $this->createValidContainerMockSet(); | |
36 | ||
37 | $allTags = [ | |
38 | 'ghi' => 1, | |
39 | 'abc' => 3, | |
40 | 'def' => 12, | |
41 | ]; | |
42 | $expectedOrder = ['abc', 'def', 'ghi']; | |
43 | ||
44 | $request = $this->createMock(Request::class); | |
45 | $request->expects(static::once())->method('getQueryParam')->with('searchtags')->willReturn(null); | |
46 | $response = new Response(); | |
47 | ||
48 | // Save RainTPL assigned variables | |
49 | $assignedVariables = []; | |
50 | $this->assignTemplateVars($assignedVariables); | |
51 | ||
52 | $this->container->bookmarkService | |
53 | ->expects(static::once()) | |
54 | ->method('bookmarksCountPerTag') | |
55 | ->with([], null) | |
56 | ->willReturnCallback(function () use ($allTags): array { | |
57 | return $allTags; | |
58 | }) | |
59 | ; | |
60 | ||
61 | // Make sure that PluginManager hook is triggered | |
62 | $this->container->pluginManager | |
63 | ->expects(static::at(0)) | |
64 | ->method('executeHooks') | |
65 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | |
66 | static::assertSame('render_tagcloud', $hook); | |
67 | static::assertSame('', $data['search_tags']); | |
68 | static::assertCount(3, $data['tags']); | |
69 | ||
70 | static::assertArrayHasKey('loggedin', $param); | |
71 | ||
72 | return $data; | |
73 | }) | |
74 | ; | |
75 | ||
3772298e | 76 | $result = $this->controller->cloud($request, $response); |
72caf4e8 A |
77 | |
78 | static::assertSame(200, $result->getStatusCode()); | |
79 | static::assertSame('tag.cloud', (string) $result->getBody()); | |
80 | static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']); | |
81 | ||
82 | static::assertSame('', $assignedVariables['search_tags']); | |
83 | static::assertCount(3, $assignedVariables['tags']); | |
84 | static::assertSame($expectedOrder, array_keys($assignedVariables['tags'])); | |
85 | ||
86 | foreach ($allTags as $tag => $count) { | |
87 | static::assertArrayHasKey($tag, $assignedVariables['tags']); | |
88 | static::assertSame($count, $assignedVariables['tags'][$tag]['count']); | |
89 | static::assertGreaterThan(0, $assignedVariables['tags'][$tag]['size']); | |
90 | static::assertLessThan(5, $assignedVariables['tags'][$tag]['size']); | |
91 | } | |
92 | } | |
93 | ||
94 | /** | |
95 | * Additional parameters: | |
96 | * - logged in | |
97 | * - visibility private | |
98 | * - search tags: `ghi` and `def` (note that filtered tags are not displayed anymore) | |
99 | */ | |
100 | public function testValidCloudControllerInvokeWithParameters(): void | |
101 | { | |
102 | $this->createValidContainerMockSet(); | |
103 | ||
104 | $allTags = [ | |
105 | 'ghi' => 1, | |
106 | 'abc' => 3, | |
107 | 'def' => 12, | |
108 | ]; | |
109 | ||
110 | $request = $this->createMock(Request::class); | |
111 | $request | |
112 | ->expects(static::once()) | |
113 | ->method('getQueryParam') | |
114 | ->with('searchtags') | |
115 | ->willReturn('ghi def') | |
116 | ; | |
117 | $response = new Response(); | |
118 | ||
119 | // Save RainTPL assigned variables | |
120 | $assignedVariables = []; | |
121 | $this->assignTemplateVars($assignedVariables); | |
122 | ||
123 | $this->container->loginManager->method('isLoggedin')->willReturn(true); | |
124 | $this->container->sessionManager->expects(static::once())->method('getSessionParameter')->willReturn('private'); | |
125 | ||
126 | $this->container->bookmarkService | |
127 | ->expects(static::once()) | |
128 | ->method('bookmarksCountPerTag') | |
129 | ->with(['ghi', 'def'], BookmarkFilter::$PRIVATE) | |
c79473bd A |
130 | ->willReturnCallback(function (): array { |
131 | return ['abc' => 3]; | |
72caf4e8 A |
132 | }) |
133 | ; | |
134 | ||
135 | // Make sure that PluginManager hook is triggered | |
136 | $this->container->pluginManager | |
137 | ->expects(static::at(0)) | |
138 | ->method('executeHooks') | |
139 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | |
140 | static::assertSame('render_tagcloud', $hook); | |
141 | static::assertSame('ghi def', $data['search_tags']); | |
142 | static::assertCount(1, $data['tags']); | |
143 | ||
144 | static::assertArrayHasKey('loggedin', $param); | |
145 | ||
146 | return $data; | |
147 | }) | |
148 | ; | |
149 | ||
3772298e | 150 | $result = $this->controller->cloud($request, $response); |
72caf4e8 A |
151 | |
152 | static::assertSame(200, $result->getStatusCode()); | |
153 | static::assertSame('tag.cloud', (string) $result->getBody()); | |
154 | static::assertSame('ghi def - Tag cloud - Shaarli', $assignedVariables['pagetitle']); | |
155 | ||
156 | static::assertSame('ghi def', $assignedVariables['search_tags']); | |
157 | static::assertCount(1, $assignedVariables['tags']); | |
158 | ||
159 | static::assertArrayHasKey('abc', $assignedVariables['tags']); | |
160 | static::assertSame(3, $assignedVariables['tags']['abc']['count']); | |
161 | static::assertGreaterThan(0, $assignedVariables['tags']['abc']['size']); | |
162 | static::assertLessThan(5, $assignedVariables['tags']['abc']['size']); | |
163 | } | |
164 | ||
165 | public function testEmptyCloud(): void | |
166 | { | |
167 | $this->createValidContainerMockSet(); | |
168 | ||
169 | $request = $this->createMock(Request::class); | |
170 | $request->expects(static::once())->method('getQueryParam')->with('searchtags')->willReturn(null); | |
171 | $response = new Response(); | |
172 | ||
173 | // Save RainTPL assigned variables | |
174 | $assignedVariables = []; | |
175 | $this->assignTemplateVars($assignedVariables); | |
176 | ||
177 | $this->container->bookmarkService | |
178 | ->expects(static::once()) | |
179 | ->method('bookmarksCountPerTag') | |
180 | ->with([], null) | |
181 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | |
182 | return []; | |
183 | }) | |
184 | ; | |
185 | ||
186 | // Make sure that PluginManager hook is triggered | |
187 | $this->container->pluginManager | |
188 | ->expects(static::at(0)) | |
189 | ->method('executeHooks') | |
190 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | |
191 | static::assertSame('render_tagcloud', $hook); | |
192 | static::assertSame('', $data['search_tags']); | |
193 | static::assertCount(0, $data['tags']); | |
194 | ||
195 | static::assertArrayHasKey('loggedin', $param); | |
196 | ||
197 | return $data; | |
198 | }) | |
199 | ; | |
200 | ||
3772298e | 201 | $result = $this->controller->cloud($request, $response); |
72caf4e8 A |
202 | |
203 | static::assertSame(200, $result->getStatusCode()); | |
204 | static::assertSame('tag.cloud', (string) $result->getBody()); | |
205 | static::assertSame('Tag cloud - Shaarli', $assignedVariables['pagetitle']); | |
206 | ||
207 | static::assertSame('', $assignedVariables['search_tags']); | |
208 | static::assertCount(0, $assignedVariables['tags']); | |
209 | } | |
210 | ||
211 | protected function createValidContainerMockSet(): void | |
212 | { | |
213 | $loginManager = $this->createMock(LoginManager::class); | |
214 | $this->container->loginManager = $loginManager; | |
215 | ||
216 | $sessionManager = $this->createMock(SessionManager::class); | |
217 | $this->container->sessionManager = $sessionManager; | |
218 | ||
219 | // Config | |
220 | $conf = $this->createMock(ConfigManager::class); | |
221 | $this->container->conf = $conf; | |
222 | ||
223 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | |
224 | return $default; | |
225 | }); | |
226 | ||
227 | // PageBuilder | |
228 | $pageBuilder = $this->createMock(PageBuilder::class); | |
229 | $pageBuilder | |
230 | ->method('render') | |
231 | ->willReturnCallback(function (string $template): string { | |
232 | return $template; | |
233 | }) | |
234 | ; | |
235 | $this->container->pageBuilder = $pageBuilder; | |
236 | ||
237 | // Plugin Manager | |
238 | $pluginManager = $this->createMock(PluginManager::class); | |
239 | $this->container->pluginManager = $pluginManager; | |
240 | ||
241 | // BookmarkService | |
242 | $bookmarkService = $this->createMock(BookmarkServiceInterface::class); | |
243 | $this->container->bookmarkService = $bookmarkService; | |
244 | } | |
245 | ||
246 | protected function assignTemplateVars(array &$variables): void | |
247 | { | |
248 | $this->container->pageBuilder | |
249 | ->expects(static::atLeastOnce()) | |
250 | ->method('assign') | |
251 | ->willReturnCallback(function ($key, $value) use (&$variables) { | |
252 | $variables[$key] = $value; | |
253 | ||
254 | return $this; | |
255 | }) | |
256 | ; | |
257 | } | |
258 | } |