123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Tests\Controller;
- use Sonata\AdminBundle\Admin\AdminHelper;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\Pool;
- use Sonata\AdminBundle\Controller\HelperController;
- use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
- use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
- use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
- use Symfony\Bridge\Twig\Extension\FormExtension;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Validator\ConstraintViolation;
- use Symfony\Component\Validator\ConstraintViolationList;
- class AdminControllerHelper_Foo
- {
- private $bar;
- public function getAdminTitle()
- {
- return 'foo';
- }
- public function setEnabled($value)
- {
- }
- public function setBar(AdminControllerHelper_Bar $bar)
- {
- $this->bar = $bar;
- }
- public function getBar()
- {
- return $this->bar;
- }
- }
- class AdminControllerHelper_Bar
- {
- public function getAdminTitle()
- {
- return 'bar';
- }
- public function setEnabled($value)
- {
- }
- public function getEnabled()
- {
- }
- }
- class HelperControllerTest extends PHPUnit_Framework_TestCase
- {
- /**
- * @var AdminInterface
- */
- private $admin;
- /**
- * @var HelperController
- */
- private $controller;
- /**
- * {@inheritdoc}
- */
- protected function setUp()
- {
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $pool = new Pool($container, 'title', 'logo.png');
- $pool->setAdminServiceIds(array('foo.admin'));
- $this->admin = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin');
- $twig = new \Twig_Environment($this->createMock('\Twig_LoaderInterface'));
- $helper = new AdminHelper($pool);
- // NEXT_MAJOR: Remove this when dropping support for SF < 2.8
- if (interface_exists('Symfony\Component\Validator\ValidatorInterface')) {
- $validator = $this->createMock('Symfony\Component\Validator\ValidatorInterface');
- } else {
- $validator = $this->createMock('Symfony\Component\Validator\Validator\ValidatorInterface');
- }
- $this->controller = new HelperController($twig, $pool, $helper, $validator);
- // php 5.3 BC
- $admin = $this->admin;
- $container->expects($this->any())
- ->method('get')
- ->will($this->returnCallback(function ($id) use ($admin) {
- switch ($id) {
- case 'foo.admin':
- return $admin;
- }
- }));
- }
- /**
- * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
- * @dataProvider getValidatorInterfaces
- */
- public function testgetShortObjectDescriptionActionInvalidAdmin($validatorInterface)
- {
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $twig = new \Twig_Environment($this->createMock('\Twig_LoaderInterface'));
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'uniqid' => 'asdasd123',
- ));
- $pool = new Pool($container, 'title', 'logo');
- $pool->setAdminServiceIds(array('sonata.post.admin'));
- $helper = new AdminHelper($pool);
- $validator = $this->createMock($validatorInterface);
- $controller = new HelperController($twig, $pool, $helper, $validator);
- $controller->getShortObjectDescriptionAction($request);
- }
- /**
- * @expectedException \RuntimeException
- * @exceptionMessage Invalid format
- *
- * @dataProvider getValidatorInterfaces
- */
- public function testgetShortObjectDescriptionActionObjectDoesNotExist($validatorInterface)
- {
- $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
- $admin->expects($this->once())->method('setUniqid');
- $admin->expects($this->once())->method('getObject')->will($this->returnValue(false));
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $twig = new \Twig_Environment($this->createMock('\Twig_LoaderInterface'));
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'uniqid' => 'asdasd123',
- ));
- $pool = new Pool($container, 'title', 'logo');
- $pool->setAdminServiceIds(array('sonata.post.admin'));
- $helper = new AdminHelper($pool);
- $validator = $this->createMock($validatorInterface);
- $controller = new HelperController($twig, $pool, $helper, $validator);
- $controller->getShortObjectDescriptionAction($request);
- }
- /**
- * @dataProvider getValidatorInterfaces
- */
- public function testgetShortObjectDescriptionActionEmptyObjectId($validatorInterface)
- {
- $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
- $admin->expects($this->once())->method('setUniqid');
- $admin->expects($this->once())->method('getObject')->with($this->identicalTo(null))->will($this->returnValue(false));
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $twig = new \Twig_Environment($this->createMock('\Twig_LoaderInterface'));
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => '',
- 'uniqid' => 'asdasd123',
- '_format' => 'html',
- ));
- $pool = new Pool($container, 'title', 'logo');
- $pool->setAdminServiceIds(array('sonata.post.admin'));
- $helper = new AdminHelper($pool);
- $validator = $this->createMock($validatorInterface);
- $controller = new HelperController($twig, $pool, $helper, $validator);
- $controller->getShortObjectDescriptionAction($request);
- }
- /**
- * @dataProvider getValidatorInterfaces
- */
- public function testgetShortObjectDescriptionActionObject($validatorInterface)
- {
- $mockTemplate = 'AdminHelperTest:mock-short-object-description.html.twig';
- $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
- $admin->expects($this->once())->method('setUniqid');
- $admin->expects($this->once())->method('getTemplate')->will($this->returnValue($mockTemplate));
- $admin->expects($this->once())->method('getObject')->will($this->returnValue(new AdminControllerHelper_Foo()));
- $admin->expects($this->once())->method('toString')->will($this->returnValue('bar'));
- $admin->expects($this->once())->method('generateObjectUrl')->will($this->returnCallback(function ($type, $object, $parameters = array()) {
- if ($type != 'edit') {
- return 'invalid name';
- }
- return '/ok/url';
- }));
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
- $twig->expects($this->once())->method('render')
- ->with($mockTemplate)
- ->will($this->returnCallback(function ($templateName, $templateParams) {
- return sprintf('<a href="%s" target="new">%s</a>', $templateParams['admin']->generateObjectUrl('edit', $templateParams['object']), $templateParams['description']);
- }));
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'uniqid' => 'asdasd123',
- '_format' => 'html',
- ));
- $pool = new Pool($container, 'title', 'logo');
- $pool->setAdminServiceIds(array('sonata.post.admin'));
- $helper = new AdminHelper($pool);
- $validator = $this->createMock($validatorInterface);
- $controller = new HelperController($twig, $pool, $helper, $validator);
- $response = $controller->getShortObjectDescriptionAction($request);
- $expected = '<a href="/ok/url" target="new">bar</a>';
- $this->assertSame($expected, $response->getContent());
- }
- /**
- * @dataProvider getValidatorInterfaces
- */
- public function testsetObjectFieldValueAction($validatorInterface)
- {
- $object = new AdminControllerHelper_Foo();
- $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
- $admin = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin');
- $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
- $admin->expects($this->once())->method('hasAccess')->will($this->returnValue(true));
- $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
- $fieldDescription->expects($this->exactly(2))->method('getAdmin')->will($this->returnValue($admin));
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $pool = new Pool($container, 'title', 'logo');
- $pool->setAdminServiceIds(array('sonata.post.admin'));
- $adminExtension = new SonataAdminExtension(
- $pool,
- $this->createMock('Psr\Log\LoggerInterface'),
- $this->createMock('Symfony\Component\Translation\TranslatorInterface')
- );
- $loader = $this->createMock('\Twig_LoaderInterface');
- // NEXT_MAJOR: Remove this check when dropping support for twig < 2
- if (method_exists('\Twig_LoaderInterface', 'getSourceContext')) {
- $loader->method('getSourceContext')->will($this->returnValue(new \Twig_Source('<foo />', 'foo')));
- } else {
- $loader->method('getSource')->will($this->returnValue('<foo />'));
- }
- $twig = new \Twig_Environment($loader);
- $twig->addExtension($adminExtension);
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'field' => 'enabled',
- 'value' => 1,
- 'context' => 'list',
- ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
- $helper = new AdminHelper($pool);
- $validator = $this->createMock($validatorInterface);
- $controller = new HelperController($twig, $pool, $helper, $validator);
- $response = $controller->setObjectFieldValueAction($request);
- $this->assertEquals(200, $response->getStatusCode());
- }
- /**
- * @dataProvider getValidatorInterfaces
- */
- public function testappendFormFieldElementAction($validatorInterface)
- {
- $object = new AdminControllerHelper_Foo();
- $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
- $mockTheme = $this->getMockBuilder('Symfony\Component\Form\FormView')
- ->disableOriginalConstructor()
- ->getMock();
- $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
- $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
- $admin->expects($this->once())->method('setRequest');
- $admin->expects($this->once())->method('setSubject');
- $admin->expects($this->once())->method('getFormTheme')->will($this->returnValue($mockTheme));
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
- ->disableOriginalConstructor()
- ->getMock();
- $mockRenderer->expects($this->once())
- ->method('searchAndRenderBlock')
- ->will($this->returnValue(new Response()));
- $twig = new \Twig_Environment($this->createMock('\Twig_LoaderInterface'));
- $twig->addExtension(new FormExtension($mockRenderer));
- if (method_exists('Symfony\Bridge\Twig\AppVariable', 'getToken')) {
- $runtimeLoader = $this
- ->getMockBuilder('Twig_RuntimeLoaderInterface')
- ->getMock();
- $runtimeLoader->expects($this->once())
- ->method('load')
- ->with($this->equalTo('Symfony\Bridge\Twig\Form\TwigRenderer'))
- ->will($this->returnValue($mockRenderer));
- $twig->addRuntimeLoader($runtimeLoader);
- }
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'field' => 'enabled',
- 'value' => 1,
- 'context' => 'list',
- ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
- $pool = new Pool($container, 'title', 'logo');
- $pool->setAdminServiceIds(array('sonata.post.admin'));
- $validator = $this->createMock($validatorInterface);
- $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
- ->disableOriginalConstructor()
- ->getMock();
- $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $mockForm->expects($this->once())
- ->method('createView')
- ->will($this->returnValue($mockView));
- $helper = $this->getMockBuilder('Sonata\AdminBundle\Admin\AdminHelper')
- ->setMethods(array('appendFormFieldElement', 'getChildFormView'))
- ->setConstructorArgs(array($pool))
- ->getMock();
- $helper->expects($this->once())->method('appendFormFieldElement')->will($this->returnValue(array(
- $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface'),
- $mockForm,
- )));
- $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
- $controller = new HelperController($twig, $pool, $helper, $validator);
- $response = $controller->appendFormFieldElementAction($request);
- $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- }
- /**
- * @dataProvider getValidatorInterfaces
- */
- public function testRetrieveFormFieldElementAction($validatorInterface)
- {
- $object = new AdminControllerHelper_Foo();
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'field' => 'enabled',
- 'value' => 1,
- 'context' => 'list',
- ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST'));
- $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
- $mockView = $this->getMockBuilder('Symfony\Component\Form\FormView')
- ->disableOriginalConstructor()
- ->getMock();
- $mockForm = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $mockForm->expects($this->once())
- ->method('setData')
- ->with($object);
- $mockForm->expects($this->once())
- ->method('handleRequest')
- ->with($request);
- $mockForm->expects($this->once())
- ->method('createView')
- ->will($this->returnValue($mockView));
- $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
- ->disableOriginalConstructor()
- ->getMock();
- $formBuilder->expects($this->once())->method('getForm')->will($this->returnValue($mockForm));
- $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
- $admin->expects($this->once())->method('getModelManager')->will($this->returnValue($modelManager));
- $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $mockRenderer = $this->getMockBuilder('Symfony\Bridge\Twig\Form\TwigRendererInterface')
- ->disableOriginalConstructor()
- ->getMock();
- $mockRenderer->expects($this->once())
- ->method('searchAndRenderBlock')
- ->will($this->returnValue(new Response()));
- $twig = new \Twig_Environment($this->createMock('\Twig_LoaderInterface'));
- $twig->addExtension(new FormExtension($mockRenderer));
- if (method_exists('Symfony\Bridge\Twig\AppVariable', 'getToken')) {
- $runtimeLoader = $this
- ->getMockBuilder('Twig_RuntimeLoaderInterface')
- ->getMock();
- $runtimeLoader->expects($this->once())
- ->method('load')
- ->with($this->equalTo('Symfony\Bridge\Twig\Form\TwigRenderer'))
- ->will($this->returnValue($mockRenderer));
- $twig->addRuntimeLoader($runtimeLoader);
- }
- $pool = new Pool($container, 'title', 'logo');
- $pool->setAdminServiceIds(array('sonata.post.admin'));
- $validator = $this->createMock($validatorInterface);
- $helper = $this->getMockBuilder('Sonata\AdminBundle\Admin\AdminHelper')
- ->setMethods(array('getChildFormView'))
- ->setConstructorArgs(array($pool))
- ->getMock();
- $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($mockView));
- $controller = new HelperController($twig, $pool, $helper, $validator);
- $response = $controller->retrieveFormFieldElementAction($request);
- $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- }
- /**
- * @dataProvider getValidatorInterfaces
- */
- public function testSetObjectFieldValueActionWithViolations($validatorInterface)
- {
- $bar = new AdminControllerHelper_Bar();
- $object = new AdminControllerHelper_Foo();
- $object->setBar($bar);
- $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
- $admin = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin');
- $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
- $admin->expects($this->once())->method('hasAccess')->will($this->returnValue(true));
- $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $twig = new \Twig_Environment($this->createMock('\Twig_LoaderInterface'));
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'field' => 'bar.enabled',
- 'value' => 1,
- 'context' => 'list',
- ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'POST', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
- $pool = new Pool($container, 'title', 'logo');
- $pool->setAdminServiceIds(array('sonata.post.admin'));
- $helper = new AdminHelper($pool);
- $violations = new ConstraintViolationList(array(
- new ConstraintViolation('error1', null, array(), null, 'enabled', null),
- new ConstraintViolation('error2', null, array(), null, 'enabled', null),
- ));
- $validator = $this->createMock($validatorInterface);
- $validator
- ->expects($this->once())
- ->method('validate')
- ->with($bar)
- ->will($this->returnValue($violations))
- ;
- $controller = new HelperController($twig, $pool, $helper, $validator);
- $response = $controller->setObjectFieldValueAction($request);
- $this->assertEquals(400, $response->getStatusCode());
- $this->assertSame(json_encode("error1\nerror2"), $response->getContent());
- }
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
- * @exceptionMessage Invalid format
- */
- public function testRetrieveAutocompleteItemsActionNotGranted()
- {
- $this->admin->expects($this->exactly(2))
- ->method('hasAccess')
- ->will($this->returnCallback(function ($operation) {
- if ($operation == 'create' || $operation == 'edit') {
- return false;
- }
- return;
- }));
- $request = new Request(array(
- 'admin_code' => 'foo.admin',
- ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
- $this->controller->retrieveAutocompleteItemsAction($request);
- }
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
- * @exceptionMessage Autocomplete list can`t be retrieved because the form element is disabled or read_only.
- */
- public function testRetrieveAutocompleteItemsActionDisabledFormelememt()
- {
- $this->admin->expects($this->once())
- ->method('hasAccess')
- ->with('create')
- ->will($this->returnValue(true));
- $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription->expects($this->once())
- ->method('getTargetEntity')
- ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
- $fieldDescription->expects($this->once())
- ->method('getName')
- ->will($this->returnValue('barField'));
- $this->admin->expects($this->once())
- ->method('getFormFieldDescriptions')
- ->will($this->returnValue(null));
- $this->admin->expects($this->once())
- ->method('getFormFieldDescription')
- ->with('barField')
- ->will($this->returnValue($fieldDescription));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $form->expects($this->once())
- ->method('get')
- ->with('barField')
- ->will($this->returnValue($formType));
- $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
- ->disableOriginalConstructor()
- ->getMock();
- $formType->expects($this->once())
- ->method('getConfig')
- ->will($this->returnValue($formConfig));
- $formConfig->expects($this->once())
- ->method('getAttribute')
- ->with('disabled')
- ->will($this->returnValue(true));
- $request = new Request(array(
- 'admin_code' => 'foo.admin',
- 'field' => 'barField',
- ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
- $this->controller->retrieveAutocompleteItemsAction($request);
- }
- public function testRetrieveAutocompleteItemsTooShortSearchString()
- {
- $this->admin->expects($this->once())
- ->method('hasAccess')
- ->with('create')
- ->will($this->returnValue(true));
- $targetAdmin = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin');
- $targetAdmin->expects($this->once())
- ->method('checkAccess')
- ->with('list')
- ->will($this->returnValue(null));
- $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription->expects($this->once())
- ->method('getTargetEntity')
- ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
- $fieldDescription->expects($this->once())
- ->method('getName')
- ->will($this->returnValue('barField'));
- $fieldDescription->expects($this->once())
- ->method('getAssociationAdmin')
- ->will($this->returnValue($targetAdmin));
- $this->admin->expects($this->once())
- ->method('getFormFieldDescriptions')
- ->will($this->returnValue(null));
- $this->admin->expects($this->once())
- ->method('getFormFieldDescription')
- ->with('barField')
- ->will($this->returnValue($fieldDescription));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $form->expects($this->once())
- ->method('get')
- ->with('barField')
- ->will($this->returnValue($formType));
- $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
- ->disableOriginalConstructor()
- ->getMock();
- $formType->expects($this->once())
- ->method('getConfig')
- ->will($this->returnValue($formConfig));
- $formConfig->expects($this->any())
- ->method('getAttribute')
- ->will($this->returnCallback(function ($name, $default = null) {
- switch ($name) {
- case 'property':
- return 'foo';
- case 'callback':
- return;
- case 'minimum_input_length':
- return 3;
- case 'items_per_page':
- return 10;
- case 'req_param_name_page_number':
- return '_page';
- case 'to_string_callback':
- return;
- case 'disabled':
- return false;
- case 'target_admin_access_action':
- return 'list';
- default:
- throw new \RuntimeException(sprintf('Unkown parameter "%s" called.', $name));
- }
- }));
- $request = new Request(array(
- 'admin_code' => 'foo.admin',
- 'field' => 'barField',
- 'q' => 'so',
- ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
- $response = $this->controller->retrieveAutocompleteItemsAction($request);
- $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame('application/json', $response->headers->get('Content-Type'));
- $this->assertSame('{"status":"KO","message":"Too short search string."}', $response->getContent());
- }
- public function testRetrieveAutocompleteItems()
- {
- $entity = new Foo();
- $this->admin->expects($this->once())
- ->method('hasAccess')
- ->with('create')
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('id')
- ->with($entity)
- ->will($this->returnValue(123));
- $targetAdmin = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin');
- $targetAdmin->expects($this->once())
- ->method('checkAccess')
- ->with('list')
- ->will($this->returnValue(null));
- $targetAdmin->expects($this->once())
- ->method('setPersistFilters')
- ->with(false)
- ->will($this->returnValue(null));
- $datagrid = $this->createMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
- $targetAdmin->expects($this->once())
- ->method('getDatagrid')
- ->with()
- ->will($this->returnValue($datagrid));
- $metadata = $this->createMock('Sonata\CoreBundle\Model\Metadata');
- $metadata->expects($this->once())
- ->method('getTitle')
- ->with()
- ->will($this->returnValue('FOO'));
- $targetAdmin->expects($this->once())
- ->method('getObjectMetadata')
- ->with($entity)
- ->will($this->returnValue($metadata));
- $datagrid->expects($this->once())
- ->method('hasFilter')
- ->with('foo')
- ->will($this->returnValue(true));
- $datagrid->expects($this->exactly(3))
- ->method('setValue')
- ->withConsecutive(
- array($this->equalTo('foo'), $this->equalTo(null), $this->equalTo('sonata')),
- array($this->equalTo('_per_page'), $this->equalTo(null), $this->equalTo(10)),
- array($this->equalTo('_page'), $this->equalTo(null), $this->equalTo(1))
- )
- ->will($this->returnValue(null));
- $datagrid->expects($this->once())
- ->method('buildPager')
- ->with()
- ->will($this->returnValue(null));
- $pager = $this->createMock('Sonata\AdminBundle\Datagrid\Pager');
- $datagrid->expects($this->once())
- ->method('getPager')
- ->with()
- ->will($this->returnValue($pager));
- $pager->expects($this->once())
- ->method('getResults')
- ->with()
- ->will($this->returnValue(array($entity)));
- $pager->expects($this->once())
- ->method('isLastPage')
- ->with()
- ->will($this->returnValue(true));
- $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription->expects($this->once())
- ->method('getTargetEntity')
- ->will($this->returnValue('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'));
- $fieldDescription->expects($this->once())
- ->method('getName')
- ->will($this->returnValue('barField'));
- $fieldDescription->expects($this->once())
- ->method('getAssociationAdmin')
- ->will($this->returnValue($targetAdmin));
- $this->admin->expects($this->once())
- ->method('getFormFieldDescriptions')
- ->will($this->returnValue(null));
- $this->admin->expects($this->once())
- ->method('getFormFieldDescription')
- ->with('barField')
- ->will($this->returnValue($fieldDescription));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $formType = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $form->expects($this->once())
- ->method('get')
- ->with('barField')
- ->will($this->returnValue($formType));
- $formConfig = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
- ->disableOriginalConstructor()
- ->getMock();
- $formType->expects($this->once())
- ->method('getConfig')
- ->will($this->returnValue($formConfig));
- $formConfig->expects($this->any())
- ->method('getAttribute')
- ->will($this->returnCallback(function ($name, $default = null) {
- switch ($name) {
- case 'property':
- return 'foo';
- case 'callback':
- return;
- case 'minimum_input_length':
- return 3;
- case 'items_per_page':
- return 10;
- case 'req_param_name_page_number':
- return '_page';
- case 'to_string_callback':
- return;
- case 'disabled':
- return false;
- case 'target_admin_access_action':
- return 'list';
- default:
- throw new \RuntimeException(sprintf('Unkown parameter "%s" called.', $name));
- }
- }));
- $request = new Request(array(
- 'admin_code' => 'foo.admin',
- 'field' => 'barField',
- 'q' => 'sonata',
- ), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
- $response = $this->controller->retrieveAutocompleteItemsAction($request);
- $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame('application/json', $response->headers->get('Content-Type'));
- $this->assertSame('{"status":"OK","more":false,"items":[{"id":123,"label":"FOO"}]}', $response->getContent());
- }
- /**
- * Symfony Validator has 2 API version (2.4 and 2.5)
- * This data provider ensure tests pass on each one.
- */
- public function getValidatorInterfaces()
- {
- $data = array();
- // For Symfony <= 2.8
- if (interface_exists('Symfony\Component\Validator\ValidatorInterface')) {
- $data['2.4'] = array('Symfony\Component\Validator\ValidatorInterface');
- }
- // For Symfony >= 2.5
- if (interface_exists('Symfony\Component\Validator\Validator\ValidatorInterface')) {
- $data['2.5'] = array('Symfony\Component\Validator\Validator\ValidatorInterface');
- }
- return $data;
- }
- }
|