1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Tests\Action; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Prophecy\Argument; |
18
|
|
|
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction; |
19
|
|
|
use Sonata\AdminBundle\Action\SetObjectFieldValueAction; |
20
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
21
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
22
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
23
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
24
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistryInterface; |
25
|
|
|
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension; |
26
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
27
|
|
|
use Symfony\Component\HttpFoundation\Request; |
28
|
|
|
use Symfony\Component\HttpFoundation\Response; |
29
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
30
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
31
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
32
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
33
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
34
|
|
|
use Twig\Environment; |
35
|
|
|
use Twig\Loader\ArrayLoader; |
36
|
|
|
|
37
|
|
|
final class SetObjectFieldValueActionTest extends TestCase |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var Pool |
41
|
|
|
*/ |
42
|
|
|
private $pool; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Environment |
46
|
|
|
*/ |
47
|
|
|
private $twig; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var GetShortObjectDescriptionAction |
51
|
|
|
*/ |
52
|
|
|
private $action; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var AbstractAdmin |
56
|
|
|
*/ |
57
|
|
|
private $admin; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var ValidatorInterface |
61
|
|
|
*/ |
62
|
|
|
private $validator; |
63
|
|
|
|
64
|
|
|
protected function setUp(): void |
65
|
|
|
{ |
66
|
|
|
$this->twig = new Environment(new ArrayLoader([ |
67
|
|
|
'admin_template' => 'renderedTemplate', |
68
|
|
|
'field_template' => 'renderedTemplate', |
69
|
|
|
])); |
70
|
|
|
$this->pool = $this->prophesize(Pool::class); |
71
|
|
|
$this->admin = $this->prophesize(AbstractAdmin::class); |
72
|
|
|
$this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal()); |
73
|
|
|
$this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled(); |
74
|
|
|
$this->validator = $this->prophesize(ValidatorInterface::class); |
75
|
|
|
$this->action = new SetObjectFieldValueAction( |
|
|
|
|
76
|
|
|
$this->twig, |
77
|
|
|
$this->pool->reveal(), |
78
|
|
|
$this->validator->reveal() |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testSetObjectFieldValueAction(): void |
83
|
|
|
{ |
84
|
|
|
$object = new Foo(); |
85
|
|
|
$request = new Request([ |
86
|
|
|
'code' => 'sonata.post.admin', |
87
|
|
|
'objectId' => 42, |
88
|
|
|
'field' => 'enabled', |
89
|
|
|
'value' => 1, |
90
|
|
|
'context' => 'list', |
91
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
92
|
|
|
|
93
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
94
|
|
|
$pool = $this->prophesize(Pool::class); |
95
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
96
|
|
|
$propertyAccessor = new PropertyAccessor(); |
97
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
98
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
99
|
|
|
|
100
|
|
|
$this->admin->getObject(42)->willReturn($object); |
101
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
102
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
103
|
|
|
$this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
104
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
105
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
106
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
107
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
108
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
109
|
|
|
$pool->reveal(), |
110
|
|
|
null, |
111
|
|
|
$translator->reveal(), |
112
|
|
|
$container->reveal() |
113
|
|
|
)); |
114
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
115
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
116
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
117
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
118
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
119
|
|
|
|
120
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
121
|
|
|
|
122
|
|
|
$response = ($this->action)($request); |
123
|
|
|
|
124
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testSetObjectFieldValueActionWithDate(): void |
128
|
|
|
{ |
129
|
|
|
$object = new Bafoo(); |
130
|
|
|
$request = new Request([ |
131
|
|
|
'code' => 'sonata.post.admin', |
132
|
|
|
'objectId' => 42, |
133
|
|
|
'field' => 'dateProp', |
134
|
|
|
'value' => '2020-12-12', |
135
|
|
|
'context' => 'list', |
136
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
137
|
|
|
|
138
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
139
|
|
|
$pool = $this->prophesize(Pool::class); |
140
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
141
|
|
|
$propertyAccessor = new PropertyAccessor(); |
142
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
143
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
144
|
|
|
|
145
|
|
|
$this->admin->getObject(42)->willReturn($object); |
146
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
147
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
148
|
|
|
$this->admin->getListFieldDescription('dateProp')->willReturn($fieldDescription->reveal()); |
|
|
|
|
149
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
150
|
|
|
|
151
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
152
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
153
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
154
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
155
|
|
|
$pool->reveal(), |
156
|
|
|
null, |
157
|
|
|
$translator->reveal(), |
158
|
|
|
$container->reveal() |
159
|
|
|
)); |
160
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
161
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
162
|
|
|
$fieldDescription->getType()->willReturn('date'); |
163
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
164
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
165
|
|
|
|
166
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
167
|
|
|
|
168
|
|
|
$response = ($this->action)($request); |
169
|
|
|
|
170
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testSetObjectFieldValueActionWithDateTime(): void |
174
|
|
|
{ |
175
|
|
|
$object = new Bafoo(); |
176
|
|
|
$request = new Request([ |
177
|
|
|
'code' => 'sonata.post.admin', |
178
|
|
|
'objectId' => 42, |
179
|
|
|
'field' => 'datetimeProp', |
180
|
|
|
'value' => '2020-12-12 23:11:23', |
181
|
|
|
'context' => 'list', |
182
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
183
|
|
|
|
184
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
185
|
|
|
$pool = $this->prophesize(Pool::class); |
186
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
187
|
|
|
$propertyAccessor = new PropertyAccessor(); |
188
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
189
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
190
|
|
|
|
191
|
|
|
$this->admin->getObject(42)->willReturn($object); |
192
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
193
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
194
|
|
|
$this->admin->getListFieldDescription('datetimeProp')->willReturn($fieldDescription->reveal()); |
|
|
|
|
195
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
196
|
|
|
|
197
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
198
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
199
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
200
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
201
|
|
|
$pool->reveal(), |
202
|
|
|
null, |
203
|
|
|
$translator->reveal(), |
204
|
|
|
$container->reveal() |
205
|
|
|
)); |
206
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
207
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
208
|
|
|
$fieldDescription->getType()->willReturn('datetime'); |
209
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
210
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
211
|
|
|
|
212
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
213
|
|
|
|
214
|
|
|
$response = ($this->action)($request); |
215
|
|
|
|
216
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testSetObjectFieldValueActionOnARelationField(): void |
220
|
|
|
{ |
221
|
|
|
$object = new Baz(); |
222
|
|
|
$associationObject = new Bar(); |
223
|
|
|
$request = new Request([ |
224
|
|
|
'code' => 'sonata.post.admin', |
225
|
|
|
'objectId' => 42, |
226
|
|
|
'field' => 'bar', |
227
|
|
|
'value' => 1, |
228
|
|
|
'context' => 'list', |
229
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
230
|
|
|
|
231
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
232
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
233
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
234
|
|
|
$propertyAccessor = new PropertyAccessor(); |
235
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
236
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
237
|
|
|
|
238
|
|
|
$this->admin->getObject(42)->willReturn($object); |
239
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
240
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
241
|
|
|
$this->admin->getListFieldDescription('bar')->willReturn($fieldDescription->reveal()); |
|
|
|
|
242
|
|
|
$this->admin->getClass()->willReturn(\get_class($object)); |
|
|
|
|
243
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
244
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
245
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
246
|
|
|
$this->admin->getModelManager()->willReturn($modelManager->reveal()); |
|
|
|
|
247
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
248
|
|
|
$this->pool->reveal(), |
|
|
|
|
249
|
|
|
null, |
250
|
|
|
$translator->reveal(), |
251
|
|
|
$container->reveal() |
252
|
|
|
)); |
253
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
254
|
|
|
$fieldDescription->getType()->willReturn('choice'); |
255
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
256
|
|
|
$fieldDescription->getOption('class')->willReturn(Bar::class); |
257
|
|
|
$fieldDescription->getTargetEntity()->willReturn(Bar::class); |
258
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
259
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
260
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
261
|
|
|
$modelManager->find(\get_class($associationObject), 1)->willReturn($associationObject); |
262
|
|
|
|
263
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
264
|
|
|
|
265
|
|
|
$response = ($this->action)($request); |
266
|
|
|
|
267
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function testSetObjectFieldValueActionWithViolations(): void |
271
|
|
|
{ |
272
|
|
|
$bar = new Bar(); |
273
|
|
|
$object = new Baz(); |
274
|
|
|
$object->setBar($bar); |
275
|
|
|
$request = new Request([ |
276
|
|
|
'code' => 'sonata.post.admin', |
277
|
|
|
'objectId' => 42, |
278
|
|
|
'field' => 'bar.enabled', |
279
|
|
|
'value' => 1, |
280
|
|
|
'context' => 'list', |
281
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
282
|
|
|
|
283
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
284
|
|
|
$propertyAccessor = new PropertyAccessor(); |
285
|
|
|
|
286
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
287
|
|
|
$this->admin->getObject(42)->willReturn($object); |
288
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
289
|
|
|
$this->admin->getListFieldDescription('bar.enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
290
|
|
|
$this->validator->validate($bar)->willReturn(new ConstraintViolationList([ |
|
|
|
|
291
|
|
|
new ConstraintViolation('error1', null, [], null, 'enabled', null), |
292
|
|
|
new ConstraintViolation('error2', null, [], null, 'enabled', null), |
293
|
|
|
])); |
294
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
295
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
296
|
|
|
|
297
|
|
|
$response = ($this->action)($request); |
298
|
|
|
|
299
|
|
|
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
300
|
|
|
$this->assertSame(json_encode("error1\nerror2"), $response->getContent()); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function testSetObjectFieldEditableMultipleValue(): void |
304
|
|
|
{ |
305
|
|
|
$object = new StatusMultiple(); |
306
|
|
|
$request = new Request([ |
307
|
|
|
'code' => 'sonata.post.admin', |
308
|
|
|
'objectId' => 42, |
309
|
|
|
'field' => 'status', |
310
|
|
|
'value' => [1, 2], |
311
|
|
|
'context' => 'list', |
312
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
313
|
|
|
|
314
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
315
|
|
|
$pool = $this->prophesize(Pool::class); |
316
|
|
|
$template = $this->prophesize(Template::class); |
|
|
|
|
317
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
318
|
|
|
$propertyAccessor = new PropertyAccessor(); |
319
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
320
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
321
|
|
|
|
322
|
|
|
$this->admin->getObject(42)->willReturn($object); |
323
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
324
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
325
|
|
|
$this->admin->getListFieldDescription('status')->willReturn($fieldDescription->reveal()); |
|
|
|
|
326
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
327
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
328
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
329
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
330
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
331
|
|
|
$pool->reveal(), |
332
|
|
|
null, |
333
|
|
|
$translator->reveal(), |
334
|
|
|
$container->reveal() |
335
|
|
|
)); |
336
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
337
|
|
|
$fieldDescription->getOption('multiple')->willReturn(true); |
338
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
339
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
340
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
341
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn(['some value']); |
342
|
|
|
|
343
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
344
|
|
|
|
345
|
|
|
$response = ($this->action)($request); |
346
|
|
|
|
347
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..