HelperControllerTest.php 18 KB

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