Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / includes / changes / RecentChangeTest.php
1 <?php
2
3 use Wikimedia\ScopedCallback;
4
5 /**
6 * @group Database
7 */
8 class RecentChangeTest extends MediaWikiTestCase {
9 protected $title;
10 protected $target;
11 protected $user;
12 protected $user_comment;
13 protected $context;
14
15 public function setUp() {
16 parent::setUp();
17
18 $this->title = Title::newFromText( 'SomeTitle' );
19 $this->target = Title::newFromText( 'TestTarget' );
20 $this->user = User::newFromName( 'UserName' );
21
22 $this->user_comment = '<User comment about action>';
23 $this->context = RequestContext::newExtraneousContext( $this->title );
24 }
25
26 /**
27 * @covers RecentChange::newFromRow
28 * @covers RecentChange::loadFromRow
29 */
30 public function testNewFromRow() {
31 $user = $this->getTestUser()->getUser();
32 $actorId = $user->getActorId();
33
34 $row = new stdClass();
35 $row->rc_foo = 'AAA';
36 $row->rc_timestamp = '20150921134808';
37 $row->rc_deleted = 'bar';
38 $row->rc_comment_text = 'comment';
39 $row->rc_comment_data = null;
40 $row->rc_user = $user->getId();
41
42 $rc = RecentChange::newFromRow( $row );
43
44 $expected = [
45 'rc_foo' => 'AAA',
46 'rc_timestamp' => '20150921134808',
47 'rc_deleted' => 'bar',
48 'rc_comment' => 'comment',
49 'rc_comment_text' => 'comment',
50 'rc_comment_data' => null,
51 'rc_user' => $user->getId(),
52 'rc_user_text' => $user->getName(),
53 'rc_actor' => $actorId,
54 ];
55 $this->assertEquals( $expected, $rc->getAttributes() );
56
57 $row = new stdClass();
58 $row->rc_foo = 'AAA';
59 $row->rc_timestamp = '20150921134808';
60 $row->rc_deleted = 'bar';
61 $row->rc_comment = 'comment';
62 $row->rc_user = $user->getId();
63
64 Wikimedia\suppressWarnings();
65 $rc = RecentChange::newFromRow( $row );
66 Wikimedia\restoreWarnings();
67
68 $expected = [
69 'rc_foo' => 'AAA',
70 'rc_timestamp' => '20150921134808',
71 'rc_deleted' => 'bar',
72 'rc_comment' => 'comment',
73 'rc_comment_text' => 'comment',
74 'rc_comment_data' => null,
75 'rc_user' => $user->getId(),
76 'rc_user_text' => $user->getName(),
77 'rc_actor' => $actorId,
78 ];
79 $this->assertEquals( $expected, $rc->getAttributes() );
80 }
81
82 /**
83 * @covers RecentChange::parseParams
84 */
85 public function testParseParams() {
86 $params = [
87 'root' => [
88 'A' => 1,
89 'B' => 'two'
90 ]
91 ];
92
93 $this->assertParseParams(
94 $params,
95 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
96 );
97
98 $this->assertParseParams(
99 null,
100 null
101 );
102
103 $this->assertParseParams(
104 null,
105 serialize( false )
106 );
107
108 $this->assertParseParams(
109 null,
110 'not-an-array'
111 );
112 }
113
114 /**
115 * @param array $expectedParseParams
116 * @param string|null $rawRcParams
117 */
118 protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
119 $rc = new RecentChange;
120 $rc->setAttribs( [ 'rc_params' => $rawRcParams ] );
121
122 $actualParseParams = $rc->parseParams();
123
124 $this->assertEquals( $expectedParseParams, $actualParseParams );
125 }
126
127 /**
128 * @return array
129 */
130 public function provideIsInRCLifespan() {
131 return [
132 [ 6000, -3000, 0, true ],
133 [ 3000, -6000, 0, false ],
134 [ 6000, -3000, 6000, true ],
135 [ 3000, -6000, 6000, true ],
136 ];
137 }
138
139 /**
140 * @covers RecentChange::isInRCLifespan
141 * @dataProvider provideIsInRCLifespan
142 */
143 public function testIsInRCLifespan( $maxAge, $offset, $tolerance, $expected ) {
144 $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
145 // Calculate this here instead of the data provider because the provider
146 // is expanded early on and the full test suite may take longer than 100 minutes
147 // when coverage is enabled.
148 $timestamp = time() + $offset;
149 $this->assertEquals( $expected, RecentChange::isInRCLifespan( $timestamp, $tolerance ) );
150 }
151
152 public function provideRCTypes() {
153 return [
154 [ RC_EDIT, 'edit' ],
155 [ RC_NEW, 'new' ],
156 [ RC_LOG, 'log' ],
157 [ RC_EXTERNAL, 'external' ],
158 [ RC_CATEGORIZE, 'categorize' ],
159 ];
160 }
161
162 /**
163 * @dataProvider provideRCTypes
164 * @covers RecentChange::parseFromRCType
165 */
166 public function testParseFromRCType( $rcType, $type ) {
167 $this->assertEquals( $type, RecentChange::parseFromRCType( $rcType ) );
168 }
169
170 /**
171 * @dataProvider provideRCTypes
172 * @covers RecentChange::parseToRCType
173 */
174 public function testParseToRCType( $rcType, $type ) {
175 $this->assertEquals( $rcType, RecentChange::parseToRCType( $type ) );
176 }
177
178 /**
179 * @return PHPUnit_Framework_MockObject_MockObject|PageProps
180 */
181 private function getMockPageProps() {
182 return $this->getMockBuilder( PageProps::class )
183 ->disableOriginalConstructor()
184 ->getMock();
185 }
186
187 public function provideCategoryContent() {
188 return [
189 [ true ],
190 [ false ],
191 ];
192 }
193
194 /**
195 * @dataProvider provideCategoryContent
196 * @covers RecentChange::newForCategorization
197 */
198 public function testHiddenCategoryChange( $isHidden ) {
199 $categoryTitle = Title::newFromText( 'CategoryPage', NS_CATEGORY );
200
201 $pageProps = $this->getMockPageProps();
202 $pageProps->expects( $this->once() )
203 ->method( 'getProperties' )
204 ->with( $categoryTitle, 'hiddencat' )
205 ->will( $this->returnValue( $isHidden ? [ $categoryTitle->getArticleID() => '' ] : [] ) );
206
207 $scopedOverride = PageProps::overrideInstance( $pageProps );
208
209 $rc = RecentChange::newForCategorization(
210 '0',
211 $categoryTitle,
212 $this->user,
213 $this->user_comment,
214 $this->title,
215 $categoryTitle->getLatestRevID(),
216 $categoryTitle->getLatestRevID(),
217 '0',
218 false
219 );
220
221 $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
222
223 ScopedCallback::consume( $scopedOverride );
224 }
225 }