/**
  * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  */
 public function testRetrieveAutocompleteItemsActionNotGrantedTarget()
 {
     $this->admin->expects($this->once())->method('isGranted')->with('CREATE')->will($this->returnValue(true));
     $entity = new Foo();
     $fieldDescription = $this->getMock('Sonata\\AdminBundle\\Admin\\FieldDescriptionInterface');
     $fieldDescription->expects($this->once())->method('getType')->will($this->returnValue('sonata_type_model_autocomplete'));
     $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'));
     $targetAdmin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($targetAdmin));
     $targetAdmin->expects($this->once())->method('isGranted')->with('LIST')->will($this->returnValue(false));
     $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->any())->method('getConfig')->will($this->returnValue($formConfig));
     $formConfig->expects($this->any())->method('getAttribute')->will($this->returnCallback(function ($name) {
         switch ($name) {
             case 'disabled':
                 return false;
             case 'property':
                 return 'fooProperty';
             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;
         }
         return;
     }));
     $request = new Request(array('code' => 'foo.admin', 'field' => 'barField'), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'));
     $this->controller->retrieveAutocompleteItemsAction($request);
 }
 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);
 }
 public function testSetObjectFieldValueActionWithViolations()
 {
     $bar = new AdminControllerHelper_Bar();
     $object = new AdminControllerHelper_Foo();
     $object->setBar($bar);
     $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));
     $twig = new Twig();
     $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');
     $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->getMock('Symfony\\Component\\Validator\\ValidatorInterface');
     $validator->expects($this->once())->method('validateProperty')->with($bar, 'enabled')->will($this->returnValue($violations));
     $controller = new HelperController($twig, $pool, $helper, $validator);
     $response = $controller->setObjectFieldValueAction($request);
     $this->assertEquals('{"status":"KO","message":"error1\\nerror2"}', $response->getContent());
 }