Przeglądaj źródła

ZF-7496
Zend_Controller, Zend_Test_PHPUnit
Correct Zend_Controller_Action to allow Zend_Test_PHPUnit_ControllerTestCase to test redirects from pre-dispatch methods properly


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24252 44c647ce-9c0f-0410-b52a-842ac1e357ba

adamlundrigan 14 lat temu
rodzic
commit
d0c29dd3ac

+ 11 - 7
library/Zend/Controller/Action.php

@@ -505,14 +505,18 @@ abstract class Zend_Controller_Action implements Zend_Controller_Action_Interfac
                 $this->_classMethods = get_class_methods($this);
             }
 
-            // preDispatch() didn't change the action, so we can continue
-            if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
-                if ($this->getInvokeArg('useCaseSensitiveActions')) {
-                    trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
+            // If pre-dispatch hooks introduced a redirect then stop dispatch
+            // @see ZF-7496
+            if (!($this->getResponse()->isRedirect())) {
+                // preDispatch() didn't change the action, so we can continue
+                if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
+                    if ($this->getInvokeArg('useCaseSensitiveActions')) {
+                        trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
+                    }
+                    $this->$action();
+                } else {
+                    $this->__call($action, array());
                 }
-                $this->$action();
-            } else {
-                $this->__call($action, array());
             }
             $this->postDispatch();
         }

+ 55 - 0
tests/Zend/Test/PHPUnit/ControllerTestCaseTest.php

@@ -781,6 +781,61 @@ class Zend_Test_PHPUnit_ControllerTestCaseTest extends PHPUnit_Framework_TestCas
                : gettype($boot);
         $this->assertTrue($boot === $this->testCase->bootstrap->getBootstrap(), $type);
     }
+    
+    /**
+     * @group ZF-7496
+     * @dataProvider providerRedirectWorksAsExpectedFromHookMethodsInActionController
+     */
+    public function testRedirectWorksAsExpectedFromHookMethodsInActionController($dispatchTo)
+    {
+        $this->testCase->getFrontController()->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers');
+        $this->testCase->dispatch($dispatchTo);
+        $this->testCase->assertRedirectTo('/login');
+        $this->assertNotEquals('action body', $this->testCase->getResponse()->getBody());
+    }
+    
+    /**
+     * Data provider for testRedirectWorksAsExpectedFromHookMethodsInActionController
+     * @return array
+     */
+    public function providerRedirectWorksAsExpectedFromHookMethodsInActionController()
+    {
+        return array(
+            array('/zend-test-redirect-from-init/baz'),
+            array('/zend-test-redirect-from-pre-dispatch/baz')
+        );
+    }
+    
+    /**
+     * @group ZF-7496
+     * @dataProvider providerRedirectWorksAsExpectedFromHookMethodsInFrontControllerPlugin
+     */
+    public function testRedirectWorksAsExpectedFromHookMethodsInFrontControllerPlugin($pluginName)
+    {
+        require_once dirname(__FILE__) . "/_files/application/plugins/RedirectFrom{$pluginName}.php";
+        $className = "Application_Plugin_RedirectFrom{$pluginName}";
+        
+        $fc = $this->testCase->getFrontController();
+        $fc->setControllerDirectory(dirname(__FILE__) . '/_files/application/controllers')
+           ->registerPlugin(new $className());
+        $this->testCase->dispatch('/');
+        $this->testCase->assertRedirectTo('/login');
+        $this->assertNotEquals('action body', $this->testCase->getResponse()->getBody());
+    }
+    
+    /**
+     * Data provider for testRedirectWorksAsExpectedFromHookMethodsInFrontControllerPlugin
+     * @return array
+     */
+    public function providerRedirectWorksAsExpectedFromHookMethodsInFrontControllerPlugin()
+    {
+        return array(
+            array('RouteStartup'),
+            array('RouteShutdown'),
+            array('DispatchLoopStartup'),
+            array('PreDispatch')
+        );
+    }
 }
 
 // Concrete test case class for testing purposes

+ 42 - 0
tests/Zend/Test/PHPUnit/_files/application/controllers/ZendTestRedirectFromInitController.php

@@ -0,0 +1,42 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class ZendTestRedirectFromInitController extends Zend_Controller_Action
+{
+    public function init()
+    {
+        return $this->_redirect('/login');
+    }
+
+    public function bazAction()
+    {
+        $this->_helper->viewRenderer->setNoRender(true);
+        echo 'action body';
+    }
+}

+ 42 - 0
tests/Zend/Test/PHPUnit/_files/application/controllers/ZendTestRedirectFromPreDispatchController.php

@@ -0,0 +1,42 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class ZendTestRedirectFromPreDispatchController extends Zend_Controller_Action
+{
+    public function preDispatch()
+    {
+        return $this->_redirect('/login');
+    }
+
+    public function bazAction()
+    {
+        $this->_helper->viewRenderer->setNoRender(true);
+        echo 'action body';
+    }
+}

+ 36 - 0
tests/Zend/Test/PHPUnit/_files/application/plugins/RedirectFromDispatchLoopStartup.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Application_Plugin_RedirectFromDispatchLoopStartup extends Zend_Controller_Plugin_Abstract
+{
+    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $r)
+    {
+        return $this->getResponse()->setRedirect('/login');
+    }
+}

+ 36 - 0
tests/Zend/Test/PHPUnit/_files/application/plugins/RedirectFromPreDispatch.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Application_Plugin_RedirectFromPreDispatch extends Zend_Controller_Plugin_Abstract
+{
+    public function preDispatch(Zend_Controller_Request_Abstract $r)
+    {
+        return $this->getResponse()->setRedirect('/login');
+    }
+}

+ 36 - 0
tests/Zend/Test/PHPUnit/_files/application/plugins/RedirectFromRouteShutdown.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Application_Plugin_RedirectFromRouteShutdown extends Zend_Controller_Plugin_Abstract
+{
+    public function routeShutdown(Zend_Controller_Request_Abstract $r)
+    {
+        return $this->getResponse()->setRedirect('/login');
+    }
+}

+ 36 - 0
tests/Zend/Test/PHPUnit/_files/application/plugins/RedirectFromRouteStartup.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Application_Plugin_RedirectFromRouteStartup extends Zend_Controller_Plugin_Abstract
+{
+    public function routeStartup(Zend_Controller_Request_Abstract $r)
+    {
+        return $this->getResponse()->setRedirect('/login');
+    }
+}