1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2015 Michael Giesler |
4
|
|
|
* |
5
|
|
|
* This file is part of Dembelo. |
6
|
|
|
* |
7
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License 3 for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
18
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @package AdminBundle |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace AdminBundle\Tests\Controller; |
26
|
|
|
|
27
|
|
|
use DembeloMain\Document\Importfile; |
28
|
|
|
use DembeloMain\Document\Licensee; |
29
|
|
|
use DembeloMain\Document\Topic; |
30
|
|
|
use DembeloMain\Document\User; |
31
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\ImportfileRepository; |
32
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\LicenseeRepository; |
33
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\TextNodeRepository; |
34
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\TopicRepository; |
35
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\UserRepository; |
36
|
|
|
use DembeloMain\Model\Repository\ImportfileRepositoryInterface; |
37
|
|
|
use PHPUnit\Framework\TestCase; |
38
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
39
|
|
|
use AdminBundle\Controller\DefaultController; |
40
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
41
|
|
|
use Symfony\Component\HttpFoundation\Request; |
42
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
43
|
|
|
use Symfony\Component\HttpFoundation\Response; |
44
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Class DefaultControllerTest |
48
|
|
|
*/ |
49
|
|
|
class DefaultControllerTest extends TestCase |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* @var DefaultController |
53
|
|
|
*/ |
54
|
|
|
private $controller; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $twineDirectory; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|EngineInterface |
63
|
|
|
*/ |
64
|
|
|
private $templatingMock; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|UserRepository |
68
|
|
|
*/ |
69
|
|
|
private $userRepositoryMock; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|LicenseeRepository |
73
|
|
|
*/ |
74
|
|
|
private $licenseeRepositoryMock; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|TopicRepository |
78
|
|
|
*/ |
79
|
|
|
private $topicRepositoryMock; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ImportfileRepositoryInterface |
83
|
|
|
*/ |
84
|
|
|
private $importfileRepositoryMock; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|TextNodeRepository |
88
|
|
|
*/ |
89
|
|
|
private $textnodeRepositoryMock; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|UserPasswordEncoder |
93
|
|
|
*/ |
94
|
|
|
private $userPasswordEncoderMock; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var string |
98
|
|
|
*/ |
99
|
|
|
private $topicImageDirectory; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|Filesystem |
103
|
|
|
*/ |
104
|
|
|
private $filesystemMock; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function setUp(): void |
110
|
|
|
{ |
111
|
|
|
$this->twineDirectory = '/tmp/twineDirectory'; |
112
|
|
|
$this->templatingMock = $this->createTemplatingMock(); |
113
|
|
|
$this->userRepositoryMock = $this->createUserRepositoryMock(); |
114
|
|
|
$this->licenseeRepositoryMock = $this->createLicenseeRepositoryMock(); |
115
|
|
|
$this->topicRepositoryMock = $this->createTopicRepositoryMock(); |
116
|
|
|
$this->importfileRepositoryMock = $this->createImportfileRepositoryMock(); |
117
|
|
|
$this->textnodeRepositoryMock = $this->createTextnodeRepositoryMock(); |
118
|
|
|
$this->userPasswordEncoderMock = $this->createUserPasswordEncoderMock(); |
119
|
|
|
$this->topicImageDirectory = '/tmp/topicImageDirectory'; |
120
|
|
|
$this->filesystemMock = $this->createFilesystemMock(); |
121
|
|
|
|
122
|
|
|
$this->controller = new DefaultController( |
123
|
|
|
$this->templatingMock, |
124
|
|
|
$this->userRepositoryMock, |
125
|
|
|
$this->licenseeRepositoryMock, |
126
|
|
|
$this->topicRepositoryMock, |
127
|
|
|
$this->importfileRepositoryMock, |
128
|
|
|
$this->userPasswordEncoderMock, |
129
|
|
|
$this->twineDirectory, |
130
|
|
|
$this->topicImageDirectory, |
131
|
|
|
$this->filesystemMock |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* tests the index action |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function testIndexAction() |
140
|
|
|
{ |
141
|
|
|
$this->templatingMock->expects(self::once()) |
142
|
|
|
->method('renderResponse') |
143
|
|
|
->willReturnCallback(function (string $template, array $arguments): Response { |
144
|
|
|
self::assertEquals('AdminBundle::index.html.twig', $template); |
145
|
|
|
self::assertArrayHasKey('mainMenuData', $arguments); |
146
|
|
|
|
147
|
|
|
return new Response(); |
148
|
|
|
}); |
149
|
|
|
self::assertInstanceOf(Response::class, $this->controller->indexAction()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function testFormsaveActionForInvalidFormType(): void |
156
|
|
|
{ |
157
|
|
|
$params = [ |
158
|
|
|
'formtype' => 'invalid', |
159
|
|
|
'id' => 'someId', |
160
|
|
|
]; |
161
|
|
|
$requestMock = $this->createRequestMock($params); |
|
|
|
|
162
|
|
|
$result = $this->controller->formsaveAction($requestMock); |
163
|
|
|
self::assertInstanceOf(Response::class, $result); |
164
|
|
|
$json = $result->getContent(); |
165
|
|
|
self::assertJsonStringEqualsJsonString( |
166
|
|
|
'{"error":true}', |
167
|
|
|
$json |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function testFormsaveActionForMissingId(): void |
175
|
|
|
{ |
176
|
|
|
$params = [ |
177
|
|
|
'formtype' => 'user', |
178
|
|
|
]; |
179
|
|
|
$requestMock = $this->createRequestMock($params); |
|
|
|
|
180
|
|
|
$result = $this->controller->formsaveAction($requestMock); |
181
|
|
|
self::assertInstanceOf(Response::class, $result); |
182
|
|
|
$json = $result->getContent(); |
183
|
|
|
self::assertJsonStringEqualsJsonString( |
184
|
|
|
'{"error":true}', |
185
|
|
|
$json |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function testFormsaveActionForNewUser(): void |
193
|
|
|
{ |
194
|
|
|
$params = [ |
195
|
|
|
'formtype' => 'user', |
196
|
|
|
'id' => 'new', |
197
|
|
|
'email' => 'someEmail', |
198
|
|
|
'password' => 'somePassword', |
199
|
|
|
]; |
200
|
|
|
$requestMock = $this->createRequestMock($params); |
|
|
|
|
201
|
|
|
|
202
|
|
|
$this->userRepositoryMock->expects(self::once()) |
|
|
|
|
203
|
|
|
->method('getClassName') |
204
|
|
|
->willReturn(User::class); |
205
|
|
|
$this->userRepositoryMock->expects(self::once()) |
|
|
|
|
206
|
|
|
->method('save') |
207
|
|
|
->willReturnCallback(function (User $user) { |
208
|
|
|
self::assertEquals('someEmail', $user->getEmail()); |
209
|
|
|
self::assertEquals('somePasswordEncoded', $user->getPassword()); |
210
|
|
|
$user->setId('someId'); |
211
|
|
|
}); |
212
|
|
|
$this->userPasswordEncoderMock->expects(self::once()) |
213
|
|
|
->method('encodePassword') |
214
|
|
|
->with(self::anything(), 'somePassword') |
215
|
|
|
->willReturn('somePasswordEncoded'); |
216
|
|
|
|
217
|
|
|
$result = $this->controller->formsaveAction($requestMock); |
218
|
|
|
|
219
|
|
|
self::assertInstanceOf(Response::class, $result); |
220
|
|
|
$json = $result->getContent(); |
221
|
|
|
self::assertJsonStringEqualsJsonString( |
222
|
|
|
'{"error":false,"newId":"someId"}', |
223
|
|
|
$json |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return void |
229
|
|
|
*/ |
230
|
|
|
public function testFormsaveActionForExistingTopic(): void |
231
|
|
|
{ |
232
|
|
|
$params = [ |
233
|
|
|
'formtype' => 'topic', |
234
|
|
|
'id' => 'someId', |
235
|
|
|
'imageFileName' => 'someImageFileName', |
236
|
|
|
'originalImageName' => 'someOriginalImageFileName', |
237
|
|
|
]; |
238
|
|
|
$requestMock = $this->createRequestMock($params); |
|
|
|
|
239
|
|
|
|
240
|
|
|
$topicMock = $this->createMock(Topic::class); |
241
|
|
|
$topicMock->expects(self::any()) |
242
|
|
|
->method('getId') |
243
|
|
|
->willReturn('someId'); |
244
|
|
|
|
245
|
|
|
$this->topicRepositoryMock->expects(self::never()) |
|
|
|
|
246
|
|
|
->method('getClassName'); |
247
|
|
|
$this->topicRepositoryMock->expects(self::once()) |
|
|
|
|
248
|
|
|
->method('find') |
249
|
|
|
->with('someId') |
250
|
|
|
->willReturn($topicMock); |
251
|
|
|
$this->topicRepositoryMock->expects(self::exactly(2)) |
|
|
|
|
252
|
|
|
->method('save') |
253
|
|
|
->with($topicMock); |
254
|
|
|
$this->userPasswordEncoderMock->expects(self::never()) |
255
|
|
|
->method('encodePassword'); |
256
|
|
|
|
257
|
|
|
$targetFileName = $this->topicImageDirectory.'someId/someOriginalImageFileName'; |
258
|
|
|
$this->filesystemMock->expects(self::once()) |
259
|
|
|
->method('rename') |
260
|
|
|
->with(self::anything(), $targetFileName); |
261
|
|
|
|
262
|
|
|
$result = $this->controller->formsaveAction($requestMock); |
263
|
|
|
|
264
|
|
|
self::assertInstanceOf(Response::class, $result); |
265
|
|
|
$json = $result->getContent(); |
266
|
|
|
self::assertJsonStringEqualsJsonString( |
267
|
|
|
'{"error":false,"newId":"someId"}', |
268
|
|
|
$json |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
|
|
public function testFormsaveActionForExistingLicensee(): void |
276
|
|
|
{ |
277
|
|
|
$params = [ |
278
|
|
|
'formtype' => 'licensee', |
279
|
|
|
'id' => 'someId', |
280
|
|
|
]; |
281
|
|
|
$requestMock = $this->createRequestMock($params); |
|
|
|
|
282
|
|
|
|
283
|
|
|
$licenseeMock = $this->createMock(Licensee::class); |
284
|
|
|
$licenseeMock->expects(self::any()) |
285
|
|
|
->method('getId') |
286
|
|
|
->willReturn('someId'); |
287
|
|
|
|
288
|
|
|
$this->licenseeRepositoryMock->expects(self::never()) |
|
|
|
|
289
|
|
|
->method('getClassName'); |
290
|
|
|
$this->licenseeRepositoryMock->expects(self::once()) |
|
|
|
|
291
|
|
|
->method('find') |
292
|
|
|
->with('someId') |
293
|
|
|
->willReturn($licenseeMock); |
294
|
|
|
$this->licenseeRepositoryMock->expects(self::once()) |
|
|
|
|
295
|
|
|
->method('save') |
296
|
|
|
->with($licenseeMock); |
297
|
|
|
|
298
|
|
|
$result = $this->controller->formsaveAction($requestMock); |
299
|
|
|
|
300
|
|
|
self::assertInstanceOf(Response::class, $result); |
301
|
|
|
$json = $result->getContent(); |
302
|
|
|
self::assertJsonStringEqualsJsonString( |
303
|
|
|
'{"error":false,"newId":"someId"}', |
304
|
|
|
$json |
305
|
|
|
); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @return void |
310
|
|
|
*/ |
311
|
|
|
public function testFormsaveActionForExistingImportfile(): void |
312
|
|
|
{ |
313
|
|
|
$params = [ |
314
|
|
|
'formtype' => 'importfile', |
315
|
|
|
'id' => 'someId', |
316
|
|
|
'filename' => 'someFilename', |
317
|
|
|
'orgname' => 'someOrgName', |
318
|
|
|
]; |
319
|
|
|
$requestMock = $this->createRequestMock($params); |
|
|
|
|
320
|
|
|
|
321
|
|
|
$importfileMock = $this->createMock(Importfile::class); |
322
|
|
|
$importfileMock->expects(self::any()) |
323
|
|
|
->method('getId') |
324
|
|
|
->willReturn('someId'); |
325
|
|
|
|
326
|
|
|
$this->importfileRepositoryMock->expects(self::never()) |
327
|
|
|
->method('getClassName'); |
328
|
|
|
$this->importfileRepositoryMock->expects(self::once()) |
329
|
|
|
->method('find') |
330
|
|
|
->with('someId') |
331
|
|
|
->willReturn($importfileMock); |
332
|
|
|
$this->importfileRepositoryMock->expects(self::exactly(2)) |
333
|
|
|
->method('save') |
334
|
|
|
->with($importfileMock); |
335
|
|
|
|
336
|
|
|
$this->filesystemMock->expects(self::once()) |
337
|
|
|
->method('exists') |
338
|
|
|
->willReturn(true); |
339
|
|
|
|
340
|
|
|
$result = $this->controller->formsaveAction($requestMock); |
341
|
|
|
|
342
|
|
|
self::assertInstanceOf(Response::class, $result); |
343
|
|
|
$json = $result->getContent(); |
344
|
|
|
self::assertJsonStringEqualsJsonString( |
345
|
|
|
'{"error":false,"newId":"someId"}', |
346
|
|
|
$json |
347
|
|
|
); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|LicenseeRepository |
352
|
|
|
*/ |
353
|
|
|
private function createLicenseeRepositoryMock(): LicenseeRepository |
354
|
|
|
{ |
355
|
|
|
return $this->createMock(LicenseeRepository::class); |
|
|
|
|
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ImportfileRepository |
360
|
|
|
*/ |
361
|
|
|
private function createImportfileRepositoryMock(): ImportfileRepository |
362
|
|
|
{ |
363
|
|
|
return $this->createMock(ImportfileRepository::class); |
|
|
|
|
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|EngineInterface |
368
|
|
|
*/ |
369
|
|
|
private function createTemplatingMock(): EngineInterface |
370
|
|
|
{ |
371
|
|
|
return $this->createMock(EngineInterface::class); |
|
|
|
|
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return UserRepository|\PHPUnit_Framework_MockObject_MockObject |
376
|
|
|
*/ |
377
|
|
|
private function createUserRepositoryMock(): UserRepository |
378
|
|
|
{ |
379
|
|
|
return $this->createMock(UserRepository::class); |
|
|
|
|
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @return TopicRepository|\PHPUnit_Framework_MockObject_MockObject |
384
|
|
|
*/ |
385
|
|
|
private function createTopicRepositoryMock(): TopicRepository |
386
|
|
|
{ |
387
|
|
|
return $this->createMock(TopicRepository::class); |
|
|
|
|
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @return TextNodeRepository|\PHPUnit_Framework_MockObject_MockObject |
392
|
|
|
*/ |
393
|
|
|
private function createTextnodeRepositoryMock(): TextNodeRepository |
394
|
|
|
{ |
395
|
|
|
return $this->createMock(TextNodeRepository::class); |
|
|
|
|
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @return UserPasswordEncoder|\PHPUnit_Framework_MockObject_MockObject |
400
|
|
|
*/ |
401
|
|
|
private function createUserPasswordEncoderMock(): UserPasswordEncoder |
402
|
|
|
{ |
403
|
|
|
return $this->createMock(UserPasswordEncoder::class); |
|
|
|
|
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @param \PHPUnit_Framework_MockObject_MockObject|Request $params |
408
|
|
|
* |
409
|
|
|
* @return Request |
410
|
|
|
*/ |
411
|
|
|
private function createRequestMock($params): Request |
412
|
|
|
{ |
413
|
|
|
$parameterBagMock = $this->createMock(ParameterBag::class); |
414
|
|
|
$parameterBagMock->expects(self::any()) |
415
|
|
|
->method('all') |
416
|
|
|
->willReturn($params); |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @var $mock \PHPUnit_Framework_MockObject_MockObject|Request |
420
|
|
|
*/ |
421
|
|
|
$mock = $this->createMock(Request::class); |
422
|
|
|
$mock->request = $parameterBagMock; |
|
|
|
|
423
|
|
|
|
424
|
|
|
return $mock; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Filesystem |
429
|
|
|
*/ |
430
|
|
|
private function createFilesystemMock(): Filesystem |
431
|
|
|
{ |
432
|
|
|
return $this->createMock(Filesystem::class); |
|
|
|
|
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|