28991a0a670ef033b12833d96f1032a07bae61df
[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 */
83 public function testUndeleteRevisions() {
84 // First make sure old revisions are archived
85 $dbr = wfGetDB( DB_REPLICA );
86 $res = $dbr->select( 'archive', '*', [ 'ar_rev_id' => $this->ipRevId ] );
87 $row = $res->fetchObject();
88 $this->assertEquals( $this->ipEditor, $row->ar_user_text );
89
90 // Should not be in revision
91 $res = $dbr->select( 'revision', '*', [ 'rev_id' => $this->ipRevId ] );
92 $this->assertFalse( $res->fetchObject() );
93
94 // Should not be in ip_changes
95 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $this->ipRevId ] );
96 $this->assertFalse( $res->fetchObject() );
97
98 // Restore the page
99 $this->archivedPage->undelete( [] );
100
101 // Should be back in revision
102 $res = $dbr->select( 'revision', '*', [ 'rev_id' => $this->ipRevId ] );
103 $row = $res->fetchObject();
104 $this->assertEquals( $this->ipEditor, $row->rev_user_text );
105
106 // Should be back in ip_changes
107 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $this->ipRevId ] );
108 $row = $res->fetchObject();
109 $this->assertEquals( IP::toHex( $this->ipEditor ), $row->ipc_hex );
110 }
111
112 /**
113 * @covers PageArchive::listRevisions
114 */
115 public function testListRevisions() {
116 $revisions = $this->archivedPage->listRevisions();
117 $this->assertEquals( 2, $revisions->numRows() );
118
119 // Get the rows as arrays
120 $row1 = (array)$revisions->current();
121 $row2 = (array)$revisions->next();
122 // Unset the timestamps (we assume they will be right...
123 $this->assertInternalType( 'string', $row1['ar_timestamp'] );
124 $this->assertInternalType( 'string', $row2['ar_timestamp'] );
125 unset( $row1['ar_timestamp'] );
126 unset( $row2['ar_timestamp'] );
127
128 $this->assertEquals(
129 [
130 'ar_minor_edit' => '0',
131 'ar_user' => '0',
132 'ar_user_text' => '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7',
133 'ar_len' => '11',
134 'ar_deleted' => '0',
135 'ar_rev_id' => '3',
136 'ar_sha1' => '0qdrpxl537ivfnx4gcpnzz0285yxryy',
137 'ar_page_id' => '2',
138 'ar_comment_text' => 'just a test',
139 'ar_comment_data' => null,
140 'ar_comment_cid' => null,
141 'ar_content_format' => null,
142 'ar_content_model' => null,
143 'ts_tags' => null,
144 ],
145 $row1
146 );
147 $this->assertEquals(
148 [
149 'ar_minor_edit' => '0',
150 'ar_user' => '0',
151 'ar_user_text' => '127.0.0.1',
152 'ar_len' => '7',
153 'ar_deleted' => '0',
154 'ar_rev_id' => '2',
155 'ar_sha1' => 'pr0s8e18148pxhgjfa0gjrvpy8fiyxc',
156 'ar_page_id' => '2',
157 'ar_comment_text' => 'testing',
158 'ar_comment_data' => null,
159 'ar_comment_cid' => null,
160 'ar_content_format' => null,
161 'ar_content_model' => null,
162 'ts_tags' => null,
163 ],
164 $row2
165 );
166 }
167
168 }