HelperControllerTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. $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. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  325. $twig->addExtension(new FormExtension($mockRenderer));
  326. $request = new Request(array(
  327. 'code' => 'sonata.post.admin',
  328. 'objectId' => 42,
  329. 'field' => 'enabled',
  330. 'value' => 1,
  331. 'context' => 'list',
  332. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
  333. $pool = new Pool($container, 'title', 'logo');
  334. $pool->setAdminServiceIds(array('sonata.post.admin'));
  335. $validator = $this->getMock($validatorInterface);
  336. $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('getChildFormView'), array($pool));
  337. $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
  338. $controller = new HelperController($twig, $pool, $helper, $validator);
  339. $response = $controller->retrieveFormFieldElementAction($request);
  340. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  341. }
  342. /**
  343. * @dataProvider getValidatorInterfaces
  344. */
  345. public function testSetObjectFieldValueActionWithViolations($validatorInterface)
  346. {
  347. $bar = new AdminControllerHelper_Bar();
  348. $object = new AdminControllerHelper_Foo();
  349. $object->setBar($bar);
  350. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  351. $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
  352. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  353. $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
  354. $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
  355. $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
  356. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  357. $container->expects($this->any())->method('get')->will($this->returnValue($admin));
  358. $twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
  359. $request = new Request(array(
  360. 'code' => 'sonata.post.admin',
  361. 'objectId' => 42,
  362. 'field' => 'bar.enabled',
  363. 'value' => 1,
  364. 'context' => 'list',
  365. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  366. $pool = new Pool($container, 'title', 'logo');
  367. $pool->setAdminServiceIds(array('sonata.post.admin'));
  368. $helper = new AdminHelper($pool);
  369. $violations = new ConstraintViolationList(array(
  370. new ConstraintViolation('error1', null, array(), null, 'enabled', null),
  371. new ConstraintViolation('error2', null, array(), null, 'enabled', null),
  372. ));
  373. $validator = $this->getMock($validatorInterface);
  374. $validator
  375. ->expects($this->once())
  376. ->method('validate')
  377. ->with($bar)
  378. ->will($this->returnValue($violations))
  379. ;
  380. $controller = new HelperController($twig, $pool, $helper, $validator);
  381. $response = $controller->setObjectFieldValueAction($request);
  382. $this->assertSame('{"status":"KO","message":"error1\nerror2"}', $response->getContent());
  383. }
  384. /**
  385. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  386. * @exceptionMessage Invalid format
  387. */
  388. public function testRetrieveAutocompleteItemsActionNotGranted()
  389. {
  390. $this->admin->expects($this->exactly(2))
  391. ->method('isGranted')
  392. ->will($this->returnCallback(function ($operation) {
  393. if ($operation == 'CREATE' || $operation == 'EDIT') {
  394. return false;
  395. }
  396. return;
  397. }));
  398. $request = new Request(array(
  399. 'admin_code' => 'foo.admin',
  400. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  401. $this->controller->retrieveAutocompleteItemsAction($request);
  402. }
  403. /**
  404. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  405. * @exceptionMessage Invalid format
  406. */
  407. public function testRetrieveFilterAutocompleteItemsActionNotGranted()
  408. {
  409. $this->admin->expects($this->exactly(1))
  410. ->method('isGranted')
  411. ->will($this->returnCallback(function ($operation) {
  412. if ($operation == 'LIST') {
  413. return false;
  414. }
  415. return;
  416. }));
  417. $request = new Request(array(
  418. 'admin_code' => 'foo.admin',
  419. '_context' => 'filter',
  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 Autocomplete list can`t be retrieved because the form element is disabled or read_only.
  426. */
  427. public function testRetrieveAutocompleteItemsActionDisabledFormelememt()
  428. {
  429. $this->admin->expects($this->once())
  430. ->method('isGranted')
  431. ->with('CREATE')
  432. ->will($this->returnValue(true));
  433. $entity = new Foo();
  434. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  435. $fieldDescription->expects($this->once())
  436. ->method('getTargetEntity')
  437. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
  438. $fieldDescription->expects($this->once())
  439. ->method('getName')
  440. ->will($this->returnValue('barField'));
  441. $this->admin->expects($this->once())
  442. ->method('getFormFieldDescriptions')
  443. ->will($this->returnValue(null));
  444. $this->admin->expects($this->once())
  445. ->method('getFormFieldDescription')
  446. ->with('barField')
  447. ->will($this->returnValue($fieldDescription));
  448. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  449. ->disableOriginalConstructor()
  450. ->getMock();
  451. $this->admin->expects($this->once())
  452. ->method('getForm')
  453. ->will($this->returnValue($form));
  454. $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
  455. ->disableOriginalConstructor()
  456. ->getMock();
  457. $form->expects($this->once())
  458. ->method('get')
  459. ->with('barField')
  460. ->will($this->returnValue($formType));
  461. $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
  462. ->disableOriginalConstructor()
  463. ->getMock();
  464. $formType->expects($this->once())
  465. ->method('getConfig')
  466. ->will($this->returnValue($formConfig));
  467. $formConfig->expects($this->once())
  468. ->method('getAttribute')
  469. ->with('disabled')
  470. ->will($this->returnValue(true));
  471. $request = new Request(array(
  472. 'admin_code' => 'foo.admin',
  473. 'field' => 'barField',
  474. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  475. $this->controller->retrieveAutocompleteItemsAction($request);
  476. }
  477. /**
  478. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  479. */
  480. public function testRetrieveAutocompleteItemsActionNotGrantedTarget()
  481. {
  482. $this->admin->expects($this->once())
  483. ->method('isGranted')
  484. ->with('CREATE')
  485. ->will($this->returnValue(true));
  486. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  487. $fieldDescription->expects($this->once())
  488. ->method('getTargetEntity')
  489. ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
  490. $fieldDescription->expects($this->once())
  491. ->method('getName')
  492. ->will($this->returnValue('barField'));
  493. $targetAdmin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  494. $fieldDescription->expects($this->once())
  495. ->method('getAssociationAdmin')
  496. ->will($this->returnValue($targetAdmin));
  497. $targetAdmin->expects($this->once())
  498. ->method('isGranted')
  499. ->with('LIST')
  500. ->will($this->returnValue(false));
  501. $this->admin->expects($this->once())
  502. ->method('getFormFieldDescriptions')
  503. ->will($this->returnValue(null));
  504. $this->admin->expects($this->once())
  505. ->method('getFormFieldDescription')
  506. ->with('barField')
  507. ->will($this->returnValue($fieldDescription));
  508. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  509. ->disableOriginalConstructor()
  510. ->getMock();
  511. $this->admin->expects($this->once())
  512. ->method('getForm')
  513. ->will($this->returnValue($form));
  514. $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
  515. ->disableOriginalConstructor()
  516. ->getMock();
  517. $form->expects($this->once())
  518. ->method('get')
  519. ->with('barField')
  520. ->will($this->returnValue($formType));
  521. $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
  522. ->disableOriginalConstructor()
  523. ->getMock();
  524. $formType->expects($this->any())
  525. ->method('getConfig')
  526. ->will($this->returnValue($formConfig));
  527. $formConfig->expects($this->any())
  528. ->method('getAttribute')
  529. ->will($this->returnCallback(function ($name) {
  530. switch ($name) {
  531. case 'disabled':
  532. return false;
  533. case 'property':
  534. return 'fooProperty';
  535. case 'callback':
  536. return;
  537. case 'minimum_input_length':
  538. return 3;
  539. case 'items_per_page':
  540. return 10;
  541. case 'req_param_name_page_number':
  542. return '_page';
  543. case 'to_string_callback':
  544. return;
  545. }
  546. }));
  547. $request = new Request(array(
  548. 'admin_code' => 'foo.admin',
  549. 'field' => 'barField',
  550. ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
  551. $this->controller->retrieveAutocompleteItemsAction($request);
  552. }
  553. /**
  554. * Symfony Validator has 2 API version (2.4 and 2.5)
  555. * This data provider ensure tests pass on each one.
  556. */
  557. public function getValidatorInterfaces()
  558. {
  559. $data = array();
  560. // For Symfony <= 2.8
  561. if (interface_exists('Symfony\Component\Validator\ValidatorInterface')) {
  562. $data['2.4'] = array('Symfony\Component\Validator\ValidatorInterface');
  563. }
  564. // For Symfony >= 2.5
  565. if (interface_exists('Symfony\Component\Validator\Validator\ValidatorInterface')) {
  566. $data['2.5'] = array('Symfony\Component\Validator\Validator\ValidatorInterface');
  567. }
  568. return $data;
  569. }
  570. }