MergeHistoryTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @group Database
  4. */
  5. class MergeHistoryTest extends MediaWikiTestCase {
  6. /**
  7. * Make some pages to work with
  8. */
  9. public function addDBDataOnce() {
  10. // Pages that won't actually be merged
  11. $this->insertPage( 'Test' );
  12. $this->insertPage( 'Test2' );
  13. // Pages that will be merged
  14. $this->insertPage( 'Merge1' );
  15. $this->insertPage( 'Merge2' );
  16. }
  17. /**
  18. * @dataProvider provideIsValidMerge
  19. * @covers MergeHistory::isValidMerge
  20. * @param string $source Source page
  21. * @param string $dest Destination page
  22. * @param string|bool $timestamp Timestamp up to which revisions are merged (or false for all)
  23. * @param string|bool $error Expected error for test (or true for no error)
  24. */
  25. public function testIsValidMerge( $source, $dest, $timestamp, $error ) {
  26. $this->setMwGlobals( 'wgContentHandlerUseDB', false );
  27. if ( $timestamp === true ) {
  28. // Although this timestamp is after the latest timestamp of both pages,
  29. // MergeHistory should select the latest source timestamp up to this which should
  30. // still work for the merge.
  31. $timestamp = time() + ( 24 * 3600 );
  32. }
  33. $mh = new MergeHistory(
  34. Title::newFromText( $source ),
  35. Title::newFromText( $dest ),
  36. $timestamp
  37. );
  38. $status = $mh->isValidMerge();
  39. if ( $error === true ) {
  40. $this->assertTrue( $status->isGood() );
  41. } else {
  42. $this->assertTrue( $status->hasMessage( $error ) );
  43. }
  44. }
  45. public static function provideIsValidMerge() {
  46. return [
  47. // for MergeHistory::isValidMerge
  48. [ 'Test', 'Test2', false, true ],
  49. // Timestamp of `true` is a placeholder for "in the future""
  50. [ 'Test', 'Test2', true, true ],
  51. [ 'Test', 'Test', false, 'mergehistory-fail-self-merge' ],
  52. [ 'Nonexistant', 'Test2', false, 'mergehistory-fail-invalid-source' ],
  53. [ 'Test', 'Nonexistant', false, 'mergehistory-fail-invalid-dest' ],
  54. [
  55. 'Test',
  56. 'Test2',
  57. 'This is obviously an invalid timestamp',
  58. 'mergehistory-fail-bad-timestamp'
  59. ],
  60. ];
  61. }
  62. /**
  63. * Test merge revision limit checking
  64. * @covers MergeHistory::isValidMerge
  65. */
  66. public function testIsValidMergeRevisionLimit() {
  67. $limit = MergeHistory::REVISION_LIMIT;
  68. $mh = $this->getMockBuilder( MergeHistory::class )
  69. ->setMethods( [ 'getRevisionCount' ] )
  70. ->setConstructorArgs( [
  71. Title::newFromText( 'Test' ),
  72. Title::newFromText( 'Test2' ),
  73. ] )
  74. ->getMock();
  75. $mh->expects( $this->once() )
  76. ->method( 'getRevisionCount' )
  77. ->will( $this->returnValue( $limit + 1 ) );
  78. $status = $mh->isValidMerge();
  79. $this->assertTrue( $status->hasMessage( 'mergehistory-fail-toobig' ) );
  80. $errors = $status->getErrorsByType( 'error' );
  81. $params = $errors[0]['params'];
  82. $this->assertEquals( $params[0], Message::numParam( $limit ) );
  83. }
  84. /**
  85. * Test user permission checking
  86. * @covers MergeHistory::checkPermissions
  87. */
  88. public function testCheckPermissions() {
  89. $mh = new MergeHistory(
  90. Title::newFromText( 'Test' ),
  91. Title::newFromText( 'Test2' )
  92. );
  93. // Sysop with mergehistory permission
  94. $sysop = static::getTestSysop()->getUser();
  95. $status = $mh->checkPermissions( $sysop, '' );
  96. $this->assertTrue( $status->isOK() );
  97. // Normal user
  98. $notSysop = static::getTestUser()->getUser();
  99. $status = $mh->checkPermissions( $notSysop, '' );
  100. $this->assertTrue( $status->hasMessage( 'mergehistory-fail-permission' ) );
  101. }
  102. /**
  103. * Test merged revision count
  104. * @covers MergeHistory::getMergedRevisionCount
  105. */
  106. public function testGetMergedRevisionCount() {
  107. $mh = new MergeHistory(
  108. Title::newFromText( 'Merge1' ),
  109. Title::newFromText( 'Merge2' )
  110. );
  111. $sysop = static::getTestSysop()->getUser();
  112. $mh->merge( $sysop );
  113. $this->assertEquals( $mh->getMergedRevisionCount(), 1 );
  114. }
  115. }