HelperControllerTest.php 13 KB

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