Skip to content

Commit

Permalink
Merge pull request from GHSA-vx6j-pjrh-vgjh
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Dec 7, 2021
1 parent bb892d7 commit 9c948f9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
Expand Up @@ -44,7 +44,10 @@ public function provide($entity, $name)
$this->translator->setLocale($entity->getLocale());
}

$result = $this->expressionLanguage->evaluate($name, ['object' => $entity, 'translator' => $this->translator]);
$result = $this->expressionLanguage->evaluate($name, [
'object' => $entity,
'translator' => new TranslatorWrapper($this->translator),
]);
$this->translator->setLocale($locale);

return $result;
Expand Down
41 changes: 41 additions & 0 deletions src/Sulu/Bundle/RouteBundle/Generator/TranslatorWrapper.php
@@ -0,0 +1,41 @@
<?php

namespace Sulu\Bundle\RouteBundle\Generator;

use Symfony\Component\Translation\TranslatorInterface;

/**
* @internal
*/
class TranslatorWrapper implements TranslatorInterface
{
/**
* @var TranslatorInterface
*/
private $translator;

public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
return $this->translator->trans($id, $parameters, $domain, $locale);
}

public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}

public function setLocale($locale)
{
throw new \Exception('Not supported.');
}

public function getLocale()
{
return $this->translator->getLocale();
}
}
Expand Up @@ -11,9 +11,11 @@

namespace Sulu\Bundle\RouteBundle\Tests\Unit\Generator;

use Prophecy\Argument;
use Sulu\Bundle\RouteBundle\Generator\CannotEvaluateTokenException;
use Sulu\Bundle\RouteBundle\Generator\SymfonyExpressionTokenProvider;
use Sulu\Bundle\RouteBundle\Model\RoutableInterface;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\TranslatorInterface;

class SymfonyExpressionTokenProviderTest extends \PHPUnit_Framework_TestCase
Expand All @@ -37,14 +39,31 @@ public function testResolveTranslation()
$translator = $this->prophesize(TranslatorInterface::class);
$translator->getLocale()->willReturn('de');
$translator->setLocale('de')->shouldBeCalled();
$translator->trans('test-key')->willReturn('TEST');
$translator->trans('test-key', Argument::cetera())->willReturn('TEST');
$entity = $this->prophesize(RoutableInterface::class);
$entity->getLocale()->willReturn('en');

$provider = new SymfonyExpressionTokenProvider($translator->reveal());
$this->assertEquals('TEST', $provider->provide($entity, 'translator.trans("test-key")'));
}

public function testResolveTranslationAddResource()
{
$this->setExpectedException(CannotEvaluateTokenException::class);

$translator = $this->prophesize(Translator::class);
$translator->getLocale()->willReturn('de');
$translator->setLocale('de')->shouldBeCalled();
$entity = $this->prophesize(RoutableInterface::class);
$entity->getLocale()->willReturn('en');

$entity->getLocale = function() {
return 'en';
};
$provider = new SymfonyExpressionTokenProvider($translator->reveal());
$provider->provide($entity, 'translator.addResource("php", "/test.php")');
}

public function testResolveNotExists()
{
$this->setExpectedException(CannotEvaluateTokenException::class);
Expand Down

0 comments on commit 9c948f9

Please sign in to comment.