3 use Wikimedia\ScopedCallback
;
8 class RecentChangeTest
extends MediaWikiTestCase
{
12 protected $user_comment;
15 public function setUp() {
18 $this->title
= Title
::newFromText( 'SomeTitle' );
19 $this->target
= Title
::newFromText( 'TestTarget' );
20 $this->user
= User
::newFromName( 'UserName' );
22 $this->user_comment
= '<User comment about action>';
23 $this->context
= RequestContext
::newExtraneousContext( $this->title
);
27 * @covers RecentChange::newFromRow
28 * @covers RecentChange::loadFromRow
30 public function testNewFromRow() {
31 $user = $this->getTestUser()->getUser();
32 $actorId = $user->getActorId();
34 $row = new stdClass();
36 $row->rc_timestamp
= '20150921134808';
37 $row->rc_deleted
= 'bar';
38 $row->rc_comment_text
= 'comment';
39 $row->rc_comment_data
= null;
40 $row->rc_user
= $user->getId();
42 $rc = RecentChange
::newFromRow( $row );
46 'rc_timestamp' => '20150921134808',
47 'rc_deleted' => 'bar',
48 'rc_comment' => 'comment',
49 'rc_comment_text' => 'comment',
50 'rc_comment_data' => null,
51 'rc_user' => $user->getId(),
52 'rc_user_text' => $user->getName(),
53 'rc_actor' => $actorId,
55 $this->assertEquals( $expected, $rc->getAttributes() );
57 $row = new stdClass();
59 $row->rc_timestamp
= '20150921134808';
60 $row->rc_deleted
= 'bar';
61 $row->rc_comment
= 'comment';
62 $row->rc_user
= $user->getId();
64 Wikimedia\
suppressWarnings();
65 $rc = RecentChange
::newFromRow( $row );
66 Wikimedia\restoreWarnings
();
70 'rc_timestamp' => '20150921134808',
71 'rc_deleted' => 'bar',
72 'rc_comment' => 'comment',
73 'rc_comment_text' => 'comment',
74 'rc_comment_data' => null,
75 'rc_user' => $user->getId(),
76 'rc_user_text' => $user->getName(),
77 'rc_actor' => $actorId,
79 $this->assertEquals( $expected, $rc->getAttributes() );
83 * @covers RecentChange::parseParams
85 public function testParseParams() {
93 $this->assertParseParams(
95 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
98 $this->assertParseParams(
103 $this->assertParseParams(
108 $this->assertParseParams(
115 * @param array $expectedParseParams
116 * @param string|null $rawRcParams
118 protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
119 $rc = new RecentChange
;
120 $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
122 $actualParseParams = $rc->parseParams();
124 $this->assertEquals( $expectedParseParams, $actualParseParams );
130 public function provideIsInRCLifespan() {
132 [ 6000, -3000, 0, true ],
133 [ 3000, -6000, 0, false ],
134 [ 6000, -3000, 6000, true ],
135 [ 3000, -6000, 6000, true ],
140 * @covers RecentChange::isInRCLifespan
141 * @dataProvider provideIsInRCLifespan
143 public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
144 $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
145 // Calculate this here instead of the data provider because the provider
146 // is expanded early on and the full test suite may take longer than 100 minutes
147 // when coverage is enabled.
148 $timestamp = time() +
$offset;
149 $this->assertEquals( $expected, RecentChange
::isInRCLifespan( $timestamp, $tolerance ) );
152 public function provideRCTypes() {
157 [ RC_EXTERNAL
, 'external' ],
158 [ RC_CATEGORIZE
, 'categorize' ],
163 * @dataProvider provideRCTypes
164 * @covers RecentChange::parseFromRCType
166 public function testParseFromRCType( $rcType, $type ) {
167 $this->assertEquals( $type, RecentChange
::parseFromRCType( $rcType ) );
171 * @dataProvider provideRCTypes
172 * @covers RecentChange::parseToRCType
174 public function testParseToRCType( $rcType, $type ) {
175 $this->assertEquals( $rcType, RecentChange
::parseToRCType( $type ) );
179 * @return PHPUnit_Framework_MockObject_MockObject|PageProps
181 private function getMockPageProps() {
182 return $this->getMockBuilder( PageProps
::class )
183 ->disableOriginalConstructor()
187 public function provideCategoryContent() {
195 * @dataProvider provideCategoryContent
196 * @covers RecentChange::newForCategorization
198 public function testHiddenCategoryChange( $isHidden ) {
199 $categoryTitle = Title
::newFromText( 'CategoryPage', NS_CATEGORY
);
201 $pageProps = $this->getMockPageProps();
202 $pageProps->expects( $this->once() )
203 ->method( 'getProperties' )
204 ->with( $categoryTitle, 'hiddencat' )
205 ->will( $this->returnValue( $isHidden ?
[ $categoryTitle->getArticleID() => '' ] : [] ) );
207 $scopedOverride = PageProps
::overrideInstance( $pageProps );
209 $rc = RecentChange
::newForCategorization(
215 $categoryTitle->getLatestRevID(),
216 $categoryTitle->getLatestRevID(),
221 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
223 ScopedCallback
::consume( $scopedOverride );