Merge "Revert "Add type hint against LinkTarget""
[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 }