Browse Source

Merge remote branch 'symfony/master' into experimental

Bernhard Schussek 14 years ago
parent
commit
c891e40c9b

+ 1 - 1
src/Symfony/Bundle/SwiftmailerBundle/DependencyInjection/Configuration.php

@@ -41,7 +41,7 @@ class Configuration
                 ->scalarNode('transport')
                     ->defaultValue('smtp')
                     ->validate()
-                        ->ifNotInArray(array ('smtp', 'mail', 'sendmail', 'gmail'))
+                        ->ifNotInArray(array ('smtp', 'mail', 'sendmail', 'gmail', null))
                         ->thenInvalid('The %s transport is not supported')
                     ->end()
                 ->end()

+ 14 - 3
src/Symfony/Bundle/SwiftmailerBundle/Tests/DependencyInjection/SwiftmailerExtensionTest.php

@@ -24,17 +24,28 @@ class SwiftmailerExtensionTest extends TestCase
         $loader = new SwiftmailerExtension();
 
         $loader->load(array(array()), $container);
-        $this->assertEquals('Swift_Mailer', $container->getParameter('swiftmailer.class'), '->mailerLoad() loads the swiftmailer.xml file if not already loaded');
+        $this->assertEquals('Swift_Mailer', $container->getParameter('swiftmailer.class'), '->load() loads the swiftmailer.xml file if not already loaded');
 
         $loader->load(array(array('transport' => 'sendmail')), $container);
-        $this->assertEquals('sendmail', $container->getParameter('swiftmailer.transport.name'), '->mailerLoad() overrides existing configuration options');
+        $this->assertEquals('sendmail', $container->getParameter('swiftmailer.transport.name'), '->load() overrides existing configuration options');
         $this->assertEquals('swiftmailer.transport.sendmail', (string) $container->getAlias('swiftmailer.transport'));
 
         $loader->load(array(array()), $container);
-        $this->assertEquals('smtp', $container->getParameter('swiftmailer.transport.name'), '->mailerLoad() provides default values for configuration options');
+        $this->assertEquals('smtp', $container->getParameter('swiftmailer.transport.name'), '->load() provides default values for configuration options');
         $this->assertEquals('swiftmailer.transport.smtp', (string) $container->getAlias('swiftmailer.transport'));
     }
 
+    public function testNullTransport()
+    {
+        $container = new ContainerBuilder();
+        $container->setParameter('kernel.debug', false);
+        $loader = new SwiftmailerExtension();
+
+        $loader->load(array(array('transport' => null)), $container);
+        $this->assertEquals('null', $container->getParameter('swiftmailer.transport.name'), '->load() uses the "null" string transport when transport is null');
+        $this->assertEquals('swiftmailer.transport.null', (string) $container->getAlias('swiftmailer.transport'));
+    }
+
     public function testSpool()
     {
         $container = new ContainerBuilder();

+ 10 - 0
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar_redirect.html.twig

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Redirection Intercepted</title>
+</head>
+<body>
+    <h1>This Request redirects to<br /><a href="{{ location }}">{{ location }}</a>.</h1>
+    <h4>The redirect was intercepted by the web debug toolbar to help debugging.<br/>For more information, see the "intercept-redirects" option of the Profiler.</h4>
+</body>
+</html>

+ 163 - 7
src/Symfony/Bundle/WebProfilerBundle/Tests/WebDebugToolbarListenerTest.php

@@ -13,6 +13,8 @@ namespace Symfony\Bundle\WebProfilerBundle\Tests;
 
 use Symfony\Bundle\WebProfilerBundle\WebDebugToolbarListener;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
 {
@@ -21,13 +23,7 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
      */
     public function testInjectToolbar($content, $expected)
     {
-        $templating = $this->getMock('Symfony\Bundle\TwigBundle\TwigEngine', array(), array(), '', false);
-        $templating->expects($this->any())
-                 ->method('render')
-                 ->will($this->returnValue('WDT'));
-        ;
-        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
-        $listener = new WebDebugToolbarListener($templating);
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
         $m = new \ReflectionMethod($listener, 'injectToolbar');
         $m->setAccessible(true);
 
@@ -54,4 +50,164 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
             </html>"),
         );
     }
+
+    public function testRedirectionIsIntercepted()
+    {
+        foreach (array(301, 302) as $statusCode) {
+            $response = new Response('Some content', $statusCode);
+            $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+            $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
+
+            $listener = new WebDebugToolbarListener($this->getTemplatingMock('Redirection'), true);
+            $listener->onCoreResponse($event);
+
+            $this->assertEquals(200, $response->getStatusCode());
+            $this->assertEquals('Redirection', $response->getContent());
+        }
+    }
+
+    public function testToolbarIsInjected()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals("<html><head></head><body>\nWDT\n</body></html>", $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedOnRedirection()
+    {
+        foreach (array(301, 302) as $statusCode) {
+            $response = new Response('<html><head></head><body></body></html>', $statusCode);
+            $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+            $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
+
+            $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+            $listener->onCoreResponse($event);
+
+            $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+        }
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedWhenThereIsNoNoXDebugTokenResponseHeader()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedWhenOnSubRequest()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::SUB_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedOnUncompleteHtmlResponses()
+    {
+        $response = new Response('<div>Some content</div>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<div>Some content</div>', $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedOnXmlHttpRequests()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(true), HttpKernelInterface::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedOnNonHtmlRequests()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'json'), HttpKernelInterface::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+    }
+
+    protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html')
+    {
+        $session = $this->getMock('Symfony\Component\HttpFoundation\Session', array(), array(), '', false);
+        $request = $this->getMock(
+            'Symfony\Component\HttpFoundation\Request',
+            array('getSession', 'isXmlHttpRequest', 'getRequestFormat'),
+            array(), '', false
+        );
+        $request->expects($this->any())
+            ->method('isXmlHttpRequest')
+            ->will($this->returnValue($isXmlHttpRequest));
+        $request->expects($this->any())
+            ->method('getRequestFormat')
+            ->will($this->returnValue($requestFormat));
+        $request->expects($this->any())
+            ->method('getSession')
+            ->will($this->returnValue($session));
+
+        return $request;
+    }
+
+    protected function getTemplatingMock($render = 'WDT')
+    {
+        $templating = $this->getMock('Symfony\Bundle\TwigBundle\TwigEngine', array(), array(), '', false);
+        $templating->expects($this->any())
+            ->method('render')
+            ->will($this->returnValue($render));
+
+        return $templating;
+    }
+
+    protected function getKernelMock()
+    {
+        return $this->getMock('Symfony\Component\HttpKernel\Kernel', array(), array(), '', false);
+    }
 }

+ 1 - 4
src/Symfony/Bundle/WebProfilerBundle/WebDebugToolbarListener.php

@@ -52,10 +52,7 @@ class WebDebugToolbarListener
             // keep current flashes for one more request
             $request->getSession()->setFlashes($request->getSession()->getFlashes());
 
-            $response->setContent(
-                sprintf('<html><head></head><body><h1>This Request redirects to<br /><a href="%1$s">%1$s</a>.</h1><h4>The redirect was intercepted by the web debug toolbar to help debugging.<br/>For more information, see the "intercept-redirects" option of the Profiler.</h4></body></html>',
-                $response->headers->get('Location'))
-            );
+            $response->setContent($this->templating->render('WebProfilerBundle:Profiler:toolbar_redirect.html.twig', array('location' => $response->headers->get('Location'))));
             $response->setStatusCode(200);
             $response->headers->remove('Location');
         }