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