summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Integration/JSONScript/ParserTestCaseProcessor.php
blob: 42159a6e1af9eccffdf7595200177dc1d1a09b9a (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
<?php

namespace SMW\Tests\Integration\JSONScript;

use RuntimeException;
use SMW\DIWikiPage;
use SMW\MediaWiki\MediaWikiNsContentReader;
use SMW\Tests\Utils\UtilityFactory;

/**
 * @group semantic-mediawiki
 * @group medium
 *
 * @license GNU GPL v2+
 * @since 2.3
 *
 * @author mwjames
 */
class ParserTestCaseProcessor extends \PHPUnit_Framework_TestCase {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @var SemanticDataValidator
	 */
	private $semanticDataValidator;

	/**
	 * @var IncomingSemanticDataValidator
	 */
	private $incomingSemanticDataValidator;

	/**
	 * @var StringValidator
	 */
	private $stringValidator;

	/**
	 * @var PageReader
	 */
	private $pageReader;

	/**
	 * @var SerializerFactory
	 */
	private $serializerFactory;

	/**
	 * @var boolean
	 */
	private $debug = false;

	/**
	 * @param Store
	 * @param SemanticDataValidator
	 * @param IncomingSemanticDataValidator
	 * @param StringValidator
	 */
	public function __construct( $store, $semanticDataValidator, $incomingSemanticDataValidator, $stringValidator ) {
		$this->store = $store;
		$this->semanticDataValidator = $semanticDataValidator;
		$this->incomingSemanticDataValidator = $incomingSemanticDataValidator;
		$this->stringValidator = $stringValidator;
		$this->pageReader = UtilityFactory::getInstance()->newPageReader();
		$this->serializerFactory = \SMW\ApplicationFactory::getInstance()->newSerializerFactory();
	}

	/**
	 * @since 2.2
	 *
	 * @param boolean $debugMode
	 */
	public function setDebugMode( $debugMode ) {
		$this->debug = $debugMode;
	}

	/**
	 * @since 2.2
	 *
	 * @param array $case
	 */
	public function process( array $case ) {

		if ( !isset( $case['subject'] ) ) {
			return;
		}

		$this->assertSemanticDataForCase(
			$case
		);

		$this->assertTextFromParserOutputForCase(
			$case
		);

		$this->assertTextFromParsedMsgForCase(
			$case
		);
	}

	private function assertSemanticDataForCase( $case ) {

		// Allows for data to be re-read from the DB instead of being fetched
		// from the store-id-cache
		if ( isset( $case['store']['clear-cache'] ) && $case['store']['clear-cache'] ) {
			$this->store->clear();
		}

		if ( !isset( $case['assert-store'] ) || !isset( $case['assert-store']['semantic-data'] ) ) {
			return;
		}

		$subject = $this->getSubjectFrom( $case, false );
		$semanticData = $this->store->getSemanticData( $subject );

		if ( $this->debug ) {
			print_r(
				$this->serializerFactory->newSemanticDataSerializer()->serialize( $semanticData )
			);
		}

		if ( isset( $case['errors'] ) && $case['errors'] !== [] ) {
			$this->assertNotEmpty(
				$semanticData->getErrors()
			);
		}

		$this->semanticDataValidator->assertThatPropertiesAreSet(
			$case['assert-store']['semantic-data'],
			$semanticData,
			$case['about']
		);

		if ( !isset( $case['assert-store']['semantic-data']['incoming'] ) ) {
			return;
		}

		$this->incomingSemanticDataValidator->assertThatIncomingDataAreSet(
			$case['assert-store']['semantic-data']['incoming'],
			$subject,
			$case['about']
		);
	}

	private function assertTextFromParserOutputForCase( $case ) {

		if ( !isset( $case['assert-output'] ) ) {
			return;
		}

		$title = $this->getSubjectFrom( $case )->getTitle();

		$parserOutput = $this->pageReader->getParserOutputFromEdit(
			$title
		);

		if ( isset( $case['assert-output']['onOutputPage'] ) && $case['assert-output']['onOutputPage'] ) {
			$context = new \RequestContext();
			$context->setTitle( $title );
			// Ensures the OutputPageBeforeHTML hook is run
			$context->getOutput()->addParserOutput( $parserOutput );
			$output = $context->getOutput()->getHtml();
		} elseif ( isset( $case['assert-output']['onPageView'] ) ) {
			$parameters = isset( $case['assert-output']['onPageView']['parameters'] ) ? $case['assert-output']['onPageView']['parameters'] : [];
			$context = \RequestContext::newExtraneousContext(
				$title,
				$parameters
			);
			\Article::newFromTitle( $title, $context )->view();
			$output = $context->getOutput()->getHtml();
		} else {
			$output = $parserOutput->getText();
		}

		// Strip HTML comments
		$output = preg_replace('/<!--(.*)-->/Uis', '', $output );

		if ( isset( $case['assert-output']['to-contain'] ) ) {
			$this->stringValidator->assertThatStringContains(
				$case['assert-output']['to-contain'],
				$output,
				$case['about']
			);
		}

		if ( isset( $case['assert-output']['not-contain'] ) ) {
			$this->stringValidator->assertThatStringNotContains(
				$case['assert-output']['not-contain'],
				$output,
				$case['about']
			);
		}
	}

	private function assertTextFromParsedMsgForCase( $case ) {

		if ( !isset( $case['assert-msgoutput'] ) ) {
			return;
		}

		$mediaWikiNsContentReader = new MediaWikiNsContentReader();
		$mediaWikiNsContentReader->skipMessageCache();

		$text = $mediaWikiNsContentReader->read( $case['subject'] );
		$text = wfMessage( 'smw-parse', $text )->parse();

		if ( isset( $case['assert-msgoutput']['to-contain'] ) ) {
			$this->stringValidator->assertThatStringContains(
				$case['assert-msgoutput']['to-contain'],
				$text,
				$case['about']
			);
		}

		if ( isset( $case['assert-msgoutput']['not-contain'] ) ) {
			$this->stringValidator->assertThatStringNotContains(
				$case['assert-msgoutput']['not-contain'],
				$text,
				$case['about']
			);
		}
	}

	private function getSubjectFrom( $case, $checkExists = true ) {

		$subject = DIWikiPage::newFromText(
			$case['subject'],
			isset( $case['namespace'] ) ? constant( $case['namespace'] ) : NS_MAIN
		);

		$title = $subject->getTitle();

		if ( $title === null ) {
			throw new RuntimeException( 'Could not create Title object for subject page "' . $case['subject'] . '".' );
		}

		if ( $checkExists && !$title->exists() ) {
			throw new RuntimeException( 'Subject page "' . $case['subject'] . '" does not exist.' );
		}

		return $subject;
	}

}