Merge "Add SelfLinkBeginHook"
[lhc/web/wiklou.git] / tests / phpunit / includes / changes / RCCacheEntryFactoryTest.php
1 <?php
2
3 /**
4 * @covers RCCacheEntryFactory
5 *
6 * @group Database
7 *
8 * @licence GNU GPL v2+
9 * @author Katie Filbert < aude.wiki@gmail.com >
10 */
11 class RCCacheEntryFactoryTest extends MediaWikiLangTestCase {
12 protected function setUp() {
13 parent::setUp();
14
15 $this->setMwGlobals( array(
16 'wgArticlePath' => '/wiki/$1'
17 ) );
18 }
19
20 /**
21 * @dataProvider editChangeProvider
22 */
23 public function testNewFromRecentChange( $expected, $context, $messages,
24 $recentChange, $watched
25 ) {
26 $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
27 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
28
29 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
30
31 $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
32 $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
33 $this->assertEquals(
34 $expected['numberofWatchingusers'], $cacheEntry->numberofWatchingusers,
35 'watching users'
36 );
37 $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
38
39 $this->assertUserLinks( 'Mary', $cacheEntry );
40 $this->assertTitleLink( 'Xyz', $cacheEntry );
41
42 $this->assertQueryLink( 'cur', $expected['cur'], $cacheEntry->curlink, 'cur link' );
43 $this->assertQueryLink( 'prev', $expected['diff'], $cacheEntry->lastlink, 'prev link' );
44 $this->assertQueryLink( 'diff', $expected['diff'], $cacheEntry->difflink, 'diff link' );
45 }
46
47 public function editChangeProvider() {
48 return array(
49 array(
50 array(
51 'title' => 'Xyz',
52 'user' => 'Mary',
53 'diff' => array( 'curid' => 5, 'diff' => 191, 'oldid' => 190 ),
54 'cur' => array( 'curid' => 5, 'diff' => 0, 'oldid' => 191 ),
55 'timestamp' => '21:21',
56 'numberofWatchingusers' => 0,
57 'unpatrolled' => false
58 ),
59 $this->getContext(),
60 $this->getMessages(),
61 $this->makeEditRecentChange(
62 'Xyz',
63 $this->getTestUser(),
64 5, // curid
65 191, // thisid
66 190, // lastid
67 '20131103212153',
68 0, // counter
69 0 // number of watching users
70 ),
71 false,
72 'edit'
73 )
74 );
75 }
76
77 private function makeEditRecentChange( $title, $user, $curid, $thisid, $lastid,
78 $timestamp, $counter, $watchingUsers
79 ) {
80
81 $attribs = array_merge(
82 $this->getDefaultAttributes( $title, $timestamp ),
83 array(
84 'rc_user' => $user->getId(),
85 'rc_user_text' => $user->getName(),
86 'rc_this_oldid' => $thisid,
87 'rc_last_oldid' => $lastid,
88 'rc_cur_id' => $curid
89 )
90 );
91
92 return $this->makeRecentChange( $attribs, $counter, $watchingUsers );
93 }
94
95 /**
96 * @dataProvider deleteChangeProvider
97 */
98 public function testNewForDeleteChange( $expected, $context, $messages, $recentChange, $watched ) {
99 $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
100 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
101
102 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
103
104 $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
105 $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
106 $this->assertEquals(
107 $expected['numberofWatchingusers'],
108 $cacheEntry->numberofWatchingusers, 'watching users'
109 );
110 $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
111
112 $this->assertDeleteLogLink( $cacheEntry );
113 $this->assertUserLinks( 'Mary', $cacheEntry );
114
115 $this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
116 $this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
117 $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
118 }
119
120 public function deleteChangeProvider() {
121 return array(
122 array(
123 array(
124 'title' => 'Abc',
125 'user' => 'Mary',
126 'timestamp' => '21:21',
127 'numberofWatchingusers' => 0,
128 'unpatrolled' => false
129 ),
130 $this->getContext(),
131 $this->getMessages(),
132 $this->makeLogRecentChange(
133 'Abc',
134 $this->getTestUser(),
135 '20131103212153',
136 0, // counter
137 0 // number of watching users
138 ),
139 false,
140 'delete'
141 )
142 );
143 }
144
145 private function makeLogRecentChange( $title, $user, $timestamp, $counter, $watchingUsers ) {
146 $attribs = array_merge(
147 $this->getDefaultAttributes( $title, $timestamp ),
148 array(
149 'rc_cur_id' => 0,
150 'rc_user' => $user->getId(),
151 'rc_user_text' => $user->getName(),
152 'rc_this_oldid' => 0,
153 'rc_last_oldid' => 0,
154 'rc_old_len' => null,
155 'rc_new_len' => null,
156 'rc_type' => 3,
157 'rc_logid' => 25,
158 'rc_log_type' => 'delete',
159 'rc_log_action' => 'delete'
160 )
161 );
162
163 return $this->makeRecentChange( $attribs, $counter, $watchingUsers );
164 }
165
166 /**
167 * @dataProvider revUserDeleteProvider
168 */
169 public function testNewForRevUserDeleteChange( $expected, $context, $messages,
170 $recentChange, $watched
171 ) {
172 $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
173 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
174
175 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
176
177 $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
178 $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
179 $this->assertEquals(
180 $expected['numberofWatchingusers'],
181 $cacheEntry->numberofWatchingusers, 'watching users'
182 );
183 $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
184
185 $this->assertRevDel( $cacheEntry );
186 $this->assertTitleLink( 'Zzz', $cacheEntry );
187
188 $this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
189 $this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
190 $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
191 }
192
193 public function revUserDeleteProvider() {
194 return array(
195 array(
196 array(
197 'title' => 'Zzz',
198 'user' => 'Mary',
199 'diff' => '',
200 'cur' => '',
201 'timestamp' => '21:21',
202 'numberofWatchingusers' => 0,
203 'unpatrolled' => false
204 ),
205 $this->getContext(),
206 $this->getMessages(),
207 $this->makeDeletedEditRecentChange(
208 'Zzz',
209 $this->getTestUser(),
210 '20131103212153',
211 191, // thisid
212 190, // lastid
213 '20131103212153',
214 0, // counter
215 0 // number of watching users
216 ),
217 false,
218 'deletedrevuser'
219 )
220 );
221 }
222
223 private function makeDeletedEditRecentChange( $title, $user, $timestamp, $curid, $thisid,
224 $lastid, $counter, $watchingUsers
225 ) {
226 $attribs = array_merge(
227 $this->getDefaultAttributes( $title, $timestamp ),
228 array(
229 'rc_user' => $user->getId(),
230 'rc_user_text' => $user->getName(),
231 'rc_deleted' => 5,
232 'rc_cur_id' => $curid,
233 'rc_this_oldid' => $thisid,
234 'rc_last_oldid' => $lastid
235 )
236 );
237
238 return $this->makeRecentChange( $attribs, $counter, $watchingUsers );
239 }
240
241 private function assertUserLinks( $user, $cacheEntry ) {
242 $this->assertTag(
243 array(
244 'tag' => 'a',
245 'attributes' => array(
246 'class' => 'new mw-userlink'
247 ),
248 'content' => $user
249 ),
250 $cacheEntry->userlink,
251 'verify user link'
252 );
253
254 $this->assertTag(
255 array(
256 'tag' => 'span',
257 'attributes' => array(
258 'class' => 'mw-usertoollinks'
259 ),
260 'child' => array(
261 'tag' => 'a',
262 'content' => 'Talk',
263 )
264 ),
265 $cacheEntry->usertalklink,
266 'verify user talk link'
267 );
268
269 $this->assertTag(
270 array(
271 'tag' => 'span',
272 'attributes' => array(
273 'class' => 'mw-usertoollinks'
274 ),
275 'child' => array(
276 'tag' => 'a',
277 'content' => 'contribs',
278 )
279 ),
280 $cacheEntry->usertalklink,
281 'verify user tool links'
282 );
283 }
284
285 private function assertDeleteLogLink( $cacheEntry ) {
286 $this->assertTag(
287 array(
288 'tag' => 'a',
289 'attributes' => array(
290 'href' => '/wiki/Special:Log/delete',
291 'title' => 'Special:Log/delete'
292 ),
293 'content' => 'Deletion log'
294 ),
295 $cacheEntry->link,
296 'verify deletion log link'
297 );
298 }
299
300 private function assertRevDel( $cacheEntry ) {
301 $this->assertTag(
302 array(
303 'tag' => 'span',
304 'attributes' => array(
305 'class' => 'history-deleted'
306 ),
307 'content' => '(username removed)'
308 ),
309 $cacheEntry->userlink,
310 'verify user link for change with deleted revision and user'
311 );
312 }
313
314 private function assertTitleLink( $title, $cacheEntry ) {
315 $this->assertTag(
316 array(
317 'tag' => 'a',
318 'attributes' => array(
319 'href' => '/wiki/' . $title,
320 'title' => $title
321 ),
322 'content' => $title
323 ),
324 $cacheEntry->link,
325 'verify title link'
326 );
327 }
328
329 private function assertQueryLink( $content, $params, $link ) {
330 $this->assertTag(
331 array(
332 'tag' => 'a',
333 'content' => $content
334 ),
335 $link,
336 'assert query link element'
337 );
338
339 foreach ( $params as $key => $value ) {
340 $this->assertRegExp( '/' . $key . '=' . $value . '/', $link, "verify $key link params" );
341 }
342 }
343
344 private function makeRecentChange( $attribs, $counter, $watchingUsers ) {
345 $change = new RecentChange();
346 $change->setAttribs( $attribs );
347 $change->counter = $counter;
348 $change->numberofWatchingusers = $watchingUsers;
349
350 return $change;
351 }
352
353 private function getDefaultAttributes( $title, $timestamp ) {
354 return array(
355 'rc_id' => 545,
356 'rc_user' => 0,
357 'rc_user_text' => '127.0.0.1',
358 'rc_ip' => '127.0.0.1',
359 'rc_title' => $title,
360 'rc_namespace' => 0,
361 'rc_timestamp' => $timestamp,
362 'rc_old_len' => 212,
363 'rc_new_len' => 188,
364 'rc_comment' => '',
365 'rc_minor' => 0,
366 'rc_bot' => 0,
367 'rc_type' => 0,
368 'rc_patrolled' => 1,
369 'rc_deleted' => 0,
370 'rc_logid' => 0,
371 'rc_log_type' => null,
372 'rc_log_action' => '',
373 'rc_params' => '',
374 'rc_source' => 'mw.edit'
375 );
376 }
377
378 private function getTestUser() {
379 $user = User::newFromName( 'Mary' );
380
381 if ( !$user->getId() ) {
382 $user->addToDatabase();
383 }
384
385 return $user;
386 }
387
388 private function getMessages() {
389 return array(
390 'cur' => 'cur',
391 'diff' => 'diff',
392 'hist' => 'hist',
393 'enhancedrc-history' => 'history',
394 'last' => 'prev',
395 'blocklink' => 'block',
396 'history' => 'Page history',
397 'semicolon-separator' => '; ',
398 'pipe-separator' => ' | '
399 );
400 }
401
402 private function getContext() {
403 $title = Title::newFromText( 'RecentChanges', NS_SPECIAL );
404
405 $context = new RequestContext();
406 $context->setTitle( $title );
407 $context->setLanguage( Language::factory( 'en' ) );
408
409 $user = $this->getTestUser();
410 $context->setUser( $user );
411
412 return $context;
413 }
414 }