From 1671c6a4c654efb2620f1fe140a066eed6474d86 Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Mon, 24 Jun 2013 14:21:18 +0000
Subject: [PATCH] =?UTF-8?q?Correction=20redirection=20sur=20page=20panier?=
 =?UTF-8?q?=20lorsque=20on=20n'est=20pas=20connect=C3=A9=20+=20ajout=20not?=
 =?UTF-8?q?ice?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../opac/controllers/AuthController.php       |  7 ++--
 .../opac/controllers/PanierController.php     |  8 ++---
 .../opac/views/scripts/auth/login.phtml       |  6 +++-
 .../opac/controllers/AuthControllerTest.php   | 34 +++++++++++++++++++
 .../opac/controllers/PanierControllerTest.php | 17 ++++++++++
 5 files changed, 65 insertions(+), 7 deletions(-)

diff --git a/application/modules/opac/controllers/AuthController.php b/application/modules/opac/controllers/AuthController.php
index f8c49e228f4..7cd549d5222 100644
--- a/application/modules/opac/controllers/AuthController.php
+++ b/application/modules/opac/controllers/AuthController.php
@@ -81,6 +81,8 @@ class AuthController extends Zend_Controller_Action
 		$error = (int)$this->_getParam('error');
 		$viewRenderer = $this->getHelper('ViewRenderer');
 		$viewRenderer->setLayoutScript('module.phtml');
+		$redirect = $this->_getParam('redirect', $this->_request->getPost('redirect'));
+		$this->view->redirect = $redirect;
 
 		$service_cas_server=$this->_request->getPost('service');
 		if (strlen($this->_getParam('service'))>0) {
@@ -101,9 +103,10 @@ class AuthController extends Zend_Controller_Action
 		if (strlen($service_cas_server)>1) 
 			return;
 
-		if (isset($_SESSION["abonne_redirect"])) {
+		if ($redirect) {
+			$this->getHelper('ViewRenderer')->setNoRender();
 			$this->getResponse()->setHeader('Content-Type', 'text/html;charset=utf-8');
-			$this->getResponse()->setBody("<script>window.location.replace('" .$_SESSION["abonne_redirect"] . "');</script>");
+			$this->getResponse()->setBody("<script>window.location.replace('" .urldecode($redirect). "');</script>");
 			return;
 		}
 
diff --git a/application/modules/opac/controllers/PanierController.php b/application/modules/opac/controllers/PanierController.php
index 471c421daa3..e013cce9033 100644
--- a/application/modules/opac/controllers/PanierController.php
+++ b/application/modules/opac/controllers/PanierController.php
@@ -23,16 +23,16 @@ class PanierController extends Zend_Controller_Action {
 	use Trait_Translator;
 	private $_user = null;								// User connecté (auth)
 
-	function init()	{
+	function preDispatch()	{
 		if ('ajout-ajax' == $this->_getParam('action'))
 			return;
 
-		if (!$this->_user = Class_Users::getIdentity())	{
-			$_SESSION["abonne_redirect"]=$this->_request->REQUEST_URI;
-			$this->_redirect('opac/auth/login');
+		if (!$this->_user = Class_Users::getIdentity()) {
+			$this->_forward('login', 'auth', 'opac', ['redirect' => $this->_request->REQUEST_URI]);
 		}
 	}
 
+
 	function indexAction()	{
 		$id_panier_courant = $this->_getParam("id_panier", 0);
 		$panier_courant = Class_PanierNotice::find($id_panier_courant);
diff --git a/application/modules/opac/views/scripts/auth/login.phtml b/application/modules/opac/views/scripts/auth/login.phtml
index 2eebb28b829..ffb6d3cb476 100644
--- a/application/modules/opac/views/scripts/auth/login.phtml
+++ b/application/modules/opac/views/scripts/auth/login.phtml
@@ -35,7 +35,11 @@
 				</td>
 		</tr>
 		</table>
-		<?php echo $this->bouton('type=V', 'form=form_login'); ?>
+		<?php 
+     if ($this->redirect)
+       echo '<input type="hidden" name="redirect" value="'.$this->redirect.'">';
+     echo $this->bouton('type=V', 'form=form_login'); 
+    ?>
 	</form>
 
 	<?php if ($this->message) {?>
diff --git a/tests/application/modules/opac/controllers/AuthControllerTest.php b/tests/application/modules/opac/controllers/AuthControllerTest.php
index 5fd55a2b06b..fb85b27f83d 100644
--- a/tests/application/modules/opac/controllers/AuthControllerTest.php
+++ b/tests/application/modules/opac/controllers/AuthControllerTest.php
@@ -324,6 +324,20 @@ class AuthControllerNobodyLoggedTest extends PortailWithOneLoginModuleTestCase {
 	public function pageShouldBeHtml5Valid() {
 		$this->assertHTML5($this->_response->getBody());
 	}
+
+
+	/** @test */
+	public function pageShouldNotContainsRedirectInput() {
+		$this->assertNotXPath('//input[@name="redirect"]');
+	}
+
+
+	/** @test */
+	public function withRedirectParamPageShouldContainsRedirectInput() {
+		$this->bootstrap();
+		$this->dispatch('/opac/auth/login/redirect/'.urlencode('/opac/paniers'));
+		$this->assertXPath('//input[@name="redirect"][@value="/opac/paniers"]');
+	}
 }
 
 
@@ -507,6 +521,26 @@ class AuthControllerPostSimpleSuccessfulTest extends AuthControllerPostSimpleSuc
 
 
 
+class AuthControllerPostSimpleSuccessfulWithRedirectTest extends AuthControllerPostSimpleSuccessfulTestCase {
+	public function setUp() {
+		parent::setUp();
+
+		$this->postDispatch('/opac/auth/login',
+												['username' => 'foo', 
+												 'password' => 'bar',
+												 'redirect' => '/opac/paniers']);
+	}
+
+
+	/** @test */
+	public function responseShouldBeAJSThatRedirectsToPaniers() {
+		$this->assertXPathContentContains('//script', 'replace(\'/opac/paniers\'', $this->_response->getBody());
+	}
+}
+
+
+
+
 class AuthControllerPostSuccessfulFromCASClientTest extends AuthControllerPostSimpleSuccessfulTestCase {
 	public function setUp() {
 		parent::setUp();
diff --git a/tests/application/modules/opac/controllers/PanierControllerTest.php b/tests/application/modules/opac/controllers/PanierControllerTest.php
index baece07e17d..c8fdb25b22a 100644
--- a/tests/application/modules/opac/controllers/PanierControllerTest.php
+++ b/tests/application/modules/opac/controllers/PanierControllerTest.php
@@ -601,6 +601,23 @@ class PanierControllerAjoutAjaxNotLoggedTest extends PanierControllerTestCase {
 
 
 
+class PanierControllerNotLoggedTest extends PanierControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+		ZendAfi_Auth::getInstance()->getStorage()->clear();
+		$this->dispatch('/panier/index',true);
+	}
+
+
+	/** @test */
+	public function responseShouldRenderLoginForm() {
+		$this->assertXPath('//input[@name="username"]');
+	}
+}
+
+
+
+
 class PanierControllerAjoutAjaxLoggedManonWithoutPanierTest extends PanierControllerTestCase {
 	protected $_xpath, $_response;
 
-- 
GitLab