basic tests for bug 34508
[lhc/web/wiklou.git] / tests / phpunit / includes / RecentChange.php
1 <?php
2 class RecentChangeTest extends MediaWikiTestCase {
3 protected $title;
4 protected $target;
5 protected $user;
6 protected $user_comment;
7
8 function __construct() {
9 parent::__construct();
10
11 $this->title = Title::newFromText( 'SomeTitle' );
12 $this->target = Title::newFromText( 'TestTarget' );
13 $this->user = User::newFromName( 'UserName' );
14
15 $this->user_comment = '<User comment about action>';
16 }
17
18 /**
19 * The testIrcMsgForAction* tests are supposed to cover the hacky
20 * LogFormatter::getIRCActionText / bug 34508
21 *
22 * Third parties bots listen to those messages. They are clever enough
23 * to fetch the i18n messages from the wiki and then analyze the IRC feed
24 * to reverse engineer the $1, $2 messages.
25 * One thing bots can not detect is when MediaWiki change the meaning of
26 * a message like what happened when we deployed 1.19. $1 became the user
27 * performing the action which broke basically all bots around.
28 *
29 * Need to cover the various switches cases:
30 * move: move, move_redir, move-noredirect, move_redir-noredirect
31 * delete: delete, restore
32 * patrol: patrol
33 * newusers: newusers, create, create2, autocreate
34 * upload: upload, overwrite
35 * suppress
36 * and default case
37 */
38
39 /**
40 * @covers LogFormatter::getIRCActionText
41 */
42 function testIrcMsgForActionMove() {
43 $move_params = array(
44 '4::target' => $this->target->getPrefixedText(),
45 '5::noredir' => 0,
46 );
47
48 # move/move
49 $this->assertIRCComment(
50 "moved [[SomeTitle]] to [[TestTarget]]: {$this->user_comment}"
51 , 'move', 'move'
52 , $move_params
53 );
54
55 # move/move_redir
56 $this->assertIRCComment(
57 "moved [[SomeTitle]] to [[TestTarget]] over redirect: {$this->user_comment}"
58 , 'move', 'move_redir'
59 , $move_params
60 );
61 }
62
63 /**
64 * @covers LogFormatter::getIRCActionText
65 */
66 function testIrcMsgForActionDelete() {
67 # delete/delete
68 $this->assertIRCComment(
69 "deleted &quot;[[SomeTitle]]&quot;: {$this->user_comment}"
70 , 'delete', 'delete'
71 , array()
72 );
73
74 # delete/restore
75 $this->assertIRCComment(
76 "restored &quot;[[SomeTitle]]&quot;: {$this->user_comment}"
77 , 'delete', 'restore'
78 , array()
79 );
80 }
81
82 /**
83 * @covers LogFormatter::getIRCActionText
84 */
85 function testIrcMsgForActionPatrol() {
86 # patrol/patrol
87 $this->assertIRCComment(
88 "marked revision 777 of [[SomeTitle]] patrolled : {$this->user_comment}"
89 , 'patrol', 'patrol'
90 , array(
91 '4::curid' => '777',
92 '5::previd' => '666',
93 '6::auto' => 0,
94
95 )
96 );
97 }
98
99 /**
100 * @covers LogFormatter::getIRCActionText
101 */
102 function testIrcMsgForActionNewusers() {
103 $this->assertIRCComment(
104 "New user account: {$this->user_comment}"
105 , 'newusers', 'newusers'
106 , array()
107 );
108 $this->assertIRCComment(
109 "New user account: {$this->user_comment}"
110 , 'newusers', 'create'
111 , array()
112 );
113 $this->assertIRCComment(
114 "created new account SomeTitle: {$this->user_comment}"
115 , 'newusers', 'create2'
116 , array()
117 );
118 $this->assertIRCComment(
119 "Account created automatically: {$this->user_comment}"
120 , 'newusers', 'autocreate'
121 , array()
122 );
123 }
124
125 /**
126 * @covers LogFormatter::getIRCActionText
127 */
128 function testIrcMsgForActionUpload() {
129 # upload/upload
130 $this->assertIRCComment(
131 "uploaded &quot;[[SomeTitle]]&quot;: {$this->user_comment}"
132 , 'upload', 'upload'
133 , array()
134 );
135
136 # upload/overwrite
137 $this->assertIRCComment(
138 "uploaded a new version of &quot;[[SomeTitle]]&quot;: {$this->user_comment}"
139 , 'upload', 'overwrite'
140 , array()
141 );
142 }
143
144 /**
145 * @covers LogFormatter::getIRCActionText
146 */
147 function testIrcMsgForActionSuppress() {
148 $this->assertIRCComment(
149 "{$this->user_comment}"
150 , 'suppress', ''
151 , array()
152 );
153 }
154
155 /**
156 * @param $expected String Expected IRC text without colors codes
157 * @param $type String Log type (move, delete, suppress, patrol ...)
158 * @param $action String A log type action
159 * @param $msg String (optional) A message for PHPUnit :-)
160 */
161 function assertIRCComment( $expected, $type, $action, $params, $msg = '' ) {
162
163 $logEntry = new ManualLogEntry( $type, $action );
164 $logEntry->setPerformer( $this->user );
165 $logEntry->setTarget ( $this->title );
166 $logEntry->setComment ( $this->user_comment );
167 $logEntry->setParameters( $params );
168
169 $formatter = LogFormatter::newFromEntry( $logEntry );
170 $formatter->setContext( RequestContext::newExtraneousContext( $this->title ) );
171
172 $this->assertEquals( $expected,
173 $formatter->getIRCActionComment( ),
174 $msg
175 );
176 }
177
178 }