| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Wikibase\DataModel\Tests\Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PHPUnit_Framework_TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Wikibase\DataModel\Entity\ItemId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @covers Wikibase\DataModel\Entity\ItemId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @covers Wikibase\DataModel\Entity\EntityId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @group Wikibase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @group WikibaseDataModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @group EntityIdTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @licence GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Jeroen De Dauw < [email protected] > | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class ItemIdTest extends PHPUnit_Framework_TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @dataProvider idSerializationProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	public function testCanConstructId( $idSerialization ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$id = new ItemId( $idSerialization ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			strtoupper( $idSerialization ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			$id->getSerialization() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 	public function idSerializationProvider() { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 		return array( | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 			array( 'q1' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 			array( 'q100' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 			array( 'q1337' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 			array( 'q31337' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 			array( 'Q31337' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 			array( 'Q42' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 			array( 'Q2147483648' ), | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * @dataProvider invalidIdSerializationProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	public function testCannotConstructWithInvalidSerialization( $invalidSerialization ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		$this->setExpectedException( 'InvalidArgumentException' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		new ItemId( $invalidSerialization ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	public function invalidIdSerializationProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			array( 'q' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			array( 'p1' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			array( 'qq1' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			array( '1q' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			array( 'q01' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			array( 'q 1' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			array( ' q1' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			array( 'q1 ' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			array( '1' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			array( ' ' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			array( '' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			array( '0' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			array( 0 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			array( 1 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	public function testGetNumericId() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		$id = new ItemId( 'Q1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		$this->assertSame( 1, $id->getNumericId() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	public function testGetEntityType() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$id = new ItemId( 'Q1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$this->assertSame( 'item', $id->getEntityType() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	public function testSerialize() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$id = new ItemId( 'Q1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$this->assertSame( '["item","Q1"]', $id->serialize() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * @dataProvider serializationProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	public function testUnserialize( $json, $expected ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$id = new ItemId( 'Q1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$id->unserialize( $json ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$this->assertSame( $expected, $id->getSerialization() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	public function serializationProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			array( '["item","Q2"]', 'Q2' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			// All these cases are kind of an injection vector and allow constructing invalid ids. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			array( '["string","Q2"]', 'Q2' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			array( '["","string"]', 'string' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			array( '["",""]', '' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			array( '["",2]', 2 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			array( '["",null]', null ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			array( '', null ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @dataProvider numericIdProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	public function testNewFromNumber( $number ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$id = ItemId::newFromNumber( $number ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		$this->assertEquals( 'Q' . $number, $id->getSerialization() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	public function numericIdProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			array( 42 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			array( '42' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			array( 42.0 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			// Check for 32-bit integer overflow on 32-bit PHP systems. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			array( 2147483648 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 			array( '2147483648' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * @dataProvider invalidNumericIdProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	public function testNewFromNumberWithInvalidNumericId( $number ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		$this->setExpectedException( 'InvalidArgumentException' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		ItemId::newFromNumber( $number ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	public function invalidNumericIdProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			array( 'Q1' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			array( '42.1' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			array( 42.1 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			array( 2147483648.1 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |