HelperControllerTest.php 28 KB

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