1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Tests\Entity; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers Wikibase\DataModel\Entity\PropertyId |
10
|
|
|
* @covers Wikibase\DataModel\Entity\EntityId |
11
|
|
|
* |
12
|
|
|
* @group Wikibase |
13
|
|
|
* @group WikibaseDataModel |
14
|
|
|
* @group EntityIdTest |
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 array( |
35
|
|
|
array( 'p1', 'P1' ), |
36
|
|
|
array( 'p100', 'P100' ), |
37
|
|
|
array( 'p1337', 'P1337' ), |
38
|
|
|
array( 'p31337', 'P31337' ), |
39
|
|
|
array( 'P31337', 'P31337' ), |
40
|
|
|
array( 'P42', 'P42' ), |
41
|
|
|
array( ':P42', 'P42' ), |
42
|
|
|
array( 'foo:P42', 'foo:P42' ), |
43
|
|
|
array( 'foo:bar:p42', 'foo:bar:P42' ), |
44
|
|
|
array( 'P2147483647', 'P2147483647' ), |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider invalidIdSerializationProvider |
50
|
|
|
*/ |
51
|
|
|
public function testCannotConstructWithInvalidSerialization( $invalidSerialization ) { |
52
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
|
|
|
|
53
|
|
|
new PropertyId( $invalidSerialization ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function invalidIdSerializationProvider() { |
57
|
|
|
return array( |
58
|
|
|
array( "P1\n" ), |
59
|
|
|
array( 'p' ), |
60
|
|
|
array( 'q1' ), |
61
|
|
|
array( 'pp1' ), |
62
|
|
|
array( '1p' ), |
63
|
|
|
array( 'p01' ), |
64
|
|
|
array( 'p 1' ), |
65
|
|
|
array( ' p1' ), |
66
|
|
|
array( 'p1 ' ), |
67
|
|
|
array( '1' ), |
68
|
|
|
array( ' ' ), |
69
|
|
|
array( '' ), |
70
|
|
|
array( '0' ), |
71
|
|
|
array( 0 ), |
72
|
|
|
array( 1 ), |
73
|
|
|
array( 'P2147483648' ), |
74
|
|
|
array( 'P99999999999' ), |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGetNumericId() { |
79
|
|
|
$id = new PropertyId( 'P1' ); |
80
|
|
|
$this->assertSame( 1, $id->getNumericId() ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testGetEntityType() { |
84
|
|
|
$id = new PropertyId( 'P1' ); |
85
|
|
|
$this->assertSame( 'property', $id->getEntityType() ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testSerialize() { |
89
|
|
|
$id = new PropertyId( 'P1' ); |
90
|
|
|
$this->assertSame( '["property","P1"]', $id->serialize() ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @dataProvider serializationProvider |
95
|
|
|
*/ |
96
|
|
|
public function testUnserialize( $json, $expected ) { |
97
|
|
|
$id = new PropertyId( 'P1' ); |
98
|
|
|
$id->unserialize( $json ); |
99
|
|
|
$this->assertSame( $expected, $id->getSerialization() ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function serializationProvider() { |
103
|
|
|
return array( |
104
|
|
|
array( '["property","P2"]', 'P2' ), |
105
|
|
|
|
106
|
|
|
// All these cases are kind of an injection vector and allow constructing invalid ids. |
107
|
|
|
array( '["string","P2"]', 'P2' ), |
108
|
|
|
array( '["","string"]', 'string' ), |
109
|
|
|
array( '["",""]', '' ), |
110
|
|
|
array( '["",2]', 2 ), |
111
|
|
|
array( '["",null]', null ), |
112
|
|
|
array( '', null ), |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @dataProvider numericIdProvider |
118
|
|
|
*/ |
119
|
|
|
public function testNewFromNumber( $number ) { |
120
|
|
|
$id = PropertyId::newFromNumber( $number ); |
121
|
|
|
$this->assertEquals( 'P' . $number, $id->getSerialization() ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function numericIdProvider() { |
125
|
|
|
return array( |
126
|
|
|
array( 42 ), |
127
|
|
|
array( '42' ), |
128
|
|
|
array( 42.0 ), |
129
|
|
|
// Check for 32-bit integer overflow on 32-bit PHP systems. |
130
|
|
|
array( 2147483647 ), |
131
|
|
|
array( '2147483647' ), |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @dataProvider invalidNumericIdProvider |
137
|
|
|
*/ |
138
|
|
|
public function testNewFromNumberWithInvalidNumericId( $number ) { |
139
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
|
|
|
|
140
|
|
|
PropertyId::newFromNumber( $number ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function invalidNumericIdProvider() { |
144
|
|
|
return array( |
145
|
|
|
array( 'P1' ), |
146
|
|
|
array( '42.1' ), |
147
|
|
|
array( 42.1 ), |
148
|
|
|
array( 2147483648 ), |
149
|
|
|
array( '2147483648' ), |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testGetNumericIdThrowsExceptionOnForeignIds() { |
154
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
|
|
|
|
155
|
|
|
( new PropertyId( 'foo:P42' ) )->getNumericId(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.