diff --git a/functions.inc.php b/functions.inc.php index 7197e56f..19108c87 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -832,14 +832,13 @@ function encode_header($string, $default_charset = "utf-8") { -// -// generate_password -// Action: Generates a random password -// Call: generate_password () -// -function generate_password() { - // length of the generated password - $length = 12; +/** + * Generate a random password of $length characters. + * @param int $length (optional, default: 12) + * @return string + * + */ +function generate_password($length = 12) { // define possible characters $possible = "2345678923456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; # skip 0 and 1 to avoid confusion with O and l diff --git a/tests/GeneratePasswordTest.php b/tests/GeneratePasswordTest.php index d16e2725..42462dfb 100644 --- a/tests/GeneratePasswordTest.php +++ b/tests/GeneratePasswordTest.php @@ -11,5 +11,18 @@ class GeneratePasswordTest extends PHPUnit_Framework_TestCase { $this->assertNotEquals($one, $two); $this->assertNotEmpty($one); $this->assertNotEmpty($two); + $this->assertEquals(12, strlen($one)); + } + + public function testLength() { + $one = generate_password(1); + + $ten = generate_password(10); + + $this->assertNotEquals($one, $ten); + $this->assertNotEmpty($one); + $this->assertNotEmpty($ten); + $this->assertEquals(10, strlen($ten)); + $this->assertEquals(1, strlen($one)); } }