|
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
|
|
|
[ '["",2]', 2 ], |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @dataProvider invalidSerializationProvider |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testGivenInvalidSerialization_unserializeThrowsException( $serialization ) { |
|
123
|
|
|
$id = new PropertyId( 'P1' ); |
|
124
|
|
|
$this->setExpectedException( InvalidArgumentException::class ); |
|
125
|
|
|
$id->unserialize( $serialization ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function invalidSerializationProvider() { |
|
129
|
|
|
return [ |
|
130
|
|
|
[ '["",""]', '' ], |
|
131
|
|
|
[ '["",null]', null ], |
|
132
|
|
|
[ '', '' ], |
|
133
|
|
|
]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @dataProvider numericIdProvider |
|
138
|
|
|
*/ |
|
139
|
|
|
public function testNewFromNumber( $number ) { |
|
140
|
|
|
$id = PropertyId::newFromNumber( $number ); |
|
141
|
|
|
$this->assertEquals( 'P' . $number, $id->getSerialization() ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function numericIdProvider() { |
|
145
|
|
|
return [ |
|
146
|
|
|
[ 42 ], |
|
147
|
|
|
[ '42' ], |
|
148
|
|
|
[ 42.0 ], |
|
149
|
|
|
// Check for 32-bit integer overflow on 32-bit PHP systems. |
|
150
|
|
|
[ 2147483647 ], |
|
151
|
|
|
[ '2147483647' ], |
|
152
|
|
|
]; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @dataProvider invalidNumericIdProvider |
|
157
|
|
|
*/ |
|
158
|
|
|
public function testNewFromNumberWithInvalidNumericId( $number ) { |
|
159
|
|
|
$this->setExpectedException( InvalidArgumentException::class ); |
|
160
|
|
|
PropertyId::newFromNumber( $number ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function invalidNumericIdProvider() { |
|
164
|
|
|
return [ |
|
165
|
|
|
[ 'P1' ], |
|
166
|
|
|
[ '42.1' ], |
|
167
|
|
|
[ 42.1 ], |
|
168
|
|
|
[ 2147483648 ], |
|
169
|
|
|
[ '2147483648' ], |
|
170
|
|
|
]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function testNewFromRepositoryAndNumber() { |
|
174
|
|
|
$id = PropertyId::newFromRepositoryAndNumber( 'foo', 1 ); |
|
175
|
|
|
$this->assertSame( 'foo:P1', $id->getSerialization() ); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function testNewFromRepositoryAndNumberWithInvalidNumericId() { |
|
179
|
|
|
$this->setExpectedException( InvalidArgumentException::class ); |
|
180
|
|
|
PropertyId::newFromRepositoryAndNumber( '', 'P1' ); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|