public function testRequiredLocales() { $locale = $this->createLocaleMock(); $locale->expects($this->once())->method('getCode')->will($this->returnValue($code = 'fr')); $this->localeProvider->expects($this->once())->method('getRequiredLocales')->will($this->returnValue([$locale])); $this->assertSame([$code], $this->a2lixLocaleProvider->getRequiredLocales()); }
public function testValidateNotDefaultLocale() { $this->localeProvider->expects($this->once())->method('getDefaultLocale')->will($this->returnValue($this->createLocaleMock())); $domainEvent = $this->createDomainEventMock(); $domainEvent->expects($this->once())->method('getData')->will($this->returnValue($this->createLocaleMock())); $domainEvent->expects($this->never())->method('setStopped'); $domainEvent->expects($this->never())->method('setStatusCode'); $domainEvent->expects($this->never())->method('setMessageType'); $domainEvent->expects($this->never())->method('setMessage'); $this->localeDomainSubscriber->validateDefaultLocale($domainEvent); }
/** * @param DomainEvent $event */ public function validateDefaultLocale(DomainEvent $event) { $data = $event->getData(); if ($data !== $this->localeProvider->getDefaultLocale()) { return; } $resource = $event->getResource(); $event->setStopped(true); $event->setStatusCode(409); $event->setMessageType('error'); $event->setMessage($this->translator->trans('lug.' . $resource->getName() . '.' . $event->getAction() . '.default', ['%' . $resource->getName() . '%' => $this->propertyAccessor->getValue($data, $resource->getLabelPropertyPath())], 'flashes')); }
/** * @param LocaleInterface $locale * @param LocaleIntegrity $constraint */ private function validateLastLocale(LocaleInterface $locale, LocaleIntegrity $constraint) { $locales = $this->localeProvider->getRequiredLocales(); if (count($locales) === 1 && reset($locales) === $locale) { if (!$locale->isEnabled()) { $this->context->buildViolation($constraint->lastEnabledMessage)->atPath('enabled')->addViolation(); } if (!$locale->isRequired()) { $this->context->buildViolation($constraint->lastRequiredMessage)->atPath('required')->addViolation(); } } }
/** * {@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; }
public function testValidateWithLastLocaleNotRequired() { $locale = $this->createLocaleMock(); $locale->expects($this->exactly(2))->method('isEnabled')->will($this->returnValue(true)); $locale->expects($this->exactly(2))->method('isRequired')->will($this->returnValue(false)); $this->localeProvider->expects($this->once())->method('getDefaultLocale')->will($this->returnValue($this->createLocaleMock())); $this->localeProvider->expects($this->once())->method('getRequiredLocales')->will($this->returnValue([$locale])); $errors = $this->validator->validate($locale); $this->assertCount(1, $errors); $this->assertArrayHasKey(0, $errors); $this->assertSame('required', $errors[0]->getPropertyPath()); $this->assertSame('The locale is the last required. You can\'t make it optional.', $errors[0]->getMessage()); }
public function testSubmitDefaultLocale() { $this->localeProvider->expects($this->once())->method('getDefaultLocale')->will($this->returnValue($defaultLocale = $this->createLocaleMock())); $defaultLocale->expects($this->exactly(2))->method('getCode')->will($this->returnValue($defaultLocaleCode = 'fr')); $defaultLocale->expects($this->never())->method('setCode'); $defaultLocale->expects($this->never())->method('setEnabled'); $defaultLocale->expects($this->never())->method('setRequired'); $this->resource->expects($this->once())->method('getModel')->will($this->returnValue(get_class($defaultLocale))); $this->repository->expects($this->once())->method('findAll')->will($this->returnValue([$defaultLocale])); $form = $this->formFactory->create(LocaleType::class, $defaultLocale)->submit(['code' => 'USD', 'enabled' => false, 'required' => false]); $view = $form->createView(); $this->assertCount(4, $view->children); $this->assertArrayHasKey('code', $view->children); $this->assertArrayHasKey('enabled', $view->children); $this->assertArrayHasKey('required', $view->children); $this->assertArrayHasKey('submit', $view->children); $this->assertArrayHasKey('disabled', $view->children['code']->vars); $this->assertTrue($view->children['code']->vars['disabled']); $this->assertArrayHasKey('disabled', $view->children['enabled']->vars); $this->assertTrue($view->children['enabled']->vars['disabled']); $this->assertArrayHasKey('disabled', $view->children['required']->vars); $this->assertTrue($view->children['required']->vars['disabled']); }
/** * {@inheritdoc} */ public function getFallbackLocale() { if (!$this->parameterResolver->resolveApi()) { return $this->localeProvider->getDefaultLocale()->getCode(); } }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $childOptions = ['disabled' => $builder->getData() === $this->localeProvider->getDefaultLocale()]; $builder->add('code', LocaleCodeType::class, $childOptions)->add('enabled', CheckboxType::class, array_merge(['invalid_message' => 'lug.locale.enabled.invalid'], $childOptions))->add('required', CheckboxType::class, array_merge(['invalid_message' => 'lug.locale.required.invalid'], $childOptions))->add('submit', SubmitType::class); }
/** * {@inheritdoc} */ public function getRequiredLocales() { return array_map($this->getCallback(), $this->localeProvider->getRequiredLocales()); }
public function testLocalesWithoutRequest() { $this->localeProvider->expects($this->once())->method('getDefaultLocale')->will($this->returnValue($locale = $this->createLocaleMock())); $locale->expects($this->once())->method('getCode')->will($this->returnValue($code = 'en')); $this->assertSame([$code], $this->localeContext->getLocales()); }