Passed
Pull Request — master (#136)
by None
04:09
created

SemanticCiteJsonTestCaseScriptRunnerTest::assertParserOutputForCase()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.9688
c 0
b 0
f 0
cc 5
nc 5
nop 1
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() : void {
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 ( $jsonTestCaseFileHandler->requiredToSkipFor( $case, $this->connectorId ) ) {
145
				continue;
146
			}
147
148
			if ( !isset( $case['subject'] ) ) {
149
				break;
150
			}
151
152
			$this->assertSemanticDataForCase( $case, $jsonTestCaseFileHandler->getDebugMode() );
153
			$this->assertParserOutputForCase( $case );
154
		}
155
	}
156
157
	private function assertSemanticDataForCase( $case, $debug ) {
158
159
		if ( !isset( $case['store'] ) || !isset( $case['store']['semantic-data'] ) ) {
160
			return;
161
		}
162
163
		$subject = DIWikiPage::newFromText(
164
			$case['subject'],
165
			isset( $case['namespace'] ) ? constant( $case['namespace'] ) : NS_MAIN
166
		);
167
168
		$semanticData = $this->getStore()->getSemanticData( $subject );
169
170
		if ( $debug ) {
171
			print_r( $semanticData );
172
		}
173
174
		if ( isset( $case['errors'] ) && $case['errors'] !== [] ) {
175
			$this->assertNotEmpty(
176
				$semanticData->getErrors()
177
			);
178
		}
179
180
		$this->semanticDataValidator->assertThatPropertiesAreSet(
181
			$case['store']['semantic-data'],
182
			$semanticData,
183
			$case['about']
184
		);
185
	}
186
187
	private function assertParserOutputForCase( $case ) {
188
189
		if ( !isset( $case['expected-output'] ) ) {
190
			return;
191
		}
192
193
		if ( !isset( $case['expected-output']['to-contain'] ) ) {
194
			$case['expected-output']['to-contain'] = [];
195
		}
196
197
		if ( !isset( $case['expected-output']['to-not-contain'] ) ) {
198
			$case['expected-output']['to-not-contain'] = [];
199
		}
200
201
		$subject = DIWikiPage::newFromText(
202
			$case['subject'],
203
			isset( $case['namespace'] ) ? constant( $case['namespace'] ) : NS_MAIN
204
		);
205
206
		$pageReader = UtilityFactory::getInstance()->newPageReader();
207
		$parserOutput = $pageReader->getEditInfo( $subject->getTitle() )->getOutput();
208
209
		// Cheating a bit here but this is to ensure the OutputPageBeforeHTML
210
		// hook is run
211
		$context = new \RequestContext();
212
		$context->setTitle( $subject->getTitle() );
213
		$context->getOutput()->addParserOutput( $parserOutput );
214
215
		$this->stringValidator->assertThatStringContains(
216
			$case['expected-output']['to-contain'],
217
			$context->getOutput()->getHtml(),
218
			$case['about']
219
		);
220
221
		$this->stringValidator->assertThatStringNotContains(
222
			$case['expected-output']['to-not-contain'],
223
			$context->getOutput()->getHtml(),
224
			$case['about']
225
		);
226
	}
227
228
}
229