Merge "Change php extract() to explicit code"
[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 $revisions = $this->archivedPage->listRevisions();
118 $this->assertEquals( 2, $revisions->numRows() );
119
120 // Get the rows as arrays
121 $row1 = (array)$revisions->current();
122 $row2 = (array)$revisions->next();
123 // Unset the timestamps (we assume they will be right...
124 $this->assertInternalType( 'string', $row1['ar_timestamp'] );
125 $this->assertInternalType( 'string', $row2['ar_timestamp'] );
126 unset( $row1['ar_timestamp'] );
127 unset( $row2['ar_timestamp'] );
128
129 $this->assertEquals(
130 [
131 'ar_minor_edit' => '0',
132 'ar_user' => '0',
133 'ar_user_text' => '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7',
134 'ar_len' => '11',
135 'ar_deleted' => '0',
136 'ar_rev_id' => '3',
137 'ar_sha1' => '0qdrpxl537ivfnx4gcpnzz0285yxryy',
138 'ar_page_id' => '2',
139 'ar_comment_text' => 'just a test',
140 'ar_comment_data' => null,
141 'ar_comment_cid' => null,
142 'ar_content_format' => null,
143 'ar_content_model' => null,
144 'ts_tags' => null,
145 'ar_id' => '2',
146 'ar_namespace' => '0',
147 'ar_title' => 'PageArchiveTest_thePage',
148 'ar_text' => '',
149 'ar_text_id' => '3',
150 'ar_parent_id' => '2',
151 ],
152 $row1
153 );
154 $this->assertEquals(
155 [
156 'ar_minor_edit' => '0',
157 'ar_user' => '0',
158 'ar_user_text' => '127.0.0.1',
159 'ar_len' => '7',
160 'ar_deleted' => '0',
161 'ar_rev_id' => '2',
162 'ar_sha1' => 'pr0s8e18148pxhgjfa0gjrvpy8fiyxc',
163 'ar_page_id' => '2',
164 'ar_comment_text' => 'testing',
165 'ar_comment_data' => null,
166 'ar_comment_cid' => null,
167 'ar_content_format' => null,
168 'ar_content_model' => null,
169 'ts_tags' => null,
170 'ar_id' => '1',
171 'ar_namespace' => '0',
172 'ar_title' => 'PageArchiveTest_thePage',
173 'ar_text' => '',
174 'ar_text_id' => '2',
175 'ar_parent_id' => '0',
176 ],
177 $row2
178 );
179 }
180
181 /**
182 * @covers PageArchive::listPagesBySearch
183 */
184 public function testListPagesBySearch() {
185 $pages = PageArchive::listPagesBySearch( 'PageArchiveTest_thePage' );
186 $this->assertSame( 1, $pages->numRows() );
187
188 $page = (array)$pages->current();
189
190 $this->assertSame(
191 [
192 'ar_namespace' => '0',
193 'ar_title' => 'PageArchiveTest_thePage',
194 'count' => '2',
195 ],
196 $page
197 );
198 }
199
200 /**
201 * @covers PageArchive::listPagesBySearch
202 */
203 public function testListPagesByPrefix() {
204 $pages = PageArchive::listPagesByPrefix( 'PageArchiveTest' );
205 $this->assertSame( 1, $pages->numRows() );
206
207 $page = (array)$pages->current();
208
209 $this->assertSame(
210 [
211 'ar_namespace' => '0',
212 'ar_title' => 'PageArchiveTest_thePage',
213 'count' => '2',
214 ],
215 $page
216 );
217 }
218
219 /**
220 * @covers PageArchive::getTextFromRow
221 */
222 public function testGetTextFromRow() {
223 $row = (object)[ 'ar_text_id' => 2 ];
224 $text = $this->archivedPage->getTextFromRow( $row );
225 $this->assertSame( 'testing', $text );
226 }
227
228 /**
229 * @covers PageArchive::getLastRevisionText
230 */
231 public function testGetLastRevisionText() {
232 $text = $this->archivedPage->getLastRevisionText();
233 $this->assertSame( 'Lorem Ipsum', $text );
234 }
235
236 /**
237 * @covers PageArchive::isDeleted
238 */
239 public function testIsDeleted() {
240 $this->assertTrue( $this->archivedPage->isDeleted() );
241 }
242 }