From 60fdc13ae60a4d1bb62cbf29a8ab4b5ac1159709 Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind@owncloud.com>
Date: Tue, 22 May 2012 20:22:53 +0200
Subject: [PATCH] enable running unit tests from cli

---
 lib/base.php    | 25 ++++++++++++++++---------
 tests/index.php | 10 ++++++++--
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/lib/base.php b/lib/base.php
index fb2d978f44..f3d6689fa2 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -78,6 +78,10 @@ class OC{
 	 * requested file of app
 	 */
 	public static $REQUESTEDFILE = '';
+	/**
+	 * check if owncloud runs in cli mode
+	 */
+	public static $CLI = false;
 	/**
 	 * SPL autoload
 	 */
@@ -320,6 +324,7 @@ class OC{
 		if (defined('DEBUG') && DEBUG){
 			ini_set('display_errors', 1);
 		}
+		self::$CLI=(php_sapi_name() == 'cli');
 
 		date_default_timezone_set('Europe/Berlin');
 		ini_set('arg_separator.output','&amp;');
@@ -369,15 +374,17 @@ class OC{
 		self::checkInstalled();
 		self::checkSSL();
 
-                // CSRF protection
-                if(isset($_SERVER['HTTP_REFERER'])) $referer=$_SERVER['HTTP_REFERER']; else $referer='';
-                if(isset($_SERVER['HTTPS']) and $_SERVER['HTTPS']<>'') $protocol='https://'; else $protocol='http://';
-                $server=$protocol.$_SERVER['SERVER_NAME'];
-                if(($_SERVER['REQUEST_METHOD']=='POST') and (substr($referer,0,strlen($server))<>$server)) {
-                        $url = $protocol.$_SERVER['SERVER_NAME'].OC::$WEBROOT.'/index.php';
-                        header("Location: $url");
-                        exit();
-                } 
+		// CSRF protection
+		if(isset($_SERVER['HTTP_REFERER'])) $referer=$_SERVER['HTTP_REFERER']; else $referer='';
+		if(isset($_SERVER['HTTPS']) and $_SERVER['HTTPS']<>'') $protocol='https://'; else $protocol='http://';
+		if(!self::$CLI){
+			$server=$protocol.$_SERVER['SERVER_NAME'];
+			if(($_SERVER['REQUEST_METHOD']=='POST') and (substr($referer,0,strlen($server))<>$server)) {
+				$url = $protocol.$_SERVER['SERVER_NAME'].OC::$WEBROOT.'/index.php';
+				header("Location: $url");
+				exit();
+			}
+		}
 
 		self::initSession();
 		self::initTemplateEngine();
diff --git a/tests/index.php b/tests/index.php
index 6dec1b050f..9c5178f81a 100644
--- a/tests/index.php
+++ b/tests/index.php
@@ -38,7 +38,13 @@ foreach($apps as $app){
 }
 
 function loadTests($dir=''){
-	$test=isset($_GET['test'])?$_GET['test']:false;
+	if(OC::$CLI){
+		$reporter='TextReporter';
+		$test=isset($_SERVER['argv'][1])?$_SERVER['argv'][1]:false;
+	}else{
+		$reporter='HtmlReporter';
+		$test=isset($_GET['test'])?$_GET['test']:false;
+	}
 	if($dh=opendir($dir)){
 		while($name=readdir($dh)){
 			if(substr($name,0,1)!='.'){//no hidden files, '.' or '..'
@@ -51,7 +57,7 @@ function loadTests($dir=''){
 						$testCase=new TestSuite($name);
 						$testCase->addFile($file);
 						if($testCase->getSize()>0){
-							$testCase->run(new HtmlReporter());
+							$testCase->run(new $reporter());
 						}
 					}
 				}
-- 
GitLab