2 use Wikimedia\ScopedCallback
;
7 class RecentChangeTest
extends MediaWikiTestCase
{
11 protected $user_comment;
14 public function setUp() {
17 $this->title
= Title
::newFromText( 'SomeTitle' );
18 $this->target
= Title
::newFromText( 'TestTarget' );
19 $this->user
= User
::newFromName( 'UserName' );
21 $this->user_comment
= '<User comment about action>';
22 $this->context
= RequestContext
::newExtraneousContext( $this->title
);
26 * @covers RecentChange::newFromRow
27 * @covers RecentChange::loadFromRow
29 public function testNewFromRow() {
30 $user = $this->getTestUser()->getUser();
31 $actorId = $user->getActorId();
33 $row = new stdClass();
35 $row->rc_timestamp
= '20150921134808';
36 $row->rc_deleted
= 'bar';
37 $row->rc_comment_text
= 'comment';
38 $row->rc_comment_data
= null;
39 $row->rc_user
= $user->getId();
41 $rc = RecentChange
::newFromRow( $row );
45 'rc_timestamp' => '20150921134808',
46 'rc_deleted' => 'bar',
47 'rc_comment' => 'comment',
48 'rc_comment_text' => 'comment',
49 'rc_comment_data' => null,
50 'rc_user' => $user->getId(),
51 'rc_user_text' => $user->getName(),
52 'rc_actor' => $actorId,
54 $this->assertEquals( $expected, $rc->getAttributes() );
56 $row = new stdClass();
58 $row->rc_timestamp
= '20150921134808';
59 $row->rc_deleted
= 'bar';
60 $row->rc_comment
= 'comment';
61 $row->rc_user
= $user->getId();
63 Wikimedia\
suppressWarnings();
64 $rc = RecentChange
::newFromRow( $row );
65 Wikimedia\restoreWarnings
();
69 'rc_timestamp' => '20150921134808',
70 'rc_deleted' => 'bar',
71 'rc_comment' => 'comment',
72 'rc_comment_text' => 'comment',
73 'rc_comment_data' => null,
74 'rc_user' => $user->getId(),
75 'rc_user_text' => $user->getName(),
76 'rc_actor' => $actorId,
78 $this->assertEquals( $expected, $rc->getAttributes() );
82 * @covers RecentChange::parseParams
84 public function testParseParams() {
92 $this->assertParseParams(
94 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
97 $this->assertParseParams(
102 $this->assertParseParams(
107 $this->assertParseParams(
114 * @param array $expectedParseParams
115 * @param string|null $rawRcParams
117 protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
118 $rc = new RecentChange
;
119 $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
121 $actualParseParams = $rc->parseParams();
123 $this->assertEquals( $expectedParseParams, $actualParseParams );
129 public function provideIsInRCLifespan() {
131 [ 6000, -3000, 0, true ],
132 [ 3000, -6000, 0, false ],
133 [ 6000, -3000, 6000, true ],
134 [ 3000, -6000, 6000, true ],
139 * @covers RecentChange::isInRCLifespan
140 * @dataProvider provideIsInRCLifespan
142 public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
143 $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
144 // Calculate this here instead of the data provider because the provider
145 // is expanded early on and the full test suite may take longer than 100 minutes
146 // when coverage is enabled.
147 $timestamp = time() +
$offset;
148 $this->assertEquals( $expected, RecentChange
::isInRCLifespan( $timestamp, $tolerance ) );
151 public function provideRCTypes() {
156 [ RC_EXTERNAL
, 'external' ],
157 [ RC_CATEGORIZE
, 'categorize' ],
162 * @dataProvider provideRCTypes
163 * @covers RecentChange::parseFromRCType
165 public function testParseFromRCType( $rcType, $type ) {
166 $this->assertEquals( $type, RecentChange
::parseFromRCType( $rcType ) );
170 * @dataProvider provideRCTypes
171 * @covers RecentChange::parseToRCType
173 public function testParseToRCType( $rcType, $type ) {
174 $this->assertEquals( $rcType, RecentChange
::parseToRCType( $type ) );
178 * @return PHPUnit_Framework_MockObject_MockObject|PageProps
180 private function getMockPageProps() {
181 return $this->getMockBuilder( PageProps
::class )
182 ->disableOriginalConstructor()
186 public function provideCategoryContent() {
194 * @dataProvider provideCategoryContent
195 * @covers RecentChange::newForCategorization
197 public function testHiddenCategoryChange( $isHidden ) {
198 $categoryTitle = Title
::newFromText( 'CategoryPage', NS_CATEGORY
);
200 $pageProps = $this->getMockPageProps();
201 $pageProps->expects( $this->once() )
202 ->method( 'getProperties' )
203 ->with( $categoryTitle, 'hiddencat' )
204 ->will( $this->returnValue( $isHidden ?
[ $categoryTitle->getArticleID() => '' ] : [] ) );
206 $scopedOverride = PageProps
::overrideInstance( $pageProps );
208 $rc = RecentChange
::newForCategorization(
214 $categoryTitle->getLatestRevID(),
215 $categoryTitle->getLatestRevID(),
220 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
222 ScopedCallback
::consume( $scopedOverride );