Skip to content

Commit

Permalink
[develop-olympus] Backported 3.1 unit tests to 3.0.
Browse files Browse the repository at this point in the history
Start adding unit tests for bugs you fix! Tests for anything are
welcome really. We have to work on these a lot.
  • Loading branch information
naderman committed Mar 10, 2010
1 parent d9567f1 commit 60bd1ed
Show file tree
Hide file tree
Showing 29 changed files with 1,766 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/all_tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

error_reporting(E_ALL);

if (!defined('PHPUnit_MAIN_METHOD'))
{
define('PHPUnit_MAIN_METHOD', 'phpbb_all_tests::main');
}

require_once 'test_framework/framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';

require_once 'utf/all_tests.php';
require_once 'request/all_tests.php';
require_once 'security/all_tests.php';
require_once 'template/all_tests.php';
require_once 'text_processing/all_tests.php';

// exclude the test directory from code coverage reports
PHPUnit_Util_Filter::addDirectoryToFilter('./');

class phpbb_all_tests
{
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}

public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('phpBB');

$suite->addTest(phpbb_utf_all_tests::suite());
$suite->addTest(phpbb_request_all_tests::suite());
$suite->addTest(phpbb_security_all_tests::suite());
$suite->addTest(phpbb_template_all_tests::suite());
$suite->addTest(phpbb_text_processing_all_tests::suite());

return $suite;
}
}

if (PHPUnit_MAIN_METHOD == 'phpbb_all_tests::main')
{
phpbb_all_tests::main();
}

41 changes: 41 additions & 0 deletions tests/request/all_tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

if (!defined('PHPUnit_MAIN_METHOD'))
{
define('PHPUnit_MAIN_METHOD', 'phpbb_request_all_tests::main');
}

require_once 'test_framework/framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';

require_once 'request/request_var.php';

class phpbb_request_all_tests
{
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}

public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('phpBB Request Parameter Handling');

$suite->addTestSuite('phpbb_request_request_var_test');

return $suite;
}
}

if (PHPUnit_MAIN_METHOD == 'phpbb_request_all_tests::main')
{
phpbb_request_all_tests::main();
}

180 changes: 180 additions & 0 deletions tests/request/request_var.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

require_once 'test_framework/framework.php';
require_once '../phpBB/includes/functions.php';

class phpbb_request_request_var_test extends phpbb_test_case
{
/**
* @dataProvider request_variables
*/
public function test_post($variable_value, $default, $multibyte, $expected)
{
$variable_name = 'name';
$this->unset_variables($variable_name);

$_POST[$variable_name] = $variable_value;
$_REQUEST[$variable_name] = $variable_value;

$result = request_var($variable_name, $default, $multibyte);

$label = 'Requesting POST variable, converting from ' . gettype($variable_value) . ' to ' . gettype($default) . (($multibyte) ? ' multibyte' : '');
$this->assertEquals($expected, $result, $label);
}

/**
* @dataProvider request_variables
*/
public function test_get($variable_value, $default, $multibyte, $expected)
{
$variable_name = 'name';
$this->unset_variables($variable_name);

$_GET[$variable_name] = $variable_value;
$_REQUEST[$variable_name] = $variable_value;

$result = request_var($variable_name, $default, $multibyte);

$label = 'Requesting GET variable, converting from ' . gettype($variable_value) . ' to ' . gettype($default) . (($multibyte) ? ' multibyte' : '');
$this->assertEquals($expected, $result, $label);
}

/**
* @dataProvider request_variables
*/
public function test_cookie($variable_value, $default, $multibyte, $expected)
{
$variable_name = 'name';
$this->unset_variables($variable_name);

$_GET[$variable_name] = false;
$_POST[$variable_name] = false;
$_REQUEST[$variable_name] = false;
$_COOKIE[$variable_name] = $variable_value;

$result = request_var($variable_name, $default, $multibyte, true);

$label = 'Requesting COOKIE variable, converting from ' . gettype($variable_value) . ' to ' . gettype($default) . (($multibyte) ? ' multibyte' : '');
$this->assertEquals($expected, $result, $label);
}

/**
* Helper for unsetting globals
*/
private function unset_variables($var)
{
unset($_GET[$var], $_POST[$var], $_REQUEST[$var], $_COOKIE[$var]);
}

public static function request_variables()
{
return array(
// strings
array('abc', '', false, 'abc'),
array(' some spaces ', '', true, 'some spaces'),
array("\r\rsome\rcarriage\r\rreturns\r", '', true, "some\ncarriage\n\nreturns"),
array("\n\nsome\ncarriage\n\nreturns\n", '', true, "some\ncarriage\n\nreturns"),
array("\r\n\r\nsome\r\ncarriage\r\n\r\nreturns\r\n", '', true, "some\ncarriage\n\nreturns"),
array("we\xC2\xA1rd\xE1\x9A\x80ch\xCE\xB1r\xC2\xADacters", '', true, "we\xC2\xA1rd\xE1\x9A\x80ch\xCE\xB1r\xC2\xADacters"),
array("we\xC2\xA1rd\xE1\x9A\x80ch\xCE\xB1r\xC2\xADacters", '', false, "we??rd???ch??r??acters"),
array("Some <html> \"entities\" like &", '', true, "Some &lt;html&gt; &quot;entities&quot; like &amp;"),

// integers
array('1234', 0, false, 1234),
array('abc', 12, false, 0),
array('324abc', 0, false, 324),

// string to array
array('123', array(0), false, array()),
array('123', array(''), false, array()),

// 1 dimensional arrays
array(
// input:
array('123', 'abc'),
// default:
array(''),
false,
// expected:
array('123', 'abc')
),
array(
// input:
array('123', 'abc'),
// default:
array(999),
false,
// expected:
array(123, 0)
),
array(
// input:
array('xyz' => '123', 'abc' => 'abc'),
// default:
array('' => ''),
false,
// expected:
array('xyz' => '123', 'abc' => 'abc')
),
array(
// input:
array('xyz' => '123', 'abc' => 'abc'),
// default:
array('' => 0),
false,
// expected:
array('xyz' => 123, 'abc' => 0)
),

// 2 dimensional arrays
array(
// input:
'',
// default:
array(array(0)),
false,
// expected:
array()
),
array(
// input:
array(
'xyz' => array('123', 'def'),
'abc' => 'abc'
),
// default:
array('' => array('')),
false,
// expected:
array(
'xyz' => array('123', 'def'),
'abc' => array()
)
),
array(
// input:
array(
'xyz' => array('123', 'def'),
'abc' => 'abc'
),
// default:
array('' => array(0)),
false,
// expected:
array(
'xyz' => array(123, 0),
'abc' => array()
)
),
);
}

}

86 changes: 86 additions & 0 deletions tests/security/all_tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

if (!defined('PHPUnit_MAIN_METHOD'))
{
define('PHPUnit_MAIN_METHOD', 'phpbb_security_all_tests::main');
}

require_once 'test_framework/framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';

require_once 'security/extract_current_page.php';
require_once 'security/redirect.php';

class phpbb_security_all_tests extends PHPUnit_Framework_TestSuite
{
/**
* Set up the required user object and server variables for the suites
*/
protected function setUp()
{
global $user, $phpbb_root_path;

// Put this into a global function being run by every test to init a proper user session
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_PORT'] = 80;
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['QUERY_STRING'] = '';
$_SERVER['REQUEST_URI'] = '/tests/';
$_SERVER['SCRIPT_NAME'] = '/tests/index.php';
$_SERVER['PHP_SELF'] = '/tests/index.php';
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14';
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';

/*
[HTTP_ACCEPT_ENCODING] => gzip,deflate
[HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
DOCUMENT_ROOT] => /var/www/
[SCRIPT_FILENAME] => /var/www/tests/index.php
*/

// Set no user and trick a bit to circumvent errors
$user = new user();
$user->lang = true;
$user->browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
$user->referer = (!empty($_SERVER['HTTP_REFERER'])) ? htmlspecialchars((string) $_SERVER['HTTP_REFERER']) : '';
$user->forwarded_for = (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? (string) $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
$user->host = (!empty($_SERVER['HTTP_HOST'])) ? (string) strtolower($_SERVER['HTTP_HOST']) : ((!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME'));
$user->page = session::extract_current_page($phpbb_root_path);
}

protected function tearDown()
{
global $user;
$user = NULL;
}

public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}

public static function suite()
{
// I bet there is a better method calling this... :)
$suite = new phpbb_security_all_tests('phpBB Security Fixes');

$suite->addTestSuite('phpbb_security_extract_current_page_test');
$suite->addTestSuite('phpbb_security_redirect_test');

return $suite;
}
}

if (PHPUnit_MAIN_METHOD == 'phpbb_security_all_tests::main')
{
phpbb_security_all_tests::main();
}
Loading

0 comments on commit 60bd1ed

Please sign in to comment.