995c4be16b533eefb1255b9761b4f9a404c4e26c
[lhc/web/wiklou.git] / tests / phpunit / includes / changes / RecentChangeTest.php
1 <?php
2 use Wikimedia\ScopedCallback;
3
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;
13
14 public function setUp() {
15 parent::setUp();
16
17 $this->title = Title::newFromText( 'SomeTitle' );
18 $this->target = Title::newFromText( 'TestTarget' );
19 $this->user = User::newFromName( 'UserName' );
20
21 $this->user_comment = '<User comment about action>';
22 $this->context = RequestContext::newExtraneousContext( $this->title );
23 }
24
25 /**
26 * @covers RecentChange::newFromRow
27 * @covers RecentChange::loadFromRow
28 */
29 public function testNewFromRow() {
30 $row = new stdClass();
31 $row->rc_foo = 'AAA';
32 $row->rc_timestamp = '20150921134808';
33 $row->rc_deleted = 'bar';
34
35 $rc = RecentChange::newFromRow( $row );
36
37 $expected = [
38 'rc_foo' => 'AAA',
39 'rc_timestamp' => '20150921134808',
40 'rc_deleted' => 'bar',
41 ];
42 $this->assertEquals( $expected, $rc->getAttributes() );
43 }
44
45 /**
46 * @covers RecentChange::parseParams
47 */
48 public function testParseParams() {
49 $params = [
50 'root' => [
51 'A' => 1,
52 'B' => 'two'
53 ]
54 ];
55
56 $this->assertParseParams(
57 $params,
58 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
59 );
60
61 $this->assertParseParams(
62 null,
63 null
64 );
65
66 $this->assertParseParams(
67 null,
68 serialize( false )
69 );
70
71 $this->assertParseParams(
72 null,
73 'not-an-array'
74 );
75 }
76
77 /**
78 * @param array $expectedParseParams
79 * @param string|null $rawRcParams
80 */
81 protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
82 $rc = new RecentChange;
83 $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
84
85 $actualParseParams = $rc->parseParams();
86
87 $this->assertEquals( $expectedParseParams, $actualParseParams );
88 }
89
90 /**
91 * 50 mins and 100 mins are used here as the tests never take that long!
92 * @return array
93 */
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 ],
100 ];
101 }
102
103 /**
104 * @covers RecentChange::isInRCLifespan
105 * @dataProvider provideIsInRCLifespan
106 */
107 public function testIsInRCLifespan( $maxAge, $timestamp, $tolerance, $expected ) {
108 $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
109 $this->assertEquals( $expected, RecentChange::isInRCLifespan( $timestamp, $tolerance ) );
110 }
111
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' ],
119 ];
120 }
121
122 /**
123 * @dataProvider provideRCTypes
124 * @covers RecentChange::parseFromRCType
125 */
126 public function testParseFromRCType( $rcType, $type ) {
127 $this->assertEquals( $type, RecentChange::parseFromRCType( $rcType ) );
128 }
129
130 /**
131 * @dataProvider provideRCTypes
132 * @covers RecentChange::parseToRCType
133 */
134 public function testParseToRCType( $rcType, $type ) {
135 $this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
136 }
137
138 /**
139 * @return PHPUnit_Framework_MockObject_MockObject|PageProps
140 */
141 private function getMockPageProps() {
142 return $this->getMockBuilder( PageProps::class )
143 ->disableOriginalConstructor()
144 ->getMock();
145 }
146
147 public function provideCategoryContent() {
148 return [
149 [ true ],
150 [ false ],
151 ];
152 }
153
154 /**
155 * @dataProvider provideCategoryContent
156 * @covers RecentChange::newForCategorization
157 */
158 public function testHiddenCategoryChange( $isHidden ) {
159 $categoryTitle = Title::newFromText( 'CategoryPage', NS_CATEGORY );
160
161 $pageProps = $this->getMockPageProps();
162 $pageProps->expects( $this->once() )
163 ->method( 'getProperties' )
164 ->with( $categoryTitle, 'hiddencat' )
165 ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [] ) );
166
167 $scopedOverride = PageProps::overrideInstance( $pageProps );
168
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
179 );
180
181 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
182
183 ScopedCallback::consume( $scopedOverride );
184 }
185 }