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