summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Integration/MediaWiki/LinksUpdateTest.php
blob: d78ce87124fd7a13390c7e2d9fd7252eb797d40b (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
<?php

namespace SMW\Tests\Integration\MediaWiki;

use SMW\ApplicationFactory;
use SMW\DIWikiPage;
use SMW\Tests\MwDBaseUnitTestCase;
use Title;

/**
 * @group semantic-mediawiki
 * @group medium
 *
 * @license GNU GPL v2+
 * @since 1.9.1
 *
 * @author mwjames
 */
class LinksUpdateTest extends MwDBaseUnitTestCase {

	protected $destroyDatabaseTablesBeforeRun = true;

	private $title = null;
	private $applicationFactory;
	private $mwHooksHandler;
	private $semanticDataValidator;
	private $pageDeleter;
	private $pageCreator;

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

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

		$this->semanticDataValidator = $this->testEnvironment->getUtilityFactory()->newValidatorFactory()->newSemanticDataValidator();
		$this->pageCreator = $this->testEnvironment->getUtilityFactory()->newPageCreator();
		$this->pageDeleter = $this->testEnvironment->getUtilityFactory()->newPageDeleter();

		$this->applicationFactory = ApplicationFactory::getInstance();
		$this->testEnvironment->addConfiguration( 'smwgPageSpecialProperties', [ '_MDAT' ] );

		$this->title = Title::newFromText( __METHOD__ );
	}

	public function tearDown() {
		$this->applicationFactory->clear();
		$this->mwHooksHandler->restoreListedHooks();

		$this->testEnvironment->flushPages( [ $this->title ] );
		$this->testEnvironment->tearDown();
		parent::tearDown();
	}

	public function testUpdateToSetPredefinedAnnotations() {

		$this->pageCreator
			->createPage( $this->title );

		$semanticData = $this->getStore()->getSemanticData(
			DIWikiPage::newFromTitle( $this->title )
		);

		$this->assertCount(
			2,
			$semanticData->getProperties()
		);

		$expected = [
			'propertyKeys' => [ '_SKEY', '_MDAT' ]
		];

		$this->semanticDataValidator->assertThatPropertiesAreSet(
			$expected,
			$semanticData
		);
	}

	/**
	 * @depends testUpdateToSetPredefinedAnnotations
	 */
	public function testDoUpdateUsingUserdefinedAnnotations() {

		$this->pageCreator
			->createPage( $this->title )
			->doEdit( '[[HasFirstLinksUpdatetest::testDoUpdate]] [[HasSecondLinksUpdatetest::testDoUpdate]]' );

		$parserData = $this->applicationFactory->newParserData(
			$this->title,
			$this->pageCreator->getEditInfo()->output
		);

		$contentParser = $this->applicationFactory->newContentParser( $this->title );
		$contentParser->parse();

		$parsedParserData = $this->applicationFactory->newParserData(
			$this->title,
			$contentParser->getOutput()
		);

		$this->assertCount(
			4,
			$parserData->getSemanticData()->getProperties()
		);

		$this->assertCount(
			4,
			$this->getStore()->getSemanticData( DIWikiPage::newFromTitle( $this->title ) )->getProperties()
		);

		/**
		 * See #347 and LinksUpdateConstructed
		 */
		$linksUpdate = new \LinksUpdate( $this->title, new \ParserOutput() );
		$linksUpdate->doUpdate();

		$this->testEnvironment->executePendingDeferredUpdates();

		/**
		 * Asserts that before and after the update, the SemanticData container
		 * holds the same amount of properties despite the fact that the ParserOutput
		 * was invoked empty
		 */
		$semanticData = $this->getStore()->getSemanticData(
			DIWikiPage::newFromTitle( $this->title )
		);

		$this->assertCount(
			4,
			$semanticData->getProperties()
		);

		$expected = [
			'propertyKeys' => [ '_SKEY', '_MDAT', 'HasFirstLinksUpdatetest', 'HasSecondLinksUpdatetest' ]
		];

		$this->semanticDataValidator->assertThatPropertiesAreSet(
			$expected,
			$semanticData
		);

		return $this->pageCreator->getPage()->getRevision();
	}

	/**
	 * @depends testDoUpdateUsingUserdefinedAnnotations
	 */
	public function testDoUpdateUsingNoAnnotations( $firstRunRevision ) {

		$this->pageCreator
			->createPage( $this->title )
			->doEdit( 'no annotation' );

		$this->assertNotSame(
			$firstRunRevision,
			$this->pageCreator->getPage()->getRevision()
		);

		$contentParser = $this->applicationFactory->newContentParser( $this->title );
		$contentParser->parse();

		$parserData = $this->applicationFactory->newParserData(
			$this->title,
			$contentParser->getOutput()
		);

		if ( count( $parserData->getSemanticData()->getProperties() ) != 0 ) {
			$this->markTestSkipped( "Something changed with MW 1.28 and I'm too lazy to investigate." );
		}

		$this->assertCount(
			0,
			$parserData->getSemanticData()->getProperties()
		);

		$this->assertCount(
			2,
			$this->getStore()->getSemanticData( DIWikiPage::newFromTitle( $this->title ) )->getProperties()
		);

		return $firstRunRevision;
	}

	/**
	 * @depends testDoUpdateUsingNoAnnotations
	 */
	public function testReparseFirstRevision( $firstRunRevision ) {

		$contentParser = $this->applicationFactory->newContentParser( $this->title );
		$contentParser->setRevision( $firstRunRevision );
		$contentParser->parse();

		$parserData = $this->applicationFactory->newParserData(
			$this->title,
			$contentParser->getOutput()
		);

		$semanticData = $parserData->getSemanticData();

		$this->assertCount(
			3,
			$semanticData->getProperties()
		);

		$expected = [
			'propertyKeys' => [ '_SKEY', 'HasFirstLinksUpdatetest', 'HasSecondLinksUpdatetest' ]
		];

		$this->semanticDataValidator->assertThatPropertiesAreSet(
			$expected,
			$semanticData
		);
	}

}