| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Wikibase\DataModel\Tests\Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PHPUnit_Framework_TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Wikibase\DataModel\Entity\PropertyId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @covers Wikibase\DataModel\Entity\PropertyId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @covers Wikibase\DataModel\Entity\EntityId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @group Wikibase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @group WikibaseDataModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @license GPL-2.0+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Jeroen De Dauw < [email protected] > | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class PropertyIdTest extends PHPUnit_Framework_TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @dataProvider idSerializationProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	public function testCanConstructId( $idSerialization, $normalizedIdSerialization ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$id = new PropertyId( $idSerialization ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			$normalizedIdSerialization, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			$id->getSerialization() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	public function idSerializationProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			[ 'p1', 'P1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			[ 'p100', 'P100' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			[ 'p1337', 'P1337' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			[ 'p31337', 'P31337' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			[ 'P31337', 'P31337' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			[ 'P42', 'P42' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 			[ ':P42', 'P42' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			[ 'foo:P42', 'foo:P42' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			[ 'foo:bar:p42', 'foo:bar:P42' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			[ 'P2147483647', 'P2147483647' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @dataProvider invalidIdSerializationProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	public function testCannotConstructWithInvalidSerialization( $invalidSerialization ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$this->setExpectedException( InvalidArgumentException::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		new PropertyId( $invalidSerialization ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	public function invalidIdSerializationProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			[ "P1\n" ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			[ 'p' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			[ 'q1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			[ 'pp1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			[ '1p' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			[ 'p01' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			[ 'p 1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			[ ' p1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			[ 'p1 ' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			[ '1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			[ ' ' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			[ '' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			[ '0' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			[ 0 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			[ 1 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			[ 'P2147483648' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			[ 'P99999999999' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	public function testGetNumericId() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$id = new PropertyId( 'P1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$this->assertSame( 1, $id->getNumericId() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	public function testGetNumericId_foreignId() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$id = new PropertyId( 'foo:P1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$this->assertSame( 1, $id->getNumericId() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	public function testGetEntityType() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$id = new PropertyId( 'P1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$this->assertSame( 'property', $id->getEntityType() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	public function testSerialize() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$id = new PropertyId( 'P1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$this->assertSame( 'P1', $id->serialize() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * @dataProvider serializationProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	public function testUnserialize( $json, $expected ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		$id = new PropertyId( 'P1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$id->unserialize( $json ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$this->assertSame( $expected, $id->getSerialization() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	public function serializationProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			[ 'P2', 'P2' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			[ '["property","P2"]', 'P2' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			// All these cases are kind of an injection vector and allow constructing invalid ids. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			[ '["string","P2"]', 'P2' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			[ '["","string"]', 'string' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 			[ '["",""]', '' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			[ '["",2]', 2 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			[ '["",null]', null ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			[ '', '' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * @dataProvider numericIdProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	public function testNewFromNumber( $number ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		$id = PropertyId::newFromNumber( $number ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		$this->assertEquals( 'P' . $number, $id->getSerialization() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	public function numericIdProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			[ 42 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			[ '42' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			[ 42.0 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			// Check for 32-bit integer overflow on 32-bit PHP systems. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			[ 2147483647 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			[ '2147483647' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * @dataProvider invalidNumericIdProvider | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  | 	public function testNewFromNumberWithInvalidNumericId( $number ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  | 		$this->setExpectedException( InvalidArgumentException::class ); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  | 		PropertyId::newFromNumber( $number ); | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	public function invalidNumericIdProvider() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 			[ 'P1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 			[ '42.1' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 			[ 42.1 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 			[ 2147483648 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 			[ '2147483648' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	public function testNewFromRepositoryAndNumber() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		$id = PropertyId::newFromRepositoryAndNumber( 'foo', 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		$this->assertSame( 'foo:P1', $id->getSerialization() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	public function testNewFromRepositoryAndNumberWithInvalidNumericId() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		$this->setExpectedException( InvalidArgumentException::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		PropertyId::newFromRepositoryAndNumber( '', 'P1' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  |