Merge "Add block notice stats on EditPage."
[lhc/web/wiklou.git] / tests / phpunit / maintenance / populateChangeTagDefTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use PopulateChangeTagDef;
6
7 /**
8 * @group Database
9 * @covers PopulateChangeTagDef
10 */
11 class PopulateChangeTagDefTest extends MaintenanceBaseTestCase {
12
13 public function getMaintenanceClass() {
14 return PopulateChangeTagDef::class;
15 }
16
17 public function setUp() {
18 parent::setUp();
19 $this->tablesUsed = [ 'change_tag', 'change_tag_def', 'updatelog' ];
20
21 $this->cleanChangeTagTables();
22 $this->insertChangeTagData();
23 }
24
25 private function cleanChangeTagTables() {
26 wfGetDB( DB_MASTER )->delete( 'change_tag', '*' );
27 wfGetDB( DB_MASTER )->delete( 'change_tag_def', '*' );
28 wfGetDB( DB_MASTER )->delete( 'updatelog', '*' );
29 }
30
31 private function insertChangeTagData() {
32 $changeTags = [];
33
34 $changeTags[] = [
35 'ct_rc_id' => 1234,
36 'ct_tag' => 'One Tag',
37 ];
38
39 $changeTags[] = [
40 'ct_rc_id' => 1235,
41 'ct_tag' => 'Two Tags',
42 ];
43
44 $changeTags[] = [
45 'ct_log_id' => 1236,
46 'ct_tag' => 'Two Tags',
47 ];
48
49 $changeTags[] = [
50 'ct_rev_id' => 1237,
51 'ct_tag' => 'Three Tags',
52 ];
53
54 $changeTags[] = [
55 'ct_rc_id' => 1238,
56 'ct_tag' => 'Three Tags',
57 ];
58
59 $changeTags[] = [
60 'ct_log_id' => 1239,
61 'ct_tag' => 'Three Tags',
62 ];
63
64 wfGetDB( DB_MASTER )->insert( 'change_tag', $changeTags );
65 }
66
67 public function testRun() {
68 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
69 $this->maintenance->loadWithArgv( [ '--sleep', '0' ] );
70
71 $this->maintenance->execute();
72
73 $changeTagDefRows = [
74 (object)[
75 'ctd_name' => 'One Tag',
76 'ctd_count' => 1,
77 ],
78 (object)[
79 'ctd_name' => 'Two Tags',
80 'ctd_count' => 2,
81 ],
82 (object)[
83 'ctd_name' => 'Three Tags',
84 'ctd_count' => 3,
85 ],
86 ];
87
88 $actualChangeTagDefs = wfGetDB( DB_REPLICA )->select(
89 [ 'change_tag_def' ],
90 [ 'ctd_name', 'ctd_count' ],
91 [],
92 __METHOD__,
93 [ 'ORDER BY' => 'ctd_count' ]
94 );
95
96 $this->assertEquals( $changeTagDefRows, iterator_to_array( $actualChangeTagDefs, false ) );
97
98 // Check if change_tag is also backpopulated
99 $actualChangeTags = wfGetDB( DB_REPLICA )->select(
100 [ 'change_tag', 'change_tag_def' ],
101 [ 'ct_tag', 'ct_tag_id', 'ctd_count' ],
102 [],
103 __METHOD__,
104 [],
105 [ 'change_tag_def' => [ 'LEFT JOIN', 'ct_tag_id=ctd_id' ] ]
106 );
107 $mapping = [
108 'One Tag' => 1,
109 'Two Tags' => 2,
110 'Three Tags' => 3
111 ];
112 foreach ( $actualChangeTags as $row ) {
113 $this->assertNotNull( $row->ct_tag_id );
114 $this->assertEquals( $row->ctd_count, $mapping[$row->ct_tag] );
115 }
116 }
117
118 public function testRunUpdateHitCountMigrationNew() {
119 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW );
120 $changeTagDefBadRows = [
121 [
122 'ctd_name' => 'One Tag',
123 'ctd_user_defined' => 0,
124 'ctd_count' => 50,
125 ],
126 [
127 'ctd_name' => 'Two Tags',
128 'ctd_user_defined' => 0,
129 'ctd_count' => 4,
130 ],
131 [
132 'ctd_name' => 'Three Tags',
133 'ctd_user_defined' => 0,
134 'ctd_count' => 3,
135 ],
136 ];
137 wfGetDB( DB_MASTER )->insert(
138 'change_tag_def',
139 $changeTagDefBadRows
140 );
141
142 $mapping = [
143 'One Tag' => 1,
144 'Two Tags' => 2,
145 'Three Tags' => 3
146 ];
147 foreach ( $mapping as $tagName => $tagId ) {
148 wfGetDB( DB_MASTER )->update(
149 'change_tag',
150 [ 'ct_tag_id' => $tagId ],
151 [ 'ct_tag' => $tagName ]
152 );
153 }
154
155 $this->maintenance->loadWithArgv( [ '--sleep', '0' ] );
156
157 $this->maintenance->execute();
158
159 $changeTagDefRows = [
160 (object)[
161 'ctd_name' => 'One Tag',
162 'ctd_count' => 1,
163 ],
164 (object)[
165 'ctd_name' => 'Two Tags',
166 'ctd_count' => 2,
167 ],
168 (object)[
169 'ctd_name' => 'Three Tags',
170 'ctd_count' => 3,
171 ],
172 ];
173
174 $actualChangeTagDefs = wfGetDB( DB_REPLICA )->select(
175 [ 'change_tag_def' ],
176 [ 'ctd_name', 'ctd_count' ],
177 [],
178 __METHOD__,
179 [ 'ORDER BY' => 'ctd_count' ]
180 );
181
182 $this->assertEquals( $changeTagDefRows, iterator_to_array( $actualChangeTagDefs, false ) );
183 }
184
185 public function testRunUpdateHitCountMigrationWriteBoth() {
186 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
187 $changeTagDefBadRows = [
188 [
189 'ctd_name' => 'One Tag',
190 'ctd_user_defined' => 0,
191 'ctd_count' => 50,
192 ],
193 [
194 'ctd_name' => 'Two Tags',
195 'ctd_user_defined' => 0,
196 'ctd_count' => 4,
197 ],
198 [
199 'ctd_name' => 'Three Tags',
200 'ctd_user_defined' => 0,
201 'ctd_count' => 3,
202 ],
203 ];
204 wfGetDB( DB_MASTER )->insert(
205 'change_tag_def',
206 $changeTagDefBadRows
207 );
208
209 $this->maintenance->loadWithArgv( [ '--sleep', '0' ] );
210
211 $this->maintenance->execute();
212
213 $changeTagDefRows = [
214 (object)[
215 'ctd_name' => 'One Tag',
216 'ctd_count' => 1,
217 ],
218 (object)[
219 'ctd_name' => 'Two Tags',
220 'ctd_count' => 2,
221 ],
222 (object)[
223 'ctd_name' => 'Three Tags',
224 'ctd_count' => 3,
225 ],
226 ];
227
228 $actualChangeTagDefs = wfGetDB( DB_REPLICA )->select(
229 [ 'change_tag_def' ],
230 [ 'ctd_name', 'ctd_count' ],
231 [],
232 __METHOD__,
233 [ 'ORDER BY' => 'ctd_count' ]
234 );
235
236 $this->assertEquals( $changeTagDefRows, iterator_to_array( $actualChangeTagDefs, false ) );
237 }
238
239 public function testDryRunMigrationNew() {
240 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW );
241 $this->maintenance->loadWithArgv( [ '--dry-run', '--sleep', '0' ] );
242
243 $this->maintenance->execute();
244
245 $actualChangeTagDefs = wfGetDB( DB_REPLICA )->select(
246 [ 'change_tag_def' ],
247 [ 'ctd_id', 'ctd_name' ]
248 );
249
250 $this->assertEquals( [], iterator_to_array( $actualChangeTagDefs, false ) );
251
252 $actualChangeTags = wfGetDB( DB_REPLICA )->select(
253 [ 'change_tag' ],
254 [ 'ct_tag_id', 'ct_tag' ]
255 );
256
257 foreach ( $actualChangeTags as $row ) {
258 $this->assertNull( $row->ct_tag_id );
259 $this->assertNotNull( $row->ct_tag );
260 }
261 }
262
263 public function testDryRunMigrationWriteBoth() {
264 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
265 $this->maintenance->loadWithArgv( [ '--dry-run', '--sleep', '0' ] );
266
267 $this->maintenance->execute();
268
269 $actualChangeTagDefs = wfGetDB( DB_REPLICA )->select(
270 [ 'change_tag_def' ],
271 [ 'ctd_id', 'ctd_name' ]
272 );
273
274 $this->assertEquals( [], iterator_to_array( $actualChangeTagDefs, false ) );
275
276 $actualChangeTags = wfGetDB( DB_REPLICA )->select(
277 [ 'change_tag' ],
278 [ 'ct_tag_id', 'ct_tag' ]
279 );
280
281 foreach ( $actualChangeTags as $row ) {
282 $this->assertNull( $row->ct_tag_id );
283 $this->assertNotNull( $row->ct_tag );
284 }
285 }
286
287 }