diff options
Diffstat (limited to 'tests/functions')
22 files changed, 1618 insertions, 44 deletions
diff --git a/tests/functions/clean_path_test.php b/tests/functions/clean_path_test.php deleted file mode 100644 index bcbe9838d9..0000000000 --- a/tests/functions/clean_path_test.php +++ /dev/null @@ -1,44 +0,0 @@ -<?php -/** -* -* @package testing -* @copyright (c) 2012 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 -* -*/ - -require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; - -class phpbb_clean_path_test extends phpbb_test_case -{ - public function clean_path_test_data() - { - return array( - array('foo', 'foo'), - array('foo/bar', 'foo/bar'), - array('foo/bar/', 'foo/bar/'), - array('foo/./bar', 'foo/bar'), - array('foo/./././bar', 'foo/bar'), - array('foo/bar/.', 'foo/bar'), - array('./foo/bar', './foo/bar'), - array('../foo/bar', '../foo/bar'), - array('one/two/three', 'one/two/three'), - array('one/two/../three', 'one/three'), - array('one/../two/three', 'two/three'), - array('one/two/..', 'one'), - array('one/two/../', 'one/'), - array('one/two/../three/../four', 'one/four'), - array('one/two/three/../../four', 'one/four'), - ); - } - - /** - * @dataProvider clean_path_test_data - */ - public function test_clean_path($input, $expected) - { - $output = phpbb_clean_path($input); - - $this->assertEquals($expected, $output); - } -} diff --git a/tests/functions/fixtures/language_select.xml b/tests/functions/fixtures/language_select.xml new file mode 100644 index 0000000000..d7232a3d28 --- /dev/null +++ b/tests/functions/fixtures/language_select.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_lang"> + <column>lang_id</column> + <column>lang_iso</column> + <column>lang_local_name</column> + <column>lang_english_name</column> + <row> + <value>1</value> + <value>en</value> + <value>English</value> + <value>English</value> + </row> + <row> + <value>2</value> + <value>cs</value> + <value>Čeština</value> + <value>Czech</value> + </row> + </table> +</dataset> diff --git a/tests/functions/fixtures/obtain_online.xml b/tests/functions/fixtures/obtain_online.xml new file mode 100644 index 0000000000..05bbe6a05e --- /dev/null +++ b/tests/functions/fixtures/obtain_online.xml @@ -0,0 +1,121 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_sessions"> + <column>session_id</column> + <column>session_user_id</column> + <column>session_forum_id</column> + <column>session_time</column> + <column>session_ip</column> + <column>session_viewonline</column> + </table> + <table name="phpbb_users"> + <column>user_id</column> + <column>username_clean</column> + <column>username</column> + <column>user_allow_viewonline</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <row> + <value>1</value> + <value>anonymous</value> + <value>anonymous</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>2</value> + <value>2</value> + <value>2</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>3</value> + <value>3</value> + <value>3</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>4</value> + <value>4</value> + <value>4</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>5</value> + <value>5</value> + <value>5</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>6</value> + <value>6</value> + <value>6</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>7</value> + <value>7</value> + <value>7</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>8</value> + <value>8</value> + <value>8</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>9</value> + <value>9</value> + <value>9</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>10</value> + <value>10</value> + <value>10</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/functions/fixtures/style_select.xml b/tests/functions/fixtures/style_select.xml new file mode 100644 index 0000000000..ca95f94461 --- /dev/null +++ b/tests/functions/fixtures/style_select.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_styles"> + <column>style_id</column> + <column>style_name</column> + <column>style_active</column> + <column>style_parent_tree</column> + <row> + <value>1</value> + <value>prosilver</value> + <value>1</value> + <value></value> + </row> + <row> + <value>2</value> + <value>subsilver2</value> + <value>1</value> + <value></value> + </row> + <row> + <value>3</value> + <value>zoo</value> + <value>0</value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/functions/fixtures/validate_email.xml b/tests/functions/fixtures/validate_email.xml new file mode 100644 index 0000000000..de7fce8a08 --- /dev/null +++ b/tests/functions/fixtures/validate_email.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_users"> + <column>user_id</column> + <column>username</column> + <column>username_clean</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <column>user_email_hash</column> + <row> + <value>1</value> + <value>admin</value> + <value>admin</value> + <value></value> + <value></value> + <value></value> + <value></value> + <value>143317126117</value> + </row> + </table> +</dataset> diff --git a/tests/functions/fixtures/validate_username.xml b/tests/functions/fixtures/validate_username.xml new file mode 100644 index 0000000000..fbe398469c --- /dev/null +++ b/tests/functions/fixtures/validate_username.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_groups"> + <column>group_name</column> + <column>group_desc</column> + <row> + <value>foobar_group</value> + <value>test123</value> + </row> + </table> + <table name="phpbb_users"> + <column>user_id</column> + <column>username</column> + <column>username_clean</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <row> + <value>1</value> + <value>admin</value> + <value>admin</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>2</value> + <value>moderator</value> + <value>moderator</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/functions/get_remote_file_test.php b/tests/functions/get_remote_file_test.php new file mode 100644 index 0000000000..4032ca5b58 --- /dev/null +++ b/tests/functions/get_remote_file_test.php @@ -0,0 +1,75 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_admin.php'; + +/** +* @group slow +*/ +class phpbb_functions_get_remote_file extends phpbb_test_case +{ + public function test_version_phpbb_com() + { + $hostname = 'version.phpbb.com'; + + if (!phpbb_checkdnsrr($hostname, 'A')) + { + $this->markTestSkipped(sprintf( + 'Could not find a DNS record for hostname %s. ' . + 'Assuming network is down.', + $hostname + )); + } + + $errstr = $errno = null; + $file = get_remote_file($hostname, '/phpbb', '30x.txt', $errstr, $errno); + + $this->assertNotEquals( + 0, + strlen($file), + 'Failed asserting that the response is not empty.' + ); + + $this->assertSame( + '', + $errstr, + 'Failed asserting that the error string is empty.' + ); + + $this->assertSame( + 0, + $errno, + 'Failed asserting that the error number is 0 (i.e. no error occurred).' + ); + + $lines = explode("\n", $file); + + $this->assertGreaterThanOrEqual( + 2, + sizeof($lines), + 'Failed asserting that the version file has at least two lines.' + ); + + $this->assertStringStartsWith( + '3.', + $lines[0], + "Failed asserting that the first line of the version file starts with '3.'" + ); + + $this->assertNotSame( + false, + filter_var($lines[1], FILTER_VALIDATE_URL), + 'Failed asserting that the second line of the version file is a valid URL.' + ); + + $this->assertContains('http', $lines[1]); + $this->assertContains('phpbb.com', $lines[1], '', true); + } +} diff --git a/tests/functions/is_absolute_test.php b/tests/functions/is_absolute_test.php new file mode 100644 index 0000000000..6d26793d82 --- /dev/null +++ b/tests/functions/is_absolute_test.php @@ -0,0 +1,56 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_functions_is_absolute_test extends phpbb_test_case +{ + static public function is_absolute_data() + { + return array( + // Empty + array('', false), + + // Absolute unix style + array('/etc/phpbb', true), + // Unix does not support \ so that is not an absolute path + array('\etc\phpbb', false), + + // Absolute windows style + array('c:\windows', true), + array('C:\Windows', true), + array('c:/windows', true), + array('C:/Windows', true), + + // Executable + array('etc/phpbb', false), + array('explorer.exe', false), + + // Relative subdir + array('Windows\System32', false), + array('Windows\System32\explorer.exe', false), + array('Windows/System32', false), + array('Windows/System32/explorer.exe', false), + + // Relative updir + array('..\Windows\System32', false), + array('..\Windows\System32\explorer.exe', false), + array('../Windows/System32', false), + array('../Windows/System32/explorer.exe', false), + ); + } + + /** + * @dataProvider is_absolute_data + */ + public function test_is_absolute($path, $expected) + { + $this->assertEquals($expected, phpbb_is_absolute($path)); + } +} diff --git a/tests/functions/language_select_test.php b/tests/functions/language_select_test.php new file mode 100644 index 0000000000..3341e2a256 --- /dev/null +++ b/tests/functions/language_select_test.php @@ -0,0 +1,39 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_functions_language_select_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/language_select.xml'); + } + + static public function language_select_data() + { + return array( + array('', '<option value="cs">Čeština</option><option value="en">English</option>'), + array('en', '<option value="cs">Čeština</option><option value="en" selected="selected">English</option>'), + array('cs', '<option value="cs" selected="selected">Čeština</option><option value="en">English</option>'), + array('de', '<option value="cs">Čeština</option><option value="en">English</option>'), + ); + } + + /** + * @dataProvider language_select_data + */ + public function test_language_select($default, $expected) + { + global $db; + $db = $this->new_dbal(); + + $this->assertEquals($expected, language_select($default)); + } +} diff --git a/tests/functions/obtain_online_test.php b/tests/functions/obtain_online_test.php new file mode 100644 index 0000000000..fe372431a9 --- /dev/null +++ b/tests/functions/obtain_online_test.php @@ -0,0 +1,227 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php'; + +class phpbb_functions_obtain_online_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/obtain_online.xml'); + } + + protected function setUp() + { + parent::setUp(); + + global $config, $db; + + $db = $this->db = $this->new_dbal(); + $config = array( + 'load_online_time' => 5, + ); + } + + static public function obtain_guest_count_data() + { + return array( + array(0, 2), + array(1, 1), + ); + } + + /** + * @dataProvider obtain_guest_count_data + */ + public function test_obtain_guest_count($forum_id, $expected) + { + $this->db->sql_query('DELETE FROM phpbb_sessions'); + + $time = time(); + $this->create_guest_sessions($time); + $this->assertEquals($expected, obtain_guest_count($forum_id)); + } + + static public function obtain_users_online_data() + { + return array( + array(0, false, array( + 'online_users' => array(2 => 2, 3 => 3, 6 => 6, 7 => 7, 10 => 10), + 'hidden_users' => array(6 => 6, 7 => 7, 10 => 10), + 'total_online' => 5, + 'visible_online' => 2, + 'hidden_online' => 3, + 'guests_online' => 0, + )), + array(0, true, array( + 'online_users' => array(2 => 2, 3 => 3, 6 => 6, 7 => 7, 10 => 10), + 'hidden_users' => array(6 => 6, 7 => 7, 10 => 10), + 'total_online' => 7, + 'visible_online' => 2, + 'hidden_online' => 3, + 'guests_online' => 2, + )), + array(1, false, array( + 'online_users' => array(3 => 3, 7 => 7), + 'hidden_users' => array(7 => 7), + 'total_online' => 2, + 'visible_online' => 1, + 'hidden_online' => 1, + 'guests_online' => 0, + )), + array(1, true, array( + 'online_users' => array(3 => 3, 7 => 7), + 'hidden_users' => array(7 => 7), + 'total_online' => 3, + 'visible_online' => 1, + 'hidden_online' => 1, + 'guests_online' => 1, + )), + array(2, false, array( + 'online_users' => array(), + 'hidden_users' => array(), + 'total_online' => 0, + 'visible_online' => 0, + 'hidden_online' => 0, + 'guests_online' => 0, + )), + array(2, true, array( + 'online_users' => array(), + 'hidden_users' => array(), + 'total_online' => 0, + 'visible_online' => 0, + 'hidden_online' => 0, + 'guests_online' => 0, + )), + ); + } + + /** + * @dataProvider obtain_users_online_data + */ + public function test_obtain_users_online($forum_id, $display_guests, $expected) + { + $this->db->sql_query('DELETE FROM phpbb_sessions'); + + global $config; + $config['load_online_guests'] = $display_guests; + + $time = time(); + $this->create_guest_sessions($time); + $this->create_user_sessions($time); + $this->assertEquals($expected, obtain_users_online($forum_id)); + } + + static public function obtain_users_online_string_data() + { + return array( + array(0, false, array( + 'online_userlist' => 'REGISTERED_USERS 2, 3', + 'l_online_users' => 'ONLINE_USERS_TOTAL 5 REG_USERS_TOTAL 2 HIDDEN_USERS_TOTAL 3', + )), + array(0, true, array( + 'online_userlist' => 'REGISTERED_USERS 2, 3', + 'l_online_users' => 'ONLINE_USERS_TOTAL_GUESTS 7 REG_USERS_TOTAL 2 HIDDEN_USERS_TOTAL 3 GUEST_USERS_TOTAL 2', + )), + array(1, false, array( + 'online_userlist' => 'BROWSING_FORUM 3', + 'l_online_users' => 'ONLINE_USERS_TOTAL 2 REG_USERS_TOTAL 1 HIDDEN_USERS_TOTAL 1', + )), + array(1, true, array( + 'online_userlist' => 'BROWSING_FORUM_GUESTS 1 3', + 'l_online_users' => 'ONLINE_USERS_TOTAL_GUESTS 3 REG_USERS_TOTAL 1 HIDDEN_USERS_TOTAL 1 GUEST_USERS_TOTAL 1', + )), + array(2, false, array( + 'online_userlist' => 'BROWSING_FORUM NO_ONLINE_USERS', + 'l_online_users' => 'ONLINE_USERS_TOTAL 0 REG_USERS_TOTAL 0 HIDDEN_USERS_TOTAL 0', + )), + array(2, true, array( + 'online_userlist' => 'BROWSING_FORUM_GUESTS 0 NO_ONLINE_USERS', + 'l_online_users' => 'ONLINE_USERS_TOTAL_GUESTS 0 REG_USERS_TOTAL 0 HIDDEN_USERS_TOTAL 0 GUEST_USERS_TOTAL 0', + )), + ); + } + + /** + * @dataProvider obtain_users_online_string_data + */ + public function test_obtain_users_online_string($forum_id, $display_guests, $expected) + { + $this->db->sql_query('DELETE FROM phpbb_sessions'); + + global $config, $user, $auth, $phpbb_dispatcher; + $config['load_online_guests'] = $display_guests; + $user = new phpbb_mock_lang(); + $user->lang = $this->load_language(); + $auth = $this->getMock('phpbb_auth'); + $acl_get_map = array( + array('u_viewonline', true), + array('u_viewprofile', true), + ); + $auth->expects($this->any()) + ->method('acl_get') + ->with($this->stringContains('_'), + $this->anything()) + ->will($this->returnValueMap($acl_get_map)); + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + + $time = time(); + $this->create_guest_sessions($time); + $this->create_user_sessions($time); + + $online_users = obtain_users_online($forum_id); + $this->assertEquals($expected, obtain_users_online_string($online_users, $forum_id)); + } + + protected function create_guest_sessions($time) + { + $this->add_session(1, '0001', '192.168.0.1', 0, true, $time); + $this->add_session(1, '0002', '192.168.0.2', 1, true, $time); + $this->add_session(1, '0003', '192.168.0.3', 0, true, $time, 10); + $this->add_session(1, '0004', '192.168.0.4', 1, true, $time, 10); + } + + protected function create_user_sessions($time) + { + $this->add_session(2, '0005', '192.168.0.5', 0, true, $time); + $this->add_session(3, '0006', '192.168.0.6', 1, true, $time); + $this->add_session(4, '0007', '192.168.0.7', 0, true, $time, 10); + $this->add_session(5, '0008', '192.168.0.8', 1, true, $time, 10); + $this->add_session(6, '0005', '192.168.0.9', 0, false, $time); + $this->add_session(7, '0006', '192.168.0.10', 1, false, $time); + $this->add_session(8, '0007', '192.168.0.11', 0, false, $time, 10); + $this->add_session(9, '0008', '192.168.0.12', 1, false, $time, 10); + $this->add_session(10, '009', '192.168.0.13', 0, false, $time); + } + + protected function add_session($user_id, $session_id, $user_ip, $forum_id, $view_online, $time, $time_delta = 0) + { + $sql_ary = array( + 'session_id' => $user_id . '_' . $forum_id . '_session00000000000000000' . $session_id, + 'session_user_id' => $user_id, + 'session_ip' => $user_ip, + 'session_forum_id' => $forum_id, + 'session_time' => $time - $time_delta * 60, + 'session_viewonline' => $view_online, + ); + $this->db->sql_query('INSERT INTO phpbb_sessions ' . $this->db->sql_build_array('INSERT', $sql_ary)); + } + + protected function load_language() + { + return array( + 'NO_ONLINE_USERS' => 'NO_ONLINE_USERS', + 'REGISTERED_USERS' => 'REGISTERED_USERS', + 'BROWSING_FORUM' => 'BROWSING_FORUM %s', + 'BROWSING_FORUM_GUEST' => 'BROWSING_FORUM_GUEST %s %d', + 'BROWSING_FORUM_GUESTS' => 'BROWSING_FORUM_GUESTS %s %d', + ); + } +} diff --git a/tests/functions/style_select_test.php b/tests/functions/style_select_test.php new file mode 100644 index 0000000000..1e44f3c2cb --- /dev/null +++ b/tests/functions/style_select_test.php @@ -0,0 +1,41 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_functions_style_select_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/style_select.xml'); + } + + static public function style_select_data() + { + return array( + array('', false, '<option value="1">prosilver</option><option value="2">subsilver2</option>'), + array('', true, '<option value="1">prosilver</option><option value="2">subsilver2</option><option value="3">zoo</option>'), + array('1', false, '<option value="1" selected="selected">prosilver</option><option value="2">subsilver2</option>'), + array('1', true, '<option value="1" selected="selected">prosilver</option><option value="2">subsilver2</option><option value="3">zoo</option>'), + array('3', false, '<option value="1">prosilver</option><option value="2">subsilver2</option>'), + array('3', true, '<option value="1">prosilver</option><option value="2">subsilver2</option><option value="3" selected="selected">zoo</option>'), + ); + } + + /** + * @dataProvider style_select_data + */ + public function test_style_select($default, $all, $expected) + { + global $db; + $db = $this->new_dbal(); + + $this->assertEquals($expected, style_select($default, $all)); + } +} diff --git a/tests/functions/validate_data_helper.php b/tests/functions/validate_data_helper.php new file mode 100644 index 0000000000..b92a3aa5eb --- /dev/null +++ b/tests/functions/validate_data_helper.php @@ -0,0 +1,36 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_functions_validate_data_helper +{ + protected $test_case; + + public function __construct($test_case) + { + $this->test_case = $test_case; + } + + /** + * Test provided input data with supplied checks and compare to expected + * results + * + * @param array $data Array containing one or more subarrays with the + * test data. The first element of a subarray is the + * expected result, the second one is the input, and the + * third is the data that should be passed to the function + * validate_data(). + */ + public function assert_valid_data($data) + { + foreach ($data as $key => $test) + { + $this->test_case->assertEquals($test[0], validate_data(array($test[1]), array($test[2]))); + } + } +} diff --git a/tests/functions/validate_date_test.php b/tests/functions/validate_date_test.php new file mode 100644 index 0000000000..1dcd1361a2 --- /dev/null +++ b/tests/functions/validate_date_test.php @@ -0,0 +1,82 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_date_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_date() + { + $this->helper->assert_valid_data(array( + 'empty' => array( + array('INVALID'), + '', + array('date'), + ), + 'empty_opt' => array( + array(), + '', + array('date', true), + ), + 'double_single' => array( + array(), + '17-06-1990', + array('date'), + ), + 'single_single' => array( + array(), + '05-05-2009', + array('date'), + ), + 'double_double' => array( + array(), + '17-12-1990', + array('date'), + ), + 'month_high' => array( + array('INVALID'), + '17-17-1990', + array('date'), + ), + 'month_low' => array( + array('INVALID'), + '01-00-1990', + array('date'), + ), + 'day_high' => array( + array('INVALID'), + '64-01-1990', + array('date'), + ), + 'day_low' => array( + array('INVALID'), + '00-12-1990', + array('date'), + ), + // Currently fails + /* + 'zero_year' => array( + array(), + '01-01-0000', + array('date'), + ), + */ + )); + } +} diff --git a/tests/functions/validate_email_test.php b/tests/functions/validate_email_test.php new file mode 100644 index 0000000000..9a6ce39251 --- /dev/null +++ b/tests/functions/validate_email_test.php @@ -0,0 +1,108 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/../mock/user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_email_test extends phpbb_database_test_case +{ + protected $db; + protected $user; + protected $helper; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/validate_email.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->user = new phpbb_mock_user; + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + /** + * Get validation prerequesites + * + * @param bool $check_mx Whether mx records should be checked + */ + protected function set_validation_prerequisites($check_mx) + { + global $config, $db, $user; + + $config['email_check_mx'] = $check_mx; + $db = $this->db; + $user = $this->user; + $user->optionset('banned_users', array('banned@example.com')); + } + + public function test_validate_email() + { + $this->set_validation_prerequisites(false); + + $this->helper->assert_valid_data(array( + 'empty' => array( + array(), + '', + array('email'), + ), + 'allowed' => array( + array(), + 'foobar@example.com', + array('email', 'foobar@example.com'), + ), + 'invalid' => array( + array('EMAIL_INVALID'), + 'fööbar@example.com', + array('email'), + ), + 'valid_complex' => array( + array(), + "'%$~test@example.com", + array('email'), + ), + 'taken' => array( + array('EMAIL_TAKEN'), + 'admin@example.com', + array('email'), + ), + 'banned' => array( + array('EMAIL_BANNED'), + 'banned@example.com', + array('email'), + ), + )); + } + + /** + * @group slow + */ + public function test_validate_email_mx() + { + $this->set_validation_prerequisites(true); + + $this->helper->assert_valid_data(array( + 'valid' => array( + array(), + 'foobar@phpbb.com', + array('email'), + ), + 'no_mx' => array( + array('DOMAIN_NO_MX_RECORD'), + 'test@does-not-exist.phpbb.com', + array('email'), + ), + )); + } +} diff --git a/tests/functions/validate_hex_colour_test.php b/tests/functions/validate_hex_colour_test.php new file mode 100644 index 0000000000..812ebe5eeb --- /dev/null +++ b/tests/functions/validate_hex_colour_test.php @@ -0,0 +1,121 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; + +class phpbb_functions_validate_hex_colour_test extends phpbb_test_case +{ + public function positive_match_data() + { + return array( + array('a00'), + array('AFF'), + array('AA0000'), + array('aa00FF'), + array('000'), + array('000000'), + ); + } + + public function negative_match_data() + { + return array( + // Invalid prefix + array('#aa0'), + array('#AA0000'), + array('vAA0000'), + + // Invalid suffix + array('AA0000v'), + + // Correct length, but out of hex range + array('ag0'), + array('AAG000'), + + // Too long + array('AA00000'), + array('AA0000 '), + array('AA0000 abf'), + array('AA0000 AA0000'), + + // empty() + array('0'), + ); + } + + public function optional_only_data() + { + return array( + // The empty colour, i.e. "no colour". + array(''), + ); + } + + public function strict_negative_match_data() + { + return array_merge( + $this->negative_match_data(), + $this->optional_only_data() + ); + } + + public function nonstrict_positive_match_data() + { + return array_merge( + $this->positive_match_data(), + $this->optional_only_data() + ); + } + + /** + * @dataProvider positive_match_data + */ + public function test_strict_positive_match($input) + { + $this->assertFalse( + phpbb_validate_hex_colour($input, false), + "Failed asserting that $input passes as a valid hex colour." + ); + } + + /** + * @dataProvider strict_negative_match_data + */ + public function test_strict_negative_match($input) + { + $this->assertSame( + 'WRONG_DATA', + phpbb_validate_hex_colour($input, false), + "Failed asserting that $input does not pass as a valid hex colour." + ); + } + + /** + * @dataProvider nonstrict_positive_match_data + */ + public function test_nonstrict_positive_match($input) + { + $this->assertFalse( + phpbb_validate_hex_colour($input, true), + "Failed asserting that $input passes as a valid or optional hex colour." + ); + } + + /** + * @dataProvider negative_match_data + */ + public function test_nonstrict_negative_match($input) + { + $this->assertSame( + 'WRONG_DATA', + phpbb_validate_hex_colour($input, true), + "Failed asserting that $input does not pass as a valid or optional hex colour." + ); + } +} diff --git a/tests/functions/validate_jabber_test.php b/tests/functions/validate_jabber_test.php new file mode 100644 index 0000000000..5a53c963bd --- /dev/null +++ b/tests/functions/validate_jabber_test.php @@ -0,0 +1,79 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_jabber_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_jabber() + { + $this->helper->assert_valid_data(array( + 'empty' => array( + array(), + '', + array('jabber'), + ), + 'no_seperator' => array( + array('WRONG_DATA'), + 'testjabber.ccc', + array('jabber'), + ), + 'no_user' => array( + array('WRONG_DATA'), + '@jabber.ccc', + array('jabber'), + ), + 'no_realm' => array( + array('WRONG_DATA'), + 'user@', + array('jabber'), + ), + 'dot_realm' => array( + array('WRONG_DATA'), + 'user@.....', + array('jabber'), + ), + '-realm' => array( + array('WRONG_DATA'), + 'user@-jabber.ccc', + array('jabber'), + ), + 'realm-' => array( + array('WRONG_DATA'), + 'user@jabber.ccc-', + array('jabber'), + ), + 'correct' => array( + array(), + 'user@jabber.09A-z.org', + array('jabber'), + ), + 'prohibited' => array( + array('WRONG_DATA'), + 'u@ser@jabber.ccc.org', + array('jabber'), + ), + 'prohibited_char' => array( + array('WRONG_DATA'), + 'u<s>er@jabber.ccc.org', + array('jabber'), + ), + )); + } +} diff --git a/tests/functions/validate_lang_iso_test.php b/tests/functions/validate_lang_iso_test.php new file mode 100644 index 0000000000..c8a5b71021 --- /dev/null +++ b/tests/functions/validate_lang_iso_test.php @@ -0,0 +1,60 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_lang_iso_test extends phpbb_database_test_case +{ + protected $db; + protected $helper; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/language_select.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_lang_iso() + { + global $db; + + $db = $this->db; + + $this->helper->assert_valid_data(array( + 'empty' => array( + array('WRONG_DATA'), + '', + array('language_iso_name'), + ), + 'en' => array( + array(), + 'en', + array('language_iso_name'), + ), + 'cs' => array( + array(), + 'cs', + array('language_iso_name'), + ), + 'de' => array( + array('WRONG_DATA'), + 'de', + array('language_iso_name'), + ), + )); + } +} diff --git a/tests/functions/validate_match_test.php b/tests/functions/validate_match_test.php new file mode 100644 index 0000000000..73a363e003 --- /dev/null +++ b/tests/functions/validate_match_test.php @@ -0,0 +1,49 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_match_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_match() + { + $this->helper->assert_valid_data(array( + 'empty_opt' => array( + array(), + '', + array('match', true, '/[a-z]$/'), + ), + 'empty_empty_match' => array( + array(), + '', + array('match'), + ), + 'foobar' => array( + array(), + 'foobar', + array('match', false, '/[a-z]$/'), + ), + 'foobar_fail' => array( + array('WRONG_DATA'), + 'foobar123', + array('match', false, '/[a-z]$/'), + ), + )); + } +} diff --git a/tests/functions/validate_num_test.php b/tests/functions/validate_num_test.php new file mode 100644 index 0000000000..4deac02ebc --- /dev/null +++ b/tests/functions/validate_num_test.php @@ -0,0 +1,59 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_num_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_num() + { + $this->helper->assert_valid_data(array( + 'empty' => array( + array(), + '', + array('num'), + ), + 'zero' => array( + array(), + '0', + array('num'), + ), + 'five_minmax_correct' => array( + array(), + '5', + array('num', false, 2, 6), + ), + 'five_minmax_short' => array( + array('TOO_SMALL'), + '5', + array('num', false, 7, 10), + ), + 'five_minmax_long' => array( + array('TOO_LARGE'), + '5', + array('num', false, 2, 3), + ), + 'string' => array( + array(), + 'foobar', + array('num'), + ), + )); + } +} diff --git a/tests/functions/validate_password_test.php b/tests/functions/validate_password_test.php new file mode 100644 index 0000000000..4639f6cc89 --- /dev/null +++ b/tests/functions/validate_password_test.php @@ -0,0 +1,96 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_password_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function validate_password_data() + { + return array( + array('PASS_TYPE_ANY', array( + 'empty' => array(), + 'foobar_any' => array(), + 'foobar_mixed' => array(), + 'foobar_alpha' => array(), + 'foobar_symbol' => array(), + )), + array('PASS_TYPE_CASE', array( + 'empty' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_mixed' => array(), + 'foobar_alpha' => array(), + 'foobar_symbol' => array(), + )), + array('PASS_TYPE_ALPHA', array( + 'empty' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_mixed' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_symbol' => array(), + )), + array('PASS_TYPE_SYMBOL', array( + 'empty' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_mixed' => array('INVALID_CHARS'), + 'foobar_alpha' => array('INVALID_CHARS'), + 'foobar_symbol' => array(), + )), + ); + } + + /** + * @dataProvider validate_password_data + */ + public function test_validate_password($pass_complexity, $expected) + { + global $config; + + // Set complexity to mixed case letters, numbers and symbols + $config['pass_complex'] = $pass_complexity; + + $this->helper->assert_valid_data(array( + 'empty' => array( + $expected['empty'], + '', + array('password'), + ), + 'foobar_any' => array( + $expected['foobar_any'], + 'foobar', + array('password'), + ), + 'foobar_mixed' => array( + $expected['foobar_mixed'], + 'FooBar', + array('password'), + ), + 'foobar_alpha' => array( + $expected['foobar_alpha'], + 'F00bar', + array('password'), + ), + 'foobar_symbol' => array( + $expected['foobar_symbol'], + 'fooBar123*', + array('password'), + ), + )); + } +} diff --git a/tests/functions/validate_string_test.php b/tests/functions/validate_string_test.php new file mode 100644 index 0000000000..ab44c28541 --- /dev/null +++ b/tests/functions/validate_string_test.php @@ -0,0 +1,70 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_string_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_string() + { + $this->helper->assert_valid_data(array( + 'empty_opt' => array( + array(), + '', + array('string', true), + ), + 'empty' => array( + array(), + '', + array('string'), + ), + 'foo' => array( + array(), + 'foobar', + array('string'), + ), + 'foo_minmax_correct' => array( + array(), + 'foobar', + array('string', false, 2, 6), + ), + 'foo_minmax_short' => array( + array('TOO_SHORT'), + 'foobar', + array('string', false, 7, 9), + ), + 'foo_minmax_long' => array( + array('TOO_LONG'), + 'foobar', + array('string', false, 2, 5), + ), + 'empty_short' => array( + array('TOO_SHORT'), + '', + array('string', false, 1, 6), + ), + 'empty_length_opt' => array( + array(), + '', + array('string', true, 1, 6), + ), + )); + } +} diff --git a/tests/functions/validate_username_test.php b/tests/functions/validate_username_test.php new file mode 100644 index 0000000000..0819974e54 --- /dev/null +++ b/tests/functions/validate_username_test.php @@ -0,0 +1,190 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; +require_once dirname(__FILE__) . '/../mock/cache.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_data_test extends phpbb_database_test_case +{ + protected $db; + protected $cache; + protected $helper; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/validate_username.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->cache = new phpbb_mock_cache; + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function validate_username_data() + { + return array( + array('USERNAME_CHARS_ANY', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array(), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array(), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array(), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('USERNAME_TAKEN'), + )), + array('USERNAME_ALPHA_ONLY', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array('INVALID_CHARS'), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array('INVALID_CHARS'), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('INVALID_CHARS'), + )), + array('USERNAME_ALPHA_SPACERS', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array(), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array('INVALID_CHARS'), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('USERNAME_TAKEN'), + )), + array('USERNAME_LETTER_NUM', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array('INVALID_CHARS'), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array('INVALID_CHARS'), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('INVALID_CHARS'), + )), + array('USERNAME_LETTER_NUM_SPACERS', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array(), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array(), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('USERNAME_TAKEN'), + )), + array('USERNAME_ASCII', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array(), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array(), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array('INVALID_CHARS'), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('USERNAME_TAKEN'), + )), + ); + } + + /** + * @dataProvider validate_username_data + */ + public function test_validate_username($allow_name_chars, $expected) + { + global $cache, $config, $db; + + $db = $this->db; + $cache = $this->cache; + $cache->put('_disallowed_usernames', array('barfoo')); + + $config['allow_name_chars'] = $allow_name_chars; + + $this->helper->assert_valid_data(array( + 'foobar_allow' => array( + $expected['foobar_allow'], + 'foobar', + array('username', 'foobar'), + ), + 'foobar_ascii' => array( + $expected['foobar_ascii'], + 'foobar', + array('username'), + ), + 'foobar_any' => array( + $expected['foobar_any'], + 'f*~*^=oo_bar1', + array('username'), + ), + 'foobar_alpha' => array( + $expected['foobar_alpha'], + 'fo0Bar', + array('username'), + ), + 'foobar_alpha_spacers' => array( + $expected['foobar_alpha_spacers'], + 'Fo0-[B]_a+ R', + array('username'), + ), + 'foobar_letter_num' => array( + $expected['foobar_letter_num'], + 'fo0Bar0', + array('username'), + ), + 'foobar_letter_num_sp' => array( + $expected['foobar_letter_num_sp'], + 'Fö0-[B]_a+ R', + array('username'), + ), + 'foobar_quot' => array( + $expected['foobar_quot'], + '"foobar"', + array('username'), + ), + 'barfoo_disallow' => array( + $expected['barfoo_disallow'], + 'barfoo', + array('username'), + ), + 'admin_taken' => array( + $expected['admin_taken'], + 'admin', + array('username'), + ), + 'group_taken' => array( + $expected['group_taken'], + 'foobar_group', + array('username'), + ), + )); + } +} |