8000 Merge branch '2.3' into 2.4 · symfony/symfony@8358ebb · GitHub
[go: up one dir, main page]

Skip to content

Commit 8358ebb

Browse files
committed
Merge branch '2.3' into 2.4
* 2.3: [HttpKernel] fixed internal fragment handling fixing yaml indentation [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 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 Fix potential DoS when parsing HOST Made optimization deprecating modulus operator Conflicts: src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml src/Symfony/Component/HttpFoundation/Request.php src/Symfony/Component/HttpFoundation/Tests/RequestTest.php src/Symfony/Component/HttpKernel/Fragment/EsiFragmentRenderer.php
2 parents 8fdfb6f + 23eb033 commit 8358ebb

File tree

28 files changed

+843
-173
lines changed
  • WebProfilerBundle
  • Component
  • 28 files changed

    +843
    -173
    lines changed

    .travis.yml

    Lines changed: 6 additions & 6 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1,12 +1,12 @@
    11
    language: php
    22

    33
    php:
    4-
    - 5.3.3
    5-
    - 5.3
    6-
    - 5.4
    7-
    - 5.5
    8-
    - 5.6
    9-
    - hhvm-nightly
    4+
    - 5.3.3
    5+
    - 5.3
    6+
    - 5.4
    7+
    - 5.5
    8+
    - 5.6
    9+
    - hhvm-nightly
    1010

    1111
    matrix:
    1212
    allow_failures:

    src/Symfony/Bundle/FrameworkBundle/Resources/config/fragment_renderer.xml

    Lines changed: 1 addition & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -39,6 +39,7 @@
    3939
    <tag name="kernel.fragment_renderer" />
    4040
    <argument type="service" id="esi" on-invalid="null" />
    4141
    <argument type="service" id="fragment.renderer.inline" />
    42+
    <argument type="service" id="uri_signer" />
    4243
    <call< 9E88 /span> method="setFragmentPath"><argument>%fragment.path%</argument></call>
    4344
    </service>
    4445
    </services>
    Lines changed: 30 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1,3 +1,33 @@
    11
    This template is used for translation message extraction tests
    22
    <?php echo $view['translator']->trans('single-quoted key') ?>
    33
    <?php echo $view['translator']->trans("double-quoted key") ?>
    4+
    <?php echo $view['translator']->trans(<<<EOF
    5+
    heredoc key
    6+
    EOF
    7+
    ) ?>
    8+
    <?php echo $view['translator']->trans(<<<'EOF'
    9+
    nowdoc key
    10+
    EOF
    11+
    ) ?>
    12+
    <?php echo $view['translator']->trans(
    13+
    "double-quoted key with whitespace and escaped \$\n\" sequences"
    14+
    ) ?>
    15+
    <?php echo $view['translator']->trans(
    16+
    'single-quoted key with whitespace and nonescaped \$\n\' sequences'
    17+
    ) ?>
    18+
    <?php echo $view['translator']->trans( <<<EOF
    19+
    heredoc key with whitespace and escaped \$\n sequences
    20+
    EOF
    21+
    ) ?>
    22+
    <?php echo $view['translator']->trans( <<<'EOF'
    23+
    nowdoc key with whitespace and nonescaped \$\n sequences
    24+
    EOF
    25+
    ) ?>
    26+
    27+
    <?php echo $view['translator']->trans('single-quoted key with "quote mark at the end"') ?>
    28+
    29+
    <?php echo $view['translator']->transChoice(
    30+
    '{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
    31+
    10,
    32+
    array('%count%' => 10)
    33+
    ) ?>

    src/Symfony/Bundle/FrameworkBundle/Tests/Translation/PhpExtractorTest.php

    Lines changed: 21 additions & 4 deletions
    6D38
    Original file line numberDiff line numberDiff line change
    @@ -27,10 +27,27 @@ public function testExtraction()
    2727
    // Act
    2828
    $extractor->extract(__DIR__.'/../Fixtures/Resources/views/', $catalogue);
    2929

    30+
    $expectedHeredoc = <<<EOF
    31+
    heredoc key with whitespace and escaped \$\n sequences
    32+
    EOF;
    33+
    $expectedNowdoc = <<<'EOF'
    34+
    nowdoc key with whitespace and nonescaped \$\n sequences
    35+
    EOF;
    3036
    // Assert
    31-
    $this->assertCount(2, $catalogue->all('messages'), '->extract() should find 1 translation');
    32-
    $this->assertTrue($catalogue->has('single-quoted key'), '->extract() should find the "single-quoted key" message');
    33-
    $this->assertTrue($catalogue->has('double-quoted key'), '->extract() should find the "double-quoted key" message');
    34-
    $this->assertEquals('prefixsingle-quoted key', $catalogue->get('single-quoted key'), '->extract() should apply "prefix" as prefix');
    37+
    $expectedCatalogue = array('messages' => array(
    38+
    'single-quoted key' => 'prefixsingle-quoted key',
    39+
    'double-quoted key' => 'prefixdouble-quoted key',
    40+
    'heredoc key' => 'prefixheredoc key',
    41+
    'nowdoc key' => 'prefixnowdoc key',
    42+
    "double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
    43+
    'single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \$\n\' sequences',
    44+
    'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
    45+
    $expectedHeredoc => "prefix".$expectedHeredoc,
    46+
    $expectedNowdoc => "prefix".$expectedNowdoc,
    47+
    '{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',
    48+
    ));
    49+
    $actualCatalogue = $catalogue->all();
    50+
    51+
    $this->assertEquals($expectedCatalogue, $actualCatalogue);
    3552
    }
    3653
    }

    src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

    Lines changed: 66 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -92,6 +92,16 @@ public function testTransWithCaching()
    9292
    $this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
    9393
    }
    9494

    95+
    public function testTransWithCachingWithInvalidLocale()
    96+
    {
    97+
    $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
    98+
    $translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
    99+
    $translator->setLocale('invalid locale');
    100+
    101+
    $this->setExpectedException('\InvalidArgumentException');
    102+
    $translator->trans('foo');
    103+
    }
    104+
    95105
    public function testGetLocale()
    96106
    {
    97107
    $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
    @@ -131,6 +141,49 @@ public function testGetLocale()
    131141
    $this->assertSame('en', $translator->getLocale());
    132142
    }
    133143

    144+
    public function testGetLocaleWithInvalidLocale()
    145+
    {
    146+
    $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
    147+
    148+
    $request
    149+
    ->expects($this->once())
    150+
    ->method('getLocale')
    151+
    ->will($this->returnValue('foo bar'))
    152+
    ;
    153+
    $request
    154+
    ->expects($this->once())
    155+
    ->method('getDefaultLocale')
    156+
    ->will($this->returnValue('en-US'))
    157+
    ;
    158+
    159+
    $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
    160+
    161+
    $container
    162+
    ->expects($this->once())
    163+
    ->method('isScopeActive')
    164+
    ->with('request')
    165+
    ->will($this->returnValue(true))
    166+
    ;
    167+
    168+
    $container
    169+
    ->expects($this->once())
    170+
    ->method('has')
    171+
    ->with('request')
    172+
    ->will($this->returnValue(true))
    173+
    ;
    174+
    175+
    $container
    176+
    ->expects($this->any())
    177+
    ->method('get')
    178+
    ->with('request')
    179+
    ->will($this->returnValue($request))
    180+
    ;
    181+
    182+
    $translator = new Translator($container, new MessageSelector());
    183+
    $this->assertSame('en-US', $translator->getLocale());
    184+
    }
    185+
    186+
    134187
    protected function getCatalogue($locale, $messages)
    135188
    {
    136189
    $catalogue = new MessageCatalogue($locale);
    @@ -211,9 +264,9 @@ protected function getContainer($loader)
    211264
    return $container;
    212265
    }
    213266

    214-
    public function getTranslator($loader, $options = array())
    267+
    public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
    215268
    {
    216-
    $translator = new Translator(
    269+
    $translator = new $translatorClass(
    217270
    $this->getContainer($loader),
    218271
    new MessageSelector(),
    219272
    array('loader' => array('loader')),
    @@ -231,3 +284,14 @@ public function getTranslator($loader, $options = array())
    231284
    return $translator;
    232285
    }
    233286
    }
    287+
    288+
    class TranslatorWithInvalidLocale extends Translator
    289+
    {
    290+
    /**
    291+
    * {@inheritdoc}
    292+
    */
    293+
    public function setLocale($locale)
    294+
    {
    295+
    $this->locale = $locale;
    296+
    }
    297+
    }

    src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php

    Lines changed: 68 additions & 14 deletions
    Original file line numberDiff line numberDiff line change
    @@ -23,7 +23,6 @@
    2323
    class PhpExtractor implements ExtractorInterface
    2424
    {
    2525
    const MESSAGE_TOKEN = 300;
    26-
    const IGNORE_TOKEN = 400;
    2726

    2827
    /**
    2928
    * Prefix for new found message.
    @@ -39,15 +38,16 @@ class PhpExtractor implements ExtractorInterface
    3938
    */
    4039
    protected $sequences = array(
    4140
    array(
    42-
    '$view',
    43-
    '[',
    44-
    '\'translator\'',
    45-
    ']',
    4641
    '->',
    4742
    'trans',
    4843
    '(',
    4944
    self::MESSAGE_TOKEN,
    50-
    ')',
    45+
    ),
    46+
    array(
    47+
    '->',
    48+
    'transChoice',
    49+
    '(',
    50+
    self::MESSAGE_TOKEN,
    5151
    ),
    5252
    );
    5353

    @@ -76,6 +76,7 @@ public function setPrefix($prefix)
    7676
    * Normalizes a token.
    7777
    *
    7878
    * @param mixed $token
    79+
    *
    7980
    * @return string
    8081
    */
    8182
    protected function normalizeToken($token)
    @@ -87,6 +88,56 @@ protected function normalizeToken($token)
    8788
    return $token;
    8889
    }
    8990

    91+
    /**
    92+
    * Seeks to a non-whitespace token.
    93+
    */
    94+
    private function seekToNextReleventToken(\Iterator $tokenIterator)
    95+
    {
    96+
    for (; $tokenIterator->valid(); $tokenIterator->next()) {
    97+
    $t = $tokenIterator->current();
    98+
    if (!is_array($t) || ($t[0] !== T_WHITESPACE)) {
    99+
    break;
    100+
    }
    101+
    }
    102+
    }
    103+
    104+
    /**
    105+
    * Extracts the message from the iterator while the tokens
    106+
    * match allowed message tokens
    107+
    */
    108+
    private function getMessage(\Iterator $tokenIterator)
    109+
    {
    110+
    $message = '';
    111+
    $docToken = '';
    112+
    113+
    for (; $tokenIterator->valid(); $tokenIterator->next()) {
    114+
    $t = $tokenIterator->current();
    115+
    if (!is_array($t)) {
    116+
    break;
    117+
    }
    118+
    119+
    switch ($t[0]) {
    120+
    case T_START_HEREDOC:
    121+
    $docToken = $t[1];
    122+
    break;
    123+
    case T_ENCAPSED_AND_WHITESPACE:
    124+
    case T_CONSTANT_ENCAPSED_STRING:
    125+
    $message .= $t[1];
    126+
    break;
    127+
    case T_END_HEREDOC:
    128+
    return PhpStringTokenParser::parseDocString($docToken, $message);
    129+
    default:
    130+
    break 2;
    131+
    }
    132+
    }
    133+
    134+
    if ($message) {
    135+
    $message = PhpStringTokenParser::parse($message);
    136+
    }
    137+
    138+
    return $message;
    139+
    }
    140+
    90141
    /**
    91142
    * Extracts trans message from PHP tokens.
    92143
    *
    @@ -95,24 +146,27 @@ protected function normalizeToken($token)
    95146
    */
    96147
    protected function parseTokens($tokens, MessageCatalogue $catalog)
    97148
    {
    98-
    foreach ($tokens as $key => $token) {
    149+
    $tokenIterator = new \ArrayIterator($tokens);
    150+
    151+
    for ($key = 0; $key < $tokenIterator->count(); $key++) {
    99152
    foreach ($this->sequences as $sequence) {
    100153
    $message = '';
    154+
    $tokenIterator->seek($key);
    101155

    102-
    foreach ($sequence as $id => $item) {
    103-
    if ($this->normalizeToken($tokens[$key + $id]) == $item) {
    156+
    foreach ($sequence as $item) {
    157+
    $this->seekToNextReleventToken($tokenIterator);
    158+
    159+
    if ($this->normalizeToken($tokenIterator->current()) == $item) {
    160+
    $tokenIterator->next();
    104161
    continue;
    105162
    } elseif (self::MESSAGE_TOKEN == $item) {
    106-
    $message = $this->normalizeToken($tokens[$key + $id]);
    107-
    } elseif (self::IGNORE_TOKEN == $item) {
    108-
    continue;
    163+
    $message = $this->getMessage($tokenIterator);
    164+
    break;
    109165
    } else {
    110166
    break;
    111167
    }
    112168
    }
    113169

    114-
    $message = trim($message, '\'"');
    115-
    116170
    if ($message) {
    117171
    $catalog->set($message, $this->prefix.$message);
    118172
    break;

    0 commit comments

    Comments
     (0)
    0