HelperControllerTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
  17. use Symfony\Bridge\Twig\Extension\FormExtension;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Validator\ConstraintViolation;
  21. use Symfony\Component\Validator\ConstraintViolationList;
  22. class AdminControllerHelper_Foo
  23. {
  24. private $bar;
  25. public function getAdminTitle()
  26. {
  27. return 'foo';
  28. }
  29. public function setEnabled($value)
  30. {
  31. }
  32. public function setBar(AdminControllerHelper_Bar $bar)
  33. {
  34. $this->bar = $bar;
  35. }
  36. public function getBar()
  37. {
  38. return $this->bar;
  39. }
  40. }
  41. class AdminControllerHelper_Bar
  42. {
  43. public function getAdminTitle()
  44. {
  45. return 'bar';
  46. }
  47. public function setEnabled($value)
  48. {
  49. }
  50. public function getEnabled()
  51. {
  52. }
  53. }
  54. class HelperControllerTest extends \PHPUnit_Framework_TestCase
  55. {
  56. /**
  57. * @var AdminInterface
  58. */
  59. private $admin;
  60. /**
  61. * @var HelperController
  62. */
  63. private $controller;
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function setUp()
  68. {
  69. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  70. $pool = new Pool($container, 'title', 'logo.png');
  71. $pool->setAdminServiceIds(array('foo.admin'));
  72. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  73. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  74. $helper = new AdminHelper($pool);
  75. $validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
  76. $this->controller = new HelperController($twig, $pool, $helper, $validator);
  77. // php 5.3 BC
  78. $admin = $this->admin;
  79. $container->expects($this->any())
  80. ->method('get')
  81. ->will($this->returnCallback(function ($id) use ($admin) {
  82. switch ($id) {
  83. case 'foo.admin':
  84. return $admin;
  85. }
  86. }));
  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. $fieldDescription->expects($this->exactly(2))->method('getAdmin')->will($this->returnValue($admin));
  211. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  212. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  213. $pool = new Pool($container, 'title', 'logo');
  214. $pool->setAdminServiceIds(array('sonata.post.admin'));
  215. $adminExtension = new SonataAdminExtension(
  216. $pool,
  217. $this->getMock('Psr\Log\LoggerInterface'),
  218. $this->getMock('Symfony\Component\Translation\TranslatorInterface')
  219. );
  220. $loader = $this->getMock('\Twig_LoaderInterface');
  221. $loader->method('getSource')->will($this->returnValue('<foo />'));
  222. $twig = new \Twig_Environment($loader);
  223. $twig->addExtension($adminExtension);
  224. $request = new Request(array(
  225. 'code' => 'sonata.post.admin',
  226. 'objectId' => 42,
  227. 'field' => 'enabled',
  228. 'value' => 1,
  229. 'context' => 'list',
  230. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  231. $helper = new AdminHelper($pool);
  232. $validator = $this->getMock($validatorInterface);
  233. $controller = new HelperController($twig, $pool, $helper, $validator);
  234. $response = $controller->setObjectFieldValueAction($request);
  235. $this->assertSame('{"status":"OK","content":"\u003Cfoo \/\u003E"}', $response->getContent());
  236. }
  237. /**
  238. * @dataProvider getValidatorInterfaces
  239. */
  240. public function testappendFormFieldElementAction($validatorInterface)
  241. {
  242. $object = new AdminControllerHelper_Foo();
  243. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  244. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  245. $mockTheme = $this->getMockBuilder('Symfony\Component\Form\FormView')
  246. ->disableOriginalConstructor()
  247. ->getMock();
  248. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  249. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  250. $admin->expects($this->once())->method('setRequest');
  251. $admin->expects($this->once())->method('setSubject');
  252. $admin->expects($this->once())->method('getFormTheme')->will($this->returnValue($mockTheme));
  253. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  254. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  255. $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
  256. ->disableOriginalConstructor()
  257. ->getMock();
  258. $mockRenderer->expects($this->once())
  259. ->method('searchAndRenderBlock')
  260. ->will($this->returnValue(new Response()));
  261. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  262. $twig->addExtension(new FormExtension($mockRenderer));
  263. if (method_exists('Symfony\Bridge\Twig\AppVariable', 'getToken')) {
  264. $runtimeLoader = $this
  265. ->getMockBuilder('Twig_RuntimeLoaderInterface')
  266. ->getMock();
  267. $runtimeLoader->expects($this->once())
  268. ->method('load')
  269. ->with($this->equalTo('Symfony\Bridge\Twig\Form\TwigRenderer'))
  270. ->will($this->returnValue($mockRenderer));
  271. $twig->addRuntimeLoader($runtimeLoader);
  272. }
  273. $request = new Request(array(
  274. 'code' => 'sonata.post.admin',
  275. 'objectId' => 42,
  276. 'field' => 'enabled',
  277. 'value' => 1,
  278. 'context' => 'list',
  279. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  280. $pool = new Pool($container, 'title', 'logo');
  281. $pool->setAdminServiceIds(array('sonata.post.admin'));
  282. $validator = $this->getMock($validatorInterface);
  283. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  284. ->disableOriginalConstructor()
  285. ->getMock();
  286. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  287. ->disableOriginalConstructor()
  288. ->getMock();
  289. $mockForm->expects($this->once())
  290. ->method('createView')
  291. ->will($this->returnValue($mockView));
  292. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('appendFormFieldElement', 'getChildFormView'), array($pool));
  293. $helper->expects($this->once())->method('appendFormFieldElement')->will($this->returnValue(array(
  294. $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface'),
  295. $mockForm,
  296. )));
  297. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  298. $controller = new HelperController($twig, $pool, $helper, $validator);
  299. $response = $controller->appendFormFieldElementAction($request);
  300. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  301. }
  302. /**
  303. * @dataProvider getValidatorInterfaces
  304. */
  305. public function testRetrieveFormFieldElementAction($validatorInterface)
  306. {
  307. $object = new AdminControllerHelper_Foo();
  308. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  309. $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
  310. $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
  311. ->disableOriginalConstructor()
  312. ->getMock();
  313. $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  314. ->disableOriginalConstructor()
  315. ->getMock();
  316. $mockForm->expects($this->once())
  317. ->method('createView')
  318. ->will($this->returnValue($mockView));
  319. $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  320. ->disableOriginalConstructor()
  321. ->getMock();
  322. $formBuilder->expects($this->once())->method('getForm')->will($this->returnValue($mockForm));
  323. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  324. $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
  325. $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  326. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  327. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  328. $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
  329. ->disableOriginalConstructor()
  330. ->getMock();
  331. $mockRenderer->expects($this->once())
  332. ->method('searchAndRenderBlock')
  333. ->will($this->returnValue(new Response()));
  334. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  335. $twig->addExtension(new FormExtension($mockRenderer));
  336. if (method_exists('Symfony\Bridge\Twig\AppVariable', 'getToken')) {
  337. $runtimeLoader = $this
  338. ->getMockBuilder('Twig_RuntimeLoaderInterface')
  339. ->getMock();
  340. $runtimeLoader->expects($this->once())
  341. ->method('load')
  342. ->with($this->equalTo('Symfony\Bridge\Twig\Form\TwigRenderer'))
  343. ->will($this->returnValue($mockRenderer));
  344. $twig->addRuntimeLoader($runtimeLoader);
  345. }
  346. $request = new Request(array(
  347. 'code' => 'sonata.post.admin',
  348. 'objectId' => 42,
  349. 'field' => 'enabled',
  350. 'value' => 1,
  351. 'context' => 'list',
  352. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  353. $pool = new Pool($container, 'title', 'logo');
  354. $pool->setAdminServiceIds(array('sonata.post.admin'));
  355. $validator = $this->getMock($validatorInterface);
  356. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('getChildFormView'), array($pool));
  357. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  358. $controller = new HelperController($twig, $pool, $helper, $validator);
  359. $response = $controller->retrieveFormFieldElementAction($request);
  360. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  361. }
  362. /**
  363. * @dataProvider getValidatorInterfaces
  364. */
  365. public function testSetObjectFieldValueActionWithViolations($validatorInterface)
  366. {
  367. $bar = new AdminControllerHelper_Bar();
  368. $object = new AdminControllerHelper_Foo();
  369. $object->setBar($bar);
  370. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  371. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
  372. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  373. $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
  374. $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
  375. $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
  376. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  377. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  378. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  379. $request = new Request(array(
  380. 'code' => 'sonata.post.admin',
  381. 'objectId' => 42,
  382. 'field' => 'bar.enabled',
  383. 'value' => 1,
  384. 'context' => 'list',
  385. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  386. $pool = new Pool($container, 'title', 'logo');
  387. $pool->setAdminServiceIds(array('sonata.post.admin'));
  388. $helper = new AdminHelper($pool);
  389. $violations = new ConstraintViolationList(array(
  390. new ConstraintViolation('error1', null, array(), null, 'enabled', null),
  391. new ConstraintViolation('error2', null, array(), null, 'enabled', null),
  392. ));
  393. $validator = $this->getMock($validatorInterface);
  394. $validator
  395. ->expects($this->once())
  396. ->method('validate')
  397. ->with($bar)
  398. ->will($this->returnValue($violations))
  399. ;
  400. $controller = new HelperController($twig, $pool, $helper, $validator);
  401. $response = $controller->setObjectFieldValueAction($request);
  402. $this->assertSame('{"status":"KO","message":"error1\nerror2"}', $response->getContent());
  403. }
  404. /**
  405. * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
  406. * @exceptionMessage Invalid format
  407. */
  408. public function testRetrieveAutocompleteItemsActionNotGranted()
  409. {
  410. $this->admin->expects($this->exactly(2))
  411. ->method('isGranted')
  412. ->will($this->returnCallback(function ($operation) {
  413. if ($operation == 'CREATE' || $operation == 'EDIT') {
  414. return false;
  415. }
  416. return;
  417. }));
  418. $request = new Request(array(
  419. 'admin_code' => 'foo.admin',
  420. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  421. $this->controller->retrieveAutocompleteItemsAction($request);
  422. }
  423. /**
  424. * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
  425. * @exceptionMessage Invalid format
  426. */
  427. public function testRetrieveFilterAutocompleteItemsActionNotGranted()
  428. {
  429. $this->admin->expects($this->exactly(1))
  430. ->method('isGranted')
  431. ->will($this->returnCallback(function ($operation) {
  432. if ($operation == 'LIST') {
  433. return false;
  434. }
  435. return;
  436. }));
  437. $request = new Request(array(
  438. 'admin_code' => 'foo.admin',
  439. '_context' => 'filter',
  440. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  441. $this->controller->retrieveAutocompleteItemsAction($request);
  442. }
  443. /**
  444. * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
  445. * @exceptionMessage Autocomplete list can`t be retrieved because the form element is disabled or read_only.
  446. */
  447. public function testRetrieveAutocompleteItemsActionDisabledFormelememt()
  448. {
  449. $this->admin->expects($this->once())
  450. ->method('isGranted')
  451. ->with('CREATE')
  452. ->will($this->returnValue(true));
  453. $entity = new Foo();
  454. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  455. $fieldDescription->expects($this->once())
  456. ->method('getTargetEntity')
  457. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
  458. $fieldDescription->expects($this->once())
  459. ->method('getName')
  460. ->will($this->returnValue('barField'));
  461. $this->admin->expects($this->once())
  462. ->method('getFormFieldDescriptions')
  463. ->will($this->returnValue(null));
  464. $this->admin->expects($this->once())
  465. ->method('getFormFieldDescription')
  466. ->with('barField')
  467. ->will($this->returnValue($fieldDescription));
  468. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  469. ->disableOriginalConstructor()
  470. ->getMock();
  471. $this->admin->expects($this->once())
  472. ->method('getForm')
  473. ->will($this->returnValue($form));
  474. $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
  475. ->disableOriginalConstructor()
  476. ->getMock();
  477. $form->expects($this->once())
  478. ->method('get')
  479. ->with('barField')
  480. ->will($this->returnValue($formType));
  481. $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
  482. ->disableOriginalConstructor()
  483. ->getMock();
  484. $formType->expects($this->once())
  485. ->method('getConfig')
  486. ->will($this->returnValue($formConfig));
  487. $formConfig->expects($this->once())
  488. ->method('getAttribute')
  489. ->with('disabled')
  490. ->will($this->returnValue(true));
  491. $request = new Request(array(
  492. 'admin_code' => 'foo.admin',
  493. 'field' => 'barField',
  494. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  495. $this->controller->retrieveAutocompleteItemsAction($request);
  496. }
  497. /**
  498. * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
  499. */
  500. public function testRetrieveAutocompleteItemsActionNotGrantedTarget()
  501. {
  502. $this->admin->expects($this->once())
  503. ->method('isGranted')
  504. ->with('CREATE')
  505. ->will($this->returnValue(true));
  506. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  507. $fieldDescription->expects($this->once())
  508. ->method('getTargetEntity')
  509. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
  510. $fieldDescription->expects($this->once())
  511. ->method('getName')
  512. ->will($this->returnValue('barField'));
  513. $targetAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  514. $fieldDescription->expects($this->once())
  515. ->method('getAssociationAdmin')
  516. ->will($this->returnValue($targetAdmin));
  517. $targetAdmin->expects($this->once())
  518. ->method('isGranted')
  519. ->with('LIST')
  520. ->will($this->returnValue(false));
  521. $this->admin->expects($this->once())
  522. ->method('getFormFieldDescriptions')
  523. ->will($this->returnValue(null));
  524. $this->admin->expects($this->once())
  525. ->method('getFormFieldDescription')
  526. ->with('barField')
  527. ->will($this->returnValue($fieldDescription));
  528. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  529. ->disableOriginalConstructor()
  530. ->getMock();
  531. $this->admin->expects($this->once())
  532. ->method('getForm')
  533. ->will($this->returnValue($form));
  534. $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
  535. ->disableOriginalConstructor()
  536. ->getMock();
  537. $form->expects($this->once())
  538. ->method('get')
  539. ->with('barField')
  540. ->will($this->returnValue($formType));
  541. $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
  542. ->disableOriginalConstructor()
  543. ->getMock();
  544. $formType->expects($this->any())
  545. ->method('getConfig')
  546. ->will($this->returnValue($formConfig));
  547. $formConfig->expects($this->any())
  548. ->method('getAttribute')
  549. ->will($this->returnCallback(function ($name) {
  550. switch ($name) {
  551. case 'disabled':
  552. return false;
  553. case 'property':
  554. return 'fooProperty';
  555. case 'callback':
  556. return;
  557. case 'minimum_input_length':
  558. return 3;
  559. case 'items_per_page':
  560. return 10;
  561. case 'req_param_name_page_number':
  562. return '_page';
  563. case 'to_string_callback':
  564. return;
  565. }
  566. }));
  567. $request = new Request(array(
  568. 'admin_code' => 'foo.admin',
  569. 'field' => 'barField',
  570. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  571. $this->controller->retrieveAutocompleteItemsAction($request);
  572. }
  573. /**
  574. * Symfony Validator has 2 API version (2.4 and 2.5)
  575. * This data provider ensure tests pass on each one.
  576. */
  577. public function getValidatorInterfaces()
  578. {
  579. $data = array();
  580. // For Symfony <= 2.8
  581. if (interface_exists('Symfony\Component\Validator\ValidatorInterface')) {
  582. $data['2.4'] = array('Symfony\Component\Validator\ValidatorInterface');
  583. }
  584. // For Symfony >= 2.5
  585. if (interface_exists('Symfony\Component\Validator\Validator\ValidatorInterface')) {
  586. $data['2.5'] = array('Symfony\Component\Validator\Validator\ValidatorInterface');
  587. }
  588. return $data;
  589. }
  590. }