HelperControllerTest.php 26 KB

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