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