Completed
Push — master ( 264630...c90943 )
by mw
230:08 queued 195:34
created

tests/phpunit/Unit/Factbox/FactboxTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW\Tests\Factbox;
4
5
use ParserOutput;
6
use ReflectionClass;
7
use SMW\DIProperty;
8
use SMW\DIWikiPage;
9
use SMW\Factbox\Factbox;
10
use SMW\ParserData;
11
use SMW\SemanticData;
12
use SMW\TableFormatter;
13
use SMW\Tests\TestEnvironment;
14
use Title;
15
16
/**
17
 * @covers \SMW\Factbox\Factbox
18
 * @group semantic-mediawiki
19
 *
20
 * @license GNU GPL v2+
21
 * @since 1.9
22
 *
23
 * @author mwjames
24
 */
25
class FactboxTest extends \PHPUnit_Framework_TestCase {
26
27
	private $stringValidator;
28
	private $testEnvironment;
29
30
	protected function setUp() {
31
		parent::setUp();
32
33
		$this->testEnvironment = new TestEnvironment();
34
		$this->stringValidator = $this->testEnvironment->getUtilityFactory()->newValidatorFactory()->newStringValidator();
35
36
		$this->testEnvironment->addConfiguration( 'smwgShowFactbox', SMW_FACTBOX_NONEMPTY );
37
	}
38
39
	protected function tearDown() {
40
		$this->testEnvironment->tearDown();
41
		parent::tearDown();
42
	}
43
44
	public function testCanConstruct() {
45
46
		$store = $this->getMockBuilder( '\SMW\Store' )
47
			->disableOriginalConstructor()
48
			->getMockForAbstractClass();
49
50
		$parserData = $this->getMockBuilder( '\SMW\ParserData' )
51
			->disableOriginalConstructor()
52
			->getMock();
53
54
		$messageBuilder = $this->getMockBuilder( '\SMW\MediaWiki\MessageBuilder' )
55
			->disableOriginalConstructor()
56
			->getMock();
57
58
		$this->assertInstanceOf(
59
			'\SMW\Factbox\Factbox',
60
			new Factbox( $store, $parserData, $messageBuilder )
61
		);
62
	}
63
64
	public function testGetContent() {
65
66
		$text = __METHOD__;
67
68
		$parserData = new ParserData(
69
			Title::newFromText( __METHOD__ ),
70
			new ParserOutput()
71
		);
72
73
		$store = $this->getMockBuilder( '\SMW\Store' )
74
			->disableOriginalConstructor()
75
			->getMockForAbstractClass();
76
77
		$messageBuilder = $this->getMockBuilder( '\SMW\MediaWiki\MessageBuilder' )
78
			->disableOriginalConstructor()
79
			->getMock();
80
81
		// Build Factbox stub object to encapsulate the method
82
		// without the need for other dependencies to occur
83
		$instance = $this->getMock( '\SMW\Factbox\Factbox',
84
			array( 'fetchContent', 'getMagicWords' ),
85
			array(
86
				$store,
87
				$parserData,
88
				$messageBuilder
89
			)
90
		);
91
92
		$instance->expects( $this->any() )
93
			->method( 'getMagicWords' )
94
			->will( $this->returnValue( 'Lula' ) );
95
96
		$instance->expects( $this->any() )
97
			->method( 'fetchContent' )
98
			->will( $this->returnValue( $text ) );
99
100
		$this->assertFalse( $instance->isVisible() );
101
102
		$instance->doBuild();
103
104
		$this->assertInternalType(
105
			'string',
106
			$instance->getContent()
107
		);
108
109
		$this->assertEquals(
110
			$text,
111
			$instance->getContent()
112
		);
113
114
		$this->assertTrue( $instance->isVisible() );
115
	}
116
117
	public function testGetContentRoundTripForNonEmptyContent() {
118
119
		$subject = DIWikiPage::newFromTitle( Title::newFromText( __METHOD__ ) );
120
121
		$this->testEnvironment->addConfiguration( 'smwgShowFactbox', SMW_FACTBOX_NONEMPTY );
122
123
		$store = $this->getMockBuilder( '\SMW\Store' )
124
			->disableOriginalConstructor()
125
			->getMockForAbstractClass();
126
127
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
128
			->disableOriginalConstructor()
129
			->getMock();
130
131
		$semanticData->expects( $this->any() )
132
			->method( 'getSubject' )
133
			->will( $this->returnValue( $subject ) );
134
135
		$semanticData->expects( $this->any() )
136
			->method( 'hasVisibleProperties' )
137
			->will( $this->returnValue( true ) );
138
139
		$semanticData->expects( $this->any() )
140
			->method( 'getPropertyValues' )
141
			->will( $this->returnValue( array( $subject ) ) );
142
143
		$semanticData->expects( $this->any() )
144
			->method( 'getProperties' )
145
			->will( $this->returnValue( array( DIProperty::newFromUserLabel( 'SomeFancyProperty' ) ) ) );
146
147
		$parserOutput = $this->setupParserOutput( $semanticData );
148
149
		$message = $this->getMockBuilder( '\Message' )
150
			->disableOriginalConstructor()
151
			->getMock();
152
153
		$message->expects( $this->any() )
154
			->method( 'inContentLanguage' )
155
			->will( $this->returnSelf() );
156
157
		$messageBuilder = $this->getMockBuilder( '\SMW\MediaWiki\MessageBuilder' )
158
			->disableOriginalConstructor()
159
			->getMock();
160
161
		$messageBuilder->expects( $this->any() )
162
			->method( 'getMessage' )
163
			->will( $this->returnValue( $message ) );
164
165
		$instance = new Factbox( $store, new ParserData( $subject->getTitle(), $parserOutput ), $messageBuilder );
0 ignored issues
show
It seems like $subject->getTitle() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
166
		$result   = $instance->doBuild()->getContent();
167
168
		$this->assertInternalType(
169
			'string',
170
			$result
171
		);
172
173
		$this->assertContains(
174
			$subject->getDBkey(),
175
			 $result
176
		);
177
178
		$this->assertEquals(
179
			$subject->getTitle(),
180
			$instance->getTitle()
181
		);
182
	}
183
184
	public function testCreateTable() {
185
186
		$parserData = new ParserData(
187
			Title::newFromText( __METHOD__ ),
188
			new ParserOutput()
189
		);
190
191
		$store = $this->getMockBuilder( '\SMW\Store' )
192
			->disableOriginalConstructor()
193
			->getMockForAbstractClass();
194
195
		$message = $this->getMockBuilder( '\Message' )
196
			->disableOriginalConstructor()
197
			->getMock();
198
199
		$message->expects( $this->any() )
200
			->method( 'inContentLanguage' )
201
			->will( $this->returnSelf() );
202
203
		$messageBuilder = $this->getMockBuilder( '\SMW\MediaWiki\MessageBuilder' )
204
			->disableOriginalConstructor()
205
			->getMock();
206
207
		$messageBuilder->expects( $this->any() )
208
			->method( 'getMessage' )
209
			->will( $this->returnValue( $message ) );
210
211
		$instance = new Factbox( $store, $parserData, $messageBuilder );
212
213
		$reflector = new ReflectionClass( '\SMW\Factbox\Factbox' );
214
		$createTable  = $reflector->getMethod( 'createTable' );
215
		$createTable->setAccessible( true );
216
217
		$this->assertInternalType(
218
			'string',
219
			$createTable->invoke( $instance, $parserData->getSemanticData() )
220
		);
221
	}
222
223
	/**
224
	 * @dataProvider fetchContentDataProvider
225
	 */
226
	public function testFetchContent( $parserData ) {
227
228
		$store = $this->getMockBuilder( '\SMW\Store' )
229
			->disableOriginalConstructor()
230
			->getMockForAbstractClass();
231
232
		$messageBuilder = $this->getMockBuilder( '\SMW\MediaWiki\MessageBuilder' )
233
			->disableOriginalConstructor()
234
			->getMock();
235
236
		$instance = new Factbox( $store, $parserData, $messageBuilder );
237
238
		$reflector = new ReflectionClass( '\SMW\Factbox\Factbox' );
239
240
		$fetchContent = $reflector->getMethod( 'fetchContent' );
241
		$fetchContent->setAccessible( true );
242
243
		$this->assertInternalType(
244
			'string',
245
			$fetchContent->invoke( $instance, SMW_FACTBOX_NONEMPTY )
246
		);
247
248
		$this->assertEmpty(
249
			$fetchContent->invoke( $instance, SMW_FACTBOX_HIDDEN )
250
		);
251
	}
252
253
	/**
254
	 * @dataProvider contentDataProvider
255
	 */
256
	public function testGetContentDataSimulation( $setup, $expected ) {
257
258
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
259
			->disableOriginalConstructor()
260
			->getMock();
261
262
		$semanticData->expects( $this->any() )
263
			->method( 'hasVisibleSpecialProperties' )
264
			->will( $this->returnValue( $setup['hasVisibleSpecialProperties'] ) );
265
266
		$semanticData->expects( $this->any() )
267
			->method( 'hasVisibleProperties' )
268
			->will( $this->returnValue( $setup['hasVisibleProperties'] ) );
269
270
		$semanticData->expects( $this->any() )
271
			->method( 'isEmpty' )
272
			->will( $this->returnValue( $setup['isEmpty'] ) );
273
274
		$store = $this->getMockBuilder( '\SMW\Store' )
275
			->disableOriginalConstructor()
276
			->getMockForAbstractClass();
277
278
		$store->expects( $this->any() )
279
			->method( 'getSemanticData' )
280
			->will( $this->returnValue( $semanticData ) );
281
282
		$parserData = $this->getMockBuilder( '\SMW\ParserData' )
283
			->disableOriginalConstructor()
284
			->getMock();
285
286
		$parserData->expects( $this->any() )
287
			->method( 'getSubject' )
288
			->will( $this->returnValue( DIWikiPage::newFromText( __METHOD__ ) ) );
289
290
		$parserData->expects( $this->any() )
291
			->method( 'getSemanticData' )
292
			->will( $this->returnValue( null ) );
293
294
		$messageBuilder = $this->getMockBuilder( '\SMW\MediaWiki\MessageBuilder' )
295
			->disableOriginalConstructor()
296
			->getMock();
297
298
		// Build Factbox stub object to encapsulate the method
299
		// without the need for other dependencies to occur
300
		$factbox = $this->getMock( '\SMW\Factbox\Factbox',
301
			array( 'createTable' ),
302
			array(
303
				$store,
304
				$parserData,
305
				$messageBuilder
306
			)
307
		);
308
309
		$factbox->expects( $this->any() )
310
			->method( 'createTable' )
311
			->will( $this->returnValue( $setup['invokedContent'] ) );
312
313
		$reflector = new ReflectionClass( '\SMW\Factbox\Factbox' );
314
		$fetchContent = $reflector->getMethod( 'fetchContent' );
315
		$fetchContent->setAccessible( true );
316
317
		$this->assertInternalType(
318
			'string',
319
			$fetchContent->invoke( $factbox )
320
		);
321
322
		$this->assertEquals(
323
			$expected,
324
			$fetchContent->invoke( $factbox, $setup['showFactbox'] )
325
		);
326
	}
327
328
	/**
329
	 * Conditional content switcher to test combinations of
330
	 * SMW_FACTBOX_NONEMPTY and SMWSemanticData etc.
331
	 *
332
	 * @return array
333
	 */
334
	public function contentDataProvider() {
335
336
		$text = __METHOD__;
337
		$provider = array();
338
339
		$provider[] = array(
340
			array(
341
				'hasVisibleSpecialProperties' => true,
342
				'hasVisibleProperties'        => true,
343
				'isEmpty'                     => false,
344
				'showFactbox'                 => SMW_FACTBOX_NONEMPTY,
345
				'invokedContent'              => $text,
346
			),
347
			$text // expected return
348
		);
349
350
		$provider[] = array(
351
			array(
352
				'hasVisibleSpecialProperties' => true,
353
				'hasVisibleProperties'        => true,
354
				'isEmpty'                     => true,
355
				'showFactbox'                 => SMW_FACTBOX_NONEMPTY,
356
				'invokedContent'              => $text,
357
			),
358
			$text // expected return
359
		);
360
361
		$provider[] = array(
362
			array(
363
				'hasVisibleSpecialProperties' => false,
364
				'hasVisibleProperties'        => true,
365
				'isEmpty'                     => false,
366
				'showFactbox'                 => SMW_FACTBOX_SPECIAL,
367
				'invokedContent'              => $text,
368
			),
369
			'' // expected return
370
		);
371
372
		$provider[] = array(
373
			array(
374
				'hasVisibleSpecialProperties' => false,
375
				'hasVisibleProperties'        => false,
376
				'isEmpty'                     => false,
377
				'showFactbox'                 => SMW_FACTBOX_NONEMPTY,
378
				'invokedContent'              => $text,
379
			),
380
			'' // expected return
381
		);
382
383
		$provider[] = array(
384
			array(
385
				'hasVisibleSpecialProperties' => true,
386
				'hasVisibleProperties'        => false,
387
				'isEmpty'                     => false,
388
				'showFactbox'                 => SMW_FACTBOX_NONEMPTY,
389
				'invokedContent'              => $text,
390
			),
391
			'' // expected return
392
		);
393
394
		return $provider;
395
	}
396
397
	public function testGetTableHeader() {
398
399
		$title = Title::newFromText( __METHOD__ );
400
401
		$parserData = new ParserData(
402
			$title,
403
			new ParserOutput()
404
		);
405
406
		$parserData->setSemanticData( new SemanticData( DIWikiPage::newFromTitle( $title ) ) );
407
		$parserData->getSemanticData()->addPropertyObjectValue(
408
			new DIProperty( 'Foo' ),
409
			DIWikiPage::newFromTitle( $title )
410
		);
411
412
		$store = $this->getMockBuilder( '\SMW\Store' )
413
			->disableOriginalConstructor()
414
			->getMockForAbstractClass();
415
416
		$message = $this->getMockBuilder( '\Message' )
417
			->disableOriginalConstructor()
418
			->getMock();
419
420
		$message->expects( $this->any() )
421
			->method( 'inContentLanguage' )
422
			->will( $this->returnSelf() );
423
424
		$messageBuilder = $this->getMockBuilder( '\SMW\MediaWiki\MessageBuilder' )
425
			->disableOriginalConstructor()
426
			->getMock();
427
428
		$messageBuilder->expects( $this->any() )
429
			->method( 'getMessage' )
430
			->will( $this->returnValue( $message ) );
431
432
		$instance = new Factbox( $store, $parserData, $messageBuilder );
433
434
		$this->stringValidator->assertThatStringContains(
435
			array(
436
				'div class="smwrdflink"'
437
			),
438
			$instance->doBuild()->getContent()
439
		);
440
	}
441
442
	/**
443
	 * @dataProvider tableContentDataProvider
444
	 */
445
	public function testGetTableContent( $test, $expected ) {
446
447
		$title = Title::newFromText( __METHOD__ );
448
449
		$parserData = new ParserData(
450
			$title,
451
			new ParserOutput()
452
		);
453
454
		$store = $this->getMockBuilder( '\SMW\Store' )
455
			->disableOriginalConstructor()
456
			->getMockForAbstractClass();
457
458
		$message = $this->getMockBuilder( '\Message' )
459
			->disableOriginalConstructor()
460
			->getMock();
461
462
		$message->expects( $this->any() )
463
			->method( 'inContentLanguage' )
464
			->will( $this->returnSelf() );
465
466
		$messageBuilder = $this->getMockBuilder( '\SMW\MediaWiki\MessageBuilder' )
467
			->disableOriginalConstructor()
468
			->getMock();
469
470
		$messageBuilder->expects( $this->any() )
471
			->method( 'getMessage' )
472
			->will( $this->returnValue( $message ) );
473
474
		$property = $this->getMockBuilder( '\SMW\DIProperty' )
475
			->disableOriginalConstructor()
476
			->getMock();
477
478
		$property->expects( $this->any() )
479
			->method( 'isUserDefined' )
480
			->will( $this->returnValue( $test['isUserDefined'] ) );
481
482
		$property->expects( $this->any() )
483
			->method( 'isShown' )
484
			->will( $this->returnValue( $test['isShown'] ) );
485
486
		$property->expects( $this->any() )
487
			->method( 'getLabel' )
488
			->will( $this->returnValue( 'Quuey' ) );
489
490
		$property->expects( $this->any() )
491
			->method( 'getDIType' )
492
			->will( $this->returnValue( \SMWDataItem::TYPE_PROPERTY ) );
493
494
		$parserData->setSemanticData(
495
			new SemanticData( DIWikiPage::newFromTitle( $title ) )
496
		);
497
498
		$parserData->getSemanticData()->addPropertyObjectValue(
499
			$property,
500
			DIWikiPage::newFromTitle( $title )
501
		);
502
503
		$instance = new Factbox( $store, $parserData, $messageBuilder );
504
505
		$this->stringValidator->assertThatStringContains(
506
			$expected,
507
			$instance->doBuild()->getContent()
508
		);
509
	}
510
511
	public function tableContentDataProvider() {
512
513
		$provider = array();
514
515
		$provider[] = array(
516
			array(
517
				'isShown'       => true,
518
				'isUserDefined' => true,
519
			),
520
			array( 'class="smwprops"' )
521
		);
522
523
		$provider[] = array(
524
			array(
525
				'isShown'       => false,
526
				'isUserDefined' => true,
527
			),
528
			''
529
		);
530
531
		$provider[] = array(
532
			array(
533
				'isShown'       => true,
534
				'isUserDefined' => false,
535
			),
536
			array( 'class="smwspecs"' )
537
		);
538
539
		$provider[] = array(
540
			array(
541
				'isShown'       => false,
542
				'isUserDefined' => false,
543
			),
544
			''
545
		);
546
547
		return $provider;
548
	}
549
550
	/**
551
	 * @return array
552
	 */
553
	public function fetchContentDataProvider() {
554
555
		$title = Title::newFromText( __METHOD__ );
556
557
		$provider = array();
558
559
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
560
			->disableOriginalConstructor()
561
			->getMock();
562
563
		$semanticData->expects( $this->any() )
564
			->method( 'getPropertyValues' )
565
			->will( $this->returnValue( array() ) );
566
567
		$semanticData->expects( $this->any() )
568
			->method( 'isEmpty' )
569
			->will( $this->returnValue( false ) );
570
571
		$parserData = new ParserData(
572
			$title,
573
			new ParserOutput()
574
		);
575
576
		$parserData->setSemanticData( $semanticData );
577
578
		$provider[] = array( $parserData );
579
580
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
581
			->disableOriginalConstructor()
582
			->getMock();
583
584
		$semanticData->expects( $this->any() )
585
			->method( 'getPropertyValues' )
586
			->will( $this->returnValue( array( new DIProperty( '_SKEY') ) ) );
587
588
		$semanticData->expects( $this->any() )
589
			->method( 'isEmpty' )
590
			->will( $this->returnValue( false ) );
591
592
		$parserData = new ParserData(
593
			$title,
594
			new ParserOutput()
595
		);
596
597
		$parserData->setSemanticData( $semanticData );
598
599
		$provider[] = array( $parserData );
600
601
		return $provider;
602
	}
603
604
	protected function setupParserOutput( $semanticData ) {
605
606
		$parserOutput = new ParserOutput();
607
608
		if ( method_exists( $parserOutput, 'setExtensionData' ) ) {
609
			$parserOutput->setExtensionData( 'smwdata', $semanticData );
610
		} else {
611
			$parserOutput->mSMWData = $semanticData;
612
		}
613
614
		return $parserOutput;
615
	}
616
617
}
618