Issues (55)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/unit/Statement/StatementListTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Wikibase\DataModel\Tests\Statement;
4
5
use ArrayObject;
6
use DataValues\StringValue;
7
use InvalidArgumentException;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Reference;
10
use Wikibase\DataModel\ReferenceList;
11
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
12
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
13
use Wikibase\DataModel\Snak\PropertyValueSnak;
14
use Wikibase\DataModel\Snak\Snak;
15
use Wikibase\DataModel\Snak\SnakList;
16
use Wikibase\DataModel\Statement\ReferencedStatementFilter;
17
use Wikibase\DataModel\Statement\Statement;
18
use Wikibase\DataModel\Statement\StatementList;
19
20
/**
21
 * @covers \Wikibase\DataModel\Statement\StatementList
22
 *
23
 * @license GPL-2.0-or-later
24
 * @author Jeroen De Dauw < [email protected] >
25
 * @author Thiemo Kreuz
26
 */
27
class StatementListTest extends \PHPUnit\Framework\TestCase {
28
29
	/**
30
	 * @param string $propertyId
31
	 * @param string|null $guid
32
	 * @param int $rank
33
	 *
34
	 * @return Statement
35
	 */
36
	private function getStatement( $propertyId, $guid, $rank = Statement::RANK_NORMAL ) {
37
		$statement = $this->createMock( Statement::class );
38
39
		$statement->expects( $this->any() )
40
			->method( 'getGuid' )
41
			->will( $this->returnValue( $guid ) );
42
43
		$statement->expects( $this->any() )
44
			->method( 'getPropertyId' )
45
			->will( $this->returnValue( new PropertyId( $propertyId ) ) );
46
47
		$statement->expects( $this->any() )
48
			->method( 'getRank' )
49
			->will( $this->returnValue( $rank ) );
50
51
		return $statement;
52
	}
53
54
	/**
55
	 * @param string $propertyId
56
	 * @param string $stringValue
57
	 *
58
	 * @return Statement
59
	 */
60
	private function getStatementWithSnak( $propertyId, $stringValue ) {
61
		$snak = $this->newSnak( $propertyId, $stringValue );
62
		$statement = new Statement( $snak );
63
		$statement->setGuid( sha1( $snak->getHash() ) );
64
		return $statement;
65
	}
66
67
	/**
68
	 * @param string $propertyId
69
	 * @param string $stringValue
70
	 *
71
	 * @return Snak
72
	 */
73
	private function newSnak( $propertyId, $stringValue ) {
74
		return new PropertyValueSnak(
75
			new PropertyId( $propertyId ),
76
			new StringValue( $stringValue )
77
		);
78
	}
79
80
	public function testConstructorAcceptsDuplicatesWithNoGuid() {
81
		$list = new StatementList(
82
			$this->getStatement( 'P1', null ),
83
			$this->getStatement( 'P1', null )
84
		);
85
86
		$this->assertSame( 2, $list->count() );
87
	}
88
89
	public function testConstructorAcceptsDuplicatesWithSameGuid() {
90
		$list = new StatementList(
91
			$this->getStatement( 'P1', 'duplicate' ),
92
			$this->getStatement( 'P1', 'duplicate' )
93
		);
94
95
		$this->assertSame( 2, $list->count() );
96
	}
97
98
	public function testGivenNoStatements_getPropertyIdsReturnsEmptyArray() {
99
		$list = new StatementList();
100
		$this->assertSame( [], $list->getPropertyIds() );
101
	}
102
103
	public function testGivenStatements_getPropertyIdsReturnsArrayWithoutDuplicates() {
104
		$list = new StatementList(
105
			$this->getStatement( 'P1', 'kittens' ),
106
			$this->getStatement( 'P3', 'foo' ),
107
			$this->getStatement( 'P2', 'bar' ),
108
			$this->getStatement( 'P2', 'baz' ),
109
			$this->getStatement( 'P1', 'bah' )
110
		);
111
112
		$this->assertEquals(
113
			[
114
				'P1' => new PropertyId( 'P1' ),
115
				'P3' => new PropertyId( 'P3' ),
116
				'P2' => new PropertyId( 'P2' ),
117
			],
118
			$list->getPropertyIds()
119
		);
120
	}
121
122
	public function testGivenStatementsWithArrayKeys_toArrayReturnsReindexedArray() {
123
		$statement = $this->getStatement( 'P1', 'guid' );
124
		$list = new StatementList( [ 'ignore-me' => $statement ] );
125
126
		$this->assertSame( [ 0 => $statement ], $list->toArray() );
127
	}
128
129
	public function testGivenSparseArray_toArrayReturnsReindexedArray() {
130
		$statement = $this->getStatement( 'P1', 'guid' );
131
		$list = new StatementList( [ 1 => $statement ] );
132
133
		$this->assertSame( [ 0 => $statement ], $list->toArray() );
134
	}
135
136
	public function testCanIterate() {
137
		$statement = $this->getStatement( 'P1', 'kittens' );
138
		$list = new StatementList( $statement );
139
140
		foreach ( $list as $statementFormList ) {
141
			$this->assertSame( $statement, $statementFormList );
142
		}
143
	}
144
145
	public function testGetUniqueMainSnaksReturnsListWithoutDuplicates() {
146
		$list = new StatementList(
147
			$this->getStatementWithSnak( 'P1', 'foo' ),
148
			$this->getStatementWithSnak( 'P2', 'foo' ),
149
			$this->getStatementWithSnak( 'P1', 'foo' ),
150
			$this->getStatementWithSnak( 'P2', 'bar' ),
151
			$this->getStatementWithSnak( 'P1', 'bar' )
152
		);
153
154
		$this->assertEquals(
155
			[
156
				$this->getStatementWithSnak( 'P1', 'foo' ),
157
				$this->getStatementWithSnak( 'P2', 'foo' ),
158
				$this->getStatementWithSnak( 'P2', 'bar' ),
159
				$this->getStatementWithSnak( 'P1', 'bar' ),
160
			],
161
			array_values( $list->getWithUniqueMainSnaks()->toArray() )
162
		);
163
	}
164
165
	public function testGetAllSnaksReturnsAllSnaks() {
166
		$list = new StatementList(
167
			$this->getStatementWithSnak( 'P1', 'foo' ),
168
			$this->getStatementWithSnak( 'P2', 'foo' ),
169
			$this->getStatementWithSnak( 'P1', 'foo' ),
170
			$this->getStatementWithSnak( 'P2', 'bar' ),
171
			$this->getStatementWithSnak( 'P1', 'bar' )
172
		);
173
174
		$this->assertEquals(
175
			[
176
				$this->newSnak( 'P1', 'foo' ),
177
				$this->newSnak( 'P2', 'foo' ),
178
				$this->newSnak( 'P1', 'foo' ),
179
				$this->newSnak( 'P2', 'bar' ),
180
				$this->newSnak( 'P1', 'bar' ),
181
			],
182
			$list->getAllSnaks()
183
		);
184
	}
185
186
	public function testAddStatementWithOnlyMainSnak() {
187
		$list = new StatementList();
188
189
		$list->addNewStatement( $this->newSnak( 'P42', 'foo' ) );
190
191
		$this->assertEquals(
192
			new StatementList( new Statement( $this->newSnak( 'P42', 'foo' ) ) ),
193
			$list
194
		);
195
	}
196
197
	public function testAddStatementWithQualifiersAsSnakArray() {
198
		$list = new StatementList();
199
200
		$list->addNewStatement(
201
			$this->newSnak( 'P42', 'foo' ),
202
			[
203
				$this->newSnak( 'P1', 'bar' )
204
			]
205
		);
206
207
		$this->assertEquals(
208
			new StatementList(
209
				new Statement(
210
					$this->newSnak( 'P42', 'foo' ),
211
					new SnakList( [
212
						$this->newSnak( 'P1', 'bar' )
213
					] )
214
				)
215
			),
216
			$list
217
		);
218
	}
219
220
	public function testAddStatementWithQualifiersAsSnakList() {
221
		$list = new StatementList();
222
		$snakList = new SnakList( [
223
			$this->newSnak( 'P1', 'bar' )
224
		] );
225
226
		$list->addNewStatement(
227
			$this->newSnak( 'P42', 'foo' ),
228
			$snakList
229
		);
230
231
		$this->assertEquals(
232
			new StatementList( new Statement( $this->newSnak( 'P42', 'foo' ), $snakList ) ),
233
			$list
234
		);
235
	}
236
237
	public function testAddStatementWithGuid() {
238
		$list = new StatementList();
239
240
		$list->addNewStatement(
241
			$this->newSnak( 'P42', 'foo' ),
242
			null,
243
			null,
244
			'kittens'
245
		);
246
247
		$statement = new Statement( $this->newSnak( 'P42', 'foo' ) );
248
249
		$statement->setGuid( 'kittens' );
250
251
		$this->assertEquals( new StatementList( $statement ), $list );
252
	}
253
254
	public function testGivenNegativeIndex_addStatementFails() {
255
		$statement = new Statement( new PropertyNoValueSnak( 1 ) );
256
		$list = new StatementList();
257
258
		$this->expectException( InvalidArgumentException::class );
259
		$list->addStatement( $statement, -1 );
260
	}
261
262
	public function testGivenLargeIndex_addStatementAppends() {
263
		$statement = new Statement( new PropertyNoValueSnak( 1 ) );
264
		$list = new StatementList();
265
266
		$list->addStatement( $statement, 1000 );
267
		$this->assertEquals( new StatementList( $statement ), $list );
268
	}
269
270
	public function testGivenZeroIndex_addStatementPrepends() {
271
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
272
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
273
		$list = new StatementList( $statement2 );
274
275
		$list->addStatement( $statement1, 0 );
276
		$this->assertEquals( new StatementList( $statement1, $statement2 ), $list );
277
	}
278
279
	public function testGivenValidIndex_addStatementInserts() {
280
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
281
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
282
		$statement3 = new Statement( new PropertyNoValueSnak( 3 ) );
283
		$list = new StatementList( $statement1, $statement3 );
284
285
		$list->addStatement( $statement2, 1 );
286
		$this->assertEquals( new StatementList( $statement1, $statement2, $statement3 ), $list );
287
		$this->assertSame( [ 0, 1, 2 ], array_keys( $list->toArray() ), 'array keys' );
288
	}
289
290
	public function testGivenGuidOfPresentStatement_statementIsRemoved() {
291
		$statement1 = new Statement( $this->newSnak( 'P24', 'foo' ), null, null, 'foo' );
292
		$statement2 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
293
		$statement3 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
294
295
		$list = new StatementList( [ $statement1, $statement2, $statement3 ] );
296
		$list->removeStatementsWithGuid( 'foo' );
297
298
		$this->assertEquals( new StatementList( $statement2, $statement3 ), $list );
299
	}
300
301
	public function testGivenGuidOfMultipleStatements_multipleStatementsAreRemoved() {
302
		$statement1 = new Statement( $this->newSnak( 'P24', 'foo' ), null, null, 'foo' );
303
		$statement2 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
304
		$statement3 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
305
306
		$list = new StatementList( [ $statement1, $statement2, $statement3 ] );
307
		$list->removeStatementsWithGuid( 'bar' );
308
309
		$this->assertEquals( new StatementList( $statement1 ), $list );
310
	}
311
312
	public function testGivenNotPresentGuid_listIsNotModified() {
313
		$statement1 = new Statement( $this->newSnak( 'P24', 'foo' ), null, null, 'foo' );
314
		$statement2 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
315
		$statement3 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
316
317
		$list = new StatementList( [ $statement1, $statement2, $statement3 ] );
318
		$list->removeStatementsWithGuid( 'baz' );
319
320
		$this->assertEquals( new StatementList( $statement1, $statement2, $statement3 ), $list );
321
	}
322
323
	public function testGivenNullGuid_allStatementsWithNoGuidAreRemoved() {
324
		$statement1 = new Statement( $this->newSnak( 'P24', 'foo' ), null, null, 'foo' );
325
		$statement2 = new Statement( $this->newSnak( 'P32', 'bar' ) );
326
		$statement3 = new Statement( $this->newSnak( 'P32', 'bar' ) );
327
328
		$list = new StatementList( [ $statement1, $statement2, $statement3 ] );
329
		$list->removeStatementsWithGuid( null );
330
331
		$this->assertEquals( new StatementList( $statement1 ), $list );
332
	}
333
334
	public function testCanConstructWithTraversableContainingOnlyStatements() {
335
		$statementArray = [
336
			$this->getStatementWithSnak( 'P1', 'foo' ),
337
			$this->getStatementWithSnak( 'P2', 'bar' ),
338
		];
339
340
		$object = new ArrayObject( $statementArray );
341
		$list = new StatementList( $object );
342
343
		$this->assertSame(
344
			$statementArray,
345
			array_values( $list->toArray() )
346
		);
347
	}
348
349
	public function testGivenTraversableWithNonStatements_constructorThrowsException() {
350
		$traversable = new ArrayObject( [
351
			$this->getStatementWithSnak( 'P1', 'foo' ),
352
			new \stdClass(),
353
			$this->getStatementWithSnak( 'P2', 'bar' ),
354
		] );
355
356
		$this->expectException( InvalidArgumentException::class );
357
		new StatementList( $traversable );
358
	}
359
360
	public function testGivenNonTraversableOrArgList_constructorThrowsException() {
361
		$this->expectException( InvalidArgumentException::class );
362
		new StatementList( null );
363
	}
364
365
	public function testCanConstructWithStatement() {
366
		$statement = new Statement( $this->newSnak( 'P42', 'foo' ) );
367
368
		$this->assertEquals(
369
			new StatementList( [ $statement ] ),
370
			new StatementList( $statement )
371
		);
372
	}
373
374
	public function testCanConstructWithStatementArgumentList() {
375
		$statement0 = new Statement( $this->newSnak( 'P42', 'foo' ) );
376
		$statement1 = new Statement( $this->newSnak( 'P42', 'bar' ) );
377
		$statement2 = new Statement( $this->newSnak( 'P42', 'baz' ) );
378
379
		$this->assertEquals(
380
			new StatementList( [ $statement0, $statement1, $statement2 ] ),
381
			new StatementList( $statement0, $statement1, $statement2 )
382
		);
383
	}
384
385
	public function testGivenArgumentListWithNonStatement_constructorThrowsException() {
386
		$statement0 = new Statement( $this->newSnak( 'P42', 'foo' ) );
387
		$statement1 = new Statement( $this->newSnak( 'P42', 'bar' ) );
388
		$statement2 = new Statement( $this->newSnak( 'P42', 'baz' ) );
389
390
		$this->expectException( InvalidArgumentException::class );
391
		new StatementList( $statement0, $statement1, [], $statement2 );
392
	}
393
394
	public function testCountForEmptyList() {
395
		$list = new StatementList();
396
		$this->assertSame( 0, count( $list ) );
397
		$this->assertSame( 0, $list->count() );
398
	}
399
400
	public function testCountForNonEmptyList() {
401
		$list = new StatementList(
402
			$this->getStatementWithSnak( 'P1', 'foo' ),
403
			$this->getStatementWithSnak( 'P2', 'bar' )
404
		);
405
406
		$this->assertSame( 2, $list->count() );
407
	}
408
409
	/**
410
	 * @dataProvider statementArrayProvider
411
	 */
412
	public function testGivenIdenticalLists_equalsReturnsTrue( array $statements ) {
413
		$firstStatements = new StatementList( $statements );
414
		$secondStatements = new StatementList( $statements );
415
416
		$this->assertTrue( $firstStatements->equals( $secondStatements ) );
417
	}
418
419
	public function statementArrayProvider() {
420
		return [
421
			[ [
422
				$this->getStatementWithSnak( 'P1', 'foo' ),
423
				$this->getStatementWithSnak( 'P2', 'bar' ),
424
			] ],
425
			[ [
426
				$this->getStatementWithSnak( 'P1', 'foo' ),
427
			] ],
428
			[ [
429
			] ],
430
		];
431
	}
432
433
	public function testGivenDifferentLists_equalsReturnsFalse() {
434
		$firstStatements = new StatementList(
435
			$this->getStatementWithSnak( 'P1', 'foo' ),
436
			$this->getStatementWithSnak( 'P2', 'bar' )
437
		);
438
439
		$secondStatements = new StatementList(
440
			$this->getStatementWithSnak( 'P1', 'foo' ),
441
			$this->getStatementWithSnak( 'P2', 'SPAM' )
442
		);
443
444
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
445
	}
446
447
	public function testGivenListsWithDifferentDuplicates_equalsReturnsFalse() {
448
		$firstStatements = new StatementList(
449
			$this->getStatementWithSnak( 'P1', 'foo' ),
450
			$this->getStatementWithSnak( 'P1', 'foo' ),
451
			$this->getStatementWithSnak( 'P2', 'bar' )
452
		);
453
454
		$secondStatements = new StatementList(
455
			$this->getStatementWithSnak( 'P1', 'foo' ),
456
			$this->getStatementWithSnak( 'P2', 'bar' ),
457
			$this->getStatementWithSnak( 'P2', 'bar' )
458
		);
459
460
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
461
	}
462
463
	public function testGivenListsWithDifferentOrder_equalsReturnsFalse() {
464
		$firstStatements = new StatementList(
465
			$this->getStatementWithSnak( 'P1', 'foo' ),
466
			$this->getStatementWithSnak( 'P2', 'bar' ),
467
			$this->getStatementWithSnak( 'P3', 'baz' )
468
		);
469
470
		$secondStatements = new StatementList(
471
			$this->getStatementWithSnak( 'P1', 'foo' ),
472
			$this->getStatementWithSnak( 'P3', 'baz' ),
473
			$this->getStatementWithSnak( 'P2', 'bar' )
474
		);
475
476
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
477
	}
478
479
	public function testEmptyListDoesNotEqualNonEmptyList() {
480
		$firstStatements = new StatementList();
481
482
		$secondStatements = new StatementList(
483
			$this->getStatementWithSnak( 'P1', 'foo' ),
484
			$this->getStatementWithSnak( 'P3', 'baz' ),
485
			$this->getStatementWithSnak( 'P2', 'bar' )
486
		);
487
488
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
489
	}
490
491
	public function testNonEmptyListDoesNotEqualEmptyList() {
492
		$firstStatements = new StatementList(
493
			$this->getStatementWithSnak( 'P1', 'foo' ),
494
			$this->getStatementWithSnak( 'P3', 'baz' ),
495
			$this->getStatementWithSnak( 'P2', 'bar' )
496
		);
497
498
		$secondStatements = new StatementList();
499
500
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
501
	}
502
503
	public function testEmptyListIsEmpty() {
504
		$list = new StatementList();
505
506
		$this->assertTrue( $list->isEmpty() );
507
	}
508
509
	public function testNonEmptyListIsNotEmpty() {
510
		$list = new StatementList( $this->getStatementWithSnak( 'P1', 'foo' ) );
511
512
		$this->assertFalse( $list->isEmpty() );
513
	}
514
515
	public function testGetMainSnaks() {
516
		$list = new StatementList();
517
518
		$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
519
		$list->addNewStatement( new PropertyNoValueSnak( 1337 ), [ new PropertyNoValueSnak( 32202 ) ] );
520
		$list->addNewStatement( new PropertyNoValueSnak( 9001 ) );
521
522
		$this->assertEquals(
523
			[
524
				new PropertyNoValueSnak( 42 ),
525
				new PropertyNoValueSnak( 1337 ),
526
				new PropertyNoValueSnak( 9001 ),
527
			],
528
			$list->getMainSnaks()
529
		);
530
	}
531
532
	public function testGivenNotKnownPropertyId_getByPropertyIdReturnsEmptyList() {
533
		$list = new StatementList();
534
		$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
535
536
		$this->assertEquals(
537
			new StatementList(),
538
			$list->getByPropertyId( new PropertyId( 'P2' ) )
539
		);
540
	}
541
542
	public function testGivenKnownPropertyId_getByPropertyIdReturnsListWithOnlyMatchingStatements() {
543
		$list = new StatementList();
544
		$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
545
		$list->addNewStatement( new PropertyNoValueSnak( 9001 ) );
546
		$list->addNewStatement( new PropertySomeValueSnak( 42 ) );
547
		$list->addNewStatement( new PropertySomeValueSnak( 9001 ) );
548
549
		$expected = new StatementList();
550
		$expected->addNewStatement( new PropertyNoValueSnak( 42 ) );
551
		$expected->addNewStatement( new PropertySomeValueSnak( 42 ) );
552
553
		$this->assertEquals(
554
			$expected,
555
			$list->getByPropertyId( new PropertyId( 'P42' ) )
556
		);
557
	}
558
559
	public function testGivenInvalidRank_getByRankReturnsEmptyList() {
560
		$list = new StatementList();
561
		$this->assertEquals( new StatementList(), $list->getByRank( 42 ) );
562
	}
563
564
	public function testGivenValidRank_getByRankReturnsOnlyMatchingStatements() {
565
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
566
		$statement->setRank( Statement::RANK_PREFERRED );
567
568
		$secondStatement = new Statement( new PropertyNoValueSnak( 1337 ) );
569
		$secondStatement->setRank( Statement::RANK_NORMAL );
570
571
		$thirdStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
572
		$thirdStatement->setRank( Statement::RANK_DEPRECATED );
573
574
		$list = new StatementList( $statement, $secondStatement, $thirdStatement );
575
576
		$this->assertEquals(
577
			new StatementList( $statement ),
578
			$list->getByRank( Statement::RANK_PREFERRED )
579
		);
580
581
		$this->assertEquals(
582
			new StatementList( $secondStatement, $thirdStatement ),
583
			$list->getByRank( [ Statement::RANK_NORMAL, Statement::RANK_DEPRECATED ] )
584
		);
585
	}
586
587
	public function testWhenListIsEmpty_getBestStatementsReturnsEmptyList() {
588
		$list = new StatementList();
589
		$this->assertEquals( new StatementList(), $list->getBestStatements() );
590
	}
591
592
	public function testWhenOnlyDeprecatedStatements_getBestStatementsReturnsEmptyList() {
593
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
594
		$statement->setRank( Statement::RANK_DEPRECATED );
595
596
		$secondStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
597
		$secondStatement->setRank( Statement::RANK_DEPRECATED );
598
599
		$list = new StatementList( $statement, $secondStatement );
600
		$this->assertEquals( new StatementList(), $list->getBestStatements() );
601
	}
602
603
	public function testWhenPreferredStatements_getBestStatementsReturnsOnlyThose() {
604
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
605
		$statement->setRank( Statement::RANK_PREFERRED );
606
607
		$secondStatement = new Statement( new PropertyNoValueSnak( 1337 ) );
608
		$secondStatement->setRank( Statement::RANK_NORMAL );
609
610
		$thirdStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
611
		$thirdStatement->setRank( Statement::RANK_DEPRECATED );
612
613
		$fourthStatement = new Statement( new PropertyNoValueSnak( 23 ) );
614
		$fourthStatement->setRank( Statement::RANK_PREFERRED );
615
616
		$list = new StatementList( $statement, $secondStatement, $thirdStatement, $fourthStatement );
617
		$this->assertEquals(
618
			new StatementList( $statement, $fourthStatement ),
619
			$list->getBestStatements()
620
		);
621
	}
622
623
	public function testWhenNoPreferredStatements_getBestStatementsReturnsOnlyNormalOnes() {
624
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
625
		$statement->setRank( Statement::RANK_NORMAL );
626
627
		$secondStatement = new Statement( new PropertyNoValueSnak( 1337 ) );
628
		$secondStatement->setRank( Statement::RANK_NORMAL );
629
630
		$thirdStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
631
		$thirdStatement->setRank( Statement::RANK_DEPRECATED );
632
633
		$list = new StatementList( $statement, $secondStatement, $thirdStatement );
634
		$this->assertEquals(
635
			new StatementList( $statement, $secondStatement ),
636
			$list->getBestStatements()
637
		);
638
	}
639
640
	public function testGivenNotPresentStatement_getFirstStatementWithGuidReturnsNull() {
641
		$statements = new StatementList();
642
643
		$this->assertNull( $statements->getFirstStatementWithGuid( 'kittens' ) );
644
	}
645
646
	public function testGivenPresentStatement_getFirstStatementWithGuidReturnsStatement() {
647
		$statement1 = $this->getStatement( 'P1', 'guid1' );
648
		$statement2 = $this->getStatement( 'P2', 'guid2' );
649
		$statement3 = $this->getStatement( 'P3', 'guid3' );
650
		$statements = new StatementList( $statement1, $statement2, $statement3 );
651
652
		$actual = $statements->getFirstStatementWithGuid( 'guid2' );
653
		$this->assertSame( $statement2, $actual );
654
	}
655
656
	public function testGivenDoublyPresentStatement_getFirstStatementWithGuidReturnsFirstMatch() {
657
		$statement1 = $this->getStatement( 'P1', 'guid1' );
658
		$statement2 = $this->getStatement( 'P2', 'guid2' );
659
		$statement3 = $this->getStatement( 'P3', 'guid3' );
660
		$statement4 = $this->getStatement( 'P2', 'guid2' );
661
		$statements = new StatementList( $statement1, $statement2, $statement3, $statement4 );
662
663
		$actual = $statements->getFirstStatementWithGuid( 'guid2' );
664
		$this->assertSame( $statement2, $actual );
665
	}
666
667
	public function testGivenStatementsWithNoGuid_getFirstStatementWithGuidReturnsFirstMatch() {
668
		$statement1 = $this->getStatement( 'P1', null );
669
		$statement2 = $this->getStatement( 'P2', null );
670
		$statements = new StatementList( $statement1, $statement2 );
671
672
		$actual = $statements->getFirstStatementWithGuid( null );
673
		$this->assertSame( $statement1, $actual );
674
	}
675
676
	public function testGivenInvalidGuid_getFirstStatementWithGuidReturnsNull() {
677
		$statements = new StatementList();
678
679
		$this->assertNull( $statements->getFirstStatementWithGuid( false ) );
0 ignored issues
show
false is of type boolean, but the function expects a string|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
680
	}
681
682
	public function testFilter() {
683
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
684
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
685
		$statement3 = new Statement( new PropertyNoValueSnak( 3 ) );
686
		$statement4 = new Statement( new PropertyNoValueSnak( 4 ) );
687
688
		$statement2->setReferences( new ReferenceList( [
689
			new Reference( [ new PropertyNoValueSnak( 20 ) ] )
690
		] ) );
691
692
		$statement3->setReferences( new ReferenceList( [
693
			new Reference( [ new PropertyNoValueSnak( 30 ) ] )
694
		] ) );
695
696
		$statements = new StatementList( $statement1, $statement2, $statement3, $statement4 );
697
698
		$this->assertEquals(
699
			new StatementList( $statement2, $statement3 ),
700
			$statements->filter( new ReferencedStatementFilter() )
701
		);
702
	}
703
704
	public function testClear() {
705
		$statement1 = $this->getStatement( 'P1', null );
706
		$statement2 = $this->getStatement( 'P2', null );
707
		$statements = new StatementList( $statement1, $statement2 );
708
709
		$statements->clear();
710
711
		$this->assertEquals( new StatementList(), $statements );
712
	}
713
714
}
715