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

declare(strict_types=1);

namespace Shaarli\Front\Controller\Admin;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UploadedFileInterface;
use Shaarli\Netscape\NetscapeBookmarkUtils;
use Shaarli\Security\SessionManager;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\UploadedFile;

class ImportControllerTest extends TestCase
{
    use FrontAdminControllerMockHelper;

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

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

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

    /**
     * Test displaying import 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('import', (string) $result->getBody());

        static::assertSame('Import - Shaarli', $assignedVariables['pagetitle']);
        static::assertIsInt($assignedVariables['maxfilesize']);
        static::assertRegExp('/\d+[KM]iB/', $assignedVariables['maxfilesizeHuman']);
    }

    /**
     * Test importing a file with default and valid parameters
     */
    public function testImportDefault(): void
    {
        $parameters = [
            'abc' => 'def',
            'other' => 'param',
        ];

        $requestFile = new UploadedFile('file', 'name', 'type', 123);

        $request = $this->createMock(Request::class);
        $request->method('getParams')->willReturnCallback(function () use ($parameters) {
            return $parameters;
        });
        $request->method('getUploadedFiles')->willReturn(['filetoupload' => $requestFile]);
        $response = new Response();

        $this->container->netscapeBookmarkUtils = $this->createMock(NetscapeBookmarkUtils::class);
        $this->container->netscapeBookmarkUtils
            ->expects(static::once())
            ->method('import')
            ->willReturnCallback(
                function (
                    array $post,
                    UploadedFileInterface $file
                ) use ($parameters, $requestFile): string {
                    static::assertSame($parameters, $post);
                    static::assertSame($requestFile, $file);

                    return 'status';
                }
            )
        ;

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

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

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

    /**
     * Test posting an import request - without import file
     */
    public function testImportFileMissing(): void
    {
        $request = $this->createMock(Request::class);
        $response = new Response();

        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_ERROR_MESSAGES, ['No import file provided.'])
        ;

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

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

    /**
     * Test posting an import request - with an empty file
     */
    public function testImportEmptyFile(): void
    {
        $requestFile = new UploadedFile('file', 'name', 'type', 0);

        $request = $this->createMock(Request::class);
        $request->method('getUploadedFiles')->willReturn(['filetoupload' => $requestFile]);
        $response = new Response();

        $this->container->netscapeBookmarkUtils = $this->createMock(NetscapeBookmarkUtils::class);
        $this->container->netscapeBookmarkUtils->expects(static::never())->method('filterAndFormat');

        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->willReturnCallback(function (string $key, array $value): SessionManager {
                static::assertSame(SessionManager::KEY_ERROR_MESSAGES, $key);
                static::assertStringStartsWith('The file you are trying to upload is probably bigger', $value[0]);

                return $this->container->sessionManager;
            })
        ;

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

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