Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / includes / page / PageArchiveTestBase.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use MediaWiki\Revision\RevisionRecord;
5
6 /**
7 * Base class for tests of PageArchive against different database schemas.
8 */
9 abstract class PageArchiveTestBase extends MediaWikiTestCase {
10
11 /**
12 * @var int
13 */
14 protected $pageId;
15
16 /**
17 * @var PageArchive $archivedPage
18 */
19 protected $archivedPage;
20
21 /**
22 * A logged out user who edited the page before it was archived.
23 * @var string $ipEditor
24 */
25 protected $ipEditor;
26
27 /**
28 * Revision of the first (initial) edit
29 * @var RevisionRecord
30 */
31 protected $firstRev;
32
33 /**
34 * Revision of the IP edit (the second edit)
35 * @var RevisionRecord
36 */
37 protected $ipRev;
38
39 function __construct( $name = null, array $data = [], $dataName = '' ) {
40 parent::__construct( $name, $data, $dataName );
41
42 $this->tablesUsed = array_merge(
43 $this->tablesUsed,
44 [
45 'page',
46 'revision',
47 'revision_comment_temp',
48 'ip_changes',
49 'text',
50 'archive',
51 'recentchanges',
52 'logging',
53 'page_props',
54 'comment',
55 ]
56 );
57 }
58
59 protected function addCoreDBData() {
60 // Blank out to avoid failures when schema overrides imposed by subclasses
61 // affect revision storage.
62 }
63
64 /**
65 * @return int
66 */
67 abstract protected function getMcrMigrationStage();
68
69 /**
70 * @return string[]
71 */
72 abstract protected function getMcrTablesToReset();
73
74 /**
75 * @return bool
76 */
77 protected function getContentHandlerUseDB() {
78 return true;
79 }
80
81 protected function setUp() {
82 parent::setUp();
83
84 $this->tablesUsed += $this->getMcrTablesToReset();
85
86 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_NEW );
87 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
88 $this->setMwGlobals(
89 'wgMultiContentRevisionSchemaMigrationStage',
90 $this->getMcrMigrationStage()
91 );
92 $this->overrideMwServices();
93
94 // First create our dummy page
95 $page = Title::newFromText( 'PageArchiveTest_thePage' );
96 $page = new WikiPage( $page );
97 $content = ContentHandler::makeContent(
98 'testing',
99 $page->getTitle(),
100 CONTENT_MODEL_WIKITEXT
101 );
102
103 $user = $this->getTestUser()->getUser();
104 $page->doEditContent( $content, 'testing', EDIT_NEW, false, $user );
105
106 $this->pageId = $page->getId();
107 $this->firstRev = $page->getRevision()->getRevisionRecord();
108
109 // Insert IP revision
110 $this->ipEditor = '2001:db8::1';
111
112 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
113
114 $ipTimestamp = wfTimestamp(
115 TS_MW,
116 wfTimestamp( TS_UNIX, $this->firstRev->getTimestamp() ) + 1
117 );
118
119 $rev = $revisionStore->newMutableRevisionFromArray( [
120 'text' => 'Lorem Ipsum',
121 'comment' => 'just a test',
122 'page' => $page->getId(),
123 'user_text' => $this->ipEditor,
124 'timestamp' => $ipTimestamp,
125 ] );
126
127 $dbw = wfGetDB( DB_MASTER );
128 $this->ipRev = $revisionStore->insertRevisionOn( $rev, $dbw );
129
130 // Delete the page
131 $page->doDeleteArticleReal( 'Just a test deletion' );
132
133 $this->archivedPage = new PageArchive( $page->getTitle() );
134 }
135
136 /**
137 * @covers PageArchive::undelete
138 * @covers PageArchive::undeleteRevisions
139 */
140 public function testUndeleteRevisions() {
141 // TODO: MCR: Test undeletion with multiple slots. Check that slots remain untouched.
142
143 // First make sure old revisions are archived
144 $dbr = wfGetDB( DB_REPLICA );
145 $arQuery = Revision::getArchiveQueryInfo();
146 $row = $dbr->selectRow(
147 $arQuery['tables'],
148 $arQuery['fields'],
149 [ 'ar_rev_id' => $this->ipRev->getId() ],
150 __METHOD__,
151 [],
152 $arQuery['joins']
153 );
154 $this->assertEquals( $this->ipEditor, $row->ar_user_text );
155
156 // Should not be in revision
157 $row = $dbr->selectRow( 'revision', '1', [ 'rev_id' => $this->ipRev->getId() ] );
158 $this->assertFalse( $row );
159
160 // Should not be in ip_changes
161 $row = $dbr->selectRow( 'ip_changes', '1', [ 'ipc_rev_id' => $this->ipRev->getId() ] );
162 $this->assertFalse( $row );
163
164 // Restore the page
165 $this->archivedPage->undelete( [] );
166
167 // Should be back in revision
168 $revQuery = Revision::getQueryInfo();
169 $row = $dbr->selectRow(
170 $revQuery['tables'],
171 $revQuery['fields'],
172 [ 'rev_id' => $this->ipRev->getId() ],
173 __METHOD__,
174 [],
175 $revQuery['joins']
176 );
177 $this->assertNotFalse( $row, 'row exists in revision table' );
178 $this->assertEquals( $this->ipEditor, $row->rev_user_text );
179
180 // Should be back in ip_changes
181 $row = $dbr->selectRow( 'ip_changes', [ 'ipc_hex' ], [ 'ipc_rev_id' => $this->ipRev->getId() ] );
182 $this->assertNotFalse( $row, 'row exists in ip_changes table' );
183 $this->assertEquals( IP::toHex( $this->ipEditor ), $row->ipc_hex );
184 }
185
186 abstract protected function getExpectedArchiveRows();
187
188 /**
189 * @covers PageArchive::listRevisions
190 */
191 public function testListRevisions() {
192 $revisions = $this->archivedPage->listRevisions();
193 $this->assertEquals( 2, $revisions->numRows() );
194
195 // Get the rows as arrays
196 $row0 = (array)$revisions->current();
197 $row1 = (array)$revisions->next();
198
199 $expectedRows = $this->getExpectedArchiveRows();
200
201 $this->assertEquals(
202 $expectedRows[0],
203 $row0
204 );
205 $this->assertEquals(
206 $expectedRows[1],
207 $row1
208 );
209 }
210
211 /**
212 * @covers PageArchive::listPagesBySearch
213 */
214 public function testListPagesBySearch() {
215 $pages = PageArchive::listPagesBySearch( 'PageArchiveTest_thePage' );
216 $this->assertSame( 1, $pages->numRows() );
217
218 $page = (array)$pages->current();
219
220 $this->assertSame(
221 [
222 'ar_namespace' => '0',
223 'ar_title' => 'PageArchiveTest_thePage',
224 'count' => '2',
225 ],
226 $page
227 );
228 }
229
230 /**
231 * @covers PageArchive::listPagesBySearch
232 */
233 public function testListPagesByPrefix() {
234 $pages = PageArchive::listPagesByPrefix( 'PageArchiveTest' );
235 $this->assertSame( 1, $pages->numRows() );
236
237 $page = (array)$pages->current();
238
239 $this->assertSame(
240 [
241 'ar_namespace' => '0',
242 'ar_title' => 'PageArchiveTest_thePage',
243 'count' => '2',
244 ],
245 $page
246 );
247 }
248
249 public function provideGetTextFromRowThrowsInvalidArgumentException() {
250 yield 'missing ar_text_id field' => [ [] ];
251 yield 'ar_text_id is null' => [ [ 'ar_text_id' => null ] ];
252 yield 'ar_text_id is zero' => [ [ 'ar_text_id' => 0 ] ];
253 yield 'ar_text_id is "0"' => [ [ 'ar_text_id' => '0' ] ];
254 }
255
256 /**
257 * @covers PageArchive::getLastRevisionId
258 */
259 public function testGetLastRevisionId() {
260 $id = $this->archivedPage->getLastRevisionId();
261 $this->assertSame( $this->ipRev->getId(), $id );
262 }
263
264 /**
265 * @covers PageArchive::isDeleted
266 */
267 public function testIsDeleted() {
268 $this->assertTrue( $this->archivedPage->isDeleted() );
269 }
270
271 /**
272 * @covers PageArchive::getRevision
273 */
274 public function testGetRevision() {
275 $rev = $this->archivedPage->getRevision( $this->ipRev->getTimestamp() );
276 $this->assertNotNull( $rev );
277 $this->assertSame( $this->pageId, $rev->getPage() );
278
279 $rev = $this->archivedPage->getRevision( '22991212115555' );
280 $this->assertNull( $rev );
281 }
282
283 /**
284 * @covers PageArchive::getRevision
285 */
286 public function testGetArchivedRevision() {
287 $rev = $this->archivedPage->getArchivedRevision( $this->ipRev->getId() );
288 $this->assertNotNull( $rev );
289 $this->assertSame( $this->ipRev->getTimestamp(), $rev->getTimestamp() );
290 $this->assertSame( $this->pageId, $rev->getPage() );
291
292 $rev = $this->archivedPage->getArchivedRevision( 632546 );
293 $this->assertNull( $rev );
294 }
295
296 /**
297 * @covers PageArchive::getPreviousRevision
298 */
299 public function testGetPreviousRevision() {
300 $rev = $this->archivedPage->getPreviousRevision( $this->ipRev->getTimestamp() );
301 $this->assertNotNull( $rev );
302 $this->assertSame( $this->firstRev->getId(), $rev->getId() );
303
304 $rev = $this->archivedPage->getPreviousRevision( $this->firstRev->getTimestamp() );
305 $this->assertNull( $rev );
306
307 // Re-create our dummy page
308 $title = Title::newFromText( 'PageArchiveTest_thePage' );
309 $page = new WikiPage( $title );
310 $content = ContentHandler::makeContent(
311 'testing again',
312 $page->getTitle(),
313 CONTENT_MODEL_WIKITEXT
314 );
315
316 $user = $this->getTestUser()->getUser();
317 $status = $page->doEditContent( $content, 'testing', EDIT_NEW, false, $user );
318
319 /** @var Revision $newRev */
320 $newRev = $status->value['revision'];
321
322 // force the revision timestamp
323 $newTimestamp = wfTimestamp(
324 TS_MW,
325 wfTimestamp( TS_UNIX, $this->ipRev->getTimestamp() ) + 1
326 );
327
328 $this->db->update(
329 'revision',
330 [ 'rev_timestamp' => $this->db->timestamp( $newTimestamp ) ],
331 [ 'rev_id' => $newRev->getId() ]
332 );
333
334 // check that we don't get the existing revision too soon.
335 $rev = $this->archivedPage->getPreviousRevision( $newTimestamp );
336 $this->assertNotNull( $rev );
337 $this->assertSame( $this->ipRev->getId(), $rev->getId() );
338
339 // check that we do get the existing revision when appropriate.
340 $afterNewTimestamp = wfTimestamp(
341 TS_MW,
342 wfTimestamp( TS_UNIX, $newTimestamp ) + 1
343 );
344
345 $rev = $this->archivedPage->getPreviousRevision( $afterNewTimestamp );
346 $this->assertNotNull( $rev );
347 $this->assertSame( $newRev->getId(), $rev->getId() );
348 }
349
350 }