HelperControllerTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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\AdminInterface;
  13. use Sonata\AdminBundle\Admin\Pool;
  14. use Sonata\AdminBundle\Controller\HelperController;
  15. use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Validator\ConstraintViolation;
  19. use Symfony\Component\Validator\ConstraintViolationList;
  20. class AdminControllerHelper_Foo
  21. {
  22. private $bar;
  23. public function getAdminTitle()
  24. {
  25. return 'foo';
  26. }
  27. public function setEnabled($value)
  28. {
  29. }
  30. public function setBar(AdminControllerHelper_Bar $bar)
  31. {
  32. $this->bar = $bar;
  33. }
  34. public function getBar()
  35. {
  36. return $this->bar;
  37. }
  38. }
  39. class AdminControllerHelper_Bar
  40. {
  41. public function getAdminTitle()
  42. {
  43. return 'bar';
  44. }
  45. public function setEnabled($value)
  46. {
  47. }
  48. public function getEnabled()
  49. {
  50. }
  51. }
  52. class HelperControllerTest extends \PHPUnit_Framework_TestCase
  53. {
  54. /**
  55. * @var AdminInterface
  56. */
  57. private $admin;
  58. /**
  59. * @var HelperController
  60. */
  61. private $controller;
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function setUp()
  66. {
  67. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  68. $pool = new Pool($container, 'title', 'logo.png');
  69. $pool->setAdminServiceIds(array('foo.admin'));
  70. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  71. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  72. $helper = new AdminHelper($pool);
  73. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  74. $this->controller = new HelperController($twig, $pool, $helper, $validator);
  75. // php 5.3 BC
  76. $admin = $this->admin;
  77. $container->expects($this->any())
  78. ->method('get')
  79. ->will($this->returnCallback(function ($id) use ($admin) {
  80. switch ($id) {
  81. case 'foo.admin':
  82. return $admin;
  83. }
  84. return;
  85. }));
  86. return;
  87. }
  88. /**
  89. * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  90. */
  91. public function testgetShortObjectDescriptionActionInvalidAdmin()
  92. {
  93. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  94. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  95. $request = new Request(array(
  96. 'code' => 'sonata.post.admin',
  97. 'objectId' => 42,
  98. 'uniqid' => 'asdasd123',
  99. ));
  100. $pool = new Pool($container, 'title', 'logo');
  101. $pool->setAdminServiceIds(array('sonata.post.admin'));
  102. $helper = new AdminHelper($pool);
  103. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  104. $controller = new HelperController($twig, $pool, $helper, $validator);
  105. $controller->getShortObjectDescriptionAction($request);
  106. }
  107. /**
  108. * @expectedException \RuntimeException
  109. * @exceptionMessage Invalid format
  110. */
  111. public function testgetShortObjectDescriptionActionObjectDoesNotExist()
  112. {
  113. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  114. $admin->expects($this->once())->method('setUniqid');
  115. $admin->expects($this->once())->method('getObject')->will($this->returnValue(false));
  116. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  117. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  118. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  119. $request = new Request(array(
  120. 'code' => 'sonata.post.admin',
  121. 'objectId' => 42,
  122. 'uniqid' => 'asdasd123',
  123. ));
  124. $pool = new Pool($container, 'title', 'logo');
  125. $pool->setAdminServiceIds(array('sonata.post.admin'));
  126. $helper = new AdminHelper($pool);
  127. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  128. $controller = new HelperController($twig, $pool, $helper, $validator);
  129. $controller->getShortObjectDescriptionAction($request);
  130. }
  131. public function testgetShortObjectDescriptionActionEmptyObjectId()
  132. {
  133. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  134. $admin->expects($this->once())->method('setUniqid');
  135. $admin->expects($this->once())->method('getObject')->with($this->identicalTo(null))->will($this->returnValue(false));
  136. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  137. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  138. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  139. $request = new Request(array(
  140. 'code' => 'sonata.post.admin',
  141. 'objectId' => '',
  142. 'uniqid' => 'asdasd123',
  143. '_format' => 'html',
  144. ));
  145. $pool = new Pool($container, 'title', 'logo');
  146. $pool->setAdminServiceIds(array('sonata.post.admin'));
  147. $helper = new AdminHelper($pool);
  148. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  149. $controller = new HelperController($twig, $pool, $helper, $validator);
  150. $controller->getShortObjectDescriptionAction($request);
  151. }
  152. public function testgetShortObjectDescriptionActionObject()
  153. {
  154. $mockTemplate = 'AdminHelperTest:mock-short-object-description.html.twig';
  155. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  156. $admin->expects($this->once())->method('setUniqid');
  157. $admin->expects($this->once())->method('getTemplate')->will($this->returnValue($mockTemplate));
  158. $admin->expects($this->once())->method('getObject')->will($this->returnValue(new AdminControllerHelper_Foo()));
  159. $admin->expects($this->once())->method('toString')->will($this->returnValue('bar'));
  160. $admin->expects($this->once())->method('generateObjectUrl')->will($this->returnCallback(function ($type, $object, $parameters = array()) {
  161. if ($type != 'edit') {
  162. return 'invalid name';
  163. }
  164. return '/ok/url';
  165. }));
  166. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  167. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  168. $twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
  169. $twig->expects($this->once())->method('render')
  170. ->with($mockTemplate)
  171. ->will($this->returnCallback(function ($templateName, $templateParams) {
  172. return sprintf('<a href="%s" target="new">%s</a>', $templateParams['admin']->generateObjectUrl('edit', $templateParams['object']), $templateParams['description']);
  173. }));
  174. $request = new Request(array(
  175. 'code' => 'sonata.post.admin',
  176. 'objectId' => 42,
  177. 'uniqid' => 'asdasd123',
  178. '_format' => 'html',
  179. ));
  180. $pool = new Pool($container, 'title', 'logo');
  181. $pool->setAdminServiceIds(array('sonata.post.admin'));
  182. $helper = new AdminHelper($pool);
  183. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  184. $controller = new HelperController($twig, $pool, $helper, $validator);
  185. $response = $controller->getShortObjectDescriptionAction($request);
  186. $expected = '<a href="/ok/url" target="new">bar</a>';
  187. $this->assertSame($expected, $response->getContent());
  188. }
  189. public function testsetObjectFieldValueAction()
  190. {
  191. $object = new AdminControllerHelper_Foo();
  192. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  193. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
  194. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  195. $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
  196. $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
  197. $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
  198. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  199. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  200. $adminExtension = $this->getMock('\Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  201. $adminExtension->expects($this->once())->method('getName')->will($this->returnValue('sonata_admin'));
  202. $adminExtension->expects($this->once())->method('renderListElement')->will($this->returnValue('<foo />'));
  203. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  204. $twig->addExtension($adminExtension);
  205. $request = new Request(array(
  206. 'code' => 'sonata.post.admin',
  207. 'objectId' => 42,
  208. 'field' => 'enabled',
  209. 'value' => 1,
  210. 'context' => 'list',
  211. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  212. $pool = new Pool($container, 'title', 'logo');
  213. $pool->setAdminServiceIds(array('sonata.post.admin'));
  214. $helper = new AdminHelper($pool);
  215. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  216. $controller = new HelperController($twig, $pool, $helper, $validator);
  217. $response = $controller->setObjectFieldValueAction($request);
  218. $this->assertSame('{"status":"OK","content":"\u003Cfoo \/\u003E"}', $response->getContent());
  219. }
  220. public function testappendFormFieldElementAction()
  221. {
  222. $object = new AdminControllerHelper_Foo();
  223. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  224. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  225. $mockTheme = $this->getMockBuilder('Symfony\Component\Form\FormView')
  226. ->disableOriginalConstructor()
  227. ->getMock();
  228. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  229. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  230. $admin->expects($this->once())->method('setRequest');
  231. $admin->expects($this->once())->method('setSubject');
  232. $admin->expects($this->once())->method('getFormTheme')->will($this->returnValue($mockTheme));
  233. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  234. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  235. $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
  236. ->disableOriginalConstructor()
  237. ->getMock();
  238. $mockRenderer->expects($this->once())
  239. ->method('searchAndRenderBlock')
  240. ->will($this->returnValue(new Response()));
  241. $formExtension = $this->getMock('\Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  242. $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
  243. $formExtension->expects($this->never())->method('searchAndRenderBlock');
  244. $formExtension->expects($this->never())->method('setTheme');
  245. $formExtension->renderer = $mockRenderer;
  246. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  247. $twig->addExtension($formExtension);
  248. $request = new Request(array(
  249. 'code' => 'sonata.post.admin',
  250. 'objectId' => 42,
  251. 'field' => 'enabled',
  252. 'value' => 1,
  253. 'context' => 'list',
  254. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  255. $pool = new Pool($container, 'title', 'logo');
  256. $pool->setAdminServiceIds(array('sonata.post.admin'));
  257. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  258. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  259. ->disableOriginalConstructor()
  260. ->getMock();
  261. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  262. ->disableOriginalConstructor()
  263. ->getMock();
  264. $mockForm->expects($this->once())
  265. ->method('createView')
  266. ->will($this->returnValue($mockView));
  267. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('appendFormFieldElement', 'getChildFormView'), array($pool));
  268. $helper->expects($this->once())->method('appendFormFieldElement')->will($this->returnValue(array(
  269. $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface'),
  270. $mockForm,
  271. )));
  272. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  273. $controller = new HelperController($twig, $pool, $helper, $validator);
  274. $response = $controller->appendFormFieldElementAction($request);
  275. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  276. }
  277. public function testretrieveFormFieldElementAction()
  278. {
  279. $object = new AdminControllerHelper_Foo();
  280. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  281. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  282. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  283. ->disableOriginalConstructor()
  284. ->getMock();
  285. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  286. ->disableOriginalConstructor()
  287. ->getMock();
  288. $mockForm->expects($this->once())
  289. ->method('createView')
  290. ->will($this->returnValue($mockView));
  291. $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  292. ->disableOriginalConstructor()
  293. ->getMock();
  294. $formBuilder->expects($this->once())->method('getForm')->will($this->returnValue($mockForm));
  295. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  296. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  297. $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  298. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  299. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  300. $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
  301. ->disableOriginalConstructor()
  302. ->getMock();
  303. $mockRenderer->expects($this->once())
  304. ->method('searchAndRenderBlock')
  305. ->will($this->returnValue(new Response()));
  306. $formExtension = $this->getMock('\Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  307. $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
  308. $formExtension->expects($this->never())->method('searchAndRenderBlock');
  309. $formExtension->expects($this->never())->method('setTheme');
  310. $formExtension->renderer = $mockRenderer;
  311. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  312. $twig->addExtension($formExtension);
  313. $request = new Request(array(
  314. 'code' => 'sonata.post.admin',
  315. 'objectId' => 42,
  316. 'field' => 'enabled',
  317. 'value' => 1,
  318. 'context' => 'list',
  319. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  320. $pool = new Pool($container, 'title', 'logo');
  321. $pool->setAdminServiceIds(array('sonata.post.admin'));
  322. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  323. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  324. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('getChildFormView'), array($pool));
  325. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  326. $controller = new HelperController($twig, $pool, $helper, $validator);
  327. $response = $controller->retrieveFormFieldElementAction($request);
  328. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  329. }
  330. public function testSetObjectFieldValueActionWithViolations()
  331. {
  332. $bar = new AdminControllerHelper_Bar();
  333. $object = new AdminControllerHelper_Foo();
  334. $object->setBar($bar);
  335. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  336. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
  337. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  338. $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
  339. $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
  340. $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
  341. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  342. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  343. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  344. $request = new Request(array(
  345. 'code' => 'sonata.post.admin',
  346. 'objectId' => 42,
  347. 'field' => 'bar.enabled',
  348. 'value' => 1,
  349. 'context' => 'list',
  350. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  351. $pool = new Pool($container, 'title', 'logo');
  352. $pool->setAdminServiceIds(array('sonata.post.admin'));
  353. $helper = new AdminHelper($pool);
  354. $violations = new ConstraintViolationList(array(
  355. new ConstraintViolation('error1', null, array(), null, 'enabled', null),
  356. new ConstraintViolation('error2', null, array(), null, 'enabled', null),
  357. ));
  358. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  359. $validator
  360. ->expects($this->once())
  361. ->method('validate')
  362. ->with($bar)
  363. ->will($this->returnValue($violations))
  364. ;
  365. $controller = new HelperController($twig, $pool, $helper, $validator);
  366. $response = $controller->setObjectFieldValueAction($request);
  367. $this->assertSame('{"status":"KO","message":"error1\nerror2"}', $response->getContent());
  368. }
  369. /**
  370. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  371. * @exceptionMessage Invalid format
  372. */
  373. public function testRetrieveAutocompleteItemsActionNotGranted()
  374. {
  375. $this->admin->expects($this->exactly(2))
  376. ->method('isGranted')
  377. ->will($this->returnCallback(function ($operation) {
  378. if ($operation == 'CREATE' || $operation == 'EDIT') {
  379. return false;
  380. }
  381. return;
  382. }));
  383. $request = new Request(array(
  384. 'admin_code' => 'foo.admin',
  385. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  386. $this->controller->retrieveAutocompleteItemsAction($request);
  387. }
  388. /**
  389. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  390. * @exceptionMessage Invalid format
  391. */
  392. public function testRetrieveFilterAutocompleteItemsActionNotGranted()
  393. {
  394. $this->admin->expects($this->exactly(1))
  395. ->method('isGranted')
  396. ->will($this->returnCallback(function ($operation) {
  397. if ($operation == 'LIST') {
  398. return false;
  399. }
  400. return;
  401. }));
  402. $request = new Request(array(
  403. 'admin_code' => 'foo.admin',
  404. '_context' => 'filter',
  405. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  406. $this->controller->retrieveAutocompleteItemsAction($request);
  407. }
  408. /**
  409. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  410. * @exceptionMessage Autocomplete list can`t be retrieved because the form element is disabled or read_only.
  411. */
  412. public function testRetrieveAutocompleteItemsActionDisabledFormelememt()
  413. {
  414. $this->admin->expects($this->once())
  415. ->method('isGranted')
  416. ->with('CREATE')
  417. ->will($this->returnValue(true));
  418. $entity = new Foo();
  419. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  420. $fieldDescription->expects($this->once())
  421. ->method('getTargetEntity')
  422. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
  423. $fieldDescription->expects($this->once())
  424. ->method('getName')
  425. ->will($this->returnValue('barField'));
  426. $this->admin->expects($this->once())
  427. ->method('getFormFieldDescriptions')
  428. ->will($this->returnValue(null));
  429. $this->admin->expects($this->once())
  430. ->method('getFormFieldDescription')
  431. ->with('barField')
  432. ->will($this->returnValue($fieldDescription));
  433. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  434. ->disableOriginalConstructor()
  435. ->getMock();
  436. $this->admin->expects($this->once())
  437. ->method('getForm')
  438. ->will($this->returnValue($form));
  439. $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
  440. ->disableOriginalConstructor()
  441. ->getMock();
  442. $form->expects($this->once())
  443. ->method('get')
  444. ->with('barField')
  445. ->will($this->returnValue($formType));
  446. $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
  447. ->disableOriginalConstructor()
  448. ->getMock();
  449. $formType->expects($this->once())
  450. ->method('getConfig')
  451. ->will($this->returnValue($formConfig));
  452. $formConfig->expects($this->once())
  453. ->method('getAttribute')
  454. ->with('disabled')
  455. ->will($this->returnValue(true));
  456. $request = new Request(array(
  457. 'admin_code' => 'foo.admin',
  458. 'field' => 'barField',
  459. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  460. $this->controller->retrieveAutocompleteItemsAction($request);
  461. }
  462. /**
  463. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  464. */
  465. public function testRetrieveAutocompleteItemsActionNotGrantedTarget()
  466. {
  467. $this->admin->expects($this->once())
  468. ->method('isGranted')
  469. ->with('CREATE')
  470. ->will($this->returnValue(true));
  471. $entity = new Foo();
  472. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  473. $fieldDescription->expects($this->once())
  474. ->method('getTargetEntity')
  475. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
  476. $fieldDescription->expects($this->once())
  477. ->method('getName')
  478. ->will($this->returnValue('barField'));
  479. $targetAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  480. $fieldDescription->expects($this->once())
  481. ->method('getAssociationAdmin')
  482. ->will($this->returnValue($targetAdmin));
  483. $targetAdmin->expects($this->once())
  484. ->method('isGranted')
  485. ->with('LIST')
  486. ->will($this->returnValue(false));
  487. $this->admin->expects($this->once())
  488. ->method('getFormFieldDescriptions')
  489. ->will($this->returnValue(null));
  490. $this->admin->expects($this->once())
  491. ->method('getFormFieldDescription')
  492. ->with('barField')
  493. ->will($this->returnValue($fieldDescription));
  494. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  495. ->disableOriginalConstructor()
  496. ->getMock();
  497. $this->admin->expects($this->once())
  498. ->method('getForm')
  499. ->will($this->returnValue($form));
  500. $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
  501. ->disableOriginalConstructor()
  502. ->getMock();
  503. $form->expects($this->once())
  504. ->method('get')
  505. ->with('barField')
  506. ->will($this->returnValue($formType));
  507. $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
  508. ->disableOriginalConstructor()
  509. ->getMock();
  510. $formType->expects($this->any())
  511. ->method('getConfig')
  512. ->will($this->returnValue($formConfig));
  513. $formConfig->expects($this->any())
  514. ->method('getAttribute')
  515. ->will($this->returnCallback(function ($name) {
  516. switch ($name) {
  517. case 'disabled':
  518. return false;
  519. case 'property':
  520. return 'fooProperty';
  521. case 'callback':
  522. return;
  523. case 'minimum_input_length':
  524. return 3;
  525. case 'items_per_page':
  526. return 10;
  527. case 'req_param_name_page_number':
  528. return '_page';
  529. case 'to_string_callback':
  530. return;
  531. }
  532. return;
  533. }));
  534. $request = new Request(array(
  535. 'admin_code' => 'foo.admin',
  536. 'field' => 'barField',
  537. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  538. $this->controller->retrieveAutocompleteItemsAction($request);
  539. }
  540. }