HelperControllerTest.php 28 KB

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