diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bbcode/parser_test.php | 29 | ||||
-rw-r--r-- | tests/class_loader/cache_mock.php | 29 | ||||
-rw-r--r-- | tests/class_loader/class_loader_test.php | 74 | ||||
-rw-r--r-- | tests/class_loader/includes/class_name.php | 6 | ||||
-rw-r--r-- | tests/class_loader/includes/dir.php | 6 | ||||
-rw-r--r-- | tests/class_loader/includes/dir/class_name.php | 6 | ||||
-rw-r--r-- | tests/class_loader/includes/dir/subdir/class_name.php | 6 | ||||
-rw-r--r-- | tests/class_loader/includes/dir2/dir2.php | 6 | ||||
-rw-r--r-- | tests/download/http_byte_range.php | 61 | ||||
-rw-r--r-- | tests/network/inet_ntop_pton.php | 54 | ||||
-rw-r--r-- | tests/network/ip_normalise.php | 64 | ||||
-rw-r--r-- | tests/request/deactivated_super_global.php | 25 | ||||
-rw-r--r-- | tests/request/request.php | 82 | ||||
-rw-r--r-- | tests/request/request_var.php | 92 | ||||
-rw-r--r-- | tests/request/type_cast_helper.php | 53 | ||||
-rw-r--r-- | tests/template/template.php | 27 | ||||
-rw-r--r-- | tests/template/templates/loop_expressions.html | 11 |
17 files changed, 606 insertions, 25 deletions
diff --git a/tests/bbcode/parser_test.php b/tests/bbcode/parser_test.php new file mode 100644 index 0000000000..4a11f47680 --- /dev/null +++ b/tests/bbcode/parser_test.php @@ -0,0 +1,29 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2008 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +// require_once __DIR__ . '/../../phpBB/includes/bbcode/bbcode_parser_base.php'; +// require_once __DIR__ . '/../../phpBB/includes/bbcode/bbcode_parser.php'; + +class phpbb_bbcode_parser_test extends PHPUnit_Framework_TestCase +{ + public function test_both_passes() + { + $this->markTestIncomplete('New bbcode parser has not been backported from feature/ascraeus-experiment yet.'); + + $parser = new phpbb_bbcode_parser(); + + $result = $parser->first_pass('[i]Italic [u]underlined text[/u][/i]'); + $result = $parser->second_pass($result); + + $expected = '<span style="font-style: italic">Italic <span style="text-decoration: underline">underlined text</span></span>'; + + $this->assertEquals($expected, $result, 'Simple nested BBCode first+second pass'); + } +} diff --git a/tests/class_loader/cache_mock.php b/tests/class_loader/cache_mock.php new file mode 100644 index 0000000000..b254978fcc --- /dev/null +++ b/tests/class_loader/cache_mock.php @@ -0,0 +1,29 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2008 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +class phpbb_cache_mock +{ + private $variables = array(); + + function get($var_name) + { + if (isset($this->variables[$var_name])) + { + return $this->variables[$var_name]; + } + + return false; + } + + function put($var_name, $value) + { + $this->variables[$var_name] = $value; + } +} diff --git a/tests/class_loader/class_loader_test.php b/tests/class_loader/class_loader_test.php new file mode 100644 index 0000000000..aef4f1de07 --- /dev/null +++ b/tests/class_loader/class_loader_test.php @@ -0,0 +1,74 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2008 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once __DIR__ . '/cache_mock.php'; + +require_once __DIR__ . '/../../phpBB/includes/class_loader.php'; + +class phpbb_class_loader_test extends PHPUnit_Framework_TestCase +{ + public function test_resolve_path() + { + $prefix = __DIR__ . '/'; + $class_loader = new phpbb_class_loader($prefix); + + $prefix .= 'includes/'; + + $this->assertEquals( + '', + $class_loader->resolve_path('phpbb_dir'), + 'Class with same name as a directory is unloadable' + ); + + $this->assertEquals( + $prefix . 'class_name.php', + $class_loader->resolve_path('phpbb_class_name'), + 'Top level class' + ); + $this->assertEquals( + $prefix . 'dir/class_name.php', + $class_loader->resolve_path('phpbb_dir_class_name'), + 'Class in a directory' + ); + $this->assertEquals( + $prefix . 'dir/subdir/class_name.php', + $class_loader->resolve_path('phpbb_dir_subdir_class_name'), + 'Class in a sub-directory' + ); + $this->assertEquals( + $prefix . 'dir2/dir2.php', + $class_loader->resolve_path('phpbb_dir2'), + 'Class with name of dir within dir (short class name)' + ); + } + + public function test_resolve_cached() + { + $cache = new phpbb_cache_mock; + $cache->put('class_loader', array('phpbb_a_cached_name' => 'a/cached_name')); + + $prefix = __DIR__ . '/'; + $class_loader = new phpbb_class_loader($prefix, '.php', $cache); + + $prefix .= 'includes/'; + + $this->assertEquals( + $prefix . 'dir/class_name.php', + $class_loader->resolve_path('phpbb_dir_class_name'), + 'Class in a directory' + ); + + $this->assertEquals( + $prefix . 'a/cached_name.php', + $class_loader->resolve_path('phpbb_a_cached_name'), + 'Class in a directory' + ); + } +} diff --git a/tests/class_loader/includes/class_name.php b/tests/class_loader/includes/class_name.php new file mode 100644 index 0000000000..e941173cdd --- /dev/null +++ b/tests/class_loader/includes/class_name.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_class_name +{ +} + diff --git a/tests/class_loader/includes/dir.php b/tests/class_loader/includes/dir.php new file mode 100644 index 0000000000..1c8930d8e7 --- /dev/null +++ b/tests/class_loader/includes/dir.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_dir +{ +} + diff --git a/tests/class_loader/includes/dir/class_name.php b/tests/class_loader/includes/dir/class_name.php new file mode 100644 index 0000000000..0675aa8fc5 --- /dev/null +++ b/tests/class_loader/includes/dir/class_name.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_dir_class_name +{ +} + diff --git a/tests/class_loader/includes/dir/subdir/class_name.php b/tests/class_loader/includes/dir/subdir/class_name.php new file mode 100644 index 0000000000..7321a609cc --- /dev/null +++ b/tests/class_loader/includes/dir/subdir/class_name.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_dir_subdir_class_name +{ +} + diff --git a/tests/class_loader/includes/dir2/dir2.php b/tests/class_loader/includes/dir2/dir2.php new file mode 100644 index 0000000000..01cf4086ff --- /dev/null +++ b/tests/class_loader/includes/dir2/dir2.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_dir2 +{ +} + diff --git a/tests/download/http_byte_range.php b/tests/download/http_byte_range.php new file mode 100644 index 0000000000..4f14746bef --- /dev/null +++ b/tests/download/http_byte_range.php @@ -0,0 +1,61 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once __DIR__ . '/../../phpBB/includes/functions_download.php'; + +class phpbb_download_http_byte_range_test extends phpbb_test_case +{ + public function test_find_range_request() + { + // Missing 'bytes=' prefix + $_SERVER['HTTP_RANGE'] = 'bztes='; + $this->assertEquals(false, phpbb_find_range_request()); + unset($_SERVER['HTTP_RANGE']); + + $_ENV['HTTP_RANGE'] = 'bztes='; + $this->assertEquals(false, phpbb_find_range_request()); + unset($_ENV['HTTP_RANGE']); + + $_SERVER['HTTP_RANGE'] = 'bytes=0-0,123-125'; + $this->assertEquals(array('0-0', '123-125'), phpbb_find_range_request()); + unset($_SERVER['HTTP_RANGE']); + } + + /** + * @dataProvider parse_range_request_data() + */ + public function test_parse_range_request($request_array, $filesize, $expected) + { + $this->assertEquals($expected, phpbb_parse_range_request($request_array, $filesize)); + } + + public function parse_range_request_data() + { + return array( + // Does not read until the end of file. + array( + array('3-4'), + 10, + false, + ), + + // Valid request, handle second range. + array( + array('0-0', '120-125'), + 125, + array( + 'byte_pos_start' => 120, + 'byte_pos_end' => 124, + 'bytes_requested' => 5, + 'bytes_total' => 125, + ) + ), + ); + } +} diff --git a/tests/network/inet_ntop_pton.php b/tests/network/inet_ntop_pton.php new file mode 100644 index 0000000000..7d4f44aaca --- /dev/null +++ b/tests/network/inet_ntop_pton.php @@ -0,0 +1,54 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once __DIR__ . '/../../phpBB/includes/functions.php'; + +class phpbb_network_inet_ntop_pton_test extends phpbb_test_case +{ + public function data_provider() + { + return array( + array('127.0.0.1', '7f000001'), + array('192.232.131.223', 'c0e883df'), + array('13.1.68.3', '0d014403'), + array('129.144.52.38', '81903426'), + + array('2001:280:0:10::5', '20010280000000100000000000000005'), + array('fe80::200:4cff:fefe:172f', 'fe8000000000000002004cfffefe172f'), + + array('::', '00000000000000000000000000000000'), + array('::1', '00000000000000000000000000000001'), + array('1::', '00010000000000000000000000000000'), + + array('1:1:0:0:1::', '00010001000000000001000000000000'), + + array('0:2:3:4:5:6:7:8', '00000002000300040005000600070008'), + array('1:2:0:4:5:6:7:8', '00010002000000040005000600070008'), + array('1:2:3:4:5:6:7:0', '00010002000300040005000600070000'), + + array('2001:0:0:1::1', '20010000000000010000000000000001'), + ); + } + + /** + * @dataProvider data_provider + */ + public function test_inet_ntop($address, $hex) + { + $this->assertEquals($address, phpbb_inet_ntop(pack('H*', $hex))); + } + + /** + * @dataProvider data_provider + */ + public function test_inet_pton($address, $hex) + { + $this->assertEquals($hex, bin2hex(phpbb_inet_pton($address))); + } +} diff --git a/tests/network/ip_normalise.php b/tests/network/ip_normalise.php new file mode 100644 index 0000000000..c10ea6e088 --- /dev/null +++ b/tests/network/ip_normalise.php @@ -0,0 +1,64 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once __DIR__ . '/../../phpBB/includes/functions.php'; + +class phpbb_network_ip_normalise_test extends phpbb_test_case +{ + public function data_provider() + { + return array( + // From: A Recommendation for IPv6 Address Text Representation + // http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-07 + + // Section 4: A Recommendation for IPv6 Text Representation + // Section 4.1: Handling Leading Zeros in a 16 Bit Field + array('2001:0db8::0001', '2001:db8::1'), + + // Section 4.2: "::" Usage + // Section 4.2.1: Shorten As Much As Possible + array('2001:db8::0:1', '2001:db8::1'), + + // Section 4.2.2: Handling One 16 Bit 0 Field + array('2001:db8::1:1:1:1:1', '2001:db8:0:1:1:1:1:1'), + + // Section 4.2.3: Choice in Placement of "::" + array('2001:db8:0:0:1:0:0:1', '2001:db8::1:0:0:1'), + + // Section 4.3: Lower Case + array('2001:DB8::1', '2001:db8::1'), + + // Section 5: Text Representation of Special Addresses + // We want to show IPv4-mapped addresses as plain IPv4 addresses, though. + array('::ffff:192.168.0.1', '192.168.0.1'), + array('0000::0000:ffff:c000:0280', '192.0.2.128'), + + // IPv6 addresses with the last 32-bit written in dotted-quad notation + // should be converted to hex-only IPv6 addresses. + array('2001:db8::192.0.2.128', '2001:db8::c000:280'), + + // Any string not passing the IPv4 or IPv6 regular expression + // is supposed to result in false being returned. + // Valid and invalid IP addresses are tested in + // tests/regex/ipv4.php and tests/regex/ipv6.php. + array('', false), + array('192.168.1.256', false), + array('::ffff:192.168.255.256', false), + array('::1111:2222:3333:4444:5555:6666::', false), + ); + } + + /** + * @dataProvider data_provider + */ + public function test_ip_normalise($ip_address, $expected) + { + $this->assertEquals($expected, phpbb_ip_normalise($ip_address)); + } +} diff --git a/tests/request/deactivated_super_global.php b/tests/request/deactivated_super_global.php new file mode 100644 index 0000000000..ea385831c9 --- /dev/null +++ b/tests/request/deactivated_super_global.php @@ -0,0 +1,25 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2009 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once __DIR__ . '/../../phpBB/includes/request/interface.php'; +require_once __DIR__ . '/../../phpBB/includes/request/deactivated_super_global.php'; + +class phpbb_deactivated_super_global_test extends phpbb_test_case +{ + /** + * Checks that on write access the correct error is thrown + */ + public function test_write_triggers_error() + { + $this->setExpectedTriggerError(E_USER_ERROR); + $obj = new phpbb_request_deactivated_super_global($this->getMock('phpbb_request_interface'), 'obj', phpbb_request_interface::POST); + $obj->offsetSet(0, 0); + } +} diff --git a/tests/request/request.php b/tests/request/request.php new file mode 100644 index 0000000000..7cec70b0d4 --- /dev/null +++ b/tests/request/request.php @@ -0,0 +1,82 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2009 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper_interface.php'; +require_once __DIR__ . '/../../phpBB/includes/request/interface.php'; +require_once __DIR__ . '/../../phpBB/includes/request/deactivated_super_global.php'; +require_once __DIR__ . '/../../phpBB/includes/request/request.php'; + +class phpbb_request_test extends phpbb_test_case +{ + private $type_cast_helper; + private $request; + + protected function setUp() + { + // populate super globals + $_POST['test'] = 1; + $_GET['test'] = 2; + $_COOKIE['test'] = 3; + $_REQUEST['test'] = 3; + $_GET['unset'] = ''; + + $this->type_cast_helper = $this->getMock('phpbb_request_type_cast_helper_interface'); + + $this->request = new phpbb_request($this->type_cast_helper); + } + + public function test_toggle_super_globals() + { + $this->assertTrue($this->request->super_globals_disabled(), 'Superglobals were not disabled'); + + $this->request->enable_super_globals(); + + $this->assertFalse($this->request->super_globals_disabled(), 'Superglobals were not enabled'); + + $this->assertEquals(1, $_POST['test'], 'Checking $_POST after enable_super_globals'); + $this->assertEquals(2, $_GET['test'], 'Checking $_GET after enable_super_globals'); + $this->assertEquals(3, $_COOKIE['test'], 'Checking $_COOKIE after enable_super_globals'); + $this->assertEquals(3, $_REQUEST['test'], 'Checking $_REQUEST after enable_super_globals'); + + $_POST['x'] = 2; + $this->assertEquals($_POST, $GLOBALS['_POST'], 'Checking whether $_POST can still be accessed via $GLOBALS[\'_POST\']'); + } + + /** + * Checks that directly accessing $_POST will trigger + * an error. + */ + public function test_disable_post_super_global() + { + $this->setExpectedTriggerError(E_USER_ERROR); + $_POST['test'] = 3; + } + + public function test_is_set_post() + { + $this->assertTrue($this->request->is_set_post('test')); + $this->assertFalse($this->request->is_set_post('unset')); + } + + public function test_variable_names() + { + $expected = array('test', 'unset'); + $result = $this->request->variable_names(); + $this->assertEquals($expected, $result); + } + + /** + * Makes sure super globals work properly after these tests + */ + protected function tearDown() + { + $this->request->enable_super_globals(); + } +} diff --git a/tests/request/request_var.php b/tests/request/request_var.php index 804e5d6390..64cdd9bc75 100644 --- a/tests/request/request_var.php +++ b/tests/request/request_var.php @@ -7,9 +7,14 @@ * */ +require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper_interface.php'; +require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper.php'; +require_once __DIR__ . '/../../phpBB/includes/request/deactivated_super_global.php'; +require_once __DIR__ . '/../../phpBB/includes/request/interface.php'; +require_once __DIR__ . '/../../phpBB/includes/request/request.php'; require_once __DIR__ . '/../../phpBB/includes/functions.php'; -class phpbb_request_request_var_test extends phpbb_test_case +class phpbb_request_var_test extends phpbb_test_case { /** * @dataProvider request_variables @@ -72,6 +77,47 @@ class phpbb_request_request_var_test extends phpbb_test_case unset($_GET[$var], $_POST[$var], $_REQUEST[$var], $_COOKIE[$var]); } + /** + * @dataProvider deep_access + * Only possible with 3.1.x (later) + */ + public function test_deep_multi_dim_array_access($path, $default, $expected) + { + $this->unset_variables('var'); + + // cannot set $_REQUEST directly because in phpbb_request implementation + // $_REQUEST = $_POST + $_GET + $_POST['var'] = array( + 0 => array( + 'b' => array( + true => array( + 5 => 'c', + 6 => 'd', + ), + ), + ), + 2 => array( + 3 => array( + false => 5, + ), + ), + ); + + $result = request_var($path, $default); + $this->assertEquals($expected, $result, 'Testing deep access to multidimensional input arrays: ' . $path); + } + + public static function deep_access() + { + return array( + // array(path, default, expected result) + array(array('var', 0, 'b', true, 5), '', 'c'), + array(array('var', 0, 'b', true, 6), '', 'd'), + array(array('var', 2, 3, false), 0, 5), + array(array('var', 0, 'b', true), array(0 => ''), array(5 => 'c', 6 => 'd')), + ); + } + public static function request_variables() { return array( @@ -172,6 +218,50 @@ class phpbb_request_request_var_test extends phpbb_test_case 'abc' => array() ) ), + array( + // input: + array( + 0 => array(0 => array(3, '4', 'ab'), 1 => array()), + 1 => array(array(3, 4)), + ), + // default: + array(0 => array(0 => array(0))), + false, + // expected: + array( + 0 => array(0 => array(3, 4, 0), 1 => array()), + 1 => array(array(3, 4)) + ) + ), + array( + // input: + array( + 'ü' => array(array('c' => 'd')), + 'ä' => array(4 => array('a' => 2, 'ö' => 3)), + ), + // default: + array('' => array(0 => array('' => 0))), + false, + // expected: + array( + '??' => array(4 => array('a' => 2, '??' => 3)), + ) + ), + array( + // input: + array( + 'ü' => array(array('c' => 'd')), + 'ä' => array(4 => array('a' => 2, 'ö' => 3)), + ), + // default: + array('' => array(0 => array('' => 0))), + true, + // expected: + array( + 'ü' => array(array('c' => 0)), + 'ä' => array(4 => array('a' => 2, 'ö' => 3)), + ) + ), ); } diff --git a/tests/request/type_cast_helper.php b/tests/request/type_cast_helper.php new file mode 100644 index 0000000000..3f27269acb --- /dev/null +++ b/tests/request/type_cast_helper.php @@ -0,0 +1,53 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2009 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php'; +require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper_interface.php'; +require_once __DIR__ . '/../../phpBB/includes/request/type_cast_helper.php'; + +class phpbb_type_cast_helper_test extends phpbb_test_case +{ + private $type_cast_helper; + + protected function setUp() + { + $this->type_cast_helper = new phpbb_request_type_cast_helper(); + } + + public function test_addslashes_recursively() + { + $data = array('some"string' => array('that"' => 'really"', 'needs"' => '"escaping')); + $expected = array('some\\"string' => array('that\\"' => 'really\\"', 'needs\\"' => '\\"escaping')); + + $this->type_cast_helper->addslashes_recursively($data); + + $this->assertEquals($expected, $data); + } + + public function test_simple_recursive_set_var() + { + $data = 'eviL<3'; + $expected = 'eviL<3'; + + $this->type_cast_helper->recursive_set_var($data, '', true); + + $this->assertEquals($expected, $data); + } + + public function test_nested_recursive_set_var() + { + $data = array('eviL<3'); + $expected = array('eviL<3'); + + $this->type_cast_helper->recursive_set_var($data, array(0 => ''), true); + + $this->assertEquals($expected, $data); + } +} diff --git a/tests/template/template.php b/tests/template/template.php index 861adf63e8..a58a0a4e0f 100644 --- a/tests/template/template.php +++ b/tests/template/template.php @@ -15,34 +15,13 @@ class phpbb_template_template_test extends phpbb_test_case private $template_path; // Keep the contents of the cache for debugging? - const PRESERVE_CACHE = true; + const PRESERVE_CACHE = false; private function display($handle) { - // allow the templates to throw notices - $error_level = error_reporting(); - error_reporting($error_level & ~E_NOTICE); - ob_start(); - - try - { - $this->assertTrue($this->template->display($handle, false)); - } - catch (Exception $exception) - { - // reset the error level even when an error occured - // PHPUnit turns trigger_error into exceptions as well - error_reporting($error_level); - ob_end_clean(); - throw $exception; - } - - $result = self::trim_template_result(ob_get_clean()); - - // reset error level - error_reporting($error_level); - return $result; + $this->assertTrue($this->template->display($handle, false)); + return self::trim_template_result(ob_get_clean()); } private static function trim_template_result($result) diff --git a/tests/template/templates/loop_expressions.html b/tests/template/templates/loop_expressions.html new file mode 100644 index 0000000000..6bff53f388 --- /dev/null +++ b/tests/template/templates/loop_expressions.html @@ -0,0 +1,11 @@ +<!-- BEGIN loop --> + +<!-- IF loop.S_ROW_NUM is even by 4 -->on<!-- ELSE -->off<!-- ENDIF --> + +<!-- END loop --> + +<!-- BEGIN loop --> + +<!-- IF loop.S_ROW_NUM is odd by 3 -->on<!-- ELSE -->off<!-- ENDIF --> + +<!-- END loop --> |