HelperControllerTest.php 17 KB

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