From a470100275a0596d57593eb2670072fb45d091ec Mon Sep 17 00:00:00 2001
From: llaffont <laurent.laffont@gmail.com>
Date: Tue, 4 Mar 2014 11:09:36 +0100
Subject: [PATCH] Cosmogramme: add test coverage for javascript url redirection

---
 cosmogramme/php/classes/classe_jsredirect.php | 33 ++++++++++++
 cosmogramme/php/fonctions/fonctions_base.php  | 15 +++---
 .../tests/php/classes/JSRedirectTest.php      | 52 +++++++++++++++++++
 3 files changed, 91 insertions(+), 9 deletions(-)
 create mode 100644 cosmogramme/php/classes/classe_jsredirect.php
 create mode 100644 cosmogramme/tests/php/classes/JSRedirectTest.php

diff --git a/cosmogramme/php/classes/classe_jsredirect.php b/cosmogramme/php/classes/classe_jsredirect.php
new file mode 100644
index 00000000000..6f8b992bd11
--- /dev/null
+++ b/cosmogramme/php/classes/classe_jsredirect.php
@@ -0,0 +1,33 @@
+<?PHP
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * AFI-OPAC 2.0 is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+class JSRedirect {
+	public function render($url) {
+		if ((0!== strpos($url, URL_BASE)) && (strleft($url,4) != "http"))
+			$url=URL_BASE."php/".$url;
+	return 
+		'<script>'
+		.'document.location="'.$url.'"'
+		.'</script>';
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/cosmogramme/php/fonctions/fonctions_base.php b/cosmogramme/php/fonctions/fonctions_base.php
index 5b82fbce980..32bc82e0164 100644
--- a/cosmogramme/php/fonctions/fonctions_base.php
+++ b/cosmogramme/php/fonctions/fonctions_base.php
@@ -52,16 +52,13 @@ function getClefSecurite()
 	$clef=md5($clef);
 	return $clef;
 }
+
+
 // Redirection en javascript
-function redirection( $url )
-{
-	if ((0!== strpos($url, URL_BASE)) && (strleft($url,4) != "http"))
-		$url=URL_BASE."php/".$url;
-	print("<script>");
-	print("document.location='".$url."'");
-	print("</script>");
-	print("</body>");
-	print("</html>");
+function redirection( $url ) {
+	require_once('classe_jsredirect.php');
+	$html = (new  JSRedirect())->render($url);
+	print ($html.'</body></html');
 	exit;
 }
 
diff --git a/cosmogramme/tests/php/classes/JSRedirectTest.php b/cosmogramme/tests/php/classes/JSRedirectTest.php
new file mode 100644
index 00000000000..8f90b108f9f
--- /dev/null
+++ b/cosmogramme/tests/php/classes/JSRedirectTest.php
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * AFI-OPAC 2.0 is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+require_once('classe_jsredirect.php');
+
+class JSRedirectTest extends PHPUnit_Framework_TestCase {
+	public function setUp() {
+		$this->_redirect = new JSRedirect();
+	}
+
+
+	/** @test */
+	public function withUrlBaseAsPrefixShouldNotChangeURL() {
+		$this->assertContains('document.location="'.URL_BASE.'/url.php"', 
+													$this->_redirect->render(URL_BASE.'/url.php'));
+		
+	} 
+
+
+	/** @test */
+	public function withNoHttpAsPrefixShouldNotAddUrlBase() {
+		$this->assertContains('document.location="'.URL_BASE.'php/url.php"', 
+													$this->_redirect->render('url.php'));
+	}
+
+
+	/** @test */
+	public function withHttpAsPrefixShouldNChangeUrl() {
+		$this->assertContains('document.location="http://wikipedia.org"', 
+													$this->_redirect->render('http://wikipedia.org'));
+	}
+}
+
+?>
\ No newline at end of file
-- 
GitLab