MediaWiki  1.33.0
UIDGeneratorTest.php
Go to the documentation of this file.
1 <?php
2 
3 class UIDGeneratorTest extends PHPUnit\Framework\TestCase {
4 
5  use MediaWikiCoversValidator;
6 
7  protected function tearDown() {
8  // T46850
10  parent::tearDown();
11  }
12 
22  public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
23  $id = call_user_func( [ UIDGenerator::class, $method ] );
24  $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
25  $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
26  "UID has the right number of digits" );
27  $this->assertLessThanOrEqual( $bits, strlen( Wikimedia\base_convert( $id, 10, 2 ) ),
28  "UID has the right number of bits" );
29 
30  $ids = [];
31  for ( $i = 0; $i < 300; $i++ ) {
32  $ids[] = call_user_func( [ UIDGenerator::class, $method ] );
33  }
34 
35  $lastId = array_shift( $ids );
36 
37  $this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." );
38 
39  foreach ( $ids as $id ) {
40  // Convert string to binary and pad to full length so we can
41  // extract segments
42  $id_bin = Wikimedia\base_convert( $id, 10, 2, $bits );
43  $lastId_bin = Wikimedia\base_convert( $lastId, 10, 2, $bits );
44 
45  $timestamp_bin = substr( $id_bin, 0, $tbits );
46  $last_timestamp_bin = substr( $lastId_bin, 0, $tbits );
47 
48  $this->assertGreaterThanOrEqual(
49  $last_timestamp_bin,
50  $timestamp_bin,
51  "timestamp ($timestamp_bin) of current ID ($id_bin) >= timestamp ($last_timestamp_bin) " .
52  "of prior one ($lastId_bin)" );
53 
54  $hostbits_bin = substr( $id_bin, -$hostbits );
55  $last_hostbits_bin = substr( $lastId_bin, -$hostbits );
56 
57  if ( $hostbits ) {
58  $this->assertEquals(
59  $hostbits_bin,
60  $last_hostbits_bin,
61  "Host ID ($hostbits_bin) of current ID ($id_bin) is same as host ID ($last_hostbits_bin) " .
62  "of prior one ($lastId_bin)." );
63  }
64 
65  $lastId = $id;
66  }
67  }
68 
73  public static function provider_testTimestampedUID() {
74  return [
75  [ 'newTimestampedUID128', 39, 128, 46, 48 ],
76  [ 'newTimestampedUID128', 39, 128, 46, 48 ],
77  [ 'newTimestampedUID88', 27, 88, 46, 32 ],
78  ];
79  }
80 
85  public function testUUIDv1() {
86  $ids = [];
87  for ( $i = 0; $i < 100; $i++ ) {
89  $this->assertEquals( true,
90  preg_match( '!^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!', $id ),
91  "UID $id has the right format" );
92  $ids[] = $id;
93 
95  $this->assertEquals( true,
96  preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
97  "UID $id has the right format" );
98 
100  $this->assertEquals( true,
101  preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
102  "UID $id has the right format" );
103  }
104 
105  $this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
106  }
107 
111  public function testUUIDv4() {
112  $ids = [];
113  for ( $i = 0; $i < 100; $i++ ) {
114  $id = UIDGenerator::newUUIDv4();
115  $ids[] = $id;
116  $this->assertEquals( true,
117  preg_match( '!^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!', $id ),
118  "UID $id has the right format" );
119  }
120 
121  $this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' );
122  }
123 
127  public function testRawUUIDv4() {
128  for ( $i = 0; $i < 100; $i++ ) {
130  $this->assertEquals( true,
131  preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
132  "UID $id has the right format" );
133  }
134  }
135 
139  public function testRawUUIDv4QuickRand() {
140  for ( $i = 0; $i < 100; $i++ ) {
142  $this->assertEquals( true,
143  preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
144  "UID $id has the right format" );
145  }
146  }
147 
151  public function testNewSequentialID() {
152  $id1 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
153  $id2 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
154 
155  $this->assertInternalType( 'float', $id1, "ID returned as float" );
156  $this->assertInternalType( 'float', $id2, "ID returned as float" );
157  $this->assertGreaterThan( 0, $id1, "ID greater than 1" );
158  $this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
159  }
160 
165  public function testNewSequentialIDs() {
166  $ids = UIDGenerator::newSequentialPerNodeIDs( 'test', 32, 5 );
167  $lastId = null;
168  foreach ( $ids as $id ) {
169  $this->assertInternalType( 'float', $id, "ID returned as float" );
170  $this->assertGreaterThan( 0, $id, "ID greater than 1" );
171  if ( $lastId ) {
172  $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );
173  }
174  $lastId = $id;
175  }
176  }
177 }
UIDGenerator\QUICK_RAND
const QUICK_RAND
Definition: UIDGenerator.php:50
UIDGenerator\newUUIDv1
static newUUIDv1()
Return an RFC4122 compliant v1 UUID.
Definition: UIDGenerator.php:219
UIDGeneratorTest
Definition: UIDGeneratorTest.php:3
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
UIDGeneratorTest\provider_testTimestampedUID
static provider_testTimestampedUID()
array( method, length, bits, hostbits ) NOTE: When adding a new method name here please update the co...
Definition: UIDGeneratorTest.php:73
UIDGenerator\newRawUUIDv4
static newRawUUIDv4( $flags=0)
Return an RFC4122 compliant v4 UUID.
Definition: UIDGenerator.php:313
UIDGeneratorTest\testTimestampedUID
testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits)
Test that generated UIDs have the expected properties.
Definition: UIDGeneratorTest.php:22
UIDGeneratorTest\testUUIDv4
testUUIDv4()
UIDGenerator::newUUIDv4.
Definition: UIDGeneratorTest.php:111
UIDGenerator\newSequentialPerNodeID
static newSequentialPerNodeID( $bucket, $bits=48, $flags=0)
Return an ID that is sequential only for this node and bucket.
Definition: UIDGenerator.php:329
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
UIDGeneratorTest\testRawUUIDv4QuickRand
testRawUUIDv4QuickRand()
UIDGenerator::newRawUUIDv4.
Definition: UIDGeneratorTest.php:139
UIDGeneratorTest\testNewSequentialIDs
testNewSequentialIDs()
UIDGenerator::newSequentialPerNodeIDs UIDGenerator::getSequentialPerNodeIDs.
Definition: UIDGeneratorTest.php:165
UIDGenerator\newUUIDv4
static newUUIDv4( $flags=0)
Return an RFC4122 compliant v4 UUID.
Definition: UIDGenerator.php:287
UIDGenerator\unitTestTearDown
static unitTestTearDown()
Cleanup resources when tearing down after a unit test.
Definition: UIDGenerator.php:674
UIDGenerator\newRawUUIDv1
static newRawUUIDv1()
Return an RFC4122 compliant v1 UUID.
Definition: UIDGenerator.php:234
UIDGeneratorTest\testUUIDv1
testUUIDv1()
UIDGenerator::newUUIDv1 UIDGenerator::getUUIDv1.
Definition: UIDGeneratorTest.php:85
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Wikimedia
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
UIDGeneratorTest\testRawUUIDv4
testRawUUIDv4()
UIDGenerator::newRawUUIDv4.
Definition: UIDGeneratorTest.php:127
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
UIDGeneratorTest\testNewSequentialID
testNewSequentialID()
UIDGenerator::newSequentialPerNodeID.
Definition: UIDGeneratorTest.php:151
UIDGenerator\newSequentialPerNodeIDs
static newSequentialPerNodeIDs( $bucket, $bits, $count, $flags=0)
Return IDs that are sequential only for this node and bucket.
Definition: UIDGenerator.php:344
UIDGeneratorTest\tearDown
tearDown()
Definition: UIDGeneratorTest.php:7