aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-06-20 15:14:24 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit1b8620b1ad4e2c647ff2d032c8e7c6687b6647a1 (patch)
tree98a76bf93f9ed84680daa06050680a7c0425e535 /tests/front/controller
parent78657347c5b463d7c22bfc8c87b7db39fe058833 (diff)
downloadShaarli-1b8620b1ad4e2c647ff2d032c8e7c6687b6647a1.tar.gz
Shaarli-1b8620b1ad4e2c647ff2d032c8e7c6687b6647a1.tar.zst
Shaarli-1b8620b1ad4e2c647ff2d032c8e7c6687b6647a1.zip
Process plugins administration page through Slim controllers
Diffstat (limited to 'tests/front/controller')
-rw-r--r--tests/front/controller/admin/PluginsControllerTest.php190
1 files changed, 190 insertions, 0 deletions
diff --git a/tests/front/controller/admin/PluginsControllerTest.php b/tests/front/controller/admin/PluginsControllerTest.php
new file mode 100644
index 00000000..700a0df2
--- /dev/null
+++ b/tests/front/controller/admin/PluginsControllerTest.php
@@ -0,0 +1,190 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Front\Exception\WrongTokenException;
10use Shaarli\Security\SessionManager;
11use Slim\Http\Request;
12use Slim\Http\Response;
13
14class PluginsControllerTest extends TestCase
15{
16 use FrontAdminControllerMockHelper;
17
18 /** @var PluginsController */
19 protected $controller;
20
21 public function setUp(): void
22 {
23 $this->createContainer();
24
25 $this->controller = new PluginsController($this->container);
26 }
27
28 /**
29 * Test displaying plugins admin page
30 */
31 public function testIndex(): void
32 {
33 $assignedVariables = [];
34 $this->assignTemplateVars($assignedVariables);
35
36 $request = $this->createMock(Request::class);
37 $response = new Response();
38
39 $data = [
40 'plugin1' => ['order' => 2, 'other' => 'field'],
41 'plugin2' => ['order' => 1],
42 'plugin3' => ['order' => false, 'abc' => 'def'],
43 'plugin4' => [],
44 ];
45
46 $this->container->pluginManager
47 ->expects(static::once())
48 ->method('getPluginsMeta')
49 ->willReturn($data);
50
51 $result = $this->controller->index($request, $response);
52
53 static::assertSame(200, $result->getStatusCode());
54 static::assertSame('pluginsadmin', (string) $result->getBody());
55
56 static::assertSame('Plugin Administration - Shaarli', $assignedVariables['pagetitle']);
57 static::assertSame(
58 ['plugin2' => $data['plugin2'], 'plugin1' => $data['plugin1']],
59 $assignedVariables['enabledPlugins']
60 );
61 static::assertSame(
62 ['plugin3' => $data['plugin3'], 'plugin4' => $data['plugin4']],
63 $assignedVariables['disabledPlugins']
64 );
65 }
66
67 /**
68 * Test save plugins admin page
69 */
70 public function testSaveEnabledPlugins(): void
71 {
72 $parameters = [
73 'plugin1' => 'on',
74 'order_plugin1' => '2',
75 'plugin2' => 'on',
76 ];
77
78 $request = $this->createMock(Request::class);
79 $request
80 ->expects(static::atLeastOnce())
81 ->method('getParams')
82 ->willReturnCallback(function () use ($parameters): array {
83 return $parameters;
84 })
85 ;
86 $response = new Response();
87
88 $this->container->pluginManager
89 ->expects(static::once())
90 ->method('executeHooks')
91 ->with('save_plugin_parameters', $parameters)
92 ;
93 $this->container->conf
94 ->expects(static::atLeastOnce())
95 ->method('set')
96 ->with('general.enabled_plugins', ['plugin1', 'plugin2'])
97 ;
98
99 $result = $this->controller->save($request, $response);
100
101 static::assertSame(302, $result->getStatusCode());
102 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
103 }
104
105 /**
106 * Test save plugin parameters
107 */
108 public function testSavePluginParameters(): void
109 {
110 $parameters = [
111 'parameters_form' => true,
112 'parameter1' => 'blip',
113 'parameter2' => 'blop',
114 ];
115
116 $request = $this->createMock(Request::class);
117 $request
118 ->expects(static::atLeastOnce())
119 ->method('getParams')
120 ->willReturnCallback(function () use ($parameters): array {
121 return $parameters;
122 })
123 ;
124 $response = new Response();
125
126 $this->container->pluginManager
127 ->expects(static::once())
128 ->method('executeHooks')
129 ->with('save_plugin_parameters', $parameters)
130 ;
131 $this->container->conf
132 ->expects(static::atLeastOnce())
133 ->method('set')
134 ->withConsecutive(['plugins.parameter1', 'blip'], ['plugins.parameter2', 'blop'])
135 ;
136
137 $result = $this->controller->save($request, $response);
138
139 static::assertSame(302, $result->getStatusCode());
140 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
141 }
142
143 /**
144 * Test save plugin parameters - error encountered
145 */
146 public function testSaveWithError(): void
147 {
148 $request = $this->createMock(Request::class);
149 $response = new Response();
150
151 $this->container->conf = $this->createMock(ConfigManager::class);
152 $this->container->conf
153 ->expects(static::atLeastOnce())
154 ->method('write')
155 ->willThrowException(new \Exception($message = 'error message'))
156 ;
157
158 $this->container->sessionManager = $this->createMock(SessionManager::class);
159 $this->container->sessionManager->method('checkToken')->willReturn(true);
160 $this->container->sessionManager
161 ->expects(static::once())
162 ->method('setSessionParameter')
163 ->with(
164 SessionManager::KEY_ERROR_MESSAGES,
165 ['ERROR while saving plugin configuration: ' . PHP_EOL . $message]
166 )
167 ;
168
169 $result = $this->controller->save($request, $response);
170
171 static::assertSame(302, $result->getStatusCode());
172 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
173 }
174
175 /**
176 * Test save plugin parameters - wrong token
177 */
178 public function testSaveWrongToken(): void
179 {
180 $this->container->sessionManager = $this->createMock(SessionManager::class);
181 $this->container->sessionManager->method('checkToken')->willReturn(false);
182
183 $request = $this->createMock(Request::class);
184 $response = new Response();
185
186 $this->expectException(WrongTokenException::class);
187
188 $this->controller->save($request, $response);
189 }
190}