Passed
Pull Request — master (#211)
by
unknown
03:18
created

FingerprintPatcherTest::newSimpleFingerprint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\Diff;
4
5
use Diff\DiffOp\Diff\Diff;
6
use Diff\DiffOp\DiffOpAdd;
7
use Diff\DiffOp\DiffOpChange;
8
use Diff\DiffOp\DiffOpRemove;
9
use Wikibase\DataModel\Services\Diff\EntityDiff;
10
use Wikibase\DataModel\Services\Diff\FingerprintPatcher;
11
use Wikibase\DataModel\Term\Fingerprint;
12
13
/**
14
 * @covers Wikibase\DataModel\Services\Diff\FingerprintPatcher
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Thiemo Kreuz
19
 */
20
class FingerprintPatcherTest extends \PHPUnit_Framework_TestCase {
21
22
	public function testGivenEmptyDiff_fingerprintIsReturnedAsIs() {
23
		$fingerprint = $this->newSimpleFingerprint();
24
25
		$this->assertFingerprintResultsFromPatch( $fingerprint, $fingerprint, new EntityDiff() );
26
	}
27
28
	private function newSimpleFingerprint() {
29
		$fingerprint = new Fingerprint();
30
31
		$fingerprint->setLabel( 'en', 'foo' );
32
		$fingerprint->setDescription( 'de', 'bar' );
33
		$fingerprint->setAliasGroup( 'nl', [ 'baz' ] );
34
35
		return $fingerprint;
36
	}
37
38
	private function assertFingerprintResultsFromPatch( Fingerprint $expected, Fingerprint $original, EntityDiff $patch ) {
39
		$this->assertTrue( $expected->equals( $this->getPatchedFingerprint( $original, $patch ) ) );
40
	}
41
42
	private function getPatchedFingerprint( Fingerprint $fingerprint, EntityDiff $patch ) {
43
		$patched = unserialize( serialize( $fingerprint ) );
44
45
		$patcher = new FingerprintPatcher();
46
		$patcher->patchFingerprint( $patched, $patch );
47
48
		return $patched;
49
	}
50
51
	public function testLabelDiffOnlyAffectsLabels() {
52
		$fingerprint = $this->newSimpleFingerprint();
53
54
		$patch = new EntityDiff( [
55
			'label' => new Diff( [
56
				'en' => new DiffOpChange( 'foo', 'bar' ),
57
				'de' => new DiffOpAdd( 'baz' ),
58
			], true )
59
		] );
60
61
		$expectedFingerprint = $this->newSimpleFingerprint();
62
		$expectedFingerprint->setLabel( 'en', 'bar' );
63
		$expectedFingerprint->setLabel( 'de', 'baz' );
64
65
		$this->assertFingerprintResultsFromPatch( $expectedFingerprint, $fingerprint, $patch );
66
	}
67
68
	public function testDescriptionDiffOnlyAffectsDescriptions() {
69
		$fingerprint = $this->newSimpleFingerprint();
70
71
		$patch = new EntityDiff( [
72
			'description' => new Diff( [
73
				'de' => new DiffOpChange( 'bar', 'foo' ),
74
				'en' => new DiffOpAdd( 'baz' ),
75
			], true )
76
		] );
77
78
		$expectedFingerprint = $this->newSimpleFingerprint();
79
		$expectedFingerprint->setDescription( 'de', 'foo' );
80
		$expectedFingerprint->setDescription( 'en', 'baz' );
81
82
		$this->assertFingerprintResultsFromPatch( $expectedFingerprint, $fingerprint, $patch );
83
	}
84
85
	public function aliasDiffProvider() {
86
		return [
87
			'diffs containing add/remove ops (default)' => [ [
88
				'de' => new Diff( [ new DiffOpAdd( 'foo' ) ], false ),
89
				'en' => new Diff( [ new DiffOpRemove( 'en-old' ), new DiffOpAdd( 'en-new' ) ], false ),
90
				'fa' => new Diff( [ new DiffOpRemove( 'fa-old' ) ], false ),
91
			] ],
92
			'associative diffs containing atomic ops' => [ [
93
				'de' => new Diff( [ new DiffOpAdd( 'foo' ) ], true ),
94
				'en' => new Diff( [ new DiffOpChange( 'en-old', 'en-new' ) ], true ),
95
				'fa' => new Diff( [ new DiffOpRemove( 'fa-old' ) ], true ),
96
			] ],
97
			'non-associative diffs containing atomic ops' => [ [
98
				'de' => new Diff( [ new DiffOpAdd( 'foo' ) ], true ),
99
				'en' => new Diff( [ new DiffOpChange( 'en-old', 'en-new' ) ], false ),
100
				'fa' => new Diff( [ new DiffOpRemove( 'fa-old' ) ], true ),
101
			] ],
102
			'partly associative diffs (auto-detected) containing atomic ops' => [ [
103
				'de' => new Diff( [ new DiffOpAdd( 'foo' ) ] ),
104
				'en' => new Diff( [ new DiffOpChange( 'en-old', 'en-new' ) ] ),
105
				'fa' => new Diff( [ new DiffOpRemove( 'fa-old' ) ] ),
106
			] ],
107
			'atomic diff ops' => [ [
108
				'de' => new DiffOpAdd( [ 'foo' ] ),
109
				'en' => new DiffOpChange( [ 'en-old' ], [ 'en-new' ] ),
110
				'fa' => new DiffOpRemove( [ 'fa-old' ] ),
111
			] ],
112
		];
113
	}
114
115
	/**
116
	 * @dataProvider aliasDiffProvider
117
	 */
118
	public function testAliasDiffOnlyAffectsAliases( array $diffOps ) {
119
		$fingerprint = $this->newSimpleFingerprint();
120
		$fingerprint->setAliasGroup( 'en', [ 'en-old' ] );
121
		$fingerprint->setAliasGroup( 'fa', [ 'fa-old' ] );
122
123
		$patch = new EntityDiff( [
124
			'aliases' => new Diff( $diffOps, true ),
125
		] );
126
127
		$expectedFingerprint = $this->newSimpleFingerprint();
128
		$expectedFingerprint->setAliasGroup( 'de', [ 'foo' ] );
129
		$expectedFingerprint->setAliasGroup( 'en', [ 'en-new' ] );
130
131
		$this->assertFingerprintResultsFromPatch( $expectedFingerprint, $fingerprint, $patch );
132
	}
133
134
	public function conflictingEditProvider() {
135
		return [
136
			'does not add existing label language' => [ [
137
				'label' => new Diff( [
138
					'en' => new DiffOpAdd( 'added' ),
139
				], true ),
140
			] ],
141
			'does not change modified label' => [ [
142
				'label' => new Diff( [
143
					'en' => new DiffOpChange( 'original', 'changed' ),
144
				], true ),
145
			] ],
146
			'does not change missing label' => [ [
147
				'label' => new Diff( [
148
					'de' => new DiffOpChange( 'original', 'changed' ),
149
				], true ),
150
			] ],
151
			'does not remove modified label' => [ [
152
				'label' => new Diff( [
153
					'en' => new DiffOpRemove( 'original' ),
154
				], true ),
155
			] ],
156
			'removing missing label is no-op' => [ [
157
				'label' => new Diff( [
158
					'de' => new DiffOpRemove( 'original' ),
159
				], true ),
160
			] ],
161
162
			'does not add existing description language' => [ [
163
				'description' => new Diff( [
164
					'en' => new DiffOpAdd( 'added' ),
165
				], true ),
166
			] ],
167
			'does not change modified description' => [ [
168
				'description' => new Diff( [
169
					'en' => new DiffOpChange( 'original', 'changed' ),
170
				], true ),
171
			] ],
172
			'changing missing description is no-op' => [ [
173
				'description' => new Diff( [
174
					'de' => new DiffOpChange( 'original', 'changed' ),
175
				], true ),
176
			] ],
177
			'does not remove modified description' => [ [
178
				'description' => new Diff( [
179
					'en' => new DiffOpRemove( 'original' ),
180
				], true ),
181
			] ],
182
			'removing missing description is no-op' => [ [
183
				'description' => new Diff( [
184
					'de' => new DiffOpRemove( 'original' ),
185
				], true ),
186
			] ],
187
188
			'does not add existing aliases language' => [ [
189
				'aliases' => new Diff( [
190
					'en' => new DiffOpAdd( [ 'added' ] ),
191
				], true ),
192
			] ],
193
			'does not change missing aliases language' => [ [
194
				'aliases' => new Diff( [
195
					'de' => new Diff( [ new DiffOpRemove( 'original' ), new DiffOpAdd( 'changed' ) ] ),
196
				], true ),
197
			] ],
198
			'changing missing aliases is no-op' => [ [
199
				'aliases' => new Diff( [
200
					'de' => new Diff( [ new DiffOpChange( 'original', 'changed' ) ], true ),
201
					'en' => new Diff( [ new DiffOpChange( 'original', 'changed' ) ], true ),
202
				], true ),
203
			] ],
204
			'changing missing aliases is no-op (atomic)' => [ [
205
				'aliases' => new Diff( [
206
					'de' => new DiffOpChange( [ 'original' ], [ 'changed' ] ),
207
					'en' => new DiffOpChange( [ 'original' ], [ 'changed' ] ),
208
				], true ),
209
			] ],
210
			'removing missing aliases is no-op' => [ [
211
				'aliases' => new Diff( [
212
					'de' => new Diff( [ new DiffOpRemove( 'original' ) ] ),
213
					'en' => new Diff( [ new DiffOpRemove( 'original' ) ] ),
214
				], true ),
215
			] ],
216
			'removing missing aliases is no-op (atomic)' => [ [
217
				'aliases' => new Diff( [
218
					'de' => new DiffOpRemove( [ 'original' ] ),
219
					'en' => new DiffOpRemove( [ 'original' ] ),
220
				], true ),
221
			] ],
222
		];
223
	}
224
225
	/**
226
	 * @dataProvider conflictingEditProvider
227
	 */
228
	public function testGivenConflictingEdit_fingerprintIsReturnedAsIs( array $diffOps ) {
229
		$fingerprint = new Fingerprint();
230
		$fingerprint->setLabel( 'en', 'conflict' );
231
		$fingerprint->setDescription( 'en', 'conflict' );
232
		$fingerprint->setAliasGroup( 'en', [ 'conflict' ] );
233
234
		$patch = new EntityDiff( $diffOps );
235
236
		$this->assertFingerprintResultsFromPatch( $fingerprint, $fingerprint, $patch );
237
	}
238
239
}
240