AdminHelperTest.php 12 KB

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