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' ];
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', '*' );
30 private function insertChangeTagData() {
35 'ct_tag' => 'One Tag',
40 'ct_tag' => 'Two Tags',
45 'ct_tag' => 'Two Tags',
50 'ct_tag' => 'Three Tags',
55 'ct_tag' => 'Three Tags',
60 'ct_tag' => 'Three Tags',
63 wfGetDB( DB_MASTER
)->insert( 'change_tag', $changeTags );
66 public function testRun() {
67 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
68 $this->maintenance
->loadWithArgv( [ '--sleep', '0' ] );
70 $this->maintenance
->execute();
74 'ctd_name' => 'One Tag',
78 'ctd_name' => 'Two Tags',
82 'ctd_name' => 'Three Tags',
87 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
89 [ 'ctd_name', 'ctd_count' ],
92 [ 'ORDER BY' => 'ctd_count' ]
95 $this->assertEquals( $changeTagDefRows, iterator_to_array( $actualChangeTagDefs, false ) );
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' ],
104 [ 'change_tag_def' => [ 'LEFT JOIN', 'ct_tag_id=ctd_id' ] ]
111 foreach ( $actualChangeTags as $row ) {
112 $this->assertNotNull( $row->ct_tag_id
);
113 $this->assertEquals( $row->ctd_count
, $mapping[$row->ct_tag
] );
117 public function testRunUpdateHitCountMigrationNew() {
118 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW
);
119 $changeTagDefBadRows = [
121 'ctd_name' => 'One Tag',
122 'ctd_user_defined' => 0,
126 'ctd_name' => 'Two Tags',
127 'ctd_user_defined' => 0,
131 'ctd_name' => 'Three Tags',
132 'ctd_user_defined' => 0,
136 wfGetDB( DB_MASTER
)->insert(
146 foreach ( $mapping as $tagName => $tagId ) {
147 wfGetDB( DB_MASTER
)->update(
149 [ 'ct_tag_id' => $tagId ],
150 [ 'ct_tag' => $tagName ]
154 $this->maintenance
->loadWithArgv( [ '--sleep', '0' ] );
156 $this->maintenance
->execute();
158 $changeTagDefRows = [
160 'ctd_name' => 'One Tag',
164 'ctd_name' => 'Two Tags',
168 'ctd_name' => 'Three Tags',
173 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
174 [ 'change_tag_def' ],
175 [ 'ctd_name', 'ctd_count' ],
178 [ 'ORDER BY' => 'ctd_count' ]
181 $this->assertEquals( $changeTagDefRows, iterator_to_array( $actualChangeTagDefs, false ) );
184 public function testRunUpdateHitCountMigrationWriteBoth() {
185 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
186 $changeTagDefBadRows = [
188 'ctd_name' => 'One Tag',
189 'ctd_user_defined' => 0,
193 'ctd_name' => 'Two Tags',
194 'ctd_user_defined' => 0,
198 'ctd_name' => 'Three Tags',
199 'ctd_user_defined' => 0,
203 wfGetDB( DB_MASTER
)->insert(
208 $this->maintenance
->loadWithArgv( [ '--sleep', '0' ] );
210 $this->maintenance
->execute();
212 $changeTagDefRows = [
214 'ctd_name' => 'One Tag',
218 'ctd_name' => 'Two Tags',
222 'ctd_name' => 'Three Tags',
227 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
228 [ 'change_tag_def' ],
229 [ 'ctd_name', 'ctd_count' ],
232 [ 'ORDER BY' => 'ctd_count' ]
235 $this->assertEquals( $changeTagDefRows, iterator_to_array( $actualChangeTagDefs, false ) );
238 public function testDryRunMigrationNew() {
239 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW
);
240 $this->maintenance
->loadWithArgv( [ '--dry-run', '--sleep', '0' ] );
242 $this->maintenance
->execute();
244 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
245 [ 'change_tag_def' ],
246 [ 'ctd_id', 'ctd_name' ]
249 $this->assertEquals( [], iterator_to_array( $actualChangeTagDefs, false ) );
251 $actualChangeTags = wfGetDB( DB_REPLICA
)->select(
253 [ 'ct_tag_id', 'ct_tag' ]
256 foreach ( $actualChangeTags as $row ) {
257 $this->assertNull( $row->ct_tag_id
);
258 $this->assertNotNull( $row->ct_tag
);
262 public function testDryRunMigrationWriteBoth() {
263 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
264 $this->maintenance
->loadWithArgv( [ '--dry-run', '--sleep', '0' ] );
266 $this->maintenance
->execute();
268 $actualChangeTagDefs = wfGetDB( DB_REPLICA
)->select(
269 [ 'change_tag_def' ],
270 [ 'ctd_id', 'ctd_name' ]
273 $this->assertEquals( [], iterator_to_array( $actualChangeTagDefs, false ) );
275 $actualChangeTags = wfGetDB( DB_REPLICA
)->select(
277 [ 'ct_tag_id', 'ct_tag' ]
280 foreach ( $actualChangeTags as $row ) {
281 $this->assertNull( $row->ct_tag_id
);
282 $this->assertNotNull( $row->ct_tag
);