Add test for RecentChange::newFromRow
[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 = array(
37 'rc_foo' => 'AAA',
38 'rc_timestamp' => '20150921134808',
39 'rc_deleted' => 'bar',
40 );
41 $this->assertEquals( $expected, $rc->getAttributes() );
42 }
43
44 /**
45 * The testIrcMsgForAction* tests are supposed to cover the hacky
46 * LogFormatter::getIRCActionText / bug 34508
47 *
48 * Third parties bots listen to those messages. They are clever enough
49 * to fetch the i18n messages from the wiki and then analyze the IRC feed
50 * to reverse engineer the $1, $2 messages.
51 * One thing bots can not detect is when MediaWiki change the meaning of
52 * a message like what happened when we deployed 1.19. $1 became the user
53 * performing the action which broke basically all bots around.
54 *
55 * Should cover the following log actions (which are most commonly used by bots):
56 * - block/block
57 * - block/unblock
58 * - block/reblock
59 * - delete/delete
60 * - delete/restore
61 * - newusers/create
62 * - newusers/create2
63 * - newusers/autocreate
64 * - move/move
65 * - move/move_redir
66 * - protect/protect
67 * - protect/modifyprotect
68 * - protect/unprotect
69 * - protect/move_prot
70 * - upload/upload
71 * - merge/merge
72 * - import/upload
73 * - import/interwiki
74 *
75 * As well as the following Auto Edit Summaries:
76 * - blank
77 * - replace
78 * - rollback
79 * - undo
80 */
81
82 /**
83 * @covers RecentChange::parseParams
84 */
85 public function testParseParams() {
86 $params = array(
87 'root' => array(
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( array( 'rc_params' => $rawRcParams ) );
121
122 $actualParseParams = $rc->parseParams();
123
124 $this->assertEquals( $expectedParseParams, $actualParseParams );
125 }
126
127 /**
128 * 50 mins and 100 mins are used here as the tests never take that long!
129 * @return array
130 */
131 public function provideIsInRCLifespan() {
132 return array(
133 array( 6000, time() - 3000, 0, true ),
134 array( 3000, time() - 6000, 0, false ),
135 array( 6000, time() - 3000, 6000, true ),
136 array( 3000, time() - 6000, 6000, true ),
137 );
138 }
139
140 /**
141 * @covers RecentChange::isInRCLifespan
142 * @dataProvider provideIsInRCLifespan
143 */
144 public function testIsInRCLifespan( $maxAge, $timestamp, $tolerance, $expected ) {
145 $this->setMwGlobals( 'wgRCMaxAge', $maxAge );
146 $this->assertEquals( $expected, RecentChange::isInRCLifespan( $timestamp, $tolerance ) );
147 }
148
149 }