HelperControllerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 \Twig_Environment as Twig;
  15. use \Twig_ExtensionInterface as Twig_ExtensionInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. use Sonata\AdminBundle\Admin\AdminInterface;
  19. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  20. use Symfony\Component\Form\Form;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Form\FormBuilder;
  24. use Symfony\Component\Form\FormView;
  25. class AdminControllerHelper_Foo
  26. {
  27. public function getAdminTitle()
  28. {
  29. return 'bar';
  30. }
  31. public function setEnabled($value)
  32. {
  33. }
  34. }
  35. class HelperControllerTest extends \PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  39. */
  40. public function testgetShortObjectDescriptionActionInvalidAdmin()
  41. {
  42. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  43. $twig = new Twig;
  44. $request = new Request(array(
  45. 'code' => 'sonata.post.admin',
  46. 'objectId' => 42,
  47. 'uniqid' => 'asdasd123'
  48. ));
  49. $pool = new Pool($container, 'title', 'logo');
  50. $helper = new AdminHelper($pool);
  51. $controller = new HelperController($twig, $pool, $helper);
  52. $controller->getShortObjectDescriptionAction($request);
  53. }
  54. public function testgetShortObjectDescriptionActionObjectDoesNotExist()
  55. {
  56. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  57. $admin->expects($this->once())->method('setUniqid');
  58. $admin->expects($this->once())->method('getObject')->will($this->returnValue(false));
  59. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  60. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  61. $twig = new Twig;
  62. $request = new Request(array(
  63. 'code' => 'sonata.post.admin',
  64. 'objectId' => 42,
  65. 'uniqid' => 'asdasd123'
  66. ));
  67. $pool = new Pool($container, 'title', 'logo');
  68. $helper = new AdminHelper($pool);
  69. $controller = new HelperController($twig, $pool, $helper);
  70. $response = $controller->getShortObjectDescriptionAction($request);
  71. $this->assertEmpty($response->getContent());
  72. }
  73. public function testgetShortObjectDescriptionActionObject()
  74. {
  75. $mockTemplate = 'AdminHelperTest:mock-short-object-description.html.twig';
  76. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  77. $admin->expects($this->once())->method('setUniqid');
  78. $admin->expects($this->once())->method('getTemplate')->will($this->returnValue($mockTemplate));
  79. $admin->expects($this->once())->method('getObject')->will($this->returnValue(new AdminControllerHelper_Foo));
  80. $admin->expects($this->once())->method('generateObjectUrl')->will($this->returnCallback(function($type, $object, $parameters = array()) {
  81. if ($type != 'edit') {
  82. return 'invalid name';
  83. }
  84. return '/ok/url';
  85. }));
  86. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  87. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  88. $twig = $this->getMock('Twig_Environment');
  89. $twig->expects($this->once())->method('render')
  90. ->with($mockTemplate)
  91. ->will($this->returnCallback(function($templateName, $templateParams) {
  92. return sprintf('<a href="%s" target="new">%s</a>', $templateParams['admin']->generateObjectUrl('edit', $templateParams['object']), $templateParams['description']);
  93. }));
  94. $request = new Request(array(
  95. 'code' => 'sonata.post.admin',
  96. 'objectId' => 42,
  97. 'uniqid' => 'asdasd123'
  98. ));
  99. $pool = new Pool($container, 'title', 'logo');
  100. $helper = new AdminHelper($pool);
  101. $controller = new HelperController($twig, $pool, $helper);
  102. $response = $controller->getShortObjectDescriptionAction($request);
  103. $expected = '<a href="/ok/url" target="new">bar</a>';
  104. $this->assertEquals($expected, $response->getContent());
  105. }
  106. public function testsetObjectFieldValueAction()
  107. {
  108. $object = new AdminControllerHelper_Foo;
  109. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  110. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
  111. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  112. $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
  113. $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
  114. $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
  115. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  116. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  117. $adminExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  118. $adminExtension->expects($this->once())->method('getName')->will($this->returnValue('sonata_admin'));
  119. $adminExtension->expects($this->once())->method('renderListElement')->will($this->returnValue('<foo />'));
  120. $twig = new Twig;
  121. $twig->addExtension($adminExtension);
  122. $request = new Request(array(
  123. 'code' => 'sonata.post.admin',
  124. 'objectId' => 42,
  125. 'field' => 'enabled',
  126. 'value' => 1,
  127. 'context' => 'list',
  128. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  129. $pool = new Pool($container, 'title', 'logo');
  130. $helper = new AdminHelper($pool);
  131. $controller = new HelperController($twig, $pool, $helper);
  132. $response = $controller->setObjectFieldValueAction($request);
  133. $this->assertEquals('{"status":"OK","content":"\u003Cfoo \/\u003E"}', $response->getContent() );
  134. }
  135. public function testappendFormFieldElementAction()
  136. {
  137. $object = new AdminControllerHelper_Foo;
  138. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  139. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  140. $mockTheme = $this->getMockBuilder('Symfony\Component\Form\FormView')
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  144. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  145. $admin->expects($this->once())->method('setRequest');
  146. $admin->expects($this->once())->method('setSubject');
  147. $admin->expects($this->once())->method('getFormTheme')->will($this->returnValue($mockTheme));
  148. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  149. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  150. $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. $mockRenderer->expects($this->once())
  154. ->method('searchAndRenderBlock')
  155. ->will($this->returnValue(new Response));
  156. $formExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  157. $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
  158. $formExtension->expects($this->never())->method('searchAndRenderBlock');
  159. $formExtension->expects($this->never())->method('setTheme');
  160. $formExtension->renderer = $mockRenderer;
  161. $twig = new Twig;
  162. $twig->addExtension($formExtension);
  163. $request = new Request(array(
  164. 'code' => 'sonata.post.admin',
  165. 'objectId' => 42,
  166. 'field' => 'enabled',
  167. 'value' => 1,
  168. 'context' => 'list',
  169. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  170. $pool = new Pool($container, 'title', 'logo');
  171. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  172. ->disableOriginalConstructor()
  173. ->getMock();
  174. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  175. ->disableOriginalConstructor()
  176. ->getMock();
  177. $mockForm->expects($this->once())
  178. ->method('createView')
  179. ->will($this->returnValue($mockView));
  180. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('appendFormFieldElement', 'getChildFormView'), array($pool));
  181. $helper->expects($this->once())->method('appendFormFieldElement')->will($this->returnValue(array(
  182. $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface'),
  183. $mockForm
  184. )));
  185. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  186. $controller = new HelperController($twig, $pool, $helper);
  187. $response = $controller->appendFormFieldElementAction($request);
  188. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  189. }
  190. public function testretrieveFormFieldElementAction()
  191. {
  192. $object = new AdminControllerHelper_Foo;
  193. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  194. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  195. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  196. ->disableOriginalConstructor()
  197. ->getMock();
  198. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  199. ->disableOriginalConstructor()
  200. ->getMock();
  201. $mockForm->expects($this->once())
  202. ->method('createView')
  203. ->will($this->returnValue($mockView));
  204. $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  205. ->disableOriginalConstructor()
  206. ->getMock();
  207. $formBuilder->expects($this->once())->method('getForm')->will($this->returnValue($mockForm));
  208. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  209. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  210. $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  211. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  212. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  213. $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
  214. ->disableOriginalConstructor()
  215. ->getMock();
  216. $mockRenderer->expects($this->once())
  217. ->method('searchAndRenderBlock')
  218. ->will($this->returnValue(new Response));
  219. $formExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  220. $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
  221. $formExtension->expects($this->never())->method('searchAndRenderBlock');
  222. $formExtension->expects($this->never())->method('setTheme');
  223. $formExtension->renderer = $mockRenderer;
  224. $twig = new Twig;
  225. $twig->addExtension($formExtension);
  226. $request = new Request(array(
  227. 'code' => 'sonata.post.admin',
  228. 'objectId' => 42,
  229. 'field' => 'enabled',
  230. 'value' => 1,
  231. 'context' => 'list',
  232. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  233. $pool = new Pool($container, 'title', 'logo');
  234. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  235. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('getChildFormView'), array($pool));
  236. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  237. $controller = new HelperController($twig, $pool, $helper);
  238. $response = $controller->retrieveFormFieldElementAction($request);
  239. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  240. }
  241. }