HelperControllerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 AdminControllerHelperTest 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, $request, $pool, $helper);
  54. $controller->getShortObjectDescriptionAction();
  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, $request, $pool, $helper);
  72. $response = $controller->getShortObjectDescriptionAction();
  73. $this->assertEmpty($response->getContent());
  74. }
  75. public function testgetShortObjectDescriptionActionObject()
  76. {
  77. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  78. $admin->expects($this->once())->method('setUniqid');
  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 = new Twig;
  92. $request = new Request(array(
  93. 'code' => 'sonata.post.admin',
  94. 'objectId' => 42,
  95. 'uniqid' => 'asdasd123'
  96. ));
  97. $pool = new Pool($container, 'title', 'logo');
  98. $helper = new AdminHelper($pool);
  99. $controller = new HelperController($twig, $request, $pool, $helper);
  100. $response = $controller->getShortObjectDescriptionAction();
  101. $expected = '<a href="/ok/url" target="new">bar</a>';
  102. $this->assertEquals($expected, $response->getContent());
  103. }
  104. public function testsetObjectFieldValueAction()
  105. {
  106. $object = new AdminControllerHelper_Foo;
  107. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  108. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
  109. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  110. $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
  111. $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
  112. $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
  113. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  114. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  115. $adminExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  116. $adminExtension->expects($this->once())->method('getName')->will($this->returnValue('sonata_admin'));
  117. $adminExtension->expects($this->once())->method('renderListElement')->will($this->returnValue('<foo />'));
  118. $twig = new Twig;
  119. $twig->addExtension($adminExtension);
  120. $request = new Request(array(
  121. 'code' => 'sonata.post.admin',
  122. 'objectId' => 42,
  123. 'field' => 'enabled',
  124. 'value' => 1,
  125. 'context' => 'list',
  126. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  127. $pool = new Pool($container, 'title', 'logo');
  128. $helper = new AdminHelper($pool);
  129. $controller = new HelperController($twig, $request, $pool, $helper);
  130. $response = $controller->setObjectFieldValueAction();
  131. $this->assertEquals('{"status":"OK","content":"<foo \/>"}', $response->getContent() );
  132. }
  133. public function testappendFormFieldElementAction()
  134. {
  135. $object = new AdminControllerHelper_Foo;
  136. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  137. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  138. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  139. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  140. $admin->expects($this->once())->method('setRequest');
  141. $admin->expects($this->once())->method('setSubject');
  142. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  143. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  144. $formExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName', 'setTheme', 'renderWidget'));
  145. $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
  146. $formExtension->expects($this->once())->method('renderWidget')->will($this->returnValue(new Response));
  147. $formExtension->expects($this->once())->method('setTheme');
  148. $twig = new Twig;
  149. $twig->addExtension($formExtension);
  150. $request = new Request(array(
  151. 'code' => 'sonata.post.admin',
  152. 'objectId' => 42,
  153. 'field' => 'enabled',
  154. 'value' => 1,
  155. 'context' => 'list',
  156. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  157. $pool = new Pool($container, 'title', 'logo');
  158. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  159. $form = new Form('foo', $dispatcher);
  160. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('appendFormFieldElement'), array($pool));
  161. $helper->expects($this->once())->method('appendFormFieldElement')->will($this->returnValue(array(
  162. $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface'),
  163. $form
  164. )));
  165. $controller = new HelperController($twig, $request, $pool, $helper);
  166. $response = $controller->appendFormFieldElementAction();
  167. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  168. }
  169. public function testretrieveFormFieldElementAction()
  170. {
  171. $object = new AdminControllerHelper_Foo;
  172. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  173. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  174. $formBuilder = new FormBuilder(
  175. 'foo',
  176. $this->getMock('Symfony\Component\Form\FormFactoryInterface'),
  177. $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
  178. );
  179. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  180. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  181. $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  182. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  183. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  184. $formExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName', 'setTheme', 'renderWidget'));
  185. $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
  186. $formExtension->expects($this->once())->method('renderWidget')->will($this->returnValue(new Response));
  187. $formExtension->expects($this->once())->method('setTheme');
  188. $twig = new Twig;
  189. $twig->addExtension($formExtension);
  190. $request = new Request(array(
  191. 'code' => 'sonata.post.admin',
  192. 'objectId' => 42,
  193. 'field' => 'enabled',
  194. 'value' => 1,
  195. 'context' => 'list',
  196. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  197. $pool = new Pool($container, 'title', 'logo');
  198. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  199. $formView = new FormView();
  200. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('getChildFormView'), array($pool));
  201. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($formView));
  202. $controller = new HelperController($twig, $request, $pool, $helper);
  203. $response = $controller->retrieveFormFieldElementAction();
  204. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  205. }
  206. }