Completed
Push — master ( e6a6bf...25902b )
by mw
04:53
created

getTestCaseLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SCI\Tests\Integration\JSONScript;
4
5
use SCI\MediaWikiNsContentMapper;
6
use SCI\HookRegistry;
7
use SCI\Options;
8
use Onoi\Cache\CacheFactory;
9
use SMW\Tests\LightweightJsonTestCaseScriptRunner;
10
use SMW\Tests\JsonTestCaseFileHandler;
11
use SMW\Tests\Utils\UtilityFactory;
12
use SMW\DIWikiPage;
13
14
/**
15
 * @group semantic-cite
16
 * @group medium
17
 *
18
 * @license GNU GPL v2+
19
 * @since 1.0
20
 *
21
 * @author mwjames
22
 */
23
class SemanticCiteJsonTestCaseScriptRunnerTest extends LightweightJsonTestCaseScriptRunner {
24
25
	private $semanticDataValidator;
26
	private $stringValidator;
27
	private $hookRegistry;
28
29
	protected function setUp() {
30
		parent::setUp();
31
32
		$this->testEnvironment->tearDown();
33
34
		// Make sure LocalSettings don't interfere with the default settings
35
		$this->testEnvironment->withConfiguration(
36
			[
37
				'smwgQueryResultCacheType' => false,
38
				'smwgPageSpecialProperties' => [ '_MDAT' ],
39
			]
40
		);
41
42
		$validatorFactory = $this->testEnvironment->getUtilityFactory()->newValidatorFactory();
43
44
		$this->semanticDataValidator = $validatorFactory->newSemanticDataValidator();
45
		$this->stringValidator = $validatorFactory->newStringValidator();
46
47
		$configuration = [
48
			'numberOfReferenceListColumns'       => 1,
49
			'browseLinkToCitationResource'       => false,
50
			'showTooltipForCitationReference'    => false,
51
			'tooltipRequestCacheTTL'             => false,
52
			'citationReferenceCaptionFormat'     => 1,
53
			'referenceListType'                  => 'ol',
54
			'enabledStrictParserValidation'      => true,
55
			'cachePrefix'                        => 'foo',
56
			'enabledCitationTextChangeUpdateJob' => false,
57
			'responsiveMonoColumnCharacterBoundLength' => 100
58
		];
59
60
		// This is to ensure we read from the DB when a test case
61
		// specifies a NS_MEDIAWIKI page
62
		MediaWikiNsContentMapper::clear();
63
		MediaWikiNsContentMapper::$skipMessageCache = true;
64
65
		$cacheFactory = new CacheFactory();
66
67
		$this->hookRegistry = new HookRegistry(
68
			$this->getStore(),
69
			$cacheFactory->newFixedInMemoryLruCache(),
70
			new Options( $configuration )
71
		);
72
73
		$this->hookRegistry->clear();
74
		$this->hookRegistry->register();
75
	}
76
77
	/**
78
	 * @see JsonTestCaseScriptRunner::getTestCaseLocation
79
	 */
80
	protected function getRequiredJsonTestCaseMinVersion() {
81
		return '0.1';
82
	}
83
84
	/**
85
	 * @see JsonTestCaseScriptRunner::getTestCaseLocation
86
	 */
87
	protected function getTestCaseLocation() {
88
		return __DIR__ . '/TestCases';
89
	}
90
91
	/**
92
	 * @see JsonTestCaseScriptRunner::getPermittedSettings
93
	 */
94
	protected function getPermittedSettings() {
95
		$settings = parent::getPermittedSettings();
96
97
		return array_merge( $settings, [
98
			'smwgNamespacesWithSemanticLinks',
99
			'smwgPageSpecialProperties',
100
			'wgLanguageCode',
101
			'wgContLang',
102
			'wgLang',
103
			'scigCitationReferenceCaptionFormat',
104
			'smwgQueryResultCacheType'
105
		] );
106
	}
107
108
	/**
109
	 * @see JsonTestCaseScriptRunner::runTestCaseFile
110
	 *
111
	 * @param JsonTestCaseFileHandler $jsonTestCaseFileHandler
112
	 */
113
	protected function runTestCaseFile( JsonTestCaseFileHandler $jsonTestCaseFileHandler ) {
114
		parent::runTestCaseFile( $jsonTestCaseFileHandler );
115
116
		// On SQLite we don't want DB dead locks due to parallel write access
117
		$this->changeGlobalSettingTo(
118
			'smwgEnabledHttpDeferredJobRequest',
119
			false
120
		);
121
122
		$this->hookRegistry->setOption(
123
			'citationReferenceCaptionFormat',
124
			$jsonTestCaseFileHandler->getSettingsFor( 'scigCitationReferenceCaptionFormat' )
125
		);
126
127
		$this->hookRegistry->setOption(
128
			'referenceListType',
129
			$jsonTestCaseFileHandler->getSettingsFor( 'scigReferenceListType' )
130
		);
131
132
		$this->createPagesFor(
133
			$jsonTestCaseFileHandler->getListOfProperties(),
134
			SMW_NS_PROPERTY
135
		);
136
137
		$this->createPagesFor(
138
			$jsonTestCaseFileHandler->getListOfSubjects(),
139
			NS_MAIN
140
		);
141
142
		foreach ( $jsonTestCaseFileHandler->findTestCasesFor( 'parser-testcases' ) as $case ) {
143
144
			if ( !isset( $case['subject'] ) ) {
145
				break;
146
			}
147
148
			$this->assertSemanticDataForCase( $case, $jsonTestCaseFileHandler->getDebugMode() );
149
			$this->assertParserOutputForCase( $case );
150
		}
151
	}
152
153
	private function assertSemanticDataForCase( $case, $debug ) {
154
155
		if ( !isset( $case['store'] ) || !isset( $case['store']['semantic-data'] ) ) {
156
			return;
157
		}
158
159
		$subject = DIWikiPage::newFromText(
160
			$case['subject'],
161
			isset( $case['namespace'] ) ? constant( $case['namespace'] ) : NS_MAIN
162
		);
163
164
		$semanticData = $this->getStore()->getSemanticData( $subject );
165
166
		if ( $debug ) {
167
			print_r( $semanticData );
168
		}
169
170
		if ( isset( $case['errors'] ) && $case['errors'] !== [] ) {
171
			$this->assertNotEmpty(
172
				$semanticData->getErrors()
173
			);
174
		}
175
176
		$this->semanticDataValidator->assertThatPropertiesAreSet(
177
			$case['store']['semantic-data'],
178
			$semanticData,
179
			$case['about']
180
		);
181
	}
182
183
	private function assertParserOutputForCase( $case ) {
184
185
		if ( !isset( $case['expected-output'] ) ) {
186
			return;
187
		}
188
189
		if ( !isset( $case['expected-output']['to-contain'] ) ) {
190
			$case['expected-output']['to-contain'] = [];
191
		}
192
193
		if ( !isset( $case['expected-output']['to-not-contain'] ) ) {
194
			$case['expected-output']['to-not-contain'] = [];
195
		}
196
197
		$subject = DIWikiPage::newFromText(
198
			$case['subject'],
199
			isset( $case['namespace'] ) ? constant( $case['namespace'] ) : NS_MAIN
200
		);
201
202
		$pageReader = UtilityFactory::getInstance()->newPageReader();
203
		$parserOutput = $pageReader->getEditInfo( $subject->getTitle() )->getOutput();
0 ignored issues
show
Bug introduced by
It seems like $subject->getTitle() can be null; however, getEditInfo() 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...
204
205
		// Cheating a bit here but this is to ensure the OutputPageBeforeHTML
206
		// hook is run
207
		$context = new \RequestContext();
208
		$context->setTitle( $subject->getTitle() );
209
		$context->getOutput()->addParserOutput( $parserOutput );
210
211
		$this->stringValidator->assertThatStringContains(
212
			$case['expected-output']['to-contain'],
213
			$context->getOutput()->getHtml(),
214
			$case['about']
215
		);
216
217
		$this->stringValidator->assertThatStringNotContains(
218
			$case['expected-output']['to-not-contain'],
219
			$context->getOutput()->getHtml(),
220
			$case['about']
221
		);
222
	}
223
224
}
225