HelperControllerTest.php 27 KB

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