Example #1
0
 /**
  * {@inheritdoc}
  */
 public function isGranted($action, $object)
 {
     if (!$this->parameterResolver->resolveVoter()) {
         return true;
     }
     return $this->authorizationChecker->isGranted('lug.' . $action, $object);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $resolver->setDefaults(['constraints' => function (Options $options) {
         return new GridSortingConstraint(['grid' => $options['grid']]);
     }, 'error_bubbling' => function (Options $options) {
         return !$this->parameterResolver->resolveApi();
     }])->setRequired('grid')->setAllowedTypes('grid', GridInterface::class);
 }
Example #3
0
 public function testLocalesWithApiRequestWithoutAcceptLanguageHeader()
 {
     $request = $this->createRequestMock();
     $request->headers->expects($this->once())->method('get')->with($this->identicalTo('Accept-Language'))->will($this->returnValue(null));
     $request->expects($this->once())->method('getLocale')->will($this->returnValue($locale = 'en'));
     $this->requestStack->expects($this->once())->method('getMasterRequest')->will($this->returnValue($request));
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $this->assertSame([$locale], $this->localeContext->getLocales());
 }
 public function testActionWithoutForm()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(false));
     $event = $event = $this->createActionEventMock();
     $event->expects($this->once())->method('getForm')->will($this->returnValue(null));
     $this->parameterResolver->expects($this->once())->method('resolveRedirectRoute')->will($this->returnValue($route = 'route'));
     $event->expects($this->once())->method('setView')->with($this->callback(function (View $view) use($route) {
         return $view->getRoute() === $route && $view->getRouteParameters() === [];
     }));
     $this->subscriber->onAction($event);
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function getLocales()
 {
     if (($request = $this->requestStack->getMasterRequest()) === null) {
         return [$this->localeProvider->getDefaultLocale()->getCode()];
     }
     $locales = [];
     if ($this->parameterResolver->resolveApi()) {
         $locales = $this->parseAcceptLanguage($request);
     }
     if (empty($locales)) {
         $locales = [$request->getLocale()];
     }
     return $locales;
 }
Example #6
0
 public function testMessageWithApi()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $event = $this->createDomainEventMock();
     $event->expects($this->never())->method('setMessage');
     $this->messageListener->addMessage($event);
 }
Example #7
0
 /**
  * @param string|FormTypeInterface|ResourceInterface $type
  * @param mixed                                      $data
  * @param mixed[]                                    $options
  *
  * @return FormInterface
  */
 public function create($type = null, $data = null, array $options = [])
 {
     if ($type instanceof ResourceInterface) {
         $type = $this->parameterResolver->resolveForm($type);
     }
     $validationGroups = $this->parameterResolver->resolveValidationGroups();
     $translationDomain = $this->parameterResolver->resolveTranslationDomain();
     if (!empty($validationGroups)) {
         $options['validation_groups'] = $validationGroups;
     }
     if (!empty($translationDomain)) {
         $options['translation_domain'] = $translationDomain;
     }
     if ($this->parameterResolver->resolveApi()) {
         $options['csrf_protection'] = false;
     }
     return $this->factory->create($type, $data, $options);
 }
 public function testViewWithPagerfanta()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(false));
     $event = $this->createViewEventMock();
     $event->expects($this->once())->method('getView')->will($this->returnValue($view = $this->createViewMock()));
     $view->expects($this->once())->method('getData')->will($this->returnValue($this->createPagerfantaMock()));
     $event->expects($this->once())->method('getResource')->will($this->returnValue($resource = $this->createResourceMock()));
     $resource->expects($this->once())->method('getName')->will($this->returnValue($name = 'name'));
     $view->expects($this->once())->method('setTemplateVar')->with($this->identicalTo($name . 's'));
     $this->subscriber->onView($event);
 }
Example #9
0
 /**
  * {@inheritdoc}
  */
 public function batch($data, array $options)
 {
     if (!is_array($data) && !$data instanceof \Traversable) {
         return;
     }
     $grid = $options['grid'];
     $domainManager = $this->domainManagerRegistry[$grid->getResource()->getName()];
     $api = $this->parameterResolver->resolveApi();
     foreach ($data as $object) {
         try {
             $domainManager->delete($object, !$api);
         } catch (DomainException $e) {
             if ($api) {
                 throw $e;
             }
         }
     }
     if ($api) {
         $domainManager->flush();
     }
 }
Example #10
0
 /**
  * @param DomainEvent $event
  */
 public function addMessage(DomainEvent $event)
 {
     if ($this->parameterResolver->resolveApi()) {
         return;
     }
     $messageType = $event->getMessageType();
     $message = $event->getMessage();
     if (empty($messageType)) {
         $messageType = $event->isStopped() ? 'error' : 'success';
     }
     if (empty($message)) {
         $data = $event->getData();
         $resource = $event->getResource();
         $name = $resource->getName();
         $labelPropertyPath = $resource->getLabelPropertyPath();
         $property = $labelPropertyPath !== null ? $this->propertyAccessor->getValue($data, $labelPropertyPath) : (string) $data;
         $message = $this->translator->trans('lug.' . $name . '.' . $event->getAction() . '.' . $messageType, ['%' . $name . '%' => $property], 'flashes');
     }
     $event->setMessageType($messageType);
     $event->setMessage($message);
 }
Example #11
0
 public function testViewWithForms()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(false));
     $event = $this->createViewEventMock();
     $event->expects($this->once())->method('getView')->will($this->returnValue($view = $this->createViewMock()));
     $view->expects($this->once())->method('getData')->will($this->returnValue([$object = new \stdClass(), $form = $this->createFormMock()]));
     $form->expects($this->once())->method('createView')->will($this->returnValue($formView = $this->createFormViewMock()));
     $this->parameterResolver->expects($this->once())->method('resolveThemes')->will($this->returnValue($themes = ['theme']));
     $this->formRenderer->expects($this->once())->method('setTheme')->with($this->identicalTo($formView), $this->identicalTo($themes));
     $view->expects($this->once())->method('setData')->with($this->identicalTo([$object, $formView]));
     $this->subscriber->onView($event);
 }
 public function testViewWithTemplateVar()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(false));
     $event = $this->createViewEventMock();
     $event->expects($this->once())->method('getView')->will($this->returnValue($view = $this->createViewMock()));
     $view->expects($this->once())->method('getData')->will($this->returnValue($data = new \stdClass()));
     $this->parameterResolver->expects($this->once())->method('resolveTemplate')->will($this->returnValue($template = 'template'));
     $view->expects($this->once())->method('setTemplate')->with($this->identicalTo($template))->will($this->returnSelf());
     $event->expects($this->once())->method('getResource')->will($this->returnValue($resource = $this->createResourceMock()));
     $view->expects($this->once())->method('getTemplateVar')->will($this->returnValue($templateVar = 'template_var'));
     $view->expects($this->once())->method('setData')->with($this->identicalTo([$templateVar => $data, 'resource' => $resource]));
     $this->subscriber->onView($event);
 }
Example #13
0
 public function testActionWithValidFormAndOkStatusCode()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $event = $this->createActionEventMock();
     $event->expects($this->once())->method('getForm')->will($this->returnValue($form = $this->createFormMock()));
     $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
     $form->expects($this->once())->method('getData')->will($this->returnValue($data = new \stdClass()));
     $event->expects($this->once())->method('getStatusCode')->will($this->returnValue($statusCode = Response::HTTP_OK));
     $event->expects($this->once())->method('setView')->with($this->callback(function (View $view) use($statusCode, $data) {
         return $view->getStatusCode() === $statusCode && $view->getData() === $data;
     }));
     $this->subscriber->onAction($event);
 }
Example #14
0
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $idPropertyPath = function (Options $options) {
         $propertyPath = $options['grid']->getDefinition()->getResource()->getIdPropertyPath();
         return function ($choice) use($propertyPath) {
             return $this->propertyAccessor->getValue($choice, $propertyPath);
         };
     };
     $labelPropertyPath = function (Options $options) {
         $propertyPath = $options['grid']->getDefinition()->getResource()->getLabelPropertyPath();
         return function ($choice) use($propertyPath) {
             return $this->propertyAccessor->getValue($choice, $propertyPath);
         };
     };
     $resolver->setDefaults(['multiple' => true, 'translation_domain' => false, 'choices' => [], 'choices_as_values' => true, 'choice_name' => $idPropertyPath, 'choice_value' => $idPropertyPath, 'choice_label' => $labelPropertyPath, 'class' => function (Options $options) {
         return $options['grid']->getDefinition()->getResource()->getModel();
     }, 'expanded' => function (Options $options) {
         return !$this->parameterResolver->resolveApi();
     }, 'constraints' => function (Options $options) {
         $resource = $options['grid']->getDefinition()->getResource();
         return [new Count(['min' => 1, 'minMessage' => 'lug.' . $resource->getName() . '.batch.empty'])];
     }])->setRequired('grid')->setAllowedTypes('grid', GridViewInterface::class);
 }
Example #15
0
 public function testViewFormThemes()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(false));
     $this->parameterResolver->expects($this->once())->method('resolveThemes')->will($this->returnValue($themes = ['theme']));
     $event = $this->createViewEventMock();
     $event->expects($this->once())->method('getView')->will($this->returnValue($view = $this->createViewMock()));
     $view->expects($this->once())->method('getData')->will($this->returnValue(['grid' => $gridView = $this->createGridViewMock(), 'batch_form' => $batchForm = $this->createFormMock()]));
     $batchForm->expects($this->once())->method('createView')->will($this->returnValue($batchFormView = $this->createFormViewMock()));
     $gridView->expects($this->once())->method('setBatchForm')->with($this->identicalTo($batchFormView));
     $gridView->expects($this->once())->method('getForm')->will($this->returnValue($formView = $this->createFormViewMock()));
     $gridView->expects($this->exactly(2))->method('getBatchForm')->willReturnOnConsecutiveCalls(null, $batchFormView);
     $this->formRenderer->expects($this->exactly(2))->method('setTheme')->withConsecutive([$formView, $themes], [$batchFormView, $themes]);
     $view->expects($this->once())->method('setTemplateVar')->with($this->identicalTo('grid'))->will($this->returnSelf());
     $view->expects($this->once())->method('setData')->with($this->identicalTo($gridView));
     $this->subscriber->onView($event);
 }
Example #16
0
 public function testBatchWithApiAndDomainException()
 {
     $data = [$object = new \stdClass()];
     $grid = $this->createGridMock();
     $grid->expects($this->once())->method('getResource')->will($this->returnValue($resource = $this->createResourceMock()));
     $resource->expects($this->once())->method('getName')->will($this->returnValue($name = 'name'));
     $this->domainManagerRegistry->expects($this->once())->method('offsetGet')->with($this->identicalTo($name))->will($this->returnValue($domainManager = $this->createDomainManagerMock()));
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $domainManager->expects($this->once())->method('delete')->with($this->identicalTo($object), $this->isFalse())->will($this->throwException($exception = $this->createDomainExceptionMock()));
     try {
         $this->type->batch($data, ['grid' => $grid]);
         $this->fail();
     } catch (DomainException $e) {
         $this->assertSame($exception, $e);
     }
 }
Example #17
0
 public function testType()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $form = $this->formFactory->create(CheckboxType::class);
     $this->assertSame(BooleanExtension::TYPE_API, $form->getConfig()->getOption('type'));
 }
Example #18
0
 public function testCreateWithApi()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $this->symfonyFormFactory->expects($this->once())->method('create')->with($this->identicalTo($type = 'type'), $this->identicalTo($data = 'data'), $this->identicalTo(array_merge($options = ['option'], ['csrf_protection' => false])))->will($this->returnValue($form = 'form'));
     $this->assertSame($form, $this->formFactory->create($type, $data, $options));
 }
Example #19
0
 public function testFallbackLocaleWithApiRequest()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $this->assertNull($this->localeContext->getFallbackLocale());
 }
Example #20
0
 public function testIsGrantedWithVoter()
 {
     $this->parameterResolver->expects($this->once())->method('resolveVoter')->will($this->returnValue(true));
     $this->authorizationChecker->expects($this->once())->method('isGranted')->with($this->identicalTo('lug.' . ($action = 'show')), $this->identicalTo($object = $this->createStdClassMock()))->will($this->returnValue(true));
     $this->assertTrue($this->securityChecker->isGranted($action, $object));
 }
Example #21
0
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     parent::configureOptions($resolver);
     $resolver->setDefaults(['base64' => $this->parameterResolver->resolveApi()]);
 }
 /**
  * @param FormEvent $event
  */
 public function onPreSetData(FormEvent $event)
 {
     $form = $event->getForm();
     $grid = $form->getConfig()->getOption('grid');
     $this->buildForm($form, !$this->parameterResolver->resolveApi() ? iterator_to_array($grid->getDataSource()) : []);
 }
 public function testResolveVoter()
 {
     $this->parameterResolver->expects($this->once())->method('resolveVoter')->will($this->returnValue(true));
     $this->assertTrue($this->cachedParameterResolver->resolveVoter());
     $this->assertTrue($this->cachedParameterResolver->resolveVoter());
 }
Example #24
0
 /**
  * @param DomainEvent $event
  */
 public function addFlash(DomainEvent $event)
 {
     if (!$this->parameterResolver->resolveApi()) {
         $this->session->getFlashBag()->add($event->getMessageType(), $event->getMessage());
     }
 }
Example #25
0
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     parent::configureOptions($resolver);
     $resolver->setDefault('type', $this->parameterResolver->resolveApi() ? self::TYPE_API : self::TYPE_CHECKBOX);
 }
Example #26
0
 /**
  * {@inheritdoc}
  */
 public function getFallbackLocale()
 {
     if (!$this->parameterResolver->resolveApi()) {
         return $this->localeProvider->getDefaultLocale()->getCode();
     }
 }
Example #27
0
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $resolver->setDefault('api', $this->parameterResolver->resolveApi())->setAllowedTypes('api', 'bool');
 }
Example #28
0
 public function testFlashWithApi()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $this->session->expects($this->never())->method('getFlashBag');
     $this->flashListener->addFlash($this->createDomainEventMock());
 }
 public function testResolveValidationGroups()
 {
     $this->parameterResolver->expects($this->once())->method('resolveValidationGroups')->will($this->returnValue($groups = ['group']));
     $this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups());
     $this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups());
 }
Example #30
0
 public function testType()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $form = $this->formFactory->create(FileType::class);
     $this->assertTrue($form->getConfig()->getOption('base64'));
 }