Explorar o código

[FrameworkBundle] fixed unescaped file_link_format parameter in CodeHelper that made the functional tests fail when checking a 4xx page. The generated file link format used in an HTML stack trace didn't contain an escaped ampersand (&) character. The resulting HTML code was not validable against its DTD and so the Crawler made the tests fail when checking a 4xx page.

Hugo Hamon %!s(int64=13) %!d(string=hai) anos
pai
achega
5e5050db53

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

@@ -86,6 +86,7 @@
             <tag name="templating.helper" alias="code" />
             <argument>%templating.helper.code.file_link_format%</argument>
             <argument>%kernel.root_dir%</argument>
+            <argument>%kernel.charset%</argument>
         </service>
 
         <service id="templating.helper.translator" class="%templating.helper.translator.class%">

+ 9 - 2
src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php

@@ -13,6 +13,10 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
 
 use Symfony\Component\Templating\Helper\Helper;
 
+if (!defined('ENT_SUBSTITUTE')) {
+    define('ENT_SUBSTITUTE', 8);
+}
+
 /**
  * CodeHelper.
  *
@@ -22,17 +26,20 @@ class CodeHelper extends Helper
 {
     protected $fileLinkFormat;
     protected $rootDir;
+    protected $charset;
 
     /**
      * Constructor.
      *
      * @param string $fileLinkFormat The format for links to source files
      * @param string $rootDir        The project root directory
+     * @param string $charset        The charset
      */
-    public function __construct($fileLinkFormat, $rootDir)
+    public function __construct($fileLinkFormat, $rootDir, $charset)
     {
         $this->fileLinkFormat = empty($fileLinkFormat) ? ini_get('xdebug.file_link_format') : $fileLinkFormat;
         $this->rootDir = str_replace('\\', '/', $rootDir).'/';
+        $this->charset = $charset;
     }
 
     /**
@@ -173,7 +180,7 @@ class CodeHelper extends Helper
         }
 
         if (false !== $link = $this->getFileLink($file, $line)) {
-            return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', $link, $text);
+            return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), $text);
         }
 
         return $text;

+ 7 - 1
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/CodeHelperTest.php

@@ -19,7 +19,13 @@ class CodeHelperTest extends \PHPUnit_Framework_TestCase
 
     static public function setUpBeforeClass()
     {
-        self::$helper = new CodeHelper('format', '/root');
+        self::$helper = new CodeHelper('txmt://open?url=file://%f&line=%l', '/root', 'UTF-8');
+    }
+
+    public function testFormatFile()
+    {
+        $expected = sprintf('<a href="txmt://open?url=file://%s&amp;line=25" title="Click to open this file" class="file_link">%s at line 25</a>', __FILE__, __FILE__);
+        $this->assertEquals($expected, self::$helper->formatFile(__FILE__, 25));
     }
 
     /**