summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/JsonTestCaseScriptRunner.php
blob: 1fb8d7791092f2ff051197d8c18328cd1020e2be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php

namespace SMW\Tests;

use SMW\Tests\Utils\UtilityFactory;
use Title;

/**
 * The JsonTestCaseScriptRunner is a convenience provider for `Json` formatted
 * integration tests to allow writing tests quicker without the need to setup
 * or tear down specific data structures.
 *
 * The JSON format should make it also possible for novice user to understand
 * what sort of tests are run as the content is based on wikitext rather than
 * native PHP.
 *
 * @group semantic-mediawiki
 * @group medium
 *
 * @license GNU GPL v2+
 * @since 2.2
 *
 * @author mwjames
 */
abstract class JsonTestCaseScriptRunner extends MwDBaseUnitTestCase {

	/**
	 * @var FileReader
	 */
	private $fileReader;

	/**
	 * @var JsonTestCaseFileHandler
	 */
	private $jsonTestCaseFileHandler;

	/**
	 * @var JsonTestCaseContentHandler
	 */
	private $jsonTestCaseContentHandler;

	/**
	 * @var array
	 */
	private $itemsMarkedForDeletion = [];

	/**
	 * @var array
	 */
	private $configValueCallback = [];

	/**
	 * @var boolean
	 */
	protected $deletePagesOnTearDown = true;

	/**
	 * @var string
	 */
	protected $searchByFileExtension = 'json';

	/**
	 * @var string
	 */
	protected $connectorId = '';

	protected function setUp() {
		parent::setUp();

		$utilityFactory = $this->testEnvironment->getUtilityFactory();
		$utilityFactory->newMwHooksHandler()->deregisterListedHooks();
		$utilityFactory->newMwHooksHandler()->invokeHooksFromRegistry();

		$this->fileReader = $utilityFactory->newJsonFileReader();

		$this->jsonTestCaseContentHandler = new JsonTestCaseContentHandler(
			$utilityFactory->newPageCreator(),
			$utilityFactory->newPageDeleter(),
			$utilityFactory->newLocalFileUpload()
		);

		if ( $this->getStore() instanceof \SMWSparqlStore ) {
			$this->connectorId = strtolower( $GLOBALS['smwgSparqlRepositoryConnector'] );
		} elseif ( $this->getStore() instanceof \SMW\Elastic\ElasticStore ) {
			$this->connectorId = 'elastic';
		} else {
			$this->connectorId = strtolower( $this->getDBConnection()->getType() );
		}
	}

	protected function tearDown() {

		if ( $this->deletePagesOnTearDown ) {
			$this->testEnvironment->flushPages( $this->itemsMarkedForDeletion );
		}

		$this->testEnvironment->tearDown();
		parent::tearDown();
	}

	/**
	 * @return string
	 */
	abstract protected function getTestCaseLocation();

	/**
	 * @param JsonTestCaseFileHandler $jsonTestCaseFileHandler
	 */
	abstract protected function runTestCaseFile( JsonTestCaseFileHandler $jsonTestCaseFileHandler );

	/**
	 * @return string
	 */
	protected function getRequiredJsonTestCaseMinVersion() {
		return '0.1';
	}

	/**
	 * @return array
	 */
	protected function getAllowedTestCaseFiles() {
		return [];
	}

	/**
	 * Selected list of settings (internal or MediaWiki related) that are
	 * permissible for the time of the test run to be manipulated.
	 *
	 * For a configuration that requires special treatment (i.e. where a simple
	 * assignment isn't sufficient), a callback can be assigned to a settings
	 * key in order to sort out required manipulation (constants etc.).
	 *
	 * @return array
	 */
	protected function getPermittedSettings() {

		// Ensure that the context is set for a selected language
		// and dependent objects are reset
		$this->registerConfigValueCallback( 'wgContLang', function( $val ) {
			\RequestContext::getMain()->setLanguage( $val );
			\SMW\Localizer::getInstance()->clear();
			$lang = \Language::factory( $val );

			// https://github.com/wikimedia/mediawiki/commit/49ce67be93dfbb40d036703dad2278ea9843f1ad
			$this->testEnvironment->redefineMediaWikiService( 'ContentLanguage', function () use ( $lang ) {
				return $lang;
			} );

			return $lang;
		} );

		$this->registerConfigValueCallback( 'wgLang', function( $val ) {
			\RequestContext::getMain()->setLanguage( $val );
			\SMW\Localizer::getInstance()->clear();
			return \Language::factory( $val );
		} );

		return [];
	}

	/**
	 * @param string $key
	 * @param Closure $callback
	 */
	protected function registerConfigValueCallback( $key, \Closure $callback ) {
		$this->configValueCallback[$key] = $callback;
	}

	/**
	 * @return callable|null
	 */
	protected function getConfigValueCallback( $key ) {
		return isset( $this->configValueCallback[$key] ) ? $this->configValueCallback[$key] : null;
	}

	/**
	 * Normally returns TRUE but can act on the list retrieved from
	 * JsonTestCaseScriptRunner::getAllowedTestCaseFiles (or hereof) to filter
	 * selected files and help fine tune a setup or debug a potential issue
	 * without having to run all test files at once.
	 *
	 * @param string $file
	 *
	 * @return boolean
	 */
	protected function canTestCaseFile( $file ) {

		// Filter specific files on-the-fly
		$allowedTestCaseFiles = $this->getAllowedTestCaseFiles();

		if ( $allowedTestCaseFiles === [] ) {
			return true;
		}

		// Doesn't require the exact name
		foreach ( $allowedTestCaseFiles as $fileName ) {
			if ( strpos( $file, $fileName ) !== false ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * @dataProvider jsonFileProvider
	 */
	public function testCaseFile( $file ) {

		if ( !$this->canTestCaseFile( $file ) ) {
			$this->markTestSkipped( $file . ' excluded from the test run' );
		}

		$this->fileReader->setFile( $file );
		$this->runTestCaseFile( new JsonTestCaseFileHandler( $this->fileReader ) );
	}

	/**
	 * @return array
	 */
	public function jsonFileProvider() {

		$provider = [];

		$bulkFileProvider = UtilityFactory::getInstance()->newBulkFileProvider(
			$this->getTestCaseLocation()
		);

		$bulkFileProvider->searchByFileExtension( $this->searchByFileExtension );

		foreach ( $bulkFileProvider->getFiles() as $file ) {
			$provider[basename( $file )] = [ $file ];
		}

		return $provider;
	}

	/**
	 * @since 2.2
	 *
	 * @param mixed $key
	 * @param mixed $value
	 */
	protected function changeGlobalSettingTo( $key, $value ) {
		$this->testEnvironment->addConfiguration( $key, $value );
	}

	/**
	 * @since 3.0
	 *
	 * @return []
	 */
	protected function getDependencyDefinitions() {
		return [];
	}

	/**
	 * @since 2.2
	 *
	 * @param JsonTestCaseFileHandler $jsonTestCaseFileHandler
	 */
	protected function checkEnvironmentToSkipCurrentTest( JsonTestCaseFileHandler $jsonTestCaseFileHandler ) {

		if ( $jsonTestCaseFileHandler->isIncomplete() ) {
			$this->markTestIncomplete( $jsonTestCaseFileHandler->getReasonForSkip() );
		}

		if ( !$jsonTestCaseFileHandler->hasAllRequirements( $this->getDependencyDefinitions() ) ) {
			$this->markTestSkipped( $jsonTestCaseFileHandler->getReasonForSkip() );
		}

		if ( $jsonTestCaseFileHandler->requiredToSkipForJsonVersion( $this->getRequiredJsonTestCaseMinVersion() ) ) {
			$this->markTestSkipped( $jsonTestCaseFileHandler->getReasonForSkip() );
		}

		if ( $jsonTestCaseFileHandler->requiredToSkipForMwVersion( $GLOBALS['wgVersion'] ) ) {
			$this->markTestSkipped( $jsonTestCaseFileHandler->getReasonForSkip() );
		}

		if ( $jsonTestCaseFileHandler->requiredToSkipForConnector( $this->getDBConnection()->getType() ) ) {
			$this->markTestSkipped( $jsonTestCaseFileHandler->getReasonForSkip() );
		}

		if ( $jsonTestCaseFileHandler->requiredToSkipForConnector( $this->connectorId ) ) {
			$this->markTestSkipped( $jsonTestCaseFileHandler->getReasonForSkip() );
		}
	}

	/**
	 * @since 2.5
	 *
	 * @param array $pages
	 * @param integer $defaultNamespace
	 */
	protected function createPagesFrom( array $pages, $defaultNamespace = NS_MAIN ) {

		$this->jsonTestCaseContentHandler->skipOn(
			$this->connectorId
		);

		$this->jsonTestCaseContentHandler->setTestCaseLocation(
			$this->getTestCaseLocation()
		);

		$this->jsonTestCaseContentHandler->createPagesFrom(
			$pages,
			$defaultNamespace
		);

		$this->testEnvironment->executePendingDeferredUpdates();

		$this->itemsMarkedForDeletion = $this->jsonTestCaseContentHandler->getPages();
	}

	/**
	 * @deprecated 2.5
	 */
	protected function createPagesFor( array $pages, $defaultNamespace ) {
		$this->createPagesFrom( $pages, $defaultNamespace );
	}

}