HelperControllerTest.php 26 KB

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