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