3 declare(strict_types
=1);
5 namespace front\controller\admin
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\Bookmark
;
9 use Shaarli\Formatter\BookmarkFormatter
;
10 use Shaarli\Formatter\BookmarkRawFormatter
;
11 use Shaarli\Front\Controller\Admin\ExportController
;
12 use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper
;
13 use Shaarli\Netscape\NetscapeBookmarkUtils
;
14 use Shaarli\Security\SessionManager
;
15 use Slim\Http\Request
;
16 use Slim\Http\Response
;
18 class ExportControllerTest
extends TestCase
20 use FrontAdminControllerMockHelper
;
22 /** @var ExportController */
23 protected $controller;
25 public function setUp(): void
27 $this->createContainer();
29 $this->controller
= new ExportController($this->container
);
33 * Test displaying export page
35 public function testIndex(): void
37 $assignedVariables = [];
38 $this->assignTemplateVars($assignedVariables);
40 $request = $this->createMock(Request
::class);
41 $response = new Response();
43 $result = $this->controller
->index($request, $response);
45 static::assertSame(200, $result->getStatusCode());
46 static::assertSame('export', (string) $result->getBody());
48 static::assertSame('Export - Shaarli', $assignedVariables['pagetitle']);
52 * Test posting an export request
54 public function testExportDefault(): void
56 $assignedVariables = [];
57 $this->assignTemplateVars($assignedVariables);
61 'prepend_note_url' => 'on',
64 $request = $this->createMock(Request
::class);
65 $request->method('getParam')->willReturnCallback(function (string $key) use ($parameters) {
66 return $parameters[$key] ?? null;
68 $response = new Response();
71 (new Bookmark())->setUrl('http://link1.tld')->setTitle('Title 1'),
72 (new Bookmark())->setUrl('http://link2.tld')->setTitle('Title 2'),
75 $this->container
->netscapeBookmarkUtils
= $this->createMock(NetscapeBookmarkUtils
::class);
76 $this->container
->netscapeBookmarkUtils
77 ->expects(static::once())
78 ->method('filterAndFormat')
81 BookmarkFormatter
$formatter,
85 ) use ($parameters, $bookmarks): array {
86 static::assertInstanceOf(BookmarkRawFormatter
::class, $formatter);
87 static::assertSame($parameters['selection'], $selection);
88 static::assertTrue($prependNoteUrl);
89 static::assertSame('http://shaarli', $indexUrl);
96 $result = $this->controller
->export($request, $response);
98 static::assertSame(200, $result->getStatusCode());
99 static::assertSame('export.bookmarks', (string) $result->getBody());
100 static::assertSame(['text/html; charset=utf-8'], $result->getHeader('content-type'));
101 static::assertRegExp(
102 '/attachment; filename=bookmarks_all_[\d]{8}_[\d]{6}\.html/',
103 $result->getHeader('content-disposition')[0]
106 static::assertNotEmpty($assignedVariables['date']);
107 static::assertSame(PHP_EOL
, $assignedVariables['eol']);
108 static::assertSame('all', $assignedVariables['selection']);
109 static::assertSame($bookmarks, $assignedVariables['links']);
113 * Test posting an export request - without selection parameter
115 public function testExportSelectionMissing(): void
117 $request = $this->createMock(Request
::class);
118 $response = new Response();
120 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
121 $this->container
->sessionManager
122 ->expects(static::once())
123 ->method('setSessionParameter')
124 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Please select an export mode.'])
127 $result = $this->controller
->export($request, $response);
129 static::assertSame(302, $result->getStatusCode());
130 static::assertSame(['/subfolder/admin/export'], $result->getHeader('location'));
134 * Test posting an export request - without selection parameter
136 public function testExportErrorEncountered(): void
139 'selection' => 'all',
142 $request = $this->createMock(Request
::class);
143 $request->method('getParam')->willReturnCallback(function (string $key) use ($parameters) {
144 return $parameters[$key] ?? null;
146 $response = new Response();
148 $this->container
->netscapeBookmarkUtils
= $this->createMock(NetscapeBookmarkUtils
::class);
149 $this->container
->netscapeBookmarkUtils
150 ->expects(static::once())
151 ->method('filterAndFormat')
152 ->willThrowException(new \
Exception($message = 'error message'));
155 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
156 $this->container
->sessionManager
157 ->expects(static::once())
158 ->method('setSessionParameter')
159 ->with(SessionManager
::KEY_ERROR_MESSAGES
, [$message])
162 $result = $this->controller
->export($request, $response);
164 static::assertSame(302, $result->getStatusCode());
165 static::assertSame(['/subfolder/admin/export'], $result->getHeader('location'));