aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin
diff options
context:
space:
mode:
Diffstat (limited to 'tests/front/controller/admin')
-rw-r--r--tests/front/controller/admin/ExportControllerTest.php167
1 files changed, 167 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ExportControllerTest.php b/tests/front/controller/admin/ExportControllerTest.php
new file mode 100644
index 00000000..e43a9626
--- /dev/null
+++ b/tests/front/controller/admin/ExportControllerTest.php
@@ -0,0 +1,167 @@
1<?php
2
3declare(strict_types=1);
4
5namespace front\controller\admin;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\Bookmark;
9use Shaarli\Formatter\BookmarkFormatter;
10use Shaarli\Formatter\BookmarkRawFormatter;
11use Shaarli\Front\Controller\Admin\ExportController;
12use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
13use Shaarli\Netscape\NetscapeBookmarkUtils;
14use Shaarli\Security\SessionManager;
15use Slim\Http\Request;
16use Slim\Http\Response;
17
18class ExportControllerTest extends TestCase
19{
20 use FrontAdminControllerMockHelper;
21
22 /** @var ExportController */
23 protected $controller;
24
25 public function setUp(): void
26 {
27 $this->createContainer();
28
29 $this->controller = new ExportController($this->container);
30 }
31
32 /**
33 * Test displaying export page
34 */
35 public function testIndex(): void
36 {
37 $assignedVariables = [];
38 $this->assignTemplateVars($assignedVariables);
39
40 $request = $this->createMock(Request::class);
41 $response = new Response();
42
43 $result = $this->controller->index($request, $response);
44
45 static::assertSame(200, $result->getStatusCode());
46 static::assertSame('export', (string) $result->getBody());
47
48 static::assertSame('Export - Shaarli', $assignedVariables['pagetitle']);
49 }
50
51 /**
52 * Test posting an export request
53 */
54 public function testExportDefault(): void
55 {
56 $assignedVariables = [];
57 $this->assignTemplateVars($assignedVariables);
58
59 $parameters = [
60 'selection' => 'all',
61 'prepend_note_url' => 'on',
62 ];
63
64 $request = $this->createMock(Request::class);
65 $request->method('getParam')->willReturnCallback(function (string $key) use ($parameters) {
66 return $parameters[$key] ?? null;
67 });
68 $response = new Response();
69
70 $bookmarks = [
71 (new Bookmark())->setUrl('http://link1.tld')->setTitle('Title 1'),
72 (new Bookmark())->setUrl('http://link2.tld')->setTitle('Title 2'),
73 ];
74
75 $this->container->netscapeBookmarkUtils = $this->createMock(NetscapeBookmarkUtils::class);
76 $this->container->netscapeBookmarkUtils
77 ->expects(static::once())
78 ->method('filterAndFormat')
79 ->willReturnCallback(
80 function (
81 BookmarkFormatter $formatter,
82 string $selection,
83 bool $prependNoteUrl,
84 string $indexUrl
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);
90
91 return $bookmarks;
92 }
93 )
94 ;
95
96 $result = $this->controller->export($request, $response);
97
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]
104 );
105
106 static::assertNotEmpty($assignedVariables['date']);
107 static::assertSame(PHP_EOL, $assignedVariables['eol']);
108 static::assertSame('all', $assignedVariables['selection']);
109 static::assertSame($bookmarks, $assignedVariables['links']);
110 }
111
112 /**
113 * Test posting an export request - without selection parameter
114 */
115 public function testExportSelectionMissing(): void
116 {
117 $request = $this->createMock(Request::class);
118 $response = new Response();
119
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.'])
125 ;
126
127 $result = $this->controller->export($request, $response);
128
129 static::assertSame(302, $result->getStatusCode());
130 static::assertSame(['/subfolder/admin/export'], $result->getHeader('location'));
131 }
132
133 /**
134 * Test posting an export request - without selection parameter
135 */
136 public function testExportErrorEncountered(): void
137 {
138 $parameters = [
139 'selection' => 'all',
140 ];
141
142 $request = $this->createMock(Request::class);
143 $request->method('getParam')->willReturnCallback(function (string $key) use ($parameters) {
144 return $parameters[$key] ?? null;
145 });
146 $response = new Response();
147
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'));
153 ;
154
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])
160 ;
161
162 $result = $this->controller->export($request, $response);
163
164 static::assertSame(302, $result->getStatusCode());
165 static::assertSame(['/subfolder/admin/export'], $result->getHeader('location'));
166 }
167}