Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / changes / RecentChangeTest.php
blob995c4be16b533eefb1255b9761b4f9a404c4e26c
1 <?php
2 use Wikimedia\ScopedCallback;
4 /**
5 * @group Database
6 */
7 class RecentChangeTest extends MediaWikiTestCase {
8 protected $title;
9 protected $target;
10 protected $user;
11 protected $user_comment;
12 protected $context;
14 public function setUp() {
15 parent::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 );
25 /**
26 * @covers RecentChange::newFromRow
27 * @covers RecentChange::loadFromRow
29 public function testNewFromRow() {
30 $row = new stdClass();
31 $row->rc_foo = 'AAA';
32 $row->rc_timestamp = '20150921134808';
33 $row->rc_deleted = 'bar';
35 $rc = RecentChange::newFromRow( $row );
37 $expected = [
38 'rc_foo' => 'AAA',
39 'rc_timestamp' => '20150921134808',
40 'rc_deleted' => 'bar',
42 $this->assertEquals( $expected, $rc->getAttributes() );
45 /**
46 * @covers RecentChange::parseParams
48 public function testParseParams() {
49 $params = [
50 'root' => [
51 'A' => 1,
52 'B' => 'two'
56 $this->assertParseParams(
57 $params,
58 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
61 $this->assertParseParams(
62 null,
63 null
66 $this->assertParseParams(
67 null,
68 serialize( false )
71 $this->assertParseParams(
72 null,
73 'not-an-array'
77 /**
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 );
90 /**
91 * 50 mins and 100 mins are used here as the tests never take that long!
92 * @return array
94 public function provideIsInRCLifespan() {
95 return [
96 [ 6000, time() - 3000, 0, true ],
97 [ 3000, time() - 6000, 0, false ],
98 [ 6000, time() - 3000, 6000, true ],
99 [ 3000, time() - 6000, 6000, true ],
104 * @covers RecentChange::isInRCLifespan
105 * @dataProvider provideIsInRCLifespan
107 public function testIsInRCLifespan( $maxAge, $timestamp, $tolerance, $expected ) {
108 $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
109 $this->assertEquals( $expected, RecentChange::isInRCLifespan( $timestamp, $tolerance ) );
112 public function provideRCTypes() {
113 return [
114 [ RC_EDIT, 'edit' ],
115 [ RC_NEW, 'new' ],
116 [ RC_LOG, 'log' ],
117 [ RC_EXTERNAL, 'external' ],
118 [ RC_CATEGORIZE, 'categorize' ],
123 * @dataProvider provideRCTypes
124 * @covers RecentChange::parseFromRCType
126 public function testParseFromRCType( $rcType, $type ) {
127 $this->assertEquals( $type, RecentChange::parseFromRCType( $rcType ) );
131 * @dataProvider provideRCTypes
132 * @covers RecentChange::parseToRCType
134 public function testParseToRCType( $rcType, $type ) {
135 $this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
139 * @return PHPUnit_Framework_MockObject_MockObject|PageProps
141 private function getMockPageProps() {
142 return $this->getMockBuilder( PageProps::class )
143 ->disableOriginalConstructor()
144 ->getMock();
147 public function provideCategoryContent() {
148 return [
149 [ true ],
150 [ false ],
155 * @dataProvider provideCategoryContent
156 * @covers RecentChange::newForCategorization
158 public function testHiddenCategoryChange( $isHidden ) {
159 $categoryTitle = Title::newFromText( 'CategoryPage', NS_CATEGORY );
161 $pageProps = $this->getMockPageProps();
162 $pageProps->expects( $this->once() )
163 ->method( 'getProperties' )
164 ->with( $categoryTitle, 'hiddencat' )
165 ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [] ) );
167 $scopedOverride = PageProps::overrideInstance( $pageProps );
169 $rc = RecentChange::newForCategorization(
170 '0',
171 $categoryTitle,
172 $this->user,
173 $this->user_comment,
174 $this->title,
175 $categoryTitle->getLatestRevID(),
176 $categoryTitle->getLatestRevID(),
177 '0',
178 false
181 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
183 ScopedCallback::consume( $scopedOverride );