123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- use Wikimedia\ScopedCallback;
- /**
- * @group Database
- */
- class RecentChangeTest extends MediaWikiTestCase {
- protected $title;
- protected $target;
- protected $user;
- protected $user_comment;
- protected $context;
- public function setUp() {
- parent::setUp();
- $this->title = Title::newFromText( 'SomeTitle' );
- $this->target = Title::newFromText( 'TestTarget' );
- $this->user = User::newFromName( 'UserName' );
- $this->user_comment = '<User comment about action>';
- $this->context = RequestContext::newExtraneousContext( $this->title );
- }
- /**
- * @covers RecentChange::newFromRow
- * @covers RecentChange::loadFromRow
- */
- public function testNewFromRow() {
- $user = $this->getTestUser()->getUser();
- $actorId = $user->getActorId();
- $row = new stdClass();
- $row->rc_foo = 'AAA';
- $row->rc_timestamp = '20150921134808';
- $row->rc_deleted = 'bar';
- $row->rc_comment_text = 'comment';
- $row->rc_comment_data = null;
- $row->rc_user = $user->getId();
- $rc = RecentChange::newFromRow( $row );
- $expected = [
- 'rc_foo' => 'AAA',
- 'rc_timestamp' => '20150921134808',
- 'rc_deleted' => 'bar',
- 'rc_comment' => 'comment',
- 'rc_comment_text' => 'comment',
- 'rc_comment_data' => null,
- 'rc_user' => $user->getId(),
- 'rc_user_text' => $user->getName(),
- 'rc_actor' => $actorId,
- ];
- $this->assertEquals( $expected, $rc->getAttributes() );
- $row = new stdClass();
- $row->rc_foo = 'AAA';
- $row->rc_timestamp = '20150921134808';
- $row->rc_deleted = 'bar';
- $row->rc_comment = 'comment';
- $row->rc_user = $user->getId();
- Wikimedia\suppressWarnings();
- $rc = RecentChange::newFromRow( $row );
- Wikimedia\restoreWarnings();
- $expected = [
- 'rc_foo' => 'AAA',
- 'rc_timestamp' => '20150921134808',
- 'rc_deleted' => 'bar',
- 'rc_comment' => 'comment',
- 'rc_comment_text' => 'comment',
- 'rc_comment_data' => null,
- 'rc_user' => $user->getId(),
- 'rc_user_text' => $user->getName(),
- 'rc_actor' => $actorId,
- ];
- $this->assertEquals( $expected, $rc->getAttributes() );
- }
- /**
- * @covers RecentChange::parseParams
- */
- public function testParseParams() {
- $params = [
- 'root' => [
- 'A' => 1,
- 'B' => 'two'
- ]
- ];
- $this->assertParseParams(
- $params,
- 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
- );
- $this->assertParseParams(
- null,
- null
- );
- $this->assertParseParams(
- null,
- serialize( false )
- );
- $this->assertParseParams(
- null,
- 'not-an-array'
- );
- }
- /**
- * @param array $expectedParseParams
- * @param string|null $rawRcParams
- */
- protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
- $rc = new RecentChange;
- $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
- $actualParseParams = $rc->parseParams();
- $this->assertEquals( $expectedParseParams, $actualParseParams );
- }
- /**
- * @return array
- */
- public function provideIsInRCLifespan() {
- return [
- [ 6000, -3000, 0, true ],
- [ 3000, -6000, 0, false ],
- [ 6000, -3000, 6000, true ],
- [ 3000, -6000, 6000, true ],
- ];
- }
- /**
- * @covers RecentChange::isInRCLifespan
- * @dataProvider provideIsInRCLifespan
- */
- public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
- $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
- // Calculate this here instead of the data provider because the provider
- // is expanded early on and the full test suite may take longer than 100 minutes
- // when coverage is enabled.
- $timestamp = time() + $offset;
- $this->assertEquals( $expected, RecentChange::isInRCLifespan( $timestamp, $tolerance ) );
- }
- public function provideRCTypes() {
- return [
- [ RC_EDIT, 'edit' ],
- [ RC_NEW, 'new' ],
- [ RC_LOG, 'log' ],
- [ RC_EXTERNAL, 'external' ],
- [ RC_CATEGORIZE, 'categorize' ],
- ];
- }
- /**
- * @dataProvider provideRCTypes
- * @covers RecentChange::parseFromRCType
- */
- public function testParseFromRCType( $rcType, $type ) {
- $this->assertEquals( $type, RecentChange::parseFromRCType( $rcType ) );
- }
- /**
- * @dataProvider provideRCTypes
- * @covers RecentChange::parseToRCType
- */
- public function testParseToRCType( $rcType, $type ) {
- $this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
- }
- /**
- * @return PHPUnit_Framework_MockObject_MockObject|PageProps
- */
- private function getMockPageProps() {
- return $this->getMockBuilder( PageProps::class )
- ->disableOriginalConstructor()
- ->getMock();
- }
- public function provideCategoryContent() {
- return [
- [ true ],
- [ false ],
- ];
- }
- /**
- * @dataProvider provideCategoryContent
- * @covers RecentChange::newForCategorization
- */
- public function testHiddenCategoryChange( $isHidden ) {
- $categoryTitle = Title::newFromText( 'CategoryPage', NS_CATEGORY );
- $pageProps = $this->getMockPageProps();
- $pageProps->expects( $this->once() )
- ->method( 'getProperties' )
- ->with( $categoryTitle, 'hiddencat' )
- ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [] ) );
- $scopedOverride = PageProps::overrideInstance( $pageProps );
- $rc = RecentChange::newForCategorization(
- '0',
- $categoryTitle,
- $this->user,
- $this->user_comment,
- $this->title,
- $categoryTitle->getLatestRevID(),
- $categoryTitle->getLatestRevID(),
- '0',
- false
- );
- $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
- ScopedCallback::consume( $scopedOverride );
- }
- }
|