Merge pull request #1402 from toopay/test-suite

Continuation of Unit-Testing
diff --git a/.travis.yml b/.travis.yml
index 6a7d378..31b74b1 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -19,7 +19,7 @@
   - sh -c "if [ '$DB' = 'pgsql' ] || [ '$DB' = 'pdo/pgsql' ]; then psql -c 'create database ci_test;' -U postgres; fi"
   - sh -c "if [ '$DB' = 'mysql' ] || [ '$DB' = 'pdo/mysql' ]; then mysql -e 'create database IF NOT EXISTS ci_test;'; fi"
 
-script: phpunit --configuration tests/travis/$DB.phpunit.xml
+script: phpunit --coverage-text --configuration tests/travis/$DB.phpunit.xml
 
 branches:
   only:
diff --git a/system/core/Security.php b/system/core/Security.php
index f953011..9b7ba57 100755
--- a/system/core/Security.php
+++ b/system/core/Security.php
@@ -191,6 +191,7 @@
 	 * Set Cross Site Request Forgery Protection Cookie
 	 *
 	 * @return	object
+	 * @codeCoverageIgnore
 	 */
 	public function csrf_set_cookie()
 	{
diff --git a/tests/codeigniter/core/Security_test.php b/tests/codeigniter/core/Security_test.php
index 1796ba7..b2f8c69 100644
--- a/tests/codeigniter/core/Security_test.php
+++ b/tests/codeigniter/core/Security_test.php
@@ -70,4 +70,36 @@
 
 		$this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $harmless_string);
 	}
+
+	// --------------------------------------------------------------------
+	
+	public function test_xss_hash()
+	{
+		$this->assertEmpty($this->security->xss_hash);
+
+		// Perform hash
+		$this->security->xss_hash();
+
+		$this->assertTrue(preg_match('#^[0-9a-f]{32}$#iS', $this->security->xss_hash) === 1);
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function test_entity_decode()
+	{
+		$encoded = '<div>Hello <b>Booya</b></div>';
+		$decoded = $this->security->entity_decode($encoded);
+
+		$this->assertEquals('<div>Hello <b>Booya</b></div>', $decoded);
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function test_sanitize_filename()
+	{
+		$filename = './<!--foo-->';
+		$safe_filename = $this->security->sanitize_filename($filename);
+
+		$this->assertEquals('foo', $safe_filename);
+	}
 }
\ No newline at end of file
diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php
index 13f338c..f5133de 100644
--- a/tests/codeigniter/libraries/Table_test.php
+++ b/tests/codeigniter/libraries/Table_test.php
@@ -291,6 +291,26 @@
 		);
 	}
 	
-	// Test main generate method
-	// --------------------------------------------------------------------
+	function test_generate()
+	{
+		// Prepare the data
+		$data = array(
+			array('Name', 'Color', 'Size'),
+			array('Fred', 'Blue', 'Small'),
+			array('Mary', 'Red', 'Large'),
+			array('John', 'Green', 'Medium')	
+		);
+
+		$table = $this->table->generate($data);
+
+		// Test the table header
+		$this->assertTrue(strpos($table, '<th>Name</th>') !== FALSE);
+		$this->assertTrue(strpos($table, '<th>Color</th>') !== FALSE);
+		$this->assertTrue(strpos($table, '<th>Size</th>') !== FALSE);
+
+		// Test the first entry
+		$this->assertTrue(strpos($table, '<td>Fred</td>') !== FALSE);
+		$this->assertTrue(strpos($table, '<td>Blue</td>') !== FALSE);
+		$this->assertTrue(strpos($table, '<td>Small</td>') !== FALSE);
+	}
 }
\ No newline at end of file
diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php
index 92c9bea..441c889 100644
--- a/tests/mocks/autoloader.php
+++ b/tests/mocks/autoloader.php
@@ -22,7 +22,7 @@
 	);
 
 	$ci_libraries = array(
-		'Calendar', 'Cart', 'Driver',
+		'Calendar', 'Cart', 'Driver_Library',
 		'Email', 'Encrypt', 'Form_validation',
 		'Ftp', 'Image_lib', 'Javascript',
 		'Log', 'Migration', 'Pagination',
@@ -50,7 +50,7 @@
 		elseif (in_array($subclass, $ci_libraries))
 		{
 			$dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR;
-			$class = $subclass;
+			$class = ($subclass == 'Driver_Library') ? 'Driver' : $subclass;
 		}
 		elseif (preg_match('/^CI_DB_(.+)_(driver|forge|result|utility)$/', $class, $m) && count($m) == 3)
 		{
diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php
index fc94d7f..e745766 100644
--- a/tests/mocks/core/common.php
+++ b/tests/mocks/core/common.php
@@ -2,53 +2,65 @@
 
 // Set up the global CI functions in their most minimal core representation
 
-function &get_instance() 
+if ( ! function_exists('get_instance'))
 {
-	$test = CI_TestCase::instance();
-	$instance = $test->ci_instance();
-	return $instance;
+	function &get_instance() 
+	{
+		$test = CI_TestCase::instance();
+		$instance = $test->ci_instance();
+		return $instance;
+	}
 }
 
 // --------------------------------------------------------------------
 
-function &get_config() {
-	$test = CI_TestCase::instance();
-	$config = $test->ci_get_config();
+if ( ! function_exists('get_config'))
+{
+	function &get_config() {
+		$test = CI_TestCase::instance();
+		$config = $test->ci_get_config();
+			
+		return $config;
+	}
+}
+
+if ( ! function_exists('config_item'))
+{
+	function config_item($item)
+	{
+		$config =& get_config();
 		
-	return $config;
-}
-
-function config_item($item)
-{
-	$config =& get_config();
-	
-	if ( ! isset($config[$item]))
-	{
-		return FALSE;
+		if ( ! isset($config[$item]))
+		{
+			return FALSE;
+		}
+		
+		return $config[$item];
 	}
-	
-	return $config[$item];
 }
 
 // --------------------------------------------------------------------
 
-function load_class($class, $directory = 'libraries', $prefix = 'CI_')
+if ( ! function_exists('load_class'))
 {
-	if ($directory != 'core' OR $prefix != 'CI_')
+	function load_class($class, $directory = 'libraries', $prefix = 'CI_')
 	{
-		throw new Exception('Not Implemented: Non-core load_class()');
+		if ($directory != 'core' OR $prefix != 'CI_')
+		{
+			throw new Exception('Not Implemented: Non-core load_class()');
+		}
+		
+		$test = CI_TestCase::instance();
+		
+		$obj =& $test->ci_core_class($class);
+		
+		if (is_string($obj))
+		{
+			throw new Exception('Bad Isolation: Use ci_set_core_class to set '.$class.'');
+		}
+		
+		return $obj;
 	}
-	
-	$test = CI_TestCase::instance();
-	
-	$obj =& $test->ci_core_class($class);
-	
-	if (is_string($obj))
-	{
-		throw new Exception('Bad Isolation: Use ci_set_core_class to set '.$class.'');
-	}
-	
-	return $obj;
 }
 
 // This is sort of meh. Should probably be mocked up with
@@ -57,76 +69,103 @@
 // bootstrap testsuite.
 // --------------------------------------------------------------------
 
-function remove_invisible_characters($str, $url_encoded = TRUE)
+if ( ! function_exists('remove_invisible_characters'))
 {
-	$non_displayables = array();
-	
-	// every control character except newline (dec 10)
-	// carriage return (dec 13), and horizontal tab (dec 09)
-	
-	if ($url_encoded)
+	function remove_invisible_characters($str, $url_encoded = TRUE)
 	{
-		$non_displayables[] = '/%0[0-8bcef]/';	// url encoded 00-08, 11, 12, 14, 15
-		$non_displayables[] = '/%1[0-9a-f]/';	// url encoded 16-31
-	}
-	
-	$non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';	// 00-08, 11, 12, 14-31, 127
+		$non_displayables = array();
+		
+		// every control character except newline (dec 10)
+		// carriage return (dec 13), and horizontal tab (dec 09)
+		
+		if ($url_encoded)
+		{
+			$non_displayables[] = '/%0[0-8bcef]/';	// url encoded 00-08, 11, 12, 14, 15
+			$non_displayables[] = '/%1[0-9a-f]/';	// url encoded 16-31
+		}
+		
+		$non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';	// 00-08, 11, 12, 14-31, 127
 
-	do
-	{
-		$str = preg_replace($non_displayables, '', $str, -1, $count);
-	}
-	while ($count);
+		do
+		{
+			$str = preg_replace($non_displayables, '', $str, -1, $count);
+		}
+		while ($count);
 
-	return $str;
+		return $str;
+	}
 }
 
 
 // Clean up error messages
 // --------------------------------------------------------------------
 
-function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
+if ( ! function_exists('show_error'))
 {
-	throw new RuntimeException('CI Error: '.$message);
+	function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
+	{
+		throw new RuntimeException('CI Error: '.$message);
+	}
 }
 
-function show_404($page = '', $log_error = TRUE)
+if ( ! function_exists('show_404'))
 {
-	throw new RuntimeException('CI Error: 404');
+	function show_404($page = '', $log_error = TRUE)
+	{
+		throw new RuntimeException('CI Error: 404');
+	}
 }
 
-function _exception_handler($severity, $message, $filepath, $line)
+if ( ! function_exists('_exception_handler'))
 {
-	throw new RuntimeException('CI Exception: '.$message.' | '.$filepath.' | '.$line);
+	function _exception_handler($severity, $message, $filepath, $line)
+	{
+		throw new RuntimeException('CI Exception: '.$message.' | '.$filepath.' | '.$line);
+	}
 }
 
 
 // We assume a few things about our environment ...
 // --------------------------------------------------------------------
 
-function is_php($version = '5.0.0')
+if ( ! function_exists('is_php'))
 {
-	return ! (version_compare(PHP_VERSION, $version) < 0);
+	function is_php($version = '5.0.0')
+	{
+		return ! (version_compare(PHP_VERSION, $version) < 0);
+	}
 }
 
-function is_really_writable($file)
+if ( ! function_exists('is_really_writable'))
 {
-	return is_writable($file);
+	function is_really_writable($file)
+	{
+		return is_writable($file);
+	}
 }
 
-function is_loaded()
+if ( ! function_exists('is_loaded'))
 {
-	throw new Exception('Bad Isolation: mock up environment');
+	function is_loaded()
+	{
+		throw new Exception('Bad Isolation: mock up environment');
+	}
 }
 
-function log_message($level = 'error', $message, $php_error = FALSE)
+if ( ! function_exists('log_message'))
 {
-	return TRUE;
+	function log_message($level = 'error', $message, $php_error = FALSE)
+	{
+		return TRUE;
+	}
 }
 
-function set_status_header($code = 200, $text = '')
+if ( ! function_exists('set_status_header'))
 {
-	return TRUE;
+	function set_status_header($code = 200, $text = '')
+	{
+		return TRUE;
+	}
 }
 
 // EOF
\ No newline at end of file
diff --git a/tests/travis/mysql.phpunit.xml b/tests/travis/mysql.phpunit.xml
index 1792ae3..38c8eba 100644
--- a/tests/travis/mysql.phpunit.xml
+++ b/tests/travis/mysql.phpunit.xml
@@ -17,10 +17,9 @@
 			<directory suffix="test.php">../codeigniter</directory>
 		</testsuite>
 	</testsuites>
-	<filters>
-		<blacklist>
-			<directory suffix=".php">PEAR_INSTALL_DIR</directory>
-			<directory suffix=".php">PHP_LIBDIR</directory>
-		</blacklist>
-	</filters>
+	<filter>
+        <whitelist addUncoveredFilesFromWhitelist="true">
+            <directory suffix=".php">../../system</directory>
+        </whitelist>
+	</filter>
 </phpunit>
\ No newline at end of file
diff --git a/tests/travis/pdo/mysql.phpunit.xml b/tests/travis/pdo/mysql.phpunit.xml
index 602030d..c3113a6 100644
--- a/tests/travis/pdo/mysql.phpunit.xml
+++ b/tests/travis/pdo/mysql.phpunit.xml
@@ -17,10 +17,9 @@
 			<directory suffix="test.php">../../codeigniter</directory>
 		</testsuite>
 	</testsuites>
-	<filters>
-		<blacklist>
-			<directory suffix=".php">PEAR_INSTALL_DIR</directory>
-			<directory suffix=".php">PHP_LIBDIR</directory>
-		</blacklist>
-	</filters>
+	<filter>
+        <whitelist addUncoveredFilesFromWhitelist="true">
+            <directory suffix=".php">../../../system</directory>
+        </whitelist>
+	</filter>
 </phpunit>
\ No newline at end of file
diff --git a/tests/travis/pdo/pgsql.phpunit.xml b/tests/travis/pdo/pgsql.phpunit.xml
index 77e1493..2320255 100644
--- a/tests/travis/pdo/pgsql.phpunit.xml
+++ b/tests/travis/pdo/pgsql.phpunit.xml
@@ -17,10 +17,9 @@
 			<directory suffix="test.php">../../codeigniter</directory>
 		</testsuite>
 	</testsuites>
-	<filters>
-		<blacklist>
-			<directory suffix=".php">PEAR_INSTALL_DIR</directory>
-			<directory suffix=".php">PHP_LIBDIR</directory>
-		</blacklist>
-	</filters>
+	<filter>
+        <whitelist addUncoveredFilesFromWhitelist="true">
+            <directory suffix=".php">../../../system</directory>
+        </whitelist>
+	</filter>
 </phpunit>
\ No newline at end of file
diff --git a/tests/travis/pdo/sqlite.phpunit.xml b/tests/travis/pdo/sqlite.phpunit.xml
index cdccef0..3d12567 100644
--- a/tests/travis/pdo/sqlite.phpunit.xml
+++ b/tests/travis/pdo/sqlite.phpunit.xml
@@ -17,10 +17,9 @@
 			<directory suffix="test.php">../../codeigniter</directory>
 		</testsuite>
 	</testsuites>
-	<filters>
-		<blacklist>
-			<directory suffix=".php">PEAR_INSTALL_DIR</directory>
-			<directory suffix=".php">PHP_LIBDIR</directory>
-		</blacklist>
-	</filters>
+	<filter>
+        <whitelist addUncoveredFilesFromWhitelist="true">
+            <directory suffix=".php">../../../system</directory>
+        </whitelist>
+	</filter>
 </phpunit>
\ No newline at end of file
diff --git a/tests/travis/pgsql.phpunit.xml b/tests/travis/pgsql.phpunit.xml
index dfc1bff..51e433d 100644
--- a/tests/travis/pgsql.phpunit.xml
+++ b/tests/travis/pgsql.phpunit.xml
@@ -17,10 +17,9 @@
 			<directory suffix="test.php">../codeigniter</directory>
 		</testsuite>
 	</testsuites>
-	<filters>
-		<blacklist>
-			<directory suffix=".php">PEAR_INSTALL_DIR</directory>
-			<directory suffix=".php">PHP_LIBDIR</directory>
-		</blacklist>
-	</filters>
+	<filter>
+        <whitelist addUncoveredFilesFromWhitelist="true">
+            <directory suffix=".php">../../system</directory>
+        </whitelist>
+	</filter>
 </phpunit>
\ No newline at end of file
diff --git a/tests/travis/sqlite.phpunit.xml b/tests/travis/sqlite.phpunit.xml
index 3223da5..7011657 100644
--- a/tests/travis/sqlite.phpunit.xml
+++ b/tests/travis/sqlite.phpunit.xml
@@ -17,10 +17,9 @@
 			<directory suffix="test.php">../codeigniter</directory>
 		</testsuite>
 	</testsuites>
-	<filters>
-		<blacklist>
-			<directory suffix=".php">PEAR_INSTALL_DIR</directory>
-			<directory suffix=".php">PHP_LIBDIR</directory>
-		</blacklist>
-	</filters>
+	<filter>
+        <whitelist addUncoveredFilesFromWhitelist="true">
+            <directory suffix=".php">../../system</directory>
+        </whitelist>
+	</filter>
 </phpunit>
\ No newline at end of file