Completed
Push — master ( 0591cb...33c12c )
by
unknown
07:17
created

assertSemanticDataForCase()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.5226
c 0
b 0
f 0
cc 7
nc 5
nop 2
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\JsonTestCaseScriptRunner;
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 JsonTestCaseScriptRunner {
24
25
	private $semanticDataValidator;
26
	private $stringValidator;
27
	private $hookRegistry;
28
29
	protected function setUp() {
30
		parent::setUp();
31
32
		$validatorFactory = $this->testEnvironment->getUtilityFactory()->newValidatorFactory();
33
34
		$this->semanticDataValidator = $validatorFactory->newSemanticDataValidator();
35
		$this->stringValidator = $validatorFactory->newStringValidator();
36
37
		$configuration = [
38
			'numberOfReferenceListColumns'       => 1,
39
			'browseLinkToCitationResource'       => false,
40
			'showTooltipForCitationReference'    => false,
41
			'tooltipRequestCacheTTL'             => false,
42
			'citationReferenceCaptionFormat'     => 1,
43
			'referenceListType'                  => 'ol',
44
			'enabledStrictParserValidation'      => true,
45
			'cachePrefix'                        => 'foo',
46
			'enabledCitationTextChangeUpdateJob' => false,
47
			'responsiveMonoColumnCharacterBoundLength' => 100
48
		];
49
50
		// This is to ensure we read from the DB when a test case
51
		// specifies a NS_MEDIAWIKI page
52
		MediaWikiNsContentMapper::clear();
53
		MediaWikiNsContentMapper::$skipMessageCache = true;
54
55
		$cacheFactory = new CacheFactory();
56
57
		$this->hookRegistry = new HookRegistry(
58
			$this->getStore(),
59
			$cacheFactory->newFixedInMemoryLruCache(),
60
			new Options( $configuration )
61
		);
62
63
		$this->hookRegistry->clear();
64
		$this->hookRegistry->register();
65
	}
66
67
	/**
68
	 * @see JsonTestCaseScriptRunner::getTestCaseLocation
69
	 */
70
	protected function getRequiredJsonTestCaseMinVersion() {
71
		return '0.1';
72
	}
73
74
	/**
75
	 * @see JsonTestCaseScriptRunner::getAllowedTestCaseFiles
76
	 */
77
	protected function getAllowedTestCaseFiles() {
78
		return [];
79
	}
80
81
	/**
82
	 * @see JsonTestCaseScriptRunner::getTestCaseLocation
83
	 */
84
	protected function getTestCaseLocation() {
85
		return __DIR__ . '/TestCases';
86
	}
87
88
	/**
89
	 * @see JsonTestCaseScriptRunner::getPermittedSettings
90
	 */
91
	protected function getPermittedSettings() {
92
		parent::getPermittedSettings();
93
94
		return [
95
			'smwgNamespacesWithSemanticLinks',
96
			'smwgPageSpecialProperties',
97
			'wgLanguageCode',
98
			'wgContLang',
99
			'wgLang',
100
			'scigCitationReferenceCaptionFormat',
101
			'smwgQueryResultCacheType'
102
		];
103
	}
104
105
	/**
106
	 * @see JsonTestCaseScriptRunner::runTestCaseFile
107
	 *
108
	 * @param JsonTestCaseFileHandler $jsonTestCaseFileHandler
109
	 */
110
	protected function runTestCaseFile( JsonTestCaseFileHandler $jsonTestCaseFileHandler ) {
111
112
		$this->checkEnvironmentToSkipCurrentTest( $jsonTestCaseFileHandler );
113
114
		foreach ( $this->getPermittedSettings() as $key ) {
115
			$this->changeGlobalSettingTo(
116
				$key,
117
				$jsonTestCaseFileHandler->getSettingsFor( $key, $this->getConfigValueCallback( $key ) )
118
			);
119
		}
120
121
		// On SQLite we don't want DB dead locks due to parallel write access
122
		$this->changeGlobalSettingTo(
123
			'smwgEnabledHttpDeferredJobRequest',
124
			false
125
		);
126
127
		$this->hookRegistry->setOption(
128
			'citationReferenceCaptionFormat',
129
			$jsonTestCaseFileHandler->getSettingsFor( 'scigCitationReferenceCaptionFormat' )
130
		);
131
132
		$this->hookRegistry->setOption(
133
			'referenceListType',
134
			$jsonTestCaseFileHandler->getSettingsFor( 'scigReferenceListType' )
135
		);
136
137
		$this->createPagesFor(
0 ignored issues
show
Deprecated Code introduced by
The method SMW\Tests\JsonTestCaseSc...unner::createPagesFor() has been deprecated with message: 2.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
138
			$jsonTestCaseFileHandler->getListOfProperties(),
139
			SMW_NS_PROPERTY
140
		);
141
142
		$this->createPagesFor(
0 ignored issues
show
Deprecated Code introduced by
The method SMW\Tests\JsonTestCaseSc...unner::createPagesFor() has been deprecated with message: 2.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
143
			$jsonTestCaseFileHandler->getListOfSubjects(),
144
			NS_MAIN
145
		);
146
147
		foreach ( $jsonTestCaseFileHandler->findTestCasesFor( 'parser-testcases' ) as $case ) {
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
		$parserOutput = UtilityFactory::getInstance()->newPageReader()->getEditInfo( $subject->getTitle() )->output;
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...
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