3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Formatter\BookmarkFormatter
;
9 use Shaarli\Formatter\BookmarkRawFormatter
;
10 use Shaarli\Netscape\NetscapeBookmarkUtils
;
11 use Shaarli\Security\SessionManager
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
16 class ExportControllerTest
extends TestCase
18 use FrontAdminControllerMockHelper
;
20 /** @var ExportController */
21 protected $controller;
23 public function setUp(): void
25 $this->createContainer();
27 $this->controller
= new ExportController($this->container
);
31 * Test displaying export page
33 public function testIndex(): void
35 $assignedVariables = [];
36 $this->assignTemplateVars($assignedVariables);
38 $request = $this->createMock(Request
::class);
39 $response = new Response();
41 $result = $this->controller
->index($request, $response);
43 static::assertSame(200, $result->getStatusCode());
44 static::assertSame('export', (string) $result->getBody());
46 static::assertSame('Export - Shaarli', $assignedVariables['pagetitle']);
50 * Test posting an export request
52 public function testExportDefault(): void
54 $assignedVariables = [];
55 $this->assignTemplateVars($assignedVariables);
59 'prepend_note_url' => 'on',
62 $request = $this->createMock(Request
::class);
63 $request->method('getParam')->willReturnCallback(function (string $key) use ($parameters) {
64 return $parameters[$key] ?? null;
66 $response = new Response();
69 (new Bookmark())->setUrl('http://link1.tld')->setTitle('Title 1'),
70 (new Bookmark())->setUrl('http://link2.tld')->setTitle('Title 2'),
73 $this->container
->netscapeBookmarkUtils
= $this->createMock(NetscapeBookmarkUtils
::class);
74 $this->container
->netscapeBookmarkUtils
75 ->expects(static::once())
76 ->method('filterAndFormat')
79 BookmarkFormatter
$formatter,
83 ) use ($parameters, $bookmarks): array {
84 static::assertInstanceOf(BookmarkRawFormatter
::class, $formatter);
85 static::assertSame($parameters['selection'], $selection);
86 static::assertTrue($prependNoteUrl);
87 static::assertSame('http://shaarli/subfolder/', $indexUrl);
94 $result = $this->controller
->export($request, $response);
96 static::assertSame(200, $result->getStatusCode());
97 static::assertSame('export.bookmarks', (string) $result->getBody());
98 static::assertSame(['text/html; charset=utf-8'], $result->getHeader('content-type'));
100 '/attachment; filename=bookmarks_all_[\d]{8}_[\d]{6}\.html/',
101 $result->getHeader('content-disposition')[0]
104 static::assertNotEmpty($assignedVariables['date']);
105 static::assertSame(PHP_EOL
, $assignedVariables['eol']);
106 static::assertSame('all', $assignedVariables['selection']);
107 static::assertSame($bookmarks, $assignedVariables['links']);
111 * Test posting an export request - without selection parameter
113 public function testExportSelectionMissing(): void
115 $request = $this->createMock(Request
::class);
116 $response = new Response();
118 $this->container
->sessionManager
119 ->expects(static::once())
120 ->method('setSessionParameter')
121 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Please select an export mode.'])
124 $result = $this->controller
->export($request, $response);
126 static::assertSame(302, $result->getStatusCode());
127 static::assertSame(['/subfolder/admin/export'], $result->getHeader('location'));
131 * Test posting an export request - without selection parameter
133 public function testExportErrorEncountered(): void
136 'selection' => 'all',
139 $request = $this->createMock(Request
::class);
140 $request->method('getParam')->willReturnCallback(function (string $key) use ($parameters) {
141 return $parameters[$key] ?? null;
143 $response = new Response();
145 $this->container
->netscapeBookmarkUtils
= $this->createMock(NetscapeBookmarkUtils
::class);
146 $this->container
->netscapeBookmarkUtils
147 ->expects(static::once())
148 ->method('filterAndFormat')
149 ->willThrowException(new \
Exception($message = 'error message'));
152 $this->container
->sessionManager
153 ->expects(static::once())
154 ->method('setSessionParameter')
155 ->with(SessionManager
::KEY_ERROR_MESSAGES
, [$message])
158 $result = $this->controller
->export($request, $response);
160 static::assertSame(302, $result->getStatusCode());
161 static::assertSame(['/subfolder/admin/export'], $result->getHeader('location'));