3 namespace MediaWiki\Tests\Maintenance
;
5 use PopulateChangeTagDef
;
9 * @covers PopulateChangeTagDef
11 class PopulateChangeTagDefTest
extends MaintenanceBaseTestCase
{
13 public function getMaintenanceClass() {
14 return PopulateChangeTagDef
::class;
17 public function setUp() {
19 $this->tablesUsed
= [ 'change_tag', 'change_tag_def', 'updatelog' ];
21 $this->cleanChangeTagTables();
22 $this->insertChangeTagData();
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', '*' );
31 private function insertChangeTagData() {
36 'ct_tag' => 'One Tag',
41 'ct_tag' => 'Two Tags',
46 'ct_tag' => 'Two Tags',
51 'ct_tag' => 'Three Tags',
56 'ct_tag' => 'Three Tags',
61 'ct_tag' => 'Three Tags',
64 wfGetDB( DB_MASTER
)->insert( 'change_tag', $changeTags );
67 public function testRun() {
68 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
69 $this->maintenance
->loadWithArgv( [ '--sleep', '0' ] );
71 $this->maintenance
->execute();
75 'ctd_name' => 'One Tag',
79 'ctd_name' => 'Two Tags',
83 'ctd_name' => 'Three Tags',
88 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
90 [ 'ctd_name', 'ctd_count' ],
93 [ 'ORDER BY' => 'ctd_count' ]
96 $this->assertEquals( $changeTagDefRows, iterator_to_array( $actualChangeTagDefs, false ) );
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' ],
105 [ 'change_tag_def' => [ 'LEFT JOIN', 'ct_tag_id=ctd_id' ] ]
112 foreach ( $actualChangeTags as $row ) {
113 $this->assertNotNull( $row->ct_tag_id
);
114 $this->assertEquals( $row->ctd_count
, $mapping[$row->ct_tag
] );
118 public function testRunUpdateHitCountMigrationNew() {
119 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW
);
120 $changeTagDefBadRows = [
122 'ctd_name' => 'One Tag',
123 'ctd_user_defined' => 0,
127 'ctd_name' => 'Two Tags',
128 'ctd_user_defined' => 0,
132 'ctd_name' => 'Three Tags',
133 'ctd_user_defined' => 0,
137 wfGetDB( DB_MASTER
)->insert(
147 foreach ( $mapping as $tagName => $tagId ) {
148 wfGetDB( DB_MASTER
)->update(
150 [ 'ct_tag_id' => $tagId ],
151 [ 'ct_tag' => $tagName ]
155 $this->maintenance
->loadWithArgv( [ '--sleep', '0' ] );
157 $this->maintenance
->execute();
159 $changeTagDefRows = [
161 'ctd_name' => 'One Tag',
165 'ctd_name' => 'Two Tags',
169 'ctd_name' => 'Three Tags',
174 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
175 [ 'change_tag_def' ],
176 [ 'ctd_name', 'ctd_count' ],
179 [ 'ORDER BY' => 'ctd_count' ]
182 $this->assertEquals( $changeTagDefRows, iterator_to_array( $actualChangeTagDefs, false ) );
185 public function testRunUpdateHitCountMigrationWriteBoth() {
186 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
187 $changeTagDefBadRows = [
189 'ctd_name' => 'One Tag',
190 'ctd_user_defined' => 0,
194 'ctd_name' => 'Two Tags',
195 'ctd_user_defined' => 0,
199 'ctd_name' => 'Three Tags',
200 'ctd_user_defined' => 0,
204 wfGetDB( DB_MASTER
)->insert(
209 $this->maintenance
->loadWithArgv( [ '--sleep', '0' ] );
211 $this->maintenance
->execute();
213 $changeTagDefRows = [
215 'ctd_name' => 'One Tag',
219 'ctd_name' => 'Two Tags',
223 'ctd_name' => 'Three Tags',
228 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
229 [ 'change_tag_def' ],
230 [ 'ctd_name', 'ctd_count' ],
233 [ 'ORDER BY' => 'ctd_count' ]
236 $this->assertEquals( $changeTagDefRows, iterator_to_array( $actualChangeTagDefs, false ) );
239 public function testDryRunMigrationNew() {
240 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW
);
241 $this->maintenance
->loadWithArgv( [ '--dry-run', '--sleep', '0' ] );
243 $this->maintenance
->execute();
245 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
246 [ 'change_tag_def' ],
247 [ 'ctd_id', 'ctd_name' ]
250 $this->assertEquals( [], iterator_to_array( $actualChangeTagDefs, false ) );
252 $actualChangeTags = wfGetDB( DB_REPLICA
)->select(
254 [ 'ct_tag_id', 'ct_tag' ]
257 foreach ( $actualChangeTags as $row ) {
258 $this->assertNull( $row->ct_tag_id
);
259 $this->assertNotNull( $row->ct_tag
);
263 public function testDryRunMigrationWriteBoth() {
264 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
265 $this->maintenance
->loadWithArgv( [ '--dry-run', '--sleep', '0' ] );
267 $this->maintenance
->execute();
269 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
270 [ 'change_tag_def' ],
271 [ 'ctd_id', 'ctd_name' ]
274 $this->assertEquals( [], iterator_to_array( $actualChangeTagDefs, false ) );
276 $actualChangeTags = wfGetDB( DB_REPLICA
)->select(
278 [ 'ct_tag_id', 'ct_tag' ]
281 foreach ( $actualChangeTags as $row ) {
282 $this->assertNull( $row->ct_tag_id
);
283 $this->assertNotNull( $row->ct_tag
);