3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
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
;
15 class ImportControllerTest
extends TestCase
17 use FrontAdminControllerMockHelper
;
19 /** @var ImportController */
20 protected $controller;
22 public function setUp(): void
24 $this->createContainer();
26 $this->controller
= new ImportController($this->container
);
30 * Test displaying import page
32 public function testIndex(): void
34 $assignedVariables = [];
35 $this->assignTemplateVars($assignedVariables);
37 $request = $this->createMock(Request
::class);
38 $response = new Response();
40 $result = $this->controller
->index($request, $response);
42 static::assertSame(200, $result->getStatusCode());
43 static::assertSame('import', (string) $result->getBody());
45 static::assertSame('Import - Shaarli', $assignedVariables['pagetitle']);
46 static::assertIsInt($assignedVariables['maxfilesize']);
47 static::assertRegExp('/\d+[KM]iB/', $assignedVariables['maxfilesizeHuman']);
51 * Test importing a file with default and valid parameters
53 public function testImportDefault(): void
60 $requestFile = new UploadedFile('file', 'name', 'type', 123);
62 $request = $this->createMock(Request
::class);
63 $request->method('getParams')->willReturnCallback(function () use ($parameters) {
66 $request->method('getUploadedFiles')->willReturn(['filetoupload' => $requestFile]);
67 $response = new Response();
69 $this->container
->netscapeBookmarkUtils
= $this->createMock(NetscapeBookmarkUtils
::class);
70 $this->container
->netscapeBookmarkUtils
71 ->expects(static::once())
76 UploadedFileInterface
$file
77 ) use ($parameters, $requestFile): string {
78 static::assertSame($parameters, $post);
79 static::assertSame($requestFile, $file);
86 $this->container
->sessionManager
87 ->expects(static::once())
88 ->method('setSessionParameter')
89 ->with(SessionManager
::KEY_SUCCESS_MESSAGES
, ['status'])
92 $result = $this->controller
->import($request, $response);
94 static::assertSame(302, $result->getStatusCode());
95 static::assertSame(['/subfolder/admin/import'], $result->getHeader('location'));
99 * Test posting an import request - without import file
101 public function testImportFileMissing(): void
103 $request = $this->createMock(Request
::class);
104 $response = new Response();
106 $this->container
->sessionManager
107 ->expects(static::once())
108 ->method('setSessionParameter')
109 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['No import file provided.'])
112 $result = $this->controller
->import($request, $response);
114 static::assertSame(302, $result->getStatusCode());
115 static::assertSame(['/subfolder/admin/import'], $result->getHeader('location'));
119 * Test posting an import request - with an empty file
121 public function testImportEmptyFile(): void
123 $requestFile = new UploadedFile('file', 'name', 'type', 0);
125 $request = $this->createMock(Request
::class);
126 $request->method('getUploadedFiles')->willReturn(['filetoupload' => $requestFile]);
127 $response = new Response();
129 $this->container
->netscapeBookmarkUtils
= $this->createMock(NetscapeBookmarkUtils
::class);
130 $this->container
->netscapeBookmarkUtils
->expects(static::never())->method('filterAndFormat');
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]);
139 return $this->container
->sessionManager
;
143 $result = $this->controller
->import($request, $response);
145 static::assertSame(302, $result->getStatusCode());
146 static::assertSame(['/subfolder/admin/import'], $result->getHeader('location'));