MediaWiki  REL1_31
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  // Bug: 44850
10  parent::tearDown();
11  }
12 
20  public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
21  $id = call_user_func( [ UIDGenerator::class, $method ] );
22  $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
23  $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
24  "UID has the right number of digits" );
25  $this->assertLessThanOrEqual( $bits, strlen( Wikimedia\base_convert( $id, 10, 2 ) ),
26  "UID has the right number of bits" );
27 
28  $ids = [];
29  for ( $i = 0; $i < 300; $i++ ) {
30  $ids[] = call_user_func( [ UIDGenerator::class, $method ] );
31  }
32 
33  $lastId = array_shift( $ids );
34 
35  $this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." );
36 
37  foreach ( $ids as $id ) {
38  // Convert string to binary and pad to full length so we can
39  // extract segments
40  $id_bin = Wikimedia\base_convert( $id, 10, 2, $bits );
41  $lastId_bin = Wikimedia\base_convert( $lastId, 10, 2, $bits );
42 
43  $timestamp_bin = substr( $id_bin, 0, $tbits );
44  $last_timestamp_bin = substr( $lastId_bin, 0, $tbits );
45 
46  $this->assertGreaterThanOrEqual(
47  $last_timestamp_bin,
48  $timestamp_bin,
49  "timestamp ($timestamp_bin) of current ID ($id_bin) >= timestamp ($last_timestamp_bin) " .
50  "of prior one ($lastId_bin)" );
51 
52  $hostbits_bin = substr( $id_bin, -$hostbits );
53  $last_hostbits_bin = substr( $lastId_bin, -$hostbits );
54 
55  if ( $hostbits ) {
56  $this->assertEquals(
57  $hostbits_bin,
58  $last_hostbits_bin,
59  "Host ID ($hostbits_bin) of current ID ($id_bin) is same as host ID ($last_hostbits_bin) " .
60  "of prior one ($lastId_bin)." );
61  }
62 
63  $lastId = $id;
64  }
65  }
66 
71  public static function provider_testTimestampedUID() {
72  return [
73  [ 'newTimestampedUID128', 39, 128, 46, 48 ],
74  [ 'newTimestampedUID128', 39, 128, 46, 48 ],
75  [ 'newTimestampedUID88', 27, 88, 46, 32 ],
76  ];
77  }
78 
82  public function testUUIDv1() {
83  $ids = [];
84  for ( $i = 0; $i < 100; $i++ ) {
86  $this->assertEquals( true,
87  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 ),
88  "UID $id has the right format" );
89  $ids[] = $id;
90 
92  $this->assertEquals( true,
93  preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
94  "UID $id has the right format" );
95 
97  $this->assertEquals( true,
98  preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
99  "UID $id has the right format" );
100  }
101 
102  $this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
103  }
104 
108  public function testUUIDv4() {
109  $ids = [];
110  for ( $i = 0; $i < 100; $i++ ) {
111  $id = UIDGenerator::newUUIDv4();
112  $ids[] = $id;
113  $this->assertEquals( true,
114  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 ),
115  "UID $id has the right format" );
116  }
117 
118  $this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' );
119  }
120 
124  public function testRawUUIDv4() {
125  for ( $i = 0; $i < 100; $i++ ) {
127  $this->assertEquals( true,
128  preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
129  "UID $id has the right format" );
130  }
131  }
132 
136  public function testRawUUIDv4QuickRand() {
137  for ( $i = 0; $i < 100; $i++ ) {
139  $this->assertEquals( true,
140  preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
141  "UID $id has the right format" );
142  }
143  }
144 
148  public function testNewSequentialID() {
149  $id1 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
150  $id2 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
151 
152  $this->assertInternalType( 'float', $id1, "ID returned as float" );
153  $this->assertInternalType( 'float', $id2, "ID returned as float" );
154  $this->assertGreaterThan( 0, $id1, "ID greater than 1" );
155  $this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
156  }
157 
161  public function testNewSequentialIDs() {
162  $ids = UIDGenerator::newSequentialPerNodeIDs( 'test', 32, 5 );
163  $lastId = null;
164  foreach ( $ids as $id ) {
165  $this->assertInternalType( 'float', $id, "ID returned as float" );
166  $this->assertGreaterThan( 0, $id, "ID greater than 1" );
167  if ( $lastId ) {
168  $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );
169  }
170  $lastId = $id;
171  }
172  }
173 }
UIDGenerator\QUICK_RAND
const QUICK_RAND
Definition: UIDGenerator.php:45
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
UIDGenerator\newUUIDv1
static newUUIDv1()
Return an RFC4122 compliant v1 UUID.
Definition: UIDGenerator.php:217
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:37
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:71
UIDGenerator\newRawUUIDv4
static newRawUUIDv4( $flags=0)
Return an RFC4122 compliant v4 UUID.
Definition: UIDGenerator.php:311
UIDGeneratorTest\testTimestampedUID
testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits)
Test that generated UIDs have the expected properties.
Definition: UIDGeneratorTest.php:20
UIDGeneratorTest\testUUIDv4
testUUIDv4()
UIDGenerator::newUUIDv4.
Definition: UIDGeneratorTest.php:108
UIDGenerator\newSequentialPerNodeID
static newSequentialPerNodeID( $bucket, $bits=48, $flags=0)
Return an ID that is sequential only for this node and bucket.
Definition: UIDGenerator.php:327
UIDGeneratorTest\testRawUUIDv4QuickRand
testRawUUIDv4QuickRand()
UIDGenerator::newRawUUIDv4.
Definition: UIDGeneratorTest.php:136
UIDGeneratorTest\testNewSequentialIDs
testNewSequentialIDs()
UIDGenerator::newSequentialPerNodeIDs.
Definition: UIDGeneratorTest.php:161
UIDGenerator\newUUIDv4
static newUUIDv4( $flags=0)
Return an RFC4122 compliant v4 UUID.
Definition: UIDGenerator.php:285
UIDGenerator\unitTestTearDown
static unitTestTearDown()
Cleanup resources when tearing down after a unit test.
Definition: UIDGenerator.php:620
UIDGenerator\newRawUUIDv1
static newRawUUIDv1()
Return an RFC4122 compliant v1 UUID.
Definition: UIDGenerator.php:232
UIDGeneratorTest\testUUIDv1
testUUIDv1()
UIDGenerator::newUUIDv1.
Definition: UIDGeneratorTest.php:82
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:22
Wikimedia
UIDGeneratorTest\testRawUUIDv4
testRawUUIDv4()
UIDGenerator::newRawUUIDv4.
Definition: UIDGeneratorTest.php:124
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:56
UIDGeneratorTest\testNewSequentialID
testNewSequentialID()
UIDGenerator::newSequentialPerNodeID.
Definition: UIDGeneratorTest.php:148
UIDGenerator\newSequentialPerNodeIDs
static newSequentialPerNodeIDs( $bucket, $bits, $count, $flags=0)
Return IDs that are sequential only for this node and bucket.
Definition: UIDGenerator.php:342
UIDGeneratorTest\tearDown
tearDown()
Definition: UIDGeneratorTest.php:7