ChangeNotificationFilterTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 479
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 479
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 1
A tearDown() 0 3 1
A testCanConstruct() 0 7 1
A testTryToGetEventRecordOnNullType() 0 11 1
B testGetEventRecordOnPropertyChange() 0 77 1
A testCannotNotifyOnChangeWhen_MDAT_REDI() 0 46 1
B testhasChangeToNotifyAbout_ChangeNotificationForAnyValue() 0 65 1
B testhasChangeToNotifyAbout_ChangeNotificationForDistinctValue() 0 72 1
B testhasChangeToNotifyAbout_ChangeNotificationForPageValue() 0 68 1
B testhasChangeToNotifyAbout_ChangeNotificationForSubobjectRelatedValue() 0 98 1
1
<?php
2
3
namespace SMW\Notifications\Tests\ChangeNotification;
4
5
use SMW\Notifications\ChangeNotification\ChangeNotificationFilter;
6
use SMW\DIWikiPage;
7
use SMWDIBlob as DIBlob;
8
use SMW\Tests\TestEnvironment;
9
10
/**
11
 * @covers \SMW\Notifications\ChangeNotification\ChangeNotificationFilter
12
 * @group semantic-notifications
13
 *
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class ChangeNotificationFilterTest extends \PHPUnit_Framework_TestCase {
20
21
	private $store;
22
	private $propertySpecificationLookup;
23
	private $testEnvironment;
24
25
	protected function setUp() {
26
27
		$this->store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' )
28
			->disableOriginalConstructor()
29
			->getMock();
30
31
		$this->propertySpecificationLookup = $this->getMockBuilder( '\SMW\PropertySpecificationLookup' )
32
			->disableOriginalConstructor()
33
			->getMock();
34
35
		$this->testEnvironment = new TestEnvironment();
36
37
		$this->testEnvironment->registerObject( 'Store', $this->store );
38
		$this->testEnvironment->registerObject( 'PropertySpecificationLookup', $this->propertySpecificationLookup );
39
	}
40
41
	protected function tearDown() {
42
		$this->testEnvironment->tearDown();
43
	}
44
45
	public function testCanConstruct() {
46
47
		$this->assertInstanceOf(
48
			ChangeNotificationFilter::class,
49
			new ChangeNotificationFilter( DIWikiPage::newFromText( __METHOD__ ), $this->store )
50
		);
51
	}
52
53
	public function testTryToGetEventRecordOnNullType() {
54
55
		$instance = new ChangeNotificationFilter(
56
			DIWikiPage::newFromText( __METHOD__ ),
57
			$this->store
58
		);
59
60
		$this->assertEmpty(
61
			$instance->getEventRecord( true )
62
		);
63
	}
64
65
	public function testGetEventRecordOnPropertyChange() {
66
67
		$subject = DIWikiPage::newFromText( __METHOD__, SMW_NS_PROPERTY );
68
		$dataItem = DIWikiPage::newFromText( 'FOO', SMW_NS_PROPERTY );
69
70
		$orderedDiffByTable = array(
71
			'fpt_foo' => array(
72
				'property' => array(
73
					'key'  => '_FOO',
74
					'p_id' => 29
75
				),
76
				'insert' => array(
77
					array(
78
						's_id' => 201,
79
						'o_serialized' => '1/2016/6/1/11/1/48/0',
80
						'o_sortkey' => '2457540.9595833'
81
					)
82
				),
83
				'delete' => array(
84
					array(
85
						's_id' => 202,
86
						'o_serialized' => '1/2016/6/1/11/1/59/0',
87
						'o_sortkey' => '2457540.9582292'
88
					)
89
				)
90
			)
91
		);
92
93
		$idTable = $this->getMockBuilder( '\stdClass' )
94
			->disableOriginalConstructor()
95
			->setMethods( array( 'getDataItemById' ) )
96
			->getMock();
97
98
		$idTable->expects( $this->any() )
99
			->method( 'getDataItemById' )
100
			->will( $this->returnValue( $dataItem ) );
101
102
		$this->store->expects( $this->any() )
103
			->method( 'getObjectIds' )
104
			->will( $this->returnValue( $idTable ) );
105
106
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
107
			->disableOriginalConstructor()
108
			->setMethods( array( 'getOrderedDiffByTable' ) )
109
			->getMock();
110
111
		$compositePropertyTableDiffIterator->expects( $this->any() )
112
			->method( 'getOrderedDiffByTable' )
113
			->will( $this->returnValue( $orderedDiffByTable ) );
114
115
		$instance = new ChangeNotificationFilter(
116
			$subject,
117
			$this->store
118
		);
119
120
		$instance->hasChangeToNotifyAbout(
121
			$compositePropertyTableDiffIterator
122
		);
123
124
		$this->assertEquals(
125
			array(
126
				'agent' => null,
127
				'extra' => array(
128
					'notifyAgent' => false,
129
					'revid' => 0,
130
					'properties' => array(
131
						'FOO#102##' => $dataItem
132
					),
133
					'subSemanticDataMatch' => array(),
134
					'subject' => $subject
135
				),
136
				'title' => $subject->getTitle(),
137
				'type' => 'smw-specification-change'
138
			),
139
			$instance->getEventRecord( true )
140
		);
141
	}
142
143
	public function testCannotNotifyOnChangeWhen_MDAT_REDI() {
144
145
		$subject = DIWikiPage::newFromText( __METHOD__ );
146
147
		$orderedDiffByTable = array(
148
			'fpt_mdat' => array(
149
				'property' => array(
150
					'key'  => '_MDAT',
151
					'p_id' => 29
152
				)
153
			),
154
			'fpt_redi' => array(
155
				'property' => array(
156
					'key'  => '_REDI',
157
					'p_id' => 290
158
				)
159
			)
160
		);
161
162
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
163
			->disableOriginalConstructor()
164
			->setMethods( array( 'getOrderedDiffByTable' ) )
165
			->getMock();
166
167
		$compositePropertyTableDiffIterator->expects( $this->any() )
168
			->method( 'getOrderedDiffByTable' )
169
			->will( $this->returnValue( $orderedDiffByTable ) );
170
171
		$instance = new ChangeNotificationFilter(
172
			$subject,
173
			$this->store
174
		);
175
176
		$instance->setPropertyExemptionList( array(
177
			'_MDAT',
178
			'_REDI'
179
		) );
180
181
		$result = $instance->hasChangeToNotifyAbout(
182
			$compositePropertyTableDiffIterator
183
		);
184
185
		$this->assertFalse(
186
			$result
187
		);
188
	}
189
190
	public function testhasChangeToNotifyAbout_ChangeNotificationForAnyValue() {
191
192
		$subject = DIWikiPage::newFromText( __METHOD__ );
193
194
		$orderedDiffByTable = array(
195
			'fpt_foo' => array(
196
				'property' => array(
197
					'key'  => '_FOO',
198
					'p_id' => 29
199
				),
200
				'insert' => array(
201
					array(
202
						's_id' => 201,
203
						'o_serialized' => '1/2016/6/1/11/1/48/0',
204
						'o_sortkey' => '2457540.9595833'
205
					)
206
				)
207
			)
208
		);
209
210
		$idTable = $this->getMockBuilder( '\stdClass' )
211
			->disableOriginalConstructor()
212
			->setMethods( array( 'getDataItemById' ) )
213
			->getMock();
214
215
		$idTable->expects( $this->any() )
216
			->method( 'getDataItemById' )
217
			->will( $this->returnValue( DIWikiPage::newFromText( 'FOO', SMW_NS_PROPERTY ) ) );
218
219
		$this->store->expects( $this->any() )
220
			->method( 'getObjectIds' )
221
			->will( $this->returnValue( $idTable ) );
222
223
		$this->propertySpecificationLookup->expects( $this->any() )
224
			->method( 'getSpecification' )
225
			->will( $this->returnValue( array( new DIBlob( '+' ) ) ) ); //Any value
226
227
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
228
			->disableOriginalConstructor()
229
			->setMethods( array( 'getOrderedDiffByTable' ) )
230
			->getMock();
231
232
		$compositePropertyTableDiffIterator->expects( $this->any() )
233
			->method( 'getOrderedDiffByTable' )
234
			->will( $this->returnValue( $orderedDiffByTable ) );
235
236
		$agent = $this->getMockBuilder( '\User' )
237
			->disableOriginalConstructor()
238
			->getMock();
239
240
		$instance = new ChangeNotificationFilter(
241
			$subject,
242
			$this->store
243
		);
244
245
		$instance->setAgent( $agent );
246
247
		$result = $instance->hasChangeToNotifyAbout(
248
			$compositePropertyTableDiffIterator
249
		);
250
251
		$this->assertTrue(
252
			$result
253
		);
254
	}
255
256
	public function testhasChangeToNotifyAbout_ChangeNotificationForDistinctValue() {
257
258
		$subject = DIWikiPage::newFromText( __METHOD__ );
259
260
		$orderedDiffByTable = array(
261
			'fpt_date' => array(
262
				'property' => array(
263
					'key'  => '_TEXT',
264
					'p_id' => 29
265
				),
266
				'insert' => array(
267
					array(
268
						's_id' => 201,
269
						'o_blob' => 'DistinctText',
270
						'o_hash' => ''
271
					)
272
				),
273
				'delete' => array(
274
					array(
275
						's_id' => 201,
276
						'o_blob' => 'Text',
277
						'o_hash' => ''
278
					)
279
				)
280
			)
281
		);
282
283
		$idTable = $this->getMockBuilder( '\stdClass' )
284
			->disableOriginalConstructor()
285
			->setMethods( array( 'getDataItemById' ) )
286
			->getMock();
287
288
		$idTable->expects( $this->any() )
289
			->method( 'getDataItemById' )
290
			->will( $this->returnValue( DIWikiPage::newFromText( '_TEXT', SMW_NS_PROPERTY ) ) );
291
292
		$this->store->expects( $this->any() )
293
			->method( 'getObjectIds' )
294
			->will( $this->returnValue( $idTable ) );
295
296
		$this->propertySpecificationLookup->expects( $this->any() )
297
			->method( 'getSpecification' )
298
			->will( $this->returnValue( array( new DIBlob( 'DistinctText' ) ) ) ); //Distinct value
299
300
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
301
			->disableOriginalConstructor()
302
			->setMethods( array( 'getOrderedDiffByTable' ) )
303
			->getMock();
304
305
		$compositePropertyTableDiffIterator->expects( $this->any() )
306
			->method( 'getOrderedDiffByTable' )
307
			->will( $this->returnValue( $orderedDiffByTable ) );
308
309
		$agent = $this->getMockBuilder( '\User' )
310
			->disableOriginalConstructor()
311
			->getMock();
312
313
		$instance = new ChangeNotificationFilter(
314
			$subject,
315
			$this->store
316
		);
317
318
		$instance->setAgent( $agent );
319
320
		$result = $instance->hasChangeToNotifyAbout(
321
			$compositePropertyTableDiffIterator
322
		);
323
324
		$this->assertTrue(
325
			$result
326
		);
327
	}
328
329
	public function testhasChangeToNotifyAbout_ChangeNotificationForPageValue() {
330
331
		$subject = DIWikiPage::newFromText( __METHOD__ );
332
333
		$orderedDiffByTable = array(
334
			'tab_id_page' => array(
335
				'insert' => array(
336
					array(
337
						'p_id' => 29,
338
						's_id' => 201,
339
						'o_id' => 1001
340
					)
341
				),
342
				'delete' => array(
343
					array(
344
						'p_id' => 29,
345
						's_id' => 201,
346
						'o_id' => 42
347
					)
348
				)
349
			)
350
		);
351
352
		$idTable = $this->getMockBuilder( '\stdClass' )
353
			->disableOriginalConstructor()
354
			->setMethods( array( 'getDataItemById' ) )
355
			->getMock();
356
357
		$idTable->expects( $this->any() )
358
			->method( 'getDataItemById' )
359
			->will( $this->returnValue( DIWikiPage::newFromText( 'BAR', SMW_NS_PROPERTY ) ) );
360
361
		$this->store->expects( $this->any() )
362
			->method( 'getObjectIds' )
363
			->will( $this->returnValue( $idTable ) );
364
365
		$this->propertySpecificationLookup->expects( $this->any() )
366
			->method( 'getSpecification' )
367
			->will( $this->returnValue( array( new DIBlob( 'BAR' ) ) ) ); //Distinct value
368
369
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
370
			->disableOriginalConstructor()
371
			->setMethods( array( 'getOrderedDiffByTable' ) )
372
			->getMock();
373
374
		$compositePropertyTableDiffIterator->expects( $this->any() )
375
			->method( 'getOrderedDiffByTable' )
376
			->will( $this->returnValue( $orderedDiffByTable ) );
377
378
		$agent = $this->getMockBuilder( '\User' )
379
			->disableOriginalConstructor()
380
			->getMock();
381
382
		$instance = new ChangeNotificationFilter(
383
			$subject,
384
			$this->store
385
		);
386
387
		$instance->setAgent( $agent );
388
389
		$result = $instance->hasChangeToNotifyAbout(
390
			$compositePropertyTableDiffIterator
391
		);
392
393
		$this->assertTrue(
394
			$result
395
		);
396
	}
397
398
	public function testhasChangeToNotifyAbout_ChangeNotificationForSubobjectRelatedValue() {
399
400
		$subject = DIWikiPage::newFromText( __METHOD__ );
401
		$dataItem = DIWikiPage::newFromText( 'BAR', SMW_NS_PROPERTY );
402
403
		$orderedDiffByTable = array(
404
			'tab_id_page' => array(
405
				'insert' => array(
406
					array(
407
						'p_id' => 29,
408
						's_id' => 201,
409
						'o_id' => 1001
410
					)
411
				),
412
				'delete' => array(
413
					array(
414
						'p_id' => 29,
415
						's_id' => 201,
416
						'o_id' => 42
417
					)
418
				)
419
			)
420
		);
421
422
		$subSemanticData = $this->getMockBuilder( '\SMW\SemanticData' )
423
			->disableOriginalConstructor()
424
			->getMock();
425
426
		$subSemanticData->expects( $this->any() )
427
			->method( 'hasProperty' )
428
			->will( $this->returnValue( true ) );
429
430
		$subSemanticData->expects( $this->any() )
431
			->method( 'getSubject' )
432
			->will( $this->returnValue( new DIWikiPage( 'BAR', SMW_NS_PROPERTY, '', 'ooooooo' ) ) );
433
434
		$subSemanticData->expects( $this->any() )
435
			->method( 'getPropertyValues' )
436
			->will( $this->returnValue( array( new DIBlob( 'BAR' ) ) ) ); //Distinct value
437
438
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
439
			->disableOriginalConstructor()
440
			->getMock();
441
442
		$semanticData->expects( $this->any() )
443
			->method( 'getSubSemanticData' )
444
			->will( $this->returnValue( array( $subSemanticData ) ) );
445
446
		$idTable = $this->getMockBuilder( '\stdClass' )
447
			->disableOriginalConstructor()
448
			->setMethods( array( 'getDataItemById' ) )
449
			->getMock();
450
451
		$idTable->expects( $this->any() )
452
			->method( 'getDataItemById' )
453
			->will( $this->returnValue( $dataItem ) );
454
455
		$this->store->expects( $this->any() )
456
			->method( 'getObjectIds' )
457
			->will( $this->returnValue( $idTable ) );
458
459
		$this->store->expects( $this->any() )
460
			->method( 'getSemanticData' )
461
			->with( $this->equalTo( $dataItem ) )
462
			->will( $this->returnValue( $semanticData ) );
463
464
		$this->propertySpecificationLookup->expects( $this->any() )
465
			->method( 'getSpecification' )
466
			->will( $this->returnValue( array() ) );
467
468
		$compositePropertyTableDiffIterator = $this->getMockBuilder( '\SMW\SQLStore\CompositePropertyTableDiffIterator' )
469
			->disableOriginalConstructor()
470
			->setMethods( array( 'getOrderedDiffByTable' ) )
471
			->getMock();
472
473
		$compositePropertyTableDiffIterator->expects( $this->any() )
474
			->method( 'getOrderedDiffByTable' )
475
			->will( $this->returnValue( $orderedDiffByTable ) );
476
477
		$agent = $this->getMockBuilder( '\User' )
478
			->disableOriginalConstructor()
479
			->getMock();
480
481
		$instance = new ChangeNotificationFilter(
482
			$subject,
483
			$this->store
484
		);
485
486
		$instance->setAgent( $agent );
487
488
		$result = $instance->hasChangeToNotifyAbout(
489
			$compositePropertyTableDiffIterator
490
		);
491
492
		$this->assertTrue(
493
			$result
494
		);
495
	}
496
497
}
498