Skip to content

Commit

Permalink
Merge branch '2.5'
Browse files Browse the repository at this point in the history
* 2.5: (23 commits)
  [HttpKernel] fixed some unit tests for 2.4 (signature now uses SHA256 instead of MD5)
  [HttpKernel] simplified code
  [HttpKernel] fixed internal fragment handling
  fixing yaml indentation
  Unexpexted ));"
  [WebProfiler] replaced the import/export feature from the web interface to a CLI tool
  Forced all fragment uris to be signed, even for ESI
  Add tests and more assertions
  [FrameworkBundle][Translator] Validate locales.
  [HttpFoundation] added some missing tests
  [HttpFoundation] Improve string values in test codes
  [Security] Add more tests for StringUtils::equals
  fix comment: not fourth but sixth argument
  fixing typo in a comment
  [FrameworkBundle] fixed CS
  [FrameworkBundle] PhpExtractor bugfix and improvements
  [Finder] Fix findertest readability
  [Filesystem] Add FTP stream wrapper context option to enable overwrite (override)
  fix parsing of Authorization header
  Test examples from Drupal SA-CORE-2014-003
  ...

Conflicts:
	src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/admin.html.twig
	src/Symfony/Component/Filesystem/Filesystem.php
	src/Symfony/Component/HttpKernel/Fragment/EsiFragmentRenderer.php
  • Loading branch information
fabpot committed Sep 3, 2014
2 parents c51f3f3 + c1b1b10 commit d31bf63
Show file tree
Hide file tree
Showing 34 changed files with 900 additions and 211 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
@@ -1,12 +1,12 @@
language: php

php:
- 5.3.3
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm-nightly
- 5.3.3
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm-nightly

matrix:
allow_failures:
Expand Down
1 change: 0 additions & 1 deletion UPGRADE-3.0.md
Expand Up @@ -863,7 +863,6 @@ UPGRADE FROM 2.x to 3.0
->atPath('property')
->setParameter('{{ value }}', $invalidValue)
->addViolation();
));
```

The methods `validate()` and `validateValue()` were removed. You should use
Expand Down
Expand Up @@ -40,6 +40,7 @@
<tag name="kernel.fragment_renderer" />
<argument type="service" id="esi" on-invalid="null" />
<argument type="service" id="fragment.renderer.inline" />
<argument type="service" id="uri_signer" />
<call method="setFragmentPath"><argument>%fragment.path%</argument></call>
</service>

Expand Down
@@ -1,3 +1,33 @@
This template is used for translation message extraction tests
<?php echo $view['translator']->trans('single-quoted key') ?>
<?php echo $view['translator']->trans("double-quoted key") ?>
<?php echo $view['translator']->trans(<<<EOF
heredoc key
EOF
) ?>
<?php echo $view['translator']->trans(<<<'EOF'
nowdoc key
EOF
) ?>
<?php echo $view['translator']->trans(
"double-quoted key with whitespace and escaped \$\n\" sequences"
) ?>
<?php echo $view['translator']->trans(
'single-quoted key with whitespace and nonescaped \$\n\' sequences'
) ?>
<?php echo $view['translator']->trans( <<<EOF
heredoc key with whitespace and escaped \$\n sequences
EOF
) ?>
<?php echo $view['translator']->trans( <<<'EOF'
nowdoc key with whitespace and nonescaped \$\n sequences
EOF
) ?>

<?php echo $view['translator']->trans('single-quoted key with "quote mark at the end"') ?>

<?php echo $view['translator']->transChoice(
'{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
10,
array('%count%' => 10)
) ?>
Expand Up @@ -27,10 +27,27 @@ public function testExtraction()
// Act
$extractor->extract(__DIR__.'/../Fixtures/Resources/views/', $catalogue);

$expectedHeredoc = <<<EOF
heredoc key with whitespace and escaped \$\n sequences
EOF;
$expectedNowdoc = <<<'EOF'
nowdoc key with whitespace and nonescaped \$\n sequences
EOF;
// Assert
$this->assertCount(2, $catalogue->all('messages'), '->extract() should find 1 translation');
$this->assertTrue($catalogue->has('single-quoted key'), '->extract() should find the "single-quoted key" message');
$this->assertTrue($catalogue->has('double-quoted key'), '->extract() should find the "double-quoted key" message');
$this->assertEquals('prefixsingle-quoted key', $catalogue->get('single-quoted key'), '->extract() should apply "prefix" as prefix');
$expectedCatalogue = array('messages' => array(
'single-quoted key' => 'prefixsingle-quoted key',
'double-quoted key' => 'prefixdouble-quoted key',
'heredoc key' => 'prefixheredoc key',
'nowdoc key' => 'prefixnowdoc key',
"double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
'single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \$\n\' sequences',
'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
$expectedHeredoc => "prefix".$expectedHeredoc,
$expectedNowdoc => "prefix".$expectedNowdoc,
'{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples' => 'prefix{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
));
$actualCatalogue = $catalogue->all();

$this->assertEquals($expectedCatalogue, $actualCatalogue);
}
}
Expand Up @@ -93,6 +93,16 @@ public function testTransWithCaching()
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
}

public function testTransWithCachingWithInvalidLocale()
{
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
$translator->setLocale('invalid locale');

$this->setExpectedException('\InvalidArgumentException');
$translator->trans('foo');
}

/**
* @dataProvider getGetLocaleData
*/
Expand All @@ -102,7 +112,7 @@ public function testGetLocale($inRequestScope)
if ($inRequestScope) {
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request
->expects($this->once())
->expects($this->any())
->method('getLocale')
->will($this->returnValue('en'))
;
Expand Down Expand Up @@ -135,6 +145,37 @@ public function getGetLocaleData()
);
}

public function testGetLocaleWithInvalidLocale()
{
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');

$request
->expects($this->any())
->method('getLocale')
->will($this->returnValue('foo bar'))
;
$request
->expects($this->once())
->method('getDefaultLocale')
->will($this->returnValue('en-US'))
;

$requestStack = new RequestStack();
$requestStack->push($request);

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container
->expects($this->once())
->method('get')
->with('request_stack')
->will($this->returnValue($requestStack))
;

$translator = new Translator($container, new MessageSelector());
$this->assertSame('en-US', $translator->getLocale());
}


protected function getCatalogue($locale, $messages)
{
$catalogue = new MessageCatalogue($locale);
Expand Down Expand Up @@ -215,9 +256,9 @@ protected function getContainer($loader)
return $container;
}

public function getTranslator($loader, $options = array())
public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
{
$translator = new Translator(
$translator = new $translatorClass(
$this->getContainer($loader),
new MessageSelector(),
array('loader' => array('loader')),
Expand All @@ -235,3 +276,14 @@ public function getTranslator($loader, $options = array())
return $translator;
}
}

class TranslatorWithInvalidLocale extends Translator
{
/**
* {@inheritdoc}
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
}
82 changes: 68 additions & 14 deletions src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php
Expand Up @@ -23,7 +23,6 @@
class PhpExtractor implements ExtractorInterface
{
const MESSAGE_TOKEN = 300;
const IGNORE_TOKEN = 400;

/**
* Prefix for new found message.
Expand All @@ -39,15 +38,16 @@ class PhpExtractor implements ExtractorInterface
*/
protected $sequences = array(
array(
'$view',
'[',
'\'translator\'',
']',
'->',
'trans',
'(',
self::MESSAGE_TOKEN,
')',
),
array(
'->',
'transChoice',
'(',
self::MESSAGE_TOKEN,
),
);

Expand Down Expand Up @@ -76,6 +76,7 @@ public function setPrefix($prefix)
* Normalizes a token.
*
* @param mixed $token
*
* @return string
*/
protected function normalizeToken($token)
Expand All @@ -87,6 +88,56 @@ protected function normalizeToken($token)
return $token;
}

/**
* Seeks to a non-whitespace token.
*/
private function seekToNextReleventToken(\Iterator $tokenIterator)
{
for (; $tokenIterator->valid(); $tokenIterator->next()) {
$t = $tokenIterator->current();
if (!is_array($t) || ($t[0] !== T_WHITESPACE)) {
break;
}
}
}

/**
* Extracts the message from the iterator while the tokens
* match allowed message tokens
*/
private function getMessage(\Iterator $tokenIterator)
{
$message = '';
$docToken = '';

for (; $tokenIterator->valid(); $tokenIterator->next()) {
$t = $tokenIterator->current();
if (!is_array($t)) {
break;
}

switch ($t[0]) {
case T_START_HEREDOC:
$docToken = $t[1];
break;
case T_ENCAPSED_AND_WHITESPACE:
case T_CONSTANT_ENCAPSED_STRING:
$message .= $t[1];
break;
case T_END_HEREDOC:
return PhpStringTokenParser::parseDocString($docToken, $message);
default:
break 2;
}
}

if ($message) {
$message = PhpStringTokenParser::parse($message);
}

return $message;
}

/**
* Extracts trans message from PHP tokens.
*
Expand All @@ -95,24 +146,27 @@ protected function normalizeToken($token)
*/
protected function parseTokens($tokens, MessageCatalogue $catalog)
{
foreach ($tokens as $key => $token) {
$tokenIterator = new \ArrayIterator($tokens);

for ($key = 0; $key < $tokenIterator->count(); $key++) {
foreach ($this->sequences as $sequence) {
$message = '';
$tokenIterator->seek($key);

foreach ($sequence as $id => $item) {
if ($this->normalizeToken($tokens[$key + $id]) == $item) {
foreach ($sequence as $item) {
$this->seekToNextReleventToken($tokenIterator);

if ($this->normalizeToken($tokenIterator->current()) == $item) {
$tokenIterator->next();
continue;
} elseif (self::MESSAGE_TOKEN == $item) {
$message = $this->normalizeToken($tokens[$key + $id]);
} elseif (self::IGNORE_TOKEN == $item) {
continue;
$message = $this->getMessage($tokenIterator);
break;
} else {
break;
}
}

$message = trim($message, '\'"');

if ($message) {
$catalog->set($message, $this->prefix.$message);
break;
Expand Down

0 comments on commit d31bf63

Please sign in to comment.