mirror of
https://github.com/phpbb/phpbb.git
synced 2025-05-06 15:45:34 +02:00
Merge branch 'ticket/bantu/9780' into develop-olympus
* ticket/bantu/9780: [ticket/9780] Adding unit tests for gen_rand_string(). [ticket/9780] Add length check back to gen_rand_string().
This commit is contained in:
commit
de8c334aeb
@ -201,7 +201,7 @@ function set_config_count($config_name, $increment, $is_dynamic = false)
|
||||
function gen_rand_string($num_chars = 8)
|
||||
{
|
||||
// [a, z] + [0, 9] = 36
|
||||
return strtoupper(base_convert(unique_id(), 16, 36));
|
||||
return substr(strtoupper(base_convert(unique_id(), 16, 36)), 0, $num_chars);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,6 +25,7 @@ require_once 'text_processing/all_tests.php';
|
||||
require_once 'dbal/all_tests.php';
|
||||
require_once 'regex/all_tests.php';
|
||||
require_once 'network/all_tests.php';
|
||||
require_once 'random/all_tests.php';
|
||||
|
||||
// exclude the test directory from code coverage reports
|
||||
PHPUnit_Util_Filter::addDirectoryToFilter('./');
|
||||
@ -48,6 +49,7 @@ class phpbb_all_tests
|
||||
$suite->addTest(phpbb_dbal_all_tests::suite());
|
||||
$suite->addTest(phpbb_regex_all_tests::suite());
|
||||
$suite->addTest(phpbb_network_all_tests::suite());
|
||||
$suite->addTest(phpbb_random_all_tests::suite());
|
||||
|
||||
return $suite;
|
||||
}
|
||||
|
40
tests/random/all_tests.php
Normal file
40
tests/random/all_tests.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package testing
|
||||
* @copyright (c) 2010 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
*
|
||||
*/
|
||||
|
||||
if (!defined('PHPUnit_MAIN_METHOD'))
|
||||
{
|
||||
define('PHPUnit_MAIN_METHOD', 'phpbb_random_all_tests::main');
|
||||
}
|
||||
|
||||
require_once 'test_framework/framework.php';
|
||||
require_once 'PHPUnit/TextUI/TestRunner.php';
|
||||
|
||||
require_once 'random/gen_rand_string.php';
|
||||
|
||||
class phpbb_random_all_tests
|
||||
{
|
||||
public static function main()
|
||||
{
|
||||
PHPUnit_TextUI_TestRunner::run(self::suite());
|
||||
}
|
||||
|
||||
public static function suite()
|
||||
{
|
||||
$suite = new PHPUnit_Framework_TestSuite('phpBB Random Functions');
|
||||
|
||||
$suite->addTestSuite('phpbb_random_gen_rand_string_test');
|
||||
|
||||
return $suite;
|
||||
}
|
||||
}
|
||||
|
||||
if (PHPUnit_MAIN_METHOD == 'phpbb_random_all_tests::main')
|
||||
{
|
||||
phpbb_random_all_tests::main();
|
||||
}
|
63
tests/random/gen_rand_string.php
Normal file
63
tests/random/gen_rand_string.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package testing
|
||||
* @copyright (c) 2010 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
*
|
||||
*/
|
||||
|
||||
require_once 'test_framework/framework.php';
|
||||
require_once '../phpBB/includes/functions.php';
|
||||
|
||||
class phpbb_random_gen_rand_string_test extends phpbb_test_case
|
||||
{
|
||||
const TEST_COUNT = 100;
|
||||
const MIN_STRING_LENGTH = 1;
|
||||
const MAX_STRING_LENGTH = 15;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
global $config;
|
||||
|
||||
if (!is_array($config))
|
||||
{
|
||||
$config = array();
|
||||
}
|
||||
|
||||
$config['rand_seed'] = '';
|
||||
$config['rand_seed_last_update'] = time() + 600;
|
||||
}
|
||||
|
||||
public function test_gen_rand_string()
|
||||
{
|
||||
for ($tests = 0; $tests <= self::TEST_COUNT; ++$tests)
|
||||
{
|
||||
for ($num_chars = self::MIN_STRING_LENGTH; $num_chars <= self::MAX_STRING_LENGTH; ++$num_chars)
|
||||
{
|
||||
$random_string = gen_rand_string($num_chars);
|
||||
$random_string_length = strlen($random_string);
|
||||
|
||||
$this->assertTrue($random_string_length >= self::MIN_STRING_LENGTH);
|
||||
$this->assertTrue($random_string_length <= $num_chars);
|
||||
$this->assertRegExp('#^[A-Z0-9]+$#', $random_string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function test_gen_rand_string_friendly()
|
||||
{
|
||||
for ($tests = 0; $tests <= self::TEST_COUNT; ++$tests)
|
||||
{
|
||||
for ($num_chars = self::MIN_STRING_LENGTH; $num_chars <= self::MAX_STRING_LENGTH; ++$num_chars)
|
||||
{
|
||||
$random_string = gen_rand_string_friendly($num_chars);
|
||||
$random_string_length = strlen($random_string);
|
||||
|
||||
$this->assertTrue($random_string_length >= self::MIN_STRING_LENGTH);
|
||||
$this->assertTrue($random_string_length <= $num_chars);
|
||||
$this->assertRegExp('#^[A-NP-Z1-9]+$#', $random_string);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user