summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/PageArchiveTest.php
blob: 623d4a65f173076eedd35586e24079ba1e10a269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php

/**
 * Test class for page archiving.
 *
 * @group ContentHandler
 * @group Database
 * ^--- important, causes temporary tables to be used instead of the real database
 *
 * @group medium
 * ^--- important, causes tests not to fail with timeout
 */
class PageArchiveTest extends MediaWikiTestCase {

	/**
	 * @var PageArchive $archivedPage
	 */
	private $archivedPage;

	/**
	 * A logged out user who edited the page before it was archived.
	 * @var string $ipEditor
	 */
	private $ipEditor;

	/**
	 * Revision ID of the IP edit
	 * @var int $ipRevId
	 */
	private $ipRevId;

	function __construct( $name = null, array $data = [], $dataName = '' ) {
		parent::__construct( $name, $data, $dataName );

		$this->tablesUsed = array_merge(
			$this->tablesUsed,
			[
				'page',
				'revision',
				'ip_changes',
				'text',
				'archive',
				'recentchanges',
				'logging',
				'page_props',
			]
		);
	}

	protected function setUp() {
		parent::setUp();

		$this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
		$this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
		$this->overrideMwServices();

		// First create our dummy page
		$page = Title::newFromText( 'PageArchiveTest_thePage' );
		$page = new WikiPage( $page );
		$content = ContentHandler::makeContent(
			'testing',
			$page->getTitle(),
			CONTENT_MODEL_WIKITEXT
		);
		$page->doEditContent( $content, 'testing', EDIT_NEW );

		// Insert IP revision
		$this->ipEditor = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
		$rev = new Revision( [
			'text' => 'Lorem Ipsum',
			'comment' => 'just a test',
			'page' => $page->getId(),
			'user_text' => $this->ipEditor,
		] );
		$dbw = wfGetDB( DB_MASTER );
		$this->ipRevId = $rev->insertOn( $dbw );

		// Delete the page
		$page->doDeleteArticleReal( 'Just a test deletion' );

		$this->archivedPage = new PageArchive( $page->getTitle() );
	}

	/**
	 * @covers PageArchive::undelete
	 * @covers PageArchive::undeleteRevisions
	 */
	public function testUndeleteRevisions() {
		// First make sure old revisions are archived
		$dbr = wfGetDB( DB_REPLICA );
		$arQuery = Revision::getArchiveQueryInfo();
		$res = $dbr->select(
			$arQuery['tables'],
			$arQuery['fields'],
			[ 'ar_rev_id' => $this->ipRevId ],
			__METHOD__,
			[],
			$arQuery['joins']
		);
		$row = $res->fetchObject();
		$this->assertEquals( $this->ipEditor, $row->ar_user_text );

		// Should not be in revision
		$res = $dbr->select( 'revision', '1', [ 'rev_id' => $this->ipRevId ] );
		$this->assertFalse( $res->fetchObject() );

		// Should not be in ip_changes
		$res = $dbr->select( 'ip_changes', '1', [ 'ipc_rev_id' => $this->ipRevId ] );
		$this->assertFalse( $res->fetchObject() );

		// Restore the page
		$this->archivedPage->undelete( [] );

		// Should be back in revision
		$revQuery = Revision::getQueryInfo();
		$res = $dbr->select(
			$revQuery['tables'],
			$revQuery['fields'],
			[ 'rev_id' => $this->ipRevId ],
			__METHOD__,
			[],
			$revQuery['joins']
		);
		$row = $res->fetchObject();
		$this->assertEquals( $this->ipEditor, $row->rev_user_text );

		// Should be back in ip_changes
		$res = $dbr->select( 'ip_changes', [ 'ipc_hex' ], [ 'ipc_rev_id' => $this->ipRevId ] );
		$row = $res->fetchObject();
		$this->assertEquals( IP::toHex( $this->ipEditor ), $row->ipc_hex );
	}

	/**
	 * @covers PageArchive::listRevisions
	 */
	public function testListRevisions() {
		$this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
		$this->overrideMwServices();

		$revisions = $this->archivedPage->listRevisions();
		$this->assertEquals( 2, $revisions->numRows() );

		// Get the rows as arrays
		$row1 = (array)$revisions->current();
		$row2 = (array)$revisions->next();
		// Unset the timestamps (we assume they will be right...
		$this->assertInternalType( 'string', $row1['ar_timestamp'] );
		$this->assertInternalType( 'string', $row2['ar_timestamp'] );
		unset( $row1['ar_timestamp'] );
		unset( $row2['ar_timestamp'] );

		$this->assertEquals(
			[
				'ar_minor_edit' => '0',
				'ar_user' => '0',
				'ar_user_text' => '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7',
				'ar_actor' => null,
				'ar_len' => '11',
				'ar_deleted' => '0',
				'ar_rev_id' => '3',
				'ar_sha1' => '0qdrpxl537ivfnx4gcpnzz0285yxryy',
				'ar_page_id' => '2',
				'ar_comment_text' => 'just a test',
				'ar_comment_data' => null,
				'ar_comment_cid' => null,
				'ar_content_format' => null,
				'ar_content_model' => null,
				'ts_tags' => null,
				'ar_id' => '2',
				'ar_namespace' => '0',
				'ar_title' => 'PageArchiveTest_thePage',
				'ar_text_id' => '3',
				'ar_parent_id' => '2',
			],
			$row1
		);
		$this->assertEquals(
			[
				'ar_minor_edit' => '0',
				'ar_user' => '0',
				'ar_user_text' => '127.0.0.1',
				'ar_actor' => null,
				'ar_len' => '7',
				'ar_deleted' => '0',
				'ar_rev_id' => '2',
				'ar_sha1' => 'pr0s8e18148pxhgjfa0gjrvpy8fiyxc',
				'ar_page_id' => '2',
				'ar_comment_text' => 'testing',
				'ar_comment_data' => null,
				'ar_comment_cid' => null,
				'ar_content_format' => null,
				'ar_content_model' => null,
				'ts_tags' => null,
				'ar_id' => '1',
				'ar_namespace' => '0',
				'ar_title' => 'PageArchiveTest_thePage',
				'ar_text_id' => '2',
				'ar_parent_id' => '0',
			],
			$row2
		);
	}

	/**
	 * @covers PageArchive::listPagesBySearch
	 */
	public function testListPagesBySearch() {
		$pages = PageArchive::listPagesBySearch( 'PageArchiveTest_thePage' );
		$this->assertSame( 1, $pages->numRows() );

		$page = (array)$pages->current();

		$this->assertSame(
			[
				'ar_namespace' => '0',
				'ar_title' => 'PageArchiveTest_thePage',
				'count' => '2',
			],
			$page
		);
	}

	/**
	 * @covers PageArchive::listPagesBySearch
	 */
	public function testListPagesByPrefix() {
		$pages = PageArchive::listPagesByPrefix( 'PageArchiveTest' );
		$this->assertSame( 1, $pages->numRows() );

		$page = (array)$pages->current();

		$this->assertSame(
			[
				'ar_namespace' => '0',
				'ar_title' => 'PageArchiveTest_thePage',
				'count' => '2',
			],
			$page
		);
	}

	/**
	 * @covers PageArchive::getTextFromRow
	 */
	public function testGetTextFromRow() {
		$row = (object)[ 'ar_text_id' => 2 ];
		$text = $this->archivedPage->getTextFromRow( $row );
		$this->assertSame( 'testing', $text );
	}

	/**
	 * @covers PageArchive::getLastRevisionText
	 */
	public function testGetLastRevisionText() {
		$text = $this->archivedPage->getLastRevisionText();
		$this->assertSame( 'Lorem Ipsum', $text );
	}

	/**
	 * @covers PageArchive::isDeleted
	 */
	public function testIsDeleted() {
		$this->assertTrue( $this->archivedPage->isDeleted() );
	}
}