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