HelperControllerTest.php 28 KB

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