MDL-61038 setuplib: Fixed PECL UUID extension support

generate_uuid() has been refactored into \core\uuid::generate()
This commit is contained in:
Matteo Scaramuccia 2019-06-08 22:35:37 +02:00
parent f3507273e9
commit c7321899c9
5 changed files with 217 additions and 37 deletions

View file

@ -25,7 +25,6 @@
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for setuplib.php
*
@ -476,4 +475,65 @@ class core_setuplib_testcase extends advanced_testcase {
public function test_get_real_size($input, $expectedbytes) {
$this->assertEquals($expectedbytes, get_real_size($input));
}
/**
* Validate the given V4 UUID.
*
* @param string $value The candidate V4 UUID
* @return bool True if valid; otherwise, false.
*/
protected static function is_valid_uuid_v4($value) {
// Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx
// where x is any hexadecimal digit and Y is one of 8, 9, aA, or bB.
// First, the size is 36 (32 + 4 dashes).
if (strlen($value) != 36) {
return false;
}
// Finally, check the format.
$uuidv4pattern = '/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i';
return (preg_match($uuidv4pattern, $value) === 1);
}
/**
* Test the \core\uuid::generate_uuid_via_pecl_uuid_extension() function.
*/
public function test_core_uuid_generate_uuid_via_pecl_uuid_extension() {
if (!extension_loaded('uuid')) {
$this->markTestSkipped("PHP 'uuid' extension not loaded.");
}
if (!function_exists('uuid_time')) {
$this->markTestSkipped("PHP PECL 'uuid' extension not loaded.");
}
// The \core\uuid::generate_uuid_via_pecl_uuid_extension static method is protected. Use Reflection to call the method.
$method = new ReflectionMethod('\core\uuid', 'generate_uuid_via_pecl_uuid_extension');
$method->setAccessible(true);
$uuid = $method->invoke(null);
$this->assertTrue(self::is_valid_uuid_v4($uuid), "Invalid v4 uuid: '$uuid'");
}
/**
* Test the \core\uuid::generate_uuid_via_random_bytes() function.
*/
public function test_core_uuid_generate_uuid_via_random_bytes() {
try {
random_bytes(1);
} catch (Exception $e) {
$this->markTestSkipped('No source of entropy for random_bytes. ' . $e->getMessage());
}
// The \core\uuid::generate_uuid_via_random_bytes static method is protected. Use Reflection to call the method.
$method = new ReflectionMethod('\core\uuid', 'generate_uuid_via_random_bytes');
$method->setAccessible(true);
$uuid = $method->invoke(null);
$this->assertTrue(self::is_valid_uuid_v4($uuid), "Invalid v4 uuid: '$uuid'");
}
/**
* Test the \core\uuid::generate() function.
*/
public function test_core_uuid_generate() {
$uuid = \core\uuid::generate();
$this->assertTrue(self::is_valid_uuid_v4($uuid), "Invalid v4 UUID: '$uuid'");
}
}