Browse Source

Fix redirects

Mark Scherer 11 years ago
parent
commit
0ba260299b

+ 15 - 30
src/Controller/Component/CommonComponent.php

@@ -85,6 +85,7 @@ class CommonComponent extends Component {
 	 *
 	 * @param mixed $components (single string or multiple array)
 	 * @param bool $callbacks (defaults to true)
+	 * @return void
 	 */
 	public function loadComponent($component, array $config = [], $callbacks = true) {
 		list($plugin, $componentName) = pluginSplit($component);
@@ -155,13 +156,13 @@ class CommonComponent extends Component {
 	 * @param mixed $url
 	 * @param bool $allowSelf if redirect to the same controller/action (url) is allowed
 	 * @param int $status
-	 * @return void
+	 * @return void|Response
 	 */
-	public function autoRedirect($whereTo, $allowSelf = true, $status = null) {
+	public function autoRedirect($whereTo, $allowSelf = false, $status = null) {
 		if ($allowSelf || $this->Controller->referer(null, true) !== '/' . $this->Controller->request->url) {
-			$this->Controller->redirect($this->Controller->referer($whereTo, true), $status);
+			return $this->Controller->redirect($this->Controller->referer($whereTo, true), $status);
 		}
-		$this->Controller->redirect($whereTo, $status);
+		return $this->Controller->redirect($whereTo, $status);
 	}
 
 	/**
@@ -173,10 +174,10 @@ class CommonComponent extends Component {
 	 * @see http://en.wikipedia.org/wiki/Post/Redirect/Get
 	 * @param mixed $url
 	 * @param int $status
-	 * @return void
+	 * @return void|Response
 	 */
 	public function postRedirect($whereTo, $status = 302) {
-		$this->Controller->redirect($whereTo, $status);
+		return $this->Controller->redirect($whereTo, $status);
 	}
 
 	/**
@@ -185,12 +186,12 @@ class CommonComponent extends Component {
 	 * @param mixed $url
 	 * @param bool $conditionalAutoRedirect false to skip whitelisting
 	 * @param int $status
-	 * @return void
+	 * @return void|Response
 	 */
 	public function autoPostRedirect($whereTo, $conditionalAutoRedirect = true, $status = 302) {
 		$referer = $this->Controller->referer($whereTo, true);
 		if (!$conditionalAutoRedirect && !empty($referer)) {
-			$this->postRedirect($referer, $status);
+			return $this->postRedirect($referer, $status);
 		}
 
 		if (!empty($referer)) {
@@ -202,7 +203,7 @@ class CommonComponent extends Component {
 			// will run into problems, if you use url rewriting.
 			$refererController = null;
 			if (isset($referer['controller'])) {
-				$refererController = Inflector::camelize($referer['controller']);
+				$refererController = $referer['controller'];
 			}
 			// fixme
 			if (!isset($this->Controller->autoRedirectActions)) {
@@ -213,16 +214,16 @@ class CommonComponent extends Component {
 				if (!empty($controller) && $refererController !== '*' && $refererController != $controller) {
 					continue;
 				}
-				if (empty($controller) && $refererController != Inflector::camelize($this->Controller->request->params['controller'])) {
+				if (empty($controller) && $refererController != $this->Controller->request->params['controller']) {
 					continue;
 				}
 				if (!in_array($referer['action'], $this->Controller->autoRedirectActions, true)) {
 					continue;
 				}
-				$this->autoRedirect($whereTo, true, $status);
+				return $this->autoRedirect($whereTo, true, $status);
 			}
 		}
-		$this->postRedirect($whereTo, $status);
+		return $this->postRedirect($whereTo, $status);
 	}
 
 	/**
@@ -235,7 +236,7 @@ class CommonComponent extends Component {
 	 * @param bool $exit
 	 * @return void
 	 */
-	public function completeRedirect($url = null, $status = null, $exit = true) {
+	public function completeRedirect($url = null, $status = null) {
 		if ($url === null) {
 			$url = $this->Controller->request->params;
 			unset($url['pass']);
@@ -244,23 +245,7 @@ class CommonComponent extends Component {
 		if (is_array($url)) {
 			$url += $this->Controller->request->params['pass'];
 		}
-		return $this->Controller->redirect($url, $status, $exit);
-	}
-
-	/**
-	 * Only redirect to itself if cookies are on
-	 * Prevents problems with lost data
-	 * Note: Many pre-HTTP/1.1 user agents do not understand the 303 status. When interoperability with such clients is a concern, the 302 status code may be used instead, since most user agents react to a 302 response as described here for 303.
-	 *
-	 * @see http://en.wikipedia.org/wiki/Post/Redirect/Get
-	 * TODO: change to 303 with backwardscompatability for older browsers...
-	 * @param int $status
-	 * @return void
-	 */
-	public function prgRedirect($status = 302) {
-		if (!empty($_COOKIE[Configure::read('Session.cookie')])) {
-			$this->Controller->redirect('/' . $this->Controller->request->url, $status);
-		}
+		return $this->Controller->redirect($url, $status);
 	}
 
 	/**

+ 1 - 1
src/Model/Table/Table.php

@@ -579,7 +579,7 @@ class Table extends CakeTable {
 		$date = $dateTime[0];
 		$time = (!empty($dateTime[1]) ? $dateTime[1] : '');
 
-		if (!empty($options['allowEmpty']) && (empty($date) && empty($time) || $date == DEFAULT_DATE && $time == DEFAULT_TIME || $date == DEFAULT_DATE && empty($time))) {
+		if (!empty($options['allowEmpty']) && (empty($date) && empty($time) || $date === DEFAULT_DATE && $time === DEFAULT_TIME || $date === DEFAULT_DATE && empty($time))) {
 			return true;
 		}
 

+ 37 - 16
tests/TestCase/Controller/Component/CommonComponentTest.php

@@ -22,7 +22,7 @@ class CommonComponentTest extends TestCase {
 
 		Configure::write('App.namespace', 'TestApp');
 
-		$this->Controller = new CommonComponentTestController();
+		$this->Controller = new CommonComponentTestController(new Request('/test'));
 		$this->Controller->startupProcess();
 	}
 
@@ -127,6 +127,42 @@ class CommonComponentTest extends TestCase {
 		$this->assertFalse($is);
 	}
 
+	/**
+	 * CommonComponentTest::testAutoRedirect()
+	 *
+	 * @return void
+	 */
+	public function testPostRedirect() {
+		$is = $this->Controller->Common->postRedirect(array('action' => 'foo'));
+		$is = $this->Controller->response->header();
+		$this->assertSame('/foo', $is['Location']);
+		$this->assertSame(302, $this->Controller->response->statusCode());
+	}
+
+	/**
+	 * CommonComponentTest::testAutoRedirect()
+	 *
+	 * @return void
+	 */
+	public function testAutoRedirect() {
+		$is = $this->Controller->Common->autoRedirect(array('action' => 'foo'));
+		$is = $this->Controller->response->header();
+		$this->assertSame('/foo', $is['Location']);
+		$this->assertSame(200, $this->Controller->response->statusCode());
+	}
+
+	/**
+	 * CommonComponentTest::testAutoRedirect()
+	 *
+	 * @return void
+	 */
+	public function testAutoRedirectReferer() {
+		$is = $this->Controller->Common->autoRedirect(array('action' => 'foo'), true);
+		$is = $this->Controller->response->header();
+		$this->assertSame('/foo', $is['Location']);
+		$this->assertSame(200, $this->Controller->response->statusCode());
+	}
+
 }
 
 /**
@@ -136,19 +172,4 @@ class CommonComponentTestController extends Controller {
 
 	public $components = ['Tools.Common'];
 
-	public $failed = false;
-
-	public $testHeaders = [];
-
-	public function fail() {
-		$this->failed = true;
-	}
-
-	public function redirect($url, $status = null, $exit = true) {
-		return $status;
-	}
-
-	public function header($status) {
-		$this->testHeaders[] = $status;
-	}
 }