649e6928ac026eead7b6d259c9dc89384c6d54b0
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / McrRevisionStoreDbTest.php
1 <?php
2 namespace MediaWiki\Tests\Storage;
3
4 use CommentStoreComment;
5 use MediaWiki\MediaWikiServices;
6 use MediaWiki\Storage\MutableRevisionRecord;
7 use MediaWiki\Storage\RevisionRecord;
8 use MediaWiki\Storage\SlotRecord;
9 use TextContent;
10 use Title;
11 use WikitextContent;
12
13 /**
14 * Tests RevisionStore against the post-migration MCR DB schema.
15 *
16 * @covers \MediaWiki\Storage\RevisionStore
17 *
18 * @group RevisionStore
19 * @group Storage
20 * @group Database
21 * @group medium
22 */
23 class McrRevisionStoreDbTest extends RevisionStoreDbTestBase {
24
25 use McrSchemaOverride;
26
27 protected function assertRevisionExistsInDatabase( RevisionRecord $rev ) {
28 $numberOfSlots = count( $rev->getSlotRoles() );
29
30 // new schema is written
31 $this->assertSelect(
32 'slots',
33 [ 'count(*)' ],
34 [ 'slot_revision_id' => $rev->getId() ],
35 [ [ (string)$numberOfSlots ] ]
36 );
37
38 $store = MediaWikiServices::getInstance()->getRevisionStore();
39 $revQuery = $store->getSlotsQueryInfo( [ 'content' ] );
40
41 $this->assertSelect(
42 $revQuery['tables'],
43 [ 'count(*)' ],
44 [
45 'slot_revision_id' => $rev->getId(),
46 ],
47 [ [ (string)$numberOfSlots ] ],
48 [],
49 $revQuery['joins']
50 );
51
52 parent::assertRevisionExistsInDatabase( $rev );
53 }
54
55 /**
56 * @param SlotRecord $a
57 * @param SlotRecord $b
58 */
59 protected function assertSameSlotContent( SlotRecord $a, SlotRecord $b ) {
60 parent::assertSameSlotContent( $a, $b );
61
62 // Assert that the same content ID has been used
63 $this->assertSame( $a->getContentId(), $b->getContentId() );
64 }
65
66 public function provideInsertRevisionOn_successes() {
67 foreach ( parent::provideInsertRevisionOn_successes() as $case ) {
68 yield $case;
69 }
70
71 yield 'Multi-slot revision insertion' => [
72 [
73 'content' => [
74 'main' => new WikitextContent( 'Chicken' ),
75 'aux' => new TextContent( 'Egg' ),
76 ],
77 'page' => true,
78 'comment' => $this->getRandomCommentStoreComment(),
79 'timestamp' => '20171117010101',
80 'user' => true,
81 ],
82 ];
83 }
84
85 public function provideNewNullRevision() {
86 foreach ( parent::provideNewNullRevision() as $case ) {
87 yield $case;
88 }
89
90 yield [
91 Title::newFromText( 'UTPage_notAutoCreated' ),
92 [
93 'content' => [
94 'main' => new WikitextContent( 'Chicken' ),
95 'aux' => new WikitextContent( 'Omelet' ),
96 ],
97 ],
98 CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment multi' ),
99 ];
100 }
101
102 public function provideNewMutableRevisionFromArray() {
103 foreach ( parent::provideNewMutableRevisionFromArray() as $case ) {
104 yield $case;
105 }
106
107 yield 'Basic array, multiple roles' => [
108 [
109 'id' => 2,
110 'page' => 1,
111 'timestamp' => '20171017114835',
112 'user_text' => '111.0.1.2',
113 'user' => 0,
114 'minor_edit' => false,
115 'deleted' => 0,
116 'len' => 29,
117 'parent_id' => 1,
118 'sha1' => '89qs83keq9c9ccw9olvvm4oc9oq50ii',
119 'comment' => 'Goat Comment!',
120 'content' => [
121 'main' => new WikitextContent( 'Söme Cöntent' ),
122 'aux' => new TextContent( 'Öther Cöntent' ),
123 ]
124 ]
125 ];
126 }
127
128 public function testGetQueryInfo_NoSlotDataJoin() {
129 $store = MediaWikiServices::getInstance()->getRevisionStore();
130 $queryInfo = $store->getQueryInfo();
131
132 // with the new schema enabled, query info should not join the main slot info
133 $this->assertFalse( array_key_exists( 'a_slot_data', $queryInfo['tables'] ) );
134 $this->assertFalse( array_key_exists( 'a_slot_data', $queryInfo['joins'] ) );
135 }
136
137 public function provideGetArchiveQueryInfo() {
138 yield [
139 [
140 'tables' => [
141 'archive',
142 ],
143 'fields' => array_merge(
144 $this->getDefaultArchiveFields( false ),
145 [
146 'ar_comment_text' => 'ar_comment',
147 'ar_comment_data' => 'NULL',
148 'ar_comment_cid' => 'NULL',
149 'ar_user_text' => 'ar_user_text',
150 'ar_user' => 'ar_user',
151 'ar_actor' => 'NULL',
152 ]
153 ),
154 'joins' => [
155 ],
156 ]
157 ];
158 }
159
160 public function provideGetQueryInfo() {
161 // TODO: more option variations
162 yield [
163 [ 'page', 'user' ],
164 [
165 'tables' => [
166 'revision',
167 'page',
168 'user',
169 ],
170 'fields' => array_merge(
171 $this->getDefaultQueryFields( false ),
172 $this->getCommentQueryFields(),
173 $this->getActorQueryFields(),
174 [
175 'page_namespace',
176 'page_title',
177 'page_id',
178 'page_latest',
179 'page_is_redirect',
180 'page_len',
181 'user_name',
182 ]
183 ),
184 'joins' => [
185 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
186 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
187 ],
188 ]
189 ];
190 }
191
192 public function provideGetSlotsQueryInfo() {
193 yield [
194 [],
195 [
196 'tables' => [
197 'slots',
198 'slot_roles',
199 ],
200 'fields' => array_merge(
201 [
202 'slot_revision_id',
203 'slot_content_id',
204 'slot_origin',
205 'role_name',
206 ]
207 ),
208 'joins' => [
209 'slot_roles' => [ 'INNER JOIN', [ 'slot_role_id = role_id' ] ],
210 ],
211 ]
212 ];
213 yield [
214 [ 'content' ],
215 [
216 'tables' => [
217 'slots',
218 'slot_roles',
219 'content',
220 'content_models',
221 ],
222 'fields' => array_merge(
223 [
224 'slot_revision_id',
225 'slot_content_id',
226 'slot_origin',
227 'role_name',
228 'content_size',
229 'content_sha1',
230 'content_address',
231 'model_name',
232 ]
233 ),
234 'joins' => [
235 'slot_roles' => [ 'INNER JOIN', [ 'slot_role_id = role_id' ] ],
236 'content' => [ 'INNER JOIN', [ 'slot_content_id = content_id' ] ],
237 'content_models' => [ 'INNER JOIN', [ 'content_model = model_id' ] ],
238 ],
239 ]
240 ];
241 }
242
243 /**
244 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
245 * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
246 * @covers \MediaWiki\Storage\RevisionStore::insertContentRowOn
247 */
248 public function testInsertRevisionOn_T202032() {
249 // This test only makes sense for MySQL
250 if ( $this->db->getType() !== 'mysql' ) {
251 $this->assertTrue( true );
252 return;
253 }
254
255 // NOTE: must be done before checking MAX(rev_id)
256 $page = $this->getTestPage();
257
258 $maxRevId = $this->db->selectField( 'revision', 'MAX(rev_id)' );
259
260 // Construct a slot row that will conflict with the insertion of the next revision ID,
261 // to emulate the failure mode described in T202032. Nothing will ever read this row,
262 // we just need it to trigger a primary key conflict.
263 $this->db->insert( 'slots', [
264 'slot_revision_id' => $maxRevId + 1,
265 'slot_role_id' => 1,
266 'slot_content_id' => 0,
267 'slot_origin' => 0
268 ], __METHOD__ );
269
270 $rev = new MutableRevisionRecord( $page->getTitle() );
271 $rev->setTimestamp( '20180101000000' );
272 $rev->setComment( CommentStoreComment::newUnsavedComment( 'test' ) );
273 $rev->setUser( $this->getTestUser()->getUser() );
274 $rev->setContent( 'main', new WikitextContent( 'Text' ) );
275 $rev->setPageId( $page->getId() );
276
277 $store = MediaWikiServices::getInstance()->getRevisionStore();
278 $return = $store->insertRevisionOn( $rev, $this->db );
279
280 $this->assertSame( $maxRevId + 2, $return->getId() );
281
282 // is the new revision correct?
283 $this->assertRevisionCompleteness( $return );
284 $this->assertRevisionRecordsEqual( $rev, $return );
285
286 // can we find it directly in the database?
287 $this->assertRevisionExistsInDatabase( $return );
288
289 // can we load it from the store?
290 $loaded = $store->getRevisionById( $return->getId() );
291 $this->assertRevisionCompleteness( $loaded );
292 $this->assertRevisionRecordsEqual( $return, $loaded );
293 }
294
295 }