123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /*
- * This file is part of the Sonata 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\Pool;
- use Sonata\AdminBundle\Controller\HelperController;
- use \Twig_Environment as Twig;
- use \Twig_ExtensionInterface as Twig_ExtensionInterface;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
- use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
- use Symfony\Component\Form\Form;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Form\FormBuilder;
- use Symfony\Component\Form\FormFactoryInterface;
- use Symfony\Component\Form\FormView;
- class AdminControllerHelper_Foo
- {
- public function getAdminTitle()
- {
- return 'bar';
- }
- public function setEnabled($value)
- {
- }
- }
- class AdminHelperTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
- */
- public function testgetShortObjectDescriptionActionInvalidAdmin()
- {
- $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $twig = new Twig;
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'uniqid' => 'asdasd123'
- ));
- $pool = new Pool($container, 'title', 'logo');
- $helper = new AdminHelper($pool);
- $controller = new HelperController($twig, $pool, $helper);
- $controller->getShortObjectDescriptionAction($request);
- }
- public function testgetShortObjectDescriptionActionObjectDoesNotExist()
- {
- $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $admin->expects($this->once())->method('setUniqid');
- $admin->expects($this->once())->method('getObject')->will($this->returnValue(false));
- $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $twig = new Twig;
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'uniqid' => 'asdasd123'
- ));
- $pool = new Pool($container, 'title', 'logo');
- $helper = new AdminHelper($pool);
- $controller = new HelperController($twig, $pool, $helper);
- $response = $controller->getShortObjectDescriptionAction($request);
- $this->assertEmpty($response->getContent());
- }
- public function testgetShortObjectDescriptionActionObject()
- {
- $mockTemplate = 'AdminHelperTest:mock-short-object-description.html.twig';
-
- $admin = $this->getMock('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('generateUrl')->will($this->returnCallback(function($name, $parameters) {
- if ($name != 'edit') {
- return 'invalid name';
- }
- if (!isset($parameters['id'])) {
- return 'id parameter not set';
- }
- return '/ok/url';
- }));
- $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $twig = $this->getMock('Twig_Environment');
-
- $twig->expects($this->once())->method('render')
- ->with($mockTemplate)
- ->will($this->returnCallback(function($templateName, $templateParams) {
- return sprintf('<a href="%s" target="new">%s</a>', $templateParams['url'], $templateParams['description']);
- }));
-
- $request = new Request(array(
- 'code' => 'sonata.post.admin',
- 'objectId' => 42,
- 'uniqid' => 'asdasd123'
- ));
- $pool = new Pool($container, 'title', 'logo');
- $helper = new AdminHelper($pool);
- $controller = new HelperController($twig, $pool, $helper);
- $response = $controller->getShortObjectDescriptionAction($request);
- $expected = '<a href="/ok/url" target="new">bar</a>';
- $this->assertEquals($expected, $response->getContent());
- }
- public function testsetObjectFieldValueAction()
- {
- $object = new AdminControllerHelper_Foo;
- $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription->expects($this->once())->method('getOption')->will($this->returnValue(true));
- $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $admin->expects($this->once())->method('getObject')->will($this->returnValue($object));
- $admin->expects($this->once())->method('isGranted')->will($this->returnValue(true));
- $admin->expects($this->once())->method('getListFieldDescription')->will($this->returnValue($fieldDescription));
- $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $adminExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName'));
- $adminExtension->expects($this->once())->method('getName')->will($this->returnValue('sonata_admin'));
- $adminExtension->expects($this->once())->method('renderListElement')->will($this->returnValue('<foo />'));
- $twig = new Twig;
- $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'));
- $pool = new Pool($container, 'title', 'logo');
- $helper = new AdminHelper($pool);
- $controller = new HelperController($twig, $pool, $helper);
- $response = $controller->setObjectFieldValueAction($request);
- $this->assertEquals('{"status":"OK","content":"<foo \/>"}', $response->getContent() );
- }
- public function testappendFormFieldElementAction()
- {
- $object = new AdminControllerHelper_Foo;
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
- $admin = $this->getMock('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');
- $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $formExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName', 'setTheme', 'renderWidget'));
- $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
- $formExtension->expects($this->once())->method('renderWidget')->will($this->returnValue(new Response));
- $formExtension->expects($this->once())->method('setTheme');
- $twig = new Twig;
- $twig->addExtension($formExtension);
- $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');
- $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
- $form = new Form('foo', $dispatcher);
- $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('appendFormFieldElement'), array($pool));
- $helper->expects($this->once())->method('appendFormFieldElement')->will($this->returnValue(array(
- $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface'),
- $form
- )));
- $controller = new HelperController($twig, $pool, $helper);
- $response = $controller->appendFormFieldElementAction($request);
- $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- }
- public function testretrieveFormFieldElementAction()
- {
- $object = new AdminControllerHelper_Foo;
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager->expects($this->once())->method('find')->will($this->returnValue($object));
- $formBuilder = new FormBuilder(
- 'foo',
- $this->getMock('Symfony\Component\Form\FormFactoryInterface'),
- $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
- );
- $admin = $this->getMock('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->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $container->expects($this->any())->method('get')->will($this->returnValue($admin));
- $formExtension = $this->getMock('Twig_ExtensionInterface', array('renderListElement', 'initRuntime', 'getTokenParsers', 'getNodeVisitors', 'getFilters', 'getTests', 'getFunctions', 'getOperators', 'getGlobals', 'getName', 'setTheme', 'renderWidget'));
- $formExtension->expects($this->once())->method('getName')->will($this->returnValue('form'));
- $formExtension->expects($this->once())->method('renderWidget')->will($this->returnValue(new Response));
- $formExtension->expects($this->once())->method('setTheme');
- $twig = new Twig;
- $twig->addExtension($formExtension);
- $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');
- $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
- $formView = new FormView();
- $helper = $this->getMock('Sonata\AdminBundle\Admin\AdminHelper', array('getChildFormView'), array($pool));
- $helper->expects($this->once())->method('getChildFormView')->will($this->returnValue($formView));
- $controller = new HelperController($twig, $pool, $helper);
- $response = $controller->retrieveFormFieldElementAction($request);
- $this->isInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- }
- }
|