CRUDControllerTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Sonata\AdminBundle\Controller\CRUDController;
  15. use Sonata\AdminBundle\Admin\Pool;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Bridge\Twig\Extension\FormExtension;
  18. use Symfony\Component\HttpFoundation\Session\Session;
  19. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  20. use Sonata\AdminBundle\Exception\ModelManagerException;
  21. /**
  22. * Test for CRUDController
  23. *
  24. * @author Andrej Hudec <pulzarraider@gmail.com>
  25. */
  26. class CRUDControllerTest extends \PHPUnit_Framework_TestCase
  27. {
  28. /**
  29. * @var CRUDController
  30. */
  31. private $controller;
  32. /**
  33. * @var Request
  34. */
  35. private $request;
  36. /**
  37. * @var Sonata\AdminBundle\Admin\AdminInterface
  38. */
  39. private $admin;
  40. /**
  41. * @var Pool
  42. */
  43. private $pool;
  44. /**
  45. * @var array
  46. */
  47. private $parameters;
  48. /**
  49. *
  50. * @var Session
  51. */
  52. private $session;
  53. /**
  54. * {@inheritDoc}
  55. */
  56. protected function setUp()
  57. {
  58. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  59. $this->request = new Request();
  60. $this->pool = new Pool($container, 'title', 'logo.png');
  61. $this->request->attributes->set('_sonata_admin', 'foo.admin');
  62. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  63. $this->parameters = array();
  64. // php 5.3 BC
  65. $params = &$this->parameters;
  66. $templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine', array(), array($container, array()));
  67. $templating->expects($this->any())
  68. ->method('renderResponse')
  69. ->will($this->returnCallback(function($view, array $parameters = array(), Response $response = null) use (&$params) {
  70. if (null === $response) {
  71. $response = new Response();
  72. }
  73. $params = $parameters;
  74. return $response;
  75. }));
  76. $this->session = new Session(new MockArraySessionStorage());
  77. // php 5.3 BC
  78. $pool = $this->pool;
  79. $request = $this->request;
  80. $admin = $this->admin;
  81. $session = $this->session;
  82. $twig = $this->getMockBuilder('Twig_Environment')
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $twigRenderer = $this->getMock('Symfony\Bridge\Twig\Form\TwigRendererInterface');
  86. $formExtension = new FormExtension($twigRenderer);
  87. $twig->expects($this->any())
  88. ->method('getExtension')
  89. ->will($this->returnCallback(function($name) use ($formExtension) {
  90. switch ($name) {
  91. case 'form':
  92. return $formExtension;
  93. }
  94. return null;
  95. }));
  96. $container->expects($this->any())
  97. ->method('get')
  98. ->will($this->returnCallback(function($id) use ($pool, $request, $admin, $templating, $twig, $session) {
  99. switch ($id) {
  100. case 'sonata.admin.pool':
  101. return $pool;
  102. case 'request':
  103. return $request;
  104. case 'foo.admin':
  105. return $admin;
  106. case 'templating':
  107. return $templating;
  108. case 'twig':
  109. return $twig;
  110. case 'session':
  111. return $session;
  112. }
  113. return null;
  114. }));
  115. $this->admin->expects($this->any())
  116. ->method('getTemplate')
  117. ->will($this->returnCallback(function($name) {
  118. switch ($name) {
  119. case 'ajax':
  120. return 'SonataAdminBundle::ajax_layout.html.twig';
  121. case 'layout':
  122. return 'SonataAdminBundle::standard_layout.html.twig';
  123. case 'show':
  124. return 'SonataAdminBundle:CRUD:show.html.twig';
  125. case 'edit':
  126. return 'SonataAdminBundle:CRUD:edit.html.twig';
  127. }
  128. return null;
  129. }));
  130. $this->admin->expects($this->any())
  131. ->method('getIdParameter')
  132. ->will($this->returnValue('id'));
  133. $this->admin->expects($this->any())
  134. ->method('generateUrl')
  135. ->will($this->returnCallback(function($name, array $parameters = array(), $absolute = false) {
  136. $result = $name;
  137. if (!empty($parameters)) {
  138. $result .= '?'.http_build_query($parameters);
  139. }
  140. return $result;
  141. }));
  142. $this->admin->expects($this->any())
  143. ->method('generateObjectUrl')
  144. ->will($this->returnCallback(function($name, $object, array $parameters = array(), $absolute = false) {
  145. $result = get_class($object).'_'.$name;
  146. if (!empty($parameters)) {
  147. $result .= '?'.http_build_query($parameters);
  148. }
  149. return $result;
  150. }));
  151. $this->controller = new CRUDController();
  152. $this->controller->setContainer($container);
  153. }
  154. public function testRenderJson1()
  155. {
  156. $data = array('example'=>'123', 'foo'=>'bar');
  157. $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded');
  158. $response = $this->controller->renderJson($data);
  159. $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
  160. $this->assertEquals(json_encode($data), $response->getContent());
  161. }
  162. public function testRenderJson2()
  163. {
  164. $data = array('example'=>'123', 'foo'=>'bar');
  165. $this->request->headers->set('Content-Type', 'multipart/form-data');
  166. $response = $this->controller->renderJson($data);
  167. $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
  168. $this->assertEquals(json_encode($data), $response->getContent());
  169. }
  170. public function testRenderJsonAjax()
  171. {
  172. $data = array('example'=>'123', 'foo'=>'bar');
  173. $this->request->attributes->set('_xml_http_request', true);
  174. $this->request->headers->set('Content-Type', 'multipart/form-data');
  175. $response = $this->controller->renderJson($data);
  176. $this->assertEquals($response->headers->get('Content-Type'), 'text/plain');
  177. $this->assertEquals(json_encode($data), $response->getContent());
  178. }
  179. public function testIsXmlHttpRequest()
  180. {
  181. $this->assertFalse($this->controller->isXmlHttpRequest());
  182. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  183. $this->assertTrue($this->controller->isXmlHttpRequest());
  184. $this->request->headers->remove('X-Requested-With');
  185. $this->assertFalse($this->controller->isXmlHttpRequest());
  186. $this->request->attributes->set('_xml_http_request', true);
  187. $this->assertTrue($this->controller->isXmlHttpRequest());
  188. }
  189. public function testConfigureWithException()
  190. {
  191. $this->setExpectedException('RuntimeException', 'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`');
  192. $this->request->attributes->remove('_sonata_admin');
  193. $this->controller->configure();
  194. }
  195. public function testConfigureWithException2()
  196. {
  197. $this->setExpectedException('RuntimeException', 'Unable to find the admin class related to the current controller (Sonata\AdminBundle\Controller\CRUDController)');
  198. $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
  199. $this->controller->configure();
  200. }
  201. public function testGetBaseTemplate()
  202. {
  203. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->controller->getBaseTemplate());
  204. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  205. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->controller->getBaseTemplate());
  206. $this->request->headers->remove('X-Requested-With');
  207. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->controller->getBaseTemplate());
  208. $this->request->attributes->set('_xml_http_request', true);
  209. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->controller->getBaseTemplate());
  210. }
  211. public function testRender()
  212. {
  213. $this->parameters = array();
  214. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array()));
  215. $this->assertEquals($this->admin, $this->parameters['admin']);
  216. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  217. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  218. }
  219. public function testRenderCustomParams()
  220. {
  221. $this->parameters = array();
  222. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array('foo'=>'bar')));
  223. $this->assertEquals($this->admin, $this->parameters['admin']);
  224. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  225. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  226. $this->assertEquals('bar', $this->parameters['foo']);
  227. }
  228. public function testRenderAjax()
  229. {
  230. $this->parameters = array();
  231. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  232. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array('foo'=>'bar')));
  233. $this->assertEquals($this->admin, $this->parameters['admin']);
  234. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  235. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  236. $this->assertEquals('bar', $this->parameters['foo']);
  237. }
  238. public function testListActionAccessDenied()
  239. {
  240. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  241. $this->admin->expects($this->once())
  242. ->method('isGranted')
  243. ->with($this->equalTo('LIST'))
  244. ->will($this->returnValue(false));
  245. $this->controller->listAction();
  246. }
  247. public function testListAction()
  248. {
  249. $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
  250. $this->admin->expects($this->once())
  251. ->method('isGranted')
  252. ->with($this->equalTo('LIST'))
  253. ->will($this->returnValue(true));
  254. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  255. ->disableOriginalConstructor()
  256. ->getMock();
  257. $form->expects($this->once())
  258. ->method('createView')
  259. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  260. $this->admin->expects($this->once())
  261. ->method('getDatagrid')
  262. ->will($this->returnValue($datagrid));
  263. $datagrid->expects($this->once())
  264. ->method('getForm')
  265. ->will($this->returnValue($form));
  266. $this->parameters = array();
  267. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->listAction());
  268. $this->assertEquals($this->admin, $this->parameters['admin']);
  269. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  270. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  271. $this->assertEquals('list', $this->parameters['action']);
  272. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  273. $this->assertInstanceOf('Sonata\AdminBundle\Datagrid\DatagridInterface', $this->parameters['datagrid']);
  274. $this->assertEquals('', $this->parameters['csrf_token']);
  275. }
  276. public function testBatchActionDeleteAccessDenied()
  277. {
  278. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  279. $this->admin->expects($this->once())
  280. ->method('isGranted')
  281. ->with($this->equalTo('DELETE'))
  282. ->will($this->returnValue(false));
  283. $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  284. }
  285. public function testBatchActionDelete()
  286. {
  287. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  288. $this->admin->expects($this->once())
  289. ->method('isGranted')
  290. ->with($this->equalTo('DELETE'))
  291. ->will($this->returnValue(true));
  292. $this->admin->expects($this->once())
  293. ->method('getModelManager')
  294. ->will($this->returnValue($modelManager));
  295. $this->admin->expects($this->once())
  296. ->method('getFilterParameters')
  297. ->will($this->returnValue(array('foo'=>'bar')));
  298. $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  299. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  300. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  301. $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  302. }
  303. public function testBatchActionDeleteWithModelManagerException()
  304. {
  305. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  306. $modelManager->expects($this->once())
  307. ->method('batchDelete')
  308. ->will($this->returnCallback(function() {
  309. throw new ModelManagerException();
  310. }));
  311. $this->admin->expects($this->once())
  312. ->method('getModelManager')
  313. ->will($this->returnValue($modelManager));
  314. $this->admin->expects($this->once())
  315. ->method('getFilterParameters')
  316. ->will($this->returnValue(array('foo'=>'bar')));
  317. $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  318. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  319. $this->assertSame(array('flash_batch_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  320. $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  321. }
  322. public function testShowActionNotFoundException()
  323. {
  324. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  325. $this->admin->expects($this->once())
  326. ->method('getObject')
  327. ->will($this->returnValue(false));
  328. $this->controller->showAction();
  329. }
  330. public function testShowActionAccessDenied()
  331. {
  332. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  333. $this->admin->expects($this->once())
  334. ->method('getObject')
  335. ->will($this->returnValue(new \stdClass()));
  336. $this->admin->expects($this->once())
  337. ->method('isGranted')
  338. ->with($this->equalTo('VIEW'))
  339. ->will($this->returnValue(false));
  340. $this->controller->showAction();
  341. }
  342. public function testShowAction()
  343. {
  344. $object = new \stdClass();
  345. $this->admin->expects($this->once())
  346. ->method('getObject')
  347. ->will($this->returnValue($object));
  348. $this->admin->expects($this->once())
  349. ->method('isGranted')
  350. ->with($this->equalTo('VIEW'))
  351. ->will($this->returnValue(true));
  352. $show = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection');
  353. $this->admin->expects($this->once())
  354. ->method('getShow')
  355. ->will($this->returnValue($show));
  356. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->showAction());
  357. $this->assertEquals($this->admin, $this->parameters['admin']);
  358. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  359. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  360. $this->assertEquals('show', $this->parameters['action']);
  361. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $this->parameters['elements']);
  362. $this->assertEquals($object, $this->parameters['object']);
  363. }
  364. /**
  365. * @dataProvider getRedirectToTests
  366. */
  367. public function testRedirectTo($expected, $queryParams, $hasActiveSubclass)
  368. {
  369. $this->admin->expects($this->any())
  370. ->method('hasActiveSubclass')
  371. ->will($this->returnValue($hasActiveSubclass));
  372. $object = new \stdClass();
  373. foreach ($queryParams as $key => $value) {
  374. $this->request->query->set($key, $value);
  375. }
  376. $response = $this->controller->redirectTo($object);
  377. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  378. $this->assertEquals($expected, $response->getTargetUrl());
  379. }
  380. public function getRedirectToTests()
  381. {
  382. return array(
  383. array('stdClass_edit', array(), false),
  384. array('list', array('btn_update_and_list'=>true), false),
  385. array('list', array('btn_create_and_list'=>true), false),
  386. array('create', array('btn_create_and_create'=>true), false),
  387. array('create?subclass=foo', array('btn_create_and_create'=>true, 'subclass'=>'foo'), true),
  388. );
  389. }
  390. public function testAddFlash()
  391. {
  392. $this->controller->addFlash('foo', 'bar');
  393. $this->assertSame(array('bar'), $this->session->getFlashBag()->get('foo'));
  394. }
  395. public function testDeleteActionNotFoundException()
  396. {
  397. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  398. $this->admin->expects($this->once())
  399. ->method('getObject')
  400. ->will($this->returnValue(false));
  401. $this->controller->deleteAction(1);
  402. }
  403. public function testDeleteActionAccessDenied()
  404. {
  405. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  406. $this->admin->expects($this->once())
  407. ->method('getObject')
  408. ->will($this->returnValue(new \stdClass()));
  409. $this->admin->expects($this->once())
  410. ->method('isGranted')
  411. ->with($this->equalTo('DELETE'))
  412. ->will($this->returnValue(false));
  413. $this->controller->deleteAction(1);
  414. }
  415. public function testDeleteAction()
  416. {
  417. $object = new \stdClass();
  418. $this->admin->expects($this->once())
  419. ->method('getObject')
  420. ->will($this->returnValue($object));
  421. $this->admin->expects($this->once())
  422. ->method('isGranted')
  423. ->with($this->equalTo('DELETE'))
  424. ->will($this->returnValue(true));
  425. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
  426. $this->assertEquals($this->admin, $this->parameters['admin']);
  427. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  428. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  429. $this->assertEquals('delete', $this->parameters['action']);
  430. $this->assertEquals($object, $this->parameters['object']);
  431. $this->assertEquals('', $this->parameters['csrf_token']);
  432. }
  433. public function testDeleteActionAjaxSuccess()
  434. {
  435. $object = new \stdClass();
  436. $this->admin->expects($this->once())
  437. ->method('getObject')
  438. ->will($this->returnValue($object));
  439. $this->admin->expects($this->once())
  440. ->method('isGranted')
  441. ->with($this->equalTo('DELETE'))
  442. ->will($this->returnValue(true));
  443. $this->request->setMethod('DELETE');
  444. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  445. $response = $this->controller->deleteAction(1);
  446. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  447. $this->assertEquals(json_encode(array('result'=>'ok')), $response->getContent());
  448. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  449. }
  450. public function testDeleteActionAjaxError()
  451. {
  452. $object = new \stdClass();
  453. $this->admin->expects($this->once())
  454. ->method('getObject')
  455. ->will($this->returnValue($object));
  456. $this->admin->expects($this->once())
  457. ->method('isGranted')
  458. ->with($this->equalTo('DELETE'))
  459. ->will($this->returnValue(true));
  460. $this->admin->expects($this->once())
  461. ->method('delete')
  462. ->will($this->returnCallback(function() {
  463. throw new ModelManagerException();
  464. }));
  465. $this->request->setMethod('DELETE');
  466. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  467. $response = $this->controller->deleteAction(1);
  468. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  469. $this->assertEquals(json_encode(array('result'=>'error')), $response->getContent());
  470. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  471. }
  472. public function testDeleteActionSuccess()
  473. {
  474. $object = new \stdClass();
  475. $this->admin->expects($this->once())
  476. ->method('getObject')
  477. ->will($this->returnValue($object));
  478. $this->admin->expects($this->once())
  479. ->method('isGranted')
  480. ->with($this->equalTo('DELETE'))
  481. ->will($this->returnValue(true));
  482. $this->request->setMethod('DELETE');
  483. $response = $this->controller->deleteAction(1);
  484. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  485. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  486. $this->assertEquals('list', $response->getTargetUrl());
  487. }
  488. public function testDeleteActionError()
  489. {
  490. $object = new \stdClass();
  491. $this->admin->expects($this->once())
  492. ->method('getObject')
  493. ->will($this->returnValue($object));
  494. $this->admin->expects($this->once())
  495. ->method('isGranted')
  496. ->with($this->equalTo('DELETE'))
  497. ->will($this->returnValue(true));
  498. $this->admin->expects($this->once())
  499. ->method('delete')
  500. ->will($this->returnCallback(function() {
  501. throw new ModelManagerException();
  502. }));
  503. $this->request->setMethod('DELETE');
  504. $response = $this->controller->deleteAction(1);
  505. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  506. $this->assertSame(array('flash_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  507. $this->assertEquals('list', $response->getTargetUrl());
  508. }
  509. public function testEditActionNotFoundException()
  510. {
  511. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  512. $this->admin->expects($this->once())
  513. ->method('getObject')
  514. ->will($this->returnValue(false));
  515. $this->controller->editAction();
  516. }
  517. public function testEditActionAccessDenied()
  518. {
  519. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  520. $this->admin->expects($this->once())
  521. ->method('getObject')
  522. ->will($this->returnValue(new \stdClass()));
  523. $this->admin->expects($this->once())
  524. ->method('isGranted')
  525. ->with($this->equalTo('EDIT'))
  526. ->will($this->returnValue(false));
  527. $this->controller->editAction();
  528. }
  529. public function testEditAction()
  530. {
  531. $object = new \stdClass();
  532. $this->admin->expects($this->once())
  533. ->method('getObject')
  534. ->will($this->returnValue($object));
  535. $this->admin->expects($this->once())
  536. ->method('isGranted')
  537. ->with($this->equalTo('EDIT'))
  538. ->will($this->returnValue(true));
  539. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  540. ->disableOriginalConstructor()
  541. ->getMock();
  542. $this->admin->expects($this->once())
  543. ->method('getForm')
  544. ->will($this->returnValue($form));
  545. $formView = $this->getMock('Symfony\Component\Form\FormView');
  546. $form->expects($this->any())
  547. ->method('createView')
  548. ->will($this->returnValue($formView));
  549. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  550. $this->assertEquals($this->admin, $this->parameters['admin']);
  551. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  552. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  553. $this->assertEquals('edit', $this->parameters['action']);
  554. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  555. $this->assertEquals($object, $this->parameters['object']);
  556. }
  557. public function testEditActionSuccess()
  558. {
  559. $object = new \stdClass();
  560. $this->admin->expects($this->once())
  561. ->method('getObject')
  562. ->will($this->returnValue($object));
  563. $this->admin->expects($this->once())
  564. ->method('isGranted')
  565. ->with($this->equalTo('EDIT'))
  566. ->will($this->returnValue(true));
  567. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  568. ->disableOriginalConstructor()
  569. ->getMock();
  570. $this->admin->expects($this->once())
  571. ->method('getForm')
  572. ->will($this->returnValue($form));
  573. $form->expects($this->once())
  574. ->method('isValid')
  575. ->will($this->returnValue(true));
  576. $this->request->setMethod('POST');
  577. $response = $this->controller->editAction();
  578. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  579. $this->assertSame(array('flash_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  580. $this->assertEquals('stdClass_edit', $response->getTargetUrl());
  581. }
  582. public function testEditActionError()
  583. {
  584. $object = new \stdClass();
  585. $this->admin->expects($this->once())
  586. ->method('getObject')
  587. ->will($this->returnValue($object));
  588. $this->admin->expects($this->once())
  589. ->method('isGranted')
  590. ->with($this->equalTo('EDIT'))
  591. ->will($this->returnValue(true));
  592. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  593. ->disableOriginalConstructor()
  594. ->getMock();
  595. $this->admin->expects($this->once())
  596. ->method('getForm')
  597. ->will($this->returnValue($form));
  598. $form->expects($this->once())
  599. ->method('isValid')
  600. ->will($this->returnValue(false));
  601. $this->request->setMethod('POST');
  602. $formView = $this->getMock('Symfony\Component\Form\FormView');
  603. $form->expects($this->any())
  604. ->method('createView')
  605. ->will($this->returnValue($formView));
  606. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  607. $this->assertEquals($this->admin, $this->parameters['admin']);
  608. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  609. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  610. $this->assertEquals('edit', $this->parameters['action']);
  611. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  612. $this->assertEquals($object, $this->parameters['object']);
  613. }
  614. public function testEditActionAjaxSuccess()
  615. {
  616. $object = new \stdClass();
  617. $this->admin->expects($this->once())
  618. ->method('getObject')
  619. ->will($this->returnValue($object));
  620. $this->admin->expects($this->once())
  621. ->method('isGranted')
  622. ->with($this->equalTo('EDIT'))
  623. ->will($this->returnValue(true));
  624. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  625. ->disableOriginalConstructor()
  626. ->getMock();
  627. $this->admin->expects($this->once())
  628. ->method('getForm')
  629. ->will($this->returnValue($form));
  630. $form->expects($this->once())
  631. ->method('isValid')
  632. ->will($this->returnValue(true));
  633. $this->admin->expects($this->once())
  634. ->method('getNormalizedIdentifier')
  635. ->with($this->equalTo($object))
  636. ->will($this->returnValue('foo_normalized'));
  637. $this->request->setMethod('POST');
  638. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  639. $response = $this->controller->editAction();
  640. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  641. $this->assertEquals(json_encode(array('result'=>'ok', 'objectId' => 'foo_normalized')), $response->getContent());
  642. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  643. }
  644. public function testEditActionAjaxError()
  645. {
  646. $object = new \stdClass();
  647. $this->admin->expects($this->once())
  648. ->method('getObject')
  649. ->will($this->returnValue($object));
  650. $this->admin->expects($this->once())
  651. ->method('isGranted')
  652. ->with($this->equalTo('EDIT'))
  653. ->will($this->returnValue(true));
  654. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  655. ->disableOriginalConstructor()
  656. ->getMock();
  657. $this->admin->expects($this->once())
  658. ->method('getForm')
  659. ->will($this->returnValue($form));
  660. $form->expects($this->once())
  661. ->method('isValid')
  662. ->will($this->returnValue(false));
  663. $this->request->setMethod('POST');
  664. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  665. $formView = $this->getMock('Symfony\Component\Form\FormView');
  666. $form->expects($this->any())
  667. ->method('createView')
  668. ->will($this->returnValue($formView));
  669. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  670. $this->assertEquals($this->admin, $this->parameters['admin']);
  671. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  672. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  673. $this->assertEquals('edit', $this->parameters['action']);
  674. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  675. $this->assertEquals($object, $this->parameters['object']);
  676. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  677. }
  678. }