Debian Codegraph @

tests/phpunit/integration/includes/Storage/EditResultBuilderDbTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use MediaWiki\CommentStore\CommentStoreComment;
6 use MediaWiki\Config\ServiceOptions;
7 use MediaWiki\Content\WikitextContent;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\Revision\MutableRevisionRecord;
10 use MediaWiki\Revision\RevisionRecord;
11 use MediaWiki\Revision\RevisionStore;
12 use MediaWiki\Revision\SlotRecord;
13 use MediaWiki\Storage\EditResult;
14 use MediaWiki\Storage\EditResultBuilder;
15 use MediaWikiIntegrationTestCase;
16 use MockTitleTrait;
17 use Wikimedia\Rdbms\IDatabase;
18 use WikiPage;
19
20 /**
21 * @covers \MediaWiki\Storage\EditResultBuilder
22 * @group Database
23 * @see EditResultBuilderTest for non-DB tests
24 */
25 class EditResultBuilderDbTest extends MediaWikiIntegrationTestCase {
26 use MockTitleTrait;
27
28 private const PAGE_NAME = 'ManualRevertTestPage';
29 private const CONTENT_A = 'Aaa.';
30 private const CONTENT_B = 'Bbb.';
31 private const CONTENT_C = 'Ccc.';
32
33 /** @var WikiPage */
34 private $wikiPage;
35
36 /** @var RevisionRecord[] */
37 private $revisions;
38
39 /**
40 * We track the top revision of the test page on our own to avoid having to update the
41 * page table in the DB.
42 *
43 * @var RevisionRecord
44 */
45 private $latestTestRevision = null;
46
47 /** @var RevisionStore */
48 private $revisionStore;
49
50 /** @var IDatabase */
51 private $dbw;
52
53 protected function setUp(): void {
54 parent::setUp();
55
56 $services = $this->getServiceContainer();
57 $this->revisionStore = $services->getRevisionStore();
58 $this->dbw = $this->getDb();
59
60 $this->wikiPage = $this->getExistingTestPage( self::PAGE_NAME );
61 $this->revisions = [];
62 $this->revisions['C1'] = $this->insertRevisionToTestPage(
63 self::CONTENT_C,
64 '20050101210030'
65 );
66 $this->revisions['A1'] = $this->insertRevisionToTestPage(
67 self::CONTENT_A,
68 '20050101210037'
69 );
70 $this->revisions['B1'] = $this->insertRevisionToTestPage(
71 self::CONTENT_B,
72 '20050101210038'
73 );
74 $this->revisions['C2'] = $this->insertRevisionToTestPage(
75 self::CONTENT_C,
76 '20050101210039'
77 );
78 $this->revisions['A2'] = $this->insertRevisionToTestPage(
79 self::CONTENT_A,
80 '20050101210040'
81 );
82 $this->revisions['A3'] = $this->insertRevisionToTestPage(
83 self::CONTENT_A,
84 '20050101210040' // same timestamp to try to confuse the query
85 );
86 $this->revisions['A4'] = $this->insertRevisionToTestPage(
87 self::CONTENT_A,
88 '20050101210040'
89 );
90 $this->revisions['B2'] = $this->insertRevisionToTestPage(
91 self::CONTENT_B,
92 '20050101210041'
93 );
94 }
95
96 private function getLatestTestRevision(): RevisionRecord {
97 return $this->latestTestRevision ??
98 $this->revisionStore->getRevisionByPageId( $this->wikiPage->getId() );
99 }
100
101 /**
102 * Inserts a new revision of the test page to the DB with specified content.
103 *
104 * We do not use MediaWikiIntegrationTestCase::editPage() on purpose, it can lead to all
105 * kinds of issues, the most significant being that it ultimately calls the code we wish
106 * to test here.
107 *
108 * @param string $content
109 *
110 * @param string $timestamp
111 *
112 * @return RevisionRecord
113 */
114 private function insertRevisionToTestPage(
115 string $content,
116 string $timestamp
117 ): RevisionRecord {
118 $revisionRecord = $this->getNewRevisionForTestPage( $content );
119 $revisionRecord->setUser( $this->getTestUser()->getUser() );
120 $revisionRecord->setTimestamp( $timestamp );
121 $revisionRecord->setComment( CommentStoreComment::newUnsavedComment( '' ) );
122
123 $this->latestTestRevision = $this->revisionStore->insertRevisionOn(
124 $revisionRecord,
125 $this->dbw
126 );
127 return $this->latestTestRevision;
128 }
129
130 /**
131 * Returns a next in sequence revision of the test page with specified content.
132 *
133 * @param string $content
134 *
135 * @return MutableRevisionRecord
136 */
137 private function getNewRevisionForTestPage(
138 string $content
139 ): MutableRevisionRecord {
140 $parentRevision = $this->getLatestTestRevision();
141
142 $revision = new MutableRevisionRecord( $this->wikiPage->getTitle() );
143 $revision->setParentId( $parentRevision->getId() );
144 $revision->setPageId( $this->wikiPage->getId() );
145 $revision->setContent(
146 SlotRecord::MAIN,
147 new WikitextContent( $content )
148 );
149
150 return $revision;
151 }
152
153 public static function provideManualReverts(): array {
154 return [
155 'reverting a single edit' => [
156 self::CONTENT_A,
157 'A4',
158 'B2',
159 'B2'
160 ],
161 'reverting multiple edits' => [
162 self::CONTENT_C,
163 'C2',
164 'A2',
165 'B2'
166 ]
167 ];
168 }
169
170 /**
171 * @dataProvider provideManualReverts
172 * @covers \MediaWiki\Storage\EditResultBuilder::detectManualRevert
173 *
174 * @param string $content
175 * @param string $expectedOriginalRevKey
176 * @param string $expectedOldestRevertedRevKey
177 * @param string $expectedNewestRevertedRevKey
178 */
179 public function testManualRevert(
180 string $content,
181 string $expectedOriginalRevKey,
182 string $expectedOldestRevertedRevKey,
183 string $expectedNewestRevertedRevKey
184 ) {
185 $erb = $this->getEditResultBuilder();
186 $newRevision = $this->getNewRevisionForTestPage( $content );
187 // we will fool the EditResultBuilder into thinking this is a saved revision
188 $newRevision->setId( 12345 );
189 $erb->setRevisionRecord( $newRevision );
190
191 $er = $erb->buildEditResult();
192
193 // first some basic tests we can do without revision magic
194 $this->assertTrue(
195 $er->isRevert(),
196 'EditResult::isRevert()'
197 );
198 $this->assertTrue(
199 $er->isExactRevert(),
200 'EditResult::isExactRevert()'
201 );
202 $this->assertSame(
203 EditResult::REVERT_MANUAL,
204 $er->getRevertMethod(),
205 'EditResult::getRevertMethod()'
206 );
207 $this->assertNotFalse(
208 $er->getOriginalRevisionId(),
209 'EditResult::getOriginalRevisionId()'
210 );
211 $this->assertNotNull(
212 $er->getOldestRevertedRevisionId(),
213 'EditResult::getOldestRevertedRevisionId()'
214 );
215 $this->assertNotNull(
216 $er->getNewestRevertedRevisionId(),
217 'EditResult::getNewestRevertedRevisionId()'
218 );
219 $this->assertArrayEquals(
220 [ 'mw-manual-revert' ],
221 $er->getRevertTags(),
222 'EditResult::getRevertTags()'
223 );
224
225 // test the original revision referenced by this EditResult
226 $originalRev = $this->revisionStore->getRevisionById(
227 $er->getOriginalRevisionId()
228 );
229 $this->assertSame(
230 $newRevision->getSha1(),
231 $originalRev->getSha1(),
232 "original revision's SHA1 matches new revision's SHA1"
233 );
234 $expectedOriginalRev = $this->revisions[$expectedOriginalRevKey];
235 $this->assertSame(
236 $expectedOriginalRev->getId(),
237 $originalRev->getId(),
238 "original revision's ID"
239 );
240
241 // test the oldest reverted revision
242 $oldestRevertedRev = $this->revisionStore->getRevisionById(
243 $er->getOldestRevertedRevisionId()
244 );
245 $expectedOldestRevertedRev = $this->revisions[$expectedOldestRevertedRevKey];
246 $this->assertSame(
247 $expectedOldestRevertedRev->getId(),
248 $oldestRevertedRev->getId(),
249 "oldest reverted revision's ID"
250 );
251
252 // test the newest reverted revision
253 $newestRevertedRev = $this->revisionStore->getRevisionById(
254 $er->getNewestRevertedRevisionId()
255 );
256 $expectedNewestRevertedRev = $this->revisions[$expectedNewestRevertedRevKey];
257 $this->assertSame(
258 $expectedNewestRevertedRev->getId(),
259 $newestRevertedRev->getId(),
260 "newest reverted revision's ID"
261 );
262 }
263
264 public static function provideNotManualReverts(): array {
265 return [
266 'edit not changing anything' => [
267 self::CONTENT_B,
268 15
269 ],
270 'revert outside search radius' => [
271 self::CONTENT_C,
272 3
273 ],
274 'normal edit' => [
275 'Some text.',
276 15
277 ]
278 ];
279 }
280
281 /**
282 * @dataProvider provideNotManualReverts
283 * @covers \MediaWiki\Storage\EditResultBuilder::detectManualRevert
284 *
285 * @param string $content
286 * @param int $searchRadius
287 */
288 public function testNotManualRevert(
289 string $content,
290 int $searchRadius
291 ) {
292 $erb = $this->getEditResultBuilder( $searchRadius );
293 $parentRevision = $this->getLatestTestRevision();
294 $newRevision = $this->getNewRevisionForTestPage( $content );
295 // we will fool the EditResultBuilder into thinking this is a saved revision
296 $newRevision->setId( 12345 );
297 $erb->setRevisionRecord( $newRevision );
298 // emulate WikiPage's behaviour for null edits
299 if ( $newRevision->getSha1() === $parentRevision->getSha1() ) {
300 $erb->setOriginalRevision( $parentRevision );
301 }
302
303 $er = $erb->buildEditResult();
304
305 $this->assertFalse( $er->isRevert(), 'EditResult::isRevert()' );
306 $this->assertFalse( $er->isExactRevert(), 'EditResult::isExactRevert()' );
307 $this->assertNull( $er->getRevertMethod(), 'EditResult::getRevertMethod()' );
308 $this->assertNull(
309 $er->getOldestRevertedRevisionId(),
310 'EditResult::getOldestRevertedRevisionId()'
311 );
312 $this->assertNull(
313 $er->getNewestRevertedRevisionId(),
314 'EditResult::getNewestRevertedRevisionId()'
315 );
316 $this->assertArrayEquals( [], $er->getRevertTags(), 'EditResult::getRevertTags()' );
317 }
318
319 /**
320 * Convenience function for creating a new EditResultBuilder object.
321 *
322 * @param int $manualRevertSearchRadius
323 *
324 * @return EditResultBuilder
325 */
326 private function getEditResultBuilder( int $manualRevertSearchRadius = 15 ) {
327 $options = new ServiceOptions(
328 EditResultBuilder::CONSTRUCTOR_OPTIONS,
329 [ MainConfigNames::ManualRevertSearchRadius => $manualRevertSearchRadius ]
330 );
331
332 return new EditResultBuilder(
333 $this->getServiceContainer()->getRevisionStore(),
334 $this->getServiceContainer()->getChangeTagsStore()->listSoftwareDefinedTags(),
335 $options
336 );
337 }
338 }