aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/ExportControllerTest.php
blob: 12d26f4a5ff5e89b93cc8514691a94b034fc093c (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
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller\Admin;

use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\Bookmark;
use Shaarli\Formatter\BookmarkFormatter;
use Shaarli\Formatter\BookmarkRawFormatter;
use Shaarli\Netscape\NetscapeBookmarkUtils;
use Shaarli\Security\SessionManager;
use Slim\Http\Request;
use Slim\Http\Response;

class ExportControllerTest extends TestCase
{
    use FrontAdminControllerMockHelper;

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

    public function setUp(): void
    {
        $this->createContainer();

        $this->controller = new ExportController($this->container);
    }

    /**
     * Test displaying export page
     */
    public function testIndex(): void
    {
        $assignedVariables = [];
        $this->assignTemplateVars($assignedVariables);

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

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

        static::assertSame(200, $result->getStatusCode());
        static::assertSame('export', (string) $result->getBody());

        static::assertSame('Export - Shaarli', $assignedVariables['pagetitle']);
    }

    /**
     * Test posting an export request
     */
    public function testExportDefault(): void
    {
        $assignedVariables = [];
        $this->assignTemplateVars($assignedVariables);

        $parameters = [
            'selection' => 'all',
            'prepend_note_url' => 'on',
        ];

        $request = $this->createMock(Request::class);
        $request->method('getParam')->willReturnCallback(function (string $key) use ($parameters) {
            return $parameters[$key] ?? null;
        });
        $response = new Response();

        $bookmarks = [
            (new Bookmark())->setUrl('http://link1.tld')->setTitle('Title 1'),
            (new Bookmark())->setUrl('http://link2.tld')->setTitle('Title 2'),
        ];

        $this->container->netscapeBookmarkUtils = $this->createMock(NetscapeBookmarkUtils::class);
        $this->container->netscapeBookmarkUtils
            ->expects(static::once())
            ->method('filterAndFormat')
            ->willReturnCallback(
                function (
                    BookmarkFormatter $formatter,
                    string $selection,
                    bool $prependNoteUrl,
                    string $indexUrl
                ) use ($parameters, $bookmarks): array {
                    static::assertInstanceOf(BookmarkRawFormatter::class, $formatter);
                    static::assertSame($parameters['selection'], $selection);
                    static::assertTrue($prependNoteUrl);
                    static::assertSame('http://shaarli/subfolder/', $indexUrl);

                    return $bookmarks;
                }
            )
        ;

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

        static::assertSame(200, $result->getStatusCode());
        static::assertSame('export.bookmarks', (string) $result->getBody());
        static::assertSame(['text/html; charset=utf-8'], $result->getHeader('content-type'));
        static::assertRegExp(
            '/attachment; filename=bookmarks_all_[\d]{8}_[\d]{6}\.html/',
            $result->getHeader('content-disposition')[0]
        );

        static::assertNotEmpty($assignedVariables['date']);
        static::assertSame(PHP_EOL, $assignedVariables['eol']);
        static::assertSame('all', $assignedVariables['selection']);
        static::assertSame($bookmarks, $assignedVariables['links']);
    }

    /**
     * Test posting an export request - without selection parameter
     */
    public function testExportSelectionMissing(): void
    {
        $request = $this->createMock(Request::class);
        $response = new Response();

        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_ERROR_MESSAGES, ['Please select an export mode.'])
        ;

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

        static::assertSame(302, $result->getStatusCode());
        static::assertSame(['/subfolder/admin/export'], $result->getHeader('location'));
    }

    /**
     * Test posting an export request - without selection parameter
     */
    public function testExportErrorEncountered(): void
    {
        $parameters = [
            'selection' => 'all',
        ];

        $request = $this->createMock(Request::class);
        $request->method('getParam')->willReturnCallback(function (string $key) use ($parameters) {
            return $parameters[$key] ?? null;
        });
        $response = new Response();

        $this->container->netscapeBookmarkUtils = $this->createMock(NetscapeBookmarkUtils::class);
        $this->container->netscapeBookmarkUtils
            ->expects(static::once())
            ->method('filterAndFormat')
            ->willThrowException(new \Exception($message = 'error message'));
        ;

        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_ERROR_MESSAGES, [$message])
        ;

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

        static::assertSame(302, $result->getStatusCode());
        static::assertSame(['/subfolder/admin/export'], $result->getHeader('location'));
    }
}