Passed
Push — currentLimits ( 193e29...261cca )
by no
06:29 queued 02:03
created

PropertyTest::testConstructorWithAllParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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