diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-06-17 19:08:02 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 78657347c5b463d7c22bfc8c87b7db39fe058833 (patch) | |
tree | a61c513ce05a597119cb5e9e65a33a94e01663d9 /tests/front/controller/admin/ImportControllerTest.php | |
parent | c70ff64a61d62cc8d35a62f30596ecc2a3c578a3 (diff) | |
download | Shaarli-78657347c5b463d7c22bfc8c87b7db39fe058833.tar.gz Shaarli-78657347c5b463d7c22bfc8c87b7db39fe058833.tar.zst Shaarli-78657347c5b463d7c22bfc8c87b7db39fe058833.zip |
Process bookmarks import through Slim controller
Diffstat (limited to 'tests/front/controller/admin/ImportControllerTest.php')
-rw-r--r-- | tests/front/controller/admin/ImportControllerTest.php | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ImportControllerTest.php b/tests/front/controller/admin/ImportControllerTest.php new file mode 100644 index 00000000..eb31fad0 --- /dev/null +++ b/tests/front/controller/admin/ImportControllerTest.php | |||
@@ -0,0 +1,148 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Psr\Http\Message\UploadedFileInterface; | ||
9 | use Shaarli\Netscape\NetscapeBookmarkUtils; | ||
10 | use Shaarli\Security\SessionManager; | ||
11 | use Slim\Http\Request; | ||
12 | use Slim\Http\Response; | ||
13 | use Slim\Http\UploadedFile; | ||
14 | |||
15 | class ImportControllerTest extends TestCase | ||
16 | { | ||
17 | use FrontAdminControllerMockHelper; | ||
18 | |||
19 | /** @var ImportController */ | ||
20 | protected $controller; | ||
21 | |||
22 | public function setUp(): void | ||
23 | { | ||
24 | $this->createContainer(); | ||
25 | |||
26 | $this->controller = new ImportController($this->container); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Test displaying import page | ||
31 | */ | ||
32 | public function testIndex(): void | ||
33 | { | ||
34 | $assignedVariables = []; | ||
35 | $this->assignTemplateVars($assignedVariables); | ||
36 | |||
37 | $request = $this->createMock(Request::class); | ||
38 | $response = new Response(); | ||
39 | |||
40 | $result = $this->controller->index($request, $response); | ||
41 | |||
42 | static::assertSame(200, $result->getStatusCode()); | ||
43 | static::assertSame('import', (string) $result->getBody()); | ||
44 | |||
45 | static::assertSame('Import - Shaarli', $assignedVariables['pagetitle']); | ||
46 | static::assertIsInt($assignedVariables['maxfilesize']); | ||
47 | static::assertRegExp('/\d+[KM]iB/', $assignedVariables['maxfilesizeHuman']); | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * Test importing a file with default and valid parameters | ||
52 | */ | ||
53 | public function testImportDefault(): void | ||
54 | { | ||
55 | $parameters = [ | ||
56 | 'abc' => 'def', | ||
57 | 'other' => 'param', | ||
58 | ]; | ||
59 | |||
60 | $requestFile = new UploadedFile('file', 'name', 'type', 123); | ||
61 | |||
62 | $request = $this->createMock(Request::class); | ||
63 | $request->method('getParams')->willReturnCallback(function () use ($parameters) { | ||
64 | return $parameters; | ||
65 | }); | ||
66 | $request->method('getUploadedFiles')->willReturn(['filetoupload' => $requestFile]); | ||
67 | $response = new Response(); | ||
68 | |||
69 | $this->container->netscapeBookmarkUtils = $this->createMock(NetscapeBookmarkUtils::class); | ||
70 | $this->container->netscapeBookmarkUtils | ||
71 | ->expects(static::once()) | ||
72 | ->method('import') | ||
73 | ->willReturnCallback( | ||
74 | function ( | ||
75 | array $post, | ||
76 | UploadedFileInterface $file | ||
77 | ) use ($parameters, $requestFile): string { | ||
78 | static::assertSame($parameters, $post); | ||
79 | static::assertSame($requestFile, $file); | ||
80 | |||
81 | return 'status'; | ||
82 | } | ||
83 | ) | ||
84 | ; | ||
85 | |||
86 | $this->container->sessionManager | ||
87 | ->expects(static::once()) | ||
88 | ->method('setSessionParameter') | ||
89 | ->with(SessionManager::KEY_SUCCESS_MESSAGES, ['status']) | ||
90 | ; | ||
91 | |||
92 | $result = $this->controller->import($request, $response); | ||
93 | |||
94 | static::assertSame(302, $result->getStatusCode()); | ||
95 | static::assertSame(['/subfolder/admin/import'], $result->getHeader('location')); | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * Test posting an import request - without import file | ||
100 | */ | ||
101 | public function testImportFileMissing(): void | ||
102 | { | ||
103 | $request = $this->createMock(Request::class); | ||
104 | $response = new Response(); | ||
105 | |||
106 | $this->container->sessionManager | ||
107 | ->expects(static::once()) | ||
108 | ->method('setSessionParameter') | ||
109 | ->with(SessionManager::KEY_ERROR_MESSAGES, ['No import file provided.']) | ||
110 | ; | ||
111 | |||
112 | $result = $this->controller->import($request, $response); | ||
113 | |||
114 | static::assertSame(302, $result->getStatusCode()); | ||
115 | static::assertSame(['/subfolder/admin/import'], $result->getHeader('location')); | ||
116 | } | ||
117 | |||
118 | /** | ||
119 | * Test posting an import request - with an empty file | ||
120 | */ | ||
121 | public function testImportEmptyFile(): void | ||
122 | { | ||
123 | $requestFile = new UploadedFile('file', 'name', 'type', 0); | ||
124 | |||
125 | $request = $this->createMock(Request::class); | ||
126 | $request->method('getUploadedFiles')->willReturn(['filetoupload' => $requestFile]); | ||
127 | $response = new Response(); | ||
128 | |||
129 | $this->container->netscapeBookmarkUtils = $this->createMock(NetscapeBookmarkUtils::class); | ||
130 | $this->container->netscapeBookmarkUtils->expects(static::never())->method('filterAndFormat'); | ||
131 | |||
132 | $this->container->sessionManager | ||
133 | ->expects(static::once()) | ||
134 | ->method('setSessionParameter') | ||
135 | ->willReturnCallback(function (string $key, array $value): SessionManager { | ||
136 | static::assertSame(SessionManager::KEY_ERROR_MESSAGES, $key); | ||
137 | static::assertStringStartsWith('The file you are trying to upload is probably bigger', $value[0]); | ||
138 | |||
139 | return $this->container->sessionManager; | ||
140 | }) | ||
141 | ; | ||
142 | |||
143 | $result = $this->controller->import($request, $response); | ||
144 | |||
145 | static::assertSame(302, $result->getStatusCode()); | ||
146 | static::assertSame(['/subfolder/admin/import'], $result->getHeader('location')); | ||
147 | } | ||
148 | } | ||