From 01fe91c5c4e897801f5c179cd4060e686762f105 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Mon, 10 Jan 2011 00:18:37 +0100 Subject: [ticket/9987] Rename test files to include a _test suffix PHPBB3-9987 --- phpunit.xml.dist | 2 +- tests/dbal/select.php | 320 ------------ tests/dbal/select_test.php | 320 ++++++++++++ tests/dbal/write.php | 171 ------- tests/dbal/write_test.php | 171 +++++++ tests/network/checkdnsrr.php | 62 --- tests/network/checkdnsrr_test.php | 62 +++ tests/random/gen_rand_string.php | 62 --- tests/random/gen_rand_string_test.php | 62 +++ tests/regex/censor.php | 40 -- tests/regex/censor_test.php | 40 ++ tests/regex/email.php | 118 ----- tests/regex/email_test.php | 118 +++++ tests/regex/ipv4.php | 71 --- tests/regex/ipv4_test.php | 71 +++ tests/regex/ipv6.php | 142 ------ tests/regex/ipv6_test.php | 142 ++++++ tests/regex/url.php | 33 -- tests/regex/url_test.php | 33 ++ tests/request/request_var.php | 180 ------- tests/request/request_var_test.php | 180 +++++++ tests/security/extract_current_page.php | 53 -- tests/security/extract_current_page_test.php | 53 ++ tests/security/redirect.php | 60 --- tests/security/redirect_test.php | 60 +++ tests/template/template.php | 689 -------------------------- tests/template/template_test.php | 689 ++++++++++++++++++++++++++ tests/text_processing/make_clickable.php | 104 ---- tests/text_processing/make_clickable_test.php | 104 ++++ 29 files changed, 2106 insertions(+), 2106 deletions(-) delete mode 100644 tests/dbal/select.php create mode 100644 tests/dbal/select_test.php delete mode 100644 tests/dbal/write.php create mode 100644 tests/dbal/write_test.php delete mode 100644 tests/network/checkdnsrr.php create mode 100644 tests/network/checkdnsrr_test.php delete mode 100644 tests/random/gen_rand_string.php create mode 100644 tests/random/gen_rand_string_test.php delete mode 100644 tests/regex/censor.php create mode 100644 tests/regex/censor_test.php delete mode 100644 tests/regex/email.php create mode 100644 tests/regex/email_test.php delete mode 100644 tests/regex/ipv4.php create mode 100644 tests/regex/ipv4_test.php delete mode 100644 tests/regex/ipv6.php create mode 100644 tests/regex/ipv6_test.php delete mode 100644 tests/regex/url.php create mode 100644 tests/regex/url_test.php delete mode 100644 tests/request/request_var.php create mode 100644 tests/request/request_var_test.php delete mode 100644 tests/security/extract_current_page.php create mode 100644 tests/security/extract_current_page_test.php delete mode 100644 tests/security/redirect.php create mode 100644 tests/security/redirect_test.php delete mode 100644 tests/template/template.php create mode 100644 tests/template/template_test.php delete mode 100644 tests/text_processing/make_clickable.php create mode 100644 tests/text_processing/make_clickable_test.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d1d8adbdd5..78c7fdd93a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,7 +13,7 @@ > - ./tests/ + ./tests/ diff --git a/tests/dbal/select.php b/tests/dbal/select.php deleted file mode 100644 index 987de5cbff..0000000000 --- a/tests/dbal/select.php +++ /dev/null @@ -1,320 +0,0 @@ -createXMLDataSet(dirname(__FILE__).'/fixtures/three_users.xml'); - } - - public static function return_on_error_select_data() - { - return array( - array('phpbb_users', "username_clean = 'bertie'", array(array('username_clean' => 'bertie'))), - array('phpbb_users', 'username_clean syntax_error', false), - ); - } - - /** - * @dataProvider return_on_error_select_data - */ - public function test_return_on_error_select($table, $where, $expected) - { - $db = $this->new_dbal(); - - $db->sql_return_on_error(true); - - $result = $db->sql_query('SELECT username_clean - FROM ' . $table . ' - WHERE ' . $where . ' - ORDER BY user_id ASC'); - - $db->sql_return_on_error(false); - - $this->assertEquals($expected, $db->sql_fetchrowset($result)); - } - - public static function fetchrow_data() - { - return array( - array('', array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'), - array('username_clean' => 'bertie'))), - array('user_id = 2', array(array('username_clean' => 'foobar'))), - array("username_clean = 'bertie'", array(array('username_clean' => 'bertie'))), - array("username_clean = 'phpBB'", array()), - ); - } - - /** - * @dataProvider fetchrow_data - */ - public function test_fetchrow($where, $expected) - { - $db = $this->new_dbal(); - - $result = $db->sql_query('SELECT username_clean - FROM phpbb_users - ' . (($where) ? ' WHERE ' . $where : '') . ' - ORDER BY user_id ASC'); - - $ary = array(); - while ($row = $db->sql_fetchrow($result)) - { - $ary[] = $row; - } - $db->sql_freeresult($result); - - $this->assertEquals($expected, $ary); - } - - /** - * @dataProvider fetchrow_data - */ - public function test_fetchrowset($where, $expected) - { - $db = $this->new_dbal(); - - $result = $db->sql_query('SELECT username_clean - FROM phpbb_users - ' . (($where) ? ' WHERE ' . $where : '') . ' - ORDER BY user_id ASC'); - - $this->assertEquals($expected, $db->sql_fetchrowset($result)); - - $db->sql_freeresult($result); - } - - public static function fetchfield_data() - { - return array( - array('', array('barfoo', 'foobar', 'bertie')), - array('user_id = 2', array('foobar')), - ); - } - - /** - * @dataProvider fetchfield_data - */ - public function test_fetchfield($where, $expected) - { - $db = $this->new_dbal(); - - $result = $db->sql_query('SELECT username_clean - FROM phpbb_users - ' . (($where) ? ' WHERE ' . $where : '') . ' - ORDER BY user_id ASC'); - - $ary = array(); - while ($row = $db->sql_fetchfield('username_clean')) - { - $ary[] = $row; - } - $db->sql_freeresult($result); - - $this->assertEquals($expected, $ary); - } - - public static function query_limit_data() - { - return array( - array(0, 0, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'), - array('username_clean' => 'bertie'))), - array(0, 1, array(array('username_clean' => 'foobar'), - array('username_clean' => 'bertie'))), - array(1, 0, array(array('username_clean' => 'barfoo'))), - array(1, 2, array(array('username_clean' => 'bertie'))), - array(2, 0, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'))), - array(2, 2, array(array('username_clean' => 'bertie'))), - array(2, 5, array()), - array(10, 1, array(array('username_clean' => 'foobar'), - array('username_clean' => 'bertie'))), - array(10, 5, array()), - ); - } - - /** - * @dataProvider query_limit_data - */ - public function test_query_limit($total, $offset, $expected) - { - $db = $this->new_dbal(); - - $result = $db->sql_query_limit('SELECT username_clean - FROM phpbb_users - ORDER BY user_id ASC', $total, $offset); - - $ary = array(); - while ($row = $db->sql_fetchrow($result)) - { - $ary[] = $row; - } - $db->sql_freeresult($result); - - $this->assertEquals($expected, $ary); - } - - public static function like_expression_data() - { - // * = any_char; # = one_char - return array( - array('barfoo', array(array('username_clean' => 'barfoo'))), - array('bar', array()), - array('bar*', array(array('username_clean' => 'barfoo'))), - array('*bar*', array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'))), - array('b*r', array()), - array('b*e', array(array('username_clean' => 'bertie'))), - array('#b*e', array()), - array('b####e', array(array('username_clean' => 'bertie'))), - ); - } - - /** - * @dataProvider like_expression_data - */ - public function test_like_expression($like_expression, $expected) - { - $db = $this->new_dbal(); - - $like_expression = str_replace('*', $db->any_char, $like_expression); - $like_expression = str_replace('#', $db->one_char, $like_expression); - $where = ($like_expression) ? 'username_clean ' . $db->sql_like_expression($like_expression) : ''; - - $result = $db->sql_query('SELECT username_clean - FROM phpbb_users - ' . (($where) ? ' WHERE ' . $where : '') . ' - ORDER BY user_id ASC'); - - $this->assertEquals($expected, $db->sql_fetchrowset($result)); - - $db->sql_freeresult($result); - } - - public static function in_set_data() - { - return array( - array('user_id', 3, false, false, array(array('username_clean' => 'bertie'))), - array('user_id', 3, false, true, array(array('username_clean' => 'bertie'))), - array('user_id', 3, true, false, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'))), - array('user_id', 3, true, true, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'))), - array('username_clean', 'bertie', false, false, array(array('username_clean' => 'bertie'))), - array('username_clean', 'bertie', false, true, array(array('username_clean' => 'bertie'))), - array('username_clean', 'bertie', true, false, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'))), - array('username_clean', 'bertie', true, true, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'))), - array('user_id', array(3), false, false, array(array('username_clean' => 'bertie'))), - array('user_id', array(3), false, true, array(array('username_clean' => 'bertie'))), - array('user_id', array(3), true, false, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'))), - array('user_id', array(3), true, true, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'))), - array('user_id', array(1, 3), false, false, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'bertie'))), - array('user_id', array(1, 3), false, true, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'bertie'))), - array('user_id', array(1, 3), true, false, array(array('username_clean' => 'foobar'))), - array('user_id', array(1, 3), true, true, array(array('username_clean' => 'foobar'))), - array('username_clean', '', false, false, array()), - array('username_clean', '', false, true, array()), - array('username_clean', '', true, false, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'), - array('username_clean' => 'bertie'))), - array('username_clean', '', true, true, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'), - array('username_clean' => 'bertie'))), - array('user_id', array(), false, true, array()), - array('user_id', array(), true, true, array(array('username_clean' => 'barfoo'), - array('username_clean' => 'foobar'), - array('username_clean' => 'bertie'))), - - // These here would throw errors and therefor $result should be false. - // Removing for now because SQLite accepts empty IN() syntax - /*array('user_id', array(), false, false, false, true), - array('user_id', array(), true, false, false, true),*/ - ); - } - - /** - * @dataProvider in_set_data - */ - public function test_in_set($field, $array, $negate, $allow_empty_set, $expected, $catch_error = false) - { - $db = $this->new_dbal(); - - if ($catch_error) - { - $db->sql_return_on_error(true); - } - - $result = $db->sql_query('SELECT username_clean - FROM phpbb_users - WHERE ' . $db->sql_in_set($field, $array, $negate, $allow_empty_set) . ' - ORDER BY user_id ASC'); - - if ($catch_error) - { - $db->sql_return_on_error(false); - } - - $this->assertEquals($expected, $db->sql_fetchrowset($result)); - - $db->sql_freeresult($result); - } - - public static function build_array_data() - { - return array( - array(array('username_clean' => 'barfoo'), array(array('username_clean' => 'barfoo'))), - array(array('username_clean' => 'barfoo', 'user_id' => 1), array(array('username_clean' => 'barfoo'))), - array(array('username_clean' => 'barfoo', 'user_id' => 2), array()), - - // These here would throw errors and therefor $result should be false. - array(array(), false, true), - array('no_array', false, true), - array(0, false, true), - ); - } - - /** - * @dataProvider build_array_data - */ - public function test_build_array($assoc_ary, $expected, $catch_error = false) - { - $db = $this->new_dbal(); - - if ($catch_error) - { - $db->sql_return_on_error(true); - } - - $sql = 'SELECT username_clean - FROM phpbb_users - WHERE ' . $db->sql_build_array('SELECT', $assoc_ary) . ' - ORDER BY user_id ASC'; - $result = $db->sql_query($sql); - - if ($catch_error) - { - $db->sql_return_on_error(false); - } - - $this->assertEquals($expected, $db->sql_fetchrowset($result)); - - $db->sql_freeresult($result); - } -} diff --git a/tests/dbal/select_test.php b/tests/dbal/select_test.php new file mode 100644 index 0000000000..987de5cbff --- /dev/null +++ b/tests/dbal/select_test.php @@ -0,0 +1,320 @@ +createXMLDataSet(dirname(__FILE__).'/fixtures/three_users.xml'); + } + + public static function return_on_error_select_data() + { + return array( + array('phpbb_users', "username_clean = 'bertie'", array(array('username_clean' => 'bertie'))), + array('phpbb_users', 'username_clean syntax_error', false), + ); + } + + /** + * @dataProvider return_on_error_select_data + */ + public function test_return_on_error_select($table, $where, $expected) + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $result = $db->sql_query('SELECT username_clean + FROM ' . $table . ' + WHERE ' . $where . ' + ORDER BY user_id ASC'); + + $db->sql_return_on_error(false); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + } + + public static function fetchrow_data() + { + return array( + array('', array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array('user_id = 2', array(array('username_clean' => 'foobar'))), + array("username_clean = 'bertie'", array(array('username_clean' => 'bertie'))), + array("username_clean = 'phpBB'", array()), + ); + } + + /** + * @dataProvider fetchrow_data + */ + public function test_fetchrow($where, $expected) + { + $db = $this->new_dbal(); + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + ' . (($where) ? ' WHERE ' . $where : '') . ' + ORDER BY user_id ASC'); + + $ary = array(); + while ($row = $db->sql_fetchrow($result)) + { + $ary[] = $row; + } + $db->sql_freeresult($result); + + $this->assertEquals($expected, $ary); + } + + /** + * @dataProvider fetchrow_data + */ + public function test_fetchrowset($where, $expected) + { + $db = $this->new_dbal(); + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + ' . (($where) ? ' WHERE ' . $where : '') . ' + ORDER BY user_id ASC'); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } + + public static function fetchfield_data() + { + return array( + array('', array('barfoo', 'foobar', 'bertie')), + array('user_id = 2', array('foobar')), + ); + } + + /** + * @dataProvider fetchfield_data + */ + public function test_fetchfield($where, $expected) + { + $db = $this->new_dbal(); + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + ' . (($where) ? ' WHERE ' . $where : '') . ' + ORDER BY user_id ASC'); + + $ary = array(); + while ($row = $db->sql_fetchfield('username_clean')) + { + $ary[] = $row; + } + $db->sql_freeresult($result); + + $this->assertEquals($expected, $ary); + } + + public static function query_limit_data() + { + return array( + array(0, 0, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array(0, 1, array(array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array(1, 0, array(array('username_clean' => 'barfoo'))), + array(1, 2, array(array('username_clean' => 'bertie'))), + array(2, 0, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array(2, 2, array(array('username_clean' => 'bertie'))), + array(2, 5, array()), + array(10, 1, array(array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array(10, 5, array()), + ); + } + + /** + * @dataProvider query_limit_data + */ + public function test_query_limit($total, $offset, $expected) + { + $db = $this->new_dbal(); + + $result = $db->sql_query_limit('SELECT username_clean + FROM phpbb_users + ORDER BY user_id ASC', $total, $offset); + + $ary = array(); + while ($row = $db->sql_fetchrow($result)) + { + $ary[] = $row; + } + $db->sql_freeresult($result); + + $this->assertEquals($expected, $ary); + } + + public static function like_expression_data() + { + // * = any_char; # = one_char + return array( + array('barfoo', array(array('username_clean' => 'barfoo'))), + array('bar', array()), + array('bar*', array(array('username_clean' => 'barfoo'))), + array('*bar*', array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('b*r', array()), + array('b*e', array(array('username_clean' => 'bertie'))), + array('#b*e', array()), + array('b####e', array(array('username_clean' => 'bertie'))), + ); + } + + /** + * @dataProvider like_expression_data + */ + public function test_like_expression($like_expression, $expected) + { + $db = $this->new_dbal(); + + $like_expression = str_replace('*', $db->any_char, $like_expression); + $like_expression = str_replace('#', $db->one_char, $like_expression); + $where = ($like_expression) ? 'username_clean ' . $db->sql_like_expression($like_expression) : ''; + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + ' . (($where) ? ' WHERE ' . $where : '') . ' + ORDER BY user_id ASC'); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } + + public static function in_set_data() + { + return array( + array('user_id', 3, false, false, array(array('username_clean' => 'bertie'))), + array('user_id', 3, false, true, array(array('username_clean' => 'bertie'))), + array('user_id', 3, true, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', 3, true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('username_clean', 'bertie', false, false, array(array('username_clean' => 'bertie'))), + array('username_clean', 'bertie', false, true, array(array('username_clean' => 'bertie'))), + array('username_clean', 'bertie', true, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('username_clean', 'bertie', true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', array(3), false, false, array(array('username_clean' => 'bertie'))), + array('user_id', array(3), false, true, array(array('username_clean' => 'bertie'))), + array('user_id', array(3), true, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', array(3), true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', array(1, 3), false, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'bertie'))), + array('user_id', array(1, 3), false, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'bertie'))), + array('user_id', array(1, 3), true, false, array(array('username_clean' => 'foobar'))), + array('user_id', array(1, 3), true, true, array(array('username_clean' => 'foobar'))), + array('username_clean', '', false, false, array()), + array('username_clean', '', false, true, array()), + array('username_clean', '', true, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array('username_clean', '', true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array('user_id', array(), false, true, array()), + array('user_id', array(), true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + + // These here would throw errors and therefor $result should be false. + // Removing for now because SQLite accepts empty IN() syntax + /*array('user_id', array(), false, false, false, true), + array('user_id', array(), true, false, false, true),*/ + ); + } + + /** + * @dataProvider in_set_data + */ + public function test_in_set($field, $array, $negate, $allow_empty_set, $expected, $catch_error = false) + { + $db = $this->new_dbal(); + + if ($catch_error) + { + $db->sql_return_on_error(true); + } + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + WHERE ' . $db->sql_in_set($field, $array, $negate, $allow_empty_set) . ' + ORDER BY user_id ASC'); + + if ($catch_error) + { + $db->sql_return_on_error(false); + } + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } + + public static function build_array_data() + { + return array( + array(array('username_clean' => 'barfoo'), array(array('username_clean' => 'barfoo'))), + array(array('username_clean' => 'barfoo', 'user_id' => 1), array(array('username_clean' => 'barfoo'))), + array(array('username_clean' => 'barfoo', 'user_id' => 2), array()), + + // These here would throw errors and therefor $result should be false. + array(array(), false, true), + array('no_array', false, true), + array(0, false, true), + ); + } + + /** + * @dataProvider build_array_data + */ + public function test_build_array($assoc_ary, $expected, $catch_error = false) + { + $db = $this->new_dbal(); + + if ($catch_error) + { + $db->sql_return_on_error(true); + } + + $sql = 'SELECT username_clean + FROM phpbb_users + WHERE ' . $db->sql_build_array('SELECT', $assoc_ary) . ' + ORDER BY user_id ASC'; + $result = $db->sql_query($sql); + + if ($catch_error) + { + $db->sql_return_on_error(false); + } + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } +} diff --git a/tests/dbal/write.php b/tests/dbal/write.php deleted file mode 100644 index a24b6efcc4..0000000000 --- a/tests/dbal/write.php +++ /dev/null @@ -1,171 +0,0 @@ -createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); - } - - public static function build_array_insert_data() - { - return array( - array(array( - 'config_name' => 'test_version', - 'config_value' => '0.0.0', - 'is_dynamic' => 1, - )), - array(array( - 'config_name' => 'second config', - 'config_value' => '10', - 'is_dynamic' => 0, - )), - ); - } - - /** - * @dataProvider build_array_insert_data - */ - public function test_build_array_insert($sql_ary) - { - $db = $this->new_dbal(); - - $sql = 'INSERT INTO phpbb_config ' . $db->sql_build_array('INSERT', $sql_ary); - $result = $db->sql_query($sql); - - $sql = "SELECT * - FROM phpbb_config - WHERE config_name = '" . $sql_ary['config_name'] . "'"; - $result = $db->sql_query_limit($sql, 1); - - $this->assertEquals($sql_ary, $db->sql_fetchrow($result)); - - $db->sql_freeresult($result); - } - - public function test_delete() - { - $db = $this->new_dbal(); - - $sql = "DELETE FROM phpbb_config - WHERE config_name = 'config1'"; - $result = $db->sql_query($sql); - - $sql = 'SELECT * - FROM phpbb_config'; - $result = $db->sql_query($sql); - $rows = $db->sql_fetchrowset($result); - - $this->assertEquals(1, sizeof($rows)); - $this->assertEquals('config2', $rows[0]['config_name']); - - $db->sql_freeresult($result); - } - - public function test_multiple_insert() - { - $db = $this->new_dbal(); - - // empty the table - $sql = 'DELETE FROM phpbb_config'; - $db->sql_query($sql); - - $batch_ary = array( - array( - 'config_name' => 'batch one', - 'config_value' => 'b1', - 'is_dynamic' => 0, - ), - array( - 'config_name' => 'batch two', - 'config_value' => 'b2', - 'is_dynamic' => 1, - ), - ); - - $result = $db->sql_multi_insert('phpbb_config', $batch_ary); - - $sql = 'SELECT * - FROM phpbb_config - ORDER BY config_name ASC'; - $result = $db->sql_query($sql); - - $this->assertEquals($batch_ary, $db->sql_fetchrowset($result)); - - $db->sql_freeresult($result); - } - - public static function update_data() - { - return array( - array( - array( - 'config_value' => '23', - 'is_dynamic' => 0, - ), - " WHERE config_name = 'config1'", - array( - array( - 'config_name' => 'config1', - 'config_value' => '23', - 'is_dynamic' => 0, - ), - array( - 'config_name' => 'config2', - 'config_value' => 'bar', - 'is_dynamic' => 1, - ), - ), - ), - array( - array( - 'config_value' => '0', - 'is_dynamic' => 1, - ), - '', - array( - array( - 'config_name' => 'config1', - 'config_value' => '0', - 'is_dynamic' => 1, - ), - array( - 'config_name' => 'config2', - 'config_value' => '0', - 'is_dynamic' => 1, - ), - ), - ), - ); - } - - /** - * @dataProvider update_data - */ - public function test_update($sql_ary, $where, $expected) - { - $db = $this->new_dbal(); - - $sql = 'UPDATE phpbb_config - SET ' . $db->sql_build_array('UPDATE', $sql_ary) . $where; - $result = $db->sql_query($sql); - - $sql = 'SELECT * - FROM phpbb_config - ORDER BY config_name ASC'; - $result = $db->sql_query($sql); - - $this->assertEquals($expected, $db->sql_fetchrowset($result)); - - $db->sql_freeresult($result); - } -} diff --git a/tests/dbal/write_test.php b/tests/dbal/write_test.php new file mode 100644 index 0000000000..a24b6efcc4 --- /dev/null +++ b/tests/dbal/write_test.php @@ -0,0 +1,171 @@ +createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); + } + + public static function build_array_insert_data() + { + return array( + array(array( + 'config_name' => 'test_version', + 'config_value' => '0.0.0', + 'is_dynamic' => 1, + )), + array(array( + 'config_name' => 'second config', + 'config_value' => '10', + 'is_dynamic' => 0, + )), + ); + } + + /** + * @dataProvider build_array_insert_data + */ + public function test_build_array_insert($sql_ary) + { + $db = $this->new_dbal(); + + $sql = 'INSERT INTO phpbb_config ' . $db->sql_build_array('INSERT', $sql_ary); + $result = $db->sql_query($sql); + + $sql = "SELECT * + FROM phpbb_config + WHERE config_name = '" . $sql_ary['config_name'] . "'"; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals($sql_ary, $db->sql_fetchrow($result)); + + $db->sql_freeresult($result); + } + + public function test_delete() + { + $db = $this->new_dbal(); + + $sql = "DELETE FROM phpbb_config + WHERE config_name = 'config1'"; + $result = $db->sql_query($sql); + + $sql = 'SELECT * + FROM phpbb_config'; + $result = $db->sql_query($sql); + $rows = $db->sql_fetchrowset($result); + + $this->assertEquals(1, sizeof($rows)); + $this->assertEquals('config2', $rows[0]['config_name']); + + $db->sql_freeresult($result); + } + + public function test_multiple_insert() + { + $db = $this->new_dbal(); + + // empty the table + $sql = 'DELETE FROM phpbb_config'; + $db->sql_query($sql); + + $batch_ary = array( + array( + 'config_name' => 'batch one', + 'config_value' => 'b1', + 'is_dynamic' => 0, + ), + array( + 'config_name' => 'batch two', + 'config_value' => 'b2', + 'is_dynamic' => 1, + ), + ); + + $result = $db->sql_multi_insert('phpbb_config', $batch_ary); + + $sql = 'SELECT * + FROM phpbb_config + ORDER BY config_name ASC'; + $result = $db->sql_query($sql); + + $this->assertEquals($batch_ary, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } + + public static function update_data() + { + return array( + array( + array( + 'config_value' => '23', + 'is_dynamic' => 0, + ), + " WHERE config_name = 'config1'", + array( + array( + 'config_name' => 'config1', + 'config_value' => '23', + 'is_dynamic' => 0, + ), + array( + 'config_name' => 'config2', + 'config_value' => 'bar', + 'is_dynamic' => 1, + ), + ), + ), + array( + array( + 'config_value' => '0', + 'is_dynamic' => 1, + ), + '', + array( + array( + 'config_name' => 'config1', + 'config_value' => '0', + 'is_dynamic' => 1, + ), + array( + 'config_name' => 'config2', + 'config_value' => '0', + 'is_dynamic' => 1, + ), + ), + ), + ); + } + + /** + * @dataProvider update_data + */ + public function test_update($sql_ary, $where, $expected) + { + $db = $this->new_dbal(); + + $sql = 'UPDATE phpbb_config + SET ' . $db->sql_build_array('UPDATE', $sql_ary) . $where; + $result = $db->sql_query($sql); + + $sql = 'SELECT * + FROM phpbb_config + ORDER BY config_name ASC'; + $result = $db->sql_query($sql); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } +} diff --git a/tests/network/checkdnsrr.php b/tests/network/checkdnsrr.php deleted file mode 100644 index 427132e508..0000000000 --- a/tests/network/checkdnsrr.php +++ /dev/null @@ -1,62 +0,0 @@ -assertEquals($expected, phpbb_checkdnsrr($host, $type)); - } -} diff --git a/tests/network/checkdnsrr_test.php b/tests/network/checkdnsrr_test.php new file mode 100644 index 0000000000..427132e508 --- /dev/null +++ b/tests/network/checkdnsrr_test.php @@ -0,0 +1,62 @@ +assertEquals($expected, phpbb_checkdnsrr($host, $type)); + } +} diff --git a/tests/random/gen_rand_string.php b/tests/random/gen_rand_string.php deleted file mode 100644 index fa519f134c..0000000000 --- a/tests/random/gen_rand_string.php +++ /dev/null @@ -1,62 +0,0 @@ -assertTrue($random_string_length >= self::MIN_STRING_LENGTH); - $this->assertTrue($random_string_length <= $num_chars); - $this->assertRegExp('#^[A-Z0-9]+$#', $random_string); - } - } - } - - public function test_gen_rand_string_friendly() - { - for ($tests = 0; $tests <= self::TEST_COUNT; ++$tests) - { - for ($num_chars = self::MIN_STRING_LENGTH; $num_chars <= self::MAX_STRING_LENGTH; ++$num_chars) - { - $random_string = gen_rand_string_friendly($num_chars); - $random_string_length = strlen($random_string); - - $this->assertTrue($random_string_length >= self::MIN_STRING_LENGTH); - $this->assertTrue($random_string_length <= $num_chars); - $this->assertRegExp('#^[A-NP-Z1-9]+$#', $random_string); - } - } - } -} diff --git a/tests/random/gen_rand_string_test.php b/tests/random/gen_rand_string_test.php new file mode 100644 index 0000000000..fa519f134c --- /dev/null +++ b/tests/random/gen_rand_string_test.php @@ -0,0 +1,62 @@ +assertTrue($random_string_length >= self::MIN_STRING_LENGTH); + $this->assertTrue($random_string_length <= $num_chars); + $this->assertRegExp('#^[A-Z0-9]+$#', $random_string); + } + } + } + + public function test_gen_rand_string_friendly() + { + for ($tests = 0; $tests <= self::TEST_COUNT; ++$tests) + { + for ($num_chars = self::MIN_STRING_LENGTH; $num_chars <= self::MAX_STRING_LENGTH; ++$num_chars) + { + $random_string = gen_rand_string_friendly($num_chars); + $random_string_length = strlen($random_string); + + $this->assertTrue($random_string_length >= self::MIN_STRING_LENGTH); + $this->assertTrue($random_string_length <= $num_chars); + $this->assertRegExp('#^[A-NP-Z1-9]+$#', $random_string); + } + } + } +} diff --git a/tests/regex/censor.php b/tests/regex/censor.php deleted file mode 100644 index ae2d86e07e..0000000000 --- a/tests/regex/censor.php +++ /dev/null @@ -1,40 +0,0 @@ -assertRegExp($regex, $subject); - } -} \ No newline at end of file diff --git a/tests/regex/censor_test.php b/tests/regex/censor_test.php new file mode 100644 index 0000000000..ae2d86e07e --- /dev/null +++ b/tests/regex/censor_test.php @@ -0,0 +1,40 @@ +assertRegExp($regex, $subject); + } +} \ No newline at end of file diff --git a/tests/regex/email.php b/tests/regex/email.php deleted file mode 100644 index 5d6e207cbb..0000000000 --- a/tests/regex/email.php +++ /dev/null @@ -1,118 +0,0 @@ -regex = '#^' . get_preg_expression('email') . '$#i'; - } - - public function positive_match_data() - { - return array( - array('nobody@phpbb.com'), - array('Nobody@sub.phpbb.com'), - array('alice.bob@foo.phpbb.com'), - array('alice-foo@bar.phpbb.com'), - array('alice_foo@bar.phpbb.com'), - array('alice+tag@foo.phpbb.com'), - array('alice&tag@foo.phpbb.com'), - - //array('"John Doe"@example.com'), - //array('Alice@[192.168.2.1]'), // IPv4 - //array('Bob@[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]'), // IPv6 - - // http://fightingforalostcause.net/misc/2006/compare-email-regex.php - array('l3tt3rsAndNumb3rs@domain.com'), - array('has-dash@domain.com'), - array('hasApostrophe.o\'leary@domain.org'), - array('uncommonTLD@domain.museum'), - array('uncommonTLD@domain.travel'), - array('uncommonTLD@domain.mobi'), - array('countryCodeTLD@domain.uk'), - array('countryCodeTLD@domain.rw'), - array('numbersInDomain@911.com'), - array('underscore_inLocal@domain.net'), - array('IPInsteadOfDomain@127.0.0.1'), - array('IPAndPort@127.0.0.1:25'), - array('subdomain@sub.domain.com'), - array('local@dash-inDomain.com'), - array('dot.inLocal@foo.com'), - array('a@singleLetterLocal.org'), - array('singleLetterDomain@x.org'), - array('&*=?^+{}\'~@validCharsInLocal.net'), - array('foor@bar.newTLD'), - ); - } - - public function negative_match_data() - { - return array( - array('foo.example.com'), // @ is missing - array('.foo.example.com'), // . as first character - array('Foo.@example.com'), // . is last in local part - array('foo..123@example.com'), // . doubled - array('a@b@c@example.com'), // @ doubled - - array('()[]\;:,<>@example.com'), // invalid characters - array('abc(def@example.com'), // invalid character ( - array('abc)def@example.com'), // invalid character ) - array('abc[def@example.com'), // invalid character [ - array('abc]def@example.com'), // invalid character ] - array('abc\def@example.com'), // invalid character \ - array('abc;def@example.com'), // invalid character ; - array('abc:def@example.com'), // invalid character : - array('abc,def@example.com'), // invalid character , - array('abcdef@example.com'), // invalid character > - - // http://fightingforalostcause.net/misc/2006/compare-email-regex.php - array('missingDomain@.com'), - array('@missingLocal.org'), - array('missingatSign.net'), - array('missingDot@com'), - array('two@@signs.com'), - array('colonButNoPort@127.0.0.1:'), - array(''), - array('someone-else@127.0.0.1.26'), - array('.localStartsWithDot@domain.com'), - array('localEndsWithDot.@domain.com'), - array('two..consecutiveDots@domain.com'), - array('domainStartsWithDash@-domain.com'), - array('domainEndsWithDash@domain-.com'), - array('numbersInTLD@domain.c0m'), - array('missingTLD@domain.'), - array('! "#$%(),/;<>[]`|@invalidCharsInLocal.org'), - array('invalidCharsInDomain@! "#$%(),/;<>_[]`|.org'), - array('local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org'), - ); - } - - /** - * @dataProvider positive_match_data - */ - public function test_positive_match($email) - { - $this->assertEquals(1, preg_match($this->regex, $email)); - } - - /** - * @dataProvider negative_match_data - */ - public function test_negative_match($email) - { - $this->assertEquals(0, preg_match($this->regex, $email)); - } -} - diff --git a/tests/regex/email_test.php b/tests/regex/email_test.php new file mode 100644 index 0000000000..5d6e207cbb --- /dev/null +++ b/tests/regex/email_test.php @@ -0,0 +1,118 @@ +regex = '#^' . get_preg_expression('email') . '$#i'; + } + + public function positive_match_data() + { + return array( + array('nobody@phpbb.com'), + array('Nobody@sub.phpbb.com'), + array('alice.bob@foo.phpbb.com'), + array('alice-foo@bar.phpbb.com'), + array('alice_foo@bar.phpbb.com'), + array('alice+tag@foo.phpbb.com'), + array('alice&tag@foo.phpbb.com'), + + //array('"John Doe"@example.com'), + //array('Alice@[192.168.2.1]'), // IPv4 + //array('Bob@[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]'), // IPv6 + + // http://fightingforalostcause.net/misc/2006/compare-email-regex.php + array('l3tt3rsAndNumb3rs@domain.com'), + array('has-dash@domain.com'), + array('hasApostrophe.o\'leary@domain.org'), + array('uncommonTLD@domain.museum'), + array('uncommonTLD@domain.travel'), + array('uncommonTLD@domain.mobi'), + array('countryCodeTLD@domain.uk'), + array('countryCodeTLD@domain.rw'), + array('numbersInDomain@911.com'), + array('underscore_inLocal@domain.net'), + array('IPInsteadOfDomain@127.0.0.1'), + array('IPAndPort@127.0.0.1:25'), + array('subdomain@sub.domain.com'), + array('local@dash-inDomain.com'), + array('dot.inLocal@foo.com'), + array('a@singleLetterLocal.org'), + array('singleLetterDomain@x.org'), + array('&*=?^+{}\'~@validCharsInLocal.net'), + array('foor@bar.newTLD'), + ); + } + + public function negative_match_data() + { + return array( + array('foo.example.com'), // @ is missing + array('.foo.example.com'), // . as first character + array('Foo.@example.com'), // . is last in local part + array('foo..123@example.com'), // . doubled + array('a@b@c@example.com'), // @ doubled + + array('()[]\;:,<>@example.com'), // invalid characters + array('abc(def@example.com'), // invalid character ( + array('abc)def@example.com'), // invalid character ) + array('abc[def@example.com'), // invalid character [ + array('abc]def@example.com'), // invalid character ] + array('abc\def@example.com'), // invalid character \ + array('abc;def@example.com'), // invalid character ; + array('abc:def@example.com'), // invalid character : + array('abc,def@example.com'), // invalid character , + array('abcdef@example.com'), // invalid character > + + // http://fightingforalostcause.net/misc/2006/compare-email-regex.php + array('missingDomain@.com'), + array('@missingLocal.org'), + array('missingatSign.net'), + array('missingDot@com'), + array('two@@signs.com'), + array('colonButNoPort@127.0.0.1:'), + array(''), + array('someone-else@127.0.0.1.26'), + array('.localStartsWithDot@domain.com'), + array('localEndsWithDot.@domain.com'), + array('two..consecutiveDots@domain.com'), + array('domainStartsWithDash@-domain.com'), + array('domainEndsWithDash@domain-.com'), + array('numbersInTLD@domain.c0m'), + array('missingTLD@domain.'), + array('! "#$%(),/;<>[]`|@invalidCharsInLocal.org'), + array('invalidCharsInDomain@! "#$%(),/;<>_[]`|.org'), + array('local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org'), + ); + } + + /** + * @dataProvider positive_match_data + */ + public function test_positive_match($email) + { + $this->assertEquals(1, preg_match($this->regex, $email)); + } + + /** + * @dataProvider negative_match_data + */ + public function test_negative_match($email) + { + $this->assertEquals(0, preg_match($this->regex, $email)); + } +} + diff --git a/tests/regex/ipv4.php b/tests/regex/ipv4.php deleted file mode 100644 index 735a2c4384..0000000000 --- a/tests/regex/ipv4.php +++ /dev/null @@ -1,71 +0,0 @@ -regex = get_preg_expression('ipv4'); - } - - public function positive_match_data() - { - return array( - array('0.0.0.0'), - array('127.0.0.1'), - array('192.168.0.1'), - array('255.255.255.255'), - ); - } - - public function negative_match_data() - { - return array( - // IPv6 addresses - array('2001:0db8:85a3:0000:0000:8a2e:0370:1337'), - array('2001:db8:85a3:c:d:8a2e:370:1337'), - array('2001:db8:85a3::8a2e:370:1337'), - array('2001:db8:0:1::192.168.0.2'), - array('0:0:0:0:0:0:0:1'), - array('0:0::0:0:1'), - array('::1'), - - // Out of scope - array('255.255.255.256'), - - // Other tests - array('a.b.c.d'), - array('11.22.33.'), - array('11.22.33'), - array('11.22'), - array('11'), - ); - } - - /** - * @dataProvider positive_match_data - */ - public function test_positive_match($address) - { - $this->assertEquals(1, preg_match($this->regex, $address)); - } - - /** - * @dataProvider negative_match_data - */ - public function test_negative_match($address) - { - $this->assertEquals(0, preg_match($this->regex, $address)); - } -} - diff --git a/tests/regex/ipv4_test.php b/tests/regex/ipv4_test.php new file mode 100644 index 0000000000..735a2c4384 --- /dev/null +++ b/tests/regex/ipv4_test.php @@ -0,0 +1,71 @@ +regex = get_preg_expression('ipv4'); + } + + public function positive_match_data() + { + return array( + array('0.0.0.0'), + array('127.0.0.1'), + array('192.168.0.1'), + array('255.255.255.255'), + ); + } + + public function negative_match_data() + { + return array( + // IPv6 addresses + array('2001:0db8:85a3:0000:0000:8a2e:0370:1337'), + array('2001:db8:85a3:c:d:8a2e:370:1337'), + array('2001:db8:85a3::8a2e:370:1337'), + array('2001:db8:0:1::192.168.0.2'), + array('0:0:0:0:0:0:0:1'), + array('0:0::0:0:1'), + array('::1'), + + // Out of scope + array('255.255.255.256'), + + // Other tests + array('a.b.c.d'), + array('11.22.33.'), + array('11.22.33'), + array('11.22'), + array('11'), + ); + } + + /** + * @dataProvider positive_match_data + */ + public function test_positive_match($address) + { + $this->assertEquals(1, preg_match($this->regex, $address)); + } + + /** + * @dataProvider negative_match_data + */ + public function test_negative_match($address) + { + $this->assertEquals(0, preg_match($this->regex, $address)); + } +} + diff --git a/tests/regex/ipv6.php b/tests/regex/ipv6.php deleted file mode 100644 index 187588f861..0000000000 --- a/tests/regex/ipv6.php +++ /dev/null @@ -1,142 +0,0 @@ -regex = get_preg_expression('ipv6'); - } - - public function positive_match_data() - { - return array( - // Full length IPv6 address - array('2001:0db8:85a3:0000:0000:8a2e:0370:1337'), - array('0000:0000:0000:0000:0000:0000:0000:0001'), - array('3FFE:0b00:0000:0000:0001:0000:0000:000a'), - array('3ffe:0b00:0000:0000:0001:0000:0000:000a'), - array('2002:0db8:0000:0000:0000:dead:1337:d00d'), - - // No leading zeroes in the group - array('2001:db8:85a3:0:0:8a2e:370:1337'), - array('2001:db8:85a3:c:d:8a2e:370:1337'), - - // Consecutive all-zero groups - array('2001:db8:85a3::8a2e:370:1337'), - array('1::2:3:4:5:6:7'), - array('1::2:3:4:5:6'), - array('1::2:3:4:5'), - array('1::2:3:4'), - array('1::2:3'), - array('1::2'), - - // Last 32bit in dotted quad notation - array('2001:db8:0:1::192.168.0.2'), - - // IPv4-compatible IPv6 address - array('::13.1.68.3'), - array('0:0:0:0:0:0:13.1.68.3'), - - // IPv4-mapped IPv6 address - array('::ffff:c000:280'), - array('::ffff:c000:0280'), - array('::ffff:192.0.2.128'), - array('0:0:0:0:0:ffff:c000:280'), - array('0:0:0:0:0:ffff:c000:0280'), - array('0:0:0:0:0:ffff:192.0.2.128'), - array('0000:0000:0000:0000:0000:ffff:c000:280'), - array('0000:0000:0000:0000:0000:ffff:c000:0280'), - array('0000:0000:0000:0000:0000:ffff:192.0.2.128'), - - // No trailing zeroes - array('fe80::'), - array('2002::'), - array('2001:db8::'), - array('2001:0db8:1234::'), - array('1:2:3:4:5:6::'), - array('1:2:3:4:5::'), - array('1:2:3:4::'), - array('1:2:3::'), - array('1:2::'), - - // No leading zeroes - array('::2:3:4:5:6:7:8'), - array('::2:3:4:5:6:7'), - array('::2:3:4:5:6'), - array('::2:3:4:5'), - array('::2:3:4'), - array('::2:3'), - array('::1'), - array('::8'), - array('::c'), - array('::abcd'), - - // All zeroes - array('::'), - array('0:0:0:0:0:0:0:0'), - array('0000:0000:0000:0000:0000:0000:0000:0000'), - - // More tests - array('2::10'), - array('0:0::0:0:1'), - array('0:0:0:0:0:0:0:1'), - array('::ffff:0:0'), - ); - } - - public function negative_match_data() - { - return array( - // Empty address - array(''), - - // IPv4 address - array('192.168.0.2'), - - // Out of scope - array('abcd:efgh:0000::0'), - array('::ffff:192.168.255.256'), - - // Double :: - array('2001::23de::2002'), - array('3ffe:b00::1::b'), - array('::1111:2222:3333:4444:5555:6666::'), - - // Too many blocks - array('2001:0db8:85a3:08d3:1319:8a2e:0370:1337:4430'), - - // More tests - array('02001:0000:1234:0000:0000:C1C0:ABCD:9876'), - array('2001:0000:1234: 0000:0000:C1C0:ABCD:9876'), - array('::ffff:192x168.255.255'), - ); - } - - /** - * @dataProvider positive_match_data - */ - public function test_positive_match($address) - { - $this->assertEquals(1, preg_match($this->regex, $address)); - } - - /** - * @dataProvider negative_match_data - */ - public function test_negative_match($address) - { - $this->assertEquals(0, preg_match($this->regex, $address)); - } -} - diff --git a/tests/regex/ipv6_test.php b/tests/regex/ipv6_test.php new file mode 100644 index 0000000000..187588f861 --- /dev/null +++ b/tests/regex/ipv6_test.php @@ -0,0 +1,142 @@ +regex = get_preg_expression('ipv6'); + } + + public function positive_match_data() + { + return array( + // Full length IPv6 address + array('2001:0db8:85a3:0000:0000:8a2e:0370:1337'), + array('0000:0000:0000:0000:0000:0000:0000:0001'), + array('3FFE:0b00:0000:0000:0001:0000:0000:000a'), + array('3ffe:0b00:0000:0000:0001:0000:0000:000a'), + array('2002:0db8:0000:0000:0000:dead:1337:d00d'), + + // No leading zeroes in the group + array('2001:db8:85a3:0:0:8a2e:370:1337'), + array('2001:db8:85a3:c:d:8a2e:370:1337'), + + // Consecutive all-zero groups + array('2001:db8:85a3::8a2e:370:1337'), + array('1::2:3:4:5:6:7'), + array('1::2:3:4:5:6'), + array('1::2:3:4:5'), + array('1::2:3:4'), + array('1::2:3'), + array('1::2'), + + // Last 32bit in dotted quad notation + array('2001:db8:0:1::192.168.0.2'), + + // IPv4-compatible IPv6 address + array('::13.1.68.3'), + array('0:0:0:0:0:0:13.1.68.3'), + + // IPv4-mapped IPv6 address + array('::ffff:c000:280'), + array('::ffff:c000:0280'), + array('::ffff:192.0.2.128'), + array('0:0:0:0:0:ffff:c000:280'), + array('0:0:0:0:0:ffff:c000:0280'), + array('0:0:0:0:0:ffff:192.0.2.128'), + array('0000:0000:0000:0000:0000:ffff:c000:280'), + array('0000:0000:0000:0000:0000:ffff:c000:0280'), + array('0000:0000:0000:0000:0000:ffff:192.0.2.128'), + + // No trailing zeroes + array('fe80::'), + array('2002::'), + array('2001:db8::'), + array('2001:0db8:1234::'), + array('1:2:3:4:5:6::'), + array('1:2:3:4:5::'), + array('1:2:3:4::'), + array('1:2:3::'), + array('1:2::'), + + // No leading zeroes + array('::2:3:4:5:6:7:8'), + array('::2:3:4:5:6:7'), + array('::2:3:4:5:6'), + array('::2:3:4:5'), + array('::2:3:4'), + array('::2:3'), + array('::1'), + array('::8'), + array('::c'), + array('::abcd'), + + // All zeroes + array('::'), + array('0:0:0:0:0:0:0:0'), + array('0000:0000:0000:0000:0000:0000:0000:0000'), + + // More tests + array('2::10'), + array('0:0::0:0:1'), + array('0:0:0:0:0:0:0:1'), + array('::ffff:0:0'), + ); + } + + public function negative_match_data() + { + return array( + // Empty address + array(''), + + // IPv4 address + array('192.168.0.2'), + + // Out of scope + array('abcd:efgh:0000::0'), + array('::ffff:192.168.255.256'), + + // Double :: + array('2001::23de::2002'), + array('3ffe:b00::1::b'), + array('::1111:2222:3333:4444:5555:6666::'), + + // Too many blocks + array('2001:0db8:85a3:08d3:1319:8a2e:0370:1337:4430'), + + // More tests + array('02001:0000:1234:0000:0000:C1C0:ABCD:9876'), + array('2001:0000:1234: 0000:0000:C1C0:ABCD:9876'), + array('::ffff:192x168.255.255'), + ); + } + + /** + * @dataProvider positive_match_data + */ + public function test_positive_match($address) + { + $this->assertEquals(1, preg_match($this->regex, $address)); + } + + /** + * @dataProvider negative_match_data + */ + public function test_negative_match($address) + { + $this->assertEquals(0, preg_match($this->regex, $address)); + } +} + diff --git a/tests/regex/url.php b/tests/regex/url.php deleted file mode 100644 index 246cbf549c..0000000000 --- a/tests/regex/url.php +++ /dev/null @@ -1,33 +0,0 @@ -assertEquals($expected, preg_match('#^' . get_preg_expression('url') . '$#i', $url)); - } -} diff --git a/tests/regex/url_test.php b/tests/regex/url_test.php new file mode 100644 index 0000000000..246cbf549c --- /dev/null +++ b/tests/regex/url_test.php @@ -0,0 +1,33 @@ +assertEquals($expected, preg_match('#^' . get_preg_expression('url') . '$#i', $url)); + } +} diff --git a/tests/request/request_var.php b/tests/request/request_var.php deleted file mode 100644 index 0901b43920..0000000000 --- a/tests/request/request_var.php +++ /dev/null @@ -1,180 +0,0 @@ -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 \"entities\" like &", '', true, "Some <html> "entities" like &"), - - // 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() - ) - ), - ); - } - -} - diff --git a/tests/request/request_var_test.php b/tests/request/request_var_test.php new file mode 100644 index 0000000000..0901b43920 --- /dev/null +++ b/tests/request/request_var_test.php @@ -0,0 +1,180 @@ +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 \"entities\" like &", '', true, "Some <html> "entities" like &"), + + // 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() + ) + ), + ); + } + +} + diff --git a/tests/security/extract_current_page.php b/tests/security/extract_current_page.php deleted file mode 100644 index ff0ab4d1bb..0000000000 --- a/tests/security/extract_current_page.php +++ /dev/null @@ -1,53 +0,0 @@ -', 'mark=forums&x=%22%3E%3Cscript%3Ealert(/XSS/);%3C/script%3E'), - array('http://localhost/phpBB/index.php', 'mark=forums&x=%22%3E%3Cscript%3Ealert(/XSS/);%3C/script%3E', 'mark=forums&x=%22%3E%3Cscript%3Ealert(/XSS/);%3C/script%3E'), - ); - } - - /** - * @dataProvider security_variables - */ - public function test_query_string_php_self($url, $query_string, $expected) - { - $_SERVER['PHP_SELF'] = $url; - $_SERVER['QUERY_STRING'] = $query_string; - - $result = session::extract_current_page('./'); - - $label = 'Running extract_current_page on ' . $query_string . ' with PHP_SELF filled.'; - $this->assertEquals($expected, $result['query_string'], $label); - } - - /** - * @dataProvider security_variables - */ - public function test_query_string_request_uri($url, $query_string, $expected) - { - $_SERVER['REQUEST_URI'] = $url . '?' . $query_string; - $_SERVER['QUERY_STRING'] = $query_string; - - $result = session::extract_current_page('./'); - - $label = 'Running extract_current_page on ' . $query_string . ' with REQUEST_URI filled.'; - $this->assertEquals($expected, $result['query_string'], $label); - } -} - diff --git a/tests/security/extract_current_page_test.php b/tests/security/extract_current_page_test.php new file mode 100644 index 0000000000..ff0ab4d1bb --- /dev/null +++ b/tests/security/extract_current_page_test.php @@ -0,0 +1,53 @@ +', 'mark=forums&x=%22%3E%3Cscript%3Ealert(/XSS/);%3C/script%3E'), + array('http://localhost/phpBB/index.php', 'mark=forums&x=%22%3E%3Cscript%3Ealert(/XSS/);%3C/script%3E', 'mark=forums&x=%22%3E%3Cscript%3Ealert(/XSS/);%3C/script%3E'), + ); + } + + /** + * @dataProvider security_variables + */ + public function test_query_string_php_self($url, $query_string, $expected) + { + $_SERVER['PHP_SELF'] = $url; + $_SERVER['QUERY_STRING'] = $query_string; + + $result = session::extract_current_page('./'); + + $label = 'Running extract_current_page on ' . $query_string . ' with PHP_SELF filled.'; + $this->assertEquals($expected, $result['query_string'], $label); + } + + /** + * @dataProvider security_variables + */ + public function test_query_string_request_uri($url, $query_string, $expected) + { + $_SERVER['REQUEST_URI'] = $url . '?' . $query_string; + $_SERVER['QUERY_STRING'] = $query_string; + + $result = session::extract_current_page('./'); + + $label = 'Running extract_current_page on ' . $query_string . ' with REQUEST_URI filled.'; + $this->assertEquals($expected, $result['query_string'], $label); + } +} + diff --git a/tests/security/redirect.php b/tests/security/redirect.php deleted file mode 100644 index c53414e7df..0000000000 --- a/tests/security/redirect.php +++ /dev/null @@ -1,60 +0,0 @@ - redirect(), expected triggered error (else false), expected returned result url (else false)) - return array( - array('data://x', false, 'http://localhost/phpBB'), - array('bad://localhost/phpBB/index.php', 'Tried to redirect to potentially insecure url.', false), - array('http://www.otherdomain.com/somescript.php', false, 'http://localhost/phpBB'), - array("http://localhost/phpBB/memberlist.php\n\rConnection: close", 'Tried to redirect to potentially insecure url.', false), - array('javascript:test', false, 'http://localhost/phpBB/../javascript:test'), - array('http://localhost/phpBB/index.php;url=', 'Tried to redirect to potentially insecure url.', false), - ); - } - - protected function setUp() - { - parent::setUp(); - - $GLOBALS['config'] = array( - 'force_server_vars' => '0', - ); - } - - /** - * @dataProvider provider - */ - public function test_redirect($test, $expected_error, $expected_result) - { - global $user; - - if ($expected_error !== false) - { - $this->setExpectedTriggerError(E_USER_ERROR, $expected_error); - } - - $result = redirect($test, true); - - // only verify result if we did not expect an error - if ($expected_error === false) - { - $this->assertEquals($expected_result, $result); - } - } -} - diff --git a/tests/security/redirect_test.php b/tests/security/redirect_test.php new file mode 100644 index 0000000000..c53414e7df --- /dev/null +++ b/tests/security/redirect_test.php @@ -0,0 +1,60 @@ + redirect(), expected triggered error (else false), expected returned result url (else false)) + return array( + array('data://x', false, 'http://localhost/phpBB'), + array('bad://localhost/phpBB/index.php', 'Tried to redirect to potentially insecure url.', false), + array('http://www.otherdomain.com/somescript.php', false, 'http://localhost/phpBB'), + array("http://localhost/phpBB/memberlist.php\n\rConnection: close", 'Tried to redirect to potentially insecure url.', false), + array('javascript:test', false, 'http://localhost/phpBB/../javascript:test'), + array('http://localhost/phpBB/index.php;url=', 'Tried to redirect to potentially insecure url.', false), + ); + } + + protected function setUp() + { + parent::setUp(); + + $GLOBALS['config'] = array( + 'force_server_vars' => '0', + ); + } + + /** + * @dataProvider provider + */ + public function test_redirect($test, $expected_error, $expected_result) + { + global $user; + + if ($expected_error !== false) + { + $this->setExpectedTriggerError(E_USER_ERROR, $expected_error); + } + + $result = redirect($test, true); + + // only verify result if we did not expect an error + if ($expected_error === false) + { + $this->assertEquals($expected_result, $result); + } + } +} + diff --git a/tests/template/template.php b/tests/template/template.php deleted file mode 100644 index 35df17e4c6..0000000000 --- a/tests/template/template.php +++ /dev/null @@ -1,689 +0,0 @@ -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; - } - - private static function trim_template_result($result) - { - return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result))))); - } - - private function setup_engine() - { - $this->template_path = dirname(__FILE__) . '/templates'; - $this->template = new template(); - $this->template->set_custom_template($this->template_path, 'tests'); - } - - protected function setUp() - { - // Test the engine can be used - $this->setup_engine(); - - if (!is_writable(dirname($this->template->cachepath))) - { - $this->markTestSkipped("Template cache directory is not writable."); - } - - foreach (glob($this->template->cachepath . '*') as $file) - { - unlink($file); - } - - $GLOBALS['config'] = array( - 'load_tplcompile' => true, - 'tpl_allow_php' => false, - ); - } - - protected function tearDown() - { - if (is_object($this->template)) - { - foreach (glob($this->template->cachepath . '*') as $file) - { - unlink($file); - } - } - } - - /** - * @todo put test data into templates/xyz.test - */ - public static function template_data() - { - return array( - /* - array( - '', // File - array(), // vars - array(), // block vars - array(), // destroy - '', // Expected result - ), - */ - array( - 'basic.html', - array(), - array(), - array(), - "pass\npass\n", - ), - array( - 'variable.html', - array('VARIABLE' => 'value'), - array(), - array(), - 'value', - ), - array( - 'if.html', - array(), - array(), - array(), - '0', - ), - array( - 'if.html', - array('S_VALUE' => true), - array(), - array(), - "1\n0", - ), - array( - 'if.html', - array('S_VALUE' => true, 'S_OTHER_VALUE' => true), - array(), - array(), - '1', - ), - array( - 'if.html', - array('S_VALUE' => false, 'S_OTHER_VALUE' => true), - array(), - array(), - '2', - ), - array( - 'loop.html', - array(), - array(), - array(), - "noloop\nnoloop", - ), - array( - 'loop.html', - array(), - array('loop' => array(array())), - array(), - "loop\nloop", - ), - array( - 'loop.html', - array(), - array('loop' => array(array(), array()), 'loop.block' => array(array())), - array(), - "loop\nloop\nloop\nloop", - ), - array( - 'loop.html', - array(), - array('loop' => array(array(), array()), 'loop.block' => array(array()), 'block' => array(array(), array())), - array(), - "loop\nloop\nloop\nloop\nloop#0-block#0\nloop#0-block#1\nloop#1-block#0\nloop#1-block#1", - ), - array( - 'loop_vars.html', - array(), - array('loop' => array(array('VARIABLE' => 'x'))), - array(), - "first\n0\nx\nset\nlast", - ),/* no nested top level loops - array( - 'loop_vars.html', - array(), - array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))), - array(), - "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast", - ), - array( - 'loop_vars.html', - array(), - array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), - array(), - "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast\n0\n\n1\nlast inner\ninner loop", - ),*/ - array( - 'loop_advanced.html', - array(), - array('loop' => array(array(), array(), array(), array(), array(), array(), array())), - array(), - "101234561\nx\n101234561\nx\n101234561\nx\n1234561\nx\n1\nx\n101\nx\n234\nx\n10\nx\n561\nx\n561", - ), - array( - 'define.html', - array(), - array('loop' => array(array(), array(), array(), array(), array(), array(), array()), 'test' => array(array()), 'test.deep' => array(array()), 'test.deep.defines' => array(array())), - array(), - "xyz\nabc", - ), - array( - 'expressions.html', - array(), - array(), - array(), - trim(str_repeat("pass", 39)), - ), - array( - 'php.html', - array(), - array(), - array(), - '', - ), - array( - 'include.html', - array('VARIABLE' => 'value'), - array(), - array(), - 'value', - ), - array( - 'loop_vars.html', - array(), - array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), - array('loop'), - '', - ),/* no top level nested loops - array( - 'loop_vars.html', - array(), - array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), - array('loop.inner'), - "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast", - ),*/ - array( - 'lang.html', - array(), - array(), - array(), - "{ VARIABLE }\n{ VARIABLE }", - ), - array( - 'lang.html', - array('L_VARIABLE' => "Value'"), - array(), - array(), - "Value'\nValue\'", - ), - array( - 'lang.html', - array('LA_VARIABLE' => "Value'"), - array(), - array(), - "{ VARIABLE }\nValue'", - ), - ); - } - - public function test_missing_file() - { - $filename = 'file_not_found.html'; - - $this->template->set_filenames(array('test' => $filename)); - $this->assertFileNotExists($this->template_path . '/' . $filename, 'Testing missing file, file cannot exist'); - - $expecting = sprintf('template->_tpl_load_file(): File %s does not exist or is empty', realpath($this->template_path . '/../') . '/templates/' . $filename); - $this->setExpectedTriggerError(E_USER_ERROR, $expecting); - - $this->display('test'); - } - - public function test_empty_file() - { - $expecting = 'template->set_filenames: Empty filename specified for test'; - - $this->setExpectedTriggerError(E_USER_ERROR, $expecting); - $this->template->set_filenames(array('test' => '')); - } - - public function test_invalid_handle() - { - $expecting = 'template->_tpl_load(): No file specified for handle test'; - $this->setExpectedTriggerError(E_USER_ERROR, $expecting); - - $this->display('test'); - } - - private function run_template($file, array $vars, array $block_vars, array $destroy, $expected, $cache_file) - { - $this->template->set_filenames(array('test' => $file)); - $this->template->assign_vars($vars); - - foreach ($block_vars as $block => $loops) - { - foreach ($loops as $_vars) - { - $this->template->assign_block_vars($block, $_vars); - } - } - - foreach ($destroy as $block) - { - $this->template->destroy_block_vars($block); - } - - try - { - $this->assertEquals($expected, $this->display('test'), "Testing $file"); - $this->assertFileExists($cache_file); - } - catch (ErrorException $e) - { - if (file_exists($cache_file)) - { - copy($cache_file, str_replace('ctpl_', 'tests_ctpl_', $cache_file)); - } - - throw $e; - } - - // For debugging - if (self::PRESERVE_CACHE) - { - copy($cache_file, str_replace('ctpl_', 'tests_ctpl_', $cache_file)); - } - } - - /** - * @dataProvider template_data - */ - public function test_template($file, array $vars, array $block_vars, array $destroy, $expected) - { - global $phpEx; - $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.' . $phpEx; - - $this->assertFileNotExists($cache_file); - - $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); - - // Reset the engine state - $this->setup_engine(); - - $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); - } - - /** - * @dataProvider template_data - */ - public function test_assign_display($file, array $vars, array $block_vars, array $destroy, $expected) - { - $this->template->set_filenames(array( - 'test' => $file, - 'container' => 'variable.html', - )); - $this->template->assign_vars($vars); - - foreach ($block_vars as $block => $loops) - { - foreach ($loops as $_vars) - { - $this->template->assign_block_vars($block, $_vars); - } - } - - foreach ($destroy as $block) - { - $this->template->destroy_block_vars($block); - } - - $error_level = error_reporting(); - error_reporting($error_level & ~E_NOTICE); - - $this->assertEquals($expected, self::trim_template_result($this->template->assign_display('test')), "Testing assign_display($file)"); - - $this->template->assign_display('test', 'VARIABLE', false); - - error_reporting($error_level); - - $this->assertEquals($expected, $this->display('container'), "Testing assign_display($file)"); - } - - public function test_php() - { - global $phpEx; - - $GLOBALS['config']['tpl_allow_php'] = true; - - $cache_file = $this->template->cachepath . 'php.html.' . $phpEx; - - $this->assertFileNotExists($cache_file); - - $this->run_template('php.html', array(), array(), array(), 'test', $cache_file); - - $GLOBALS['config']['tpl_allow_php'] = false; - } - - public function test_includephp() - { - $this->markTestIncomplete('Include PHP test file paths are broken'); - - $GLOBALS['config']['tpl_allow_php'] = true; - - $cache_file = $this->template->cachepath . 'includephp.html.' . PHP_EXT; - - $cwd = getcwd(); - chdir(dirname(__FILE__) . '/templates'); - - $this->run_template('includephp.html', array(), array(), array(), 'testing included php', $cache_file); - - $this->template->set_filenames(array('test' => 'includephp.html')); - $this->assertEquals('testing included php', $this->display('test'), "Testing $file"); - - chdir($cwd); - - $GLOBALS['config']['tpl_allow_php'] = false; - } - - public static function alter_block_array_data() - { - return array( - array( - 'outer', - array('VARIABLE' => 'before'), - false, - 'insert', - << 'after'), - true, - 'insert', - << 'pos #1'), - 1, - 'insert', - << 'pos #1'), - 0, - 'change', - << 'before'), - false, - 'insert', - << 'after'), - true, - 'insert', - << 'pos #1'), - 1, - 'insert', - << 'before'), - false, - 'insert', - << 'before'), - false, - 'insert', - << 'before'), - false, - 'insert', - <<markTestIncomplete('Alter Block Test is broken'); - - $this->template->set_filenames(array('test' => 'loop_nested.html')); - - // @todo Change this - $this->template->assign_block_vars('outer', array()); - $this->template->assign_block_vars('outer.middle', array()); - $this->template->assign_block_vars('outer.middle', array()); - $this->template->assign_block_vars('outer', array()); - $this->template->assign_block_vars('outer.middle', array()); - $this->template->assign_block_vars('outer.middle', array()); - $this->template->assign_block_vars('outer.middle', array()); - $this->template->assign_block_vars('outer', array()); - $this->template->assign_block_vars('outer.middle', array()); - $this->template->assign_block_vars('outer.middle', array()); - - $this->assertEquals("outer - 0/3\nmiddle - 0/2\nmiddle - 1/2\nouter - 1/3\nmiddle - 0/3\nmiddle - 1/3\nmiddle - 2/3\nouter - 2/3\nmiddle - 0/2\nmiddle - 1/2", $this->display('test'), 'Ensuring template is built correctly before modification'); - - $this->template->alter_block_array($alter_block, $vararray, $key, $mode); - $this->assertEquals($expect, $this->display('test'), $description); - } -} - diff --git a/tests/template/template_test.php b/tests/template/template_test.php new file mode 100644 index 0000000000..35df17e4c6 --- /dev/null +++ b/tests/template/template_test.php @@ -0,0 +1,689 @@ +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; + } + + private static function trim_template_result($result) + { + return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result))))); + } + + private function setup_engine() + { + $this->template_path = dirname(__FILE__) . '/templates'; + $this->template = new template(); + $this->template->set_custom_template($this->template_path, 'tests'); + } + + protected function setUp() + { + // Test the engine can be used + $this->setup_engine(); + + if (!is_writable(dirname($this->template->cachepath))) + { + $this->markTestSkipped("Template cache directory is not writable."); + } + + foreach (glob($this->template->cachepath . '*') as $file) + { + unlink($file); + } + + $GLOBALS['config'] = array( + 'load_tplcompile' => true, + 'tpl_allow_php' => false, + ); + } + + protected function tearDown() + { + if (is_object($this->template)) + { + foreach (glob($this->template->cachepath . '*') as $file) + { + unlink($file); + } + } + } + + /** + * @todo put test data into templates/xyz.test + */ + public static function template_data() + { + return array( + /* + array( + '', // File + array(), // vars + array(), // block vars + array(), // destroy + '', // Expected result + ), + */ + array( + 'basic.html', + array(), + array(), + array(), + "pass\npass\n", + ), + array( + 'variable.html', + array('VARIABLE' => 'value'), + array(), + array(), + 'value', + ), + array( + 'if.html', + array(), + array(), + array(), + '0', + ), + array( + 'if.html', + array('S_VALUE' => true), + array(), + array(), + "1\n0", + ), + array( + 'if.html', + array('S_VALUE' => true, 'S_OTHER_VALUE' => true), + array(), + array(), + '1', + ), + array( + 'if.html', + array('S_VALUE' => false, 'S_OTHER_VALUE' => true), + array(), + array(), + '2', + ), + array( + 'loop.html', + array(), + array(), + array(), + "noloop\nnoloop", + ), + array( + 'loop.html', + array(), + array('loop' => array(array())), + array(), + "loop\nloop", + ), + array( + 'loop.html', + array(), + array('loop' => array(array(), array()), 'loop.block' => array(array())), + array(), + "loop\nloop\nloop\nloop", + ), + array( + 'loop.html', + array(), + array('loop' => array(array(), array()), 'loop.block' => array(array()), 'block' => array(array(), array())), + array(), + "loop\nloop\nloop\nloop\nloop#0-block#0\nloop#0-block#1\nloop#1-block#0\nloop#1-block#1", + ), + array( + 'loop_vars.html', + array(), + array('loop' => array(array('VARIABLE' => 'x'))), + array(), + "first\n0\nx\nset\nlast", + ),/* no nested top level loops + array( + 'loop_vars.html', + array(), + array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))), + array(), + "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast", + ), + array( + 'loop_vars.html', + array(), + array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), + array(), + "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast\n0\n\n1\nlast inner\ninner loop", + ),*/ + array( + 'loop_advanced.html', + array(), + array('loop' => array(array(), array(), array(), array(), array(), array(), array())), + array(), + "101234561\nx\n101234561\nx\n101234561\nx\n1234561\nx\n1\nx\n101\nx\n234\nx\n10\nx\n561\nx\n561", + ), + array( + 'define.html', + array(), + array('loop' => array(array(), array(), array(), array(), array(), array(), array()), 'test' => array(array()), 'test.deep' => array(array()), 'test.deep.defines' => array(array())), + array(), + "xyz\nabc", + ), + array( + 'expressions.html', + array(), + array(), + array(), + trim(str_repeat("pass", 39)), + ), + array( + 'php.html', + array(), + array(), + array(), + '', + ), + array( + 'include.html', + array('VARIABLE' => 'value'), + array(), + array(), + 'value', + ), + array( + 'loop_vars.html', + array(), + array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), + array('loop'), + '', + ),/* no top level nested loops + array( + 'loop_vars.html', + array(), + array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), + array('loop.inner'), + "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast", + ),*/ + array( + 'lang.html', + array(), + array(), + array(), + "{ VARIABLE }\n{ VARIABLE }", + ), + array( + 'lang.html', + array('L_VARIABLE' => "Value'"), + array(), + array(), + "Value'\nValue\'", + ), + array( + 'lang.html', + array('LA_VARIABLE' => "Value'"), + array(), + array(), + "{ VARIABLE }\nValue'", + ), + ); + } + + public function test_missing_file() + { + $filename = 'file_not_found.html'; + + $this->template->set_filenames(array('test' => $filename)); + $this->assertFileNotExists($this->template_path . '/' . $filename, 'Testing missing file, file cannot exist'); + + $expecting = sprintf('template->_tpl_load_file(): File %s does not exist or is empty', realpath($this->template_path . '/../') . '/templates/' . $filename); + $this->setExpectedTriggerError(E_USER_ERROR, $expecting); + + $this->display('test'); + } + + public function test_empty_file() + { + $expecting = 'template->set_filenames: Empty filename specified for test'; + + $this->setExpectedTriggerError(E_USER_ERROR, $expecting); + $this->template->set_filenames(array('test' => '')); + } + + public function test_invalid_handle() + { + $expecting = 'template->_tpl_load(): No file specified for handle test'; + $this->setExpectedTriggerError(E_USER_ERROR, $expecting); + + $this->display('test'); + } + + private function run_template($file, array $vars, array $block_vars, array $destroy, $expected, $cache_file) + { + $this->template->set_filenames(array('test' => $file)); + $this->template->assign_vars($vars); + + foreach ($block_vars as $block => $loops) + { + foreach ($loops as $_vars) + { + $this->template->assign_block_vars($block, $_vars); + } + } + + foreach ($destroy as $block) + { + $this->template->destroy_block_vars($block); + } + + try + { + $this->assertEquals($expected, $this->display('test'), "Testing $file"); + $this->assertFileExists($cache_file); + } + catch (ErrorException $e) + { + if (file_exists($cache_file)) + { + copy($cache_file, str_replace('ctpl_', 'tests_ctpl_', $cache_file)); + } + + throw $e; + } + + // For debugging + if (self::PRESERVE_CACHE) + { + copy($cache_file, str_replace('ctpl_', 'tests_ctpl_', $cache_file)); + } + } + + /** + * @dataProvider template_data + */ + public function test_template($file, array $vars, array $block_vars, array $destroy, $expected) + { + global $phpEx; + $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.' . $phpEx; + + $this->assertFileNotExists($cache_file); + + $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); + + // Reset the engine state + $this->setup_engine(); + + $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); + } + + /** + * @dataProvider template_data + */ + public function test_assign_display($file, array $vars, array $block_vars, array $destroy, $expected) + { + $this->template->set_filenames(array( + 'test' => $file, + 'container' => 'variable.html', + )); + $this->template->assign_vars($vars); + + foreach ($block_vars as $block => $loops) + { + foreach ($loops as $_vars) + { + $this->template->assign_block_vars($block, $_vars); + } + } + + foreach ($destroy as $block) + { + $this->template->destroy_block_vars($block); + } + + $error_level = error_reporting(); + error_reporting($error_level & ~E_NOTICE); + + $this->assertEquals($expected, self::trim_template_result($this->template->assign_display('test')), "Testing assign_display($file)"); + + $this->template->assign_display('test', 'VARIABLE', false); + + error_reporting($error_level); + + $this->assertEquals($expected, $this->display('container'), "Testing assign_display($file)"); + } + + public function test_php() + { + global $phpEx; + + $GLOBALS['config']['tpl_allow_php'] = true; + + $cache_file = $this->template->cachepath . 'php.html.' . $phpEx; + + $this->assertFileNotExists($cache_file); + + $this->run_template('php.html', array(), array(), array(), 'test', $cache_file); + + $GLOBALS['config']['tpl_allow_php'] = false; + } + + public function test_includephp() + { + $this->markTestIncomplete('Include PHP test file paths are broken'); + + $GLOBALS['config']['tpl_allow_php'] = true; + + $cache_file = $this->template->cachepath . 'includephp.html.' . PHP_EXT; + + $cwd = getcwd(); + chdir(dirname(__FILE__) . '/templates'); + + $this->run_template('includephp.html', array(), array(), array(), 'testing included php', $cache_file); + + $this->template->set_filenames(array('test' => 'includephp.html')); + $this->assertEquals('testing included php', $this->display('test'), "Testing $file"); + + chdir($cwd); + + $GLOBALS['config']['tpl_allow_php'] = false; + } + + public static function alter_block_array_data() + { + return array( + array( + 'outer', + array('VARIABLE' => 'before'), + false, + 'insert', + << 'after'), + true, + 'insert', + << 'pos #1'), + 1, + 'insert', + << 'pos #1'), + 0, + 'change', + << 'before'), + false, + 'insert', + << 'after'), + true, + 'insert', + << 'pos #1'), + 1, + 'insert', + << 'before'), + false, + 'insert', + << 'before'), + false, + 'insert', + << 'before'), + false, + 'insert', + <<markTestIncomplete('Alter Block Test is broken'); + + $this->template->set_filenames(array('test' => 'loop_nested.html')); + + // @todo Change this + $this->template->assign_block_vars('outer', array()); + $this->template->assign_block_vars('outer.middle', array()); + $this->template->assign_block_vars('outer.middle', array()); + $this->template->assign_block_vars('outer', array()); + $this->template->assign_block_vars('outer.middle', array()); + $this->template->assign_block_vars('outer.middle', array()); + $this->template->assign_block_vars('outer.middle', array()); + $this->template->assign_block_vars('outer', array()); + $this->template->assign_block_vars('outer.middle', array()); + $this->template->assign_block_vars('outer.middle', array()); + + $this->assertEquals("outer - 0/3\nmiddle - 0/2\nmiddle - 1/2\nouter - 1/3\nmiddle - 0/3\nmiddle - 1/3\nmiddle - 2/3\nouter - 2/3\nmiddle - 0/2\nmiddle - 1/2", $this->display('test'), 'Ensuring template is built correctly before modification'); + + $this->template->alter_block_array($alter_block, $vararray, $key, $mode); + $this->assertEquals($expect, $this->display('test'), $description); + } +} + diff --git a/tests/text_processing/make_clickable.php b/tests/text_processing/make_clickable.php deleted file mode 100644 index 75a35daf82..0000000000 --- a/tests/text_processing/make_clickable.php +++ /dev/null @@ -1,104 +0,0 @@ - whether it should work - $prefix_texts = array( - '' => true, - "np \n" => true, - 'bp text ' => true, - 'cp text>' => true, - 'ep text.' => array('w' => false), // doesn't work for www. type urls, but for everything else - ); - $suffix_texts = array( - '' => true, - "\n ns" => true, - ' bs text.' => true, - '>cs text' => true, - '"ds text' => true, - '. es text.' => true, - ', fs text.' => true, - ); - - $urls = array( - 'http://example.com' => array('tag' => 'm', 'url' => false, 'text' => false), // false means same as key - 'http://example.com/' => array('tag' => 'm', 'url' => false, 'text' => false), - 'http://example.com/path?query=abc' => array('tag' => 'm', 'url' => false, 'text' => false), - 'http://example.com/1' => array('tag' => 'm', 'url' => false, 'text' => false), - 'http://example.com/some/very/long/path/with/over/55/characters?and=a&long=query&too=1' => array('tag' => 'm', 'url' => false, 'text' => 'http://example.com/some/very/long/path/ ... uery&too=1'), - 'http://localhost' => array('tag' => 'm', 'url' => false, 'text' => false), - 'http://localhost/#abc' => array('tag' => 'm', 'url' => false, 'text' => false), - - 'www.example.com/path/' => array('tag' => 'w', 'url' => 'http://www.example.com/path/', 'text' => false), - 'randomwww.example.com/path/' => false, - - 'http://thisdomain.org' => array('tag' => 'm', 'url' => false, 'text' => false), - 'http://thisdomain.org/' => array('tag' => 'm', 'url' => false, 'text' => false), - 'http://thisdomain.org/1' => array('tag' => 'l', 'url' => false, 'text' => '1'), - 'http://thisdomain.org/path/some?query=abc#test' => array('tag' => 'l', 'url' => false, 'text' => 'path/some?query=abc#test'), - - 'javascript:www.example.com/' => false, - ); - - $test_data = array(); - - // run the test for each combination - foreach ($prefix_texts as $prefix => $prefix_success) - { - foreach ($suffix_texts as $suffix => $suffix_success) - { - foreach ($urls as $url => $url_type) - { - $input = $prefix . $url . $suffix; - // no valid url => no change - $output = $input; - - if ( - ($prefix_success && $suffix_success && is_array($url_type)) && - // handle except syntax for prefix/suffix - (!is_array($prefix_success) || !isset($prefix_success[$url_type['tag']]) || $prefix_success[$url_type['tag']] == true) && - (!is_array($suffix_success) || !isset($suffix_success[$url_type['tag']]) || $suffix_success[$url_type['tag']] == true) - ) - { - // false means it's the same as the url, less typing - $url_type['url'] = ($url_type['url']) ? $url_type['url'] : $url; - $url_type['text'] = ($url_type['text']) ? $url_type['text'] : $url; - - $class = ($url_type['tag'] === 'l') ? 'postlink-local' : 'postlink'; - - // replace the url with the desired output format - $output = $prefix . '' . $url_type['text'] . '' . $suffix; - } - $test_data[] = array($input, $output); - } - } - } - - return $test_data; - } - - /** - * @dataProvider make_clickable_data - */ - public function test_make_clickable($input, $expected) - { - $result = make_clickable($input, 'http://thisdomain.org'); - - $label = 'Making text clickable: ' . $input; - $this->assertEquals($expected, $result, $label); - } - -} - diff --git a/tests/text_processing/make_clickable_test.php b/tests/text_processing/make_clickable_test.php new file mode 100644 index 0000000000..75a35daf82 --- /dev/null +++ b/tests/text_processing/make_clickable_test.php @@ -0,0 +1,104 @@ + whether it should work + $prefix_texts = array( + '' => true, + "np \n" => true, + 'bp text ' => true, + 'cp text>' => true, + 'ep text.' => array('w' => false), // doesn't work for www. type urls, but for everything else + ); + $suffix_texts = array( + '' => true, + "\n ns" => true, + ' bs text.' => true, + '>cs text' => true, + '"ds text' => true, + '. es text.' => true, + ', fs text.' => true, + ); + + $urls = array( + 'http://example.com' => array('tag' => 'm', 'url' => false, 'text' => false), // false means same as key + 'http://example.com/' => array('tag' => 'm', 'url' => false, 'text' => false), + 'http://example.com/path?query=abc' => array('tag' => 'm', 'url' => false, 'text' => false), + 'http://example.com/1' => array('tag' => 'm', 'url' => false, 'text' => false), + 'http://example.com/some/very/long/path/with/over/55/characters?and=a&long=query&too=1' => array('tag' => 'm', 'url' => false, 'text' => 'http://example.com/some/very/long/path/ ... uery&too=1'), + 'http://localhost' => array('tag' => 'm', 'url' => false, 'text' => false), + 'http://localhost/#abc' => array('tag' => 'm', 'url' => false, 'text' => false), + + 'www.example.com/path/' => array('tag' => 'w', 'url' => 'http://www.example.com/path/', 'text' => false), + 'randomwww.example.com/path/' => false, + + 'http://thisdomain.org' => array('tag' => 'm', 'url' => false, 'text' => false), + 'http://thisdomain.org/' => array('tag' => 'm', 'url' => false, 'text' => false), + 'http://thisdomain.org/1' => array('tag' => 'l', 'url' => false, 'text' => '1'), + 'http://thisdomain.org/path/some?query=abc#test' => array('tag' => 'l', 'url' => false, 'text' => 'path/some?query=abc#test'), + + 'javascript:www.example.com/' => false, + ); + + $test_data = array(); + + // run the test for each combination + foreach ($prefix_texts as $prefix => $prefix_success) + { + foreach ($suffix_texts as $suffix => $suffix_success) + { + foreach ($urls as $url => $url_type) + { + $input = $prefix . $url . $suffix; + // no valid url => no change + $output = $input; + + if ( + ($prefix_success && $suffix_success && is_array($url_type)) && + // handle except syntax for prefix/suffix + (!is_array($prefix_success) || !isset($prefix_success[$url_type['tag']]) || $prefix_success[$url_type['tag']] == true) && + (!is_array($suffix_success) || !isset($suffix_success[$url_type['tag']]) || $suffix_success[$url_type['tag']] == true) + ) + { + // false means it's the same as the url, less typing + $url_type['url'] = ($url_type['url']) ? $url_type['url'] : $url; + $url_type['text'] = ($url_type['text']) ? $url_type['text'] : $url; + + $class = ($url_type['tag'] === 'l') ? 'postlink-local' : 'postlink'; + + // replace the url with the desired output format + $output = $prefix . '' . $url_type['text'] . '' . $suffix; + } + $test_data[] = array($input, $output); + } + } + } + + return $test_data; + } + + /** + * @dataProvider make_clickable_data + */ + public function test_make_clickable($input, $expected) + { + $result = make_clickable($input, 'http://thisdomain.org'); + + $label = 'Making text clickable: ' . $input; + $this->assertEquals($expected, $result, $label); + } + +} + -- cgit v1.2.1