aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/PictureWallControllerTest.php
blob: 63802abdde978253e184ddfe56e082d15fe409c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller;

use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\Bookmark;
use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
use Shaarli\Container\ShaarliContainer;
use Shaarli\Formatter\BookmarkFormatter;
use Shaarli\Formatter\BookmarkRawFormatter;
use Shaarli\Formatter\FormatterFactory;
use Shaarli\Front\Exception\ThumbnailsDisabledException;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\PageBuilder;
use Shaarli\Security\LoginManager;
use Shaarli\Thumbnailer;
use Slim\Http\Request;
use Slim\Http\Response;

class PictureWallControllerTest extends TestCase
{
    /** @var ShaarliContainer */
    protected $container;

    /** @var PictureWallController */
    protected $controller;

    public function setUp(): void
    {
        $this->container = $this->createMock(ShaarliContainer::class);
        $this->controller = new PictureWallController($this->container);
    }

    public function testValidControllerInvokeDefault(): void
    {
        $this->createValidContainerMockSet();

        $request = $this->createMock(Request::class);
        $request->expects(static::once())->method('getQueryParams')->willReturn([]);
        $response = new Response();

        // ConfigManager: thumbnails are enabled
        $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
            if ($parameter === 'thumbnails.mode') {
                return Thumbnailer::MODE_COMMON;
            }

            return $default;
        });

        // Save RainTPL assigned variables
        $assignedVariables = [];
        $this->container->pageBuilder
            ->expects(static::atLeastOnce())
            ->method('assign')
            ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
                $assignedVariables[$key] = $value;

                return $this;
            })
        ;

        // Links dataset: 2 links with thumbnails
        $this->container->bookmarkService
            ->expects(static::once())
            ->method('search')
            ->willReturnCallback(function (array $parameters, ?string $visibility): array {
                // Visibility is set through the container, not the call
                static::assertNull($visibility);

                // No query parameters
                if (count($parameters) === 0) {
                    return [
                        (new Bookmark())->setId(1)->setUrl('http://url.tld')->setThumbnail('thumb1'),
                        (new Bookmark())->setId(2)->setUrl('http://url2.tld'),
                        (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setThumbnail('thumb2'),
                    ];
                }
            })
        ;

        // Make sure that PluginManager hook is triggered
        $this->container->pluginManager
            ->expects(static::at(0))
            ->method('executeHooks')
            ->willReturnCallback(function (string $hook, array $data, array $param): array {
                static::assertSame('render_picwall', $hook);
                static::assertArrayHasKey('linksToDisplay', $data);
                static::assertCount(2, $data['linksToDisplay']);
                static::assertSame(1, $data['linksToDisplay'][0]['id']);
                static::assertSame(3, $data['linksToDisplay'][1]['id']);
                static::assertArrayHasKey('loggedin', $param);

                return $data;
            });

        $result = $this->controller->index($request, $response);

        static::assertSame(200, $result->getStatusCode());
        static::assertSame('picwall', (string) $result->getBody());
        static::assertSame('Picture wall - Shaarli', $assignedVariables['pagetitle']);
        static::assertCount(2, $assignedVariables['linksToDisplay']);

        $link = $assignedVariables['linksToDisplay'][0];

        static::assertSame(1, $link['id']);
        static::assertSame('http://url.tld', $link['url']);
        static::assertSame('thumb1', $link['thumbnail']);

        $link = $assignedVariables['linksToDisplay'][1];

        static::assertSame(3, $link['id']);
        static::assertSame('http://url3.tld', $link['url']);
        static::assertSame('thumb2', $link['thumbnail']);
    }

    public function testControllerWithThumbnailsDisabled(): void
    {
        $this->expectException(ThumbnailsDisabledException::class);

        $this->createValidContainerMockSet();

        $request = $this->createMock(Request::class);
        $response = new Response();

        // ConfigManager: thumbnails are disabled
        $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
            if ($parameter === 'thumbnails.mode') {
                return Thumbnailer::MODE_NONE;
            }

            return $default;
        });

        $this->controller->index($request, $response);
    }

    protected function createValidContainerMockSet(): void
    {
        $loginManager = $this->createMock(LoginManager::class);
        $this->container->loginManager = $loginManager;

        // Config
        $conf = $this->createMock(ConfigManager::class);
        $this->container->conf = $conf;

        // PageBuilder
        $pageBuilder = $this->createMock(PageBuilder::class);
        $pageBuilder
            ->method('render')
            ->willReturnCallback(function (string $template): string {
                return $template;
            })
        ;
        $this->container->pageBuilder = $pageBuilder;

        // Plugin Manager
        $pluginManager = $this->createMock(PluginManager::class);
        $this->container->pluginManager = $pluginManager;

        // BookmarkService
        $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
        $this->container->bookmarkService = $bookmarkService;

        // Formatter
        $formatterFactory = $this->createMock(FormatterFactory::class);
        $formatterFactory
            ->method('getFormatter')
            ->willReturnCallback(function (string $type): BookmarkFormatter {
                if ($type === 'raw') {
                    return new BookmarkRawFormatter($this->container->conf, true);
                }
            })
        ;
        $this->container->formatterFactory = $formatterFactory;
    }
}