HelperControllerTest.php 28 KB

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