|
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\Bridge\Twig\AppVariable; |
|
27
|
|
|
use Symfony\Bridge\Twig\Command\DebugCommand; |
|
28
|
|
|
use Symfony\Bridge\Twig\Extension\FormExtension; |
|
29
|
|
|
use Symfony\Bridge\Twig\Form\TwigRenderer; |
|
30
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
31
|
|
|
use Symfony\Component\Form\FormRenderer; |
|
32
|
|
|
use Symfony\Component\Form\FormRendererEngineInterface; |
|
33
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
34
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
|
35
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
36
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
|
37
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
|
38
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
39
|
|
|
use Twig\Environment; |
|
40
|
|
|
use Twig\Loader\ArrayLoader; |
|
41
|
|
|
use Twig\RuntimeLoader\FactoryRuntimeLoader; |
|
42
|
|
|
use Twig\Template; |
|
43
|
|
|
|
|
44
|
|
|
class Foo |
|
45
|
|
|
{ |
|
46
|
|
|
public function setEnabled($value): void |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
class Bar |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
public function setEnabled($value): void |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
class Baz |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
private $bar; |
|
61
|
|
|
|
|
62
|
|
|
public function setBar(Bar $bar): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->bar = $bar; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getBar() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->bar; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
final class SetObjectFieldValueActionTest extends TestCase |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
/** |
|
76
|
|
|
* @var Pool |
|
77
|
|
|
*/ |
|
78
|
|
|
private $pool; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var Environment |
|
82
|
|
|
*/ |
|
83
|
|
|
private $twig; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var GetShortObjectDescriptionAction |
|
87
|
|
|
*/ |
|
88
|
|
|
private $action; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @var AbstractAdmin |
|
92
|
|
|
*/ |
|
93
|
|
|
private $admin; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @var ValidatorInterface |
|
97
|
|
|
*/ |
|
98
|
|
|
private $validator; |
|
99
|
|
|
|
|
100
|
|
|
protected function setUp(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$this->twig = new Environment(new ArrayLoader([ |
|
103
|
|
|
'admin_template' => 'renderedTemplate', |
|
104
|
|
|
'field_template' => 'renderedTemplate', |
|
105
|
|
|
])); |
|
106
|
|
|
$this->pool = $this->prophesize(Pool::class); |
|
107
|
|
|
$this->admin = $this->prophesize(AbstractAdmin::class); |
|
108
|
|
|
$this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal()); |
|
109
|
|
|
$this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled(); |
|
110
|
|
|
$this->validator = $this->prophesize(ValidatorInterface::class); |
|
111
|
|
|
$this->action = new SetObjectFieldValueAction( |
|
|
|
|
|
|
112
|
|
|
$this->twig, |
|
113
|
|
|
$this->pool->reveal(), |
|
114
|
|
|
$this->validator->reveal() |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testSetObjectFieldValueAction(): void |
|
119
|
|
|
{ |
|
120
|
|
|
$object = new Foo(); |
|
121
|
|
|
$request = new Request([ |
|
122
|
|
|
'code' => 'sonata.post.admin', |
|
123
|
|
|
'objectId' => 42, |
|
124
|
|
|
'field' => 'enabled', |
|
125
|
|
|
'value' => 1, |
|
126
|
|
|
'context' => 'list', |
|
127
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
|
128
|
|
|
|
|
129
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
|
130
|
|
|
$pool = $this->prophesize(Pool::class); |
|
131
|
|
|
$template = $this->prophesize(Template::class); |
|
|
|
|
|
|
132
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
|
133
|
|
|
$propertyAccessor = new PropertyAccessor(); |
|
134
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
|
135
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
|
136
|
|
|
|
|
137
|
|
|
$this->admin->getObject(42)->willReturn($object); |
|
138
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
|
|
139
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
|
|
140
|
|
|
$this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
|
|
141
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
|
142
|
|
|
// NEXT_MAJOR: Remove this line |
|
143
|
|
|
$this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
|
|
|
|
|
|
144
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
|
145
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
|
146
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
|
|
147
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
|
148
|
|
|
$pool->reveal(), |
|
149
|
|
|
null, |
|
150
|
|
|
$translator->reveal(), |
|
151
|
|
|
$container->reveal() |
|
152
|
|
|
)); |
|
153
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
|
154
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
|
|
155
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
|
156
|
|
|
$fieldDescription->getTemplate()->willReturn(false); |
|
157
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
|
158
|
|
|
|
|
159
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
|
|
160
|
|
|
$action = $this->action; |
|
161
|
|
|
$response = $action($request); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testSetObjectFieldValueActionOnARelationField(): void |
|
167
|
|
|
{ |
|
168
|
|
|
$object = new Baz(); |
|
169
|
|
|
$associationObject = new Bar(); |
|
170
|
|
|
$request = new Request([ |
|
171
|
|
|
'code' => 'sonata.post.admin', |
|
172
|
|
|
'objectId' => 42, |
|
173
|
|
|
'field' => 'bar', |
|
174
|
|
|
'value' => 1, |
|
175
|
|
|
'context' => 'list', |
|
176
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
|
177
|
|
|
|
|
178
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
|
179
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
|
180
|
|
|
$template = $this->prophesize(Template::class); |
|
|
|
|
|
|
181
|
|
|
$translator = $this->prophesize(TranslatorInterface::class); |
|
182
|
|
|
$propertyAccessor = new PropertyAccessor(); |
|
183
|
|
|
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
|
184
|
|
|
$container = $this->prophesize(ContainerInterface::class); |
|
185
|
|
|
|
|
186
|
|
|
$this->admin->getObject(42)->willReturn($object); |
|
187
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
|
|
188
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
|
|
189
|
|
|
$this->admin->getListFieldDescription('bar')->willReturn($fieldDescription->reveal()); |
|
|
|
|
|
|
190
|
|
|
$this->admin->getClass()->willReturn(get_class($object)); |
|
|
|
|
|
|
191
|
|
|
$this->admin->update($object)->shouldBeCalled(); |
|
192
|
|
|
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal()); |
|
193
|
|
|
// NEXT_MAJOR: Remove this line |
|
194
|
|
|
$this->admin->getTemplate('base_list_field')->willReturn('admin_template'); |
|
|
|
|
|
|
195
|
|
|
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template'); |
|
196
|
|
|
$this->admin->getModelManager()->willReturn($modelManager->reveal()); |
|
|
|
|
|
|
197
|
|
|
$this->twig->addExtension(new SonataAdminExtension( |
|
198
|
|
|
$this->pool->reveal(), |
|
|
|
|
|
|
199
|
|
|
null, |
|
200
|
|
|
$translator->reveal(), |
|
201
|
|
|
$container->reveal() |
|
202
|
|
|
)); |
|
203
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
|
|
204
|
|
|
$fieldDescription->getType()->willReturn('choice'); |
|
205
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
|
206
|
|
|
$fieldDescription->getOption('class')->willReturn(Bar::class); |
|
207
|
|
|
$fieldDescription->getTargetEntity()->willReturn(Bar::class); |
|
208
|
|
|
$fieldDescription->getAdmin()->willReturn($this->admin->reveal()); |
|
|
|
|
|
|
209
|
|
|
$fieldDescription->getTemplate()->willReturn('field_template'); |
|
210
|
|
|
$fieldDescription->getValue(Argument::cetera())->willReturn('some value'); |
|
211
|
|
|
$modelManager->find(get_class($associationObject), 1)->willReturn($associationObject); |
|
212
|
|
|
|
|
213
|
|
|
$this->validator->validate($object)->willReturn(new ConstraintViolationList([])); |
|
|
|
|
|
|
214
|
|
|
$action = $this->action; |
|
215
|
|
|
$response = $action($request); |
|
216
|
|
|
|
|
217
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function testSetObjectFieldValueActionWithViolations(): void |
|
221
|
|
|
{ |
|
222
|
|
|
$bar = new Bar(); |
|
223
|
|
|
$object = new Baz(); |
|
224
|
|
|
$object->setBar($bar); |
|
225
|
|
|
$request = new Request([ |
|
226
|
|
|
'code' => 'sonata.post.admin', |
|
227
|
|
|
'objectId' => 42, |
|
228
|
|
|
'field' => 'bar.enabled', |
|
229
|
|
|
'value' => 1, |
|
230
|
|
|
'context' => 'list', |
|
231
|
|
|
], [], [], [], [], ['REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']); |
|
232
|
|
|
|
|
233
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
|
234
|
|
|
$propertyAccessor = new PropertyAccessor(); |
|
235
|
|
|
|
|
236
|
|
|
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor); |
|
|
|
|
|
|
237
|
|
|
$this->admin->getObject(42)->willReturn($object); |
|
238
|
|
|
$this->admin->hasAccess('edit', $object)->willReturn(true); |
|
|
|
|
|
|
239
|
|
|
$this->admin->getListFieldDescription('bar.enabled')->willReturn($fieldDescription->reveal()); |
|
|
|
|
|
|
240
|
|
|
$this->validator->validate($bar)->willReturn(new ConstraintViolationList([ |
|
|
|
|
|
|
241
|
|
|
new ConstraintViolation('error1', null, [], null, 'enabled', null), |
|
242
|
|
|
new ConstraintViolation('error2', null, [], null, 'enabled', null), |
|
243
|
|
|
])); |
|
244
|
|
|
$fieldDescription->getOption('editable')->willReturn(true); |
|
245
|
|
|
$fieldDescription->getType()->willReturn('boolean'); |
|
246
|
|
|
|
|
247
|
|
|
$action = $this->action; |
|
248
|
|
|
$response = $action($request); |
|
249
|
|
|
|
|
250
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
|
251
|
|
|
$this->assertSame(json_encode("error1\nerror2"), $response->getContent()); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
private function configureFormRenderer() |
|
|
|
|
|
|
255
|
|
|
{ |
|
256
|
|
|
$runtime = new FormRenderer($this->createMock( |
|
257
|
|
|
FormRendererEngineInterface::class, |
|
258
|
|
|
CsrfTokenManagerInterface::class |
|
259
|
|
|
)); |
|
260
|
|
|
|
|
261
|
|
|
// Remove the condition when dropping sf < 3.2 |
|
262
|
|
|
if (!method_exists(AppVariable::class, 'getToken')) { |
|
263
|
|
|
$extension = new FormExtension(); |
|
264
|
|
|
|
|
265
|
|
|
$this->twig->addExtension($extension); |
|
266
|
|
|
$extension->renderer = $runtime; |
|
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
return $runtime; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
// Remove the condition when dropping sf < 3.4 |
|
272
|
|
|
if (!method_exists(DebugCommand::class, 'getLoaderPaths')) { |
|
273
|
|
|
$twigRuntime = $this->prophesize(TwigRenderer::class); |
|
274
|
|
|
|
|
275
|
|
|
$this->twig->addRuntimeLoader(new FactoryRuntimeLoader( |
|
276
|
|
|
FormRenderer::class, |
|
277
|
|
|
function () use ($runtime) { |
|
|
|
|
|
|
278
|
|
|
return $runtime; |
|
279
|
|
|
} |
|
280
|
|
|
)); |
|
281
|
|
|
$this->twig->getRuntime(TwigRenderer::class)->willReturn($twigRuntime->reveal()); |
|
282
|
|
|
$twigRuntime->setEnvironment($this->twig)->shouldBeCalled(); |
|
283
|
|
|
|
|
284
|
|
|
return $twigRuntime; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
$this->twig->addRuntimeLoader(new FactoryRuntimeLoader( |
|
288
|
|
|
FormRenderer::class, |
|
289
|
|
|
function () use ($runtime) { |
|
|
|
|
|
|
290
|
|
|
return $runtime; |
|
291
|
|
|
} |
|
292
|
|
|
)); |
|
293
|
|
|
|
|
294
|
|
|
return $runtime; |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.