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 $row = new stdClass();
32 $row->rc_timestamp
= '20150921134808';
33 $row->rc_deleted
= 'bar';
34 $row->rc_comment_text
= 'comment';
35 $row->rc_comment_data
= null;
37 $rc = RecentChange
::newFromRow( $row );
41 'rc_timestamp' => '20150921134808',
42 'rc_deleted' => 'bar',
43 'rc_comment' => 'comment',
44 'rc_comment_text' => 'comment',
45 'rc_comment_data' => null,
47 $this->assertEquals( $expected, $rc->getAttributes() );
49 $row = new stdClass();
51 $row->rc_timestamp
= '20150921134808';
52 $row->rc_deleted
= 'bar';
53 $row->rc_comment
= 'comment';
55 MediaWiki\
suppressWarnings();
56 $rc = RecentChange
::newFromRow( $row );
57 MediaWiki\restoreWarnings
();
61 'rc_timestamp' => '20150921134808',
62 'rc_deleted' => 'bar',
63 'rc_comment' => 'comment',
64 'rc_comment_text' => 'comment',
65 'rc_comment_data' => null,
67 $this->assertEquals( $expected, $rc->getAttributes() );
71 * @covers RecentChange::parseParams
73 public function testParseParams() {
81 $this->assertParseParams(
83 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
86 $this->assertParseParams(
91 $this->assertParseParams(
96 $this->assertParseParams(
103 * @param array $expectedParseParams
104 * @param string|null $rawRcParams
106 protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
107 $rc = new RecentChange
;
108 $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
110 $actualParseParams = $rc->parseParams();
112 $this->assertEquals( $expectedParseParams, $actualParseParams );
118 public function provideIsInRCLifespan() {
120 [ 6000, -3000, 0, true ],
121 [ 3000, -6000, 0, false ],
122 [ 6000, -3000, 6000, true ],
123 [ 3000, -6000, 6000, true ],
128 * @covers RecentChange::isInRCLifespan
129 * @dataProvider provideIsInRCLifespan
131 public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
132 $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
133 // Calculate this here instead of the data provider because the provider
134 // is expanded early on and the full test suite may take longer than 100 minutes
135 // when coverage is enabled.
136 $timestamp = time() +
$offset;
137 $this->assertEquals( $expected, RecentChange
::isInRCLifespan( $timestamp, $tolerance ) );
140 public function provideRCTypes() {
145 [ RC_EXTERNAL
, 'external' ],
146 [ RC_CATEGORIZE
, 'categorize' ],
151 * @dataProvider provideRCTypes
152 * @covers RecentChange::parseFromRCType
154 public function testParseFromRCType( $rcType, $type ) {
155 $this->assertEquals( $type, RecentChange
::parseFromRCType( $rcType ) );
159 * @dataProvider provideRCTypes
160 * @covers RecentChange::parseToRCType
162 public function testParseToRCType( $rcType, $type ) {
163 $this->assertEquals( $rcType, RecentChange
::parseToRCType( $type ) );
167 * @return PHPUnit_Framework_MockObject_MockObject|PageProps
169 private function getMockPageProps() {
170 return $this->getMockBuilder( PageProps
::class )
171 ->disableOriginalConstructor()
175 public function provideCategoryContent() {
183 * @dataProvider provideCategoryContent
184 * @covers RecentChange::newForCategorization
186 public function testHiddenCategoryChange( $isHidden ) {
187 $categoryTitle = Title
::newFromText( 'CategoryPage', NS_CATEGORY
);
189 $pageProps = $this->getMockPageProps();
190 $pageProps->expects( $this->once() )
191 ->method( 'getProperties' )
192 ->with( $categoryTitle, 'hiddencat' )
193 ->will( $this->returnValue( $isHidden ?
[ $categoryTitle->getArticleID() => '' ] : [] ) );
195 $scopedOverride = PageProps
::overrideInstance( $pageProps );
197 $rc = RecentChange
::newForCategorization(
203 $categoryTitle->getLatestRevID(),
204 $categoryTitle->getLatestRevID(),
209 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
211 ScopedCallback
::consume( $scopedOverride );