RecentChangeTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. use Wikimedia\ScopedCallback;
  3. /**
  4. * @group Database
  5. */
  6. class RecentChangeTest extends MediaWikiTestCase {
  7. protected $title;
  8. protected $target;
  9. protected $user;
  10. protected $user_comment;
  11. protected $context;
  12. public function setUp() {
  13. parent::setUp();
  14. $this->title = Title::newFromText( 'SomeTitle' );
  15. $this->target = Title::newFromText( 'TestTarget' );
  16. $this->user = User::newFromName( 'UserName' );
  17. $this->user_comment = '<User comment about action>';
  18. $this->context = RequestContext::newExtraneousContext( $this->title );
  19. }
  20. /**
  21. * @covers RecentChange::newFromRow
  22. * @covers RecentChange::loadFromRow
  23. */
  24. public function testNewFromRow() {
  25. $user = $this->getTestUser()->getUser();
  26. $actorId = $user->getActorId();
  27. $row = new stdClass();
  28. $row->rc_foo = 'AAA';
  29. $row->rc_timestamp = '20150921134808';
  30. $row->rc_deleted = 'bar';
  31. $row->rc_comment_text = 'comment';
  32. $row->rc_comment_data = null;
  33. $row->rc_user = $user->getId();
  34. $rc = RecentChange::newFromRow( $row );
  35. $expected = [
  36. 'rc_foo' => 'AAA',
  37. 'rc_timestamp' => '20150921134808',
  38. 'rc_deleted' => 'bar',
  39. 'rc_comment' => 'comment',
  40. 'rc_comment_text' => 'comment',
  41. 'rc_comment_data' => null,
  42. 'rc_user' => $user->getId(),
  43. 'rc_user_text' => $user->getName(),
  44. 'rc_actor' => $actorId,
  45. ];
  46. $this->assertEquals( $expected, $rc->getAttributes() );
  47. $row = new stdClass();
  48. $row->rc_foo = 'AAA';
  49. $row->rc_timestamp = '20150921134808';
  50. $row->rc_deleted = 'bar';
  51. $row->rc_comment = 'comment';
  52. $row->rc_user = $user->getId();
  53. Wikimedia\suppressWarnings();
  54. $rc = RecentChange::newFromRow( $row );
  55. Wikimedia\restoreWarnings();
  56. $expected = [
  57. 'rc_foo' => 'AAA',
  58. 'rc_timestamp' => '20150921134808',
  59. 'rc_deleted' => 'bar',
  60. 'rc_comment' => 'comment',
  61. 'rc_comment_text' => 'comment',
  62. 'rc_comment_data' => null,
  63. 'rc_user' => $user->getId(),
  64. 'rc_user_text' => $user->getName(),
  65. 'rc_actor' => $actorId,
  66. ];
  67. $this->assertEquals( $expected, $rc->getAttributes() );
  68. }
  69. /**
  70. * @covers RecentChange::parseParams
  71. */
  72. public function testParseParams() {
  73. $params = [
  74. 'root' => [
  75. 'A' => 1,
  76. 'B' => 'two'
  77. ]
  78. ];
  79. $this->assertParseParams(
  80. $params,
  81. 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
  82. );
  83. $this->assertParseParams(
  84. null,
  85. null
  86. );
  87. $this->assertParseParams(
  88. null,
  89. serialize( false )
  90. );
  91. $this->assertParseParams(
  92. null,
  93. 'not-an-array'
  94. );
  95. }
  96. /**
  97. * @param array $expectedParseParams
  98. * @param string|null $rawRcParams
  99. */
  100. protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
  101. $rc = new RecentChange;
  102. $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
  103. $actualParseParams = $rc->parseParams();
  104. $this->assertEquals( $expectedParseParams, $actualParseParams );
  105. }
  106. /**
  107. * @return array
  108. */
  109. public function provideIsInRCLifespan() {
  110. return [
  111. [ 6000, -3000, 0, true ],
  112. [ 3000, -6000, 0, false ],
  113. [ 6000, -3000, 6000, true ],
  114. [ 3000, -6000, 6000, true ],
  115. ];
  116. }
  117. /**
  118. * @covers RecentChange::isInRCLifespan
  119. * @dataProvider provideIsInRCLifespan
  120. */
  121. public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
  122. $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
  123. // Calculate this here instead of the data provider because the provider
  124. // is expanded early on and the full test suite may take longer than 100 minutes
  125. // when coverage is enabled.
  126. $timestamp = time() + $offset;
  127. $this->assertEquals( $expected, RecentChange::isInRCLifespan( $timestamp, $tolerance ) );
  128. }
  129. public function provideRCTypes() {
  130. return [
  131. [ RC_EDIT, 'edit' ],
  132. [ RC_NEW, 'new' ],
  133. [ RC_LOG, 'log' ],
  134. [ RC_EXTERNAL, 'external' ],
  135. [ RC_CATEGORIZE, 'categorize' ],
  136. ];
  137. }
  138. /**
  139. * @dataProvider provideRCTypes
  140. * @covers RecentChange::parseFromRCType
  141. */
  142. public function testParseFromRCType( $rcType, $type ) {
  143. $this->assertEquals( $type, RecentChange::parseFromRCType( $rcType ) );
  144. }
  145. /**
  146. * @dataProvider provideRCTypes
  147. * @covers RecentChange::parseToRCType
  148. */
  149. public function testParseToRCType( $rcType, $type ) {
  150. $this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
  151. }
  152. /**
  153. * @return PHPUnit_Framework_MockObject_MockObject|PageProps
  154. */
  155. private function getMockPageProps() {
  156. return $this->getMockBuilder( PageProps::class )
  157. ->disableOriginalConstructor()
  158. ->getMock();
  159. }
  160. public function provideCategoryContent() {
  161. return [
  162. [ true ],
  163. [ false ],
  164. ];
  165. }
  166. /**
  167. * @dataProvider provideCategoryContent
  168. * @covers RecentChange::newForCategorization
  169. */
  170. public function testHiddenCategoryChange( $isHidden ) {
  171. $categoryTitle = Title::newFromText( 'CategoryPage', NS_CATEGORY );
  172. $pageProps = $this->getMockPageProps();
  173. $pageProps->expects( $this->once() )
  174. ->method( 'getProperties' )
  175. ->with( $categoryTitle, 'hiddencat' )
  176. ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [] ) );
  177. $scopedOverride = PageProps::overrideInstance( $pageProps );
  178. $rc = RecentChange::newForCategorization(
  179. '0',
  180. $categoryTitle,
  181. $this->user,
  182. $this->user_comment,
  183. $this->title,
  184. $categoryTitle->getLatestRevID(),
  185. $categoryTitle->getLatestRevID(),
  186. '0',
  187. false
  188. );
  189. $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
  190. ScopedCallback::consume( $scopedOverride );
  191. }
  192. }