]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Visitor; | |
6 | ||
7 | use Shaarli\Bookmark\Bookmark; | |
8 | use Shaarli\Bookmark\SearchResult; | |
9 | use Shaarli\Config\ConfigManager; | |
10 | use Shaarli\Front\Exception\ThumbnailsDisabledException; | |
11 | use Shaarli\TestCase; | |
12 | use Shaarli\Thumbnailer; | |
13 | use Slim\Http\Request; | |
14 | use Slim\Http\Response; | |
15 | ||
16 | class PictureWallControllerTest extends TestCase | |
17 | { | |
18 | use FrontControllerMockHelper; | |
19 | ||
20 | /** @var PictureWallController */ | |
21 | protected $controller; | |
22 | ||
23 | public function setUp(): void | |
24 | { | |
25 | $this->createContainer(); | |
26 | ||
27 | $this->controller = new PictureWallController($this->container); | |
28 | } | |
29 | ||
30 | public function testValidControllerInvokeDefault(): void | |
31 | { | |
32 | $request = $this->createMock(Request::class); | |
33 | $request->expects(static::once())->method('getQueryParams')->willReturn([]); | |
34 | $response = new Response(); | |
35 | ||
36 | // ConfigManager: thumbnails are enabled | |
37 | $this->container->conf = $this->createMock(ConfigManager::class); | |
38 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | |
39 | if ($parameter === 'thumbnails.mode') { | |
40 | return Thumbnailer::MODE_COMMON; | |
41 | } | |
42 | ||
43 | return $default; | |
44 | }); | |
45 | ||
46 | // Save RainTPL assigned variables | |
47 | $assignedVariables = []; | |
48 | $this->assignTemplateVars($assignedVariables); | |
49 | ||
50 | // Links dataset: 2 links with thumbnails | |
51 | $this->container->bookmarkService | |
52 | ->expects(static::once()) | |
53 | ->method('search') | |
54 | ->willReturnCallback(function (array $parameters, ?string $visibility): SearchResult { | |
55 | // Visibility is set through the container, not the call | |
56 | static::assertNull($visibility); | |
57 | ||
58 | // No query parameters | |
59 | if (count($parameters) === 0) { | |
60 | return SearchResult::getSearchResult([ | |
61 | (new Bookmark())->setId(1)->setUrl('http://url.tld')->setThumbnail('thumb1'), | |
62 | (new Bookmark())->setId(2)->setUrl('http://url2.tld'), | |
63 | (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setThumbnail('thumb2'), | |
64 | ]); | |
65 | } | |
66 | }) | |
67 | ; | |
68 | ||
69 | // Make sure that PluginManager hook is triggered | |
70 | $this->container->pluginManager | |
71 | ->expects(static::atLeastOnce()) | |
72 | ->method('executeHooks') | |
73 | ->withConsecutive(['render_picwall']) | |
74 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | |
75 | if ('render_picwall' === $hook) { | |
76 | static::assertArrayHasKey('linksToDisplay', $data); | |
77 | static::assertCount(2, $data['linksToDisplay']); | |
78 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | |
79 | static::assertSame(3, $data['linksToDisplay'][1]['id']); | |
80 | static::assertArrayHasKey('loggedin', $param); | |
81 | } | |
82 | ||
83 | return $data; | |
84 | }); | |
85 | ||
86 | $result = $this->controller->index($request, $response); | |
87 | ||
88 | static::assertSame(200, $result->getStatusCode()); | |
89 | static::assertSame('picwall', (string) $result->getBody()); | |
90 | static::assertSame('Picture wall - Shaarli', $assignedVariables['pagetitle']); | |
91 | static::assertCount(2, $assignedVariables['linksToDisplay']); | |
92 | ||
93 | $link = $assignedVariables['linksToDisplay'][0]; | |
94 | ||
95 | static::assertSame(1, $link['id']); | |
96 | static::assertSame('http://url.tld', $link['url']); | |
97 | static::assertSame('thumb1', $link['thumbnail']); | |
98 | ||
99 | $link = $assignedVariables['linksToDisplay'][1]; | |
100 | ||
101 | static::assertSame(3, $link['id']); | |
102 | static::assertSame('http://url3.tld', $link['url']); | |
103 | static::assertSame('thumb2', $link['thumbnail']); | |
104 | } | |
105 | ||
106 | public function testControllerWithThumbnailsDisabled(): void | |
107 | { | |
108 | $this->expectException(ThumbnailsDisabledException::class); | |
109 | ||
110 | $request = $this->createMock(Request::class); | |
111 | $response = new Response(); | |
112 | ||
113 | // ConfigManager: thumbnails are disabled | |
114 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | |
115 | if ($parameter === 'thumbnails.mode') { | |
116 | return Thumbnailer::MODE_NONE; | |
117 | } | |
118 | ||
119 | return $default; | |
120 | }); | |
121 | ||
122 | $this->controller->index($request, $response); | |
123 | } | |
124 | } |