Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[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 /**
138 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
139 * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
140 * @covers \MediaWiki\Storage\RevisionStore::insertContentRowOn
141 */
142 public function testInsertRevisionOn_T202032() {
143 // This test only makes sense for MySQL
144 if ( $this->db->getType() !== 'mysql' ) {
145 $this->assertTrue( true );
146 return;
147 }
148
149 // NOTE: must be done before checking MAX(rev_id)
150 $page = $this->getTestPage();
151
152 $maxRevId = $this->db->selectField( 'revision', 'MAX(rev_id)' );
153
154 // Construct a slot row that will conflict with the insertion of the next revision ID,
155 // to emulate the failure mode described in T202032. Nothing will ever read this row,
156 // we just need it to trigger a primary key conflict.
157 $this->db->insert( 'slots', [
158 'slot_revision_id' => $maxRevId + 1,
159 'slot_role_id' => 1,
160 'slot_content_id' => 0,
161 'slot_origin' => 0
162 ], __METHOD__ );
163
164 $rev = new MutableRevisionRecord( $page->getTitle() );
165 $rev->setTimestamp( '20180101000000' );
166 $rev->setComment( CommentStoreComment::newUnsavedComment( 'test' ) );
167 $rev->setUser( $this->getTestUser()->getUser() );
168 $rev->setContent( 'main', new WikitextContent( 'Text' ) );
169 $rev->setPageId( $page->getId() );
170
171 $store = MediaWikiServices::getInstance()->getRevisionStore();
172 $return = $store->insertRevisionOn( $rev, $this->db );
173
174 $this->assertSame( $maxRevId + 2, $return->getId() );
175
176 // is the new revision correct?
177 $this->assertRevisionCompleteness( $return );
178 $this->assertRevisionRecordsEqual( $rev, $return );
179
180 // can we find it directly in the database?
181 $this->assertRevisionExistsInDatabase( $return );
182
183 // can we load it from the store?
184 $loaded = $store->getRevisionById( $return->getId() );
185 $this->assertRevisionCompleteness( $loaded );
186 $this->assertRevisionRecordsEqual( $return, $loaded );
187 }
188
189 }