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';
35 $rc = RecentChange
::newFromRow( $row );
39 'rc_timestamp' => '20150921134808',
40 'rc_deleted' => 'bar',
42 $this->assertEquals( $expected, $rc->getAttributes() );
46 * @covers RecentChange::parseParams
48 public function testParseParams() {
56 $this->assertParseParams(
58 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
61 $this->assertParseParams(
66 $this->assertParseParams(
71 $this->assertParseParams(
78 * @param array $expectedParseParams
79 * @param string|null $rawRcParams
81 protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
82 $rc = new RecentChange
;
83 $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
85 $actualParseParams = $rc->parseParams();
87 $this->assertEquals( $expectedParseParams, $actualParseParams );
93 public function provideIsInRCLifespan() {
95 [ 6000, -3000, 0, true ],
96 [ 3000, -6000, 0, false ],
97 [ 6000, -3000, 6000, true ],
98 [ 3000, -6000, 6000, true ],
103 * @covers RecentChange::isInRCLifespan
104 * @dataProvider provideIsInRCLifespan
106 public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
107 $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
108 // Calculate this here instead of the data provider because the provider
109 // is expanded early on and the full test suite may take longer than 100 minutes
110 // when coverage is enabled.
111 $timestamp = time() +
$offset;
112 $this->assertEquals( $expected, RecentChange
::isInRCLifespan( $timestamp, $tolerance ) );
115 public function provideRCTypes() {
120 [ RC_EXTERNAL
, 'external' ],
121 [ RC_CATEGORIZE
, 'categorize' ],
126 * @dataProvider provideRCTypes
127 * @covers RecentChange::parseFromRCType
129 public function testParseFromRCType( $rcType, $type ) {
130 $this->assertEquals( $type, RecentChange
::parseFromRCType( $rcType ) );
134 * @dataProvider provideRCTypes
135 * @covers RecentChange::parseToRCType
137 public function testParseToRCType( $rcType, $type ) {
138 $this->assertEquals( $rcType, RecentChange
::parseToRCType( $type ) );
142 * @return PHPUnit_Framework_MockObject_MockObject|PageProps
144 private function getMockPageProps() {
145 return $this->getMockBuilder( PageProps
::class )
146 ->disableOriginalConstructor()
150 public function provideCategoryContent() {
158 * @dataProvider provideCategoryContent
159 * @covers RecentChange::newForCategorization
161 public function testHiddenCategoryChange( $isHidden ) {
162 $categoryTitle = Title
::newFromText( 'CategoryPage', NS_CATEGORY
);
164 $pageProps = $this->getMockPageProps();
165 $pageProps->expects( $this->once() )
166 ->method( 'getProperties' )
167 ->with( $categoryTitle, 'hiddencat' )
168 ->will( $this->returnValue( $isHidden ?
[ $categoryTitle->getArticleID() => '' ] : [] ) );
170 $scopedOverride = PageProps
::overrideInstance( $pageProps );
172 $rc = RecentChange
::newForCategorization(
178 $categoryTitle->getLatestRevID(),
179 $categoryTitle->getLatestRevID(),
184 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
186 ScopedCallback
::consume( $scopedOverride );