Completed
Push — master ( 9e1461...cdd4b1 )
by
unknown
13:17
created

testFindNoLinkReferencesForTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SIL\Tests;
4
5
use SIL\InterlanguageLinksLookup;
6
use SIL\InterlanguageLink;
7
use SMW\DataValueFactory;
8
use SMW\DIWikiPage;
9
use SMW\PropertyRegistry;
10
use SMWDIBlob as DIBlob;
11
use Title;
12
13
/**
14
 * @covers \SIL\InterlanguageLinksLookup
15
 * @group semantic-interlanguage-links
16
 *
17
 * @license GNU GPL v2+
18
 * @since 1.0
19
 *
20
 * @author mwjames
21
 */
22
class InterlanguageLinksLookupTest extends \PHPUnit_Framework_TestCase {
23
24
	private $store;
25
26
	protected function setUp() {
27
		parent::setUp();
28
29
		$this->store = $this->getMockBuilder( '\SMW\Store' )
30
			->disableOriginalConstructor()
31
			->getMockForAbstractClass();
32
	}
33
34
	public function testCanConstruct() {
35
36
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
37
			->disableOriginalConstructor()
38
			->getMock();
39
40
		$this->assertInstanceOf(
41
			'\SIL\InterlanguageLinksLookup',
42
			new InterlanguageLinksLookup( $languageTargetLinksCache )
43
		);
44
	}
45
46
	public function testRedirectTargetFor() {
47
48
		$title = Title::newFromText( __METHOD__ );
49
50
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
51
			->disableOriginalConstructor()
52
			->getMock();
53
54
		$store = $this->getMockBuilder( '\SMW\Store' )
55
			->disableOriginalConstructor()
56
			->setMethods( array( 'getRedirectTarget' ) )
57
			->getMockForAbstractClass();
58
59
		$store->expects( $this->once() )
60
			->method( 'getRedirectTarget' )
61
			->with( $this->equalTo( DIWikiPage::newFromTitle( $title ) ) )
62
			->will( $this->returnValue( DIWikiPage::newFromTitle( $title ) ) );
63
64
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
65
		$instance->setStore( $store );
66
67
		$this->assertEquals(
68
			$title,
69
			$instance->getRedirectTargetFor( $title )
70
		);
71
	}
72
73
	public function testFindValidPageLanguageForTarget() {
74
75
		$title = Title::newFromText( __METHOD__ );
76
77
		$verifyPropertyTypeId = function( $property ) {
78
			return $property->findPropertyTypeID() === '_txt';
79
		};
80
81
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
82
			->disableOriginalConstructor()
83
			->getMock();
84
85
		$store = $this->getMockBuilder( '\SMW\Store' )
86
			->disableOriginalConstructor()
87
			->getMockForAbstractClass();
88
89
		$store->expects( $this->at( 0 ) )
90
			->method( 'getPropertyValues' )
91
			->with( $this->equalTo( DIWikiPage::newFromTitle( $title ) ) )
92
			->will( $this->returnValue( array( new DIWikiPage( 'Foo', NS_MAIN ) ) ) );
93
94
		$store->expects( $this->at( 1 ) )
95
			->method( 'getPropertyValues' )
96
			->with(
97
				$this->equalTo( new DIWikiPage( 'Foo', NS_MAIN ) ),
98
				$this->callback( $verifyPropertyTypeId ) )
99
			->will( $this->returnValue( array( new DIBlob( 'en' ), new DIBlob( 'ja' ) ) ) );
100
101
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
102
		$instance->setStore( $store );
103
104
		$this->assertEquals(
105
			'ja',
106
			$instance->findPageLanguageForTarget( $title )
107
		);
108
	}
109
110
	public function testInvalidValueToFindNoPageLanguageForTarget() {
111
112
		$title = Title::newFromText( __METHOD__ );
113
114
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
115
			->disableOriginalConstructor()
116
			->getMock();
117
118
		$store = $this->getMockBuilder( '\SMW\Store' )
119
			->disableOriginalConstructor()
120
			->getMockForAbstractClass();
121
122
		$store->expects( $this->at( 0 ) )
123
			->method( 'getPropertyValues' )
124
			->with( $this->equalTo( DIWikiPage::newFromTitle( $title ) ) )
125
			->will( $this->returnValue( array( new DIWikiPage( 'Foo', NS_MAIN ) ) ) );
126
127
		$store->expects( $this->at( 1 ) )
128
			->method( 'getPropertyValues' )
129
			->with( $this->equalTo( new DIWikiPage( 'Foo', NS_MAIN ) ) )
130
			->will( $this->returnValue( array( new DIWikiPage( 'invalid', NS_MAIN ) ) ) );
131
132
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
133
		$instance->setStore( $store );
134
135
		$this->assertEquals(
136
			InterlanguageLinksLookup::NO_LANG,
137
			$instance->findPageLanguageForTarget( $title )
138
		);
139
	}
140
141
	public function testFindNoPageLanguageForTarget() {
142
143
		$title = Title::newFromText( __METHOD__ );
144
145
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
146
			->disableOriginalConstructor()
147
			->getMock();
148
149
		$store = $this->getMockBuilder( '\SMW\Store' )
150
			->disableOriginalConstructor()
151
			->getMockForAbstractClass();
152
153
		$store->expects( $this->at( 0 ) )
154
			->method( 'getPropertyValues' )
155
			->with( $this->equalTo( DIWikiPage::newFromTitle( $title ) ) )
156
			->will( $this->returnValue( array() ) );
157
158
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
159
		$instance->setStore( $store );
160
161
		$this->assertEquals(
162
			InterlanguageLinksLookup::NO_LANG,
163
			$instance->findPageLanguageForTarget( $title )
164
		);
165
	}
166
167
	public function testFindPageLanguageForTargetFromCache() {
168
169
		$target = Title::newFromText( __METHOD__ );
170
171
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
172
			->disableOriginalConstructor()
173
			->getMock();
174
175
		$languageTargetLinksCache->expects( $this->once() )
176
			->method( 'getPageLanguageFromCache' )
177
			->with( $this->equalTo( $target ) )
178
			->will( $this->returnValue( 'foo' ) );
179
180
		$store = $this->getMockBuilder( '\SMW\Store' )
181
			->disableOriginalConstructor()
182
			->getMockForAbstractClass();
183
184
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
185
		$instance->setStore( $store );
186
187
		$this->assertEquals(
188
			'foo',
189
			$instance->findPageLanguageForTarget( $target )
190
		);
191
	}
192
193
	public function testFindFullListOfReferenceTargetLinksSpecificTarget() {
194
195
		$title = Title::newFromText( __METHOD__ );
196
197
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
198
			->disableOriginalConstructor()
199
			->getMock();
200
201
		$store = $this->getMockBuilder( '\SMW\Store' )
202
			->disableOriginalConstructor()
203
			->getMockForAbstractClass();
204
205
		$store->expects( $this->at( 0 ) )
206
			->method( 'getPropertyValues' )
207
			->with( $this->equalTo( DIWikiPage::newFromTitle( $title ) ) )
208
			->will( $this->returnValue( array( new DIWikiPage( 'Foo', NS_MAIN ) ) ) );
209
210
		$store->expects( $this->at( 1 ) )
211
			->method( 'getPropertyValues' )
212
			->with( $this->equalTo( new DIWikiPage( 'Foo', NS_MAIN ) ) )
213
			->will( $this->returnValue( array( new DIWikiPage( 'Bar', NS_MAIN ) ) ) );
214
215
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
216
		$instance->setStore( $store );
217
218
		$this->assertEquals(
219
			array( new DIWikiPage( 'Bar', NS_MAIN ) ),
220
			$instance->findFullListOfReferenceTargetLinks( $title )
221
		);
222
	}
223
224
	public function testFindNoLinkReferencesForTarget() {
225
226
		$title = Title::newFromText( __METHOD__ );
227
228
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
229
			->disableOriginalConstructor()
230
			->getMock();
231
232
		$store = $this->getMockBuilder( '\SMW\Store' )
233
			->disableOriginalConstructor()
234
			->getMockForAbstractClass();
235
236
		$store->expects( $this->at( 0 ) )
237
			->method( 'getPropertyValues' )
238
			->with( $this->equalTo( DIWikiPage::newFromTitle( $title ) ) )
239
			->will( $this->returnValue( array() ) );
240
241
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
242
		$instance->setStore( $store );
243
244
		$this->assertEmpty(
245
			$instance->findFullListOfReferenceTargetLinks( $title )
246
		);
247
	}
248
249
	public function testVerifyQueryStringByQueryingLanguageTargetLinks() {
250
251
		$interlanguageLink = new InterlanguageLink( 'en', 'Foo' );
252
253
		$verifyQueryCallback = function( $query ) {
254
			return $query->getQueryString() === '[[Interlanguage reference::Foo]]';
255
		};
256
257
		$queryResult = $this->getMockBuilder( '\SMWQueryResult' )
258
			->disableOriginalConstructor()
259
			->getMock();
260
261
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
262
			->disableOriginalConstructor()
263
			->getMock();
264
265
		$store = $this->getMockBuilder( '\SMW\Store' )
266
			->disableOriginalConstructor()
267
			->getMockForAbstractClass();
268
269
		$store->expects( $this->once() )
270
			->method( 'getQueryResult' )
271
			->with( $this->callback( $verifyQueryCallback ) )
272
			->will( $this->returnValue( $queryResult ) );
273
274
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
275
		$instance->setStore( $store );
276
277
		$instance->queryLanguageTargetLinks( $interlanguageLink );
278
	}
279
280
	public function testQueryLanguageTargetLinks() {
281
282
		$interlanguageLink = new InterlanguageLink( 'en', 'Foo' );
283
284
		$blobValue = DataValueFactory::getInstance()->newDataValueByType( '_txt' );
285
		$blobValue->setUserValue( 'vi' );
286
287
		$resultArray = $this->getMockBuilder( '\SMWResultArray' )
288
			->disableOriginalConstructor()
289
			->getMock();
290
291
		$resultArray->expects( $this->any() )
292
			->method( 'getNextDataValue' )
293
			->will( $this->onConsecutiveCalls( $blobValue, false )  );
294
295
		$resultArray->expects( $this->any() )
296
			->method( 'getResultSubject' )
297
			->will( $this->returnValue( new DIWikiPage( 'Bar', NS_MAIN ) ) );
298
299
		$queryResult = $this->getMockBuilder( '\SMWQueryResult' )
300
			->disableOriginalConstructor()
301
			->getMock();
302
303
		$queryResult->expects( $this->any() )
304
			->method( 'getNext' )
305
			->will( $this->onConsecutiveCalls(
306
				array( $resultArray ),
307
				array( $resultArray ),
308
				false ) );
309
310
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
311
			->disableOriginalConstructor()
312
			->getMock();
313
314
		$store = $this->getMockBuilder( '\SMW\Store' )
315
			->disableOriginalConstructor()
316
			->getMockForAbstractClass();
317
318
		$store->expects( $this->once() )
319
			->method( 'getQueryResult' )
320
			->will( $this->returnValue( $queryResult ) );
321
322
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
323
		$instance->setStore( $store );
324
325
		$instance->queryLanguageTargetLinks( $interlanguageLink );
326
	}
327
328
	public function testQueryLanguageTargetLinksContainsCurrentTargetOnly() {
329
330
		$currentTarget = Title::newFromText( 'Bar' );
331
		$interlanguageLink = new InterlanguageLink( 'en', 'Foo' );
332
333
		$queryResult = $this->getMockBuilder( '\SMWQueryResult' )
334
			->disableOriginalConstructor()
335
			->getMock();
336
337
		$queryResult->expects( $this->any() )
338
			->method( 'getNext' )
339
			->will( $this->onConsecutiveCalls( false ) );
340
341
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
342
			->disableOriginalConstructor()
343
			->getMock();
344
345
		$store = $this->getMockBuilder( '\SMW\Store' )
346
			->disableOriginalConstructor()
347
			->getMockForAbstractClass();
348
349
		$store->expects( $this->once() )
350
			->method( 'getQueryResult' )
351
			->will( $this->returnValue( $queryResult ) );
352
353
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
354
		$instance->setStore( $store );
355
356
		$expected = array(
357
			'en' => 'Bar'
358
		);
359
360
		$this->assertEquals(
361
			$expected,
362
			$instance->queryLanguageTargetLinks( $interlanguageLink, $currentTarget )
363
		);
364
	}
365
366
	public function testGetLanguageTargetLinksFromCache() {
367
368
		$interlanguageLink = new InterlanguageLink( 'en', 'Foo' );
369
370
		$languageTargetLinks = array(
371
			'bo' => 'Bar',
372
			'en' => Title::newFromText( 'Foo' )
373
		);
374
375
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
376
			->disableOriginalConstructor()
377
			->getMock();
378
379
		$languageTargetLinksCache->expects( $this->once() )
380
			->method( 'getLanguageTargetLinksFromCache' )
381
			->with( $this->equalTo( $interlanguageLink ) )
382
			->will( $this->returnValue( $languageTargetLinks ) );
383
384
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
385
		$instance->queryLanguageTargetLinks( $interlanguageLink );
386
	}
387
388
	public function testTryCachedPageLanguageForTarget() {
389
390
		$target = Title::newFromText( 'Foo' );
391
392
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
393
			->disableOriginalConstructor()
394
			->getMock();
395
396
		$languageTargetLinksCache->expects( $this->once() )
397
			->method( 'getPageLanguageFromCache' )
398
			->with( $this->equalTo( $target ) )
399
			->will( $this->returnValue( 'en' ) );
400
401
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
402
403
		$this->assertEquals(
404
			'en',
405
			$instance->findPageLanguageForTarget( $target )
406
		);
407
	}
408
/*
409
	public function testTryFalseCachedPageLanguageForTarget() {
410
411
		$target = Title::newFromText( 'Foo' );
412
413
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
414
			->disableOriginalConstructor()
415
			->getMock();
416
417
		$languageTargetLinksCache->expects( $this->once() )
418
			->method( 'getPageLanguageFromCache' )
419
			->with( $this->equalTo( $target ) )
420
			->will( $this->returnValue( false ) );
421
422
		$languageTargetLinksCache->expects( $this->once() )
423
			->method( 'pushPageLanguageToCache' );
424
425
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
426
		$instance->findPageLanguageForTarget( $target );
427
	}
428
*/
429
430
	public function testInvalidateCachedLanguageTargetLinks() {
431
432
		$target = Title::newFromText( 'Foo' );
433
434
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
435
			->disableOriginalConstructor()
436
			->getMock();
437
438
		$languageTargetLinksCache->expects( $this->once() )
439
			->method( 'deleteLanguageTargetLinksFromCache' );
440
441
		$languageTargetLinksCache->expects( $this->once() )
442
			->method( 'deletePageLanguageForTargetFromCache' )
443
			->with( $this->equalTo( $target ) );
444
445
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
446
		$instance->setStore( $this->store );
447
448
		$instance->resetLookupCacheBy( $target );
449
	}
450
451
	public function testTryLookupForUngregisteredProperty() {
452
453
		PropertyRegistry::clear();
454
455
		$target = Title::newFromText( 'Foo' );
456
457
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
458
			->disableOriginalConstructor()
459
			->getMock();
460
461
		$languageTargetLinksCache->expects( $this->atLeastOnce() )
462
			->method( 'getPageLanguageFromCache' );
463
464
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
465
		$instance->setStore( $this->store );
466
467
		$this->assertEquals(
468
			InterlanguageLinksLookup::NO_LANG,
469
			$instance->findPageLanguageForTarget( $target )
470
		);
471
472
		PropertyRegistry::clear();
473
	}
474
475
	public function testTryFindListOfReferenceTargetLinksForUngregisteredProperty() {
476
477
		PropertyRegistry::clear();
478
479
		$target = Title::newFromText( 'Foo' );
480
481
		$languageTargetLinksCache = $this->getMockBuilder( '\SIL\LanguageTargetLinksCache' )
482
			->disableOriginalConstructor()
483
			->getMock();
484
485
		$instance = new InterlanguageLinksLookup( $languageTargetLinksCache );
486
		$instance->setStore( $this->store );
487
488
		$this->assertEmpty(
489
			$instance->findFullListOfReferenceTargetLinks( $target )
490
		);
491
492
		PropertyRegistry::clear();
493
	}
494
495
}
496