Passed
Push — micro-opti-entityid-stuffz ( ca1ae8...81f418 )
by Leszek
03:49
created

PropertyTest::testNotEquals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use InvalidArgumentException;
6
use PHPUnit_Framework_TestCase;
7
use Wikibase\DataModel\Entity\Property;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
10
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
11
use Wikibase\DataModel\Statement\Statement;
12
use Wikibase\DataModel\Statement\StatementList;
13
use Wikibase\DataModel\Term\AliasGroup;
14
use Wikibase\DataModel\Term\AliasGroupList;
15
use Wikibase\DataModel\Term\Fingerprint;
16
use Wikibase\DataModel\Term\Term;
17
use Wikibase\DataModel\Term\TermList;
18
19
/**
20
 * @covers Wikibase\DataModel\Entity\Property
21
 *
22
 * @group Wikibase
23
 * @group WikibaseDataModel
24
 *
25
 * @license GPL-2.0+
26
 * @author Jeroen De Dauw < [email protected] >
27
 */
28
class PropertyTest extends PHPUnit_Framework_TestCase {
29
30
	/**
31
	 * @return Property
32
	 */
33
	private function getNewEmpty() {
34
		return Property::newFromType( 'string' );
35
	}
36
37
	public function testConstructorWithAllParameters() {
38
		$property = new Property(
39
			new PropertyId( 'P42' ),
40
			new Fingerprint(),
41
			'string',
42
			new StatementList()
43
		);
44
		$this->assertInstanceOf( Property::class, $property );
45
		$this->assertEquals( new PropertyId( 'P42' ), $property->getId() );
46
		$this->assertEquals( new Fingerprint(), $property->getFingerprint() );
47
		$this->assertSame( 'string', $property->getDataTypeId() );
48
		$this->assertEquals( new StatementList(), $property->getStatements() );
49
	}
50
51
	public function testConstructorWithMinimalParameters() {
52
		$property = new Property( null, null, '' );
53
		$this->assertInstanceOf( Property::class, $property );
54
		$this->assertNull( $property->getId() );
55
		$this->assertEquals( new Fingerprint(), $property->getFingerprint() );
56
		$this->assertSame( '', $property->getDataTypeId() );
57
		$this->assertEquals( new StatementList(), $property->getStatements() );
58
	}
59
60
	/**
61
	 * @expectedException InvalidArgumentException
62
	 */
63
	public function testGivenInvalidType_ConstructorThrowsException() {
64
		new Property( null, null, null );
65
	}
66
67
	public function testNewFromType() {
68
		$property = Property::newFromType( 'string' );
69
		$this->assertInstanceOf( Property::class, $property );
70
		$this->assertSame( 'string', $property->getDataTypeId() );
71
	}
72
73
	public function testSetAndGetDataTypeId() {
74
		$property = Property::newFromType( 'string' );
75
76
		foreach ( [ 'string', 'foobar', 'nyan', 'string' ] as $typeId ) {
77
			$property->setDataTypeId( $typeId );
78
			$this->assertSame( $typeId, $property->getDataTypeId() );
79
		}
80
	}
81
82
	public function testWhenIdSetWithNumber_GetIdReturnsPropertyId() {
83
		$property = Property::newFromType( 'string' );
84
		$property->setId( 42 );
85
86
		$this->assertHasCorrectIdType( $property );
87
	}
88
89
	protected function assertHasCorrectIdType( Property $property ) {
90
		$this->assertInstanceOf( PropertyId::class, $property->getId() );
91
	}
92
93
	public function testWhenIdSetWithPropertyId_GetIdReturnsPropertyId() {
94
		$property = Property::newFromType( 'string' );
95
		$property->setId( new PropertyId( 'P42' ) );
96
97
		$this->assertHasCorrectIdType( $property );
98
	}
99
100
	public function testPropertyWithTypeIsEmpty() {
101
		$this->assertTrue( Property::newFromType( 'string' )->isEmpty() );
102
	}
103
104
	public function testPropertyWithIdIsEmpty() {
105
		$property = Property::newFromType( 'string' );
106
		$property->setId( 1337 );
107
		$this->assertTrue( $property->isEmpty() );
108
	}
109
110
	public function testPropertyWithFingerprintIsNotEmpty() {
111
		$property = Property::newFromType( 'string' );
112
		$property->setAliases( 'en', [ 'foo' ] );
113
		$this->assertFalse( $property->isEmpty() );
114
	}
115
116
	public function testGetStatementsReturnsEmptyListForEmptyProperty() {
117
		$property = Property::newFromType( 'string' );
118
119
		$this->assertEquals( new StatementList(), $property->getStatements() );
120
	}
121
122
	public function testSetAndGetStatements() {
123
		$property = Property::newFromType( 'string' );
124
125
		$statementList = $this->newNonEmptyStatementList();
126
		$property->setStatements( $statementList );
127
128
		$this->assertEquals( $statementList, $property->getStatements() );
129
	}
130
131
	private function newNonEmptyStatementList() {
132
		$statementList = new StatementList();
133
		$statementList->addNewStatement( new PropertyNoValueSnak( 42 ) );
134
		$statementList->addNewStatement( new PropertyNoValueSnak( 1337 ) );
135
136
		return $statementList;
137
	}
138
139
	public function equalsProvider() {
140
		$firstProperty = Property::newFromType( 'string' );
141
		$firstProperty->setStatements( $this->newNonEmptyStatementList() );
142
143
		$secondProperty = Property::newFromType( 'string' );
144
		$secondProperty->setStatements( $this->newNonEmptyStatementList() );
145
146
		$secondPropertyWithId = $secondProperty->copy();
147
		$secondPropertyWithId->setId( 42 );
148
149
		$differentId = $secondPropertyWithId->copy();
150
		$differentId->setId( 43 );
151
152
		return [
153
			[ Property::newFromType( 'string' ), Property::newFromType( 'string' ) ],
154
			[ $firstProperty, $secondProperty ],
155
			[ $secondProperty, $secondPropertyWithId ],
156
			[ $secondPropertyWithId, $differentId ],
157
		];
158
	}
159
160
	/**
161
	 * @dataProvider equalsProvider
162
	 */
163
	public function testEquals( Property $firstProperty, Property $secondProperty ) {
164
		$this->assertTrue( $firstProperty->equals( $secondProperty ) );
165
		$this->assertTrue( $secondProperty->equals( $firstProperty ) );
166
	}
167
168
	private function getBaseProperty() {
169
		$property = Property::newFromType( 'string' );
170
171
		$property->setId( 42 );
172
		$property->setLabel( 'en', 'Same' );
173
		$property->setDescription( 'en', 'Same' );
174
		$property->setAliases( 'en', [ 'Same' ] );
175
		$property->setStatements( $this->newNonEmptyStatementList() );
176
177
		return $property;
178
	}
179
180
	public function notEqualsProvider() {
181
		$differentLabel = $this->getBaseProperty();
182
		$differentLabel->setLabel( 'en', 'Different' );
183
184
		$differentDescription = $this->getBaseProperty();
185
		$differentDescription->setDescription( 'en', 'Different' );
186
187
		$differentAlias = $this->getBaseProperty();
188
		$differentAlias->setAliases( 'en', [ 'Different' ] );
189
190
		$differentStatement = $this->getBaseProperty();
191
		$differentStatement->setStatements( new StatementList() );
192
193
		$property = $this->getBaseProperty();
194
195
		return [
196
			'empty' => [ $property, Property::newFromType( 'string' ) ],
197
			'label' => [ $property, $differentLabel ],
198
			'description' => [ $property, $differentDescription ],
199
			'alias' => [ $property, $differentAlias ],
200
			'dataType' => [ Property::newFromType( 'string' ), Property::newFromType( 'foo' ) ],
201
			'statement' => [ $property, $differentStatement ],
202
		];
203
	}
204
205
	/**
206
	 * @dataProvider notEqualsProvider
207
	 */
208
	public function testNotEquals( Property $firstProperty, Property $secondProperty ) {
209
		$this->assertFalse( $firstProperty->equals( $secondProperty ) );
210
		$this->assertFalse( $secondProperty->equals( $firstProperty ) );
211
	}
212
213
	public function testPropertyWithStatementsIsNotEmpty() {
214
		$property = Property::newFromType( 'string' );
215
		$property->setStatements( $this->newNonEmptyStatementList() );
216
217
		$this->assertFalse( $property->isEmpty() );
218
	}
219
220
	public function cloneProvider() {
221
		$property = new Property( new PropertyId( 'P1' ), null, 'string' );
222
		$property->setLabel( 'en', 'original' );
223
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
224
225
		return [
226
			'copy' => [ $property, $property->copy() ],
227
			'native clone' => [ $property, clone $property ],
228
		];
229
	}
230
231
	/**
232
	 * @dataProvider cloneProvider
233
	 */
234
	public function testCloneIsEqualButNotIdentical( Property $original, Property $clone ) {
235
		$this->assertNotSame( $original, $clone );
236
		$this->assertTrue( $original->equals( $clone ) );
237
		$this->assertSame(
238
			$original->getId(),
239
			$clone->getId(),
240
			'id is immutable and must not be cloned'
241
		);
242
243
		// The clone must not reference the same mutable objects
244
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
245
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
246
		$this->assertNotSame(
247
			$original->getStatements()->getFirstStatementWithGuid( null ),
248
			$clone->getStatements()->getFirstStatementWithGuid( null )
249
		);
250
	}
251
252
	/**
253
	 * @dataProvider cloneProvider
254
	 */
255
	public function testOriginalDoesNotChangeWithClone( Property $original, Property $clone ) {
256
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
257
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
258
259
		$clone->setLabel( 'en', 'clone' );
260
		$clone->setDescription( 'en', 'clone' );
261
		$clone->setAliases( 'en', [ 'clone' ] );
262
		$clonedStatement->setGuid( 'clone' );
263
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
264
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
265
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
266
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
267
268
		$this->assertSame( 'original', $original->getLabels()->getByLanguage( 'en' )->getText() );
269
		$this->assertFalse( $original->getDescriptions()->hasTermForLanguage( 'en' ) );
270
		$this->assertFalse( $original->getAliasGroups()->hasGroupForLanguage( 'en' ) );
271
		$this->assertNull( $originalStatement->getGuid() );
272
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
273
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
274
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
275
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
276
	}
277
278
	// Below are tests copied from EntityTest
279
280
	public function labelProvider() {
281
		return [
282
			[ 'en', 'spam' ],
283
			[ 'en', 'spam', 'spam' ],
284
			[ 'de', 'foo bar baz' ],
285
		];
286
	}
287
288
	/**
289
	 * @dataProvider labelProvider
290
	 * @param string $languageCode
291
	 * @param string $labelText
292
	 * @param string $moarText
293
	 */
294
	public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) {
295
		$entity = $this->getNewEmpty();
296
297
		$entity->setLabel( $languageCode, $labelText );
298
299
		$this->assertSame( $labelText, $entity->getFingerprint()->getLabel( $languageCode )->getText() );
300
301
		$entity->setLabel( $languageCode, $moarText );
302
303
		$this->assertSame( $moarText, $entity->getFingerprint()->getLabel( $languageCode )->getText() );
304
	}
305
306
	public function descriptionProvider() {
307
		return [
308
			[ 'en', 'spam' ],
309
			[ 'en', 'spam', 'spam' ],
310
			[ 'de', 'foo bar baz' ],
311
		];
312
	}
313
314
	/**
315
	 * @dataProvider descriptionProvider
316
	 * @param string $languageCode
317
	 * @param string $description
318
	 * @param string $moarText
319
	 */
320
	public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) {
321
		$entity = $this->getNewEmpty();
322
323
		$entity->setDescription( $languageCode, $description );
324
325
		$this->assertSame( $description, $entity->getFingerprint()->getDescription( $languageCode )->getText() );
326
327
		$entity->setDescription( $languageCode, $moarText );
328
329
		$this->assertSame( $moarText, $entity->getFingerprint()->getDescription( $languageCode )->getText() );
330
	}
331
332
	public function aliasesProvider() {
333
		return [
334
			[ [
335
				       'en' => [ [ 'spam' ] ]
336
			       ] ],
337
			[ [
338
				       'en' => [ [ 'foo', 'bar', 'baz' ] ]
339
			       ] ],
340
			[ [
341
				       'en' => [ [ 'foo', 'bar' ], [ 'baz', 'spam' ] ]
342
			       ] ],
343
			[ [
344
				       'en' => [ [ 'foo', 'bar', 'baz' ] ],
345
				       'de' => [ [ 'foobar' ], [ 'baz' ] ],
346
			       ] ],
347
			// with duplicates
348
			[ [
349
				       'en' => [ [ 'spam', 'ham', 'ham' ] ]
350
			       ] ],
351
			[ [
352
				       'en' => [ [ 'foo', 'bar' ], [ 'bar', 'spam' ] ]
353
			       ] ],
354
		];
355
	}
356
357
	/**
358
	 * @dataProvider aliasesProvider
359
	 */
360
	public function testSetAliases( array $aliasesLists ) {
361
		$entity = $this->getNewEmpty();
362
363
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
364
			foreach ( $aliasesList as $aliases ) {
365
				$entity->setAliases( $langCode, $aliases );
366
			}
367
		}
368
369
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
370
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
371
			$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases();
372
			$this->assertSame( $expected, $actual );
373
		}
374
	}
375
376
	/**
377
	 * @dataProvider aliasesProvider
378
	 */
379
	public function testSetEmptyAlias( array $aliasesLists ) {
380
		$entity = $this->getNewEmpty();
381
382
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
383
			foreach ( $aliasesList as $aliases ) {
384
				$entity->setAliases( $langCode, $aliases );
385
			}
386
		}
387
		$entity->setAliases( 'zh', [ 'wind', 'air', '', 'fire' ] );
388
		$entity->setAliases( 'zu', [ '', '' ] );
389
390
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
391
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
392
			asort( $aliasesList );
393
394
			$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases();
395
			asort( $actual );
396
397
			$this->assertEquals( $expected, $actual );
398
		}
399
	}
400
401
	public function instanceProvider() {
402
		$entities = [];
403
404
		// empty
405
		$entity = $this->getNewEmpty();
406
		$entities[] = $entity;
407
408
		// ID only
409
		$entity = clone $entity;
410
		$entity->setId( 44 );
411
412
		$entities[] = $entity;
413
414
		// with labels and stuff
415
		$entity = $this->getNewEmpty();
416
		$entity->setAliases( 'en', [ 'o', 'noez' ] );
417
		$entity->setLabel( 'de', 'spam' );
418
		$entity->setDescription( 'en', 'foo bar baz' );
419
420
		$entities[] = $entity;
421
422
		// with labels etc and ID
423
		$entity = clone $entity;
424
		$entity->setId( 42 );
425
426
		$entities[] = $entity;
427
428
		$argLists = [];
429
430
		foreach ( $entities as $entity ) {
431
			$argLists[] = [ $entity ];
432
		}
433
434
		return $argLists;
435
	}
436
437
	/**
438
	 * @dataProvider instanceProvider
439
	 * @param Property $entity
440
	 */
441
	public function testCopy( Property $entity ) {
442
		$copy = $entity->copy();
443
444
		// The equality method alone is not enough since it does not check the IDs.
445
		$this->assertTrue( $entity->equals( $copy ) );
446
		$this->assertEquals( $entity->getId(), $copy->getId() );
447
448
		$this->assertNotSame( $entity, $copy );
449
	}
450
451
	public function testCopyRetainsLabels() {
452
		$property = Property::newFromType( 'string' );
453
454
		$property->getFingerprint()->setLabel( 'en', 'foo' );
455
		$property->getFingerprint()->setLabel( 'de', 'bar' );
456
457
		$newProperty = $property->copy();
458
459
		$this->assertTrue( $newProperty->getFingerprint()->getLabels()->hasTermForLanguage( 'en' ) );
460
		$this->assertTrue( $newProperty->getFingerprint()->getLabels()->hasTermForLanguage( 'de' ) );
461
	}
462
463
	/**
464
	 * @dataProvider instanceProvider
465
	 * @param Property $entity
466
	 */
467
	public function testSerialize( Property $entity ) {
468
		$string = serialize( $entity );
469
470
		$this->assertInternalType( 'string', $string );
471
472
		$instance = unserialize( $string );
473
474
		$this->assertTrue( $entity->equals( $instance ) );
475
		$this->assertEquals( $entity->getId(), $instance->getId() );
476
	}
477
478
	public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() {
479
		$entity = $this->getNewEmpty();
480
481
		$this->assertEquals(
482
			new Fingerprint(),
483
			$entity->getFingerprint()
484
		);
485
	}
486
487
	public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() {
488
		$entity = $this->getNewEmpty();
489
490
		$entity->setLabel( 'en', 'foo' );
491
		$entity->setLabel( 'de', 'bar' );
492
493
		$this->assertEquals(
494
			new Fingerprint(
495
				new TermList( [
496
					new Term( 'en', 'foo' ),
497
					new Term( 'de', 'bar' ),
498
				] )
499
			),
500
			$entity->getFingerprint()
501
		);
502
	}
503
504
	public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() {
505
		$entity = $this->getNewEmpty();
506
507
		$entity->setLabel( 'en', 'foo' );
508
		$entity->setDescription( 'en', 'foo bar' );
509
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
510
511
		$this->assertEquals(
512
			new Fingerprint(
513
				new TermList( [
514
					new Term( 'en', 'foo' ),
515
				] ),
516
				new TermList( [
517
					new Term( 'en', 'foo bar' )
518
				] ),
519
				new AliasGroupList( [
520
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
521
				] )
522
			),
523
			$entity->getFingerprint()
524
		);
525
	}
526
527
	public function testGivenEmptyFingerprint_noTermsAreSet() {
528
		$entity = $this->getNewEmpty();
529
		$entity->setFingerprint( new Fingerprint() );
530
531
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
532
	}
533
534
	public function testGivenEmptyFingerprint_existingTermsAreRemoved() {
535
		$entity = $this->getNewEmpty();
536
537
		$entity->setLabel( 'en', 'foo' );
538
		$entity->setDescription( 'en', 'foo bar' );
539
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
540
541
		$entity->setFingerprint( new Fingerprint() );
542
543
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
544
	}
545
546
	public function testWhenSettingFingerprint_getFingerprintReturnsIt() {
547
		$fingerprint = new Fingerprint(
548
			new TermList( [
549
				new Term( 'en', 'english label' ),
550
			] ),
551
			new TermList( [
552
				new Term( 'en', 'english description' )
553
			] ),
554
			new AliasGroupList( [
555
				new AliasGroup( 'en', [ 'first en alias', 'second en alias' ] )
556
			] )
557
		);
558
559
		$entity = $this->getNewEmpty();
560
		$entity->setFingerprint( $fingerprint );
561
		$newFingerprint = $entity->getFingerprint();
562
563
		$this->assertSame( $fingerprint, $newFingerprint );
564
	}
565
566
	public function testGetLabels() {
567
		$property = Property::newFromType( 'string' );
568
		$property->setLabel( 'en', 'foo' );
569
570
		$this->assertEquals(
571
			new TermList( [
572
				new Term( 'en', 'foo' )
573
			] ),
574
			$property->getLabels()
575
		);
576
	}
577
578
	public function testGetDescriptions() {
579
		$property = Property::newFromType( 'string' );
580
		$property->setDescription( 'en', 'foo bar' );
581
582
		$this->assertEquals(
583
			new TermList( [
584
				new Term( 'en', 'foo bar' )
585
			] ),
586
			$property->getDescriptions()
587
		);
588
	}
589
590
	public function testGetAliasGroups() {
591
		$property = Property::newFromType( 'string' );
592
		$property->setAliases( 'en', [ 'foo', 'bar' ] );
593
594
		$this->assertEquals(
595
			new AliasGroupList( [
596
				new AliasGroup( 'en', [ 'foo', 'bar' ] )
597
			] ),
598
			$property->getAliasGroups()
599
		);
600
	}
601
602
	public function testGetLabels_sameListAsFingerprint() {
603
		$property = Property::newFromType( 'string' );
604
605
		$this->assertSame(
606
			$property->getFingerprint()->getLabels(),
607
			$property->getLabels()
608
		);
609
	}
610
611
	public function testGetDescriptions_sameListAsFingerprint() {
612
		$property = Property::newFromType( 'string' );
613
614
		$this->assertSame(
615
			$property->getFingerprint()->getDescriptions(),
616
			$property->getDescriptions()
617
		);
618
	}
619
620
	public function testGetAliasGroups_sameListAsFingerprint() {
621
		$property = Property::newFromType( 'string' );
622
623
		$this->assertSame(
624
			$property->getFingerprint()->getAliasGroups(),
625
			$property->getAliasGroups()
626
		);
627
	}
628
629
}
630