1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\AdminBundle\Tests\Controller; |
13
|
|
|
|
14
|
|
|
use Exporter\Exporter; |
15
|
|
|
use Exporter\Source\SourceIteratorInterface; |
16
|
|
|
use Exporter\Writer\JsonWriter; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
20
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
21
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
22
|
|
|
use Sonata\AdminBundle\Controller\CRUDController; |
23
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridInterface; |
24
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
25
|
|
|
use Sonata\AdminBundle\Exception\LockException; |
26
|
|
|
use Sonata\AdminBundle\Exception\ModelManagerException; |
27
|
|
|
use Sonata\AdminBundle\Export\Exporter as SonataExporter; |
28
|
|
|
use Sonata\AdminBundle\Model\AuditManager; |
29
|
|
|
use Sonata\AdminBundle\Model\AuditReaderInterface; |
30
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
31
|
|
|
use Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap; |
32
|
|
|
use Sonata\AdminBundle\Security\Handler\AclSecurityHandler; |
33
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistryInterface; |
34
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin; |
35
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Controller\BatchAdminController; |
36
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Controller\PreCRUDController; |
37
|
|
|
use Sonata\AdminBundle\Util\AdminObjectAclData; |
38
|
|
|
use Sonata\AdminBundle\Util\AdminObjectAclManipulator; |
39
|
|
|
use Symfony\Bridge\Twig\Extension\FormExtension; |
40
|
|
|
use Symfony\Bridge\Twig\Form\TwigRenderer; |
41
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
42
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine; |
43
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
44
|
|
|
use Symfony\Component\Form\Form; |
45
|
|
|
use Symfony\Component\Form\FormRenderer; |
46
|
|
|
use Symfony\Component\Form\FormView; |
47
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
48
|
|
|
use Symfony\Component\HttpFoundation\Request; |
49
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
50
|
|
|
use Symfony\Component\HttpFoundation\Response; |
51
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
52
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
53
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
54
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
55
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
56
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
57
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
58
|
|
|
use Symfony\Component\Security\Csrf\CsrfToken; |
59
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
60
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test for CRUDController. |
64
|
|
|
* |
65
|
|
|
* @author Andrej Hudec <[email protected]> |
66
|
|
|
* |
67
|
|
|
* @group legacy |
68
|
|
|
*/ |
69
|
|
|
class CRUDControllerTest extends TestCase |
70
|
|
|
{ |
71
|
|
|
/** |
72
|
|
|
* @var CRUDController |
73
|
|
|
*/ |
74
|
|
|
private $controller; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var Request |
78
|
|
|
*/ |
79
|
|
|
private $request; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var AbstractAdmin |
83
|
|
|
*/ |
84
|
|
|
private $admin; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var TemplateRegistryInterface |
88
|
|
|
*/ |
89
|
|
|
private $templateRegistry; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var Pool |
93
|
|
|
*/ |
94
|
|
|
private $pool; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var array |
98
|
|
|
*/ |
99
|
|
|
private $parameters; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var Session |
103
|
|
|
*/ |
104
|
|
|
private $session; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var AuditManager |
108
|
|
|
*/ |
109
|
|
|
private $auditManager; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var ContainerInterface |
113
|
|
|
*/ |
114
|
|
|
private $container; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @var AdminObjectAclManipulator |
118
|
|
|
*/ |
119
|
|
|
private $adminObjectAclManipulator; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @var string |
123
|
|
|
*/ |
124
|
|
|
private $template; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @var array |
128
|
|
|
*/ |
129
|
|
|
private $protectedTestedMethods; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @var CsrfTokenManagerInterface |
133
|
|
|
*/ |
134
|
|
|
private $csrfProvider; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var KernelInterface |
138
|
|
|
*/ |
139
|
|
|
private $kernel; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @var TranslatorInterface |
143
|
|
|
*/ |
144
|
|
|
private $translator; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
protected function setUp() |
150
|
|
|
{ |
151
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
152
|
|
|
|
153
|
|
|
$this->request = new Request(); |
154
|
|
|
$this->pool = new Pool($this->container, 'title', 'logo.png'); |
155
|
|
|
$this->pool->setAdminServiceIds(['foo.admin']); |
156
|
|
|
$this->request->attributes->set('_sonata_admin', 'foo.admin'); |
157
|
|
|
$this->admin = $this->getMockBuilder(AbstractAdmin::class) |
158
|
|
|
->disableOriginalConstructor() |
159
|
|
|
->getMock(); |
160
|
|
|
$this->translator = $this->createMock(TranslatorInterface::class); |
161
|
|
|
$this->parameters = []; |
162
|
|
|
$this->template = ''; |
163
|
|
|
|
164
|
|
|
$this->templateRegistry = $this->prophesize(TemplateRegistryInterface::class); |
165
|
|
|
|
166
|
|
|
$templating = $this->getMockBuilder(DelegatingEngine::class) |
167
|
|
|
->setConstructorArgs([$this->container, []]) |
168
|
|
|
->getMock(); |
169
|
|
|
|
170
|
|
|
$templatingRenderReturnCallback = $this->returnCallback(function ( |
171
|
|
|
$view, |
172
|
|
|
array $parameters = [], |
173
|
|
|
Response $response = null |
174
|
|
|
) { |
175
|
|
|
$this->template = $view; |
176
|
|
|
|
177
|
|
|
if (null === $response) { |
178
|
|
|
$response = new Response(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$this->parameters = $parameters; |
182
|
|
|
|
183
|
|
|
return $response; |
184
|
|
|
}); |
185
|
|
|
|
186
|
|
|
// SF < 3.3.10 BC |
187
|
|
|
$templating->expects($this->any()) |
188
|
|
|
->method('renderResponse') |
189
|
|
|
->will($templatingRenderReturnCallback); |
190
|
|
|
|
191
|
|
|
$templating->expects($this->any()) |
192
|
|
|
->method('render') |
193
|
|
|
->will($templatingRenderReturnCallback); |
194
|
|
|
|
195
|
|
|
$this->session = new Session(new MockArraySessionStorage()); |
196
|
|
|
|
197
|
|
|
$twig = $this->getMockBuilder(\Twig_Environment::class) |
198
|
|
|
->disableOriginalConstructor() |
199
|
|
|
->getMock(); |
200
|
|
|
|
201
|
|
|
$twig->expects($this->any()) |
202
|
|
|
->method('getExtension') |
203
|
|
|
->will($this->returnCallback(function ($name) { |
204
|
|
|
switch ($name) { |
205
|
|
|
case FormExtension::class: |
206
|
|
|
return new FormExtension($this->createMock(TwigRenderer::class)); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
})); |
209
|
|
|
|
210
|
|
|
$twig->expects($this->any()) |
211
|
|
|
->method('getRuntime') |
212
|
|
|
->will($this->returnCallback(function ($name) { |
213
|
|
|
switch ($name) { |
214
|
|
|
case TwigRenderer::class: |
215
|
|
|
return $this->createMock(TwigRenderer::class); |
216
|
|
|
case FormRenderer::class: |
217
|
|
|
return $this->createMock(FormRenderer::class); |
218
|
|
|
} |
219
|
|
|
})); |
220
|
|
|
|
221
|
|
|
// NEXT_MAJOR : require sonata/exporter ^1.7 and remove conditional |
222
|
|
|
if (class_exists(Exporter::class)) { |
223
|
|
|
$exporter = new Exporter([new JsonWriter('/tmp/sonataadmin/export.json')]); |
224
|
|
|
} else { |
225
|
|
|
$exporter = $this->createMock(SonataExporter::class); |
226
|
|
|
|
227
|
|
|
$exporter->expects($this->any()) |
228
|
|
|
->method('getResponse') |
229
|
|
|
->will($this->returnValue(new StreamedResponse())); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$this->auditManager = $this->getMockBuilder(AuditManager::class) |
233
|
|
|
->disableOriginalConstructor() |
234
|
|
|
->getMock(); |
235
|
|
|
|
236
|
|
|
$this->adminObjectAclManipulator = $this->getMockBuilder(AdminObjectAclManipulator::class) |
237
|
|
|
->disableOriginalConstructor() |
238
|
|
|
->getMock(); |
239
|
|
|
|
240
|
|
|
$this->csrfProvider = $this->getMockBuilder(CsrfTokenManagerInterface::class) |
241
|
|
|
->getMock(); |
242
|
|
|
|
243
|
|
|
$this->csrfProvider->expects($this->any()) |
244
|
|
|
->method('getToken') |
245
|
|
|
->will($this->returnCallback(function ($intention) { |
246
|
|
|
return new CsrfToken($intention, 'csrf-token-123_'.$intention); |
247
|
|
|
})); |
248
|
|
|
|
249
|
|
|
$this->csrfProvider->expects($this->any()) |
250
|
|
|
->method('isTokenValid') |
251
|
|
|
->will($this->returnCallback(function (CsrfToken $token) { |
252
|
|
|
if ($token->getValue() == 'csrf-token-123_'.$token->getId()) { |
253
|
|
|
return true; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return false; |
257
|
|
|
})); |
258
|
|
|
|
259
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
260
|
|
|
|
261
|
|
|
$requestStack = new RequestStack(); |
262
|
|
|
$requestStack->push($this->request); |
263
|
|
|
|
264
|
|
|
$this->kernel = $this->createMock(KernelInterface::class); |
265
|
|
|
|
266
|
|
|
$this->container->expects($this->any()) |
267
|
|
|
->method('get') |
268
|
|
|
->will($this->returnCallback(function ($id) use ( |
269
|
|
|
$templating, |
270
|
|
|
$twig, |
271
|
|
|
$exporter, |
272
|
|
|
$requestStack |
273
|
|
|
) { |
274
|
|
|
switch ($id) { |
275
|
|
|
case 'sonata.admin.pool': |
276
|
|
|
return $this->pool; |
277
|
|
|
case 'request_stack': |
278
|
|
|
return $requestStack; |
279
|
|
|
case 'foo.admin': |
280
|
|
|
return $this->admin; |
281
|
|
|
case 'foo.admin.template_registry': |
282
|
|
|
return $this->templateRegistry->reveal(); |
|
|
|
|
283
|
|
|
case 'templating': |
284
|
|
|
return $templating; |
285
|
|
|
case 'twig': |
286
|
|
|
return $twig; |
287
|
|
|
case 'session': |
288
|
|
|
return $this->session; |
289
|
|
|
case 'sonata.admin.exporter': |
290
|
|
|
return $exporter; |
291
|
|
|
case 'sonata.admin.audit.manager': |
292
|
|
|
return $this->auditManager; |
293
|
|
|
case 'sonata.admin.object.manipulator.acl.admin': |
294
|
|
|
return $this->adminObjectAclManipulator; |
295
|
|
|
case 'security.csrf.token_manager': |
296
|
|
|
return $this->csrfProvider; |
297
|
|
|
case 'logger': |
298
|
|
|
return $this->logger; |
299
|
|
|
case 'kernel': |
300
|
|
|
return $this->kernel; |
301
|
|
|
case 'translator': |
302
|
|
|
return $this->translator; |
303
|
|
|
} |
304
|
|
|
})); |
305
|
|
|
|
306
|
|
|
$this->container->expects($this->any()) |
307
|
|
|
->method('has') |
308
|
|
|
->will($this->returnCallback(function ($id) { |
309
|
|
|
if ('security.csrf.token_manager' == $id && null !== $this->getCsrfProvider()) { |
310
|
|
|
return true; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
if ('logger' == $id) { |
314
|
|
|
return true; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if ('session' == $id) { |
318
|
|
|
return true; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
if ('templating' == $id) { |
322
|
|
|
return true; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if ('translator' == $id) { |
326
|
|
|
return true; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return false; |
330
|
|
|
})); |
331
|
|
|
|
332
|
|
|
$this->container->expects($this->any()) |
333
|
|
|
->method('getParameter') |
334
|
|
|
->will($this->returnCallback(function ($name) { |
335
|
|
|
switch ($name) { |
336
|
|
|
case 'security.role_hierarchy.roles': |
337
|
|
|
return ['ROLE_SUPER_ADMIN' => ['ROLE_USER', 'ROLE_SONATA_ADMIN', 'ROLE_ADMIN']]; |
338
|
|
|
} |
339
|
|
|
})); |
340
|
|
|
|
341
|
|
|
$this->templateRegistry->getTemplate('ajax')->willReturn('@SonataAdmin/ajax_layout.html.twig'); |
342
|
|
|
$this->templateRegistry->getTemplate('layout')->willReturn('@SonataAdmin/standard_layout.html.twig'); |
343
|
|
|
$this->templateRegistry->getTemplate('show')->willReturn('@SonataAdmin/CRUD/show.html.twig'); |
344
|
|
|
$this->templateRegistry->getTemplate('show_compare')->willReturn('@SonataAdmin/CRUD/show_compare.html.twig'); |
345
|
|
|
$this->templateRegistry->getTemplate('edit')->willReturn('@SonataAdmin/CRUD/edit.html.twig'); |
346
|
|
|
$this->templateRegistry->getTemplate('dashboard')->willReturn('@SonataAdmin/Core/dashboard.html.twig'); |
347
|
|
|
$this->templateRegistry->getTemplate('search')->willReturn('@SonataAdmin/Core/search.html.twig'); |
348
|
|
|
$this->templateRegistry->getTemplate('list')->willReturn('@SonataAdmin/CRUD/list.html.twig'); |
349
|
|
|
$this->templateRegistry->getTemplate('preview')->willReturn('@SonataAdmin/CRUD/preview.html.twig'); |
350
|
|
|
$this->templateRegistry->getTemplate('history')->willReturn('@SonataAdmin/CRUD/history.html.twig'); |
351
|
|
|
$this->templateRegistry->getTemplate('acl')->willReturn('@SonataAdmin/CRUD/acl.html.twig'); |
352
|
|
|
$this->templateRegistry->getTemplate('delete')->willReturn('@SonataAdmin/CRUD/delete.html.twig'); |
353
|
|
|
$this->templateRegistry->getTemplate('batch')->willReturn('@SonataAdmin/CRUD/list__batch.html.twig'); |
354
|
|
|
$this->templateRegistry->getTemplate('batch_confirmation')->willReturn('@SonataAdmin/CRUD/batch_confirmation.html.twig'); |
355
|
|
|
|
356
|
|
|
// NEXT_MAJOR: Remove this call |
357
|
|
|
$this->admin->method('getTemplate')->willReturnMap([ |
358
|
|
|
['ajax', '@SonataAdmin/ajax_layout.html.twig'], |
359
|
|
|
['layout', '@SonataAdmin/standard_layout.html.twig'], |
360
|
|
|
['show', '@SonataAdmin/CRUD/show.html.twig'], |
361
|
|
|
['show_compare', '@SonataAdmin/CRUD/show_compare.html.twig'], |
362
|
|
|
['edit', '@SonataAdmin/CRUD/edit.html.twig'], |
363
|
|
|
['dashboard', '@SonataAdmin/Core/dashboard.html.twig'], |
364
|
|
|
['search', '@SonataAdmin/Core/search.html.twig'], |
365
|
|
|
['list', '@SonataAdmin/CRUD/list.html.twig'], |
366
|
|
|
['preview', '@SonataAdmin/CRUD/preview.html.twig'], |
367
|
|
|
['history', '@SonataAdmin/CRUD/history.html.twig'], |
368
|
|
|
['acl', '@SonataAdmin/CRUD/acl.html.twig'], |
369
|
|
|
['delete', '@SonataAdmin/CRUD/delete.html.twig'], |
370
|
|
|
['batch', '@SonataAdmin/CRUD/list__batch.html.twig'], |
371
|
|
|
['batch_confirmation', '@SonataAdmin/CRUD/batch_confirmation.html.twig'], |
372
|
|
|
]); |
373
|
|
|
|
374
|
|
|
$this->admin->expects($this->any()) |
375
|
|
|
->method('getIdParameter') |
376
|
|
|
->will($this->returnValue('id')); |
377
|
|
|
|
378
|
|
|
$this->admin->expects($this->any()) |
379
|
|
|
->method('getAccessMapping') |
380
|
|
|
->will($this->returnValue([])); |
381
|
|
|
|
382
|
|
|
$this->admin->expects($this->any()) |
383
|
|
|
->method('generateUrl') |
384
|
|
|
->will( |
385
|
|
|
$this->returnCallback( |
386
|
|
|
function ($name, array $parameters = [], $absolute = false) { |
|
|
|
|
387
|
|
|
$result = $name; |
388
|
|
|
if (!empty($parameters)) { |
389
|
|
|
$result .= '?'.http_build_query($parameters); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
return $result; |
393
|
|
|
} |
394
|
|
|
) |
395
|
|
|
); |
396
|
|
|
|
397
|
|
|
$this->admin->expects($this->any()) |
398
|
|
|
->method('generateObjectUrl') |
399
|
|
|
->will( |
400
|
|
|
$this->returnCallback( |
401
|
|
|
function ($name, $object, array $parameters = [], $absolute = false) { |
|
|
|
|
402
|
|
|
$result = \get_class($object).'_'.$name; |
403
|
|
|
if (!empty($parameters)) { |
404
|
|
|
$result .= '?'.http_build_query($parameters); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
return $result; |
408
|
|
|
} |
409
|
|
|
) |
410
|
|
|
); |
411
|
|
|
|
412
|
|
|
$this->admin->expects($this->any()) |
413
|
|
|
->method('getCode') |
414
|
|
|
->willReturn('foo.admin'); |
415
|
|
|
|
416
|
|
|
$this->controller = new CRUDController(); |
417
|
|
|
$this->controller->setContainer($this->container); |
418
|
|
|
|
419
|
|
|
// Make some methods public to test them |
420
|
|
|
$testedMethods = [ |
421
|
|
|
'renderJson', |
422
|
|
|
'isXmlHttpRequest', |
423
|
|
|
'configure', |
424
|
|
|
'getBaseTemplate', |
425
|
|
|
'redirectTo', |
426
|
|
|
'addFlash', |
427
|
|
|
]; |
428
|
|
|
foreach ($testedMethods as $testedMethod) { |
429
|
|
|
// NEXT_MAJOR: Remove this check and only use CRUDController |
430
|
|
|
if (method_exists(CRUDController::class, $testedMethod)) { |
431
|
|
|
$method = new \ReflectionMethod(CRUDController::class, $testedMethod); |
432
|
|
|
} else { |
433
|
|
|
$method = new \ReflectionMethod(Controller::class, $testedMethod); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
$method->setAccessible(true); |
437
|
|
|
$this->protectedTestedMethods[$testedMethod] = $method; |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
public function testRenderJson1() |
442
|
|
|
{ |
443
|
|
|
$data = ['example' => '123', 'foo' => 'bar']; |
444
|
|
|
|
445
|
|
|
$this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded'); |
446
|
|
|
$response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
447
|
|
|
|
448
|
|
|
$this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
449
|
|
|
$this->assertSame(json_encode($data), $response->getContent()); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
public function testRenderJson2() |
453
|
|
|
{ |
454
|
|
|
$data = ['example' => '123', 'foo' => 'bar']; |
455
|
|
|
|
456
|
|
|
$this->request->headers->set('Content-Type', 'multipart/form-data'); |
457
|
|
|
$response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
458
|
|
|
|
459
|
|
|
$this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
460
|
|
|
$this->assertSame(json_encode($data), $response->getContent()); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
public function testRenderJsonAjax() |
464
|
|
|
{ |
465
|
|
|
$data = ['example' => '123', 'foo' => 'bar']; |
466
|
|
|
|
467
|
|
|
$this->request->attributes->set('_xml_http_request', true); |
468
|
|
|
$this->request->headers->set('Content-Type', 'multipart/form-data'); |
469
|
|
|
$response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
470
|
|
|
|
471
|
|
|
$this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
472
|
|
|
$this->assertSame(json_encode($data), $response->getContent()); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
public function testIsXmlHttpRequest() |
476
|
|
|
{ |
477
|
|
|
$this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
478
|
|
|
|
479
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
480
|
|
|
|
481
|
|
|
$this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
482
|
|
|
|
483
|
|
|
$this->request->headers->remove('X-Requested-With'); |
484
|
|
|
$this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
485
|
|
|
|
486
|
|
|
$this->request->attributes->set('_xml_http_request', true); |
487
|
|
|
$this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
public function testConfigure() |
491
|
|
|
{ |
492
|
|
|
$uniqueId = ''; |
493
|
|
|
|
494
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
495
|
|
|
->method('setUniqid') |
496
|
|
|
->will($this->returnCallback(function ($uniqid) use (&$uniqueId) { |
497
|
|
|
$uniqueId = $uniqid; |
498
|
|
|
})); |
499
|
|
|
|
500
|
|
|
$this->request->query->set('uniqid', 123456); |
501
|
|
|
$this->protectedTestedMethods['configure']->invoke($this->controller); |
502
|
|
|
|
503
|
|
|
$this->assertSame(123456, $uniqueId); |
504
|
|
|
$this->assertAttributeSame($this->admin, 'admin', $this->controller); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
public function testConfigureChild() |
508
|
|
|
{ |
509
|
|
|
$uniqueId = ''; |
510
|
|
|
|
511
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
512
|
|
|
->method('setUniqid') |
513
|
|
|
->will($this->returnCallback(function ($uniqid) use (&$uniqueId) { |
514
|
|
|
$uniqueId = $uniqid; |
515
|
|
|
})); |
516
|
|
|
|
517
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
518
|
|
|
->method('isChild') |
519
|
|
|
->will($this->returnValue(true)); |
520
|
|
|
|
521
|
|
|
$adminParent = $this->getMockBuilder(AbstractAdmin::class) |
522
|
|
|
->disableOriginalConstructor() |
523
|
|
|
->getMock(); |
524
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
525
|
|
|
->method('getParent') |
526
|
|
|
->will($this->returnValue($adminParent)); |
527
|
|
|
|
528
|
|
|
$this->request->query->set('uniqid', 123456); |
529
|
|
|
$this->protectedTestedMethods['configure']->invoke($this->controller); |
530
|
|
|
|
531
|
|
|
$this->assertSame(123456, $uniqueId); |
532
|
|
|
$this->assertAttributeInstanceOf(\get_class($adminParent), 'admin', $this->controller); |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
public function testConfigureWithException() |
536
|
|
|
{ |
537
|
|
|
$this->expectException( |
538
|
|
|
\RuntimeException::class, |
539
|
|
|
'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`' |
540
|
|
|
); |
541
|
|
|
|
542
|
|
|
$this->request->attributes->remove('_sonata_admin'); |
543
|
|
|
$this->protectedTestedMethods['configure']->invoke($this->controller); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
public function testConfigureWithException2() |
547
|
|
|
{ |
548
|
|
|
$this->expectException( |
549
|
|
|
\InvalidArgumentException::class, |
550
|
|
|
'Found service "nonexistent.admin" is not a valid admin service' |
551
|
|
|
); |
552
|
|
|
|
553
|
|
|
$this->pool->setAdminServiceIds(['nonexistent.admin']); |
554
|
|
|
$this->request->attributes->set('_sonata_admin', 'nonexistent.admin'); |
555
|
|
|
$this->protectedTestedMethods['configure']->invoke($this->controller); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
public function testGetBaseTemplate() |
559
|
|
|
{ |
560
|
|
|
$this->assertSame( |
561
|
|
|
'@SonataAdmin/standard_layout.html.twig', |
562
|
|
|
$this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
563
|
|
|
); |
564
|
|
|
|
565
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
566
|
|
|
$this->assertSame( |
567
|
|
|
'@SonataAdmin/ajax_layout.html.twig', |
568
|
|
|
$this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
569
|
|
|
); |
570
|
|
|
|
571
|
|
|
$this->request->headers->remove('X-Requested-With'); |
572
|
|
|
$this->assertSame( |
573
|
|
|
'@SonataAdmin/standard_layout.html.twig', |
574
|
|
|
$this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
575
|
|
|
); |
576
|
|
|
|
577
|
|
|
$this->request->attributes->set('_xml_http_request', true); |
578
|
|
|
$this->assertSame( |
579
|
|
|
'@SonataAdmin/ajax_layout.html.twig', |
580
|
|
|
$this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
581
|
|
|
); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
public function testRender() |
585
|
|
|
{ |
586
|
|
|
$this->parameters = []; |
587
|
|
|
$this->assertInstanceOf( |
588
|
|
|
Response::class, |
589
|
|
|
$this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], null) |
590
|
|
|
); |
591
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
592
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
593
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
594
|
|
|
$this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
public function testRenderWithResponse() |
598
|
|
|
{ |
599
|
|
|
$this->parameters = []; |
600
|
|
|
$response = $response = new Response(); |
601
|
|
|
$response->headers->set('X-foo', 'bar'); |
602
|
|
|
$responseResult = $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], $response); |
603
|
|
|
|
604
|
|
|
$this->assertSame($response, $responseResult); |
605
|
|
|
$this->assertSame('bar', $responseResult->headers->get('X-foo')); |
606
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
607
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
608
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
609
|
|
|
$this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
public function testRenderCustomParams() |
613
|
|
|
{ |
614
|
|
|
$this->parameters = []; |
615
|
|
|
$this->assertInstanceOf( |
616
|
|
|
Response::class, |
617
|
|
|
$this->controller->renderWithExtraParams( |
618
|
|
|
'@FooAdmin/foo.html.twig', |
619
|
|
|
['foo' => 'bar'], |
620
|
|
|
null |
621
|
|
|
) |
622
|
|
|
); |
623
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
624
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
625
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
626
|
|
|
$this->assertSame('bar', $this->parameters['foo']); |
627
|
|
|
$this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
public function testRenderAjax() |
631
|
|
|
{ |
632
|
|
|
$this->parameters = []; |
633
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
634
|
|
|
$this->assertInstanceOf( |
635
|
|
|
Response::class, |
636
|
|
|
$this->controller->renderWithExtraParams( |
637
|
|
|
'@FooAdmin/foo.html.twig', |
638
|
|
|
['foo' => 'bar'], |
639
|
|
|
null |
640
|
|
|
) |
641
|
|
|
); |
642
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
643
|
|
|
$this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']); |
644
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
645
|
|
|
$this->assertSame('bar', $this->parameters['foo']); |
646
|
|
|
$this->assertSame('@FooAdmin/foo.html.twig', $this->template); |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
public function testListActionAccessDenied() |
650
|
|
|
{ |
651
|
|
|
$this->expectException(AccessDeniedException::class); |
652
|
|
|
|
653
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
654
|
|
|
->method('checkAccess') |
655
|
|
|
->with($this->equalTo('list')) |
656
|
|
|
->will($this->throwException(new AccessDeniedException())); |
657
|
|
|
|
658
|
|
|
$this->controller->listAction($this->request); |
|
|
|
|
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
public function testPreList() |
662
|
|
|
{ |
663
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
664
|
|
|
->method('hasRoute') |
665
|
|
|
->with($this->equalTo('list')) |
666
|
|
|
->will($this->returnValue(true)); |
667
|
|
|
|
668
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
669
|
|
|
->method('checkAccess') |
670
|
|
|
->with($this->equalTo('list')) |
671
|
|
|
->will($this->returnValue(true)); |
672
|
|
|
|
673
|
|
|
$controller = new PreCRUDController(); |
674
|
|
|
$controller->setContainer($this->container); |
675
|
|
|
|
676
|
|
|
$response = $controller->listAction($this->request); |
|
|
|
|
677
|
|
|
$this->assertInstanceOf(Response::class, $response); |
678
|
|
|
$this->assertSame('preList called', $response->getContent()); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
public function testListAction() |
682
|
|
|
{ |
683
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
684
|
|
|
|
685
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
686
|
|
|
->method('hasRoute') |
687
|
|
|
->with($this->equalTo('list')) |
688
|
|
|
->will($this->returnValue(true)); |
689
|
|
|
|
690
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
691
|
|
|
->method('checkAccess') |
692
|
|
|
->with($this->equalTo('list')) |
693
|
|
|
->will($this->returnValue(true)); |
694
|
|
|
|
695
|
|
|
$form = $this->getMockBuilder(Form::class) |
696
|
|
|
->disableOriginalConstructor() |
697
|
|
|
->getMock(); |
698
|
|
|
|
699
|
|
|
$form->expects($this->once()) |
700
|
|
|
->method('createView') |
701
|
|
|
->will($this->returnValue($this->createMock(FormView::class))); |
702
|
|
|
|
703
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
704
|
|
|
->method('getDatagrid') |
705
|
|
|
->will($this->returnValue($datagrid)); |
706
|
|
|
|
707
|
|
|
$datagrid->expects($this->once()) |
708
|
|
|
->method('getForm') |
709
|
|
|
->will($this->returnValue($form)); |
710
|
|
|
|
711
|
|
|
$this->parameters = []; |
712
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->listAction($this->request)); |
|
|
|
|
713
|
|
|
|
714
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
715
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
716
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
717
|
|
|
|
718
|
|
|
$this->assertSame('list', $this->parameters['action']); |
719
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
720
|
|
|
$this->assertInstanceOf(DatagridInterface::class, $this->parameters['datagrid']); |
721
|
|
|
$this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']); |
722
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
723
|
|
|
$this->assertSame('@SonataAdmin/CRUD/list.html.twig', $this->template); |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
public function testBatchActionDeleteAccessDenied() |
727
|
|
|
{ |
728
|
|
|
$this->expectException(AccessDeniedException::class); |
729
|
|
|
|
730
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
731
|
|
|
->method('checkAccess') |
732
|
|
|
->with($this->equalTo('batchDelete')) |
733
|
|
|
->will($this->throwException(new AccessDeniedException())); |
734
|
|
|
|
735
|
|
|
$this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
public function testBatchActionDelete() |
739
|
|
|
{ |
740
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
741
|
|
|
|
742
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
743
|
|
|
->method('checkAccess') |
744
|
|
|
->with($this->equalTo('batchDelete')) |
745
|
|
|
->will($this->returnValue(true)); |
746
|
|
|
|
747
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
748
|
|
|
->method('getModelManager') |
749
|
|
|
->will($this->returnValue($modelManager)); |
750
|
|
|
|
751
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
752
|
|
|
->method('getFilterParameters') |
753
|
|
|
->will($this->returnValue(['foo' => 'bar'])); |
754
|
|
|
|
755
|
|
|
$this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
756
|
|
|
|
757
|
|
|
$result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
758
|
|
|
|
759
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result); |
760
|
|
|
$this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
761
|
|
|
$this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl()); |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
public function testBatchActionDeleteWithModelManagerException() |
765
|
|
|
{ |
766
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
767
|
|
|
$this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete'); |
768
|
|
|
|
769
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
770
|
|
|
->method('getModelManager') |
771
|
|
|
->will($this->returnValue($modelManager)); |
772
|
|
|
|
773
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
774
|
|
|
->method('getFilterParameters') |
775
|
|
|
->will($this->returnValue(['foo' => 'bar'])); |
776
|
|
|
|
777
|
|
|
$this->expectTranslate('flash_batch_delete_error', [], 'SonataAdminBundle'); |
778
|
|
|
|
779
|
|
|
$result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
780
|
|
|
|
781
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result); |
782
|
|
|
$this->assertSame(['flash_batch_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error')); |
783
|
|
|
$this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl()); |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
public function testBatchActionDeleteWithModelManagerExceptionInDebugMode() |
787
|
|
|
{ |
788
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
789
|
|
|
$this->expectException(ModelManagerException::class); |
790
|
|
|
|
791
|
|
|
$modelManager->expects($this->once()) |
792
|
|
|
->method('batchDelete') |
793
|
|
|
->will($this->returnCallback(function () { |
794
|
|
|
throw new ModelManagerException(); |
795
|
|
|
})); |
796
|
|
|
|
797
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
798
|
|
|
->method('getModelManager') |
799
|
|
|
->will($this->returnValue($modelManager)); |
800
|
|
|
|
801
|
|
|
$this->kernel->expects($this->once()) |
|
|
|
|
802
|
|
|
->method('isDebug') |
803
|
|
|
->will($this->returnValue(true)); |
804
|
|
|
|
805
|
|
|
$this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class)); |
806
|
|
|
} |
807
|
|
|
|
808
|
|
|
public function testShowActionNotFoundException() |
809
|
|
|
{ |
810
|
|
|
$this->expectException(NotFoundHttpException::class); |
811
|
|
|
|
812
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
813
|
|
|
->method('getObject') |
814
|
|
|
->will($this->returnValue(false)); |
815
|
|
|
|
816
|
|
|
$this->controller->showAction(null, $this->request); |
|
|
|
|
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
public function testShowActionAccessDenied() |
820
|
|
|
{ |
821
|
|
|
$this->expectException(AccessDeniedException::class); |
822
|
|
|
|
823
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
824
|
|
|
->method('getObject') |
825
|
|
|
->will($this->returnValue(new \stdClass())); |
826
|
|
|
|
827
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
828
|
|
|
->method('checkAccess') |
829
|
|
|
->with($this->equalTo('show')) |
830
|
|
|
->will($this->throwException(new AccessDeniedException())); |
831
|
|
|
|
832
|
|
|
$this->controller->showAction(null, $this->request); |
|
|
|
|
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
/** |
836
|
|
|
* @group legacy |
837
|
|
|
* @expectedDeprecation Calling this method without implementing "configureShowFields" is not supported since 3.x and will no longer be possible in 4.0 |
838
|
|
|
*/ |
839
|
|
|
public function testShowActionDeprecation() |
840
|
|
|
{ |
841
|
|
|
$object = new \stdClass(); |
842
|
|
|
|
843
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
844
|
|
|
->method('getObject') |
845
|
|
|
->will($this->returnValue($object)); |
846
|
|
|
|
847
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
848
|
|
|
->method('checkAccess') |
849
|
|
|
->with($this->equalTo('show')) |
850
|
|
|
->will($this->returnValue(true)); |
851
|
|
|
|
852
|
|
|
$show = $this->createMock(FieldDescriptionCollection::class); |
853
|
|
|
|
854
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
855
|
|
|
->method('getShow') |
856
|
|
|
->will($this->returnValue($show)); |
857
|
|
|
|
858
|
|
|
$show->expects($this->once()) |
859
|
|
|
->method('getElements') |
860
|
|
|
->willReturn([]); |
861
|
|
|
|
862
|
|
|
$show->expects($this->once()) |
863
|
|
|
->method('count') |
864
|
|
|
->willReturn(0); |
865
|
|
|
|
866
|
|
|
$this->controller->showAction(null, $this->request); |
|
|
|
|
867
|
|
|
} |
868
|
|
|
|
869
|
|
|
public function testPreShow() |
870
|
|
|
{ |
871
|
|
|
$object = new \stdClass(); |
872
|
|
|
$object->foo = 123456; |
873
|
|
|
|
874
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
875
|
|
|
->method('getObject') |
876
|
|
|
->will($this->returnValue($object)); |
877
|
|
|
|
878
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
879
|
|
|
->method('checkAccess') |
880
|
|
|
->with($this->equalTo('show')) |
881
|
|
|
->will($this->returnValue(true)); |
882
|
|
|
|
883
|
|
|
$controller = new PreCRUDController(); |
884
|
|
|
$controller->setContainer($this->container); |
885
|
|
|
|
886
|
|
|
$response = $controller->showAction(null, $this->request); |
|
|
|
|
887
|
|
|
$this->assertInstanceOf(Response::class, $response); |
888
|
|
|
$this->assertSame('preShow called: 123456', $response->getContent()); |
889
|
|
|
} |
890
|
|
|
|
891
|
|
|
public function testShowAction() |
892
|
|
|
{ |
893
|
|
|
$object = new \stdClass(); |
894
|
|
|
|
895
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
896
|
|
|
->method('getObject') |
897
|
|
|
->will($this->returnValue($object)); |
898
|
|
|
|
899
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
900
|
|
|
->method('checkAccess') |
901
|
|
|
->with($this->equalTo('show')) |
902
|
|
|
->will($this->returnValue(true)); |
903
|
|
|
|
904
|
|
|
$show = $this->createMock(FieldDescriptionCollection::class); |
905
|
|
|
|
906
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
907
|
|
|
->method('getShow') |
908
|
|
|
->will($this->returnValue($show)); |
909
|
|
|
|
910
|
|
|
$show->expects($this->once()) |
911
|
|
|
->method('getElements') |
912
|
|
|
->willReturn(['field' => 'fielddata']); |
913
|
|
|
|
914
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->showAction(null, $this->request)); |
|
|
|
|
915
|
|
|
|
916
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
917
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
918
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
919
|
|
|
|
920
|
|
|
$this->assertSame('show', $this->parameters['action']); |
921
|
|
|
$this->assertInstanceOf(FieldDescriptionCollection::class, $this->parameters['elements']); |
922
|
|
|
$this->assertSame($object, $this->parameters['object']); |
923
|
|
|
|
924
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
925
|
|
|
$this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template); |
926
|
|
|
} |
927
|
|
|
|
928
|
|
|
/** |
929
|
|
|
* @dataProvider getRedirectToTests |
930
|
|
|
*/ |
931
|
|
|
public function testRedirectTo($expected, $route, $queryParams, $hasActiveSubclass) |
932
|
|
|
{ |
933
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
934
|
|
|
->method('hasActiveSubclass') |
935
|
|
|
->will($this->returnValue($hasActiveSubclass)); |
936
|
|
|
|
937
|
|
|
$object = new \stdClass(); |
938
|
|
|
|
939
|
|
|
foreach ($queryParams as $key => $value) { |
940
|
|
|
$this->request->query->set($key, $value); |
941
|
|
|
} |
942
|
|
|
|
943
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
944
|
|
|
->method('hasRoute') |
945
|
|
|
->with($this->equalTo($route)) |
946
|
|
|
->will($this->returnValue(true)); |
947
|
|
|
|
948
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
949
|
|
|
->method('hasAccess') |
950
|
|
|
->with($this->equalTo($route)) |
951
|
|
|
->will($this->returnValue(true)); |
952
|
|
|
|
953
|
|
|
$response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
954
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
955
|
|
|
$this->assertSame($expected, $response->getTargetUrl()); |
956
|
|
|
} |
957
|
|
|
|
958
|
|
|
public function testRedirectToWithObject() |
959
|
|
|
{ |
960
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
961
|
|
|
->method('hasActiveSubclass') |
962
|
|
|
->will($this->returnValue(false)); |
963
|
|
|
|
964
|
|
|
$object = new \stdClass(); |
965
|
|
|
|
966
|
|
|
$this->admin->expects($this->at(0)) |
|
|
|
|
967
|
|
|
->method('hasRoute') |
968
|
|
|
->with($this->equalTo('edit')) |
969
|
|
|
->will($this->returnValue(true)); |
970
|
|
|
|
971
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
972
|
|
|
->method('hasAccess') |
973
|
|
|
->with($this->equalTo('edit'), $object) |
974
|
|
|
->will($this->returnValue(false)); |
975
|
|
|
|
976
|
|
|
$response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
977
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
978
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
979
|
|
|
} |
980
|
|
|
|
981
|
|
|
public function getRedirectToTests() |
982
|
|
|
{ |
983
|
|
|
return [ |
984
|
|
|
['stdClass_edit', 'edit', [], false], |
985
|
|
|
['list', 'list', ['btn_update_and_list' => true], false], |
986
|
|
|
['list', 'list', ['btn_create_and_list' => true], false], |
987
|
|
|
['create', 'create', ['btn_create_and_create' => true], false], |
988
|
|
|
['create?subclass=foo', 'create', ['btn_create_and_create' => true, 'subclass' => 'foo'], true], |
989
|
|
|
]; |
990
|
|
|
} |
991
|
|
|
|
992
|
|
|
public function testDeleteActionNotFoundException() |
993
|
|
|
{ |
994
|
|
|
$this->expectException(NotFoundHttpException::class); |
995
|
|
|
|
996
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
997
|
|
|
->method('getObject') |
998
|
|
|
->will($this->returnValue(false)); |
999
|
|
|
|
1000
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1001
|
|
|
} |
1002
|
|
|
|
1003
|
|
|
public function testDeleteActionAccessDenied() |
1004
|
|
|
{ |
1005
|
|
|
$this->expectException(AccessDeniedException::class); |
1006
|
|
|
|
1007
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1008
|
|
|
->method('getObject') |
1009
|
|
|
->will($this->returnValue(new \stdClass())); |
1010
|
|
|
|
1011
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1012
|
|
|
->method('checkAccess') |
1013
|
|
|
->with($this->equalTo('delete')) |
1014
|
|
|
->will($this->throwException(new AccessDeniedException())); |
1015
|
|
|
|
1016
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1017
|
|
|
} |
1018
|
|
|
|
1019
|
|
|
public function testPreDelete() |
1020
|
|
|
{ |
1021
|
|
|
$object = new \stdClass(); |
1022
|
|
|
$object->foo = 123456; |
1023
|
|
|
|
1024
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1025
|
|
|
->method('getObject') |
1026
|
|
|
->will($this->returnValue($object)); |
1027
|
|
|
|
1028
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1029
|
|
|
->method('checkAccess') |
1030
|
|
|
->with($this->equalTo('delete')) |
1031
|
|
|
->will($this->returnValue(true)); |
1032
|
|
|
|
1033
|
|
|
$controller = new PreCRUDController(); |
1034
|
|
|
$controller->setContainer($this->container); |
1035
|
|
|
|
1036
|
|
|
$response = $controller->deleteAction(null, $this->request); |
|
|
|
|
1037
|
|
|
$this->assertInstanceOf(Response::class, $response); |
1038
|
|
|
$this->assertSame('preDelete called: 123456', $response->getContent()); |
1039
|
|
|
} |
1040
|
|
|
|
1041
|
|
|
public function testDeleteAction() |
1042
|
|
|
{ |
1043
|
|
|
$object = new \stdClass(); |
1044
|
|
|
|
1045
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1046
|
|
|
->method('getObject') |
1047
|
|
|
->will($this->returnValue($object)); |
1048
|
|
|
|
1049
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1050
|
|
|
->method('checkAccess') |
1051
|
|
|
->with($this->equalTo('delete')) |
1052
|
|
|
->will($this->returnValue(true)); |
1053
|
|
|
|
1054
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->deleteAction(1, $this->request)); |
|
|
|
|
1055
|
|
|
|
1056
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1057
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
1058
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1059
|
|
|
|
1060
|
|
|
$this->assertSame('delete', $this->parameters['action']); |
1061
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1062
|
|
|
$this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
1063
|
|
|
|
1064
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1065
|
|
|
$this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
/** |
1069
|
|
|
* @group legacy |
1070
|
|
|
* @expectedDeprecation Accessing a child that isn't connected to a given parent is deprecated since 3.34 and won't be allowed in 4.0. |
1071
|
|
|
*/ |
1072
|
|
|
public function testDeleteActionChildDeprecation() |
1073
|
|
|
{ |
1074
|
|
|
$object = new \stdClass(); |
1075
|
|
|
$object->parent = 'test'; |
1076
|
|
|
|
1077
|
|
|
$object2 = new \stdClass(); |
1078
|
|
|
|
1079
|
|
|
$admin = $this->createMock(PostAdmin::class); |
1080
|
|
|
|
1081
|
|
|
$admin->expects($this->once()) |
1082
|
|
|
->method('getObject') |
1083
|
|
|
->will($this->returnValue($object2)); |
1084
|
|
|
|
1085
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1086
|
|
|
->method('getObject') |
1087
|
|
|
->will($this->returnValue($object)); |
1088
|
|
|
|
1089
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1090
|
|
|
->method('getParent') |
1091
|
|
|
->will($this->returnValue($admin)); |
1092
|
|
|
|
1093
|
|
|
$this->admin->expects($this->exactly(2)) |
|
|
|
|
1094
|
|
|
->method('getParentAssociationMapping') |
1095
|
|
|
->will($this->returnValue('parent')); |
1096
|
|
|
|
1097
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1098
|
|
|
} |
1099
|
|
|
|
1100
|
|
|
public function testDeleteActionNoParentMappings() |
1101
|
|
|
{ |
1102
|
|
|
$object = new \stdClass(); |
1103
|
|
|
|
1104
|
|
|
$admin = $this->createMock(PostAdmin::class); |
1105
|
|
|
|
1106
|
|
|
$admin->expects($this->never()) |
1107
|
|
|
->method('getObject'); |
1108
|
|
|
|
1109
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1110
|
|
|
->method('getObject') |
1111
|
|
|
->will($this->returnValue($object)); |
1112
|
|
|
|
1113
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1114
|
|
|
->method('getParent') |
1115
|
|
|
->will($this->returnValue($admin)); |
1116
|
|
|
|
1117
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1118
|
|
|
->method('getParentAssociationMapping') |
1119
|
|
|
->will($this->returnValue(false)); |
1120
|
|
|
|
1121
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1122
|
|
|
} |
1123
|
|
|
|
1124
|
|
|
public function testDeleteActionNoCsrfToken() |
1125
|
|
|
{ |
1126
|
|
|
$this->csrfProvider = null; |
1127
|
|
|
|
1128
|
|
|
$object = new \stdClass(); |
1129
|
|
|
|
1130
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1131
|
|
|
->method('getObject') |
1132
|
|
|
->will($this->returnValue($object)); |
1133
|
|
|
|
1134
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1135
|
|
|
->method('checkAccess') |
1136
|
|
|
->with($this->equalTo('delete')) |
1137
|
|
|
->will($this->returnValue(true)); |
1138
|
|
|
|
1139
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->deleteAction(1, $this->request)); |
|
|
|
|
1140
|
|
|
|
1141
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1142
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
1143
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1144
|
|
|
|
1145
|
|
|
$this->assertSame('delete', $this->parameters['action']); |
1146
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1147
|
|
|
$this->assertFalse($this->parameters['csrf_token']); |
1148
|
|
|
|
1149
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1150
|
|
|
$this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
1151
|
|
|
} |
1152
|
|
|
|
1153
|
|
|
public function testDeleteActionAjaxSuccess1() |
1154
|
|
|
{ |
1155
|
|
|
$object = new \stdClass(); |
1156
|
|
|
|
1157
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1158
|
|
|
->method('getObject') |
1159
|
|
|
->will($this->returnValue($object)); |
1160
|
|
|
|
1161
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1162
|
|
|
->method('checkAccess') |
1163
|
|
|
->with($this->equalTo('delete')) |
1164
|
|
|
->will($this->returnValue(true)); |
1165
|
|
|
|
1166
|
|
|
$this->request->setMethod('DELETE'); |
1167
|
|
|
|
1168
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1169
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1170
|
|
|
|
1171
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1172
|
|
|
|
1173
|
|
|
$this->assertInstanceOf(Response::class, $response); |
1174
|
|
|
$this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
1175
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1176
|
|
|
} |
1177
|
|
|
|
1178
|
|
|
public function testDeleteActionAjaxSuccess2() |
1179
|
|
|
{ |
1180
|
|
|
$object = new \stdClass(); |
1181
|
|
|
|
1182
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1183
|
|
|
->method('getObject') |
1184
|
|
|
->will($this->returnValue($object)); |
1185
|
|
|
|
1186
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1187
|
|
|
->method('checkAccess') |
1188
|
|
|
->with($this->equalTo('delete')) |
1189
|
|
|
->will($this->returnValue(true)); |
1190
|
|
|
|
1191
|
|
|
$this->request->setMethod('POST'); |
1192
|
|
|
$this->request->request->set('_method', 'DELETE'); |
1193
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1194
|
|
|
|
1195
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1196
|
|
|
|
1197
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1198
|
|
|
|
1199
|
|
|
$this->assertInstanceOf(Response::class, $response); |
1200
|
|
|
$this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
1201
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1202
|
|
|
} |
1203
|
|
|
|
1204
|
|
|
public function testDeleteActionAjaxError() |
1205
|
|
|
{ |
1206
|
|
|
$object = new \stdClass(); |
1207
|
|
|
|
1208
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1209
|
|
|
->method('getObject') |
1210
|
|
|
->will($this->returnValue($object)); |
1211
|
|
|
|
1212
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1213
|
|
|
->method('checkAccess') |
1214
|
|
|
->with($this->equalTo('delete')) |
1215
|
|
|
->will($this->returnValue(true)); |
1216
|
|
|
|
1217
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1218
|
|
|
->method('getClass') |
1219
|
|
|
->will($this->returnValue('stdClass')); |
1220
|
|
|
|
1221
|
|
|
$this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
1222
|
|
|
|
1223
|
|
|
$this->request->setMethod('DELETE'); |
1224
|
|
|
|
1225
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1226
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1227
|
|
|
|
1228
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1229
|
|
|
|
1230
|
|
|
$this->assertInstanceOf(Response::class, $response); |
1231
|
|
|
$this->assertSame(json_encode(['result' => 'error']), $response->getContent()); |
1232
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1233
|
|
|
} |
1234
|
|
|
|
1235
|
|
|
public function testDeleteActionWithModelManagerExceptionInDebugMode() |
1236
|
|
|
{ |
1237
|
|
|
$this->expectException(ModelManagerException::class); |
1238
|
|
|
|
1239
|
|
|
$object = new \stdClass(); |
1240
|
|
|
|
1241
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1242
|
|
|
->method('getObject') |
1243
|
|
|
->will($this->returnValue($object)); |
1244
|
|
|
|
1245
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1246
|
|
|
->method('checkAccess') |
1247
|
|
|
->with($this->equalTo('delete')) |
1248
|
|
|
->will($this->returnValue(true)); |
1249
|
|
|
|
1250
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1251
|
|
|
->method('delete') |
1252
|
|
|
->will($this->returnCallback(function () { |
1253
|
|
|
throw new ModelManagerException(); |
1254
|
|
|
})); |
1255
|
|
|
|
1256
|
|
|
$this->kernel->expects($this->once()) |
|
|
|
|
1257
|
|
|
->method('isDebug') |
1258
|
|
|
->will($this->returnValue(true)); |
1259
|
|
|
|
1260
|
|
|
$this->request->setMethod('DELETE'); |
1261
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1262
|
|
|
|
1263
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1264
|
|
|
} |
1265
|
|
|
|
1266
|
|
|
/** |
1267
|
|
|
* @dataProvider getToStringValues |
1268
|
|
|
*/ |
1269
|
|
|
public function testDeleteActionSuccess1($expectedToStringValue, $toStringValue) |
1270
|
|
|
{ |
1271
|
|
|
$object = new \stdClass(); |
1272
|
|
|
|
1273
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1274
|
|
|
->method('getObject') |
1275
|
|
|
->will($this->returnValue($object)); |
1276
|
|
|
|
1277
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1278
|
|
|
->method('toString') |
1279
|
|
|
->with($this->equalTo($object)) |
1280
|
|
|
->will($this->returnValue($toStringValue)); |
1281
|
|
|
|
1282
|
|
|
$this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1283
|
|
|
|
1284
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1285
|
|
|
->method('checkAccess') |
1286
|
|
|
->with($this->equalTo('delete')) |
1287
|
|
|
->will($this->returnValue(true)); |
1288
|
|
|
|
1289
|
|
|
$this->request->setMethod('DELETE'); |
1290
|
|
|
|
1291
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1292
|
|
|
|
1293
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1294
|
|
|
|
1295
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
1296
|
|
|
$this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
1297
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
1298
|
|
|
} |
1299
|
|
|
|
1300
|
|
|
/** |
1301
|
|
|
* @dataProvider getToStringValues |
1302
|
|
|
*/ |
1303
|
|
|
public function testDeleteActionSuccess2($expectedToStringValue, $toStringValue) |
1304
|
|
|
{ |
1305
|
|
|
$object = new \stdClass(); |
1306
|
|
|
|
1307
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1308
|
|
|
->method('getObject') |
1309
|
|
|
->will($this->returnValue($object)); |
1310
|
|
|
|
1311
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1312
|
|
|
->method('checkAccess') |
1313
|
|
|
->with($this->equalTo('delete')) |
1314
|
|
|
->will($this->returnValue(true)); |
1315
|
|
|
|
1316
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1317
|
|
|
->method('toString') |
1318
|
|
|
->with($this->equalTo($object)) |
1319
|
|
|
->will($this->returnValue($toStringValue)); |
1320
|
|
|
|
1321
|
|
|
$this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1322
|
|
|
|
1323
|
|
|
$this->request->setMethod('POST'); |
1324
|
|
|
$this->request->request->set('_method', 'DELETE'); |
1325
|
|
|
|
1326
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1327
|
|
|
|
1328
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1329
|
|
|
|
1330
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
1331
|
|
|
$this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
1332
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
1333
|
|
|
} |
1334
|
|
|
|
1335
|
|
|
/** |
1336
|
|
|
* @dataProvider getToStringValues |
1337
|
|
|
*/ |
1338
|
|
|
public function testDeleteActionSuccessNoCsrfTokenProvider($expectedToStringValue, $toStringValue) |
1339
|
|
|
{ |
1340
|
|
|
$this->csrfProvider = null; |
1341
|
|
|
|
1342
|
|
|
$object = new \stdClass(); |
1343
|
|
|
|
1344
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1345
|
|
|
->method('getObject') |
1346
|
|
|
->will($this->returnValue($object)); |
1347
|
|
|
|
1348
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1349
|
|
|
->method('checkAccess') |
1350
|
|
|
->with($this->equalTo('delete')) |
1351
|
|
|
->will($this->returnValue(true)); |
1352
|
|
|
|
1353
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1354
|
|
|
->method('toString') |
1355
|
|
|
->with($this->equalTo($object)) |
1356
|
|
|
->will($this->returnValue($toStringValue)); |
1357
|
|
|
|
1358
|
|
|
$this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1359
|
|
|
|
1360
|
|
|
$this->request->setMethod('POST'); |
1361
|
|
|
$this->request->request->set('_method', 'DELETE'); |
1362
|
|
|
|
1363
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1364
|
|
|
|
1365
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
1366
|
|
|
$this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
1367
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
1368
|
|
|
} |
1369
|
|
|
|
1370
|
|
|
public function testDeleteActionWrongRequestMethod() |
1371
|
|
|
{ |
1372
|
|
|
$object = new \stdClass(); |
1373
|
|
|
|
1374
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1375
|
|
|
->method('getObject') |
1376
|
|
|
->will($this->returnValue($object)); |
1377
|
|
|
|
1378
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1379
|
|
|
->method('checkAccess') |
1380
|
|
|
->with($this->equalTo('delete')) |
1381
|
|
|
->will($this->returnValue(true)); |
1382
|
|
|
|
1383
|
|
|
//without POST request parameter "_method" should not be used as real REST method |
1384
|
|
|
$this->request->query->set('_method', 'DELETE'); |
1385
|
|
|
|
1386
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->deleteAction(1, $this->request)); |
|
|
|
|
1387
|
|
|
|
1388
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1389
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
1390
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1391
|
|
|
|
1392
|
|
|
$this->assertSame('delete', $this->parameters['action']); |
1393
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1394
|
|
|
$this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
1395
|
|
|
|
1396
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1397
|
|
|
$this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template); |
1398
|
|
|
} |
1399
|
|
|
|
1400
|
|
|
/** |
1401
|
|
|
* @dataProvider getToStringValues |
1402
|
|
|
*/ |
1403
|
|
|
public function testDeleteActionError($expectedToStringValue, $toStringValue) |
1404
|
|
|
{ |
1405
|
|
|
$object = new \stdClass(); |
1406
|
|
|
|
1407
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1408
|
|
|
->method('getObject') |
1409
|
|
|
->will($this->returnValue($object)); |
1410
|
|
|
|
1411
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1412
|
|
|
->method('checkAccess') |
1413
|
|
|
->with($this->equalTo('delete')) |
1414
|
|
|
->will($this->returnValue(true)); |
1415
|
|
|
|
1416
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1417
|
|
|
->method('toString') |
1418
|
|
|
->with($this->equalTo($object)) |
1419
|
|
|
->will($this->returnValue($toStringValue)); |
1420
|
|
|
|
1421
|
|
|
$this->expectTranslate('flash_delete_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1422
|
|
|
|
1423
|
|
|
$this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
1424
|
|
|
|
1425
|
|
|
$this->request->setMethod('DELETE'); |
1426
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1427
|
|
|
|
1428
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1429
|
|
|
|
1430
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
1431
|
|
|
$this->assertSame(['flash_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error')); |
1432
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
1433
|
|
|
} |
1434
|
|
|
|
1435
|
|
|
public function testDeleteActionInvalidCsrfToken() |
1436
|
|
|
{ |
1437
|
|
|
$object = new \stdClass(); |
1438
|
|
|
|
1439
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1440
|
|
|
->method('getObject') |
1441
|
|
|
->will($this->returnValue($object)); |
1442
|
|
|
|
1443
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1444
|
|
|
->method('checkAccess') |
1445
|
|
|
->with($this->equalTo('delete')) |
1446
|
|
|
->will($this->returnValue(true)); |
1447
|
|
|
|
1448
|
|
|
$this->request->setMethod('POST'); |
1449
|
|
|
$this->request->request->set('_method', 'DELETE'); |
1450
|
|
|
$this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID'); |
1451
|
|
|
|
1452
|
|
|
try { |
1453
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1454
|
|
|
} catch (HttpException $e) { |
1455
|
|
|
$this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage()); |
1456
|
|
|
$this->assertSame(400, $e->getStatusCode()); |
1457
|
|
|
} |
1458
|
|
|
} |
1459
|
|
|
|
1460
|
|
|
public function testEditActionNotFoundException() |
1461
|
|
|
{ |
1462
|
|
|
$this->expectException(NotFoundHttpException::class); |
1463
|
|
|
|
1464
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1465
|
|
|
->method('getObject') |
1466
|
|
|
->will($this->returnValue(false)); |
1467
|
|
|
|
1468
|
|
|
$this->controller->editAction(null, $this->request); |
|
|
|
|
1469
|
|
|
} |
1470
|
|
|
|
1471
|
|
|
public function testEditActionRuntimeException() |
1472
|
|
|
{ |
1473
|
|
|
$this->expectException(\RuntimeException::class); |
1474
|
|
|
|
1475
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1476
|
|
|
->method('getObject') |
1477
|
|
|
->will($this->returnValue(new \stdClass())); |
1478
|
|
|
|
1479
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1480
|
|
|
->method('checkAccess') |
1481
|
|
|
->with($this->equalTo('edit')) |
1482
|
|
|
->will($this->returnValue(true)); |
1483
|
|
|
|
1484
|
|
|
$form = $this->createMock(Form::class); |
1485
|
|
|
|
1486
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1487
|
|
|
->method('getForm') |
1488
|
|
|
->will($this->returnValue($form)); |
1489
|
|
|
|
1490
|
|
|
$form->expects($this->once()) |
1491
|
|
|
->method('all') |
1492
|
|
|
->willReturn([]); |
1493
|
|
|
|
1494
|
|
|
$this->controller->editAction(null, $this->request); |
|
|
|
|
1495
|
|
|
} |
1496
|
|
|
|
1497
|
|
|
public function testEditActionAccessDenied() |
1498
|
|
|
{ |
1499
|
|
|
$this->expectException(AccessDeniedException::class); |
1500
|
|
|
|
1501
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1502
|
|
|
->method('getObject') |
1503
|
|
|
->will($this->returnValue(new \stdClass())); |
1504
|
|
|
|
1505
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1506
|
|
|
->method('checkAccess') |
1507
|
|
|
->with($this->equalTo('edit')) |
1508
|
|
|
->will($this->throwException(new AccessDeniedException())); |
1509
|
|
|
|
1510
|
|
|
$this->controller->editAction(null, $this->request); |
|
|
|
|
1511
|
|
|
} |
1512
|
|
|
|
1513
|
|
|
public function testPreEdit() |
1514
|
|
|
{ |
1515
|
|
|
$object = new \stdClass(); |
1516
|
|
|
$object->foo = 123456; |
1517
|
|
|
|
1518
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1519
|
|
|
->method('getObject') |
1520
|
|
|
->will($this->returnValue($object)); |
1521
|
|
|
|
1522
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1523
|
|
|
->method('checkAccess') |
1524
|
|
|
->with($this->equalTo('edit')) |
1525
|
|
|
->will($this->returnValue(true)); |
1526
|
|
|
|
1527
|
|
|
$controller = new PreCRUDController(); |
1528
|
|
|
$controller->setContainer($this->container); |
1529
|
|
|
|
1530
|
|
|
$response = $controller->editAction(null, $this->request); |
|
|
|
|
1531
|
|
|
$this->assertInstanceOf(Response::class, $response); |
1532
|
|
|
$this->assertSame('preEdit called: 123456', $response->getContent()); |
1533
|
|
|
} |
1534
|
|
|
|
1535
|
|
|
public function testEditAction() |
1536
|
|
|
{ |
1537
|
|
|
$object = new \stdClass(); |
1538
|
|
|
|
1539
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1540
|
|
|
->method('getObject') |
1541
|
|
|
->will($this->returnValue($object)); |
1542
|
|
|
|
1543
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1544
|
|
|
->method('checkAccess') |
1545
|
|
|
->with($this->equalTo('edit')) |
1546
|
|
|
->will($this->returnValue(true)); |
1547
|
|
|
|
1548
|
|
|
$form = $this->createMock(Form::class); |
1549
|
|
|
|
1550
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1551
|
|
|
->method('getForm') |
1552
|
|
|
->will($this->returnValue($form)); |
1553
|
|
|
|
1554
|
|
|
$formView = $this->createMock(FormView::class); |
1555
|
|
|
|
1556
|
|
|
$form->expects($this->any()) |
1557
|
|
|
->method('createView') |
1558
|
|
|
->will($this->returnValue($formView)); |
1559
|
|
|
|
1560
|
|
|
$form->expects($this->once()) |
1561
|
|
|
->method('all') |
1562
|
|
|
->willReturn(['field' => 'fielddata']); |
1563
|
|
|
|
1564
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
|
|
|
|
1565
|
|
|
|
1566
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1567
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
1568
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1569
|
|
|
|
1570
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1571
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
1572
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1573
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1574
|
|
|
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
1575
|
|
|
} |
1576
|
|
|
|
1577
|
|
|
/** |
1578
|
|
|
* @dataProvider getToStringValues |
1579
|
|
|
*/ |
1580
|
|
|
public function testEditActionSuccess($expectedToStringValue, $toStringValue) |
1581
|
|
|
{ |
1582
|
|
|
$object = new \stdClass(); |
1583
|
|
|
|
1584
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1585
|
|
|
->method('getObject') |
1586
|
|
|
->will($this->returnValue($object)); |
1587
|
|
|
|
1588
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1589
|
|
|
->method('update') |
1590
|
|
|
->will($this->returnArgument(0)); |
1591
|
|
|
|
1592
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1593
|
|
|
->method('checkAccess') |
1594
|
|
|
->with($this->equalTo('edit')); |
1595
|
|
|
|
1596
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1597
|
|
|
->method('hasRoute') |
1598
|
|
|
->with($this->equalTo('edit')) |
1599
|
|
|
->will($this->returnValue(true)); |
1600
|
|
|
|
1601
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1602
|
|
|
->method('hasAccess') |
1603
|
|
|
->with($this->equalTo('edit')) |
1604
|
|
|
->will($this->returnValue(true)); |
1605
|
|
|
|
1606
|
|
|
$form = $this->createMock(Form::class); |
1607
|
|
|
|
1608
|
|
|
$form->expects($this->once()) |
1609
|
|
|
->method('getData') |
1610
|
|
|
->will($this->returnValue($object)); |
1611
|
|
|
|
1612
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1613
|
|
|
->method('getForm') |
1614
|
|
|
->will($this->returnValue($form)); |
1615
|
|
|
|
1616
|
|
|
$form->expects($this->once()) |
1617
|
|
|
->method('isSubmitted') |
1618
|
|
|
->will($this->returnValue(true)); |
1619
|
|
|
|
1620
|
|
|
$form->expects($this->once()) |
1621
|
|
|
->method('isValid') |
1622
|
|
|
->will($this->returnValue(true)); |
1623
|
|
|
|
1624
|
|
|
$form->expects($this->once()) |
1625
|
|
|
->method('all') |
1626
|
|
|
->willReturn(['field' => 'fielddata']); |
1627
|
|
|
|
1628
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1629
|
|
|
->method('toString') |
1630
|
|
|
->with($this->equalTo($object)) |
1631
|
|
|
->will($this->returnValue($toStringValue)); |
1632
|
|
|
|
1633
|
|
|
$this->expectTranslate('flash_edit_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1634
|
|
|
|
1635
|
|
|
$this->request->setMethod('POST'); |
1636
|
|
|
|
1637
|
|
|
$response = $this->controller->editAction(null, $this->request); |
|
|
|
|
1638
|
|
|
|
1639
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
1640
|
|
|
$this->assertSame(['flash_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
1641
|
|
|
$this->assertSame('stdClass_edit', $response->getTargetUrl()); |
1642
|
|
|
} |
1643
|
|
|
|
1644
|
|
|
/** |
1645
|
|
|
* @dataProvider getToStringValues |
1646
|
|
|
*/ |
1647
|
|
|
public function testEditActionError($expectedToStringValue, $toStringValue) |
1648
|
|
|
{ |
1649
|
|
|
$object = new \stdClass(); |
1650
|
|
|
|
1651
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1652
|
|
|
->method('getObject') |
1653
|
|
|
->will($this->returnValue($object)); |
1654
|
|
|
|
1655
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1656
|
|
|
->method('checkAccess') |
1657
|
|
|
->with($this->equalTo('edit')) |
1658
|
|
|
->will($this->returnValue(true)); |
1659
|
|
|
|
1660
|
|
|
$form = $this->createMock(Form::class); |
1661
|
|
|
|
1662
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1663
|
|
|
->method('getForm') |
1664
|
|
|
->will($this->returnValue($form)); |
1665
|
|
|
|
1666
|
|
|
$form->expects($this->once()) |
1667
|
|
|
->method('isSubmitted') |
1668
|
|
|
->will($this->returnValue(true)); |
1669
|
|
|
|
1670
|
|
|
$form->expects($this->once()) |
1671
|
|
|
->method('isValid') |
1672
|
|
|
->will($this->returnValue(false)); |
1673
|
|
|
|
1674
|
|
|
$form->expects($this->once()) |
1675
|
|
|
->method('all') |
1676
|
|
|
->willReturn(['field' => 'fielddata']); |
1677
|
|
|
|
1678
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1679
|
|
|
->method('toString') |
1680
|
|
|
->with($this->equalTo($object)) |
1681
|
|
|
->will($this->returnValue($toStringValue)); |
1682
|
|
|
|
1683
|
|
|
$this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1684
|
|
|
|
1685
|
|
|
$this->request->setMethod('POST'); |
1686
|
|
|
|
1687
|
|
|
$formView = $this->createMock(FormView::class); |
1688
|
|
|
|
1689
|
|
|
$form->expects($this->any()) |
1690
|
|
|
->method('createView') |
1691
|
|
|
->will($this->returnValue($formView)); |
1692
|
|
|
|
1693
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
|
|
|
|
1694
|
|
|
|
1695
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1696
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
1697
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1698
|
|
|
|
1699
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1700
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
1701
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1702
|
|
|
|
1703
|
|
|
$this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
1704
|
|
|
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
1705
|
|
|
} |
1706
|
|
|
|
1707
|
|
|
public function testEditActionAjaxSuccess() |
1708
|
|
|
{ |
1709
|
|
|
$object = new \stdClass(); |
1710
|
|
|
|
1711
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1712
|
|
|
->method('getObject') |
1713
|
|
|
->will($this->returnValue($object)); |
1714
|
|
|
|
1715
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1716
|
|
|
->method('update') |
1717
|
|
|
->will($this->returnArgument(0)); |
1718
|
|
|
|
1719
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1720
|
|
|
->method('checkAccess') |
1721
|
|
|
->with($this->equalTo('edit')) |
1722
|
|
|
->will($this->returnValue(true)); |
1723
|
|
|
|
1724
|
|
|
$form = $this->createMock(Form::class); |
1725
|
|
|
|
1726
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1727
|
|
|
->method('getForm') |
1728
|
|
|
->will($this->returnValue($form)); |
1729
|
|
|
|
1730
|
|
|
$form->expects($this->once()) |
1731
|
|
|
->method('isSubmitted') |
1732
|
|
|
->will($this->returnValue(true)); |
1733
|
|
|
|
1734
|
|
|
$form->expects($this->once()) |
1735
|
|
|
->method('isValid') |
1736
|
|
|
->will($this->returnValue(true)); |
1737
|
|
|
|
1738
|
|
|
$form->expects($this->once()) |
1739
|
|
|
->method('getData') |
1740
|
|
|
->will($this->returnValue($object)); |
1741
|
|
|
|
1742
|
|
|
$form->expects($this->once()) |
1743
|
|
|
->method('all') |
1744
|
|
|
->willReturn(['field' => 'fielddata']); |
1745
|
|
|
|
1746
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1747
|
|
|
->method('getNormalizedIdentifier') |
1748
|
|
|
->with($this->equalTo($object)) |
1749
|
|
|
->will($this->returnValue('foo_normalized')); |
1750
|
|
|
|
1751
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1752
|
|
|
->method('toString') |
1753
|
|
|
->will($this->returnValue('foo')); |
1754
|
|
|
|
1755
|
|
|
$this->request->setMethod('POST'); |
1756
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1757
|
|
|
|
1758
|
|
|
$response = $this->controller->editAction(null, $this->request); |
|
|
|
|
1759
|
|
|
|
1760
|
|
|
$this->assertInstanceOf(Response::class, $response); |
1761
|
|
|
$this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent()); |
1762
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1763
|
|
|
} |
1764
|
|
|
|
1765
|
|
|
public function testEditActionAjaxError() |
1766
|
|
|
{ |
1767
|
|
|
$object = new \stdClass(); |
1768
|
|
|
|
1769
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1770
|
|
|
->method('getObject') |
1771
|
|
|
->will($this->returnValue($object)); |
1772
|
|
|
|
1773
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1774
|
|
|
->method('checkAccess') |
1775
|
|
|
->with($this->equalTo('edit')) |
1776
|
|
|
->will($this->returnValue(true)); |
1777
|
|
|
|
1778
|
|
|
$form = $this->createMock(Form::class); |
1779
|
|
|
|
1780
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1781
|
|
|
->method('getForm') |
1782
|
|
|
->will($this->returnValue($form)); |
1783
|
|
|
|
1784
|
|
|
$form->expects($this->once()) |
1785
|
|
|
->method('isSubmitted') |
1786
|
|
|
->will($this->returnValue(true)); |
1787
|
|
|
|
1788
|
|
|
$form->expects($this->once()) |
1789
|
|
|
->method('isValid') |
1790
|
|
|
->will($this->returnValue(false)); |
1791
|
|
|
|
1792
|
|
|
$form->expects($this->once()) |
1793
|
|
|
->method('all') |
1794
|
|
|
->willReturn(['field' => 'fielddata']); |
1795
|
|
|
|
1796
|
|
|
$this->request->setMethod('POST'); |
1797
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1798
|
|
|
|
1799
|
|
|
$formView = $this->createMock(FormView::class); |
1800
|
|
|
|
1801
|
|
|
$form->expects($this->any()) |
1802
|
|
|
->method('createView') |
1803
|
|
|
->will($this->returnValue($formView)); |
1804
|
|
|
|
1805
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
|
|
|
|
1806
|
|
|
|
1807
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1808
|
|
|
$this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']); |
1809
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1810
|
|
|
|
1811
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1812
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
1813
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1814
|
|
|
|
1815
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1816
|
|
|
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
1817
|
|
|
} |
1818
|
|
|
|
1819
|
|
|
/** |
1820
|
|
|
* @dataProvider getToStringValues |
1821
|
|
|
*/ |
1822
|
|
|
public function testEditActionWithModelManagerException($expectedToStringValue, $toStringValue) |
1823
|
|
|
{ |
1824
|
|
|
$object = new \stdClass(); |
1825
|
|
|
|
1826
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1827
|
|
|
->method('getObject') |
1828
|
|
|
->will($this->returnValue($object)); |
1829
|
|
|
|
1830
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1831
|
|
|
->method('checkAccess') |
1832
|
|
|
->with($this->equalTo('edit')) |
1833
|
|
|
->will($this->returnValue(true)); |
1834
|
|
|
|
1835
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1836
|
|
|
->method('getClass') |
1837
|
|
|
->will($this->returnValue('stdClass')); |
1838
|
|
|
|
1839
|
|
|
$form = $this->createMock(Form::class); |
1840
|
|
|
|
1841
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1842
|
|
|
->method('getForm') |
1843
|
|
|
->will($this->returnValue($form)); |
1844
|
|
|
|
1845
|
|
|
$form->expects($this->once()) |
1846
|
|
|
->method('isValid') |
1847
|
|
|
->will($this->returnValue(true)); |
1848
|
|
|
|
1849
|
|
|
$form->expects($this->once()) |
1850
|
|
|
->method('getData') |
1851
|
|
|
->will($this->returnValue($object)); |
1852
|
|
|
|
1853
|
|
|
$form->expects($this->once()) |
1854
|
|
|
->method('all') |
1855
|
|
|
->willReturn(['field' => 'fielddata']); |
1856
|
|
|
|
1857
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1858
|
|
|
->method('toString') |
1859
|
|
|
->with($this->equalTo($object)) |
1860
|
|
|
->will($this->returnValue($toStringValue)); |
1861
|
|
|
|
1862
|
|
|
$this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1863
|
|
|
|
1864
|
|
|
$form->expects($this->once()) |
1865
|
|
|
->method('isSubmitted') |
1866
|
|
|
->will($this->returnValue(true)); |
1867
|
|
|
$this->request->setMethod('POST'); |
1868
|
|
|
|
1869
|
|
|
$formView = $this->createMock(FormView::class); |
1870
|
|
|
|
1871
|
|
|
$form->expects($this->any()) |
1872
|
|
|
->method('createView') |
1873
|
|
|
->will($this->returnValue($formView)); |
1874
|
|
|
|
1875
|
|
|
$this->assertLoggerLogsModelManagerException($this->admin, 'update'); |
1876
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
|
|
|
|
1877
|
|
|
|
1878
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1879
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
1880
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1881
|
|
|
|
1882
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1883
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
1884
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1885
|
|
|
|
1886
|
|
|
$this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
1887
|
|
|
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
1888
|
|
|
} |
1889
|
|
|
|
1890
|
|
|
public function testEditActionWithPreview() |
1891
|
|
|
{ |
1892
|
|
|
$object = new \stdClass(); |
1893
|
|
|
|
1894
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1895
|
|
|
->method('getObject') |
1896
|
|
|
->will($this->returnValue($object)); |
1897
|
|
|
|
1898
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1899
|
|
|
->method('checkAccess') |
1900
|
|
|
->with($this->equalTo('edit')) |
1901
|
|
|
->will($this->returnValue(true)); |
1902
|
|
|
|
1903
|
|
|
$form = $this->createMock(Form::class); |
1904
|
|
|
|
1905
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1906
|
|
|
->method('getForm') |
1907
|
|
|
->will($this->returnValue($form)); |
1908
|
|
|
|
1909
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1910
|
|
|
->method('supportsPreviewMode') |
1911
|
|
|
->will($this->returnValue(true)); |
1912
|
|
|
|
1913
|
|
|
$formView = $this->createMock(FormView::class); |
1914
|
|
|
|
1915
|
|
|
$form->expects($this->any()) |
1916
|
|
|
->method('createView') |
1917
|
|
|
->will($this->returnValue($formView)); |
1918
|
|
|
|
1919
|
|
|
$form->expects($this->once()) |
1920
|
|
|
->method('isSubmitted') |
1921
|
|
|
->will($this->returnValue(true)); |
1922
|
|
|
|
1923
|
|
|
$form->expects($this->once()) |
1924
|
|
|
->method('isValid') |
1925
|
|
|
->will($this->returnValue(true)); |
1926
|
|
|
|
1927
|
|
|
$form->expects($this->once()) |
1928
|
|
|
->method('all') |
1929
|
|
|
->willReturn(['field' => 'fielddata']); |
1930
|
|
|
|
1931
|
|
|
$this->request->setMethod('POST'); |
1932
|
|
|
$this->request->request->set('btn_preview', 'Preview'); |
1933
|
|
|
|
1934
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
|
|
|
|
1935
|
|
|
|
1936
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1937
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
1938
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1939
|
|
|
|
1940
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1941
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
1942
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1943
|
|
|
|
1944
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1945
|
|
|
$this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template); |
1946
|
|
|
} |
1947
|
|
|
|
1948
|
|
|
public function testEditActionWithLockException() |
1949
|
|
|
{ |
1950
|
|
|
$object = new \stdClass(); |
1951
|
|
|
$class = \get_class($object); |
1952
|
|
|
|
1953
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1954
|
|
|
->method('getObject') |
1955
|
|
|
->will($this->returnValue($object)); |
1956
|
|
|
|
1957
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1958
|
|
|
->method('checkAccess') |
1959
|
|
|
->with($this->equalTo('edit')) |
1960
|
|
|
->will($this->returnValue(true)); |
1961
|
|
|
|
1962
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1963
|
|
|
->method('getClass') |
1964
|
|
|
->will($this->returnValue($class)); |
1965
|
|
|
|
1966
|
|
|
$form = $this->createMock(Form::class); |
1967
|
|
|
|
1968
|
|
|
$form->expects($this->any()) |
1969
|
|
|
->method('isValid') |
1970
|
|
|
->will($this->returnValue(true)); |
1971
|
|
|
|
1972
|
|
|
$form->expects($this->once()) |
1973
|
|
|
->method('getData') |
1974
|
|
|
->will($this->returnValue($object)); |
1975
|
|
|
|
1976
|
|
|
$form->expects($this->once()) |
1977
|
|
|
->method('all') |
1978
|
|
|
->willReturn(['field' => 'fielddata']); |
1979
|
|
|
|
1980
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1981
|
|
|
->method('getForm') |
1982
|
|
|
->will($this->returnValue($form)); |
1983
|
|
|
|
1984
|
|
|
$form->expects($this->any()) |
1985
|
|
|
->method('isSubmitted') |
1986
|
|
|
->will($this->returnValue(true)); |
1987
|
|
|
$this->request->setMethod('POST'); |
1988
|
|
|
|
1989
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1990
|
|
|
->method('update') |
1991
|
|
|
->will($this->throwException(new LockException())); |
1992
|
|
|
|
1993
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1994
|
|
|
->method('toString') |
1995
|
|
|
->with($this->equalTo($object)) |
1996
|
|
|
->will($this->returnValue($class)); |
1997
|
|
|
|
1998
|
|
|
$formView = $this->createMock(FormView::class); |
1999
|
|
|
|
2000
|
|
|
$form->expects($this->any()) |
2001
|
|
|
->method('createView') |
2002
|
|
|
->will($this->returnValue($formView)); |
2003
|
|
|
|
2004
|
|
|
$this->expectTranslate('flash_lock_error', [ |
2005
|
|
|
'%name%' => $class, |
2006
|
|
|
'%link_start%' => '<a href="stdClass_edit">', |
2007
|
|
|
'%link_end%' => '</a>', |
2008
|
|
|
], 'SonataAdminBundle'); |
2009
|
|
|
|
2010
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request)); |
|
|
|
|
2011
|
|
|
} |
2012
|
|
|
|
2013
|
|
|
public function testCreateActionAccessDenied() |
2014
|
|
|
{ |
2015
|
|
|
$this->expectException(AccessDeniedException::class); |
2016
|
|
|
|
2017
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2018
|
|
|
->method('checkAccess') |
2019
|
|
|
->with($this->equalTo('create')) |
2020
|
|
|
->will($this->throwException(new AccessDeniedException())); |
2021
|
|
|
|
2022
|
|
|
$this->controller->createAction($this->request); |
|
|
|
|
2023
|
|
|
} |
2024
|
|
|
|
2025
|
|
|
public function testCreateActionRuntimeException() |
2026
|
|
|
{ |
2027
|
|
|
$this->expectException(\RuntimeException::class); |
2028
|
|
|
|
2029
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2030
|
|
|
->method('checkAccess') |
2031
|
|
|
->with($this->equalTo('create')) |
2032
|
|
|
->will($this->returnValue(true)); |
2033
|
|
|
|
2034
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2035
|
|
|
->method('getClass') |
2036
|
|
|
->will($this->returnValue('stdClass')); |
2037
|
|
|
|
2038
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2039
|
|
|
->method('getNewInstance') |
2040
|
|
|
->will($this->returnValue(new \stdClass())); |
2041
|
|
|
|
2042
|
|
|
$form = $this->createMock(Form::class); |
2043
|
|
|
|
2044
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2045
|
|
|
->method('getForm') |
2046
|
|
|
->will($this->returnValue($form)); |
2047
|
|
|
|
2048
|
|
|
$form->expects($this->once()) |
2049
|
|
|
->method('all') |
2050
|
|
|
->willReturn([]); |
2051
|
|
|
|
2052
|
|
|
$this->controller->createAction($this->request); |
|
|
|
|
2053
|
|
|
} |
2054
|
|
|
|
2055
|
|
|
public function testPreCreate() |
2056
|
|
|
{ |
2057
|
|
|
$object = new \stdClass(); |
2058
|
|
|
$object->foo = 123456; |
2059
|
|
|
|
2060
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2061
|
|
|
->method('checkAccess') |
2062
|
|
|
->with($this->equalTo('create')) |
2063
|
|
|
->will($this->returnValue(true)); |
2064
|
|
|
|
2065
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2066
|
|
|
->method('getClass') |
2067
|
|
|
->will($this->returnValue('stdClass')); |
2068
|
|
|
|
2069
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2070
|
|
|
->method('getNewInstance') |
2071
|
|
|
->will($this->returnValue($object)); |
2072
|
|
|
|
2073
|
|
|
$controller = new PreCRUDController(); |
2074
|
|
|
$controller->setContainer($this->container); |
2075
|
|
|
|
2076
|
|
|
$response = $controller->createAction($this->request); |
|
|
|
|
2077
|
|
|
$this->assertInstanceOf(Response::class, $response); |
2078
|
|
|
$this->assertSame('preCreate called: 123456', $response->getContent()); |
2079
|
|
|
} |
2080
|
|
|
|
2081
|
|
|
public function testCreateAction() |
2082
|
|
|
{ |
2083
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2084
|
|
|
->method('checkAccess') |
2085
|
|
|
->with($this->equalTo('create')) |
2086
|
|
|
->will($this->returnValue(true)); |
2087
|
|
|
|
2088
|
|
|
$object = new \stdClass(); |
2089
|
|
|
|
2090
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2091
|
|
|
->method('getClass') |
2092
|
|
|
->will($this->returnValue('stdClass')); |
2093
|
|
|
|
2094
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2095
|
|
|
->method('getNewInstance') |
2096
|
|
|
->will($this->returnValue($object)); |
2097
|
|
|
|
2098
|
|
|
$form = $this->createMock(Form::class); |
2099
|
|
|
|
2100
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2101
|
|
|
->method('getForm') |
2102
|
|
|
->will($this->returnValue($form)); |
2103
|
|
|
|
2104
|
|
|
$form->expects($this->once()) |
2105
|
|
|
->method('all') |
2106
|
|
|
->willReturn(['field' => 'fielddata']); |
2107
|
|
|
|
2108
|
|
|
$formView = $this->createMock(FormView::class); |
2109
|
|
|
|
2110
|
|
|
$form->expects($this->any()) |
2111
|
|
|
->method('createView') |
2112
|
|
|
->will($this->returnValue($formView)); |
2113
|
|
|
|
2114
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
|
|
|
|
2115
|
|
|
|
2116
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2117
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
2118
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2119
|
|
|
|
2120
|
|
|
$this->assertSame('create', $this->parameters['action']); |
2121
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
2122
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2123
|
|
|
|
2124
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2125
|
|
|
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
2126
|
|
|
} |
2127
|
|
|
|
2128
|
|
|
/** |
2129
|
|
|
* @dataProvider getToStringValues |
2130
|
|
|
*/ |
2131
|
|
|
public function testCreateActionSuccess($expectedToStringValue, $toStringValue) |
2132
|
|
|
{ |
2133
|
|
|
$object = new \stdClass(); |
2134
|
|
|
|
2135
|
|
|
$this->admin->expects($this->exactly(2)) |
|
|
|
|
2136
|
|
|
->method('checkAccess') |
2137
|
|
|
->will($this->returnCallback(function ($name, $objectIn = null) use ($object) { |
2138
|
|
|
if ('edit' == $name) { |
2139
|
|
|
return true; |
2140
|
|
|
} |
2141
|
|
|
|
2142
|
|
|
if ('create' != $name) { |
2143
|
|
|
return false; |
2144
|
|
|
} |
2145
|
|
|
|
2146
|
|
|
if (null === $objectIn) { |
2147
|
|
|
return true; |
2148
|
|
|
} |
2149
|
|
|
|
2150
|
|
|
return $objectIn === $object; |
2151
|
|
|
})); |
2152
|
|
|
|
2153
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2154
|
|
|
->method('hasRoute') |
2155
|
|
|
->with($this->equalTo('edit')) |
2156
|
|
|
->will($this->returnValue(true)); |
2157
|
|
|
|
2158
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2159
|
|
|
->method('hasAccess') |
2160
|
|
|
->with($this->equalTo('edit')) |
2161
|
|
|
->will($this->returnValue(true)); |
2162
|
|
|
|
2163
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2164
|
|
|
->method('getNewInstance') |
2165
|
|
|
->will($this->returnValue($object)); |
2166
|
|
|
|
2167
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2168
|
|
|
->method('create') |
2169
|
|
|
->will($this->returnArgument(0)); |
2170
|
|
|
|
2171
|
|
|
$form = $this->createMock(Form::class); |
2172
|
|
|
|
2173
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2174
|
|
|
->method('getClass') |
2175
|
|
|
->will($this->returnValue('stdClass')); |
2176
|
|
|
|
2177
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2178
|
|
|
->method('getForm') |
2179
|
|
|
->will($this->returnValue($form)); |
2180
|
|
|
|
2181
|
|
|
$form->expects($this->once()) |
2182
|
|
|
->method('all') |
2183
|
|
|
->willReturn(['field' => 'fielddata']); |
2184
|
|
|
|
2185
|
|
|
$form->expects($this->once()) |
2186
|
|
|
->method('isSubmitted') |
2187
|
|
|
->will($this->returnValue(true)); |
2188
|
|
|
|
2189
|
|
|
$form->expects($this->once()) |
2190
|
|
|
->method('isValid') |
2191
|
|
|
->will($this->returnValue(true)); |
2192
|
|
|
|
2193
|
|
|
$form->expects($this->once()) |
2194
|
|
|
->method('getData') |
2195
|
|
|
->will($this->returnValue($object)); |
2196
|
|
|
|
2197
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2198
|
|
|
->method('toString') |
2199
|
|
|
->with($this->equalTo($object)) |
2200
|
|
|
->will($this->returnValue($toStringValue)); |
2201
|
|
|
|
2202
|
|
|
$this->expectTranslate('flash_create_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
2203
|
|
|
|
2204
|
|
|
$this->request->setMethod('POST'); |
2205
|
|
|
|
2206
|
|
|
$response = $this->controller->createAction($this->request); |
|
|
|
|
2207
|
|
|
|
2208
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
2209
|
|
|
$this->assertSame(['flash_create_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
2210
|
|
|
$this->assertSame('stdClass_edit', $response->getTargetUrl()); |
2211
|
|
|
} |
2212
|
|
|
|
2213
|
|
|
public function testCreateActionAccessDenied2() |
2214
|
|
|
{ |
2215
|
|
|
$this->expectException(AccessDeniedException::class); |
2216
|
|
|
|
2217
|
|
|
$object = new \stdClass(); |
2218
|
|
|
|
2219
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2220
|
|
|
->method('checkAccess') |
2221
|
|
|
->will($this->returnCallback(function ($name, $object = null) { |
2222
|
|
|
if ('create' != $name) { |
2223
|
|
|
throw new AccessDeniedException(); |
2224
|
|
|
} |
2225
|
|
|
if (null === $object) { |
2226
|
|
|
return true; |
2227
|
|
|
} |
2228
|
|
|
|
2229
|
|
|
throw new AccessDeniedException(); |
2230
|
|
|
})); |
2231
|
|
|
|
2232
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2233
|
|
|
->method('getNewInstance') |
2234
|
|
|
->will($this->returnValue($object)); |
2235
|
|
|
|
2236
|
|
|
$form = $this->createMock(Form::class); |
2237
|
|
|
|
2238
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2239
|
|
|
->method('getClass') |
2240
|
|
|
->will($this->returnValue('stdClass')); |
2241
|
|
|
|
2242
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2243
|
|
|
->method('getForm') |
2244
|
|
|
->will($this->returnValue($form)); |
2245
|
|
|
|
2246
|
|
|
$form->expects($this->once()) |
2247
|
|
|
->method('all') |
2248
|
|
|
->willReturn(['field' => 'fielddata']); |
2249
|
|
|
|
2250
|
|
|
$form->expects($this->once()) |
2251
|
|
|
->method('isSubmitted') |
2252
|
|
|
->will($this->returnValue(true)); |
2253
|
|
|
|
2254
|
|
|
$form->expects($this->once()) |
2255
|
|
|
->method('getData') |
2256
|
|
|
->will($this->returnValue($object)); |
2257
|
|
|
|
2258
|
|
|
$form->expects($this->once()) |
2259
|
|
|
->method('isValid') |
2260
|
|
|
->will($this->returnValue(true)); |
2261
|
|
|
|
2262
|
|
|
$this->request->setMethod('POST'); |
2263
|
|
|
|
2264
|
|
|
$this->controller->createAction($this->request); |
|
|
|
|
2265
|
|
|
} |
2266
|
|
|
|
2267
|
|
|
/** |
2268
|
|
|
* @dataProvider getToStringValues |
2269
|
|
|
*/ |
2270
|
|
|
public function testCreateActionError($expectedToStringValue, $toStringValue) |
2271
|
|
|
{ |
2272
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2273
|
|
|
->method('checkAccess') |
2274
|
|
|
->with($this->equalTo('create')) |
2275
|
|
|
->will($this->returnValue(true)); |
2276
|
|
|
|
2277
|
|
|
$object = new \stdClass(); |
2278
|
|
|
|
2279
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2280
|
|
|
->method('getClass') |
2281
|
|
|
->will($this->returnValue('stdClass')); |
2282
|
|
|
|
2283
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2284
|
|
|
->method('getNewInstance') |
2285
|
|
|
->will($this->returnValue($object)); |
2286
|
|
|
|
2287
|
|
|
$form = $this->createMock(Form::class); |
2288
|
|
|
|
2289
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2290
|
|
|
->method('getForm') |
2291
|
|
|
->will($this->returnValue($form)); |
2292
|
|
|
|
2293
|
|
|
$form->expects($this->once()) |
2294
|
|
|
->method('all') |
2295
|
|
|
->willReturn(['field' => 'fielddata']); |
2296
|
|
|
|
2297
|
|
|
$form->expects($this->once()) |
2298
|
|
|
->method('isSubmitted') |
2299
|
|
|
->will($this->returnValue(true)); |
2300
|
|
|
|
2301
|
|
|
$form->expects($this->once()) |
2302
|
|
|
->method('isValid') |
2303
|
|
|
->will($this->returnValue(false)); |
2304
|
|
|
|
2305
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2306
|
|
|
->method('toString') |
2307
|
|
|
->with($this->equalTo($object)) |
2308
|
|
|
->will($this->returnValue($toStringValue)); |
2309
|
|
|
|
2310
|
|
|
$this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
2311
|
|
|
|
2312
|
|
|
$this->request->setMethod('POST'); |
2313
|
|
|
|
2314
|
|
|
$formView = $this->createMock(FormView::class); |
2315
|
|
|
|
2316
|
|
|
$form->expects($this->any()) |
2317
|
|
|
->method('createView') |
2318
|
|
|
->will($this->returnValue($formView)); |
2319
|
|
|
|
2320
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
|
|
|
|
2321
|
|
|
|
2322
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2323
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
2324
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2325
|
|
|
|
2326
|
|
|
$this->assertSame('create', $this->parameters['action']); |
2327
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
2328
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2329
|
|
|
|
2330
|
|
|
$this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
2331
|
|
|
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
2332
|
|
|
} |
2333
|
|
|
|
2334
|
|
|
/** |
2335
|
|
|
* @dataProvider getToStringValues |
2336
|
|
|
*/ |
2337
|
|
|
public function testCreateActionWithModelManagerException($expectedToStringValue, $toStringValue) |
2338
|
|
|
{ |
2339
|
|
|
$this->admin->expects($this->exactly(2)) |
|
|
|
|
2340
|
|
|
->method('checkAccess') |
2341
|
|
|
->with($this->equalTo('create')) |
2342
|
|
|
->will($this->returnValue(true)); |
2343
|
|
|
|
2344
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2345
|
|
|
->method('getClass') |
2346
|
|
|
->will($this->returnValue('stdClass')); |
2347
|
|
|
|
2348
|
|
|
$object = new \stdClass(); |
2349
|
|
|
|
2350
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2351
|
|
|
->method('getNewInstance') |
2352
|
|
|
->will($this->returnValue($object)); |
2353
|
|
|
|
2354
|
|
|
$form = $this->createMock(Form::class); |
2355
|
|
|
|
2356
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2357
|
|
|
->method('getForm') |
2358
|
|
|
->will($this->returnValue($form)); |
2359
|
|
|
|
2360
|
|
|
$form->expects($this->once()) |
2361
|
|
|
->method('all') |
2362
|
|
|
->willReturn(['field' => 'fielddata']); |
2363
|
|
|
|
2364
|
|
|
$form->expects($this->once()) |
2365
|
|
|
->method('isValid') |
2366
|
|
|
->will($this->returnValue(true)); |
2367
|
|
|
|
2368
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2369
|
|
|
->method('toString') |
2370
|
|
|
->with($this->equalTo($object)) |
2371
|
|
|
->will($this->returnValue($toStringValue)); |
2372
|
|
|
|
2373
|
|
|
$this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
2374
|
|
|
|
2375
|
|
|
$form->expects($this->once()) |
2376
|
|
|
->method('isSubmitted') |
2377
|
|
|
->will($this->returnValue(true)); |
2378
|
|
|
|
2379
|
|
|
$form->expects($this->once()) |
2380
|
|
|
->method('getData') |
2381
|
|
|
->will($this->returnValue($object)); |
2382
|
|
|
|
2383
|
|
|
$this->request->setMethod('POST'); |
2384
|
|
|
|
2385
|
|
|
$formView = $this->createMock(FormView::class); |
2386
|
|
|
|
2387
|
|
|
$form->expects($this->any()) |
2388
|
|
|
->method('createView') |
2389
|
|
|
->will($this->returnValue($formView)); |
2390
|
|
|
|
2391
|
|
|
$this->assertLoggerLogsModelManagerException($this->admin, 'create'); |
2392
|
|
|
|
2393
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
|
|
|
|
2394
|
|
|
|
2395
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2396
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
2397
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2398
|
|
|
|
2399
|
|
|
$this->assertSame('create', $this->parameters['action']); |
2400
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
2401
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2402
|
|
|
|
2403
|
|
|
$this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
2404
|
|
|
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
2405
|
|
|
} |
2406
|
|
|
|
2407
|
|
|
public function testCreateActionAjaxSuccess() |
2408
|
|
|
{ |
2409
|
|
|
$object = new \stdClass(); |
2410
|
|
|
|
2411
|
|
|
$this->admin->expects($this->exactly(2)) |
|
|
|
|
2412
|
|
|
->method('checkAccess') |
2413
|
|
|
->will($this->returnCallback(function ($name, $objectIn = null) use ($object) { |
2414
|
|
|
if ('create' != $name) { |
2415
|
|
|
return false; |
2416
|
|
|
} |
2417
|
|
|
|
2418
|
|
|
if (null === $objectIn) { |
2419
|
|
|
return true; |
2420
|
|
|
} |
2421
|
|
|
|
2422
|
|
|
return $objectIn === $object; |
2423
|
|
|
})); |
2424
|
|
|
|
2425
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2426
|
|
|
->method('getNewInstance') |
2427
|
|
|
->will($this->returnValue($object)); |
2428
|
|
|
|
2429
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2430
|
|
|
->method('create') |
2431
|
|
|
->will($this->returnArgument(0)); |
2432
|
|
|
|
2433
|
|
|
$form = $this->createMock(Form::class); |
2434
|
|
|
|
2435
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2436
|
|
|
->method('getForm') |
2437
|
|
|
->will($this->returnValue($form)); |
2438
|
|
|
|
2439
|
|
|
$form->expects($this->once()) |
2440
|
|
|
->method('all') |
2441
|
|
|
->willReturn(['field' => 'fielddata']); |
2442
|
|
|
|
2443
|
|
|
$form->expects($this->once()) |
2444
|
|
|
->method('isSubmitted') |
2445
|
|
|
->will($this->returnValue(true)); |
2446
|
|
|
|
2447
|
|
|
$form->expects($this->once()) |
2448
|
|
|
->method('isValid') |
2449
|
|
|
->will($this->returnValue(true)); |
2450
|
|
|
|
2451
|
|
|
$form->expects($this->once()) |
2452
|
|
|
->method('getData') |
2453
|
|
|
->will($this->returnValue($object)); |
2454
|
|
|
|
2455
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2456
|
|
|
->method('getClass') |
2457
|
|
|
->will($this->returnValue('stdClass')); |
2458
|
|
|
|
2459
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2460
|
|
|
->method('getNormalizedIdentifier') |
2461
|
|
|
->with($this->equalTo($object)) |
2462
|
|
|
->will($this->returnValue('foo_normalized')); |
2463
|
|
|
|
2464
|
|
|
$this->request->setMethod('POST'); |
2465
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
2466
|
|
|
|
2467
|
|
|
$response = $this->controller->createAction($this->request); |
|
|
|
|
2468
|
|
|
|
2469
|
|
|
$this->assertInstanceOf(Response::class, $response); |
2470
|
|
|
$this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized']), $response->getContent()); |
2471
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2472
|
|
|
} |
2473
|
|
|
|
2474
|
|
|
public function testCreateActionAjaxError() |
2475
|
|
|
{ |
2476
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2477
|
|
|
->method('checkAccess') |
2478
|
|
|
->with($this->equalTo('create')) |
2479
|
|
|
->will($this->returnValue(true)); |
2480
|
|
|
|
2481
|
|
|
$object = new \stdClass(); |
2482
|
|
|
|
2483
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2484
|
|
|
->method('getNewInstance') |
2485
|
|
|
->will($this->returnValue($object)); |
2486
|
|
|
|
2487
|
|
|
$form = $this->createMock(Form::class); |
2488
|
|
|
|
2489
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2490
|
|
|
->method('getClass') |
2491
|
|
|
->will($this->returnValue('stdClass')); |
2492
|
|
|
|
2493
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2494
|
|
|
->method('getForm') |
2495
|
|
|
->will($this->returnValue($form)); |
2496
|
|
|
|
2497
|
|
|
$form->expects($this->once()) |
2498
|
|
|
->method('all') |
2499
|
|
|
->willReturn(['field' => 'fielddata']); |
2500
|
|
|
|
2501
|
|
|
$form->expects($this->once()) |
2502
|
|
|
->method('isSubmitted') |
2503
|
|
|
->will($this->returnValue(true)); |
2504
|
|
|
|
2505
|
|
|
$form->expects($this->once()) |
2506
|
|
|
->method('isValid') |
2507
|
|
|
->will($this->returnValue(false)); |
2508
|
|
|
|
2509
|
|
|
$this->request->setMethod('POST'); |
2510
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
2511
|
|
|
|
2512
|
|
|
$formView = $this->createMock(FormView::class); |
2513
|
|
|
|
2514
|
|
|
$form->expects($this->any()) |
2515
|
|
|
->method('createView') |
2516
|
|
|
->will($this->returnValue($formView)); |
2517
|
|
|
|
2518
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
|
|
|
|
2519
|
|
|
|
2520
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2521
|
|
|
$this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']); |
2522
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2523
|
|
|
|
2524
|
|
|
$this->assertSame('create', $this->parameters['action']); |
2525
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
2526
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2527
|
|
|
|
2528
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2529
|
|
|
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template); |
2530
|
|
|
} |
2531
|
|
|
|
2532
|
|
|
public function testCreateActionWithPreview() |
2533
|
|
|
{ |
2534
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2535
|
|
|
->method('checkAccess') |
2536
|
|
|
->with($this->equalTo('create')) |
2537
|
|
|
->will($this->returnValue(true)); |
2538
|
|
|
|
2539
|
|
|
$object = new \stdClass(); |
2540
|
|
|
|
2541
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2542
|
|
|
->method('getNewInstance') |
2543
|
|
|
->will($this->returnValue($object)); |
2544
|
|
|
|
2545
|
|
|
$form = $this->createMock(Form::class); |
2546
|
|
|
|
2547
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2548
|
|
|
->method('getClass') |
2549
|
|
|
->will($this->returnValue('stdClass')); |
2550
|
|
|
|
2551
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2552
|
|
|
->method('getForm') |
2553
|
|
|
->will($this->returnValue($form)); |
2554
|
|
|
|
2555
|
|
|
$form->expects($this->once()) |
2556
|
|
|
->method('all') |
2557
|
|
|
->willReturn(['field' => 'fielddata']); |
2558
|
|
|
|
2559
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2560
|
|
|
->method('supportsPreviewMode') |
2561
|
|
|
->will($this->returnValue(true)); |
2562
|
|
|
|
2563
|
|
|
$formView = $this->createMock(FormView::class); |
2564
|
|
|
|
2565
|
|
|
$form->expects($this->any()) |
2566
|
|
|
->method('createView') |
2567
|
|
|
->will($this->returnValue($formView)); |
2568
|
|
|
|
2569
|
|
|
$form->expects($this->once()) |
2570
|
|
|
->method('isSubmitted') |
2571
|
|
|
->will($this->returnValue(true)); |
2572
|
|
|
|
2573
|
|
|
$form->expects($this->once()) |
2574
|
|
|
->method('isValid') |
2575
|
|
|
->will($this->returnValue(true)); |
2576
|
|
|
|
2577
|
|
|
$this->request->setMethod('POST'); |
2578
|
|
|
$this->request->request->set('btn_preview', 'Preview'); |
2579
|
|
|
|
2580
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->createAction($this->request)); |
|
|
|
|
2581
|
|
|
|
2582
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2583
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
2584
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2585
|
|
|
|
2586
|
|
|
$this->assertSame('create', $this->parameters['action']); |
2587
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
2588
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2589
|
|
|
|
2590
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2591
|
|
|
$this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template); |
2592
|
|
|
} |
2593
|
|
|
|
2594
|
|
|
public function testExportActionAccessDenied() |
2595
|
|
|
{ |
2596
|
|
|
$this->expectException(AccessDeniedException::class); |
2597
|
|
|
|
2598
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2599
|
|
|
->method('checkAccess') |
2600
|
|
|
->with($this->equalTo('export')) |
2601
|
|
|
->will($this->throwException(new AccessDeniedException())); |
2602
|
|
|
|
2603
|
|
|
$this->controller->exportAction($this->request); |
2604
|
|
|
} |
2605
|
|
|
|
2606
|
|
|
public function testExportActionWrongFormat() |
2607
|
|
|
{ |
2608
|
|
|
$this->expectException(\RuntimeException::class, 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`'); |
2609
|
|
|
|
2610
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2611
|
|
|
->method('checkAccess') |
2612
|
|
|
->with($this->equalTo('export')) |
2613
|
|
|
->will($this->returnValue(true)); |
2614
|
|
|
|
2615
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2616
|
|
|
->method('getExportFormats') |
2617
|
|
|
->will($this->returnValue(['json'])); |
2618
|
|
|
|
2619
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2620
|
|
|
->method('getClass') |
2621
|
|
|
->will($this->returnValue('Foo')); |
2622
|
|
|
|
2623
|
|
|
$this->request->query->set('format', 'csv'); |
2624
|
|
|
|
2625
|
|
|
$this->controller->exportAction($this->request); |
2626
|
|
|
} |
2627
|
|
|
|
2628
|
|
|
public function testExportAction() |
2629
|
|
|
{ |
2630
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2631
|
|
|
->method('checkAccess') |
2632
|
|
|
->with($this->equalTo('export')) |
2633
|
|
|
->will($this->returnValue(true)); |
2634
|
|
|
|
2635
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2636
|
|
|
->method('getExportFormats') |
2637
|
|
|
->will($this->returnValue(['json'])); |
2638
|
|
|
|
2639
|
|
|
$dataSourceIterator = $this->createMock(SourceIteratorInterface::class); |
2640
|
|
|
|
2641
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2642
|
|
|
->method('getDataSourceIterator') |
2643
|
|
|
->will($this->returnValue($dataSourceIterator)); |
2644
|
|
|
|
2645
|
|
|
$this->request->query->set('format', 'json'); |
2646
|
|
|
|
2647
|
|
|
$response = $this->controller->exportAction($this->request); |
2648
|
|
|
$this->assertInstanceOf(StreamedResponse::class, $response); |
2649
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
2650
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2651
|
|
|
} |
2652
|
|
|
|
2653
|
|
|
public function testHistoryActionAccessDenied() |
2654
|
|
|
{ |
2655
|
|
|
$this->expectException(AccessDeniedException::class); |
2656
|
|
|
|
2657
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2658
|
|
|
->method('getObject') |
2659
|
|
|
->will($this->returnValue(new \StdClass())); |
2660
|
|
|
|
2661
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2662
|
|
|
->method('checkAccess') |
2663
|
|
|
->with($this->equalTo('history')) |
2664
|
|
|
->will($this->throwException(new AccessDeniedException())); |
2665
|
|
|
|
2666
|
|
|
$this->controller->historyAction(null, $this->request); |
|
|
|
|
2667
|
|
|
} |
2668
|
|
|
|
2669
|
|
|
public function testHistoryActionNotFoundException() |
2670
|
|
|
{ |
2671
|
|
|
$this->expectException(NotFoundHttpException::class); |
2672
|
|
|
|
2673
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2674
|
|
|
->method('getObject') |
2675
|
|
|
->will($this->returnValue(false)); |
2676
|
|
|
|
2677
|
|
|
$this->controller->historyAction(null, $this->request); |
|
|
|
|
2678
|
|
|
} |
2679
|
|
|
|
2680
|
|
|
public function testHistoryActionNoReader() |
2681
|
|
|
{ |
2682
|
|
|
$this->expectException(NotFoundHttpException::class, 'unable to find the audit reader for class : Foo'); |
2683
|
|
|
|
2684
|
|
|
$this->request->query->set('id', 123); |
2685
|
|
|
|
2686
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2687
|
|
|
->method('checkAccess') |
2688
|
|
|
->with($this->equalTo('history')) |
2689
|
|
|
->will($this->returnValue(true)); |
2690
|
|
|
|
2691
|
|
|
$object = new \stdClass(); |
2692
|
|
|
|
2693
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2694
|
|
|
->method('getObject') |
2695
|
|
|
->will($this->returnValue($object)); |
2696
|
|
|
|
2697
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2698
|
|
|
->method('getClass') |
2699
|
|
|
->will($this->returnValue('Foo')); |
2700
|
|
|
|
2701
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
2702
|
|
|
->method('hasReader') |
2703
|
|
|
->with($this->equalTo('Foo')) |
2704
|
|
|
->will($this->returnValue(false)); |
2705
|
|
|
|
2706
|
|
|
$this->controller->historyAction(null, $this->request); |
|
|
|
|
2707
|
|
|
} |
2708
|
|
|
|
2709
|
|
|
public function testHistoryAction() |
2710
|
|
|
{ |
2711
|
|
|
$this->request->query->set('id', 123); |
2712
|
|
|
|
2713
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2714
|
|
|
->method('checkAccess') |
2715
|
|
|
->with($this->equalTo('history')) |
2716
|
|
|
->will($this->returnValue(true)); |
2717
|
|
|
|
2718
|
|
|
$object = new \stdClass(); |
2719
|
|
|
|
2720
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2721
|
|
|
->method('getObject') |
2722
|
|
|
->will($this->returnValue($object)); |
2723
|
|
|
|
2724
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2725
|
|
|
->method('getClass') |
2726
|
|
|
->will($this->returnValue('Foo')); |
2727
|
|
|
|
2728
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
2729
|
|
|
->method('hasReader') |
2730
|
|
|
->with($this->equalTo('Foo')) |
2731
|
|
|
->will($this->returnValue(true)); |
2732
|
|
|
|
2733
|
|
|
$reader = $this->createMock(AuditReaderInterface::class); |
2734
|
|
|
|
2735
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
2736
|
|
|
->method('getReader') |
2737
|
|
|
->with($this->equalTo('Foo')) |
2738
|
|
|
->will($this->returnValue($reader)); |
2739
|
|
|
|
2740
|
|
|
$reader->expects($this->once()) |
2741
|
|
|
->method('findRevisions') |
2742
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123)) |
2743
|
|
|
->will($this->returnValue([])); |
2744
|
|
|
|
2745
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->historyAction(null, $this->request)); |
|
|
|
|
2746
|
|
|
|
2747
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2748
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
2749
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2750
|
|
|
|
2751
|
|
|
$this->assertSame('history', $this->parameters['action']); |
2752
|
|
|
$this->assertSame([], $this->parameters['revisions']); |
2753
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2754
|
|
|
|
2755
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2756
|
|
|
$this->assertSame('@SonataAdmin/CRUD/history.html.twig', $this->template); |
2757
|
|
|
} |
2758
|
|
|
|
2759
|
|
|
public function testAclActionAclNotEnabled() |
2760
|
|
|
{ |
2761
|
|
|
$this->expectException(NotFoundHttpException::class, 'ACL are not enabled for this admin'); |
2762
|
|
|
|
2763
|
|
|
$this->controller->aclAction(null, $this->request); |
|
|
|
|
2764
|
|
|
} |
2765
|
|
|
|
2766
|
|
|
public function testAclActionNotFoundException() |
2767
|
|
|
{ |
2768
|
|
|
$this->expectException(NotFoundHttpException::class); |
2769
|
|
|
|
2770
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2771
|
|
|
->method('isAclEnabled') |
2772
|
|
|
->will($this->returnValue(true)); |
2773
|
|
|
|
2774
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2775
|
|
|
->method('getObject') |
2776
|
|
|
->will($this->returnValue(false)); |
2777
|
|
|
|
2778
|
|
|
$this->controller->aclAction(null, $this->request); |
|
|
|
|
2779
|
|
|
} |
2780
|
|
|
|
2781
|
|
|
public function testAclActionAccessDenied() |
2782
|
|
|
{ |
2783
|
|
|
$this->expectException(AccessDeniedException::class); |
2784
|
|
|
|
2785
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2786
|
|
|
->method('isAclEnabled') |
2787
|
|
|
->will($this->returnValue(true)); |
2788
|
|
|
|
2789
|
|
|
$object = new \stdClass(); |
2790
|
|
|
|
2791
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2792
|
|
|
->method('getObject') |
2793
|
|
|
->will($this->returnValue($object)); |
2794
|
|
|
|
2795
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2796
|
|
|
->method('checkAccess') |
2797
|
|
|
->with($this->equalTo('acl'), $this->equalTo($object)) |
2798
|
|
|
->will($this->throwException(new AccessDeniedException())); |
2799
|
|
|
|
2800
|
|
|
$this->controller->aclAction(null, $this->request); |
|
|
|
|
2801
|
|
|
} |
2802
|
|
|
|
2803
|
|
|
public function testAclAction() |
2804
|
|
|
{ |
2805
|
|
|
$this->request->query->set('id', 123); |
2806
|
|
|
|
2807
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2808
|
|
|
->method('isAclEnabled') |
2809
|
|
|
->will($this->returnValue(true)); |
2810
|
|
|
|
2811
|
|
|
$object = new \stdClass(); |
2812
|
|
|
|
2813
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2814
|
|
|
->method('getObject') |
2815
|
|
|
->will($this->returnValue($object)); |
2816
|
|
|
|
2817
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2818
|
|
|
->method('checkAccess') |
2819
|
|
|
->will($this->returnValue(true)); |
2820
|
|
|
|
2821
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2822
|
|
|
->method('getSecurityInformation') |
2823
|
|
|
->will($this->returnValue([])); |
2824
|
|
|
|
2825
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2826
|
|
|
->method('getMaskBuilderClass') |
2827
|
|
|
->will($this->returnValue(AdminPermissionMap::class)); |
2828
|
|
|
|
2829
|
|
|
$aclUsersForm = $this->getMockBuilder(Form::class) |
2830
|
|
|
->disableOriginalConstructor() |
2831
|
|
|
->getMock(); |
2832
|
|
|
|
2833
|
|
|
$aclUsersForm->expects($this->once()) |
2834
|
|
|
->method('createView') |
2835
|
|
|
->will($this->returnValue($this->createMock(FormView::class))); |
2836
|
|
|
|
2837
|
|
|
$aclRolesForm = $this->getMockBuilder(Form::class) |
2838
|
|
|
->disableOriginalConstructor() |
2839
|
|
|
->getMock(); |
2840
|
|
|
|
2841
|
|
|
$aclRolesForm->expects($this->once()) |
2842
|
|
|
->method('createView') |
2843
|
|
|
->will($this->returnValue($this->createMock(FormView::class))); |
2844
|
|
|
|
2845
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2846
|
|
|
->method('createAclUsersForm') |
2847
|
|
|
->with($this->isInstanceOf(AdminObjectAclData::class)) |
2848
|
|
|
->will($this->returnValue($aclUsersForm)); |
2849
|
|
|
|
2850
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2851
|
|
|
->method('createAclRolesForm') |
2852
|
|
|
->with($this->isInstanceOf(AdminObjectAclData::class)) |
2853
|
|
|
->will($this->returnValue($aclRolesForm)); |
2854
|
|
|
|
2855
|
|
|
$aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class) |
2856
|
|
|
->disableOriginalConstructor() |
2857
|
|
|
->getMock(); |
2858
|
|
|
|
2859
|
|
|
$aclSecurityHandler->expects($this->any()) |
2860
|
|
|
->method('getObjectPermissions') |
2861
|
|
|
->will($this->returnValue([])); |
2862
|
|
|
|
2863
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2864
|
|
|
->method('getSecurityHandler') |
2865
|
|
|
->will($this->returnValue($aclSecurityHandler)); |
2866
|
|
|
|
2867
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->aclAction(null, $this->request)); |
|
|
|
|
2868
|
|
|
|
2869
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2870
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
2871
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2872
|
|
|
|
2873
|
|
|
$this->assertSame('acl', $this->parameters['action']); |
2874
|
|
|
$this->assertSame([], $this->parameters['permissions']); |
2875
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2876
|
|
|
$this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']); |
2877
|
|
|
$this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']); |
2878
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']); |
2879
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']); |
2880
|
|
|
|
2881
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2882
|
|
|
$this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template); |
2883
|
|
|
} |
2884
|
|
|
|
2885
|
|
|
public function testAclActionInvalidUpdate() |
2886
|
|
|
{ |
2887
|
|
|
$this->request->query->set('id', 123); |
2888
|
|
|
$this->request->request->set(AdminObjectAclManipulator::ACL_USERS_FORM_NAME, []); |
2889
|
|
|
|
2890
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2891
|
|
|
->method('isAclEnabled') |
2892
|
|
|
->will($this->returnValue(true)); |
2893
|
|
|
|
2894
|
|
|
$object = new \stdClass(); |
2895
|
|
|
|
2896
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2897
|
|
|
->method('getObject') |
2898
|
|
|
->will($this->returnValue($object)); |
2899
|
|
|
|
2900
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2901
|
|
|
->method('checkAccess') |
2902
|
|
|
->will($this->returnValue(true)); |
2903
|
|
|
|
2904
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2905
|
|
|
->method('getSecurityInformation') |
2906
|
|
|
->will($this->returnValue([])); |
2907
|
|
|
|
2908
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2909
|
|
|
->method('getMaskBuilderClass') |
2910
|
|
|
->will($this->returnValue(AdminPermissionMap::class)); |
2911
|
|
|
|
2912
|
|
|
$aclUsersForm = $this->getMockBuilder(Form::class) |
2913
|
|
|
->disableOriginalConstructor() |
2914
|
|
|
->getMock(); |
2915
|
|
|
|
2916
|
|
|
$aclUsersForm->expects($this->once()) |
2917
|
|
|
->method('isValid') |
2918
|
|
|
->will($this->returnValue(false)); |
2919
|
|
|
|
2920
|
|
|
$aclUsersForm->expects($this->once()) |
2921
|
|
|
->method('createView') |
2922
|
|
|
->will($this->returnValue($this->createMock(FormView::class))); |
2923
|
|
|
|
2924
|
|
|
$aclRolesForm = $this->getMockBuilder(Form::class) |
2925
|
|
|
->disableOriginalConstructor() |
2926
|
|
|
->getMock(); |
2927
|
|
|
|
2928
|
|
|
$aclRolesForm->expects($this->once()) |
2929
|
|
|
->method('createView') |
2930
|
|
|
->will($this->returnValue($this->createMock(FormView::class))); |
2931
|
|
|
|
2932
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2933
|
|
|
->method('createAclUsersForm') |
2934
|
|
|
->with($this->isInstanceOf(AdminObjectAclData::class)) |
2935
|
|
|
->will($this->returnValue($aclUsersForm)); |
2936
|
|
|
|
2937
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2938
|
|
|
->method('createAclRolesForm') |
2939
|
|
|
->with($this->isInstanceOf(AdminObjectAclData::class)) |
2940
|
|
|
->will($this->returnValue($aclRolesForm)); |
2941
|
|
|
|
2942
|
|
|
$aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class) |
2943
|
|
|
->disableOriginalConstructor() |
2944
|
|
|
->getMock(); |
2945
|
|
|
|
2946
|
|
|
$aclSecurityHandler->expects($this->any()) |
2947
|
|
|
->method('getObjectPermissions') |
2948
|
|
|
->will($this->returnValue([])); |
2949
|
|
|
|
2950
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2951
|
|
|
->method('getSecurityHandler') |
2952
|
|
|
->will($this->returnValue($aclSecurityHandler)); |
2953
|
|
|
|
2954
|
|
|
$this->request->setMethod('POST'); |
2955
|
|
|
|
2956
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->aclAction(null, $this->request)); |
|
|
|
|
2957
|
|
|
|
2958
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2959
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
2960
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2961
|
|
|
|
2962
|
|
|
$this->assertSame('acl', $this->parameters['action']); |
2963
|
|
|
$this->assertSame([], $this->parameters['permissions']); |
2964
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2965
|
|
|
$this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']); |
2966
|
|
|
$this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']); |
2967
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']); |
2968
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']); |
2969
|
|
|
|
2970
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2971
|
|
|
$this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template); |
2972
|
|
|
} |
2973
|
|
|
|
2974
|
|
|
public function testAclActionSuccessfulUpdate() |
2975
|
|
|
{ |
2976
|
|
|
$this->request->query->set('id', 123); |
2977
|
|
|
$this->request->request->set(AdminObjectAclManipulator::ACL_ROLES_FORM_NAME, []); |
2978
|
|
|
|
2979
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2980
|
|
|
->method('isAclEnabled') |
2981
|
|
|
->will($this->returnValue(true)); |
2982
|
|
|
|
2983
|
|
|
$object = new \stdClass(); |
2984
|
|
|
|
2985
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2986
|
|
|
->method('getObject') |
2987
|
|
|
->will($this->returnValue($object)); |
2988
|
|
|
|
2989
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2990
|
|
|
->method('checkAccess') |
2991
|
|
|
->will($this->returnValue(true)); |
2992
|
|
|
|
2993
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2994
|
|
|
->method('getSecurityInformation') |
2995
|
|
|
->will($this->returnValue([])); |
2996
|
|
|
|
2997
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2998
|
|
|
->method('getMaskBuilderClass') |
2999
|
|
|
->will($this->returnValue(AdminPermissionMap::class)); |
3000
|
|
|
|
3001
|
|
|
$aclUsersForm = $this->getMockBuilder(Form::class) |
3002
|
|
|
->disableOriginalConstructor() |
3003
|
|
|
->getMock(); |
3004
|
|
|
|
3005
|
|
|
$aclUsersForm->expects($this->any()) |
3006
|
|
|
->method('createView') |
3007
|
|
|
->will($this->returnValue($this->createMock(FormView::class))); |
3008
|
|
|
|
3009
|
|
|
$aclRolesForm = $this->getMockBuilder(Form::class) |
3010
|
|
|
->disableOriginalConstructor() |
3011
|
|
|
->getMock(); |
3012
|
|
|
|
3013
|
|
|
$aclRolesForm->expects($this->any()) |
3014
|
|
|
->method('createView') |
3015
|
|
|
->will($this->returnValue($this->createMock(FormView::class))); |
3016
|
|
|
|
3017
|
|
|
$aclRolesForm->expects($this->once()) |
3018
|
|
|
->method('isValid') |
3019
|
|
|
->will($this->returnValue(true)); |
3020
|
|
|
|
3021
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
3022
|
|
|
->method('createAclUsersForm') |
3023
|
|
|
->with($this->isInstanceOf(AdminObjectAclData::class)) |
3024
|
|
|
->will($this->returnValue($aclUsersForm)); |
3025
|
|
|
|
3026
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
3027
|
|
|
->method('createAclRolesForm') |
3028
|
|
|
->with($this->isInstanceOf(AdminObjectAclData::class)) |
3029
|
|
|
->will($this->returnValue($aclRolesForm)); |
3030
|
|
|
|
3031
|
|
|
$aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class) |
3032
|
|
|
->disableOriginalConstructor() |
3033
|
|
|
->getMock(); |
3034
|
|
|
|
3035
|
|
|
$aclSecurityHandler->expects($this->any()) |
3036
|
|
|
->method('getObjectPermissions') |
3037
|
|
|
->will($this->returnValue([])); |
3038
|
|
|
|
3039
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3040
|
|
|
->method('getSecurityHandler') |
3041
|
|
|
->will($this->returnValue($aclSecurityHandler)); |
3042
|
|
|
|
3043
|
|
|
$this->expectTranslate('flash_acl_edit_success', [], 'SonataAdminBundle'); |
3044
|
|
|
|
3045
|
|
|
$this->request->setMethod('POST'); |
3046
|
|
|
|
3047
|
|
|
$response = $this->controller->aclAction(null, $this->request); |
|
|
|
|
3048
|
|
|
|
3049
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
3050
|
|
|
|
3051
|
|
|
$this->assertSame(['flash_acl_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
3052
|
|
|
$this->assertSame('stdClass_acl', $response->getTargetUrl()); |
3053
|
|
|
} |
3054
|
|
|
|
3055
|
|
|
public function testHistoryViewRevisionActionAccessDenied() |
3056
|
|
|
{ |
3057
|
|
|
$this->expectException(AccessDeniedException::class); |
3058
|
|
|
|
3059
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3060
|
|
|
->method('getObject') |
3061
|
|
|
->will($this->returnValue(new \StdClass())); |
3062
|
|
|
|
3063
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3064
|
|
|
->method('checkAccess') |
3065
|
|
|
->with($this->equalTo('historyViewRevision')) |
3066
|
|
|
->will($this->throwException(new AccessDeniedException())); |
3067
|
|
|
|
3068
|
|
|
$this->controller->historyViewRevisionAction(null, null, $this->request); |
|
|
|
|
3069
|
|
|
} |
3070
|
|
|
|
3071
|
|
|
public function testHistoryViewRevisionActionNotFoundException() |
3072
|
|
|
{ |
3073
|
|
|
$this->expectException(NotFoundHttpException::class, 'unable to find the object with id: 123'); |
3074
|
|
|
|
3075
|
|
|
$this->request->query->set('id', 123); |
3076
|
|
|
|
3077
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3078
|
|
|
->method('getObject') |
3079
|
|
|
->will($this->returnValue(false)); |
3080
|
|
|
|
3081
|
|
|
$this->controller->historyViewRevisionAction(null, null, $this->request); |
|
|
|
|
3082
|
|
|
} |
3083
|
|
|
|
3084
|
|
|
public function testHistoryViewRevisionActionNoReader() |
3085
|
|
|
{ |
3086
|
|
|
$this->expectException(NotFoundHttpException::class, 'unable to find the audit reader for class : Foo'); |
3087
|
|
|
|
3088
|
|
|
$this->request->query->set('id', 123); |
3089
|
|
|
|
3090
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3091
|
|
|
->method('checkAccess') |
3092
|
|
|
->with($this->equalTo('historyViewRevision')) |
3093
|
|
|
->will($this->returnValue(true)); |
3094
|
|
|
|
3095
|
|
|
$object = new \stdClass(); |
3096
|
|
|
|
3097
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3098
|
|
|
->method('getObject') |
3099
|
|
|
->will($this->returnValue($object)); |
3100
|
|
|
|
3101
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3102
|
|
|
->method('getClass') |
3103
|
|
|
->will($this->returnValue('Foo')); |
3104
|
|
|
|
3105
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3106
|
|
|
->method('hasReader') |
3107
|
|
|
->with($this->equalTo('Foo')) |
3108
|
|
|
->will($this->returnValue(false)); |
3109
|
|
|
|
3110
|
|
|
$this->controller->historyViewRevisionAction(null, null, $this->request); |
|
|
|
|
3111
|
|
|
} |
3112
|
|
|
|
3113
|
|
|
public function testHistoryViewRevisionActionNotFoundRevision() |
3114
|
|
|
{ |
3115
|
|
|
$this->expectException(NotFoundHttpException::class, 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'); |
3116
|
|
|
|
3117
|
|
|
$this->request->query->set('id', 123); |
3118
|
|
|
|
3119
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3120
|
|
|
->method('checkAccess') |
3121
|
|
|
->with($this->equalTo('historyViewRevision')) |
3122
|
|
|
->will($this->returnValue(true)); |
3123
|
|
|
|
3124
|
|
|
$object = new \stdClass(); |
3125
|
|
|
|
3126
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3127
|
|
|
->method('getObject') |
3128
|
|
|
->will($this->returnValue($object)); |
3129
|
|
|
|
3130
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3131
|
|
|
->method('getClass') |
3132
|
|
|
->will($this->returnValue('Foo')); |
3133
|
|
|
|
3134
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3135
|
|
|
->method('hasReader') |
3136
|
|
|
->with($this->equalTo('Foo')) |
3137
|
|
|
->will($this->returnValue(true)); |
3138
|
|
|
|
3139
|
|
|
$reader = $this->createMock(AuditReaderInterface::class); |
3140
|
|
|
|
3141
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3142
|
|
|
->method('getReader') |
3143
|
|
|
->with($this->equalTo('Foo')) |
3144
|
|
|
->will($this->returnValue($reader)); |
3145
|
|
|
|
3146
|
|
|
$reader->expects($this->once()) |
3147
|
|
|
->method('find') |
3148
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3149
|
|
|
->will($this->returnValue(null)); |
3150
|
|
|
|
3151
|
|
|
$this->controller->historyViewRevisionAction(123, 456, $this->request); |
|
|
|
|
3152
|
|
|
} |
3153
|
|
|
|
3154
|
|
|
public function testHistoryViewRevisionAction() |
3155
|
|
|
{ |
3156
|
|
|
$this->request->query->set('id', 123); |
3157
|
|
|
|
3158
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3159
|
|
|
->method('checkAccess') |
3160
|
|
|
->with($this->equalTo('historyViewRevision')) |
3161
|
|
|
->will($this->returnValue(true)); |
3162
|
|
|
|
3163
|
|
|
$object = new \stdClass(); |
3164
|
|
|
|
3165
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3166
|
|
|
->method('getObject') |
3167
|
|
|
->will($this->returnValue($object)); |
3168
|
|
|
|
3169
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3170
|
|
|
->method('getClass') |
3171
|
|
|
->will($this->returnValue('Foo')); |
3172
|
|
|
|
3173
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3174
|
|
|
->method('hasReader') |
3175
|
|
|
->with($this->equalTo('Foo')) |
3176
|
|
|
->will($this->returnValue(true)); |
3177
|
|
|
|
3178
|
|
|
$reader = $this->createMock(AuditReaderInterface::class); |
3179
|
|
|
|
3180
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3181
|
|
|
->method('getReader') |
3182
|
|
|
->with($this->equalTo('Foo')) |
3183
|
|
|
->will($this->returnValue($reader)); |
3184
|
|
|
|
3185
|
|
|
$objectRevision = new \stdClass(); |
3186
|
|
|
$objectRevision->revision = 456; |
3187
|
|
|
|
3188
|
|
|
$reader->expects($this->once()) |
3189
|
|
|
->method('find') |
3190
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3191
|
|
|
->will($this->returnValue($objectRevision)); |
3192
|
|
|
|
3193
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3194
|
|
|
->method('setSubject') |
3195
|
|
|
->with($this->equalTo($objectRevision)) |
3196
|
|
|
->will($this->returnValue(null)); |
3197
|
|
|
|
3198
|
|
|
$fieldDescriptionCollection = new FieldDescriptionCollection(); |
3199
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3200
|
|
|
->method('getShow') |
3201
|
|
|
->will($this->returnValue($fieldDescriptionCollection)); |
3202
|
|
|
|
3203
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->historyViewRevisionAction(123, 456, $this->request)); |
|
|
|
|
3204
|
|
|
|
3205
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
3206
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
3207
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
3208
|
|
|
|
3209
|
|
|
$this->assertSame('show', $this->parameters['action']); |
3210
|
|
|
$this->assertSame($objectRevision, $this->parameters['object']); |
3211
|
|
|
$this->assertSame($fieldDescriptionCollection, $this->parameters['elements']); |
3212
|
|
|
|
3213
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
3214
|
|
|
$this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template); |
3215
|
|
|
} |
3216
|
|
|
|
3217
|
|
|
public function testHistoryCompareRevisionsActionAccessDenied() |
3218
|
|
|
{ |
3219
|
|
|
$this->expectException(AccessDeniedException::class); |
3220
|
|
|
|
3221
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3222
|
|
|
->method('checkAccess') |
3223
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3224
|
|
|
->will($this->throwException(new AccessDeniedException())); |
3225
|
|
|
|
3226
|
|
|
$this->controller->historyCompareRevisionsAction(null, null, null, $this->request); |
|
|
|
|
3227
|
|
|
} |
3228
|
|
|
|
3229
|
|
|
public function testHistoryCompareRevisionsActionNotFoundException() |
3230
|
|
|
{ |
3231
|
|
|
$this->expectException(NotFoundHttpException::class, 'unable to find the object with id: 123'); |
3232
|
|
|
|
3233
|
|
|
$this->request->query->set('id', 123); |
3234
|
|
|
|
3235
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3236
|
|
|
->method('checkAccess') |
3237
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3238
|
|
|
->will($this->returnValue(true)); |
3239
|
|
|
|
3240
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3241
|
|
|
->method('getObject') |
3242
|
|
|
->will($this->returnValue(false)); |
3243
|
|
|
|
3244
|
|
|
$this->controller->historyCompareRevisionsAction(null, null, null, $this->request); |
|
|
|
|
3245
|
|
|
} |
3246
|
|
|
|
3247
|
|
|
public function testHistoryCompareRevisionsActionNoReader() |
3248
|
|
|
{ |
3249
|
|
|
$this->expectException(NotFoundHttpException::class, 'unable to find the audit reader for class : Foo'); |
3250
|
|
|
|
3251
|
|
|
$this->request->query->set('id', 123); |
3252
|
|
|
|
3253
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3254
|
|
|
->method('checkAccess') |
3255
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3256
|
|
|
->will($this->returnValue(true)); |
3257
|
|
|
|
3258
|
|
|
$object = new \stdClass(); |
3259
|
|
|
|
3260
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3261
|
|
|
->method('getObject') |
3262
|
|
|
->will($this->returnValue($object)); |
3263
|
|
|
|
3264
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3265
|
|
|
->method('getClass') |
3266
|
|
|
->will($this->returnValue('Foo')); |
3267
|
|
|
|
3268
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3269
|
|
|
->method('hasReader') |
3270
|
|
|
->with($this->equalTo('Foo')) |
3271
|
|
|
->will($this->returnValue(false)); |
3272
|
|
|
|
3273
|
|
|
$this->controller->historyCompareRevisionsAction(null, null, null, $this->request); |
|
|
|
|
3274
|
|
|
} |
3275
|
|
|
|
3276
|
|
|
public function testHistoryCompareRevisionsActionNotFoundBaseRevision() |
3277
|
|
|
{ |
3278
|
|
|
$this->expectException(NotFoundHttpException::class, 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'); |
3279
|
|
|
|
3280
|
|
|
$this->request->query->set('id', 123); |
3281
|
|
|
|
3282
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3283
|
|
|
->method('checkAccess') |
3284
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3285
|
|
|
->will($this->returnValue(true)); |
3286
|
|
|
|
3287
|
|
|
$object = new \stdClass(); |
3288
|
|
|
|
3289
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3290
|
|
|
->method('getObject') |
3291
|
|
|
->will($this->returnValue($object)); |
3292
|
|
|
|
3293
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3294
|
|
|
->method('getClass') |
3295
|
|
|
->will($this->returnValue('Foo')); |
3296
|
|
|
|
3297
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3298
|
|
|
->method('hasReader') |
3299
|
|
|
->with($this->equalTo('Foo')) |
3300
|
|
|
->will($this->returnValue(true)); |
3301
|
|
|
|
3302
|
|
|
$reader = $this->createMock(AuditReaderInterface::class); |
3303
|
|
|
|
3304
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3305
|
|
|
->method('getReader') |
3306
|
|
|
->with($this->equalTo('Foo')) |
3307
|
|
|
->will($this->returnValue($reader)); |
3308
|
|
|
|
3309
|
|
|
// once because it will not be found and therefore the second call won't be executed |
3310
|
|
|
$reader->expects($this->once()) |
3311
|
|
|
->method('find') |
3312
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3313
|
|
|
->will($this->returnValue(null)); |
3314
|
|
|
|
3315
|
|
|
$this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request); |
|
|
|
|
3316
|
|
|
} |
3317
|
|
|
|
3318
|
|
|
public function testHistoryCompareRevisionsActionNotFoundCompareRevision() |
3319
|
|
|
{ |
3320
|
|
|
$this->expectException(NotFoundHttpException::class, 'unable to find the targeted object `123` from the revision `789` with classname : `Foo`'); |
3321
|
|
|
|
3322
|
|
|
$this->request->query->set('id', 123); |
3323
|
|
|
|
3324
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3325
|
|
|
->method('checkAccess') |
3326
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3327
|
|
|
->will($this->returnValue(true)); |
3328
|
|
|
|
3329
|
|
|
$object = new \stdClass(); |
3330
|
|
|
|
3331
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3332
|
|
|
->method('getObject') |
3333
|
|
|
->will($this->returnValue($object)); |
3334
|
|
|
|
3335
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3336
|
|
|
->method('getClass') |
3337
|
|
|
->will($this->returnValue('Foo')); |
3338
|
|
|
|
3339
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3340
|
|
|
->method('hasReader') |
3341
|
|
|
->with($this->equalTo('Foo')) |
3342
|
|
|
->will($this->returnValue(true)); |
3343
|
|
|
|
3344
|
|
|
$reader = $this->createMock(AuditReaderInterface::class); |
3345
|
|
|
|
3346
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3347
|
|
|
->method('getReader') |
3348
|
|
|
->with($this->equalTo('Foo')) |
3349
|
|
|
->will($this->returnValue($reader)); |
3350
|
|
|
|
3351
|
|
|
$objectRevision = new \stdClass(); |
3352
|
|
|
$objectRevision->revision = 456; |
3353
|
|
|
|
3354
|
|
|
// first call should return, so the second call will throw an exception |
3355
|
|
|
$reader->expects($this->at(0)) |
3356
|
|
|
->method('find') |
3357
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3358
|
|
|
->will($this->returnValue($objectRevision)); |
3359
|
|
|
|
3360
|
|
|
$reader->expects($this->at(1)) |
3361
|
|
|
->method('find') |
3362
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789)) |
3363
|
|
|
->will($this->returnValue(null)); |
3364
|
|
|
|
3365
|
|
|
$this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request); |
|
|
|
|
3366
|
|
|
} |
3367
|
|
|
|
3368
|
|
|
public function testHistoryCompareRevisionsActionAction() |
3369
|
|
|
{ |
3370
|
|
|
$this->request->query->set('id', 123); |
3371
|
|
|
|
3372
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3373
|
|
|
->method('checkAccess') |
3374
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3375
|
|
|
->will($this->returnValue(true)); |
3376
|
|
|
|
3377
|
|
|
$object = new \stdClass(); |
3378
|
|
|
|
3379
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3380
|
|
|
->method('getObject') |
3381
|
|
|
->will($this->returnValue($object)); |
3382
|
|
|
|
3383
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3384
|
|
|
->method('getClass') |
3385
|
|
|
->will($this->returnValue('Foo')); |
3386
|
|
|
|
3387
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3388
|
|
|
->method('hasReader') |
3389
|
|
|
->with($this->equalTo('Foo')) |
3390
|
|
|
->will($this->returnValue(true)); |
3391
|
|
|
|
3392
|
|
|
$reader = $this->createMock(AuditReaderInterface::class); |
3393
|
|
|
|
3394
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3395
|
|
|
->method('getReader') |
3396
|
|
|
->with($this->equalTo('Foo')) |
3397
|
|
|
->will($this->returnValue($reader)); |
3398
|
|
|
|
3399
|
|
|
$objectRevision = new \stdClass(); |
3400
|
|
|
$objectRevision->revision = 456; |
3401
|
|
|
|
3402
|
|
|
$compareObjectRevision = new \stdClass(); |
3403
|
|
|
$compareObjectRevision->revision = 789; |
3404
|
|
|
|
3405
|
|
|
$reader->expects($this->at(0)) |
3406
|
|
|
->method('find') |
3407
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3408
|
|
|
->will($this->returnValue($objectRevision)); |
3409
|
|
|
|
3410
|
|
|
$reader->expects($this->at(1)) |
3411
|
|
|
->method('find') |
3412
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789)) |
3413
|
|
|
->will($this->returnValue($compareObjectRevision)); |
3414
|
|
|
|
3415
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3416
|
|
|
->method('setSubject') |
3417
|
|
|
->with($this->equalTo($objectRevision)) |
3418
|
|
|
->will($this->returnValue(null)); |
3419
|
|
|
|
3420
|
|
|
$fieldDescriptionCollection = new FieldDescriptionCollection(); |
3421
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3422
|
|
|
->method('getShow') |
3423
|
|
|
->will($this->returnValue($fieldDescriptionCollection)); |
3424
|
|
|
|
3425
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request)); |
|
|
|
|
3426
|
|
|
|
3427
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
3428
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
3429
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
3430
|
|
|
|
3431
|
|
|
$this->assertSame('show', $this->parameters['action']); |
3432
|
|
|
$this->assertSame($objectRevision, $this->parameters['object']); |
3433
|
|
|
$this->assertSame($compareObjectRevision, $this->parameters['object_compare']); |
3434
|
|
|
$this->assertSame($fieldDescriptionCollection, $this->parameters['elements']); |
3435
|
|
|
|
3436
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
3437
|
|
|
$this->assertSame('@SonataAdmin/CRUD/show_compare.html.twig', $this->template); |
3438
|
|
|
} |
3439
|
|
|
|
3440
|
|
|
public function testBatchActionWrongMethod() |
3441
|
|
|
{ |
3442
|
|
|
$this->expectException(NotFoundHttpException::class, 'Invalid request type "GET", POST expected'); |
3443
|
|
|
|
3444
|
|
|
$this->controller->batchAction($this->request); |
|
|
|
|
3445
|
|
|
} |
3446
|
|
|
|
3447
|
|
|
/** |
3448
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3449
|
|
|
* |
3450
|
|
|
* @group legacy |
3451
|
|
|
*/ |
3452
|
|
|
public function testBatchActionActionNotDefined() |
3453
|
|
|
{ |
3454
|
|
|
$this->expectException(\RuntimeException::class, 'The `foo` batch action is not defined'); |
3455
|
|
|
|
3456
|
|
|
$batchActions = []; |
3457
|
|
|
|
3458
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3459
|
|
|
->method('getBatchActions') |
3460
|
|
|
->will($this->returnValue($batchActions)); |
3461
|
|
|
|
3462
|
|
|
$this->request->setMethod('POST'); |
3463
|
|
|
$this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
3464
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3465
|
|
|
|
3466
|
|
|
$this->controller->batchAction($this->request); |
|
|
|
|
3467
|
|
|
} |
3468
|
|
|
|
3469
|
|
|
public function testBatchActionActionInvalidCsrfToken() |
3470
|
|
|
{ |
3471
|
|
|
$this->request->setMethod('POST'); |
3472
|
|
|
$this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
3473
|
|
|
$this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID'); |
3474
|
|
|
|
3475
|
|
|
try { |
3476
|
|
|
$this->controller->batchAction($this->request); |
|
|
|
|
3477
|
|
|
} catch (HttpException $e) { |
3478
|
|
|
$this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage()); |
3479
|
|
|
$this->assertSame(400, $e->getStatusCode()); |
3480
|
|
|
} |
3481
|
|
|
} |
3482
|
|
|
|
3483
|
|
|
/** |
3484
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3485
|
|
|
* |
3486
|
|
|
* @group legacy |
3487
|
|
|
*/ |
3488
|
|
|
public function testBatchActionMethodNotExist() |
3489
|
|
|
{ |
3490
|
|
|
$this->expectException(\RuntimeException::class, 'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be callable'); |
3491
|
|
|
|
3492
|
|
|
$batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3493
|
|
|
|
3494
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3495
|
|
|
->method('getBatchActions') |
3496
|
|
|
->will($this->returnValue($batchActions)); |
3497
|
|
|
|
3498
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3499
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3500
|
|
|
->method('getDatagrid') |
3501
|
|
|
->will($this->returnValue($datagrid)); |
3502
|
|
|
|
3503
|
|
|
$this->request->setMethod('POST'); |
3504
|
|
|
$this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
3505
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3506
|
|
|
|
3507
|
|
|
$this->controller->batchAction($this->request); |
|
|
|
|
3508
|
|
|
} |
3509
|
|
|
|
3510
|
|
|
/** |
3511
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3512
|
|
|
* |
3513
|
|
|
* @group legacy |
3514
|
|
|
*/ |
3515
|
|
|
public function testBatchActionWithoutConfirmation() |
3516
|
|
|
{ |
3517
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3518
|
|
|
|
3519
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3520
|
|
|
->method('getBatchActions') |
3521
|
|
|
->will($this->returnValue($batchActions)); |
3522
|
|
|
|
3523
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3524
|
|
|
|
3525
|
|
|
$query = $this->createMock(ProxyQueryInterface::class); |
3526
|
|
|
$datagrid->expects($this->once()) |
3527
|
|
|
->method('getQuery') |
3528
|
|
|
->will($this->returnValue($query)); |
3529
|
|
|
|
3530
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3531
|
|
|
->method('getDatagrid') |
3532
|
|
|
->will($this->returnValue($datagrid)); |
3533
|
|
|
|
3534
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
3535
|
|
|
|
3536
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3537
|
|
|
->method('checkAccess') |
3538
|
|
|
->with($this->equalTo('batchDelete')) |
3539
|
|
|
->will($this->returnValue(true)); |
3540
|
|
|
|
3541
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3542
|
|
|
->method('getModelManager') |
3543
|
|
|
->will($this->returnValue($modelManager)); |
3544
|
|
|
|
3545
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3546
|
|
|
->method('getClass') |
3547
|
|
|
->will($this->returnValue('Foo')); |
3548
|
|
|
|
3549
|
|
|
$modelManager->expects($this->once()) |
3550
|
|
|
->method('addIdentifiersToQuery') |
3551
|
|
|
->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456'])) |
3552
|
|
|
->will($this->returnValue(true)); |
3553
|
|
|
|
3554
|
|
|
$this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
3555
|
|
|
|
3556
|
|
|
$this->request->setMethod('POST'); |
3557
|
|
|
$this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false])); |
3558
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3559
|
|
|
|
3560
|
|
|
$result = $this->controller->batchAction($this->request); |
|
|
|
|
3561
|
|
|
|
3562
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result); |
3563
|
|
|
$this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
3564
|
|
|
$this->assertSame('list', $result->getTargetUrl()); |
3565
|
|
|
} |
3566
|
|
|
|
3567
|
|
|
/** |
3568
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3569
|
|
|
* |
3570
|
|
|
* @group legacy |
3571
|
|
|
*/ |
3572
|
|
|
public function testBatchActionWithoutConfirmation2() |
3573
|
|
|
{ |
3574
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3575
|
|
|
|
3576
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3577
|
|
|
->method('getBatchActions') |
3578
|
|
|
->will($this->returnValue($batchActions)); |
3579
|
|
|
|
3580
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3581
|
|
|
|
3582
|
|
|
$query = $this->createMock(ProxyQueryInterface::class); |
3583
|
|
|
$datagrid->expects($this->once()) |
3584
|
|
|
->method('getQuery') |
3585
|
|
|
->will($this->returnValue($query)); |
3586
|
|
|
|
3587
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3588
|
|
|
->method('getDatagrid') |
3589
|
|
|
->will($this->returnValue($datagrid)); |
3590
|
|
|
|
3591
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
3592
|
|
|
|
3593
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3594
|
|
|
->method('checkAccess') |
3595
|
|
|
->with($this->equalTo('batchDelete')) |
3596
|
|
|
->will($this->returnValue(true)); |
3597
|
|
|
|
3598
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3599
|
|
|
->method('getModelManager') |
3600
|
|
|
->will($this->returnValue($modelManager)); |
3601
|
|
|
|
3602
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3603
|
|
|
->method('getClass') |
3604
|
|
|
->will($this->returnValue('Foo')); |
3605
|
|
|
|
3606
|
|
|
$modelManager->expects($this->once()) |
3607
|
|
|
->method('addIdentifiersToQuery') |
3608
|
|
|
->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456'])) |
3609
|
|
|
->will($this->returnValue(true)); |
3610
|
|
|
|
3611
|
|
|
$this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
3612
|
|
|
|
3613
|
|
|
$this->request->setMethod('POST'); |
3614
|
|
|
$this->request->request->set('action', 'delete'); |
3615
|
|
|
$this->request->request->set('idx', ['123', '456']); |
3616
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3617
|
|
|
|
3618
|
|
|
$result = $this->controller->batchAction($this->request); |
|
|
|
|
3619
|
|
|
|
3620
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result); |
3621
|
|
|
$this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
3622
|
|
|
$this->assertSame('list', $result->getTargetUrl()); |
3623
|
|
|
} |
3624
|
|
|
|
3625
|
|
|
/** |
3626
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3627
|
|
|
* |
3628
|
|
|
* @group legacy |
3629
|
|
|
*/ |
3630
|
|
|
public function testBatchActionWithConfirmation() |
3631
|
|
|
{ |
3632
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', 'ask_confirmation' => true]]; |
3633
|
|
|
|
3634
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3635
|
|
|
->method('getBatchActions') |
3636
|
|
|
->will($this->returnValue($batchActions)); |
3637
|
|
|
|
3638
|
|
|
$data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]; |
3639
|
|
|
|
3640
|
|
|
$this->request->setMethod('POST'); |
3641
|
|
|
$this->request->request->set('data', json_encode($data)); |
3642
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3643
|
|
|
|
3644
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3645
|
|
|
|
3646
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3647
|
|
|
->method('getDatagrid') |
3648
|
|
|
->will($this->returnValue($datagrid)); |
3649
|
|
|
|
3650
|
|
|
$form = $this->getMockBuilder(Form::class) |
3651
|
|
|
->disableOriginalConstructor() |
3652
|
|
|
->getMock(); |
3653
|
|
|
|
3654
|
|
|
$form->expects($this->once()) |
3655
|
|
|
->method('createView') |
3656
|
|
|
->will($this->returnValue($this->createMock(FormView::class))); |
3657
|
|
|
|
3658
|
|
|
$datagrid->expects($this->once()) |
3659
|
|
|
->method('getForm') |
3660
|
|
|
->will($this->returnValue($form)); |
3661
|
|
|
|
3662
|
|
|
$this->assertInstanceOf(Response::class, $this->controller->batchAction($this->request)); |
|
|
|
|
3663
|
|
|
|
3664
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
3665
|
|
|
$this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']); |
3666
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
3667
|
|
|
|
3668
|
|
|
$this->assertSame('list', $this->parameters['action']); |
3669
|
|
|
$this->assertSame($datagrid, $this->parameters['datagrid']); |
3670
|
|
|
$this->assertInstanceOf(FormView::class, $this->parameters['form']); |
3671
|
|
|
$this->assertSame($data, $this->parameters['data']); |
3672
|
|
|
$this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']); |
3673
|
|
|
$this->assertSame('Foo Bar', $this->parameters['action_label']); |
3674
|
|
|
|
3675
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
3676
|
|
|
$this->assertSame('@SonataAdmin/CRUD/batch_confirmation.html.twig', $this->template); |
3677
|
|
|
} |
3678
|
|
|
|
3679
|
|
|
/** |
3680
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3681
|
|
|
* |
3682
|
|
|
* @group legacy |
3683
|
|
|
*/ |
3684
|
|
|
public function testBatchActionNonRelevantAction() |
3685
|
|
|
{ |
3686
|
|
|
$controller = new BatchAdminController(); |
3687
|
|
|
$controller->setContainer($this->container); |
3688
|
|
|
|
3689
|
|
|
$batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3690
|
|
|
|
3691
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3692
|
|
|
->method('getBatchActions') |
3693
|
|
|
->will($this->returnValue($batchActions)); |
3694
|
|
|
|
3695
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3696
|
|
|
|
3697
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3698
|
|
|
->method('getDatagrid') |
3699
|
|
|
->will($this->returnValue($datagrid)); |
3700
|
|
|
|
3701
|
|
|
$this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle'); |
3702
|
|
|
|
3703
|
|
|
$this->request->setMethod('POST'); |
3704
|
|
|
$this->request->request->set('action', 'foo'); |
3705
|
|
|
$this->request->request->set('idx', ['789']); |
3706
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3707
|
|
|
|
3708
|
|
|
$result = $controller->batchAction($this->request); |
|
|
|
|
3709
|
|
|
|
3710
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result); |
3711
|
|
|
$this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info')); |
3712
|
|
|
$this->assertSame('list', $result->getTargetUrl()); |
3713
|
|
|
} |
3714
|
|
|
|
3715
|
|
|
public function testBatchActionWithCustomConfirmationTemplate() |
3716
|
|
|
{ |
3717
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true, 'template' => 'custom_template.html.twig']]; |
3718
|
|
|
|
3719
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3720
|
|
|
->method('getBatchActions') |
3721
|
|
|
->will($this->returnValue($batchActions)); |
3722
|
|
|
|
3723
|
|
|
$data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]; |
3724
|
|
|
|
3725
|
|
|
$this->request->setMethod('POST'); |
3726
|
|
|
$this->request->request->set('data', json_encode($data)); |
3727
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3728
|
|
|
|
3729
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3730
|
|
|
|
3731
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3732
|
|
|
->method('getDatagrid') |
3733
|
|
|
->will($this->returnValue($datagrid)); |
3734
|
|
|
|
3735
|
|
|
$form = $this->createMock(Form::class); |
3736
|
|
|
|
3737
|
|
|
$form->expects($this->once()) |
3738
|
|
|
->method('createView') |
3739
|
|
|
->will($this->returnValue($this->createMock(FormView::class))); |
3740
|
|
|
|
3741
|
|
|
$datagrid->expects($this->once()) |
3742
|
|
|
->method('getForm') |
3743
|
|
|
->will($this->returnValue($form)); |
3744
|
|
|
|
3745
|
|
|
$this->controller->batchAction($this->request); |
|
|
|
|
3746
|
|
|
|
3747
|
|
|
$this->assertSame('custom_template.html.twig', $this->template); |
3748
|
|
|
} |
3749
|
|
|
|
3750
|
|
|
/** |
3751
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3752
|
|
|
* |
3753
|
|
|
* @group legacy |
3754
|
|
|
*/ |
3755
|
|
|
public function testBatchActionNonRelevantAction2() |
3756
|
|
|
{ |
3757
|
|
|
$controller = new BatchAdminController(); |
3758
|
|
|
$controller->setContainer($this->container); |
3759
|
|
|
|
3760
|
|
|
$batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3761
|
|
|
|
3762
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3763
|
|
|
->method('getBatchActions') |
3764
|
|
|
->will($this->returnValue($batchActions)); |
3765
|
|
|
|
3766
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3767
|
|
|
|
3768
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3769
|
|
|
->method('getDatagrid') |
3770
|
|
|
->will($this->returnValue($datagrid)); |
3771
|
|
|
|
3772
|
|
|
$this->expectTranslate('flash_foo_error', [], 'SonataAdminBundle'); |
3773
|
|
|
|
3774
|
|
|
$this->request->setMethod('POST'); |
3775
|
|
|
$this->request->request->set('action', 'foo'); |
3776
|
|
|
$this->request->request->set('idx', ['999']); |
3777
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3778
|
|
|
|
3779
|
|
|
$result = $controller->batchAction($this->request); |
|
|
|
|
3780
|
|
|
|
3781
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result); |
3782
|
|
|
$this->assertSame(['flash_foo_error'], $this->session->getFlashBag()->get('sonata_flash_info')); |
3783
|
|
|
$this->assertSame('list', $result->getTargetUrl()); |
3784
|
|
|
} |
3785
|
|
|
|
3786
|
|
|
/** |
3787
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3788
|
|
|
* |
3789
|
|
|
* @group legacy |
3790
|
|
|
*/ |
3791
|
|
|
public function testBatchActionNoItems() |
3792
|
|
|
{ |
3793
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true]]; |
3794
|
|
|
|
3795
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3796
|
|
|
->method('getBatchActions') |
3797
|
|
|
->will($this->returnValue($batchActions)); |
3798
|
|
|
|
3799
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3800
|
|
|
|
3801
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3802
|
|
|
->method('getDatagrid') |
3803
|
|
|
->will($this->returnValue($datagrid)); |
3804
|
|
|
|
3805
|
|
|
$this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle'); |
3806
|
|
|
|
3807
|
|
|
$this->request->setMethod('POST'); |
3808
|
|
|
$this->request->request->set('action', 'delete'); |
3809
|
|
|
$this->request->request->set('idx', []); |
3810
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3811
|
|
|
|
3812
|
|
|
$result = $this->controller->batchAction($this->request); |
|
|
|
|
3813
|
|
|
|
3814
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result); |
3815
|
|
|
$this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info')); |
3816
|
|
|
$this->assertSame('list', $result->getTargetUrl()); |
3817
|
|
|
} |
3818
|
|
|
|
3819
|
|
|
/** |
3820
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3821
|
|
|
* |
3822
|
|
|
* @group legacy |
3823
|
|
|
*/ |
3824
|
|
|
public function testBatchActionNoItemsEmptyQuery() |
3825
|
|
|
{ |
3826
|
|
|
$controller = new BatchAdminController(); |
3827
|
|
|
$controller->setContainer($this->container); |
3828
|
|
|
|
3829
|
|
|
$batchActions = ['bar' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3830
|
|
|
|
3831
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3832
|
|
|
->method('getBatchActions') |
3833
|
|
|
->will($this->returnValue($batchActions)); |
3834
|
|
|
|
3835
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3836
|
|
|
|
3837
|
|
|
$query = $this->createMock(ProxyQueryInterface::class); |
3838
|
|
|
$datagrid->expects($this->once()) |
3839
|
|
|
->method('getQuery') |
3840
|
|
|
->will($this->returnValue($query)); |
3841
|
|
|
|
3842
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3843
|
|
|
->method('getDatagrid') |
3844
|
|
|
->will($this->returnValue($datagrid)); |
3845
|
|
|
|
3846
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
3847
|
|
|
|
3848
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3849
|
|
|
->method('getModelManager') |
3850
|
|
|
->will($this->returnValue($modelManager)); |
3851
|
|
|
|
3852
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3853
|
|
|
->method('getClass') |
3854
|
|
|
->will($this->returnValue('Foo')); |
3855
|
|
|
|
3856
|
|
|
$this->request->setMethod('POST'); |
3857
|
|
|
$this->request->request->set('action', 'bar'); |
3858
|
|
|
$this->request->request->set('idx', []); |
3859
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3860
|
|
|
|
3861
|
|
|
$this->expectTranslate('flash_batch_no_elements_processed', [], 'SonataAdminBundle'); |
3862
|
|
|
$result = $controller->batchAction($this->request); |
|
|
|
|
3863
|
|
|
|
3864
|
|
|
$this->assertInstanceOf(Response::class, $result); |
3865
|
|
|
$this->assertRegExp('/Redirecting to list/', $result->getContent()); |
3866
|
|
|
} |
3867
|
|
|
|
3868
|
|
|
/** |
3869
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3870
|
|
|
* |
3871
|
|
|
* @group legacy |
3872
|
|
|
*/ |
3873
|
|
|
public function testBatchActionWithRequesData() |
3874
|
|
|
{ |
3875
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3876
|
|
|
|
3877
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3878
|
|
|
->method('getBatchActions') |
3879
|
|
|
->will($this->returnValue($batchActions)); |
3880
|
|
|
|
3881
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
3882
|
|
|
|
3883
|
|
|
$query = $this->createMock(ProxyQueryInterface::class); |
3884
|
|
|
$datagrid->expects($this->once()) |
3885
|
|
|
->method('getQuery') |
3886
|
|
|
->will($this->returnValue($query)); |
3887
|
|
|
|
3888
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3889
|
|
|
->method('getDatagrid') |
3890
|
|
|
->will($this->returnValue($datagrid)); |
3891
|
|
|
|
3892
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
3893
|
|
|
|
3894
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3895
|
|
|
->method('checkAccess') |
3896
|
|
|
->with($this->equalTo('batchDelete')) |
3897
|
|
|
->will($this->returnValue(true)); |
3898
|
|
|
|
3899
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3900
|
|
|
->method('getModelManager') |
3901
|
|
|
->will($this->returnValue($modelManager)); |
3902
|
|
|
|
3903
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3904
|
|
|
->method('getClass') |
3905
|
|
|
->will($this->returnValue('Foo')); |
3906
|
|
|
|
3907
|
|
|
$modelManager->expects($this->once()) |
3908
|
|
|
->method('addIdentifiersToQuery') |
3909
|
|
|
->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456'])) |
3910
|
|
|
->will($this->returnValue(true)); |
3911
|
|
|
|
3912
|
|
|
$this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
3913
|
|
|
|
3914
|
|
|
$this->request->setMethod('POST'); |
3915
|
|
|
$this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false])); |
3916
|
|
|
$this->request->request->set('foo', 'bar'); |
3917
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3918
|
|
|
|
3919
|
|
|
$result = $this->controller->batchAction($this->request); |
|
|
|
|
3920
|
|
|
|
3921
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $result); |
3922
|
|
|
$this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
3923
|
|
|
$this->assertSame('list', $result->getTargetUrl()); |
3924
|
|
|
$this->assertSame('bar', $this->request->request->get('foo')); |
3925
|
|
|
} |
3926
|
|
|
|
3927
|
|
|
public function testItThrowsWhenCallingAnUndefinedMethod() |
3928
|
|
|
{ |
3929
|
|
|
$this->expectException( |
3930
|
|
|
\LogicException::class |
3931
|
|
|
); |
3932
|
|
|
$this->expectExceptionMessage( |
3933
|
|
|
'Call to undefined method Sonata\AdminBundle\Controller\CRUDController::doesNotExist' |
3934
|
|
|
); |
3935
|
|
|
$this->controller->doesNotExist(); |
|
|
|
|
3936
|
|
|
} |
3937
|
|
|
|
3938
|
|
|
/** |
3939
|
|
|
* @expectedDeprecation Method Sonata\AdminBundle\Controller\CRUDController::render has been renamed to Sonata\AdminBundle\Controller\CRUDController::renderWithExtraParams. |
3940
|
|
|
*/ |
3941
|
|
|
public function testRenderIsDeprecated() |
3942
|
|
|
{ |
3943
|
|
|
$this->controller->render('toto.html.twig'); |
|
|
|
|
3944
|
|
|
} |
3945
|
|
|
|
3946
|
|
|
public function getCsrfProvider() |
3947
|
|
|
{ |
3948
|
|
|
return $this->csrfProvider; |
3949
|
|
|
} |
3950
|
|
|
|
3951
|
|
|
public function getToStringValues() |
3952
|
|
|
{ |
3953
|
|
|
return [ |
3954
|
|
|
['', ''], |
3955
|
|
|
['Foo', 'Foo'], |
3956
|
|
|
['<a href="http://foo">Bar</a>', '<a href="http://foo">Bar</a>'], |
3957
|
|
|
['<>&"'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/', '<>&"\'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/'], |
3958
|
|
|
]; |
3959
|
|
|
} |
3960
|
|
|
|
3961
|
|
|
private function assertLoggerLogsModelManagerException($subject, $method) |
3962
|
|
|
{ |
3963
|
|
|
$exception = new ModelManagerException( |
3964
|
|
|
$message = 'message', |
3965
|
|
|
1234, |
3966
|
|
|
new \Exception($previousExceptionMessage = 'very useful message') |
3967
|
|
|
); |
3968
|
|
|
|
3969
|
|
|
$subject->expects($this->once()) |
3970
|
|
|
->method($method) |
3971
|
|
|
->will($this->returnCallback(function () use ($exception) { |
3972
|
|
|
throw $exception; |
3973
|
|
|
})); |
3974
|
|
|
|
3975
|
|
|
$this->logger->expects($this->once()) |
3976
|
|
|
->method('error') |
3977
|
|
|
->with($message, [ |
3978
|
|
|
'exception' => $exception, |
3979
|
|
|
'previous_exception_message' => $previousExceptionMessage, |
3980
|
|
|
]); |
3981
|
|
|
} |
3982
|
|
|
|
3983
|
|
|
private function expectTranslate($id, array $parameters = [], $domain = null, $locale = null) |
3984
|
|
|
{ |
3985
|
|
|
$this->translator->expects($this->once()) |
|
|
|
|
3986
|
|
|
->method('trans') |
3987
|
|
|
->with($this->equalTo($id), $this->equalTo($parameters), $this->equalTo($domain), $this->equalTo($locale)) |
3988
|
|
|
->will($this->returnValue($id)); |
3989
|
|
|
} |
3990
|
|
|
} |
3991
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.