From 3d625ab0cf9fe8d1869fbdd66a4602553bb744d4 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 29 May 2013 18:24:54 +0200 Subject: [PATCH 01/10] [ticket/11579] Add basic set of unit tests for validate_data() This currently includes tests for the string, num, date, match, and language iso validation functions. PHPBB3-11579 --- tests/functions/validate_data_test.php | 252 +++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 tests/functions/validate_data_test.php diff --git a/tests/functions/validate_data_test.php b/tests/functions/validate_data_test.php new file mode 100644 index 0000000000..6a429a5529 --- /dev/null +++ b/tests/functions/validate_data_test.php @@ -0,0 +1,252 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/language_select.xml'); + } + + /** + * Test provided input data with supplied checks and compare to expected + * results + * + * @param array $input Input data with specific array keys that need to + * be matched by the ones in the other 2 params + * @param array $validate_check Array containing validate_data check + * settings, i.e. array('foobar' => array('string')) + * @param array $expected Array containing the expected results. Either + * an array containing the error message or the an empty + * array if input is correct + */ + public function validate_data_check($input, $validate_check, $expected) + { + foreach ($input as $key => $data) + { + $this->assertEquals($expected[$key], validate_data(array($data), array($validate_check[$key]))); + } + } + + /* + * Types to test + * - string: + * empty + optional = true --> good + * empty + optional = false --> good (min = 0) + * 'foobar' --> good + * 'foobar' + optional = false|true + min = 2 + max = 6 --> good + * 'foobar' + " + min = 7 + max = 9 --> TOO_SHORT + * 'foobar' + " + min = 2 + max = 5 --> TOO_LONG + * '' + optional = false + min = 1 + max = 6 --> TOO_SHORT + * '' + optional = true + min = 1 + max = 6 --> good + */ + public function test_validate_string() + { + $this->validate_data_check(array( + 'empty_opt' => '', + 'empty' => '', + 'foo' => 'foobar', + 'foo_minmax_correct' => 'foobar', + 'foo_minmax_short' => 'foobar', + 'foo_minmax_long' => 'foobar', + 'empty_short' => '', + 'empty_length_opt' => '', + ), + array( + 'empty_opt' => array('string', true), + 'empty' => array('string'), + 'foo' => array('string'), + 'foo_minmax_correct' => array('string', false, 2, 6), + 'foo_minmax_short' => array('string', false, 7, 9), + 'foo_minmax_long' => array('string', false, 2, 5), + 'empty_short' => array('string', false, 1, 6), + 'empty_length_opt' => array('string', true, 1, 6), + ), + array( + 'empty_opt' => array(), + 'empty' => array(), + 'foo' => array(), + 'foo_minmax_correct' => array(), + 'foo_minmax_short' => array('TOO_SHORT'), + 'foo_minmax_long' => array('TOO_LONG'), + 'empty_short' => array('TOO_SHORT'), + 'empty_length_opt' => array(), + )); + } + + /* + * Types to test + * - num + * empty + optional = true|false --> good + * 0 --> good + * 5 + optional = false|true + min = 2 + max = 6 --> good + * 5 + optional = false|true + min = 7 + max = 10 --> TOO_SMALL + * 5 + optional = false|true + min = 2 + max = 3 --> TOO_LARGE + * 'foobar' --> should fail with WRONG_DATA_NUMERIC !!! + */ + public function test_validate_num() + { + $this->validate_data_check(array( + 'empty' => '', + 'zero' => 0, + 'five_minmax_correct' => 5, + 'five_minmax_short' => 5, + 'five_minmax_long' => 5, + 'string' => 'foobar', + ), + array( + 'empty' => array('num'), + 'zero' => array('num'), + 'five_minmax_correct' => array('num', false, 2, 6), + 'five_minmax_short' => array('num', false, 7, 10), + 'five_minmax_long' => array('num', false, 2, 3), + 'string' => array('num'), + ), + array( + 'empty' => array(), + 'zero' => array(), + 'five_minmax_correct' => array(), + 'five_minmax_short' => array('TOO_SMALL'), + 'five_minmax_long' => array('TOO_LARGE'), + 'string' => array(), + )); + } + + /* + * Types to test + * - date + * . '' --> invalid + * . '' + optional = true --> good + * . 17-06-1990 --> good + * . 05-05-1990 --> good + * . 17-12-1990 --> good + * . 01-01-0000 --> good!!! + * . 17-17-1990 --> invalid + * . 00-12-1990 --> invalid + * . 01-00-1990 --> invalid + */ + public function test_validate_date() + { + $this->validate_data_check(array( + 'empty' => '', + 'empty_opt' => '', + 'double_single' => '17-06-1990', + 'single_single' => '05-05-2009', + 'double_double' => '17-12-1990', + // Currently fails + //'zero_year' => '01-01-0000', + 'month_high' => '17-17-1990', + 'month_low' => '01-00-1990', + 'day_high' => '64-01-1990', + 'day_low' => '00-12-1990', + ), + array( + 'empty' => array('date'), + 'empty_opt' => array('date', true), + 'double_single' => array('date'), + 'single_single' => array('date'), + 'double_double' => array('date'), + // Currently fails + //'zero_year' => array('date'), + 'month_high' => array('date'), + 'month_low' => array('date'), + 'day_high' => array('date'), + 'day_low' => array('date'), + ), + array( + 'empty' => array('INVALID'), + 'empty_opt' => array(), + 'double_single' => array(), + 'single_single' => array(), + 'double_double' => array(), + // Currently fails + //'zero_year' => array(), + 'month_high' => array('INVALID'), + 'month_low' => array('INVALID'), + 'day_high' => array('INVALID'), + 'day_low' => array('INVALID'), + )); + } + + /* + * Types to test + * - match + * . empty + optional = true --> good + * . empty + empty match --> good + * . 'test' + optional = true|false + match = '/[a-z]/' --> good + * . 'test123' + optional = true|false + match = '/[a-z]/' --> WRONG_DATA_MATCH + */ + public function test_validate_match() + { + $this->validate_data_check(array( + 'empty_opt' => '', + 'empty_empty_match' => '', + 'foobar' => 'foobar', + 'foobar_fail' => 'foobar123', + ), + array( + 'empty_opt' => array('match', true, '/[a-z]$/'), + 'empty_empty_match' => array('match'), + 'foobar' => array('match', false, '/[a-z]$/'), + 'foobar_fail' => array('match', false, '/[a-z]$/'), + ), + array( + 'empty_opt' => array(), + 'empty_empty_match' => array(), + 'foobar' => array(), + 'foobar_fail' => array('WRONG_DATA'), + )); + } + + /* + * Types to test + * - language_iso_name + * . empty --> WRONG_DATA + * . 'en' --> good + * . 'cs' --> good + * . 'de' --> WRONG_DATA (won't exist) + */ + public function test_validate_lang_iso() + { + global $db; + + $db = $this->new_dbal(); + + $this->validate_data_check(array( + 'empty' => '', + 'en' => 'en', + 'cs' => 'cs', + 'de' => 'de', + ), + array( + 'empty' => array('language_iso_name'), + 'en' => array('language_iso_name'), + 'cs' => array('language_iso_name'), + 'de' => array('language_iso_name'), + ), + array( + 'empty' => array('WRONG_DATA'), + 'en' => array(), + 'cs' => array(), + 'de' => array('WRONG_DATA'), + )); + } +} From 3f657bc63e6649da43d860be1848116c42214a65 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 30 May 2013 16:05:19 +0200 Subject: [PATCH 02/10] [ticket/11579] Add remaining unit tests for validate_data functions This now includes tests for usernames, passwords, emails, and jabber addresses. A few small changes had to be applied to phpbb_mock_cache and phpbb_mock_user in order to incorporate needed methods. PHPBB3-11579 --- tests/functions/fixtures/validate_data.xml | 59 ++++ tests/functions/validate_data_test.php | 350 +++++++++++++++++---- tests/mock/cache.php | 15 + tests/mock/user.php | 13 + 4 files changed, 375 insertions(+), 62 deletions(-) create mode 100644 tests/functions/fixtures/validate_data.xml diff --git a/tests/functions/fixtures/validate_data.xml b/tests/functions/fixtures/validate_data.xml new file mode 100644 index 0000000000..38ecae6ad2 --- /dev/null +++ b/tests/functions/fixtures/validate_data.xml @@ -0,0 +1,59 @@ + + + + group_name + group_desc + + foobar_group + test123 + +
+ + lang_id + lang_iso + lang_local_name + lang_english_name + + 1 + en + English + English + + + 2 + cs + Čeština + Czech + +
+ + user_id + username + username_clean + user_permissions + user_sig + user_occ + user_interests + user_email_hash + + 1 + admin + admin + + + + + 143317126117 + + + 2 + moderator + moderator + + + + + 0 + +
+
diff --git a/tests/functions/validate_data_test.php b/tests/functions/validate_data_test.php index 6a429a5529..ed91f782ba 100644 --- a/tests/functions/validate_data_test.php +++ b/tests/functions/validate_data_test.php @@ -7,23 +7,30 @@ * */ +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; 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__) . '/../mock/user.php'; class phpbb_functions_validate_data_test extends phpbb_database_test_case { - /* - * Types to test - * - username - * . - * - password - * - email - * - jabber - */ + protected $db; + protected $cache; + protected $user; public function getDataSet() { - return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/language_select.xml'); + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/validate_data.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->cache = new phpbb_mock_cache; + $this->user = new phpbb_mock_user; } /** @@ -42,22 +49,15 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case { foreach ($input as $key => $data) { - $this->assertEquals($expected[$key], validate_data(array($data), array($validate_check[$key]))); + $test = validate_data(array($data), array($validate_check[$key])); + if ($test != $expected[$key]) + { + var_dump($key, $data, $test, $expected[$key]); + } + $this->assertEquals($expected[$key], $test); } } - /* - * Types to test - * - string: - * empty + optional = true --> good - * empty + optional = false --> good (min = 0) - * 'foobar' --> good - * 'foobar' + optional = false|true + min = 2 + max = 6 --> good - * 'foobar' + " + min = 7 + max = 9 --> TOO_SHORT - * 'foobar' + " + min = 2 + max = 5 --> TOO_LONG - * '' + optional = false + min = 1 + max = 6 --> TOO_SHORT - * '' + optional = true + min = 1 + max = 6 --> good - */ public function test_validate_string() { $this->validate_data_check(array( @@ -92,16 +92,6 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case )); } - /* - * Types to test - * - num - * empty + optional = true|false --> good - * 0 --> good - * 5 + optional = false|true + min = 2 + max = 6 --> good - * 5 + optional = false|true + min = 7 + max = 10 --> TOO_SMALL - * 5 + optional = false|true + min = 2 + max = 3 --> TOO_LARGE - * 'foobar' --> should fail with WRONG_DATA_NUMERIC !!! - */ public function test_validate_num() { $this->validate_data_check(array( @@ -130,19 +120,6 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case )); } - /* - * Types to test - * - date - * . '' --> invalid - * . '' + optional = true --> good - * . 17-06-1990 --> good - * . 05-05-1990 --> good - * . 17-12-1990 --> good - * . 01-01-0000 --> good!!! - * . 17-17-1990 --> invalid - * . 00-12-1990 --> invalid - * . 01-00-1990 --> invalid - */ public function test_validate_date() { $this->validate_data_check(array( @@ -186,14 +163,6 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case )); } - /* - * Types to test - * - match - * . empty + optional = true --> good - * . empty + empty match --> good - * . 'test' + optional = true|false + match = '/[a-z]/' --> good - * . 'test123' + optional = true|false + match = '/[a-z]/' --> WRONG_DATA_MATCH - */ public function test_validate_match() { $this->validate_data_check(array( @@ -216,19 +185,11 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case )); } - /* - * Types to test - * - language_iso_name - * . empty --> WRONG_DATA - * . 'en' --> good - * . 'cs' --> good - * . 'de' --> WRONG_DATA (won't exist) - */ public function test_validate_lang_iso() { global $db; - $db = $this->new_dbal(); + $db = $this->db; $this->validate_data_check(array( 'empty' => '', @@ -249,4 +210,269 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case 'de' => array('WRONG_DATA'), )); } + + 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->validate_data_check(array( + 'foobar_allow' => 'foobar', + 'foobar_ascii' => 'foobar', + 'foobar_any' => 'f*~*^=oo_bar1', + 'foobar_alpha' => 'fo0Bar', + 'foobar_alpha_spacers' => 'Fo0-[B]_a+ R', + 'foobar_letter_num' => 'fo0Bar0', + 'foobar_letter_num_sp' => 'Fö0-[B]_a+ R', + 'foobar_quot' => '"foobar"', + 'barfoo_disallow' => 'barfoo', + 'admin_taken' => 'admin', + 'group_taken' => 'foobar_group', + ), + array( + 'foobar_allow' => array('username', 'foobar'), + 'foobar_ascii' => array('username'), + 'foobar_any' => array('username'), + 'foobar_alpha' => array('username'), + 'foobar_alpha_spacers' => array('username'), + 'foobar_letter_num' => array('username'), + 'foobar_letter_num_sp' => array('username'), + 'foobar_quot' => array('username'), + 'barfoo_disallow' => array('username'), + 'admin_taken' => array('username'), + 'group_taken' => array('username'), + ), + $expected); + } + + 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->validate_data_check(array( + 'empty' => '', + 'foobar_any' => 'foobar', + 'foobar_mixed' => 'FooBar', + 'foobar_alpha' => 'F00bar', + 'foobar_symbol' => 'fooBar123*', + ), + array( + 'empty' => array('password'), + 'foobar_any' => array('password'), + 'foobar_mixed' => array('password'), + 'foobar_alpha' => array('password'), + 'foobar_symbol' => array('password'), + ), + $expected); + } + + public function test_validate_email() + { + global $config, $db, $user; + + $config['email_check_mx'] = true; + $db = $this->db; + $user = $this->user; + $user->optionset('banned_users', array('banned@example.com')); + + $this->validate_data_check(array( + 'empty' => '', + 'allowed' => 'foobar@example.com', + 'invalid' => 'fööbar@example.com', + 'valid_complex' => "'%$~test@example.com", + 'taken' => 'admin@example.com', + 'banned' => 'banned@example.com', + 'no_mx' => 'test@wwrrrhhghgghgh.ttv', + ), + array( + 'empty' => array('email'), + 'allowed' => array('email', 'foobar@example.com'), + 'invalid' => array('email'), + 'valid_complex' => array('email'), + 'taken' => array('email'), + 'banned' => array('email'), + 'no_mx' => array('email'), + ), + array( + 'empty' => array(), + 'allowed' => array(), + 'invalid' => array('EMAIL_INVALID'), + 'valid_complex' => array(), + 'taken' => array('EMAIL_TAKEN'), + 'banned' => array('EMAIL_BANNED'), + 'no_mx' => array('DOMAIN_NO_MX_RECORD'), + )); + } + + public function test_validate_jabber() + { + $this->validate_data_check(array( + 'empty' => '', + 'no_seperator' => 'testjabber.ccc', + 'no_user' => '@jabber.ccc', + 'no_realm' => 'user@', + 'dot_realm' => 'user@.....', + '-realm' => 'user@-jabber.ccc', + 'realm-' => 'user@jabber.ccc-', + 'correct' => 'user@jabber.09A-z.org', + 'prohibited' => 'u@ser@jabber.ccc.org', + 'prohibited_char' => 'uer@jabber.ccc.org', + ), + array( + 'empty' => array('jabber'), + 'no_seperator' => array('jabber'), + 'no_user' => array('jabber'), + 'no_realm' => array('jabber'), + 'dot_realm' => array('jabber'), + '-realm' => array('jabber'), + 'realm-' => array('jabber'), + 'correct' => array('jabber'), + 'prohibited' => array('jabber'), + 'prohibited_char' => array('jabber'), + ), + array( + 'empty' => array(), + 'no_seperator' => array('WRONG_DATA'), + 'no_user' => array('WRONG_DATA'), + 'no_realm' => array('WRONG_DATA'), + 'dot_realm' => array('WRONG_DATA'), + '-realm' => array('WRONG_DATA'), + 'realm-' => array('WRONG_DATA'), + 'correct' => array(), + 'prohibited' => array('WRONG_DATA'), + 'prohibited_char' => array('WRONG_DATA'), + )); + } } diff --git a/tests/mock/cache.php b/tests/mock/cache.php index aa0db5ab20..acf4288319 100644 --- a/tests/mock/cache.php +++ b/tests/mock/cache.php @@ -74,6 +74,21 @@ class phpbb_mock_cache ); } + /** + * Obtain disallowed usernames. Input data via standard put method. + */ + public function obtain_disallowed_usernames() + { + if (($usernames = $this->get('_disallowed_usernames')) !== false) + { + return $usernames; + } + else + { + return array(); + } + } + public function set_bots($bots) { $this->data['_bots'] = $bots; diff --git a/tests/mock/user.php b/tests/mock/user.php index ec14ce430e..bd547b3973 100644 --- a/tests/mock/user.php +++ b/tests/mock/user.php @@ -33,4 +33,17 @@ class phpbb_mock_user { $this->options[$item] = $value; } + + public function check_ban($user_id = false, $user_ips = false, $user_email = false, $return = false) + { + $banned_users = $this->optionget('banned_users'); + foreach ($banned_users as $banned) + { + if ($banned == $user_id || $banned == $user_ips || $banned == $user_email) + { + return true; + } + } + return false; + } } From 6d5da402ecfe686a918608875eda8d0d817d4c07 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 30 May 2013 16:09:06 +0200 Subject: [PATCH 03/10] [ticket/11579] Remove unnecessary globals from validate_password() The globals $db and $user are not used in that function. PHPBB3-11579 --- phpBB/includes/functions_user.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index 2b26c6787c..ea8b0a4640 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -1554,7 +1554,7 @@ function validate_username($username, $allowed_username = false) */ function validate_password($password) { - global $config, $db, $user; + global $config; if ($password === '' || $config['pass_complex'] === 'PASS_TYPE_ANY') { From 33a0859f4ac3454c12dda651f708e16fc6c45adb Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 30 May 2013 20:34:21 +0200 Subject: [PATCH 04/10] [ticket/11579] Move tests into seperate files depending on needed fixture PHPBB3-11579 --- tests/functions/common_validate_data.php | 31 ++ tests/functions/fixtures/validate_email.xml | 23 + ...alidate_data.xml => validate_username.xml} | 21 - tests/functions/validate_data_simple_test.php | 252 +++++++++ tests/functions/validate_data_test.php | 478 ------------------ tests/functions/validate_email_test.php | 72 +++ tests/functions/validate_lang_iso_test.php | 56 ++ tests/functions/validate_username_test.php | 160 ++++++ 8 files changed, 594 insertions(+), 499 deletions(-) create mode 100644 tests/functions/common_validate_data.php create mode 100644 tests/functions/fixtures/validate_email.xml rename tests/functions/fixtures/{validate_data.xml => validate_username.xml} (63%) create mode 100644 tests/functions/validate_data_simple_test.php delete mode 100644 tests/functions/validate_data_test.php create mode 100644 tests/functions/validate_email_test.php create mode 100644 tests/functions/validate_lang_iso_test.php create mode 100644 tests/functions/validate_username_test.php diff --git a/tests/functions/common_validate_data.php b/tests/functions/common_validate_data.php new file mode 100644 index 0000000000..64c9499ac3 --- /dev/null +++ b/tests/functions/common_validate_data.php @@ -0,0 +1,31 @@ + array('string')) + * @param array $expected Array containing the expected results. Either + * an array containing the error message or the an empty + * array if input is correct + */ + public function validate_data_check($input, $validate_check, $expected) + { + foreach ($input as $key => $data) + { + $this->assertEquals($expected[$key], validate_data(array($data), array($validate_check[$key]))); + } + } +} 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 @@ + + + + user_id + username + username_clean + user_permissions + user_sig + user_occ + user_interests + user_email_hash + + 1 + admin + admin + + + + + 143317126117 + +
+
diff --git a/tests/functions/fixtures/validate_data.xml b/tests/functions/fixtures/validate_username.xml similarity index 63% rename from tests/functions/fixtures/validate_data.xml rename to tests/functions/fixtures/validate_username.xml index 38ecae6ad2..fbe398469c 100644 --- a/tests/functions/fixtures/validate_data.xml +++ b/tests/functions/fixtures/validate_username.xml @@ -8,24 +8,6 @@ test123 - - lang_id - lang_iso - lang_local_name - lang_english_name - - 1 - en - English - English - - - 2 - cs - Čeština - Czech - -
user_idusername @@ -34,7 +16,6 @@ user_siguser_occuser_interests - user_email_hash 1 admin @@ -43,7 +24,6 @@ - 143317126117 2 @@ -53,7 +33,6 @@ - 0
diff --git a/tests/functions/validate_data_simple_test.php b/tests/functions/validate_data_simple_test.php new file mode 100644 index 0000000000..002b1f2298 --- /dev/null +++ b/tests/functions/validate_data_simple_test.php @@ -0,0 +1,252 @@ +common = new phpbb_functions_common_validate_data; + } + + public function test_validate_string() + { + $this->common->validate_data_check(array( + 'empty_opt' => '', + 'empty' => '', + 'foo' => 'foobar', + 'foo_minmax_correct' => 'foobar', + 'foo_minmax_short' => 'foobar', + 'foo_minmax_long' => 'foobar', + 'empty_short' => '', + 'empty_length_opt' => '', + ), + array( + 'empty_opt' => array('string', true), + 'empty' => array('string'), + 'foo' => array('string'), + 'foo_minmax_correct' => array('string', false, 2, 6), + 'foo_minmax_short' => array('string', false, 7, 9), + 'foo_minmax_long' => array('string', false, 2, 5), + 'empty_short' => array('string', false, 1, 6), + 'empty_length_opt' => array('string', true, 1, 6), + ), + array( + 'empty_opt' => array(), + 'empty' => array(), + 'foo' => array(), + 'foo_minmax_correct' => array(), + 'foo_minmax_short' => array('TOO_SHORT'), + 'foo_minmax_long' => array('TOO_LONG'), + 'empty_short' => array('TOO_SHORT'), + 'empty_length_opt' => array(), + )); + } + + public function test_validate_num() + { + $this->common->validate_data_check(array( + 'empty' => '', + 'zero' => 0, + 'five_minmax_correct' => 5, + 'five_minmax_short' => 5, + 'five_minmax_long' => 5, + 'string' => 'foobar', + ), + array( + 'empty' => array('num'), + 'zero' => array('num'), + 'five_minmax_correct' => array('num', false, 2, 6), + 'five_minmax_short' => array('num', false, 7, 10), + 'five_minmax_long' => array('num', false, 2, 3), + 'string' => array('num'), + ), + array( + 'empty' => array(), + 'zero' => array(), + 'five_minmax_correct' => array(), + 'five_minmax_short' => array('TOO_SMALL'), + 'five_minmax_long' => array('TOO_LARGE'), + 'string' => array(), + )); + } + + public function test_validate_date() + { + $this->common->validate_data_check(array( + 'empty' => '', + 'empty_opt' => '', + 'double_single' => '17-06-1990', + 'single_single' => '05-05-2009', + 'double_double' => '17-12-1990', + // Currently fails + //'zero_year' => '01-01-0000', + 'month_high' => '17-17-1990', + 'month_low' => '01-00-1990', + 'day_high' => '64-01-1990', + 'day_low' => '00-12-1990', + ), + array( + 'empty' => array('date'), + 'empty_opt' => array('date', true), + 'double_single' => array('date'), + 'single_single' => array('date'), + 'double_double' => array('date'), + // Currently fails + //'zero_year' => array('date'), + 'month_high' => array('date'), + 'month_low' => array('date'), + 'day_high' => array('date'), + 'day_low' => array('date'), + ), + array( + 'empty' => array('INVALID'), + 'empty_opt' => array(), + 'double_single' => array(), + 'single_single' => array(), + 'double_double' => array(), + // Currently fails + //'zero_year' => array(), + 'month_high' => array('INVALID'), + 'month_low' => array('INVALID'), + 'day_high' => array('INVALID'), + 'day_low' => array('INVALID'), + )); + } + + public function test_validate_match() + { + $this->common->validate_data_check(array( + 'empty_opt' => '', + 'empty_empty_match' => '', + 'foobar' => 'foobar', + 'foobar_fail' => 'foobar123', + ), + array( + 'empty_opt' => array('match', true, '/[a-z]$/'), + 'empty_empty_match' => array('match'), + 'foobar' => array('match', false, '/[a-z]$/'), + 'foobar_fail' => array('match', false, '/[a-z]$/'), + ), + array( + 'empty_opt' => array(), + 'empty_empty_match' => array(), + 'foobar' => array(), + 'foobar_fail' => array('WRONG_DATA'), + )); + } + + 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->common->validate_data_check(array( + 'empty' => '', + 'foobar_any' => 'foobar', + 'foobar_mixed' => 'FooBar', + 'foobar_alpha' => 'F00bar', + 'foobar_symbol' => 'fooBar123*', + ), + array( + 'empty' => array('password'), + 'foobar_any' => array('password'), + 'foobar_mixed' => array('password'), + 'foobar_alpha' => array('password'), + 'foobar_symbol' => array('password'), + ), + $expected); + } + + public function test_validate_jabber() + { + $this->common->validate_data_check(array( + 'empty' => '', + 'no_seperator' => 'testjabber.ccc', + 'no_user' => '@jabber.ccc', + 'no_realm' => 'user@', + 'dot_realm' => 'user@.....', + '-realm' => 'user@-jabber.ccc', + 'realm-' => 'user@jabber.ccc-', + 'correct' => 'user@jabber.09A-z.org', + 'prohibited' => 'u@ser@jabber.ccc.org', + 'prohibited_char' => 'uer@jabber.ccc.org', + ), + array( + 'empty' => array('jabber'), + 'no_seperator' => array('jabber'), + 'no_user' => array('jabber'), + 'no_realm' => array('jabber'), + 'dot_realm' => array('jabber'), + '-realm' => array('jabber'), + 'realm-' => array('jabber'), + 'correct' => array('jabber'), + 'prohibited' => array('jabber'), + 'prohibited_char' => array('jabber'), + ), + array( + 'empty' => array(), + 'no_seperator' => array('WRONG_DATA'), + 'no_user' => array('WRONG_DATA'), + 'no_realm' => array('WRONG_DATA'), + 'dot_realm' => array('WRONG_DATA'), + '-realm' => array('WRONG_DATA'), + 'realm-' => array('WRONG_DATA'), + 'correct' => array(), + 'prohibited' => array('WRONG_DATA'), + 'prohibited_char' => array('WRONG_DATA'), + )); + } +} diff --git a/tests/functions/validate_data_test.php b/tests/functions/validate_data_test.php deleted file mode 100644 index ed91f782ba..0000000000 --- a/tests/functions/validate_data_test.php +++ /dev/null @@ -1,478 +0,0 @@ -createXMLDataSet(dirname(__FILE__) . '/fixtures/validate_data.xml'); - } - - protected function setUp() - { - parent::setUp(); - - $this->db = $this->new_dbal(); - $this->cache = new phpbb_mock_cache; - $this->user = new phpbb_mock_user; - } - - /** - * Test provided input data with supplied checks and compare to expected - * results - * - * @param array $input Input data with specific array keys that need to - * be matched by the ones in the other 2 params - * @param array $validate_check Array containing validate_data check - * settings, i.e. array('foobar' => array('string')) - * @param array $expected Array containing the expected results. Either - * an array containing the error message or the an empty - * array if input is correct - */ - public function validate_data_check($input, $validate_check, $expected) - { - foreach ($input as $key => $data) - { - $test = validate_data(array($data), array($validate_check[$key])); - if ($test != $expected[$key]) - { - var_dump($key, $data, $test, $expected[$key]); - } - $this->assertEquals($expected[$key], $test); - } - } - - public function test_validate_string() - { - $this->validate_data_check(array( - 'empty_opt' => '', - 'empty' => '', - 'foo' => 'foobar', - 'foo_minmax_correct' => 'foobar', - 'foo_minmax_short' => 'foobar', - 'foo_minmax_long' => 'foobar', - 'empty_short' => '', - 'empty_length_opt' => '', - ), - array( - 'empty_opt' => array('string', true), - 'empty' => array('string'), - 'foo' => array('string'), - 'foo_minmax_correct' => array('string', false, 2, 6), - 'foo_minmax_short' => array('string', false, 7, 9), - 'foo_minmax_long' => array('string', false, 2, 5), - 'empty_short' => array('string', false, 1, 6), - 'empty_length_opt' => array('string', true, 1, 6), - ), - array( - 'empty_opt' => array(), - 'empty' => array(), - 'foo' => array(), - 'foo_minmax_correct' => array(), - 'foo_minmax_short' => array('TOO_SHORT'), - 'foo_minmax_long' => array('TOO_LONG'), - 'empty_short' => array('TOO_SHORT'), - 'empty_length_opt' => array(), - )); - } - - public function test_validate_num() - { - $this->validate_data_check(array( - 'empty' => '', - 'zero' => 0, - 'five_minmax_correct' => 5, - 'five_minmax_short' => 5, - 'five_minmax_long' => 5, - 'string' => 'foobar', - ), - array( - 'empty' => array('num'), - 'zero' => array('num'), - 'five_minmax_correct' => array('num', false, 2, 6), - 'five_minmax_short' => array('num', false, 7, 10), - 'five_minmax_long' => array('num', false, 2, 3), - 'string' => array('num'), - ), - array( - 'empty' => array(), - 'zero' => array(), - 'five_minmax_correct' => array(), - 'five_minmax_short' => array('TOO_SMALL'), - 'five_minmax_long' => array('TOO_LARGE'), - 'string' => array(), - )); - } - - public function test_validate_date() - { - $this->validate_data_check(array( - 'empty' => '', - 'empty_opt' => '', - 'double_single' => '17-06-1990', - 'single_single' => '05-05-2009', - 'double_double' => '17-12-1990', - // Currently fails - //'zero_year' => '01-01-0000', - 'month_high' => '17-17-1990', - 'month_low' => '01-00-1990', - 'day_high' => '64-01-1990', - 'day_low' => '00-12-1990', - ), - array( - 'empty' => array('date'), - 'empty_opt' => array('date', true), - 'double_single' => array('date'), - 'single_single' => array('date'), - 'double_double' => array('date'), - // Currently fails - //'zero_year' => array('date'), - 'month_high' => array('date'), - 'month_low' => array('date'), - 'day_high' => array('date'), - 'day_low' => array('date'), - ), - array( - 'empty' => array('INVALID'), - 'empty_opt' => array(), - 'double_single' => array(), - 'single_single' => array(), - 'double_double' => array(), - // Currently fails - //'zero_year' => array(), - 'month_high' => array('INVALID'), - 'month_low' => array('INVALID'), - 'day_high' => array('INVALID'), - 'day_low' => array('INVALID'), - )); - } - - public function test_validate_match() - { - $this->validate_data_check(array( - 'empty_opt' => '', - 'empty_empty_match' => '', - 'foobar' => 'foobar', - 'foobar_fail' => 'foobar123', - ), - array( - 'empty_opt' => array('match', true, '/[a-z]$/'), - 'empty_empty_match' => array('match'), - 'foobar' => array('match', false, '/[a-z]$/'), - 'foobar_fail' => array('match', false, '/[a-z]$/'), - ), - array( - 'empty_opt' => array(), - 'empty_empty_match' => array(), - 'foobar' => array(), - 'foobar_fail' => array('WRONG_DATA'), - )); - } - - public function test_validate_lang_iso() - { - global $db; - - $db = $this->db; - - $this->validate_data_check(array( - 'empty' => '', - 'en' => 'en', - 'cs' => 'cs', - 'de' => 'de', - ), - array( - 'empty' => array('language_iso_name'), - 'en' => array('language_iso_name'), - 'cs' => array('language_iso_name'), - 'de' => array('language_iso_name'), - ), - array( - 'empty' => array('WRONG_DATA'), - 'en' => array(), - 'cs' => array(), - 'de' => array('WRONG_DATA'), - )); - } - - 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->validate_data_check(array( - 'foobar_allow' => 'foobar', - 'foobar_ascii' => 'foobar', - 'foobar_any' => 'f*~*^=oo_bar1', - 'foobar_alpha' => 'fo0Bar', - 'foobar_alpha_spacers' => 'Fo0-[B]_a+ R', - 'foobar_letter_num' => 'fo0Bar0', - 'foobar_letter_num_sp' => 'Fö0-[B]_a+ R', - 'foobar_quot' => '"foobar"', - 'barfoo_disallow' => 'barfoo', - 'admin_taken' => 'admin', - 'group_taken' => 'foobar_group', - ), - array( - 'foobar_allow' => array('username', 'foobar'), - 'foobar_ascii' => array('username'), - 'foobar_any' => array('username'), - 'foobar_alpha' => array('username'), - 'foobar_alpha_spacers' => array('username'), - 'foobar_letter_num' => array('username'), - 'foobar_letter_num_sp' => array('username'), - 'foobar_quot' => array('username'), - 'barfoo_disallow' => array('username'), - 'admin_taken' => array('username'), - 'group_taken' => array('username'), - ), - $expected); - } - - 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->validate_data_check(array( - 'empty' => '', - 'foobar_any' => 'foobar', - 'foobar_mixed' => 'FooBar', - 'foobar_alpha' => 'F00bar', - 'foobar_symbol' => 'fooBar123*', - ), - array( - 'empty' => array('password'), - 'foobar_any' => array('password'), - 'foobar_mixed' => array('password'), - 'foobar_alpha' => array('password'), - 'foobar_symbol' => array('password'), - ), - $expected); - } - - public function test_validate_email() - { - global $config, $db, $user; - - $config['email_check_mx'] = true; - $db = $this->db; - $user = $this->user; - $user->optionset('banned_users', array('banned@example.com')); - - $this->validate_data_check(array( - 'empty' => '', - 'allowed' => 'foobar@example.com', - 'invalid' => 'fööbar@example.com', - 'valid_complex' => "'%$~test@example.com", - 'taken' => 'admin@example.com', - 'banned' => 'banned@example.com', - 'no_mx' => 'test@wwrrrhhghgghgh.ttv', - ), - array( - 'empty' => array('email'), - 'allowed' => array('email', 'foobar@example.com'), - 'invalid' => array('email'), - 'valid_complex' => array('email'), - 'taken' => array('email'), - 'banned' => array('email'), - 'no_mx' => array('email'), - ), - array( - 'empty' => array(), - 'allowed' => array(), - 'invalid' => array('EMAIL_INVALID'), - 'valid_complex' => array(), - 'taken' => array('EMAIL_TAKEN'), - 'banned' => array('EMAIL_BANNED'), - 'no_mx' => array('DOMAIN_NO_MX_RECORD'), - )); - } - - public function test_validate_jabber() - { - $this->validate_data_check(array( - 'empty' => '', - 'no_seperator' => 'testjabber.ccc', - 'no_user' => '@jabber.ccc', - 'no_realm' => 'user@', - 'dot_realm' => 'user@.....', - '-realm' => 'user@-jabber.ccc', - 'realm-' => 'user@jabber.ccc-', - 'correct' => 'user@jabber.09A-z.org', - 'prohibited' => 'u@ser@jabber.ccc.org', - 'prohibited_char' => 'uer@jabber.ccc.org', - ), - array( - 'empty' => array('jabber'), - 'no_seperator' => array('jabber'), - 'no_user' => array('jabber'), - 'no_realm' => array('jabber'), - 'dot_realm' => array('jabber'), - '-realm' => array('jabber'), - 'realm-' => array('jabber'), - 'correct' => array('jabber'), - 'prohibited' => array('jabber'), - 'prohibited_char' => array('jabber'), - ), - array( - 'empty' => array(), - 'no_seperator' => array('WRONG_DATA'), - 'no_user' => array('WRONG_DATA'), - 'no_realm' => array('WRONG_DATA'), - 'dot_realm' => array('WRONG_DATA'), - '-realm' => array('WRONG_DATA'), - 'realm-' => array('WRONG_DATA'), - 'correct' => array(), - 'prohibited' => array('WRONG_DATA'), - 'prohibited_char' => array('WRONG_DATA'), - )); - } -} diff --git a/tests/functions/validate_email_test.php b/tests/functions/validate_email_test.php new file mode 100644 index 0000000000..47aa37e11f --- /dev/null +++ b/tests/functions/validate_email_test.php @@ -0,0 +1,72 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/validate_email.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->user = new phpbb_mock_user; + $this->common = new phpbb_functions_common_validate_data; + } + + public function test_validate_email() + { + global $config, $db, $user; + + $config['email_check_mx'] = true; + $db = $this->db; + $user = $this->user; + $user->optionset('banned_users', array('banned@example.com')); + + $this->common->validate_data_check(array( + 'empty' => '', + 'allowed' => 'foobar@example.com', + 'invalid' => 'fööbar@example.com', + 'valid_complex' => "'%$~test@example.com", + 'taken' => 'admin@example.com', + 'banned' => 'banned@example.com', + 'no_mx' => 'test@wwrrrhhghgghgh.ttv', + ), + array( + 'empty' => array('email'), + 'allowed' => array('email', 'foobar@example.com'), + 'invalid' => array('email'), + 'valid_complex' => array('email'), + 'taken' => array('email'), + 'banned' => array('email'), + 'no_mx' => array('email'), + ), + array( + 'empty' => array(), + 'allowed' => array(), + 'invalid' => array('EMAIL_INVALID'), + 'valid_complex' => array(), + 'taken' => array('EMAIL_TAKEN'), + 'banned' => array('EMAIL_BANNED'), + 'no_mx' => array('DOMAIN_NO_MX_RECORD'), + )); + } +} diff --git a/tests/functions/validate_lang_iso_test.php b/tests/functions/validate_lang_iso_test.php new file mode 100644 index 0000000000..b8a1827432 --- /dev/null +++ b/tests/functions/validate_lang_iso_test.php @@ -0,0 +1,56 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/language_select.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->common = new phpbb_functions_common_validate_data; + } + + public function test_validate_lang_iso() + { + global $db; + + $db = $this->db; + + $this->common->validate_data_check(array( + 'empty' => '', + 'en' => 'en', + 'cs' => 'cs', + 'de' => 'de', + ), + array( + 'empty' => array('language_iso_name'), + 'en' => array('language_iso_name'), + 'cs' => array('language_iso_name'), + 'de' => array('language_iso_name'), + ), + array( + 'empty' => array('WRONG_DATA'), + 'en' => array(), + 'cs' => array(), + 'de' => array('WRONG_DATA'), + )); + } +} diff --git a/tests/functions/validate_username_test.php b/tests/functions/validate_username_test.php new file mode 100644 index 0000000000..656248cec3 --- /dev/null +++ b/tests/functions/validate_username_test.php @@ -0,0 +1,160 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/validate_username.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->cache = new phpbb_mock_cache; + $this->common = new phpbb_functions_common_validate_data; + } + + 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->common->validate_data_check(array( + 'foobar_allow' => 'foobar', + 'foobar_ascii' => 'foobar', + 'foobar_any' => 'f*~*^=oo_bar1', + 'foobar_alpha' => 'fo0Bar', + 'foobar_alpha_spacers' => 'Fo0-[B]_a+ R', + 'foobar_letter_num' => 'fo0Bar0', + 'foobar_letter_num_sp' => 'Fö0-[B]_a+ R', + 'foobar_quot' => '"foobar"', + 'barfoo_disallow' => 'barfoo', + 'admin_taken' => 'admin', + 'group_taken' => 'foobar_group', + ), + array( + 'foobar_allow' => array('username', 'foobar'), + 'foobar_ascii' => array('username'), + 'foobar_any' => array('username'), + 'foobar_alpha' => array('username'), + 'foobar_alpha_spacers' => array('username'), + 'foobar_letter_num' => array('username'), + 'foobar_letter_num_sp' => array('username'), + 'foobar_quot' => array('username'), + 'barfoo_disallow' => array('username'), + 'admin_taken' => array('username'), + 'group_taken' => array('username'), + ), + $expected); + } +} From 3487b70cc066f090c55990a9082ed9684d438147 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 3 Jun 2013 16:06:11 +0200 Subject: [PATCH 05/10] [ticket/11579] Use test case helper class and use assert prefix for method PHPBB3-11579 --- ...date_data.php => validate_data_helper.php} | 19 ++- tests/functions/validate_data_simple_test.php | 119 +++++++++--------- tests/functions/validate_email_test.php | 26 ++-- tests/functions/validate_lang_iso_test.php | 20 +-- tests/functions/validate_username_test.php | 11 +- 5 files changed, 100 insertions(+), 95 deletions(-) rename tests/functions/{common_validate_data.php => validate_data_helper.php} (65%) diff --git a/tests/functions/common_validate_data.php b/tests/functions/validate_data_helper.php similarity index 65% rename from tests/functions/common_validate_data.php rename to tests/functions/validate_data_helper.php index 64c9499ac3..b8e8bfded3 100644 --- a/tests/functions/common_validate_data.php +++ b/tests/functions/validate_data_helper.php @@ -7,25 +7,32 @@ * */ -class phpbb_functions_common_validate_data extends phpbb_test_case +class phpbb_functions_validate_data_helper extends PHPUnit_Framework_TestCase { + 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 $expected Array containing the expected results. Either + * an array containing the error message or the an empty + * array if input is correct * @param array $input Input data with specific array keys that need to * be matched by the ones in the other 2 params * @param array $validate_check Array containing validate_data check * settings, i.e. array('foobar' => array('string')) - * @param array $expected Array containing the expected results. Either - * an array containing the error message or the an empty - * array if input is correct */ - public function validate_data_check($input, $validate_check, $expected) + public function assert_validate_data($expected, $input, $validate_check) { foreach ($input as $key => $data) { - $this->assertEquals($expected[$key], validate_data(array($data), array($validate_check[$key]))); + $this->test_case->assertEquals($expected[$key], validate_data(array($data), array($validate_check[$key]))); } } } diff --git a/tests/functions/validate_data_simple_test.php b/tests/functions/validate_data_simple_test.php index 002b1f2298..db4f218eed 100644 --- a/tests/functions/validate_data_simple_test.php +++ b/tests/functions/validate_data_simple_test.php @@ -9,22 +9,32 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; -require_once dirname(__FILE__) . '/common_validate_data.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; class phpbb_functions_validate_data_simple_test extends phpbb_test_case { - protected $common; + protected $helper; protected function setUp() { parent::setUp(); - $this->common = new phpbb_functions_common_validate_data; + $this->helper = new phpbb_functions_validate_data_helper($this); } public function test_validate_string() { - $this->common->validate_data_check(array( + $this->helper->assert_validate_data(array( + 'empty_opt' => array(), + 'empty' => array(), + 'foo' => array(), + 'foo_minmax_correct' => array(), + 'foo_minmax_short' => array('TOO_SHORT'), + 'foo_minmax_long' => array('TOO_LONG'), + 'empty_short' => array('TOO_SHORT'), + 'empty_length_opt' => array(), + ), + array( 'empty_opt' => '', 'empty' => '', 'foo' => 'foobar', @@ -43,22 +53,20 @@ class phpbb_functions_validate_data_simple_test extends phpbb_test_case 'foo_minmax_long' => array('string', false, 2, 5), 'empty_short' => array('string', false, 1, 6), 'empty_length_opt' => array('string', true, 1, 6), - ), - array( - 'empty_opt' => array(), - 'empty' => array(), - 'foo' => array(), - 'foo_minmax_correct' => array(), - 'foo_minmax_short' => array('TOO_SHORT'), - 'foo_minmax_long' => array('TOO_LONG'), - 'empty_short' => array('TOO_SHORT'), - 'empty_length_opt' => array(), )); } public function test_validate_num() { - $this->common->validate_data_check(array( + $this->helper->assert_validate_data(array( + 'empty' => array(), + 'zero' => array(), + 'five_minmax_correct' => array(), + 'five_minmax_short' => array('TOO_SMALL'), + 'five_minmax_long' => array('TOO_LARGE'), + 'string' => array(), + ), + array( 'empty' => '', 'zero' => 0, 'five_minmax_correct' => 5, @@ -73,20 +81,25 @@ class phpbb_functions_validate_data_simple_test extends phpbb_test_case 'five_minmax_short' => array('num', false, 7, 10), 'five_minmax_long' => array('num', false, 2, 3), 'string' => array('num'), - ), - array( - 'empty' => array(), - 'zero' => array(), - 'five_minmax_correct' => array(), - 'five_minmax_short' => array('TOO_SMALL'), - 'five_minmax_long' => array('TOO_LARGE'), - 'string' => array(), )); } public function test_validate_date() { - $this->common->validate_data_check(array( + $this->helper->assert_validate_data(array( + 'empty' => array('INVALID'), + 'empty_opt' => array(), + 'double_single' => array(), + 'single_single' => array(), + 'double_double' => array(), + // Currently fails + //'zero_year' => array(), + 'month_high' => array('INVALID'), + 'month_low' => array('INVALID'), + 'day_high' => array('INVALID'), + 'day_low' => array('INVALID'), + ), + array( 'empty' => '', 'empty_opt' => '', 'double_single' => '17-06-1990', @@ -111,25 +124,18 @@ class phpbb_functions_validate_data_simple_test extends phpbb_test_case 'month_low' => array('date'), 'day_high' => array('date'), 'day_low' => array('date'), - ), - array( - 'empty' => array('INVALID'), - 'empty_opt' => array(), - 'double_single' => array(), - 'single_single' => array(), - 'double_double' => array(), - // Currently fails - //'zero_year' => array(), - 'month_high' => array('INVALID'), - 'month_low' => array('INVALID'), - 'day_high' => array('INVALID'), - 'day_low' => array('INVALID'), )); } public function test_validate_match() { - $this->common->validate_data_check(array( + $this->helper->assert_validate_data(array( + 'empty_opt' => array(), + 'empty_empty_match' => array(), + 'foobar' => array(), + 'foobar_fail' => array('WRONG_DATA'), + ), + array( 'empty_opt' => '', 'empty_empty_match' => '', 'foobar' => 'foobar', @@ -140,12 +146,6 @@ class phpbb_functions_validate_data_simple_test extends phpbb_test_case 'empty_empty_match' => array('match'), 'foobar' => array('match', false, '/[a-z]$/'), 'foobar_fail' => array('match', false, '/[a-z]$/'), - ), - array( - 'empty_opt' => array(), - 'empty_empty_match' => array(), - 'foobar' => array(), - 'foobar_fail' => array('WRONG_DATA'), )); } @@ -193,7 +193,7 @@ class phpbb_functions_validate_data_simple_test extends phpbb_test_case // Set complexity to mixed case letters, numbers and symbols $config['pass_complex'] = $pass_complexity; - $this->common->validate_data_check(array( + $this->helper->assert_validate_data($expected, array( 'empty' => '', 'foobar_any' => 'foobar', 'foobar_mixed' => 'FooBar', @@ -206,13 +206,24 @@ class phpbb_functions_validate_data_simple_test extends phpbb_test_case 'foobar_mixed' => array('password'), 'foobar_alpha' => array('password'), 'foobar_symbol' => array('password'), - ), - $expected); + )); } public function test_validate_jabber() { - $this->common->validate_data_check(array( + $this->helper->assert_validate_data(array( + 'empty' => array(), + 'no_seperator' => array('WRONG_DATA'), + 'no_user' => array('WRONG_DATA'), + 'no_realm' => array('WRONG_DATA'), + 'dot_realm' => array('WRONG_DATA'), + '-realm' => array('WRONG_DATA'), + 'realm-' => array('WRONG_DATA'), + 'correct' => array(), + 'prohibited' => array('WRONG_DATA'), + 'prohibited_char' => array('WRONG_DATA'), + ), + array( 'empty' => '', 'no_seperator' => 'testjabber.ccc', 'no_user' => '@jabber.ccc', @@ -235,18 +246,6 @@ class phpbb_functions_validate_data_simple_test extends phpbb_test_case 'correct' => array('jabber'), 'prohibited' => array('jabber'), 'prohibited_char' => array('jabber'), - ), - array( - 'empty' => array(), - 'no_seperator' => array('WRONG_DATA'), - 'no_user' => array('WRONG_DATA'), - 'no_realm' => array('WRONG_DATA'), - 'dot_realm' => array('WRONG_DATA'), - '-realm' => array('WRONG_DATA'), - 'realm-' => array('WRONG_DATA'), - 'correct' => array(), - 'prohibited' => array('WRONG_DATA'), - 'prohibited_char' => array('WRONG_DATA'), )); } } diff --git a/tests/functions/validate_email_test.php b/tests/functions/validate_email_test.php index 47aa37e11f..2e81d3277e 100644 --- a/tests/functions/validate_email_test.php +++ b/tests/functions/validate_email_test.php @@ -10,13 +10,13 @@ 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__) . '/common_validate_data.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 $common; + protected $helper; public function getDataSet() { @@ -29,7 +29,7 @@ class phpbb_functions_validate_email_test extends phpbb_database_test_case $this->db = $this->new_dbal(); $this->user = new phpbb_mock_user; - $this->common = new phpbb_functions_common_validate_data; + $this->helper = new phpbb_functions_validate_data_helper($this); } public function test_validate_email() @@ -41,7 +41,16 @@ class phpbb_functions_validate_email_test extends phpbb_database_test_case $user = $this->user; $user->optionset('banned_users', array('banned@example.com')); - $this->common->validate_data_check(array( + $this->helper->assert_validate_data(array( + 'empty' => array(), + 'allowed' => array(), + 'invalid' => array('EMAIL_INVALID'), + 'valid_complex' => array(), + 'taken' => array('EMAIL_TAKEN'), + 'banned' => array('EMAIL_BANNED'), + 'no_mx' => array('DOMAIN_NO_MX_RECORD'), + ), + array( 'empty' => '', 'allowed' => 'foobar@example.com', 'invalid' => 'fööbar@example.com', @@ -58,15 +67,6 @@ class phpbb_functions_validate_email_test extends phpbb_database_test_case 'taken' => array('email'), 'banned' => array('email'), 'no_mx' => array('email'), - ), - array( - 'empty' => array(), - 'allowed' => array(), - 'invalid' => array('EMAIL_INVALID'), - 'valid_complex' => array(), - 'taken' => array('EMAIL_TAKEN'), - 'banned' => array('EMAIL_BANNED'), - 'no_mx' => array('DOMAIN_NO_MX_RECORD'), )); } } diff --git a/tests/functions/validate_lang_iso_test.php b/tests/functions/validate_lang_iso_test.php index b8a1827432..baf75108b7 100644 --- a/tests/functions/validate_lang_iso_test.php +++ b/tests/functions/validate_lang_iso_test.php @@ -8,12 +8,12 @@ */ require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; -require_once dirname(__FILE__) . '/common_validate_data.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; class phpbb_functions_validate_lang_iso_test extends phpbb_database_test_case { protected $db; - protected $common; + protected $helper; public function getDataSet() { @@ -25,7 +25,7 @@ class phpbb_functions_validate_lang_iso_test extends phpbb_database_test_case parent::setUp(); $this->db = $this->new_dbal(); - $this->common = new phpbb_functions_common_validate_data; + $this->helper = new phpbb_functions_validate_data_helper($this); } public function test_validate_lang_iso() @@ -34,7 +34,13 @@ class phpbb_functions_validate_lang_iso_test extends phpbb_database_test_case $db = $this->db; - $this->common->validate_data_check(array( + $this->helper->assert_validate_data(array( + 'empty' => array('WRONG_DATA'), + 'en' => array(), + 'cs' => array(), + 'de' => array('WRONG_DATA'), + ), + array( 'empty' => '', 'en' => 'en', 'cs' => 'cs', @@ -45,12 +51,6 @@ class phpbb_functions_validate_lang_iso_test extends phpbb_database_test_case 'en' => array('language_iso_name'), 'cs' => array('language_iso_name'), 'de' => array('language_iso_name'), - ), - array( - 'empty' => array('WRONG_DATA'), - 'en' => array(), - 'cs' => array(), - 'de' => array('WRONG_DATA'), )); } } diff --git a/tests/functions/validate_username_test.php b/tests/functions/validate_username_test.php index 656248cec3..92c5ba6ee1 100644 --- a/tests/functions/validate_username_test.php +++ b/tests/functions/validate_username_test.php @@ -10,13 +10,13 @@ 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__) . '/common_validate_data.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 $common; + protected $helper; public function getDataSet() { @@ -29,7 +29,7 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case $this->db = $this->new_dbal(); $this->cache = new phpbb_mock_cache; - $this->common = new phpbb_functions_common_validate_data; + $this->helper = new phpbb_functions_validate_data_helper($this); } public function validate_username_data() @@ -129,7 +129,7 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case $config['allow_name_chars'] = $allow_name_chars; - $this->common->validate_data_check(array( + $this->helper->assert_validate_data($expected, array( 'foobar_allow' => 'foobar', 'foobar_ascii' => 'foobar', 'foobar_any' => 'f*~*^=oo_bar1', @@ -154,7 +154,6 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case 'barfoo_disallow' => array('username'), 'admin_taken' => array('username'), 'group_taken' => array('username'), - ), - $expected); + )); } } From c2bc82ebfd54cebba03bd04dccaf5a8e317844ae Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 3 Jun 2013 16:15:08 +0200 Subject: [PATCH 06/10] [ticket/11579] Move simple tests into seperate files PHPBB3-11579 --- tests/functions/validate_data_simple_test.php | 251 ------------------ tests/functions/validate_date_test.php | 66 +++++ tests/functions/validate_jabber_test.php | 63 +++++ tests/functions/validate_match_test.php | 45 ++++ tests/functions/validate_num_test.php | 51 ++++ tests/functions/validate_password_test.php | 83 ++++++ tests/functions/validate_string_test.php | 58 ++++ 7 files changed, 366 insertions(+), 251 deletions(-) delete mode 100644 tests/functions/validate_data_simple_test.php create mode 100644 tests/functions/validate_date_test.php create mode 100644 tests/functions/validate_jabber_test.php create mode 100644 tests/functions/validate_match_test.php create mode 100644 tests/functions/validate_num_test.php create mode 100644 tests/functions/validate_password_test.php create mode 100644 tests/functions/validate_string_test.php diff --git a/tests/functions/validate_data_simple_test.php b/tests/functions/validate_data_simple_test.php deleted file mode 100644 index db4f218eed..0000000000 --- a/tests/functions/validate_data_simple_test.php +++ /dev/null @@ -1,251 +0,0 @@ -helper = new phpbb_functions_validate_data_helper($this); - } - - public function test_validate_string() - { - $this->helper->assert_validate_data(array( - 'empty_opt' => array(), - 'empty' => array(), - 'foo' => array(), - 'foo_minmax_correct' => array(), - 'foo_minmax_short' => array('TOO_SHORT'), - 'foo_minmax_long' => array('TOO_LONG'), - 'empty_short' => array('TOO_SHORT'), - 'empty_length_opt' => array(), - ), - array( - 'empty_opt' => '', - 'empty' => '', - 'foo' => 'foobar', - 'foo_minmax_correct' => 'foobar', - 'foo_minmax_short' => 'foobar', - 'foo_minmax_long' => 'foobar', - 'empty_short' => '', - 'empty_length_opt' => '', - ), - array( - 'empty_opt' => array('string', true), - 'empty' => array('string'), - 'foo' => array('string'), - 'foo_minmax_correct' => array('string', false, 2, 6), - 'foo_minmax_short' => array('string', false, 7, 9), - 'foo_minmax_long' => array('string', false, 2, 5), - 'empty_short' => array('string', false, 1, 6), - 'empty_length_opt' => array('string', true, 1, 6), - )); - } - - public function test_validate_num() - { - $this->helper->assert_validate_data(array( - 'empty' => array(), - 'zero' => array(), - 'five_minmax_correct' => array(), - 'five_minmax_short' => array('TOO_SMALL'), - 'five_minmax_long' => array('TOO_LARGE'), - 'string' => array(), - ), - array( - 'empty' => '', - 'zero' => 0, - 'five_minmax_correct' => 5, - 'five_minmax_short' => 5, - 'five_minmax_long' => 5, - 'string' => 'foobar', - ), - array( - 'empty' => array('num'), - 'zero' => array('num'), - 'five_minmax_correct' => array('num', false, 2, 6), - 'five_minmax_short' => array('num', false, 7, 10), - 'five_minmax_long' => array('num', false, 2, 3), - 'string' => array('num'), - )); - } - - public function test_validate_date() - { - $this->helper->assert_validate_data(array( - 'empty' => array('INVALID'), - 'empty_opt' => array(), - 'double_single' => array(), - 'single_single' => array(), - 'double_double' => array(), - // Currently fails - //'zero_year' => array(), - 'month_high' => array('INVALID'), - 'month_low' => array('INVALID'), - 'day_high' => array('INVALID'), - 'day_low' => array('INVALID'), - ), - array( - 'empty' => '', - 'empty_opt' => '', - 'double_single' => '17-06-1990', - 'single_single' => '05-05-2009', - 'double_double' => '17-12-1990', - // Currently fails - //'zero_year' => '01-01-0000', - 'month_high' => '17-17-1990', - 'month_low' => '01-00-1990', - 'day_high' => '64-01-1990', - 'day_low' => '00-12-1990', - ), - array( - 'empty' => array('date'), - 'empty_opt' => array('date', true), - 'double_single' => array('date'), - 'single_single' => array('date'), - 'double_double' => array('date'), - // Currently fails - //'zero_year' => array('date'), - 'month_high' => array('date'), - 'month_low' => array('date'), - 'day_high' => array('date'), - 'day_low' => array('date'), - )); - } - - public function test_validate_match() - { - $this->helper->assert_validate_data(array( - 'empty_opt' => array(), - 'empty_empty_match' => array(), - 'foobar' => array(), - 'foobar_fail' => array('WRONG_DATA'), - ), - array( - 'empty_opt' => '', - 'empty_empty_match' => '', - 'foobar' => 'foobar', - 'foobar_fail' => 'foobar123', - ), - array( - 'empty_opt' => array('match', true, '/[a-z]$/'), - 'empty_empty_match' => array('match'), - 'foobar' => array('match', false, '/[a-z]$/'), - 'foobar_fail' => array('match', false, '/[a-z]$/'), - )); - } - - 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_validate_data($expected, array( - 'empty' => '', - 'foobar_any' => 'foobar', - 'foobar_mixed' => 'FooBar', - 'foobar_alpha' => 'F00bar', - 'foobar_symbol' => 'fooBar123*', - ), - array( - 'empty' => array('password'), - 'foobar_any' => array('password'), - 'foobar_mixed' => array('password'), - 'foobar_alpha' => array('password'), - 'foobar_symbol' => array('password'), - )); - } - - public function test_validate_jabber() - { - $this->helper->assert_validate_data(array( - 'empty' => array(), - 'no_seperator' => array('WRONG_DATA'), - 'no_user' => array('WRONG_DATA'), - 'no_realm' => array('WRONG_DATA'), - 'dot_realm' => array('WRONG_DATA'), - '-realm' => array('WRONG_DATA'), - 'realm-' => array('WRONG_DATA'), - 'correct' => array(), - 'prohibited' => array('WRONG_DATA'), - 'prohibited_char' => array('WRONG_DATA'), - ), - array( - 'empty' => '', - 'no_seperator' => 'testjabber.ccc', - 'no_user' => '@jabber.ccc', - 'no_realm' => 'user@', - 'dot_realm' => 'user@.....', - '-realm' => 'user@-jabber.ccc', - 'realm-' => 'user@jabber.ccc-', - 'correct' => 'user@jabber.09A-z.org', - 'prohibited' => 'u@ser@jabber.ccc.org', - 'prohibited_char' => 'uer@jabber.ccc.org', - ), - array( - 'empty' => array('jabber'), - 'no_seperator' => array('jabber'), - 'no_user' => array('jabber'), - 'no_realm' => array('jabber'), - 'dot_realm' => array('jabber'), - '-realm' => array('jabber'), - 'realm-' => array('jabber'), - 'correct' => array('jabber'), - 'prohibited' => array('jabber'), - 'prohibited_char' => array('jabber'), - )); - } -} diff --git a/tests/functions/validate_date_test.php b/tests/functions/validate_date_test.php new file mode 100644 index 0000000000..e7a279879c --- /dev/null +++ b/tests/functions/validate_date_test.php @@ -0,0 +1,66 @@ +helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_date() + { + $this->helper->assert_validate_data(array( + 'empty' => array('INVALID'), + 'empty_opt' => array(), + 'double_single' => array(), + 'single_single' => array(), + 'double_double' => array(), + // Currently fails + //'zero_year' => array(), + 'month_high' => array('INVALID'), + 'month_low' => array('INVALID'), + 'day_high' => array('INVALID'), + 'day_low' => array('INVALID'), + ), + array( + 'empty' => '', + 'empty_opt' => '', + 'double_single' => '17-06-1990', + 'single_single' => '05-05-2009', + 'double_double' => '17-12-1990', + // Currently fails + //'zero_year' => '01-01-0000', + 'month_high' => '17-17-1990', + 'month_low' => '01-00-1990', + 'day_high' => '64-01-1990', + 'day_low' => '00-12-1990', + ), + array( + 'empty' => array('date'), + 'empty_opt' => array('date', true), + 'double_single' => array('date'), + 'single_single' => array('date'), + 'double_double' => array('date'), + // Currently fails + //'zero_year' => array('date'), + 'month_high' => array('date'), + 'month_low' => array('date'), + 'day_high' => array('date'), + 'day_low' => array('date'), + )); + } +} diff --git a/tests/functions/validate_jabber_test.php b/tests/functions/validate_jabber_test.php new file mode 100644 index 0000000000..551b243f81 --- /dev/null +++ b/tests/functions/validate_jabber_test.php @@ -0,0 +1,63 @@ +helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_jabber() + { + $this->helper->assert_validate_data(array( + 'empty' => array(), + 'no_seperator' => array('WRONG_DATA'), + 'no_user' => array('WRONG_DATA'), + 'no_realm' => array('WRONG_DATA'), + 'dot_realm' => array('WRONG_DATA'), + '-realm' => array('WRONG_DATA'), + 'realm-' => array('WRONG_DATA'), + 'correct' => array(), + 'prohibited' => array('WRONG_DATA'), + 'prohibited_char' => array('WRONG_DATA'), + ), + array( + 'empty' => '', + 'no_seperator' => 'testjabber.ccc', + 'no_user' => '@jabber.ccc', + 'no_realm' => 'user@', + 'dot_realm' => 'user@.....', + '-realm' => 'user@-jabber.ccc', + 'realm-' => 'user@jabber.ccc-', + 'correct' => 'user@jabber.09A-z.org', + 'prohibited' => 'u@ser@jabber.ccc.org', + 'prohibited_char' => 'uer@jabber.ccc.org', + ), + array( + 'empty' => array('jabber'), + 'no_seperator' => array('jabber'), + 'no_user' => array('jabber'), + 'no_realm' => array('jabber'), + 'dot_realm' => array('jabber'), + '-realm' => array('jabber'), + 'realm-' => array('jabber'), + 'correct' => array('jabber'), + 'prohibited' => array('jabber'), + 'prohibited_char' => array('jabber'), + )); + } +} diff --git a/tests/functions/validate_match_test.php b/tests/functions/validate_match_test.php new file mode 100644 index 0000000000..5d44f1e00b --- /dev/null +++ b/tests/functions/validate_match_test.php @@ -0,0 +1,45 @@ +helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_match() + { + $this->helper->assert_validate_data(array( + 'empty_opt' => array(), + 'empty_empty_match' => array(), + 'foobar' => array(), + 'foobar_fail' => array('WRONG_DATA'), + ), + array( + 'empty_opt' => '', + 'empty_empty_match' => '', + 'foobar' => 'foobar', + 'foobar_fail' => 'foobar123', + ), + array( + 'empty_opt' => array('match', true, '/[a-z]$/'), + 'empty_empty_match' => array('match'), + 'foobar' => array('match', false, '/[a-z]$/'), + 'foobar_fail' => 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..4e210ba29a --- /dev/null +++ b/tests/functions/validate_num_test.php @@ -0,0 +1,51 @@ +helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_num() + { + $this->helper->assert_validate_data(array( + 'empty' => array(), + 'zero' => array(), + 'five_minmax_correct' => array(), + 'five_minmax_short' => array('TOO_SMALL'), + 'five_minmax_long' => array('TOO_LARGE'), + 'string' => array(), + ), + array( + 'empty' => '', + 'zero' => 0, + 'five_minmax_correct' => 5, + 'five_minmax_short' => 5, + 'five_minmax_long' => 5, + 'string' => 'foobar', + ), + array( + 'empty' => array('num'), + 'zero' => array('num'), + 'five_minmax_correct' => array('num', false, 2, 6), + 'five_minmax_short' => array('num', false, 7, 10), + 'five_minmax_long' => array('num', false, 2, 3), + 'string' => array('num'), + )); + } +} diff --git a/tests/functions/validate_password_test.php b/tests/functions/validate_password_test.php new file mode 100644 index 0000000000..e8dc7e0dea --- /dev/null +++ b/tests/functions/validate_password_test.php @@ -0,0 +1,83 @@ +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_validate_data($expected, array( + 'empty' => '', + 'foobar_any' => 'foobar', + 'foobar_mixed' => 'FooBar', + 'foobar_alpha' => 'F00bar', + 'foobar_symbol' => 'fooBar123*', + ), + array( + 'empty' => array('password'), + 'foobar_any' => array('password'), + 'foobar_mixed' => array('password'), + 'foobar_alpha' => array('password'), + 'foobar_symbol' => array('password'), + )); + } +} diff --git a/tests/functions/validate_string_test.php b/tests/functions/validate_string_test.php new file mode 100644 index 0000000000..2b4f7321a6 --- /dev/null +++ b/tests/functions/validate_string_test.php @@ -0,0 +1,58 @@ +helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_string() + { + $this->helper->assert_validate_data(array( + 'empty_opt' => array(), + 'empty' => array(), + 'foo' => array(), + 'foo_minmax_correct' => array(), + 'foo_minmax_short' => array('TOO_SHORT'), + 'foo_minmax_long' => array('TOO_LONG'), + 'empty_short' => array('TOO_SHORT'), + 'empty_length_opt' => array(), + ), + array( + 'empty_opt' => '', + 'empty' => '', + 'foo' => 'foobar', + 'foo_minmax_correct' => 'foobar', + 'foo_minmax_short' => 'foobar', + 'foo_minmax_long' => 'foobar', + 'empty_short' => '', + 'empty_length_opt' => '', + ), + array( + 'empty_opt' => array('string', true), + 'empty' => array('string'), + 'foo' => array('string'), + 'foo_minmax_correct' => array('string', false, 2, 6), + 'foo_minmax_short' => array('string', false, 7, 9), + 'foo_minmax_long' => array('string', false, 2, 5), + 'empty_short' => array('string', false, 1, 6), + 'empty_length_opt' => array('string', true, 1, 6), + )); + } +} From 11678678b810c26376728166cf334550cbc30124 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 3 Jun 2013 21:30:13 +0200 Subject: [PATCH 07/10] [ticket/11579] Rework calls to validate_data_helper PHPBB3-11579 --- tests/functions/validate_data_helper.php | 18 ++--- tests/functions/validate_date_test.php | 90 +++++++++++++--------- tests/functions/validate_email_test.php | 62 ++++++++------- tests/functions/validate_jabber_test.php | 86 ++++++++++++--------- tests/functions/validate_lang_iso_test.php | 38 +++++---- tests/functions/validate_match_test.php | 38 +++++---- tests/functions/validate_num_test.php | 54 +++++++------ tests/functions/validate_password_test.php | 39 ++++++---- tests/functions/validate_string_test.php | 70 ++++++++++------- tests/functions/validate_username_test.php | 81 +++++++++++++------ 10 files changed, 344 insertions(+), 232 deletions(-) diff --git a/tests/functions/validate_data_helper.php b/tests/functions/validate_data_helper.php index b8e8bfded3..94ddf60429 100644 --- a/tests/functions/validate_data_helper.php +++ b/tests/functions/validate_data_helper.php @@ -20,19 +20,17 @@ class phpbb_functions_validate_data_helper extends PHPUnit_Framework_TestCase * Test provided input data with supplied checks and compare to expected * results * - * @param array $expected Array containing the expected results. Either - * an array containing the error message or the an empty - * array if input is correct - * @param array $input Input data with specific array keys that need to - * be matched by the ones in the other 2 params - * @param array $validate_check Array containing validate_data check - * settings, i.e. array('foobar' => array('string')) + * @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_validate_data($expected, $input, $validate_check) + public function assert_valid_data($data) { - foreach ($input as $key => $data) + foreach ($data as $key => $test) { - $this->test_case->assertEquals($expected[$key], validate_data(array($data), array($validate_check[$key]))); + $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 index e7a279879c..1dcd1361a2 100644 --- a/tests/functions/validate_date_test.php +++ b/tests/functions/validate_date_test.php @@ -23,44 +23,60 @@ class phpbb_functions_validate_date_test extends phpbb_test_case public function test_validate_date() { - $this->helper->assert_validate_data(array( - 'empty' => array('INVALID'), - 'empty_opt' => array(), - 'double_single' => array(), - 'single_single' => array(), - 'double_double' => array(), + $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(), - 'month_high' => array('INVALID'), - 'month_low' => array('INVALID'), - 'day_high' => array('INVALID'), - 'day_low' => array('INVALID'), - ), - array( - 'empty' => '', - 'empty_opt' => '', - 'double_single' => '17-06-1990', - 'single_single' => '05-05-2009', - 'double_double' => '17-12-1990', - // Currently fails - //'zero_year' => '01-01-0000', - 'month_high' => '17-17-1990', - 'month_low' => '01-00-1990', - 'day_high' => '64-01-1990', - 'day_low' => '00-12-1990', - ), - array( - 'empty' => array('date'), - 'empty_opt' => array('date', true), - 'double_single' => array('date'), - 'single_single' => array('date'), - 'double_double' => array('date'), - // Currently fails - //'zero_year' => array('date'), - 'month_high' => array('date'), - 'month_low' => array('date'), - 'day_high' => array('date'), - 'day_low' => array('date'), + /* + '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 index 2e81d3277e..93b5ba0896 100644 --- a/tests/functions/validate_email_test.php +++ b/tests/functions/validate_email_test.php @@ -41,32 +41,42 @@ class phpbb_functions_validate_email_test extends phpbb_database_test_case $user = $this->user; $user->optionset('banned_users', array('banned@example.com')); - $this->helper->assert_validate_data(array( - 'empty' => array(), - 'allowed' => array(), - 'invalid' => array('EMAIL_INVALID'), - 'valid_complex' => array(), - 'taken' => array('EMAIL_TAKEN'), - 'banned' => array('EMAIL_BANNED'), - 'no_mx' => array('DOMAIN_NO_MX_RECORD'), - ), - array( - 'empty' => '', - 'allowed' => 'foobar@example.com', - 'invalid' => 'fööbar@example.com', - 'valid_complex' => "'%$~test@example.com", - 'taken' => 'admin@example.com', - 'banned' => 'banned@example.com', - 'no_mx' => 'test@wwrrrhhghgghgh.ttv', - ), - array( - 'empty' => array('email'), - 'allowed' => array('email', 'foobar@example.com'), - 'invalid' => array('email'), - 'valid_complex' => array('email'), - 'taken' => array('email'), - 'banned' => array('email'), - 'no_mx' => array('email'), + $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'), + ), + 'no_mx' => array( + array('DOMAIN_NO_MX_RECORD'), + 'test@wwrrrhhghgghgh.ttv', + array('email'), + ), )); } } diff --git a/tests/functions/validate_jabber_test.php b/tests/functions/validate_jabber_test.php index 551b243f81..5a53c963bd 100644 --- a/tests/functions/validate_jabber_test.php +++ b/tests/functions/validate_jabber_test.php @@ -23,41 +23,57 @@ class phpbb_functions_validate_jabber_test extends phpbb_test_case public function test_validate_jabber() { - $this->helper->assert_validate_data(array( - 'empty' => array(), - 'no_seperator' => array('WRONG_DATA'), - 'no_user' => array('WRONG_DATA'), - 'no_realm' => array('WRONG_DATA'), - 'dot_realm' => array('WRONG_DATA'), - '-realm' => array('WRONG_DATA'), - 'realm-' => array('WRONG_DATA'), - 'correct' => array(), - 'prohibited' => array('WRONG_DATA'), - 'prohibited_char' => array('WRONG_DATA'), - ), - array( - 'empty' => '', - 'no_seperator' => 'testjabber.ccc', - 'no_user' => '@jabber.ccc', - 'no_realm' => 'user@', - 'dot_realm' => 'user@.....', - '-realm' => 'user@-jabber.ccc', - 'realm-' => 'user@jabber.ccc-', - 'correct' => 'user@jabber.09A-z.org', - 'prohibited' => 'u@ser@jabber.ccc.org', - 'prohibited_char' => 'uer@jabber.ccc.org', - ), - array( - 'empty' => array('jabber'), - 'no_seperator' => array('jabber'), - 'no_user' => array('jabber'), - 'no_realm' => array('jabber'), - 'dot_realm' => array('jabber'), - '-realm' => array('jabber'), - 'realm-' => array('jabber'), - 'correct' => array('jabber'), - 'prohibited' => array('jabber'), - 'prohibited_char' => array('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'), + 'uer@jabber.ccc.org', + array('jabber'), + ), )); } } diff --git a/tests/functions/validate_lang_iso_test.php b/tests/functions/validate_lang_iso_test.php index baf75108b7..c8a5b71021 100644 --- a/tests/functions/validate_lang_iso_test.php +++ b/tests/functions/validate_lang_iso_test.php @@ -34,23 +34,27 @@ class phpbb_functions_validate_lang_iso_test extends phpbb_database_test_case $db = $this->db; - $this->helper->assert_validate_data(array( - 'empty' => array('WRONG_DATA'), - 'en' => array(), - 'cs' => array(), - 'de' => array('WRONG_DATA'), - ), - array( - 'empty' => '', - 'en' => 'en', - 'cs' => 'cs', - 'de' => 'de', - ), - array( - 'empty' => array('language_iso_name'), - 'en' => array('language_iso_name'), - 'cs' => array('language_iso_name'), - 'de' => array('language_iso_name'), + $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 index 5d44f1e00b..73a363e003 100644 --- a/tests/functions/validate_match_test.php +++ b/tests/functions/validate_match_test.php @@ -23,23 +23,27 @@ class phpbb_functions_validate_match_test extends phpbb_test_case public function test_validate_match() { - $this->helper->assert_validate_data(array( - 'empty_opt' => array(), - 'empty_empty_match' => array(), - 'foobar' => array(), - 'foobar_fail' => array('WRONG_DATA'), - ), - array( - 'empty_opt' => '', - 'empty_empty_match' => '', - 'foobar' => 'foobar', - 'foobar_fail' => 'foobar123', - ), - array( - 'empty_opt' => array('match', true, '/[a-z]$/'), - 'empty_empty_match' => array('match'), - 'foobar' => array('match', false, '/[a-z]$/'), - 'foobar_fail' => array('match', false, '/[a-z]$/'), + $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 index 4e210ba29a..4deac02ebc 100644 --- a/tests/functions/validate_num_test.php +++ b/tests/functions/validate_num_test.php @@ -23,29 +23,37 @@ class phpbb_functions_validate_num_test extends phpbb_test_case public function test_validate_num() { - $this->helper->assert_validate_data(array( - 'empty' => array(), - 'zero' => array(), - 'five_minmax_correct' => array(), - 'five_minmax_short' => array('TOO_SMALL'), - 'five_minmax_long' => array('TOO_LARGE'), - 'string' => array(), - ), - array( - 'empty' => '', - 'zero' => 0, - 'five_minmax_correct' => 5, - 'five_minmax_short' => 5, - 'five_minmax_long' => 5, - 'string' => 'foobar', - ), - array( - 'empty' => array('num'), - 'zero' => array('num'), - 'five_minmax_correct' => array('num', false, 2, 6), - 'five_minmax_short' => array('num', false, 7, 10), - 'five_minmax_long' => array('num', false, 2, 3), - 'string' => array('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 index e8dc7e0dea..4639f6cc89 100644 --- a/tests/functions/validate_password_test.php +++ b/tests/functions/validate_password_test.php @@ -65,19 +65,32 @@ class phpbb_functions_validate_password_test extends phpbb_test_case // Set complexity to mixed case letters, numbers and symbols $config['pass_complex'] = $pass_complexity; - $this->helper->assert_validate_data($expected, array( - 'empty' => '', - 'foobar_any' => 'foobar', - 'foobar_mixed' => 'FooBar', - 'foobar_alpha' => 'F00bar', - 'foobar_symbol' => 'fooBar123*', - ), - array( - 'empty' => array('password'), - 'foobar_any' => array('password'), - 'foobar_mixed' => array('password'), - 'foobar_alpha' => array('password'), - 'foobar_symbol' => array('password'), + $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 index 2b4f7321a6..ab44c28541 100644 --- a/tests/functions/validate_string_test.php +++ b/tests/functions/validate_string_test.php @@ -24,35 +24,47 @@ class phpbb_functions_validate_string_test extends phpbb_test_case public function test_validate_string() { - $this->helper->assert_validate_data(array( - 'empty_opt' => array(), - 'empty' => array(), - 'foo' => array(), - 'foo_minmax_correct' => array(), - 'foo_minmax_short' => array('TOO_SHORT'), - 'foo_minmax_long' => array('TOO_LONG'), - 'empty_short' => array('TOO_SHORT'), - 'empty_length_opt' => array(), - ), - array( - 'empty_opt' => '', - 'empty' => '', - 'foo' => 'foobar', - 'foo_minmax_correct' => 'foobar', - 'foo_minmax_short' => 'foobar', - 'foo_minmax_long' => 'foobar', - 'empty_short' => '', - 'empty_length_opt' => '', - ), - array( - 'empty_opt' => array('string', true), - 'empty' => array('string'), - 'foo' => array('string'), - 'foo_minmax_correct' => array('string', false, 2, 6), - 'foo_minmax_short' => array('string', false, 7, 9), - 'foo_minmax_long' => array('string', false, 2, 5), - 'empty_short' => array('string', false, 1, 6), - 'empty_length_opt' => array('string', true, 1, 6), + $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 index 92c5ba6ee1..9adfb63812 100644 --- a/tests/functions/validate_username_test.php +++ b/tests/functions/validate_username_test.php @@ -129,31 +129,62 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case $config['allow_name_chars'] = $allow_name_chars; - $this->helper->assert_validate_data($expected, array( - 'foobar_allow' => 'foobar', - 'foobar_ascii' => 'foobar', - 'foobar_any' => 'f*~*^=oo_bar1', - 'foobar_alpha' => 'fo0Bar', - 'foobar_alpha_spacers' => 'Fo0-[B]_a+ R', - 'foobar_letter_num' => 'fo0Bar0', - 'foobar_letter_num_sp' => 'Fö0-[B]_a+ R', - 'foobar_quot' => '"foobar"', - 'barfoo_disallow' => 'barfoo', - 'admin_taken' => 'admin', - 'group_taken' => 'foobar_group', - ), - array( - 'foobar_allow' => array('username', 'foobar'), - 'foobar_ascii' => array('username'), - 'foobar_any' => array('username'), - 'foobar_alpha' => array('username'), - 'foobar_alpha_spacers' => array('username'), - 'foobar_letter_num' => array('username'), - 'foobar_letter_num_sp' => array('username'), - 'foobar_quot' => array('username'), - 'barfoo_disallow' => array('username'), - 'admin_taken' => array('username'), - 'group_taken' => array('username'), + $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'), + ), )); } } From b288bd441436c8dd3417f67013729732ca87939f Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 3 Jun 2013 21:33:02 +0200 Subject: [PATCH 08/10] [ticket/11579] Add missing commas to validate_username_test PHPBB3-11579 --- tests/functions/validate_username_test.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/functions/validate_username_test.php b/tests/functions/validate_username_test.php index 9adfb63812..0819974e54 100644 --- a/tests/functions/validate_username_test.php +++ b/tests/functions/validate_username_test.php @@ -46,7 +46,7 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case 'foobar_quot' => array('INVALID_CHARS'), 'barfoo_disallow' => array('USERNAME_DISALLOWED'), 'admin_taken' => array('USERNAME_TAKEN'), - 'group_taken' => array('USERNAME_TAKEN') + 'group_taken' => array('USERNAME_TAKEN'), )), array('USERNAME_ALPHA_ONLY', array( 'foobar_allow' => array(), @@ -59,7 +59,7 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case 'foobar_quot' => array('INVALID_CHARS'), 'barfoo_disallow' => array('USERNAME_DISALLOWED'), 'admin_taken' => array('USERNAME_TAKEN'), - 'group_taken' => array('INVALID_CHARS') + 'group_taken' => array('INVALID_CHARS'), )), array('USERNAME_ALPHA_SPACERS', array( 'foobar_allow' => array(), @@ -72,7 +72,7 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case 'foobar_quot' => array('INVALID_CHARS'), 'barfoo_disallow' => array('USERNAME_DISALLOWED'), 'admin_taken' => array('USERNAME_TAKEN'), - 'group_taken' => array('USERNAME_TAKEN') + 'group_taken' => array('USERNAME_TAKEN'), )), array('USERNAME_LETTER_NUM', array( 'foobar_allow' => array(), @@ -85,7 +85,7 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case 'foobar_quot' => array('INVALID_CHARS'), 'barfoo_disallow' => array('USERNAME_DISALLOWED'), 'admin_taken' => array('USERNAME_TAKEN'), - 'group_taken' => array('INVALID_CHARS') + 'group_taken' => array('INVALID_CHARS'), )), array('USERNAME_LETTER_NUM_SPACERS', array( 'foobar_allow' => array(), @@ -98,7 +98,7 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case 'foobar_quot' => array('INVALID_CHARS'), 'barfoo_disallow' => array('USERNAME_DISALLOWED'), 'admin_taken' => array('USERNAME_TAKEN'), - 'group_taken' => array('USERNAME_TAKEN') + 'group_taken' => array('USERNAME_TAKEN'), )), array('USERNAME_ASCII', array( 'foobar_allow' => array(), @@ -111,7 +111,7 @@ class phpbb_functions_validate_data_test extends phpbb_database_test_case 'foobar_quot' => array('INVALID_CHARS'), 'barfoo_disallow' => array('USERNAME_DISALLOWED'), 'admin_taken' => array('USERNAME_TAKEN'), - 'group_taken' => array('USERNAME_TAKEN') + 'group_taken' => array('USERNAME_TAKEN'), )), ); } From 6a77ee1f30961f6363818691f91162fa67618872 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 5 Jun 2013 17:05:10 +0200 Subject: [PATCH 09/10] [ticket/11579] Do not extend validate_data_helper PHPBB3-11579 --- tests/functions/validate_data_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functions/validate_data_helper.php b/tests/functions/validate_data_helper.php index 94ddf60429..b92a3aa5eb 100644 --- a/tests/functions/validate_data_helper.php +++ b/tests/functions/validate_data_helper.php @@ -7,7 +7,7 @@ * */ -class phpbb_functions_validate_data_helper extends PHPUnit_Framework_TestCase +class phpbb_functions_validate_data_helper { protected $test_case; From c6ba894acdfdf5b0800dc3903d01605ddd83e8e9 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 5 Jun 2013 17:36:20 +0200 Subject: [PATCH 10/10] [ticket/11579] Add method for validating emails for valid MX and mark as slow A method for setting up the prerequisities also has been added in order to reduce the amount of necessary code. PHPBB3-11579 --- tests/functions/validate_email_test.php | 32 ++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/tests/functions/validate_email_test.php b/tests/functions/validate_email_test.php index 93b5ba0896..9a6ce39251 100644 --- a/tests/functions/validate_email_test.php +++ b/tests/functions/validate_email_test.php @@ -32,14 +32,24 @@ class phpbb_functions_validate_email_test extends phpbb_database_test_case $this->helper = new phpbb_functions_validate_data_helper($this); } - public function test_validate_email() + /** + * 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'] = true; + $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( @@ -72,9 +82,25 @@ class phpbb_functions_validate_email_test extends phpbb_database_test_case '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@wwrrrhhghgghgh.ttv', + 'test@does-not-exist.phpbb.com', array('email'), ), ));