From c8189caf7be7fe4476f039d3ce5e74743a131be1 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Wed, 31 Aug 2011 06:24:10 -0400
Subject: [PATCH] - Patch #1252486 by dixon_, skwashd, heyrocker, Rob Loach:
 low level UUID API in core.

---
 CHANGELOG.txt              |  2 ++
 modules/system/system.test | 65 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 926a3688ccf8..e0c3ee46a97b 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -6,6 +6,8 @@ Drupal 8.0, xxxx-xx-xx (development version)
       modules with similar functionality are available:
       * Blog
       * Profile
+- Universally Unique IDentifier (UUID):
+    * Support for generating and validating UUIDs.
 
 
 Drupal 7.0, 2011-01-05
diff --git a/modules/system/system.test b/modules/system/system.test
index 1ce8e6b7e0ed..846653bd8eb2 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -2450,3 +2450,68 @@ class SystemIndexPhpTest extends DrupalWebTestCase {
   }
 }
 
+/**
+ * Tests uuid.inc and related functions.
+ */
+class UuidUnitTestCase extends DrupalUnitTestCase {
+
+  /**
+   * The UUID object to be used for generating UUIDs.
+   *
+   * @var Uuid
+   */
+  protected $uuid;
+
+  public static function getInfo() {
+    return array(
+      'name' => 'UUID handling',
+      'description' => "Test the handling of Universally Unique IDentifiers (UUIDs).",
+      'group' => 'System',
+    );
+  }
+
+  public function setUp() {
+    // Initiate the generator. This will lazy-load uuid.inc.
+    $this->uuid = new Uuid();
+    parent::setUp();
+  }
+
+  /**
+   * Test generating a UUID.
+   */
+  public function testGenerateUuid() {
+    $uuid = $this->uuid->generate();
+    $this->assertTrue($this->uuid->isValid($uuid), 'UUID generation works.');
+  }
+
+  /**
+   * Test that generated UUIDs are unique.
+   */
+  public function testUuidIsUnique() {
+    $uuid1 = $this->uuid->generate();
+    $uuid2 = $this->uuid->generate();
+    $this->assertNotEqual($uuid1, $uuid2, 'Same UUID was not generated twice.');
+  }
+
+  /**
+   * Test UUID validation.
+   */
+  function testUuidValidation() {
+    // These valid UUIDs.
+    $uuid_fqdn = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
+    $uuid_min = '00000000-0000-0000-0000-000000000000';
+    $uuid_max = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
+
+    $this->assertTrue($this->uuid->isValid($uuid_fqdn), t('FQDN namespace UUID (@uuid) is valid', array('@uuid' => $uuid_fqdn)));
+    $this->assertTrue($this->uuid->isValid($uuid_min), t('Minimum UUID value (@uuid) is valid', array('@uuid' => $uuid_min)));
+    $this->assertTrue($this->uuid->isValid($uuid_max), t('Maximum UUID value (@uuid) is valid', array('@uuid' => $uuid_max)));
+
+    // These are invalid UUIDs.
+    $invalid_format = '0ab26e6b-f074-4e44-9da-601205fa0e976';
+    $invalid_length = '0ab26e6b-f074-4e44-9daf-1205fa0e9761f';
+
+    $this->assertFalse($this->uuid->isValid($invalid_format), t('@uuid is not a valid UUID', array('@uuid' => $invalid_format)));
+    $this->assertFalse($this->uuid->isValid($invalid_length), t('@uuid is not a valid UUID', array('@uuid' => $invalid_length)));
+
+  }
+}
-- 
GitLab