|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
|
7
|
|
|
use Wikibase\DataModel\Entity\ItemIdSet; |
|
8
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
|
9
|
|
|
use Wikibase\DataModel\SiteLink; |
|
10
|
|
|
use Wikibase\DataModel\Tests\Facet\FacetContainerContractTester; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @covers Wikibase\DataModel\SiteLink |
|
14
|
|
|
* |
|
15
|
|
|
* @group Wikibase |
|
16
|
|
|
* @group WikibaseDataModel |
|
17
|
|
|
* |
|
18
|
|
|
* @licence GNU GPL v2+ |
|
19
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
20
|
|
|
* @author Michał Łazowik |
|
21
|
|
|
* @author Thiemo Mättig |
|
22
|
|
|
*/ |
|
23
|
|
|
class SiteLinkTest extends \PHPUnit_Framework_TestCase { |
|
24
|
|
|
|
|
25
|
|
|
public function testCanConstruct() { |
|
26
|
|
|
new SiteLink( 'enwiki', 'Wikidata' ); |
|
27
|
|
|
$this->assertTrue( true ); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @dataProvider siteIdProvider |
|
32
|
|
|
*/ |
|
33
|
|
|
public function testGetSiteId( $siteId ) { |
|
34
|
|
|
$siteLink = new SiteLink( $siteId, 'Wikidata' ); |
|
35
|
|
|
$this->assertEquals( $siteId, $siteLink->getSiteId() ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function siteIdProvider() { |
|
39
|
|
|
$argLists = array(); |
|
40
|
|
|
|
|
41
|
|
|
$argLists[] = array( 'enwiki' ); |
|
42
|
|
|
$argLists[] = array( 'nlwiki' ); |
|
43
|
|
|
$argLists[] = array( 'Nyan!' ); |
|
44
|
|
|
|
|
45
|
|
|
return $argLists; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @dataProvider invalidStringIdentifierProvider |
|
50
|
|
|
* @expectedException InvalidArgumentException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testCannotConstructWithNonStringSiteId( $invalidSiteId ) { |
|
53
|
|
|
new SiteLink( $invalidSiteId, 'Wikidata' ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function invalidStringIdentifierProvider() { |
|
57
|
|
|
return array( |
|
58
|
|
|
array( null ), |
|
59
|
|
|
array( true ), |
|
60
|
|
|
array( 42 ), |
|
61
|
|
|
array( '' ), |
|
62
|
|
|
array( array() ), |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @dataProvider pageNameProvider |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testGetPageName( $pageName ) { |
|
70
|
|
|
$siteLink = new SiteLink( 'enwiki', $pageName ); |
|
71
|
|
|
$this->assertEquals( $pageName, $siteLink->getPageName() ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function pageNameProvider() { |
|
75
|
|
|
$argLists = array(); |
|
76
|
|
|
|
|
77
|
|
|
$argLists[] = array( 'Wikidata' ); |
|
78
|
|
|
$argLists[] = array( 'Nyan_Cat' ); |
|
79
|
|
|
$argLists[] = array( 'NYAN DATA ALL ACROSS THE SKY ~=[,,_,,]:3' ); |
|
80
|
|
|
|
|
81
|
|
|
return $argLists; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @dataProvider invalidStringIdentifierProvider |
|
86
|
|
|
* @expectedException InvalidArgumentException |
|
87
|
|
|
*/ |
|
88
|
|
|
public function testCannotConstructWithNonStringPageName( $invalidPageName ) { |
|
89
|
|
|
new SiteLink( 'enwiki', $invalidPageName ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @dataProvider badgesProvider |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testGetBadges( $badges, $expected ) { |
|
96
|
|
|
$siteLink = new SiteLink( 'enwiki', 'Wikidata', $badges ); |
|
97
|
|
|
$this->assertEquals( $expected, $siteLink->getBadges() ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function badgesProvider() { |
|
101
|
|
|
$argLists = array(); |
|
102
|
|
|
|
|
103
|
|
|
$argLists[] = array( null, array() ); |
|
104
|
|
|
|
|
105
|
|
|
$badges = array(); |
|
106
|
|
|
$expected = array_values( $badges ); |
|
107
|
|
|
|
|
108
|
|
|
$argLists[] = array( $badges, $expected ); |
|
109
|
|
|
|
|
110
|
|
|
$badges = array( |
|
111
|
|
|
new ItemId( 'Q149' ) |
|
112
|
|
|
); |
|
113
|
|
|
$expected = array_values( $badges ); |
|
114
|
|
|
|
|
115
|
|
|
$argLists[] = array( $badges, $expected ); |
|
116
|
|
|
|
|
117
|
|
|
// removing from the middle of array |
|
118
|
|
|
$badges = array( |
|
119
|
|
|
new ItemId( 'Q36' ), |
|
120
|
|
|
new ItemId( 'Q149' ), |
|
121
|
|
|
new ItemId( 'Q7' ) |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
$key = array_search( |
|
125
|
|
|
new ItemId( 'Q149' ), |
|
126
|
|
|
$badges |
|
127
|
|
|
); |
|
128
|
|
|
unset( $badges[$key] ); |
|
129
|
|
|
|
|
130
|
|
|
$expected = array_values( $badges ); |
|
131
|
|
|
|
|
132
|
|
|
$argLists[] = array( $badges, $expected ); |
|
133
|
|
|
|
|
134
|
|
|
return $argLists; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @dataProvider invalidBadgesProvider |
|
139
|
|
|
* @expectedException InvalidArgumentException |
|
140
|
|
|
*/ |
|
141
|
|
|
public function testCannotConstructWithInvalidBadges( $invalidBadges ) { |
|
142
|
|
|
new SiteLink( 'enwiki', 'Wikidata', $invalidBadges ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function invalidBadgesProvider() { |
|
146
|
|
|
return array( |
|
147
|
|
|
// Stuff that's not an array |
|
148
|
|
|
array( true ), |
|
149
|
|
|
array( 42 ), |
|
150
|
|
|
array( 'nyan nyan' ), |
|
151
|
|
|
// Arrays with stuff that's not even an object |
|
152
|
|
|
array( array( 'nyan', 42 ) ), |
|
153
|
|
|
array( array( 'nyan', array() ) ), |
|
154
|
|
|
// Arrays with Entities that aren't Items |
|
155
|
|
|
array( array( new PropertyId( 'P2' ), new ItemId( 'Q149' ) ) ), |
|
156
|
|
|
array( array( new PropertyId( 'P2' ), new PropertyId( 'P3' ) ) ), |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @dataProvider linkProvider |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testSelfComparisonReturnsTrue( SiteLink $link ) { |
|
164
|
|
|
$this->assertTrue( $link->equals( $link ) ); |
|
165
|
|
|
|
|
166
|
|
|
$linkCopy = unserialize( serialize( $link ) ); |
|
167
|
|
|
$this->assertTrue( $link->equals( $linkCopy ) ); |
|
168
|
|
|
$this->assertTrue( $linkCopy->equals( $link ) ); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function linkProvider() { |
|
172
|
|
|
return array( |
|
173
|
|
|
array( new SiteLink( 'foo', 'Bar' ) ), |
|
174
|
|
|
array( new SiteLink( 'foo', 'Bar', array( new ItemId( 'Q42' ), new ItemId( 'Q9001' ) ) ) ), |
|
175
|
|
|
array( new SiteLink( 'foo', 'foo' ) ), |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @dataProvider nonEqualityProvider |
|
181
|
|
|
*/ |
|
182
|
|
|
public function testGivenNonEqualLinks_equalsReturnsFalse( SiteLink $linkOne, SiteLink $linkTwo ) { |
|
183
|
|
|
$this->assertFalse( $linkOne->equals( $linkTwo ) ); |
|
184
|
|
|
$this->assertFalse( $linkTwo->equals( $linkOne ) ); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function nonEqualityProvider() { |
|
188
|
|
|
return array( |
|
189
|
|
|
array( |
|
190
|
|
|
new SiteLink( 'foo', 'bar' ), |
|
191
|
|
|
new SiteLink( 'foo', 'Bar' ), |
|
192
|
|
|
), |
|
193
|
|
|
array( |
|
194
|
|
|
new SiteLink( 'foo', 'bar' ), |
|
195
|
|
|
new SiteLink( 'Foo', 'bar' ), |
|
196
|
|
|
), |
|
197
|
|
|
array( |
|
198
|
|
|
new SiteLink( 'foo', 'bar' ), |
|
199
|
|
|
new SiteLink( 'foo', 'bar', array( new ItemId( 'Q42' ) ) ), |
|
200
|
|
|
), |
|
201
|
|
|
array( |
|
202
|
|
|
new SiteLink( 'foo', 'bar', array( new ItemId( 'Q42' ) ) ), |
|
203
|
|
|
new SiteLink( 'foo', 'bar', array( new ItemId( 'Q42' ), new ItemId( 'Q9001' ) ) ), |
|
204
|
|
|
), |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function testCanConstructWithItemIdSet() { |
|
209
|
|
|
$badgesArray = array( |
|
210
|
|
|
new ItemId( 'Q36' ), |
|
211
|
|
|
new ItemId( 'Q149' ), |
|
212
|
|
|
); |
|
213
|
|
|
$badges = new ItemIdSet( $badgesArray ); |
|
214
|
|
|
|
|
215
|
|
|
$siteLink = new SiteLink( 'foo', 'bar', $badges ); |
|
216
|
|
|
|
|
217
|
|
|
$this->assertEquals( $badgesArray, $siteLink->getBadges() ); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function testGivenNonItemIdCollectionForBadges_constructorThrowsException() { |
|
221
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
|
222
|
|
|
new SiteLink( 'foo', 'bar', 42 ); |
|
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function testHasFacet() { |
|
226
|
|
|
$tester = new FacetContainerContractTester(); |
|
227
|
|
|
$siteLink = new SiteLink( 'foo', 'bar' ); |
|
228
|
|
|
|
|
229
|
|
|
$tester->testHasFacet( $siteLink ); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function testListFacets() { |
|
233
|
|
|
$tester = new FacetContainerContractTester(); |
|
234
|
|
|
$siteLink = new SiteLink( 'foo', 'bar' ); |
|
235
|
|
|
|
|
236
|
|
|
$tester->testListFacets( $siteLink ); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function testGetFacet() { |
|
240
|
|
|
$tester = new FacetContainerContractTester(); |
|
241
|
|
|
$siteLink = new SiteLink( 'foo', 'bar' ); |
|
242
|
|
|
|
|
243
|
|
|
$tester->testGetFacet( $siteLink ); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function testAddFacet() { |
|
247
|
|
|
$tester = new FacetContainerContractTester(); |
|
248
|
|
|
$siteLink = new SiteLink( 'foo', 'bar' ); |
|
249
|
|
|
|
|
250
|
|
|
$tester->testAddFacet( $siteLink ); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: