Add tests for ExtensionJsonValidator
[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 // First create our dummy page
54 $page = Title::newFromText( 'PageArchiveTest_thePage' );
55 $page = new WikiPage( $page );
56 $content = ContentHandler::makeContent(
57 'testing',
58 $page->getTitle(),
59 CONTENT_MODEL_WIKITEXT
60 );
61 $page->doEditContent( $content, 'testing', EDIT_NEW );
62
63 // Insert IP revision
64 $this->ipEditor = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
65 $rev = new Revision( [
66 'text' => 'Lorem Ipsum',
67 'comment' => 'just a test',
68 'page' => $page->getId(),
69 'user_text' => $this->ipEditor,
70 ] );
71 $dbw = wfGetDB( DB_MASTER );
72 $this->ipRevId = $rev->insertOn( $dbw );
73
74 // Delete the page
75 $page->doDeleteArticleReal( 'Just a test deletion' );
76
77 $this->archivedPage = new PageArchive( $page->getTitle() );
78 }
79
80 /**
81 * @covers PageArchive::undelete
82 * @covers PageArchive::undeleteRevisions
83 */
84 public function testUndeleteRevisions() {
85 // First make sure old revisions are archived
86 $dbr = wfGetDB( DB_REPLICA );
87 $res = $dbr->select( 'archive', '*', [ 'ar_rev_id' => $this->ipRevId ] );
88 $row = $res->fetchObject();
89 $this->assertEquals( $this->ipEditor, $row->ar_user_text );
90
91 // Should not be in revision
92 $res = $dbr->select( 'revision', '*', [ 'rev_id' => $this->ipRevId ] );
93 $this->assertFalse( $res->fetchObject() );
94
95 // Should not be in ip_changes
96 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $this->ipRevId ] );
97 $this->assertFalse( $res->fetchObject() );
98
99 // Restore the page
100 $this->archivedPage->undelete( [] );
101
102 // Should be back in revision
103 $res = $dbr->select( 'revision', '*', [ 'rev_id' => $this->ipRevId ] );
104 $row = $res->fetchObject();
105 $this->assertEquals( $this->ipEditor, $row->rev_user_text );
106
107 // Should be back in ip_changes
108 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $this->ipRevId ] );
109 $row = $res->fetchObject();
110 $this->assertEquals( IP::toHex( $this->ipEditor ), $row->ipc_hex );
111 }
112
113 /**
114 * @covers PageArchive::listRevisions
115 */
116 public function testListRevisions() {
117 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
118 $this->overrideMwServices();
119
120 $revisions = $this->archivedPage->listRevisions();
121 $this->assertEquals( 2, $revisions->numRows() );
122
123 // Get the rows as arrays
124 $row1 = (array)$revisions->current();
125 $row2 = (array)$revisions->next();
126 // Unset the timestamps (we assume they will be right...
127 $this->assertInternalType( 'string', $row1['ar_timestamp'] );
128 $this->assertInternalType( 'string', $row2['ar_timestamp'] );
129 unset( $row1['ar_timestamp'] );
130 unset( $row2['ar_timestamp'] );
131
132 $this->assertEquals(
133 [
134 'ar_minor_edit' => '0',
135 'ar_user' => '0',
136 'ar_user_text' => '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7',
137 'ar_len' => '11',
138 'ar_deleted' => '0',
139 'ar_rev_id' => '3',
140 'ar_sha1' => '0qdrpxl537ivfnx4gcpnzz0285yxryy',
141 'ar_page_id' => '2',
142 'ar_comment_text' => 'just a test',
143 'ar_comment_data' => null,
144 'ar_comment_cid' => null,
145 'ar_content_format' => null,
146 'ar_content_model' => null,
147 'ts_tags' => null,
148 'ar_id' => '2',
149 'ar_namespace' => '0',
150 'ar_title' => 'PageArchiveTest_thePage',
151 'ar_text' => '',
152 'ar_text_id' => '3',
153 'ar_parent_id' => '2',
154 ],
155 $row1
156 );
157 $this->assertEquals(
158 [
159 'ar_minor_edit' => '0',
160 'ar_user' => '0',
161 'ar_user_text' => '127.0.0.1',
162 'ar_len' => '7',
163 'ar_deleted' => '0',
164 'ar_rev_id' => '2',
165 'ar_sha1' => 'pr0s8e18148pxhgjfa0gjrvpy8fiyxc',
166 'ar_page_id' => '2',
167 'ar_comment_text' => 'testing',
168 'ar_comment_data' => null,
169 'ar_comment_cid' => null,
170 'ar_content_format' => null,
171 'ar_content_model' => null,
172 'ts_tags' => null,
173 'ar_id' => '1',
174 'ar_namespace' => '0',
175 'ar_title' => 'PageArchiveTest_thePage',
176 'ar_text' => '',
177 'ar_text_id' => '2',
178 'ar_parent_id' => '0',
179 ],
180 $row2
181 );
182 }
183
184 /**
185 * @covers PageArchive::listPagesBySearch
186 */
187 public function testListPagesBySearch() {
188 $pages = PageArchive::listPagesBySearch( 'PageArchiveTest_thePage' );
189 $this->assertSame( 1, $pages->numRows() );
190
191 $page = (array)$pages->current();
192
193 $this->assertSame(
194 [
195 'ar_namespace' => '0',
196 'ar_title' => 'PageArchiveTest_thePage',
197 'count' => '2',
198 ],
199 $page
200 );
201 }
202
203 /**
204 * @covers PageArchive::listPagesBySearch
205 */
206 public function testListPagesByPrefix() {
207 $pages = PageArchive::listPagesByPrefix( 'PageArchiveTest' );
208 $this->assertSame( 1, $pages->numRows() );
209
210 $page = (array)$pages->current();
211
212 $this->assertSame(
213 [
214 'ar_namespace' => '0',
215 'ar_title' => 'PageArchiveTest_thePage',
216 'count' => '2',
217 ],
218 $page
219 );
220 }
221
222 /**
223 * @covers PageArchive::getTextFromRow
224 */
225 public function testGetTextFromRow() {
226 $row = (object)[ 'ar_text_id' => 2 ];
227 $text = $this->archivedPage->getTextFromRow( $row );
228 $this->assertSame( 'testing', $text );
229 }
230
231 /**
232 * @covers PageArchive::getLastRevisionText
233 */
234 public function testGetLastRevisionText() {
235 $text = $this->archivedPage->getLastRevisionText();
236 $this->assertSame( 'Lorem Ipsum', $text );
237 }
238
239 /**
240 * @covers PageArchive::isDeleted
241 */
242 public function testIsDeleted() {
243 $this->assertTrue( $this->archivedPage->isDeleted() );
244 }
245 }