Skip to content

Commit

Permalink
Merge pull request #26 from TimGroenenberg/custom-locale
Browse files Browse the repository at this point in the history
Allow render of templates with specific locale
  • Loading branch information
Tim Groenenberg authored May 22, 2019
2 parents 75dbe70 + 5a42204 commit 456b446
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
31 changes: 27 additions & 4 deletions Controller/CookieConsentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
use ConnectHolland\CookieConsentBundle\Form\CookieConsentType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Translation\TranslatorInterface;

class CookieConsentController
{
Expand All @@ -38,25 +40,34 @@ class CookieConsentController
*/
private $cookieConsentTheme;

/**
* @var TranslatorInterface
*/
private $translator;

public function __construct(
\Twig_Environment $twigEnvironment,
FormFactoryInterface $formFactory,
CookieChecker $cookieChecker,
string $cookieConsentTheme
string $cookieConsentTheme,
TranslatorInterface $translator
) {
$this->twigEnvironment = $twigEnvironment;
$this->formFactory = $formFactory;
$this->cookieChecker = $cookieChecker;
$this->cookieConsentTheme = $cookieConsentTheme;
$this->translator = $translator;
}

/**
* Show cookie consent.
*
* @Route("/cookie_consent", name="ch_cookie_consent.show")
*/
public function show(): Response
public function show(Request $request): Response
{
$this->setLocale($request);

return new Response(
$this->twigEnvironment->render('@CHCookieConsent/cookie_consent.html.twig', [
'form' => $this->createCookieConsentForm()->createView(),
Expand All @@ -72,10 +83,10 @@ public function show(): Response
*
* @Route("/cookie_consent_alt", name="ch_cookie_consent.show_if_cookie_consent_not_set")
*/
public function showIfCookieConsentNotSet(): Response
public function showIfCookieConsentNotSet(Request $request): Response
{
if ($this->cookieChecker->isCookieConsentSavedByUser() === false) {
return $this->show();
return $this->show($request);
}

return new Response();
Expand All @@ -88,4 +99,16 @@ protected function createCookieConsentForm(): FormInterface
{
return $this->formFactory->create(CookieConsentType::class);
}

/**
* Set locale if available as GET parameter.
*/
protected function setLocale(Request $request)
{
$locale = $request->get('locale');
if (empty($locale) === false) {
$this->translator->setLocale($locale);
$request->setLocale($locale);
}
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ Load the cookie consent in Twig via render_esi ( to prevent caching ) at any pla
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set')) }}
```

If you want to load the cookie consent with a specific locale you can pass the locale as a parameter:
```twig
{{ render_esi(path('ch_cookie_consent.show', { 'locale' : 'en' })) }}
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set', { 'locale' : app.request.locale })) }}
```

### Cookies
When a user submits the form the preferences are saved as cookies. The cookies have a lifetime of 1 year. The following cookies are saved:
- **Cookie_Consent**: date of submit
Expand Down
4 changes: 2 additions & 2 deletions Resources/translations/CHCookieConsentBundle.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ ch_cookie_consent:
social_media:
title: 'Do you want social media cookies?'
description: ''
no: 'Nee'
yes: 'Ja'
no: 'No'
yes: 'Yes'
save: 'Save'
55 changes: 52 additions & 3 deletions Tests/Controller/CookieConsentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;

class CookieConsentControllerTest extends TestCase
{
Expand All @@ -36,6 +37,11 @@ class CookieConsentControllerTest extends TestCase
*/
private $cookieChecker;

/**
* @var MockObject
*/
private $translator;

/**
* @var CookieConsentController
*/
Expand All @@ -46,7 +52,8 @@ public function setUp()
$this->templating = $this->createMock(\Twig_Environment::class);
$this->formFactory = $this->createMock(FormFactoryInterface::class);
$this->cookieChecker = $this->createMock(CookieChecker::class);
$this->cookieConsentController = new CookieConsentController($this->templating, $this->formFactory, $this->cookieChecker, 'dark');
$this->translator = $this->createMock(TranslatorInterface::class);
$this->cookieConsentController = new CookieConsentController($this->templating, $this->formFactory, $this->cookieChecker, 'dark', $this->translator);
}

public function testShow()
Expand All @@ -62,7 +69,7 @@ public function testShow()
->method('render')
->willReturn('test');

$response = $this->cookieConsentController->show();
$response = $this->cookieConsentController->show(new Request());

$this->assertInstanceOf(Response::class, $response);
}
Expand All @@ -85,7 +92,49 @@ public function testShowIfCookieConsentNotSet()
->method('render')
->willReturn('test');

$response = $this->cookieConsentController->showIfCookieConsentNotSet();
$response = $this->cookieConsentController->showIfCookieConsentNotSet(new Request());

$this->assertInstanceOf(Response::class, $response);
}

public function testShowIfCookieConsentNotSetWithLocale()
{
$this->cookieChecker
->expects($this->once())
->method('isCookieConsentSavedByUser')
->willReturn(false);

$this->formFactory
->expects($this->once())
->method('create')
->with(CookieConsentType::class)
->willReturn($this->createMock(FormInterface::class));

$this->templating
->expects($this->once())
->method('render')
->willReturn('test');

$request = $this->createMock(Request::class);
$locale = 'en';

$request
->expects($this->once())
->method('get')
->with('locale')
->willReturn($locale);

$this->translator
->expects($this->once())
->method('setLocale')
->with($locale);

$request
->expects($this->once())
->method('setLocale')
->with($locale);

$response = $this->cookieConsentController->showIfCookieConsentNotSet($request);

$this->assertInstanceOf(Response::class, $response);
}
Expand Down

0 comments on commit 456b446

Please sign in to comment.