HelperControllerTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 Sonata\AdminBundle\Admin\AdminHelper;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Sonata\AdminBundle\Controller\HelperController;
  14. use Symfony\Component\Validator\ConstraintViolation;
  15. use Symfony\Component\Validator\ConstraintViolationList;
  16. use \Twig_Environment as Twig;
  17. use \Twig_ExtensionInterface as Twig_ExtensionInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\DependencyInjection\ContainerInterface;
  20. use Sonata\AdminBundle\Admin\AdminInterface;
  21. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  22. use Symfony\Component\Form\Form;
  23. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\Form\FormBuilder;
  26. use Symfony\Component\Form\FormView;
  27. class AdminControllerHelper_Foo
  28. {
  29. private $bar;
  30. public function getAdminTitle()
  31. {
  32. return 'foo';
  33. }
  34. public function setEnabled($value)
  35. {
  36. }
  37. public function setBar(AdminControllerHelper_Bar $bar)
  38. {
  39. $this->bar = $bar;
  40. }
  41. public function getBar()
  42. {
  43. return $this->bar;
  44. }
  45. }
  46. class AdminControllerHelper_Bar
  47. {
  48. public function getAdminTitle()
  49. {
  50. return 'bar';
  51. }
  52. public function setEnabled($value)
  53. {
  54. }
  55. public function getEnabled()
  56. {
  57. }
  58. }
  59. class HelperControllerTest extends \PHPUnit_Framework_TestCase
  60. {
  61. /**
  62. * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  63. */
  64. public function testgetShortObjectDescriptionActionInvalidAdmin()
  65. {
  66. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  67. $twig = new Twig();
  68. $request = new Request(array(
  69. 'code' => 'sonata.post.admin',
  70. 'objectId' => 42,
  71. 'uniqid' => 'asdasd123'
  72. ));
  73. $pool = new Pool($container, 'title', 'logo');
  74. $pool->setAdminServiceIds(array('sonata.post.admin'));
  75. $helper = new AdminHelper($pool);
  76. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  77. $controller = new HelperController($twig, $pool, $helper, $validator);
  78. $controller->getShortObjectDescriptionAction($request);
  79. }
  80. /**
  81. * @expectedException \RuntimeException
  82. * @exceptionMessage Invalid format
  83. */
  84. public function testgetShortObjectDescriptionActionObjectDoesNotExist()
  85. {
  86. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  87. $admin->expects($this->once())->method('setUniqid');
  88. $admin->expects($this->once())->method('getObject')->will($this->returnValue(false));
  89. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  90. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  91. $twig = new Twig();
  92. $request = new Request(array(
  93. 'code' => 'sonata.post.admin',
  94. 'objectId' => 42,
  95. 'uniqid' => 'asdasd123'
  96. ));
  97. $pool = new Pool($container, 'title', 'logo');
  98. $pool->setAdminServiceIds(array('sonata.post.admin'));
  99. $helper = new AdminHelper($pool);
  100. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  101. $controller = new HelperController($twig, $pool, $helper, $validator);
  102. $controller->getShortObjectDescriptionAction($request);
  103. }
  104. public function testgetShortObjectDescriptionActionEmptyObjectId()
  105. {
  106. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  107. $admin->expects($this->once())->method('setUniqid');
  108. $admin->expects($this->once())->method('getObject')->with($this->identicalTo(null))->will($this->returnValue(false));
  109. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  110. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  111. $twig = new Twig();
  112. $request = new Request(array(
  113. 'code' => 'sonata.post.admin',
  114. 'objectId' => "",
  115. 'uniqid' => 'asdasd123',
  116. '_format' => 'html'
  117. ));
  118. $pool = new Pool($container, 'title', 'logo');
  119. $pool->setAdminServiceIds(array('sonata.post.admin'));
  120. $helper = new AdminHelper($pool);
  121. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  122. $controller = new HelperController($twig, $pool, $helper, $validator);
  123. $controller->getShortObjectDescriptionAction($request);
  124. }
  125. public function testgetShortObjectDescriptionActionObject()
  126. {
  127. $mockTemplate = 'AdminHelperTest:mock-short-object-description.html.twig';
  128. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  129. $admin->expects($this->once())->method('setUniqid');
  130. $admin->expects($this->once())->method('getTemplate')->will($this->returnValue($mockTemplate));
  131. $admin->expects($this->once())->method('getObject')->will($this->returnValue(new AdminControllerHelper_Foo()));
  132. $admin->expects($this->once())->method('toString')->will($this->returnValue('bar'));
  133. $admin->expects($this->once())->method('generateObjectUrl')->will($this->returnCallback(function($type, $object, $parameters = array()) {
  134. if ($type != 'edit') {
  135. return 'invalid name';
  136. }
  137. return '/ok/url';
  138. }));
  139. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  140. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  141. $twig = $this->getMock('Twig_Environment');
  142. $twig->expects($this->once())->method('render')
  143. ->with($mockTemplate)
  144. ->will($this->returnCallback(function ($templateName, $templateParams) {
  145. return sprintf('<a href="%s" target="new">%s</a>', $templateParams['admin']->generateObjectUrl('edit', $templateParams['object']), $templateParams['description']);
  146. }));
  147. $request = new Request(array(
  148. 'code' => 'sonata.post.admin',
  149. 'objectId' => 42,
  150. 'uniqid' => 'asdasd123',
  151. '_format' => 'html'
  152. ));
  153. $pool = new Pool($container, 'title', 'logo');
  154. $pool->setAdminServiceIds(array('sonata.post.admin'));
  155. $helper = new AdminHelper($pool);
  156. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  157. $controller = new HelperController($twig, $pool, $helper, $validator);
  158. $response = $controller->getShortObjectDescriptionAction($request);
  159. $expected = '<a href="/ok/url" target="new">bar</a>';
  160. $this->assertEquals($expected, $response->getContent());
  161. }
  162. public function testsetObjectFieldValueAction()
  163. {
  164. $object = new AdminControllerHelper_Foo();
  165. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  166. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
  167. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  168. $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
  169. $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
  170. $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
  171. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  172. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  173. $adminExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  174. $adminExtension->expects($this->once())->method('getName')->will($this->returnValue('sonata_admin'));
  175. $adminExtension->expects($this->once())->method('renderListElement')->will($this->returnValue('<foo />'));
  176. $twig = new Twig();
  177. $twig->addExtension($adminExtension);
  178. $request = new Request(array(
  179. 'code' => 'sonata.post.admin',
  180. 'objectId' => 42,
  181. 'field' => 'enabled',
  182. 'value' => 1,
  183. 'context' => 'list',
  184. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  185. $pool = new Pool($container, 'title', 'logo');
  186. $pool->setAdminServiceIds(array('sonata.post.admin'));
  187. $helper = new AdminHelper($pool);
  188. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  189. $controller = new HelperController($twig, $pool, $helper, $validator);
  190. $response = $controller->setObjectFieldValueAction($request);
  191. $this->assertEquals('{"status":"OK","content":"\u003Cfoo \/\u003E"}', $response->getContent() );
  192. }
  193. public function testappendFormFieldElementAction()
  194. {
  195. $object = new AdminControllerHelper_Foo();
  196. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  197. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  198. $mockTheme = $this->getMockBuilder('Symfony\Component\Form\FormView')
  199. ->disableOriginalConstructor()
  200. ->getMock();
  201. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  202. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  203. $admin->expects($this->once())->method('setRequest');
  204. $admin->expects($this->once())->method('setSubject');
  205. $admin->expects($this->once())->method('getFormTheme')->will($this->returnValue($mockTheme));
  206. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  207. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  208. $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
  209. ->disableOriginalConstructor()
  210. ->getMock();
  211. $mockRenderer->expects($this->once())
  212. ->method('searchAndRenderBlock')
  213. ->will($this->returnValue(new Response()));
  214. $formExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  215. $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
  216. $formExtension->expects($this->never())->method('searchAndRenderBlock');
  217. $formExtension->expects($this->never())->method('setTheme');
  218. $formExtension->renderer = $mockRenderer;
  219. $twig = new Twig();
  220. $twig->addExtension($formExtension);
  221. $request = new Request(array(
  222. 'code' => 'sonata.post.admin',
  223. 'objectId' => 42,
  224. 'field' => 'enabled',
  225. 'value' => 1,
  226. 'context' => 'list',
  227. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  228. $pool = new Pool($container, 'title', 'logo');
  229. $pool->setAdminServiceIds(array('sonata.post.admin'));
  230. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  231. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  232. ->disableOriginalConstructor()
  233. ->getMock();
  234. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  235. ->disableOriginalConstructor()
  236. ->getMock();
  237. $mockForm->expects($this->once())
  238. ->method('createView')
  239. ->will($this->returnValue($mockView));
  240. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('appendFormFieldElement', 'getChildFormView'), array($pool));
  241. $helper->expects($this->once())->method('appendFormFieldElement')->will($this->returnValue(array(
  242. $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface'),
  243. $mockForm
  244. )));
  245. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  246. $controller = new HelperController($twig, $pool, $helper, $validator);
  247. $response = $controller->appendFormFieldElementAction($request);
  248. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  249. }
  250. public function testretrieveFormFieldElementAction()
  251. {
  252. $object = new AdminControllerHelper_Foo();
  253. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  254. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  255. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  256. ->disableOriginalConstructor()
  257. ->getMock();
  258. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  259. ->disableOriginalConstructor()
  260. ->getMock();
  261. $mockForm->expects($this->once())
  262. ->method('createView')
  263. ->will($this->returnValue($mockView));
  264. $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  265. ->disableOriginalConstructor()
  266. ->getMock();
  267. $formBuilder->expects($this->once())->method('getForm')->will($this->returnValue($mockForm));
  268. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  269. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  270. $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  271. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  272. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  273. $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
  274. ->disableOriginalConstructor()
  275. ->getMock();
  276. $mockRenderer->expects($this->once())
  277. ->method('searchAndRenderBlock')
  278. ->will($this->returnValue(new Response()));
  279. $formExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  280. $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
  281. $formExtension->expects($this->never())->method('searchAndRenderBlock');
  282. $formExtension->expects($this->never())->method('setTheme');
  283. $formExtension->renderer = $mockRenderer;
  284. $twig = new Twig();
  285. $twig->addExtension($formExtension);
  286. $request = new Request(array(
  287. 'code' => 'sonata.post.admin',
  288. 'objectId' => 42,
  289. 'field' => 'enabled',
  290. 'value' => 1,
  291. 'context' => 'list',
  292. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  293. $pool = new Pool($container, 'title', 'logo');
  294. $pool->setAdminServiceIds(array('sonata.post.admin'));
  295. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  296. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  297. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('getChildFormView'), array($pool));
  298. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  299. $controller = new HelperController($twig, $pool, $helper, $validator);
  300. $response = $controller->retrieveFormFieldElementAction($request);
  301. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  302. }
  303. public function testSetObjectFieldValueActionWithViolations()
  304. {
  305. $bar = new AdminControllerHelper_Bar();
  306. $object = new AdminControllerHelper_Foo();
  307. $object->setBar($bar);
  308. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  309. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
  310. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  311. $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
  312. $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
  313. $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
  314. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  315. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  316. $twig = new Twig();
  317. $request = new Request(array(
  318. 'code' => 'sonata.post.admin',
  319. 'objectId' => 42,
  320. 'field' => 'bar.enabled',
  321. 'value' => 1,
  322. 'context' => 'list',
  323. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  324. $pool = new Pool($container, 'title', 'logo');
  325. $pool->setAdminServiceIds(array('sonata.post.admin'));
  326. $helper = new AdminHelper($pool);
  327. $violations = new ConstraintViolationList(array(
  328. new ConstraintViolation('error1', null, array(), null, 'enabled', null),
  329. new ConstraintViolation('error2', null, array(), null, 'enabled', null),
  330. ));
  331. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  332. $validator
  333. ->expects($this->once())
  334. ->method('validateProperty')
  335. ->with($bar, 'enabled')
  336. ->will($this->returnValue($violations))
  337. ;
  338. $controller = new HelperController($twig, $pool, $helper, $validator);
  339. $response = $controller->setObjectFieldValueAction($request);
  340. $this->assertEquals('{"status":"KO","message":"error1\nerror2"}', $response->getContent() );
  341. }
  342. }