HelperControllerTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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\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->assertSame($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->assertSame('{"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->renderer = $mockRenderer;
  247. $twig = new Twig();
  248. $twig->addExtension($formExtension);
  249. $request = new Request(array(
  250. 'code' => 'sonata.post.admin',
  251. 'objectId' => 42,
  252. 'field' => 'enabled',
  253. 'value' => 1,
  254. 'context' => 'list',
  255. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  256. $pool = new Pool($container, 'title', 'logo');
  257. $pool->setAdminServiceIds(array('sonata.post.admin'));
  258. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  259. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  260. ->disableOriginalConstructor()
  261. ->getMock();
  262. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  263. ->disableOriginalConstructor()
  264. ->getMock();
  265. $mockForm->expects($this->once())
  266. ->method('createView')
  267. ->will($this->returnValue($mockView));
  268. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('appendFormFieldElement', 'getChildFormView'), array($pool));
  269. $helper->expects($this->once())->method('appendFormFieldElement')->will($this->returnValue(array(
  270. $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface'),
  271. $mockForm,
  272. )));
  273. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  274. $controller = new HelperController($twig, $pool, $helper, $validator);
  275. $response = $controller->appendFormFieldElementAction($request);
  276. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  277. }
  278. public function testretrieveFormFieldElementAction()
  279. {
  280. $object = new AdminControllerHelper_Foo();
  281. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  282. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  283. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  284. ->disableOriginalConstructor()
  285. ->getMock();
  286. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  287. ->disableOriginalConstructor()
  288. ->getMock();
  289. $mockForm->expects($this->once())
  290. ->method('createView')
  291. ->will($this->returnValue($mockView));
  292. $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  293. ->disableOriginalConstructor()
  294. ->getMock();
  295. $formBuilder->expects($this->once())->method('getForm')->will($this->returnValue($mockForm));
  296. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  297. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  298. $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  299. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  300. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  301. $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
  302. ->disableOriginalConstructor()
  303. ->getMock();
  304. $mockRenderer->expects($this->once())
  305. ->method('searchAndRenderBlock')
  306. ->will($this->returnValue(new Response()));
  307. $formExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
  308. $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
  309. $formExtension->renderer = $mockRenderer;
  310. $twig = new Twig();
  311. $twig->addExtension($formExtension);
  312. $request = new Request(array(
  313. 'code' => 'sonata.post.admin',
  314. 'objectId' => 42,
  315. 'field' => 'enabled',
  316. 'value' => 1,
  317. 'context' => 'list',
  318. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  319. $pool = new Pool($container, 'title', 'logo');
  320. $pool->setAdminServiceIds(array('sonata.post.admin'));
  321. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  322. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  323. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('getChildFormView'), array($pool));
  324. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  325. $controller = new HelperController($twig, $pool, $helper, $validator);
  326. $response = $controller->retrieveFormFieldElementAction($request);
  327. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  328. }
  329. public function testSetObjectFieldValueActionWithViolations()
  330. {
  331. $bar = new AdminControllerHelper_Bar();
  332. $object = new AdminControllerHelper_Foo();
  333. $object->setBar($bar);
  334. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  335. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
  336. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  337. $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
  338. $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
  339. $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
  340. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  341. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  342. $twig = new Twig();
  343. $request = new Request(array(
  344. 'code' => 'sonata.post.admin',
  345. 'objectId' => 42,
  346. 'field' => 'bar.enabled',
  347. 'value' => 1,
  348. 'context' => 'list',
  349. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  350. $pool = new Pool($container, 'title', 'logo');
  351. $pool->setAdminServiceIds(array('sonata.post.admin'));
  352. $helper = new AdminHelper($pool);
  353. $violations = new ConstraintViolationList(array(
  354. new ConstraintViolation('error1', null, array(), null, 'enabled', null),
  355. new ConstraintViolation('error2', null, array(), null, 'enabled', null),
  356. ));
  357. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  358. $validator
  359. ->expects($this->once())
  360. ->method('validateProperty')
  361. ->with($bar, 'enabled')
  362. ->will($this->returnValue($violations))
  363. ;
  364. $controller = new HelperController($twig, $pool, $helper, $validator);
  365. $response = $controller->setObjectFieldValueAction($request);
  366. $this->assertSame('{"status":"KO","message":"error1\nerror2"}', $response->getContent());
  367. }
  368. /**
  369. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  370. * @exceptionMessage Invalid format
  371. */
  372. public function testRetrieveAutocompleteItemsActionNotGranted()
  373. {
  374. $this->admin->expects($this->exactly(2))
  375. ->method('isGranted')
  376. ->will($this->returnCallback(function ($operation) {
  377. if ($operation == 'CREATE' || $operation == 'EDIT') {
  378. return false;
  379. }
  380. return;
  381. }));
  382. $request = new Request(array(
  383. 'code' => 'foo.admin',
  384. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  385. $this->controller->retrieveAutocompleteItemsAction($request);
  386. }
  387. /**
  388. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  389. * @exceptionMessage Autocomplete list can`t be retrieved because the form element is disabled or read_only.
  390. */
  391. public function testRetrieveAutocompleteItemsActionDisabledFormelememt()
  392. {
  393. $this->admin->expects($this->once())
  394. ->method('isGranted')
  395. ->with('CREATE')
  396. ->will($this->returnValue(true));
  397. $entity = new Foo();
  398. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  399. $fieldDescription->expects($this->once())
  400. ->method('getType')
  401. ->will($this->returnValue('sonata_type_model_autocomplete'));
  402. $fieldDescription->expects($this->once())
  403. ->method('getTargetEntity')
  404. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
  405. $fieldDescription->expects($this->once())
  406. ->method('getName')
  407. ->will($this->returnValue('barField'));
  408. $this->admin->expects($this->once())
  409. ->method('getFormFieldDescriptions')
  410. ->will($this->returnValue(null));
  411. $this->admin->expects($this->once())
  412. ->method('getFormFieldDescription')
  413. ->with('barField')
  414. ->will($this->returnValue($fieldDescription));
  415. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  416. ->disableOriginalConstructor()
  417. ->getMock();
  418. $this->admin->expects($this->once())
  419. ->method('getForm')
  420. ->will($this->returnValue($form));
  421. $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
  422. ->disableOriginalConstructor()
  423. ->getMock();
  424. $form->expects($this->once())
  425. ->method('get')
  426. ->with('barField')
  427. ->will($this->returnValue($formType));
  428. $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
  429. ->disableOriginalConstructor()
  430. ->getMock();
  431. $formType->expects($this->once())
  432. ->method('getConfig')
  433. ->will($this->returnValue($formConfig));
  434. $formConfig->expects($this->once())
  435. ->method('getAttribute')
  436. ->with('disabled')
  437. ->will($this->returnValue(true));
  438. $request = new Request(array(
  439. 'code' => 'foo.admin',
  440. 'field' => 'barField',
  441. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  442. $this->controller->retrieveAutocompleteItemsAction($request);
  443. }
  444. /**
  445. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  446. */
  447. public function testRetrieveAutocompleteItemsActionNotGrantedTarget()
  448. {
  449. $this->admin->expects($this->once())
  450. ->method('isGranted')
  451. ->with('CREATE')
  452. ->will($this->returnValue(true));
  453. $entity = new Foo();
  454. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  455. $fieldDescription->expects($this->once())
  456. ->method('getType')
  457. ->will($this->returnValue('sonata_type_model_autocomplete'));
  458. $fieldDescription->expects($this->once())
  459. ->method('getTargetEntity')
  460. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
  461. $fieldDescription->expects($this->once())
  462. ->method('getName')
  463. ->will($this->returnValue('barField'));
  464. $targetAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  465. $fieldDescription->expects($this->once())
  466. ->method('getAssociationAdmin')
  467. ->will($this->returnValue($targetAdmin));
  468. $targetAdmin->expects($this->once())
  469. ->method('isGranted')
  470. ->with('LIST')
  471. ->will($this->returnValue(false));
  472. $this->admin->expects($this->once())
  473. ->method('getFormFieldDescriptions')
  474. ->will($this->returnValue(null));
  475. $this->admin->expects($this->once())
  476. ->method('getFormFieldDescription')
  477. ->with('barField')
  478. ->will($this->returnValue($fieldDescription));
  479. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  480. ->disableOriginalConstructor()
  481. ->getMock();
  482. $this->admin->expects($this->once())
  483. ->method('getForm')
  484. ->will($this->returnValue($form));
  485. $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
  486. ->disableOriginalConstructor()
  487. ->getMock();
  488. $form->expects($this->once())
  489. ->method('get')
  490. ->with('barField')
  491. ->will($this->returnValue($formType));
  492. $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
  493. ->disableOriginalConstructor()
  494. ->getMock();
  495. $formType->expects($this->any())
  496. ->method('getConfig')
  497. ->will($this->returnValue($formConfig));
  498. $formConfig->expects($this->any())
  499. ->method('getAttribute')
  500. ->will($this->returnCallback(function ($name) {
  501. switch ($name) {
  502. case 'disabled':
  503. return false;
  504. case 'property':
  505. return 'fooProperty';
  506. case 'callback':
  507. return;
  508. case 'minimum_input_length':
  509. return 3;
  510. case 'items_per_page':
  511. return 10;
  512. case 'req_param_name_page_number':
  513. return '_page';
  514. case 'to_string_callback':
  515. return;
  516. }
  517. return;
  518. }));
  519. $request = new Request(array(
  520. 'code' => 'foo.admin',
  521. 'field' => 'barField',
  522. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  523. $this->controller->retrieveAutocompleteItemsAction($request);
  524. }
  525. }