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