Completed
Push — master ( 2c5a15...079bf8 )
by mw
04:12 queued 46s
created

getTestCaseLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\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 = array(
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 array();
79
	}
80
81
	/**
82
	 * @see JsonTestCaseScriptRunner::getTestCaseLocation
83
	 */
84
	protected function getTestCaseLocation() {
85
		return __DIR__ . '/TestCases';
86
	}
87
88
	/**
89
	 * @see JsonTestCaseScriptRunner::runTestCaseFile
90
	 *
91
	 * @param JsonTestCaseFileHandler $jsonTestCaseFileHandler
92
	 */
93
	protected function runTestCaseFile( JsonTestCaseFileHandler $jsonTestCaseFileHandler ) {
94
95
		$this->checkEnvironmentToSkipCurrentTest( $jsonTestCaseFileHandler );
96
97
		$permittedSettings = array(
98
			'smwgNamespacesWithSemanticLinks',
99
			'smwgPageSpecialProperties',
100
			'wgLanguageCode',
101
			'wgContLang',
102
			'wgLang',
103
			'scigCitationReferenceCaptionFormat',
104
			'smwgQueryResultCacheType'
105
		);
106
107
		foreach ( $permittedSettings as $key ) {
108
			$this->changeGlobalSettingTo(
109
				$key,
110
				$jsonTestCaseFileHandler->getSettingsFor( $key )
111
			);
112
		}
113
114
		// On SQLite we don't want DB dead locks due to parallel write access
115
		$this->changeGlobalSettingTo(
116
			'smwgEnabledHttpDeferredJobRequest',
117
			false
118
		);
119
120
		$this->hookRegistry->setOption(
121
			'citationReferenceCaptionFormat',
122
			$jsonTestCaseFileHandler->getSettingsFor( 'scigCitationReferenceCaptionFormat' )
123
		);
124
125
		$this->hookRegistry->setOption(
126
			'referenceListType',
127
			$jsonTestCaseFileHandler->getSettingsFor( 'scigReferenceListType' )
128
		);
129
130
		$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...
131
			$jsonTestCaseFileHandler->getListOfProperties(),
132
			SMW_NS_PROPERTY
133
		);
134
135
		$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...
136
			$jsonTestCaseFileHandler->getListOfSubjects(),
137
			NS_MAIN
138
		);
139
140
		foreach ( $jsonTestCaseFileHandler->findTestCasesFor( 'parser-testcases' ) as $case ) {
141
142
			if ( !isset( $case['subject'] ) ) {
143
				break;
144
			}
145
146
			$this->assertSemanticDataForCase( $case, $jsonTestCaseFileHandler->getDebugMode() );
147
			$this->assertParserOutputForCase( $case );
148
		}
149
	}
150
151
	private function assertSemanticDataForCase( $case, $debug ) {
152
153
		if ( !isset( $case['store'] ) || !isset( $case['store']['semantic-data'] ) ) {
154
			return;
155
		}
156
157
		$subject = DIWikiPage::newFromText(
158
			$case['subject'],
159
			isset( $case['namespace'] ) ? constant( $case['namespace'] ) : NS_MAIN
160
		);
161
162
		$semanticData = $this->getStore()->getSemanticData( $subject );
163
164
		if ( $debug ) {
165
			print_r( $semanticData );
166
		}
167
168
		if ( isset( $case['errors'] ) && $case['errors'] !== array() ) {
169
			$this->assertNotEmpty(
170
				$semanticData->getErrors()
171
			);
172
		}
173
174
		$this->semanticDataValidator->assertThatPropertiesAreSet(
175
			$case['store']['semantic-data'],
176
			$semanticData,
177
			$case['about']
178
		);
179
	}
180
181
	private function assertParserOutputForCase( $case ) {
182
183
		if ( !isset( $case['expected-output'] ) ) {
184
			return;
185
		}
186
187
		if ( !isset( $case['expected-output']['to-contain'] ) ) {
188
			$case['expected-output']['to-contain'] = array();
189
		}
190
191
		if ( !isset( $case['expected-output']['to-not-contain'] ) ) {
192
			$case['expected-output']['to-not-contain'] = array();
193
		}
194
195
		$subject = DIWikiPage::newFromText(
196
			$case['subject'],
197
			isset( $case['namespace'] ) ? constant( $case['namespace'] ) : NS_MAIN
198
		);
199
200
		$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...
201
202
		// Cheating a bit here but this is to ensure the OutputPageBeforeHTML
203
		// hook is run
204
		$context = new \RequestContext();
205
		$context->setTitle( $subject->getTitle() );
206
		$context->getOutput()->addParserOutput( $parserOutput );
207
208
		$this->stringValidator->assertThatStringContains(
209
			$case['expected-output']['to-contain'],
210
			$context->getOutput()->getHtml(),
211
			$case['about']
212
		);
213
214
		$this->stringValidator->assertThatStringNotContains(
215
			$case['expected-output']['to-not-contain'],
216
			$context->getOutput()->getHtml(),
217
			$case['about']
218
		);
219
	}
220
221
}
222