Completed
Push — testSetEmptyAlias ( 7a4565 )
by no
03:21
created

testWhenIdSetWithPropertyId_GetIdReturnsPropertyId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
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->assertEquals( '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->assertEquals( '', $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->assertEquals( '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->assertEquals( $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->assertEquals( $labelText, $entity->getFingerprint()->getLabel( $languageCode )->getText() );
300
301
		$entity->setLabel( $languageCode, $moarText );
302
303
		$this->assertEquals( $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->assertEquals( $description, $entity->getFingerprint()->getDescription( $languageCode )->getText() );
326
327
		$entity->setDescription( $languageCode, $moarText );
328
329
		$this->assertEquals( $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
			asort( $aliasesList );
372
373
			$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases();
374
			asort( $actual );
375
376
			$this->assertEquals( $expected, $actual );
377
		}
378
	}
379
380
	public function testSetEmptyAlias() {
381
		$property = Property::newFromType( 'string' );
382
383
		$property->setAliases( 'en', [ 'wind', 'air', '', 'fire' ] );
384
		$this->assertSame(
385
			[ 'wind', 'air', 'fire' ],
386
			$property->getAliasGroups()->getByLanguage( 'en' )->getAliases()
387
		);
388
389
		$property->setAliases( 'en', [ '', '' ] );
390
		$this->assertFalse( $property->getAliasGroups()->hasGroupForLanguage( 'en' ) );
391
	}
392
393
	public function instanceProvider() {
394
		$entities = [];
395
396
		// empty
397
		$entity = $this->getNewEmpty();
398
		$entities[] = $entity;
399
400
		// ID only
401
		$entity = clone $entity;
402
		$entity->setId( 44 );
403
404
		$entities[] = $entity;
405
406
		// with labels and stuff
407
		$entity = $this->getNewEmpty();
408
		$entity->setAliases( 'en', [ 'o', 'noez' ] );
409
		$entity->setLabel( 'de', 'spam' );
410
		$entity->setDescription( 'en', 'foo bar baz' );
411
412
		$entities[] = $entity;
413
414
		// with labels etc and ID
415
		$entity = clone $entity;
416
		$entity->setId( 42 );
417
418
		$entities[] = $entity;
419
420
		$argLists = [];
421
422
		foreach ( $entities as $entity ) {
423
			$argLists[] = [ $entity ];
424
		}
425
426
		return $argLists;
427
	}
428
429
	/**
430
	 * @dataProvider instanceProvider
431
	 * @param Property $entity
432
	 */
433
	public function testCopy( Property $entity ) {
434
		$copy = $entity->copy();
435
436
		// The equality method alone is not enough since it does not check the IDs.
437
		$this->assertTrue( $entity->equals( $copy ) );
438
		$this->assertEquals( $entity->getId(), $copy->getId() );
439
440
		$this->assertNotSame( $entity, $copy );
441
	}
442
443
	public function testCopyRetainsLabels() {
444
		$property = Property::newFromType( 'string' );
445
446
		$property->getFingerprint()->setLabel( 'en', 'foo' );
447
		$property->getFingerprint()->setLabel( 'de', 'bar' );
448
449
		$newProperty = $property->copy();
450
451
		$this->assertTrue( $newProperty->getFingerprint()->getLabels()->hasTermForLanguage( 'en' ) );
452
		$this->assertTrue( $newProperty->getFingerprint()->getLabels()->hasTermForLanguage( 'de' ) );
453
	}
454
455
	/**
456
	 * @dataProvider instanceProvider
457
	 * @param Property $entity
458
	 */
459
	public function testSerialize( Property $entity ) {
460
		$string = serialize( $entity );
461
462
		$this->assertInternalType( 'string', $string );
463
464
		$instance = unserialize( $string );
465
466
		$this->assertTrue( $entity->equals( $instance ) );
467
		$this->assertEquals( $entity->getId(), $instance->getId() );
468
	}
469
470
	public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() {
471
		$entity = $this->getNewEmpty();
472
473
		$this->assertEquals(
474
			new Fingerprint(),
475
			$entity->getFingerprint()
476
		);
477
	}
478
479
	public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() {
480
		$entity = $this->getNewEmpty();
481
482
		$entity->setLabel( 'en', 'foo' );
483
		$entity->setLabel( 'de', 'bar' );
484
485
		$this->assertEquals(
486
			new Fingerprint(
487
				new TermList( [
488
					new Term( 'en', 'foo' ),
489
					new Term( 'de', 'bar' ),
490
				] )
491
			),
492
			$entity->getFingerprint()
493
		);
494
	}
495
496
	public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() {
497
		$entity = $this->getNewEmpty();
498
499
		$entity->setLabel( 'en', 'foo' );
500
		$entity->setDescription( 'en', 'foo bar' );
501
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
502
503
		$this->assertEquals(
504
			new Fingerprint(
505
				new TermList( [
506
					new Term( 'en', 'foo' ),
507
				] ),
508
				new TermList( [
509
					new Term( 'en', 'foo bar' )
510
				] ),
511
				new AliasGroupList( [
512
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
513
				] )
514
			),
515
			$entity->getFingerprint()
516
		);
517
	}
518
519
	public function testGivenEmptyFingerprint_noTermsAreSet() {
520
		$entity = $this->getNewEmpty();
521
		$entity->setFingerprint( new Fingerprint() );
522
523
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
524
	}
525
526
	public function testGivenEmptyFingerprint_existingTermsAreRemoved() {
527
		$entity = $this->getNewEmpty();
528
529
		$entity->setLabel( 'en', 'foo' );
530
		$entity->setDescription( 'en', 'foo bar' );
531
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
532
533
		$entity->setFingerprint( new Fingerprint() );
534
535
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
536
	}
537
538
	public function testWhenSettingFingerprint_getFingerprintReturnsIt() {
539
		$fingerprint = new Fingerprint(
540
			new TermList( [
541
				new Term( 'en', 'english label' ),
542
			] ),
543
			new TermList( [
544
				new Term( 'en', 'english description' )
545
			] ),
546
			new AliasGroupList( [
547
				new AliasGroup( 'en', [ 'first en alias', 'second en alias' ] )
548
			] )
549
		);
550
551
		$entity = $this->getNewEmpty();
552
		$entity->setFingerprint( $fingerprint );
553
		$newFingerprint = $entity->getFingerprint();
554
555
		$this->assertSame( $fingerprint, $newFingerprint );
556
	}
557
558
	public function testGetLabels() {
559
		$property = Property::newFromType( 'string' );
560
		$property->setLabel( 'en', 'foo' );
561
562
		$this->assertEquals(
563
			new TermList( [
564
				new Term( 'en', 'foo' )
565
			] ),
566
			$property->getLabels()
567
		);
568
	}
569
570
	public function testGetDescriptions() {
571
		$property = Property::newFromType( 'string' );
572
		$property->setDescription( 'en', 'foo bar' );
573
574
		$this->assertEquals(
575
			new TermList( [
576
				new Term( 'en', 'foo bar' )
577
			] ),
578
			$property->getDescriptions()
579
		);
580
	}
581
582
	public function testGetAliasGroups() {
583
		$property = Property::newFromType( 'string' );
584
		$property->setAliases( 'en', [ 'foo', 'bar' ] );
585
586
		$this->assertEquals(
587
			new AliasGroupList( [
588
				new AliasGroup( 'en', [ 'foo', 'bar' ] )
589
			] ),
590
			$property->getAliasGroups()
591
		);
592
	}
593
594
	public function testGetLabels_sameListAsFingerprint() {
595
		$property = Property::newFromType( 'string' );
596
597
		$this->assertSame(
598
			$property->getFingerprint()->getLabels(),
599
			$property->getLabels()
600
		);
601
	}
602
603
	public function testGetDescriptions_sameListAsFingerprint() {
604
		$property = Property::newFromType( 'string' );
605
606
		$this->assertSame(
607
			$property->getFingerprint()->getDescriptions(),
608
			$property->getDescriptions()
609
		);
610
	}
611
612
	public function testGetAliasGroups_sameListAsFingerprint() {
613
		$property = Property::newFromType( 'string' );
614
615
		$this->assertSame(
616
			$property->getFingerprint()->getAliasGroups(),
617
			$property->getAliasGroups()
618
		);
619
	}
620
621
}
622