Completed
Push — master ( d7c46f...dfbbd6 )
by
unknown
26s
created

testGivenConflictingEdit_fingerprintIsReturnedAsIs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 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
 * @licence GNU GPL v2+
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' ) ) ),
89
				'en' => new Diff( array( new DiffOpRemove( 'en-old' ), new DiffOpAdd( 'en-new' ) ) ),
90
				'fa' => new Diff( array( new DiffOpRemove( 'fa-old' ) ) ),
91
			) ),
92
			'diffs containing atomic ops' => array( array(
93
				'de' => new Diff( array( new DiffOpAdd( 'foo' ) ) ),
94
				'en' => new Diff( array( new DiffOpChange( 'en-old', 'en-new' ) ) ),
95
				'fa' => new Diff( array( new DiffOpRemove( 'fa-old' ) ) ),
96
			) ),
97
			'atomic diff ops' => array( array(
98
				'de' => new DiffOpAdd( array( 'foo' ) ),
99
				'en' => new DiffOpChange( array( 'en-old' ), array( 'en-new' ) ),
100
				'fa' => new DiffOpRemove( array( 'fa-old' ) ),
101
			) ),
102
		);
103
	}
104
105
	/**
106
	 * @dataProvider aliasDiffProvider
107
	 */
108
	public function testAliasDiffOnlyAffectsAliases( array $diffOps ) {
109
		$fingerprint = $this->newSimpleFingerprint();
110
		$fingerprint->setAliasGroup( 'en', array( 'en-old' ) );
111
		$fingerprint->setAliasGroup( 'fa', array( 'fa-old' ) );
112
113
		$patch = new EntityDiff( array(
114
			'aliases' => new Diff( $diffOps ),
115
		) );
116
117
		$expectedFingerprint = $this->newSimpleFingerprint();
118
		$expectedFingerprint->setAliasGroup( 'de', array( 'foo' ) );
119
		$expectedFingerprint->setAliasGroup( 'en', array( 'en-new' ) );
120
121
		$this->assertFingerprintResultsFromPatch( $expectedFingerprint, $fingerprint, $patch );
122
	}
123
124
	public function conflictingEditProvider() {
125
		return array(
126
			'does not add existing label language' => array( array(
127
				'label' => new Diff( array(
128
					'en' => new DiffOpAdd( 'added' ),
129
				), true ),
130
			) ),
131
			'does not change modified label' => array( array(
132
				'label' => new Diff( array(
133
					'en' => new DiffOpChange( 'original', 'changed' ),
134
				), true ),
135
			) ),
136
			'does not change missing label' => array( array(
137
				'label' => new Diff( array(
138
					'de' => new DiffOpChange( 'original', 'changed' ),
139
				), true ),
140
			) ),
141
			'does not remove modified label' => array( array(
142
				'label' => new Diff( array(
143
					'en' => new DiffOpRemove( 'original' ),
144
				), true ),
145
			) ),
146
			'removing missing label is no-op' => array( array(
147
				'label' => new Diff( array(
148
					'de' => new DiffOpRemove( 'original' ),
149
				), true ),
150
			) ),
151
152
			'does not add existing description language' => array( array(
153
				'description' => new Diff( array(
154
					'en' => new DiffOpAdd( 'added' ),
155
				), true ),
156
			) ),
157
			'does not change modified description' => array( array(
158
				'description' => new Diff( array(
159
					'en' => new DiffOpChange( 'original', 'changed' ),
160
				), true ),
161
			) ),
162
			'changing missing description is no-op' => array( array(
163
				'description' => new Diff( array(
164
					'de' => new DiffOpChange( 'original', 'changed' ),
165
				), true ),
166
			) ),
167
			'does not remove modified description' => array( array(
168
				'description' => new Diff( array(
169
					'en' => new DiffOpRemove( 'original' ),
170
				), true ),
171
			) ),
172
			'removing missing description is no-op' => array( array(
173
				'description' => new Diff( array(
174
					'de' => new DiffOpRemove( 'original' ),
175
				), true ),
176
			) ),
177
178
			'does not add existing aliases language' => array( array(
179
				'aliases' => new Diff( array(
180
					'en' => new DiffOpAdd( array( 'added' ) ),
181
				), true ),
182
			) ),
183
			'does not change missing aliases language' => array( array(
184
				'aliases' => new Diff( array(
185
					'de' => new Diff( array( new DiffOpRemove( 'original' ), new DiffOpAdd( 'changed' ) ) ),
186
				), true ),
187
			) ),
188
			'changing missing aliases is no-op' => array( array(
189
				'aliases' => new Diff( array(
190
					'de' => new Diff( array( new DiffOpChange( 'original', 'changed' ) ) ),
191
					'en' => new Diff( array( new DiffOpChange( 'original', 'changed' ) ) ),
192
				), true ),
193
			) ),
194
			'changing missing aliases is no-op (atomic)' => array( array(
195
				'aliases' => new Diff( array(
196
					'de' => new DiffOpChange( array( 'original' ), array( 'changed' ) ),
197
					'en' => new DiffOpChange( array( 'original' ), array( 'changed' ) ),
198
				), true ),
199
			) ),
200
			'removing missing aliases is no-op' => array( array(
201
				'aliases' => new Diff( array(
202
					'de' => new Diff( array( new DiffOpRemove( 'original' ) ) ),
203
					'en' => new Diff( array( new DiffOpRemove( 'original' ) ) ),
204
				), true ),
205
			) ),
206
			'removing missing aliases is no-op (atomic)' => array( array(
207
				'aliases' => new Diff( array(
208
					'de' => new DiffOpRemove( array( 'original' ) ),
209
					'en' => new DiffOpRemove( array( 'original' ) ),
210
				), true ),
211
			) ),
212
		);
213
	}
214
215
	/**
216
	 * @dataProvider conflictingEditProvider
217
	 */
218
	public function testGivenConflictingEdit_fingerprintIsReturnedAsIs( array $diffOps ) {
219
		$fingerprint = new Fingerprint();
220
		$fingerprint->setLabel( 'en', 'conflict' );
221
		$fingerprint->setDescription( 'en', 'conflict' );
222
		$fingerprint->setAliasGroup( 'en', array( 'conflict' ) );
223
224
		$patch = new EntityDiff( $diffOps );
225
226
		$this->assertFingerprintResultsFromPatch( $fingerprint, $fingerprint, $patch );
227
	}
228
229
}
230