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\SetObjectFieldValueAction; |
19
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
20
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
21
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
22
|
|
|
use Sonata\AdminBundle\Form\DataTransformerResolver; |
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\Form\CallbackTransformer; |
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
30
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
31
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
32
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
33
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
34
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
35
|
|
|
use Twig\Environment; |
36
|
|
|
use Twig\Loader\ArrayLoader; |
37
|
|
|
|
38
|
|
|
final class SetObjectFieldValueActionTest extends TestCase |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var Pool |
42
|
|
|
*/ |
43
|
|
|
private $pool; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Environment |
47
|
|
|
*/ |
48
|
|
|
private $twig; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var SetObjectFieldValueAction |
52
|
|
|
*/ |
53
|
|
|
private $action; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var AbstractAdmin |
57
|
|
|
*/ |
58
|
|
|
private $admin; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var ValidatorInterface |
62
|
|
|
*/ |
63
|
|
|
private $validator; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var ModelManagerInterface |
67
|
|
|
*/ |
68
|
|
|
private $modelManager; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var DataTransformerResolver |
72
|
|
|
*/ |
73
|
|
|
private $resolver; |
74
|
|
|
|
75
|
|
|
protected function setUp(): void |
76
|
|
|
{ |
77
|
|
|
$this->twig = new Environment(new ArrayLoader([ |
78
|
|
|
'admin_template' => 'renderedTemplate', |
79
|
|
|
'field_template' => 'renderedTemplate', |
80
|
|
|
])); |
81
|
|
|
$this->pool = $this->prophesize(Pool::class); |
82
|
|
|
$this->admin = $this->prophesize(AbstractAdmin::class); |
83
|
|
|
$this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal()); |
84
|
|
|
$this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled(); |
85
|
|
|
$this->validator = $this->prophesize(ValidatorInterface::class); |
86
|
|
|
$this->modelManager = $this->prophesize(ModelManagerInterface::class); |
87
|
|
|
$this->resolver = new DataTransformerResolver(); |
88
|
|
|
$this->action = new SetObjectFieldValueAction( |
89
|
|
|
$this->twig, |
90
|
|
|
$this->pool->reveal(), |
91
|
|
|
$this->validator->reveal(), |
92
|
|
|
$this->resolver |
93
|
|
|
); |
94
|
|
|
$this->admin->getModelManager()->willReturn($this->modelManager->reveal()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testSetObjectFieldValueAction(): void |
98
|
|
|
{ |
99
|
|
|
$object = new Foo(); |
100
|
|
|
$request = new Request([ |
101
|
|
|
'code' => 'sonata.post.admin', |
102
|
|
|
'objectId' => 42, |
103
|
|
|
'field' => 'enabled', |
104
|
|
|
'value' => 1, |
105
|
|
|
'context' => 'list', |
106
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
107
|
|
|
|
108
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
109
|
|
|
$pool = $this->prophesize(Pool::class); |
110
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
111
|
|
|
$propertyAccessor = new PropertyAccessor(); |
112
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
113
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
114
|
|
|
|
115
|
|
|
$this->admin->getObject(42)->willReturn($object); |
116
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
117
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
118
|
|
|
$this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
119
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
120
|
|
|
// NEXT_MAJOR: Remove this line |
121
|
|
|
$this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
|
|
|
|
122
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
123
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
124
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
125
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
126
|
|
|
$pool->reveal(), |
127
|
|
|
null, |
128
|
|
|
$translator->reveal(), |
129
|
|
|
$container->reveal() |
130
|
|
|
)); |
131
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
132
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
133
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
134
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
135
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
136
|
|
|
$fieldDescription->getOption('data_transformer')->willReturn(null); |
137
|
|
|
|
138
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$response = ($this->action)($request); |
141
|
|
|
|
142
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getTimeZones(): iterable |
146
|
|
|
{ |
147
|
|
|
$default = new \DateTimeZone(date_default_timezone_get()); |
148
|
|
|
$custom = new \DateTimeZone('Europe/Rome'); |
149
|
|
|
|
150
|
|
|
return [ |
151
|
|
|
'empty timezone' => [null, $default], |
152
|
|
|
'disabled timezone' => [false, $default], |
153
|
|
|
'default timezone by name' => [$default->getName(), $default], |
154
|
|
|
'default timezone by object' => [$default, $default], |
155
|
|
|
'custom timezone by name' => [$custom->getName(), $custom], |
156
|
|
|
'custom timezone by object' => [$custom, $custom], |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @dataProvider getTimeZones |
162
|
|
|
*/ |
163
|
|
|
public function testSetObjectFieldValueActionWithDate($timezone, \DateTimeZone $expectedTimezone): void |
164
|
|
|
{ |
165
|
|
|
$object = new Bafoo(); |
166
|
|
|
$request = new Request([ |
167
|
|
|
'code' => 'sonata.post.admin', |
168
|
|
|
'objectId' => 42, |
169
|
|
|
'field' => 'dateProp', |
170
|
|
|
'value' => '2020-12-12', |
171
|
|
|
'context' => 'list', |
172
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
173
|
|
|
|
174
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
175
|
|
|
$pool = $this->prophesize(Pool::class); |
176
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
177
|
|
|
$propertyAccessor = new PropertyAccessor(); |
178
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
179
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
180
|
|
|
|
181
|
|
|
$this->admin->getObject(42)->willReturn($object); |
182
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
183
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
184
|
|
|
$this->admin->getListFieldDescription('dateProp')->willReturn($fieldDescription->reveal()); |
|
|
|
|
185
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
186
|
|
|
|
187
|
|
|
$this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
|
|
|
|
188
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
189
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
190
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
191
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
192
|
|
|
$pool->reveal(), |
193
|
|
|
null, |
194
|
|
|
$translator->reveal(), |
195
|
|
|
$container->reveal() |
196
|
|
|
)); |
197
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
198
|
|
|
$fieldDescription->getOption('timezone')->willReturn($timezone); |
199
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
200
|
|
|
$fieldDescription->getType()->willReturn('date'); |
201
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
202
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
203
|
|
|
$fieldDescription->getOption('data_transformer')->willReturn(null); |
204
|
|
|
|
205
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
206
|
|
|
|
207
|
|
|
$response = ($this->action)($request); |
208
|
|
|
|
209
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
210
|
|
|
|
211
|
|
|
$defaultTimezone = new \DateTimeZone(date_default_timezone_get()); |
212
|
|
|
$expectedDate = new \DateTime($request->query->get('value'), $expectedTimezone); |
213
|
|
|
$expectedDate->setTimezone($defaultTimezone); |
214
|
|
|
|
215
|
|
|
$this->assertInstanceOf(\DateTime::class, $object->getDateProp()); |
216
|
|
|
$this->assertSame($expectedDate->format('Y-m-d'), $object->getDateProp()->format('Y-m-d')); |
217
|
|
|
$this->assertSame($defaultTimezone->getName(), $object->getDateProp()->getTimezone()->getName()); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testSetObjectFieldValueActionOnARelationField(): void |
221
|
|
|
{ |
222
|
|
|
$object = new Baz(); |
223
|
|
|
$associationObject = new Bar(); |
224
|
|
|
$request = new Request([ |
225
|
|
|
'code' => 'sonata.post.admin', |
226
|
|
|
'objectId' => 42, |
227
|
|
|
'field' => 'bar', |
228
|
|
|
'value' => 1, |
229
|
|
|
'context' => 'list', |
230
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
231
|
|
|
|
232
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::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
|
|
|
// NEXT_MAJOR: Remove this line |
246
|
|
|
$this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
|
|
|
|
247
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
248
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
249
|
|
|
$this->pool->reveal(), |
|
|
|
|
250
|
|
|
null, |
251
|
|
|
$translator->reveal(), |
252
|
|
|
$container->reveal() |
253
|
|
|
)); |
254
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
255
|
|
|
$fieldDescription->getType()->willReturn('choice'); |
256
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
257
|
|
|
$fieldDescription->getOption('class')->willReturn(Bar::class); |
258
|
|
|
$fieldDescription->getTargetModel()->willReturn(Bar::class); |
259
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
260
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
261
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
262
|
|
|
$fieldDescription->getOption('data_transformer')->willReturn(null); |
263
|
|
|
$this->modelManager->find(\get_class($associationObject), 1)->willReturn($associationObject); |
264
|
|
|
|
265
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
266
|
|
|
|
267
|
|
|
$response = ($this->action)($request); |
268
|
|
|
|
269
|
|
|
$this->assertSame($associationObject, $object->getBar()); |
270
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function testSetObjectFieldValueActionWithViolations(): void |
274
|
|
|
{ |
275
|
|
|
$bar = new Bar(); |
276
|
|
|
$object = new Baz(); |
277
|
|
|
$object->setBar($bar); |
278
|
|
|
$request = new Request([ |
279
|
|
|
'code' => 'sonata.post.admin', |
280
|
|
|
'objectId' => 42, |
281
|
|
|
'field' => 'bar.enabled', |
282
|
|
|
'value' => 1, |
283
|
|
|
'context' => 'list', |
284
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
285
|
|
|
|
286
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
287
|
|
|
$propertyAccessor = new PropertyAccessor(); |
288
|
|
|
|
289
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
290
|
|
|
$this->admin->getObject(42)->willReturn($object); |
291
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
292
|
|
|
$this->admin->getListFieldDescription('bar.enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
293
|
|
|
$this->validator->validate($bar)->willReturn(new ConstraintViolationList([ |
|
|
|
|
294
|
|
|
new ConstraintViolation('error1', null, [], null, 'enabled', null), |
295
|
|
|
new ConstraintViolation('error2', null, [], null, 'enabled', null), |
296
|
|
|
])); |
297
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
298
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
299
|
|
|
$fieldDescription->getOption('data_transformer')->willReturn(null); |
300
|
|
|
|
301
|
|
|
$response = ($this->action)($request); |
302
|
|
|
|
303
|
|
|
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
304
|
|
|
$this->assertSame(json_encode("error1\nerror2"), $response->getContent()); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function testSetObjectFieldEditableMultipleValue(): void |
308
|
|
|
{ |
309
|
|
|
$object = new StatusMultiple(); |
310
|
|
|
$request = new Request([ |
311
|
|
|
'code' => 'sonata.post.admin', |
312
|
|
|
'objectId' => 42, |
313
|
|
|
'field' => 'status', |
314
|
|
|
'value' => [1, 2], |
315
|
|
|
'context' => 'list', |
316
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
317
|
|
|
|
318
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
319
|
|
|
$pool = $this->prophesize(Pool::class); |
320
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
321
|
|
|
$propertyAccessor = new PropertyAccessor(); |
322
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
323
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
324
|
|
|
|
325
|
|
|
$this->admin->getObject(42)->willReturn($object); |
326
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
327
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
328
|
|
|
$this->admin->getListFieldDescription('status')->willReturn($fieldDescription->reveal()); |
|
|
|
|
329
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
330
|
|
|
// NEXT_MAJOR: Remove this line |
331
|
|
|
$this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
|
|
|
|
332
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
333
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
334
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
335
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
336
|
|
|
$pool->reveal(), |
337
|
|
|
null, |
338
|
|
|
$translator->reveal(), |
339
|
|
|
$container->reveal() |
340
|
|
|
)); |
341
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
342
|
|
|
$fieldDescription->getOption('multiple')->willReturn(true); |
343
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
344
|
|
|
$fieldDescription->getType()->willReturn(null); |
345
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
346
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn(['some value']); |
347
|
|
|
$fieldDescription->getOption('data_transformer')->willReturn(null); |
348
|
|
|
|
349
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
350
|
|
|
|
351
|
|
|
$response = ($this->action)($request); |
352
|
|
|
|
353
|
|
|
$this->assertSame([1, 2], $object->status); |
354
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
public function testSetObjectFieldTransformed(): void |
358
|
|
|
{ |
359
|
|
|
$object = new Foo(); |
360
|
|
|
$request = new Request([ |
361
|
|
|
'code' => 'sonata.post.admin', |
362
|
|
|
'objectId' => 42, |
363
|
|
|
'field' => 'enabled', |
364
|
|
|
'value' => 'yes', |
365
|
|
|
'context' => 'list', |
366
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
367
|
|
|
|
368
|
|
|
$dataTransformer = new CallbackTransformer(static function ($value): string { |
369
|
|
|
return (string) (int) $value; |
370
|
|
|
}, static function ($value): bool { |
371
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
372
|
|
|
}); |
373
|
|
|
|
374
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
375
|
|
|
$pool = $this->prophesize(Pool::class); |
376
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
377
|
|
|
$propertyAccessor = new PropertyAccessor(); |
378
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
379
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
380
|
|
|
|
381
|
|
|
$this->admin->getObject(42)->willReturn($object); |
382
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
383
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
384
|
|
|
$this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
385
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
386
|
|
|
// NEXT_MAJOR: Remove this line |
387
|
|
|
$this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
|
|
|
|
388
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
389
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
390
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
391
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
392
|
|
|
$pool->reveal(), |
393
|
|
|
null, |
394
|
|
|
$translator->reveal(), |
395
|
|
|
$container->reveal() |
396
|
|
|
)); |
397
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
398
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
399
|
|
|
$fieldDescription->getType()->willReturn(null); |
400
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
401
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
402
|
|
|
$fieldDescription->getOption('data_transformer')->willReturn($dataTransformer); |
403
|
|
|
|
404
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
405
|
|
|
|
406
|
|
|
$response = ($this->action)($request); |
407
|
|
|
|
408
|
|
|
$this->assertTrue($object->getEnabled()); |
409
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
public function testSetObjectFieldOverrideTransformer(): void |
413
|
|
|
{ |
414
|
|
|
$object = new Foo(); |
415
|
|
|
$request = new Request([ |
416
|
|
|
'code' => 'sonata.post.admin', |
417
|
|
|
'objectId' => 42, |
418
|
|
|
'field' => 'enabled', |
419
|
|
|
'value' => 'yes', |
420
|
|
|
'context' => 'list', |
421
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
422
|
|
|
|
423
|
|
|
$isOverridden = false; |
424
|
|
|
$dataTransformer = new CallbackTransformer(static function ($value): string { |
425
|
|
|
return (string) (int) $value; |
426
|
|
|
}, static function ($value) use (&$isOverridden): bool { |
427
|
|
|
$isOverridden = true; |
428
|
|
|
|
429
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
430
|
|
|
}); |
431
|
|
|
|
432
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
433
|
|
|
$pool = $this->prophesize(Pool::class); |
434
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
435
|
|
|
$propertyAccessor = new PropertyAccessor(); |
436
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
437
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
438
|
|
|
|
439
|
|
|
$this->admin->getObject(42)->willReturn($object); |
440
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
441
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
442
|
|
|
$this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
443
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
444
|
|
|
// NEXT_MAJOR: Remove this line |
445
|
|
|
$this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
|
|
|
|
446
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
447
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
448
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
449
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
450
|
|
|
$pool->reveal(), |
451
|
|
|
null, |
452
|
|
|
$translator->reveal(), |
453
|
|
|
$container->reveal() |
454
|
|
|
)); |
455
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
456
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
457
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
458
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
459
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
460
|
|
|
$fieldDescription->getOption('data_transformer')->willReturn($dataTransformer); |
461
|
|
|
|
462
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
463
|
|
|
|
464
|
|
|
$response = ($this->action)($request); |
465
|
|
|
|
466
|
|
|
$this->assertTrue($object->getEnabled()); |
467
|
|
|
$this->assertTrue($isOverridden); |
468
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.