15a991e38721f42253cb8e32f5c4ca1874bd3bff
[lhc/web/wiklou.git] / tests / phpunit / includes / PageArchiveTest.php
1 <?php
2
3 /**
4 * Test class for page archiving.
5 *
6 * @group ContentHandler
7 * @group Database
8 * ^--- important, causes temporary tables to be used instead of the real database
9 *
10 * @group medium
11 * ^--- important, causes tests not to fail with timeout
12 */
13 class PageArchiveTest extends MediaWikiTestCase {
14
15 /**
16 * @var PageArchive $archivedPage
17 */
18 private $archivedPage;
19
20 /**
21 * A logged out user who edited the page before it was archived.
22 * @var string $ipEditor
23 */
24 private $ipEditor;
25
26 /**
27 * Revision ID of the IP edit
28 * @var int $ipRevId
29 */
30 private $ipRevId;
31
32 function __construct( $name = null, array $data = [], $dataName = '' ) {
33 parent::__construct( $name, $data, $dataName );
34
35 $this->tablesUsed = array_merge(
36 $this->tablesUsed,
37 [
38 'page',
39 'revision',
40 'ip_changes',
41 'text',
42 'archive',
43 'recentchanges',
44 'logging',
45 'page_props',
46 ]
47 );
48 }
49
50 protected function setUp() {
51 parent::setUp();
52
53 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
54 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
55 $this->setMwGlobals( 'wgMultiContentRevisionSchemaMigrationStage', SCHEMA_COMPAT_OLD );
56 $this->overrideMwServices();
57
58 // First create our dummy page
59 $page = Title::newFromText( 'PageArchiveTest_thePage' );
60 $page = new WikiPage( $page );
61 $content = ContentHandler::makeContent(
62 'testing',
63 $page->getTitle(),
64 CONTENT_MODEL_WIKITEXT
65 );
66 $page->doEditContent( $content, 'testing', EDIT_NEW );
67
68 // Insert IP revision
69 $this->ipEditor = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
70 $rev = new Revision( [
71 'text' => 'Lorem Ipsum',
72 'comment' => 'just a test',
73 'page' => $page->getId(),
74 'user_text' => $this->ipEditor,
75 ] );
76 $dbw = wfGetDB( DB_MASTER );
77 $this->ipRevId = $rev->insertOn( $dbw );
78
79 // Delete the page
80 $page->doDeleteArticleReal( 'Just a test deletion' );
81
82 $this->archivedPage = new PageArchive( $page->getTitle() );
83 }
84
85 /**
86 * @covers PageArchive::undelete
87 * @covers PageArchive::undeleteRevisions
88 */
89 public function testUndeleteRevisions() {
90 // First make sure old revisions are archived
91 $dbr = wfGetDB( DB_REPLICA );
92 $arQuery = Revision::getArchiveQueryInfo();
93 $res = $dbr->select(
94 $arQuery['tables'],
95 $arQuery['fields'],
96 [ 'ar_rev_id' => $this->ipRevId ],
97 __METHOD__,
98 [],
99 $arQuery['joins']
100 );
101 $row = $res->fetchObject();
102 $this->assertEquals( $this->ipEditor, $row->ar_user_text );
103
104 // Should not be in revision
105 $res = $dbr->select( 'revision', '1', [ 'rev_id' => $this->ipRevId ] );
106 $this->assertFalse( $res->fetchObject() );
107
108 // Should not be in ip_changes
109 $res = $dbr->select( 'ip_changes', '1', [ 'ipc_rev_id' => $this->ipRevId ] );
110 $this->assertFalse( $res->fetchObject() );
111
112 // Restore the page
113 $this->archivedPage->undelete( [] );
114
115 // Should be back in revision
116 $revQuery = Revision::getQueryInfo();
117 $res = $dbr->select(
118 $revQuery['tables'],
119 $revQuery['fields'],
120 [ 'rev_id' => $this->ipRevId ],
121 __METHOD__,
122 [],
123 $revQuery['joins']
124 );
125 $row = $res->fetchObject();
126 $this->assertEquals( $this->ipEditor, $row->rev_user_text );
127
128 // Should be back in ip_changes
129 $res = $dbr->select( 'ip_changes', [ 'ipc_hex' ], [ 'ipc_rev_id' => $this->ipRevId ] );
130 $row = $res->fetchObject();
131 $this->assertEquals( IP::toHex( $this->ipEditor ), $row->ipc_hex );
132 }
133
134 /**
135 * @covers PageArchive::listRevisions
136 */
137 public function testListRevisions() {
138 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
139 $this->setMwGlobals( 'wgMultiContentRevisionSchemaMigrationStage', SCHEMA_COMPAT_OLD );
140 $this->overrideMwServices();
141
142 $revisions = $this->archivedPage->listRevisions();
143 $this->assertEquals( 2, $revisions->numRows() );
144
145 // Get the rows as arrays
146 $row1 = (array)$revisions->current();
147 $row2 = (array)$revisions->next();
148 // Unset the timestamps (we assume they will be right...
149 $this->assertInternalType( 'string', $row1['ar_timestamp'] );
150 $this->assertInternalType( 'string', $row2['ar_timestamp'] );
151 unset( $row1['ar_timestamp'] );
152 unset( $row2['ar_timestamp'] );
153
154 $this->assertEquals(
155 [
156 'ar_minor_edit' => '0',
157 'ar_user' => '0',
158 'ar_user_text' => '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7',
159 'ar_actor' => null,
160 'ar_len' => '11',
161 'ar_deleted' => '0',
162 'ar_rev_id' => '3',
163 'ar_sha1' => '0qdrpxl537ivfnx4gcpnzz0285yxryy',
164 'ar_page_id' => '2',
165 'ar_comment_text' => 'just a test',
166 'ar_comment_data' => null,
167 'ar_comment_cid' => null,
168 'ar_content_format' => null,
169 'ar_content_model' => null,
170 'ts_tags' => null,
171 'ar_id' => '2',
172 'ar_namespace' => '0',
173 'ar_title' => 'PageArchiveTest_thePage',
174 'ar_text_id' => '3',
175 'ar_parent_id' => '2',
176 ],
177 $row1
178 );
179 $this->assertEquals(
180 [
181 'ar_minor_edit' => '0',
182 'ar_user' => '0',
183 'ar_user_text' => '127.0.0.1',
184 'ar_actor' => null,
185 'ar_len' => '7',
186 'ar_deleted' => '0',
187 'ar_rev_id' => '2',
188 'ar_sha1' => 'pr0s8e18148pxhgjfa0gjrvpy8fiyxc',
189 'ar_page_id' => '2',
190 'ar_comment_text' => 'testing',
191 'ar_comment_data' => null,
192 'ar_comment_cid' => null,
193 'ar_content_format' => null,
194 'ar_content_model' => null,
195 'ts_tags' => null,
196 'ar_id' => '1',
197 'ar_namespace' => '0',
198 'ar_title' => 'PageArchiveTest_thePage',
199 'ar_text_id' => '2',
200 'ar_parent_id' => '0',
201 ],
202 $row2
203 );
204 }
205
206 /**
207 * @covers PageArchive::listPagesBySearch
208 */
209 public function testListPagesBySearch() {
210 $pages = PageArchive::listPagesBySearch( 'PageArchiveTest_thePage' );
211 $this->assertSame( 1, $pages->numRows() );
212
213 $page = (array)$pages->current();
214
215 $this->assertSame(
216 [
217 'ar_namespace' => '0',
218 'ar_title' => 'PageArchiveTest_thePage',
219 'count' => '2',
220 ],
221 $page
222 );
223 }
224
225 /**
226 * @covers PageArchive::listPagesBySearch
227 */
228 public function testListPagesByPrefix() {
229 $pages = PageArchive::listPagesByPrefix( 'PageArchiveTest' );
230 $this->assertSame( 1, $pages->numRows() );
231
232 $page = (array)$pages->current();
233
234 $this->assertSame(
235 [
236 'ar_namespace' => '0',
237 'ar_title' => 'PageArchiveTest_thePage',
238 'count' => '2',
239 ],
240 $page
241 );
242 }
243
244 /**
245 * @covers PageArchive::getTextFromRow
246 */
247 public function testGetTextFromRow() {
248 $row = (object)[ 'ar_text_id' => 2 ];
249 $text = $this->archivedPage->getTextFromRow( $row );
250 $this->assertSame( 'testing', $text );
251 }
252
253 /**
254 * @covers PageArchive::getLastRevisionText
255 */
256 public function testGetLastRevisionText() {
257 $text = $this->archivedPage->getLastRevisionText();
258 $this->assertSame( 'Lorem Ipsum', $text );
259 }
260
261 /**
262 * @covers PageArchive::isDeleted
263 */
264 public function testIsDeleted() {
265 $this->assertTrue( $this->archivedPage->isDeleted() );
266 }
267 }