Completed
Push — master ( 6e25e9...31dda1 )
by
unknown
02:52
created

FingerprintPatcherTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 10
c 2
b 0
f 2
lcom 1
cbo 7
dl 0
loc 220
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testGivenEmptyDiff_fingerprintIsReturnedAsIs() 0 5 1
A newSimpleFingerprint() 0 9 1
A assertFingerprintResultsFromPatch() 0 3 1
A getPatchedFingerprint() 0 8 1
A testLabelDiffOnlyAffectsLabels() 0 16 1
A testDescriptionDiffOnlyAffectsDescriptions() 0 16 1
B aliasDiffProvider() 0 24 1
A testAliasDiffOnlyAffectsAliases() 0 15 1
B conflictingEditProvider() 0 95 1
A testGivenConflictingEdit_fingerprintIsReturnedAsIs() 0 10 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Tests\Diff\Internal;
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\Internal\FingerprintPatcher;
11
use Wikibase\DataModel\Term\Fingerprint;
12
13
/**
14
 * @covers Wikibase\DataModel\Services\Diff\Internal\FingerprintPatcher
15
 *
16
 * @license GPL-2.0+
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Thiemo Mättig
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', array( '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( array(
55
			'label' => new Diff( array(
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( array(
72
			'description' => new Diff( array(
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 array(
87
			'diffs containing add/remove ops (default)' => array( array(
88
				'de' => new Diff( array( new DiffOpAdd( 'foo' ) ), false ),
89
				'en' => new Diff( array( new DiffOpRemove( 'en-old' ), new DiffOpAdd( 'en-new' ) ), false ),
90
				'fa' => new Diff( array( new DiffOpRemove( 'fa-old' ) ), false ),
91
			) ),
92
			'associative diffs containing atomic ops' => array( array(
93
				'de' => new Diff( array( new DiffOpAdd( 'foo' ) ), true ),
94
				'en' => new Diff( array( new DiffOpChange( 'en-old', 'en-new' ) ), true ),
95
				'fa' => new Diff( array( new DiffOpRemove( 'fa-old' ) ), true ),
96
			) ),
97
			'partly associative diffs (auto-detected) containing atomic ops' => array( array(
98
				'de' => new Diff( array( new DiffOpAdd( 'foo' ) ) ),
99
				'en' => new Diff( array( new DiffOpChange( 'en-old', 'en-new' ) ) ),
100
				'fa' => new Diff( array( new DiffOpRemove( 'fa-old' ) ) ),
101
			) ),
102
			'atomic diff ops' => array( array(
103
				'de' => new DiffOpAdd( array( 'foo' ) ),
104
				'en' => new DiffOpChange( array( 'en-old' ), array( 'en-new' ) ),
105
				'fa' => new DiffOpRemove( array( 'fa-old' ) ),
106
			) ),
107
		);
108
	}
109
110
	/**
111
	 * @dataProvider aliasDiffProvider
112
	 */
113
	public function testAliasDiffOnlyAffectsAliases( array $diffOps ) {
114
		$fingerprint = $this->newSimpleFingerprint();
115
		$fingerprint->setAliasGroup( 'en', array( 'en-old' ) );
116
		$fingerprint->setAliasGroup( 'fa', array( 'fa-old' ) );
117
118
		$patch = new EntityDiff( array(
119
			'aliases' => new Diff( $diffOps, true ),
120
		) );
121
122
		$expectedFingerprint = $this->newSimpleFingerprint();
123
		$expectedFingerprint->setAliasGroup( 'de', array( 'foo' ) );
124
		$expectedFingerprint->setAliasGroup( 'en', array( 'en-new' ) );
125
126
		$this->assertFingerprintResultsFromPatch( $expectedFingerprint, $fingerprint, $patch );
127
	}
128
129
	public function conflictingEditProvider() {
130
		return array(
131
			'does not add existing label language' => array( array(
132
				'label' => new Diff( array(
133
					'en' => new DiffOpAdd( 'added' ),
134
				), true ),
135
			) ),
136
			'does not change modified label' => array( array(
137
				'label' => new Diff( array(
138
					'en' => new DiffOpChange( 'original', 'changed' ),
139
				), true ),
140
			) ),
141
			'does not change missing label' => array( array(
142
				'label' => new Diff( array(
143
					'de' => new DiffOpChange( 'original', 'changed' ),
144
				), true ),
145
			) ),
146
			'does not remove modified label' => array( array(
147
				'label' => new Diff( array(
148
					'en' => new DiffOpRemove( 'original' ),
149
				), true ),
150
			) ),
151
			'removing missing label is no-op' => array( array(
152
				'label' => new Diff( array(
153
					'de' => new DiffOpRemove( 'original' ),
154
				), true ),
155
			) ),
156
157
			'does not add existing description language' => array( array(
158
				'description' => new Diff( array(
159
					'en' => new DiffOpAdd( 'added' ),
160
				), true ),
161
			) ),
162
			'does not change modified description' => array( array(
163
				'description' => new Diff( array(
164
					'en' => new DiffOpChange( 'original', 'changed' ),
165
				), true ),
166
			) ),
167
			'changing missing description is no-op' => array( array(
168
				'description' => new Diff( array(
169
					'de' => new DiffOpChange( 'original', 'changed' ),
170
				), true ),
171
			) ),
172
			'does not remove modified description' => array( array(
173
				'description' => new Diff( array(
174
					'en' => new DiffOpRemove( 'original' ),
175
				), true ),
176
			) ),
177
			'removing missing description is no-op' => array( array(
178
				'description' => new Diff( array(
179
					'de' => new DiffOpRemove( 'original' ),
180
				), true ),
181
			) ),
182
183
			'does not add existing aliases language' => array( array(
184
				'aliases' => new Diff( array(
185
					'en' => new DiffOpAdd( array( 'added' ) ),
186
				), true ),
187
			) ),
188
			'does not change missing aliases language' => array( array(
189
				'aliases' => new Diff( array(
190
					'de' => new Diff( array( new DiffOpRemove( 'original' ), new DiffOpAdd( 'changed' ) ) ),
191
				), true ),
192
			) ),
193
			'changing missing aliases is no-op' => array( array(
194
				'aliases' => new Diff( array(
195
					'de' => new Diff( array( new DiffOpChange( 'original', 'changed' ) ), true ),
196
					'en' => new Diff( array( new DiffOpChange( 'original', 'changed' ) ), true ),
197
				), true ),
198
			) ),
199
			'non-associative aliases change is ignored' => array( array(
200
				'aliases' => new Diff( array(
201
					'en' => new Diff( array( new DiffOpChange( 'conflict', 'changed' ) ), false ),
202
				), true ),
203
			) ),
204
			'changing missing aliases is no-op (atomic)' => array( array(
205
				'aliases' => new Diff( array(
206
					'de' => new DiffOpChange( array( 'original' ), array( 'changed' ) ),
207
					'en' => new DiffOpChange( array( 'original' ), array( 'changed' ) ),
208
				), true ),
209
			) ),
210
			'removing missing aliases is no-op' => array( array(
211
				'aliases' => new Diff( array(
212
					'de' => new Diff( array( new DiffOpRemove( 'original' ) ) ),
213
					'en' => new Diff( array( new DiffOpRemove( 'original' ) ) ),
214
				), true ),
215
			) ),
216
			'removing missing aliases is no-op (atomic)' => array( array(
217
				'aliases' => new Diff( array(
218
					'de' => new DiffOpRemove( array( 'original' ) ),
219
					'en' => new DiffOpRemove( array( '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', array( 'conflict' ) );
233
234
		$patch = new EntityDiff( $diffOps );
235
236
		$this->assertFingerprintResultsFromPatch( $fingerprint, $fingerprint, $patch );
237
	}
238
239
}
240