phpunit: Fix RecentChangeTest failure when coverage is enabled
[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 * @return array
92 */
93 public function provideIsInRCLifespan() {
94 return [
95 [ 6000, -3000, 0, true ],
96 [ 3000, -6000, 0, false ],
97 [ 6000, -3000, 6000, true ],
98 [ 3000, -6000, 6000, true ],
99 ];
100 }
101
102 /**
103 * @covers RecentChange::isInRCLifespan
104 * @dataProvider provideIsInRCLifespan
105 */
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 ) );
113 }
114
115 public function provideRCTypes() {
116 return [
117 [ RC_EDIT, 'edit' ],
118 [ RC_NEW, 'new' ],
119 [ RC_LOG, 'log' ],
120 [ RC_EXTERNAL, 'external' ],
121 [ RC_CATEGORIZE, 'categorize' ],
122 ];
123 }
124
125 /**
126 * @dataProvider provideRCTypes
127 * @covers RecentChange::parseFromRCType
128 */
129 public function testParseFromRCType( $rcType, $type ) {
130 $this->assertEquals( $type, RecentChange::parseFromRCType( $rcType ) );
131 }
132
133 /**
134 * @dataProvider provideRCTypes
135 * @covers RecentChange::parseToRCType
136 */
137 public function testParseToRCType( $rcType, $type ) {
138 $this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
139 }
140
141 /**
142 * @return PHPUnit_Framework_MockObject_MockObject|PageProps
143 */
144 private function getMockPageProps() {
145 return $this->getMockBuilder( PageProps::class )
146 ->disableOriginalConstructor()
147 ->getMock();
148 }
149
150 public function provideCategoryContent() {
151 return [
152 [ true ],
153 [ false ],
154 ];
155 }
156
157 /**
158 * @dataProvider provideCategoryContent
159 * @covers RecentChange::newForCategorization
160 */
161 public function testHiddenCategoryChange( $isHidden ) {
162 $categoryTitle = Title::newFromText( 'CategoryPage', NS_CATEGORY );
163
164 $pageProps = $this->getMockPageProps();
165 $pageProps->expects( $this->once() )
166 ->method( 'getProperties' )
167 ->with( $categoryTitle, 'hiddencat' )
168 ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [] ) );
169
170 $scopedOverride = PageProps::overrideInstance( $pageProps );
171
172 $rc = RecentChange::newForCategorization(
173 '0',
174 $categoryTitle,
175 $this->user,
176 $this->user_comment,
177 $this->title,
178 $categoryTitle->getLatestRevID(),
179 $categoryTitle->getLatestRevID(),
180 '0',
181 false
182 );
183
184 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
185
186 ScopedCallback::consume( $scopedOverride );
187 }
188 }