File: EditResultBuilderDbTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (338 lines) | stat: -rw-r--r-- 9,295 bytes parent folder | download | duplicates (2)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php

namespace MediaWiki\Tests\Storage;

use MediaWiki\CommentStore\CommentStoreComment;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Content\WikitextContent;
use MediaWiki\MainConfigNames;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\RevisionStore;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Storage\EditResult;
use MediaWiki\Storage\EditResultBuilder;
use MediaWikiIntegrationTestCase;
use MockTitleTrait;
use Wikimedia\Rdbms\IDatabase;
use WikiPage;

/**
 * @covers \MediaWiki\Storage\EditResultBuilder
 * @group Database
 * @see EditResultBuilderTest for non-DB tests
 */
class EditResultBuilderDbTest extends MediaWikiIntegrationTestCase {
	use MockTitleTrait;

	private const PAGE_NAME = 'ManualRevertTestPage';
	private const CONTENT_A = 'Aaa.';
	private const CONTENT_B = 'Bbb.';
	private const CONTENT_C = 'Ccc.';

	/** @var WikiPage */
	private $wikiPage;

	/** @var RevisionRecord[] */
	private $revisions;

	/**
	 * We track the top revision of the test page on our own to avoid having to update the
	 * page table in the DB.
	 *
	 * @var RevisionRecord
	 */
	private $latestTestRevision = null;

	/** @var RevisionStore */
	private $revisionStore;

	/** @var IDatabase */
	private $dbw;

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

		$services = $this->getServiceContainer();
		$this->revisionStore = $services->getRevisionStore();
		$this->dbw = $this->getDb();

		$this->wikiPage = $this->getExistingTestPage( self::PAGE_NAME );
		$this->revisions = [];
		$this->revisions['C1'] = $this->insertRevisionToTestPage(
			self::CONTENT_C,
			'20050101210030'
		);
		$this->revisions['A1'] = $this->insertRevisionToTestPage(
			self::CONTENT_A,
			'20050101210037'
		);
		$this->revisions['B1'] = $this->insertRevisionToTestPage(
			self::CONTENT_B,
			'20050101210038'
		);
		$this->revisions['C2'] = $this->insertRevisionToTestPage(
			self::CONTENT_C,
			'20050101210039'
		);
		$this->revisions['A2'] = $this->insertRevisionToTestPage(
			self::CONTENT_A,
			'20050101210040'
		);
		$this->revisions['A3'] = $this->insertRevisionToTestPage(
			self::CONTENT_A,
			'20050101210040' // same timestamp to try to confuse the query
		);
		$this->revisions['A4'] = $this->insertRevisionToTestPage(
			self::CONTENT_A,
			'20050101210040'
		);
		$this->revisions['B2'] = $this->insertRevisionToTestPage(
			self::CONTENT_B,
			'20050101210041'
		);
	}

	private function getLatestTestRevision(): RevisionRecord {
		return $this->latestTestRevision ??
			$this->revisionStore->getRevisionByPageId( $this->wikiPage->getId() );
	}

	/**
	 * Inserts a new revision of the test page to the DB with specified content.
	 *
	 * We do not use MediaWikiIntegrationTestCase::editPage() on purpose, it can lead to all
	 * kinds of issues, the most significant being that it ultimately calls the code we wish
	 * to test here.
	 *
	 * @param string $content
	 *
	 * @param string $timestamp
	 *
	 * @return RevisionRecord
	 */
	private function insertRevisionToTestPage(
		string $content,
		string $timestamp
	): RevisionRecord {
		$revisionRecord = $this->getNewRevisionForTestPage( $content );
		$revisionRecord->setUser( $this->getTestUser()->getUser() );
		$revisionRecord->setTimestamp( $timestamp );
		$revisionRecord->setComment( CommentStoreComment::newUnsavedComment( '' ) );

		$this->latestTestRevision = $this->revisionStore->insertRevisionOn(
			$revisionRecord,
			$this->dbw
		);
		return $this->latestTestRevision;
	}

	/**
	 * Returns a next in sequence revision of the test page with specified content.
	 *
	 * @param string $content
	 *
	 * @return MutableRevisionRecord
	 */
	private function getNewRevisionForTestPage(
		string $content
	): MutableRevisionRecord {
		$parentRevision = $this->getLatestTestRevision();

		$revision = new MutableRevisionRecord( $this->wikiPage->getTitle() );
		$revision->setParentId( $parentRevision->getId() );
		$revision->setPageId( $this->wikiPage->getId() );
		$revision->setContent(
			SlotRecord::MAIN,
			new WikitextContent( $content )
		);

		return $revision;
	}

	public static function provideManualReverts(): array {
		return [
			'reverting a single edit' => [
				self::CONTENT_A,
				'A4',
				'B2',
				'B2'
			],
			'reverting multiple edits' => [
				self::CONTENT_C,
				'C2',
				'A2',
				'B2'
			]
		];
	}

	/**
	 * @dataProvider provideManualReverts
	 * @covers \MediaWiki\Storage\EditResultBuilder::detectManualRevert
	 *
	 * @param string $content
	 * @param string $expectedOriginalRevKey
	 * @param string $expectedOldestRevertedRevKey
	 * @param string $expectedNewestRevertedRevKey
	 */
	public function testManualRevert(
		string $content,
		string $expectedOriginalRevKey,
		string $expectedOldestRevertedRevKey,
		string $expectedNewestRevertedRevKey
	) {
		$erb = $this->getEditResultBuilder();
		$newRevision = $this->getNewRevisionForTestPage( $content );
		// we will fool the EditResultBuilder into thinking this is a saved revision
		$newRevision->setId( 12345 );
		$erb->setRevisionRecord( $newRevision );

		$er = $erb->buildEditResult();

		// first some basic tests we can do without revision magic
		$this->assertTrue(
			$er->isRevert(),
			'EditResult::isRevert()'
		);
		$this->assertTrue(
			$er->isExactRevert(),
			'EditResult::isExactRevert()'
		);
		$this->assertSame(
			EditResult::REVERT_MANUAL,
			$er->getRevertMethod(),
			'EditResult::getRevertMethod()'
		);
		$this->assertNotFalse(
			$er->getOriginalRevisionId(),
			'EditResult::getOriginalRevisionId()'
		);
		$this->assertNotNull(
			$er->getOldestRevertedRevisionId(),
			'EditResult::getOldestRevertedRevisionId()'
		);
		$this->assertNotNull(
			$er->getNewestRevertedRevisionId(),
			'EditResult::getNewestRevertedRevisionId()'
		);
		$this->assertArrayEquals(
			[ 'mw-manual-revert' ],
			$er->getRevertTags(),
			'EditResult::getRevertTags()'
		);

		// test the original revision referenced by this EditResult
		$originalRev = $this->revisionStore->getRevisionById(
			$er->getOriginalRevisionId()
		);
		$this->assertSame(
			$newRevision->getSha1(),
			$originalRev->getSha1(),
			"original revision's SHA1 matches new revision's SHA1"
		);
		$expectedOriginalRev = $this->revisions[$expectedOriginalRevKey];
		$this->assertSame(
			$expectedOriginalRev->getId(),
			$originalRev->getId(),
			"original revision's ID"
		);

		// test the oldest reverted revision
		$oldestRevertedRev = $this->revisionStore->getRevisionById(
			$er->getOldestRevertedRevisionId()
		);
		$expectedOldestRevertedRev = $this->revisions[$expectedOldestRevertedRevKey];
		$this->assertSame(
			$expectedOldestRevertedRev->getId(),
			$oldestRevertedRev->getId(),
			"oldest reverted revision's ID"
		);

		// test the newest reverted revision
		$newestRevertedRev = $this->revisionStore->getRevisionById(
			$er->getNewestRevertedRevisionId()
		);
		$expectedNewestRevertedRev = $this->revisions[$expectedNewestRevertedRevKey];
		$this->assertSame(
			$expectedNewestRevertedRev->getId(),
			$newestRevertedRev->getId(),
			"newest reverted revision's ID"
		);
	}

	public static function provideNotManualReverts(): array {
		return [
			'edit not changing anything' => [
				self::CONTENT_B,
				15
			],
			'revert outside search radius' => [
				self::CONTENT_C,
				3
			],
			'normal edit' => [
				'Some text.',
				15
			]
		];
	}

	/**
	 * @dataProvider provideNotManualReverts
	 * @covers \MediaWiki\Storage\EditResultBuilder::detectManualRevert
	 *
	 * @param string $content
	 * @param int $searchRadius
	 */
	public function testNotManualRevert(
		string $content,
		int $searchRadius
	) {
		$erb = $this->getEditResultBuilder( $searchRadius );
		$parentRevision = $this->getLatestTestRevision();
		$newRevision = $this->getNewRevisionForTestPage( $content );
		// we will fool the EditResultBuilder into thinking this is a saved revision
		$newRevision->setId( 12345 );
		$erb->setRevisionRecord( $newRevision );
		// emulate WikiPage's behaviour for null edits
		if ( $newRevision->getSha1() === $parentRevision->getSha1() ) {
			$erb->setOriginalRevision( $parentRevision );
		}

		$er = $erb->buildEditResult();

		$this->assertFalse( $er->isRevert(), 'EditResult::isRevert()' );
		$this->assertFalse( $er->isExactRevert(), 'EditResult::isExactRevert()' );
		$this->assertNull( $er->getRevertMethod(), 'EditResult::getRevertMethod()' );
		$this->assertNull(
			$er->getOldestRevertedRevisionId(),
			'EditResult::getOldestRevertedRevisionId()'
		);
		$this->assertNull(
			$er->getNewestRevertedRevisionId(),
			'EditResult::getNewestRevertedRevisionId()'
		);
		$this->assertArrayEquals( [], $er->getRevertTags(), 'EditResult::getRevertTags()' );
	}

	/**
	 * Convenience function for creating a new EditResultBuilder object.
	 *
	 * @param int $manualRevertSearchRadius
	 *
	 * @return EditResultBuilder
	 */
	private function getEditResultBuilder( int $manualRevertSearchRadius = 15 ) {
		$options = new ServiceOptions(
			EditResultBuilder::CONSTRUCTOR_OPTIONS,
			[ MainConfigNames::ManualRevertSearchRadius => $manualRevertSearchRadius ]
		);

		return new EditResultBuilder(
			$this->getServiceContainer()->getRevisionStore(),
			$this->getServiceContainer()->getChangeTagsStore()->listSoftwareDefinedTags(),
			$options
		);
	}
}