populateChangeTagDefTest.php 6.2 KB

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