1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivoaz ContentEditable bundle. |
5
|
|
|
* |
6
|
|
|
* (c) Ivo Azirjans <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivoaz\Bundle\ContentEditableBundle\Tests\Controller; |
13
|
|
|
|
14
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Controller\ContentController; |
15
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Entity\Content; |
16
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Form\Model\Batch; |
17
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Form\Model\Content as FormContent; |
18
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Manager\ContentManagerInterface; |
19
|
|
|
use Symfony\Component\Form\FormError; |
20
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
21
|
|
|
use Symfony\Component\Form\FormInterface; |
22
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
25
|
|
|
|
26
|
|
|
class ContentControllerTest extends \PHPUnit_Framework_TestCase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ContentManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
30
|
|
|
*/ |
31
|
|
|
private $manager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject |
35
|
|
|
*/ |
36
|
|
|
private $formFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var FormInterface|\PHPUnit_Framework_MockObject_MockObject |
40
|
|
|
*/ |
41
|
|
|
private $form; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var AuthorizationCheckerInterface|\PHPUnit_Framework_MockObject_MockObject |
45
|
|
|
*/ |
46
|
|
|
private $authorizationChecker; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ContentController |
50
|
|
|
*/ |
51
|
|
|
private $controller; |
52
|
|
|
|
53
|
|
|
public function setUp() |
54
|
|
|
{ |
55
|
|
|
$this->manager = $this->getMock(ContentManagerInterface::class); |
56
|
|
|
$this->form = $this->getMock(FormInterface::class); |
57
|
|
|
$this->formFactory = $this->getMock(FormFactoryInterface::class); |
58
|
|
|
$this->formFactory->method('create') |
59
|
|
|
->willReturn($this->form); |
60
|
|
|
$this->authorizationChecker = $this->getMock(AuthorizationCheckerInterface::class); |
61
|
|
|
|
62
|
|
|
$this->controller = new ContentController($this->manager, $this->formFactory, $this->authorizationChecker); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testUpdateActionReturnsErrorWhenContentNotFound() |
66
|
|
|
{ |
67
|
|
|
$this->setAuthorized(true); |
68
|
|
|
|
69
|
|
|
$this->manager->method('find') |
70
|
|
|
->with(1) |
71
|
|
|
->willReturn(null); |
72
|
|
|
|
73
|
|
|
$request = new Request([], [], ['id' => 1]); |
74
|
|
|
$response = $this->controller->updateAction($request); |
75
|
|
|
|
76
|
|
|
$expectedResponse = new JsonResponse( |
77
|
|
|
['errors' => [['title' => sprintf('Content with id "1" was not found.')]]], |
78
|
|
|
400 |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$this->assertEquals($expectedResponse, $response); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testUpdateActionReturnsFormErrors() |
85
|
|
|
{ |
86
|
|
|
$this->setAuthorized(true); |
87
|
|
|
|
88
|
|
|
$content = new Content(); |
89
|
|
|
|
90
|
|
|
$this->manager->method('find') |
91
|
|
|
->with(1) |
92
|
|
|
->willReturn($content); |
93
|
|
|
|
94
|
|
|
$this->form->method('isValid') |
95
|
|
|
->willReturn(false); |
96
|
|
|
|
97
|
|
|
$this->form->method('getErrors') |
98
|
|
|
->with(true, true) |
99
|
|
|
->willReturn( |
100
|
|
|
[ |
101
|
|
|
new FormError('Test error1'), |
102
|
|
|
new FormError('Test error2'), |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$request = new Request([], [], ['id' => 1]); |
107
|
|
|
$response = $this->controller->updateAction($request); |
108
|
|
|
|
109
|
|
|
$expectedResponse = new JsonResponse( |
110
|
|
|
['errors' => [['title' => 'Test error1'], ['title' => 'Test error2']]], |
111
|
|
|
400 |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$this->assertEquals($expectedResponse, $response); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testUpdateActionUpdatesContent() |
118
|
|
|
{ |
119
|
|
|
$this->setAuthorized(true); |
120
|
|
|
|
121
|
|
|
$content = new Content(); |
122
|
|
|
|
123
|
|
|
$this->manager->method('find') |
124
|
|
|
->with(1) |
125
|
|
|
->willReturn($content); |
126
|
|
|
|
127
|
|
|
$this->form->method('isValid') |
128
|
|
|
->willReturn(true); |
129
|
|
|
|
130
|
|
|
$this->form->method('getData') |
131
|
|
|
->willReturn(new FormContent()); |
132
|
|
|
|
133
|
|
|
$this->manager->expects($this->once()) |
134
|
|
|
->method('update') |
135
|
|
|
->with($content); |
136
|
|
|
|
137
|
|
|
$request = new Request([], [], ['id' => 1]); |
138
|
|
|
$response = $this->controller->updateAction($request); |
139
|
|
|
|
140
|
|
|
$expectedResponse = new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT); |
141
|
|
|
|
142
|
|
|
$this->assertEquals($expectedResponse, $response); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testBatchUpdateActionReturnsErrorWhenContentNotFound() |
146
|
|
|
{ |
147
|
|
|
$this->setAuthorized(true); |
148
|
|
|
|
149
|
|
|
$this->form->method('isValid') |
150
|
|
|
->willReturn(true); |
151
|
|
|
|
152
|
|
|
$this->form->method('getData') |
153
|
|
|
->willReturn($this->getBatch()); |
154
|
|
|
|
155
|
|
|
$this->manager->method('find') |
156
|
|
|
->willReturn(null); |
157
|
|
|
|
158
|
|
|
$response = $this->controller->batchUpdateAction(new Request()); |
159
|
|
|
|
160
|
|
|
$expectedResponse = new JsonResponse( |
161
|
|
|
[ |
162
|
|
|
'errors' => [ |
163
|
|
|
['title' => 'Content with id "1" was not found.'], |
164
|
|
|
['title' => 'Content with id "2" was not found.'], |
165
|
|
|
], |
166
|
|
|
], |
167
|
|
|
400 |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$this->assertEquals($expectedResponse, $response); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testBatchUpdateActionReturnsFormErrors() |
174
|
|
|
{ |
175
|
|
|
$this->setAuthorized(true); |
176
|
|
|
|
177
|
|
|
$this->form->method('isValid') |
178
|
|
|
->willReturn(false); |
179
|
|
|
|
180
|
|
|
$this->form->method('getErrors') |
181
|
|
|
->with(true, true) |
182
|
|
|
->willReturn( |
183
|
|
|
[ |
184
|
|
|
new FormError('Test error1'), |
185
|
|
|
new FormError('Test error2'), |
186
|
|
|
] |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$response = $this->controller->batchUpdateAction(new Request()); |
190
|
|
|
|
191
|
|
|
$expectedResponse = new JsonResponse( |
192
|
|
|
['errors' => [['title' => 'Test error1'], ['title' => 'Test error2']]], |
193
|
|
|
400 |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
$this->assertEquals($expectedResponse, $response); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testBatchUpdateActionUpdatesContent() |
200
|
|
|
{ |
201
|
|
|
$this->setAuthorized(true); |
202
|
|
|
|
203
|
|
|
$content1 = new Content(); |
204
|
|
|
$content2 = new Content(); |
205
|
|
|
|
206
|
|
|
$this->manager->method('find') |
207
|
|
|
->will($this->returnValueMap([[1, $content1], [2, $content2]])); |
208
|
|
|
|
209
|
|
|
$this->form->method('isValid') |
210
|
|
|
->willReturn(true); |
211
|
|
|
|
212
|
|
|
$this->form->method('getData') |
213
|
|
|
->willReturn($this->getBatch()); |
214
|
|
|
|
215
|
|
|
$this->manager->expects($this->exactly(2)) |
216
|
|
|
->method('update') |
217
|
|
|
->withConsecutive([$content1], [$content2]); |
218
|
|
|
|
219
|
|
|
$response = $this->controller->batchUpdateAction(new Request()); |
220
|
|
|
|
221
|
|
|
$expectedResponse = new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT); |
222
|
|
|
|
223
|
|
|
$this->assertEquals($expectedResponse, $response); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @dataProvider getActionIsForbiddenTestData |
228
|
|
|
* |
229
|
|
|
* @param string $method |
230
|
|
|
*/ |
231
|
|
|
public function testActionIsForbidden($method) |
232
|
|
|
{ |
233
|
|
|
$this->setAuthorized(false); |
234
|
|
|
|
235
|
|
|
$response = call_user_func([$this->controller, $method], new Request()); |
236
|
|
|
$expectedResponse = new JsonResponse(null, JsonResponse::HTTP_FORBIDDEN); |
237
|
|
|
|
238
|
|
|
$this->assertEquals($expectedResponse, $response, sprintf('Action "%s" should be forbidden.', $method)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return array |
243
|
|
|
*/ |
244
|
|
|
public function getActionIsForbiddenTestData() |
245
|
|
|
{ |
246
|
|
|
return [['updateAction'], ['batchUpdateAction']]; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param bool $authorized |
251
|
|
|
*/ |
252
|
|
|
private function setAuthorized($authorized) |
253
|
|
|
{ |
254
|
|
|
$this->authorizationChecker->method('isGranted') |
255
|
|
|
->with('ROLE_ADMIN') |
256
|
|
|
->willReturn($authorized); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return Batch |
261
|
|
|
*/ |
262
|
|
|
private function getBatch() |
263
|
|
|
{ |
264
|
|
|
$batch = new Batch(); |
265
|
|
|
$batch->contents[] = new FormContent(); |
266
|
|
|
$batch->contents[] = new FormContent(); |
267
|
|
|
$batch->contents[0]->id = 1; |
|
|
|
|
268
|
|
|
$batch->contents[1]->id = 2; |
269
|
|
|
$batch->contents[0]->text = 'Text 1'; |
270
|
|
|
$batch->contents[1]->text = 'Text 2'; |
271
|
|
|
|
272
|
|
|
return $batch; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.