Merge "mw.widgets.CategorySelector: Prevent duplicates"
[lhc/web/wiklou.git] / tests / phpunit / includes / changes / RCCacheEntryFactoryTest.php
1 <?php
2
3 /**
4 * @covers RCCacheEntryFactory
5 *
6 * @group Database
7 *
8 * @author Katie Filbert < aude.wiki@gmail.com >
9 */
10 class RCCacheEntryFactoryTest extends MediaWikiLangTestCase {
11
12 /**
13 * @var TestRecentChangesHelper
14 */
15 private $testRecentChangesHelper;
16
17 public function __construct( $name = null, array $data = array(), $dataName = '' ) {
18 parent::__construct( $name, $data, $dataName );
19
20 $this->testRecentChangesHelper = new TestRecentChangesHelper();
21 }
22
23 protected function setUp() {
24 parent::setUp();
25
26 $this->setMwGlobals( array(
27 'wgArticlePath' => '/wiki/$1'
28 ) );
29 }
30
31 /**
32 * @dataProvider editChangeProvider
33 */
34 public function testNewFromRecentChange( $expected, $context, $messages,
35 $recentChange, $watched
36 ) {
37 $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
38 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
39
40 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
41
42 $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
43 $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
44 $this->assertEquals(
45 $expected['numberofWatchingusers'], $cacheEntry->numberofWatchingusers,
46 'watching users'
47 );
48 $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
49
50 $this->assertUserLinks( 'TestRecentChangesUser', $cacheEntry );
51 $this->assertTitleLink( 'Xyz', $cacheEntry );
52
53 $this->assertQueryLink( 'cur', $expected['cur'], $cacheEntry->curlink, 'cur link' );
54 $this->assertQueryLink( 'prev', $expected['diff'], $cacheEntry->lastlink, 'prev link' );
55 $this->assertQueryLink( 'diff', $expected['diff'], $cacheEntry->difflink, 'diff link' );
56 }
57
58 public function editChangeProvider() {
59 return array(
60 array(
61 array(
62 'title' => 'Xyz',
63 'user' => 'TestRecentChangesUser',
64 'diff' => array( 'curid' => 5, 'diff' => 191, 'oldid' => 190 ),
65 'cur' => array( 'curid' => 5, 'diff' => 0, 'oldid' => 191 ),
66 'timestamp' => '21:21',
67 'numberofWatchingusers' => 0,
68 'unpatrolled' => false
69 ),
70 $this->getContext(),
71 $this->getMessages(),
72 $this->testRecentChangesHelper->makeEditRecentChange(
73 $this->getTestUser(),
74 'Xyz',
75 5, // curid
76 191, // thisid
77 190, // lastid
78 '20131103212153',
79 0, // counter
80 0 // number of watching users
81 ),
82 false
83 )
84 );
85 }
86
87 /**
88 * @dataProvider deleteChangeProvider
89 */
90 public function testNewForDeleteChange( $expected, $context, $messages, $recentChange, $watched ) {
91 $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
92 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
93
94 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
95
96 $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
97 $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
98 $this->assertEquals(
99 $expected['numberofWatchingusers'],
100 $cacheEntry->numberofWatchingusers, 'watching users'
101 );
102 $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
103
104 $this->assertDeleteLogLink( $cacheEntry );
105 $this->assertUserLinks( 'TestRecentChangesUser', $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' => 'TestRecentChangesUser',
118 'timestamp' => '21:21',
119 'numberofWatchingusers' => 0,
120 'unpatrolled' => false
121 ),
122 $this->getContext(),
123 $this->getMessages(),
124 $this->testRecentChangesHelper->makeLogRecentChange(
125 'delete',
126 'delete',
127 $this->getTestUser(),
128 'Abc',
129 '20131103212153',
130 0, // counter
131 0 // number of watching users
132 ),
133 false
134 )
135 );
136 }
137
138 /**
139 * @dataProvider revUserDeleteProvider
140 */
141 public function testNewForRevUserDeleteChange( $expected, $context, $messages,
142 $recentChange, $watched
143 ) {
144 $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
145 $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
146
147 $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
148
149 $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
150 $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
151 $this->assertEquals(
152 $expected['numberofWatchingusers'],
153 $cacheEntry->numberofWatchingusers, 'watching users'
154 );
155 $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
156
157 $this->assertRevDel( $cacheEntry );
158 $this->assertTitleLink( 'Zzz', $cacheEntry );
159
160 $this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
161 $this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
162 $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
163 }
164
165 public function revUserDeleteProvider() {
166 return array(
167 array(
168 array(
169 'title' => 'Zzz',
170 'user' => 'TestRecentChangesUser',
171 'diff' => '',
172 'cur' => '',
173 'timestamp' => '21:21',
174 'numberofWatchingusers' => 0,
175 'unpatrolled' => false
176 ),
177 $this->getContext(),
178 $this->getMessages(),
179 $this->testRecentChangesHelper->makeDeletedEditRecentChange(
180 $this->getTestUser(),
181 'Zzz',
182 '20131103212153',
183 191, // thisid
184 190, // lastid
185 '20131103212153',
186 0, // counter
187 0 // number of watching users
188 ),
189 false
190 )
191 );
192 }
193
194 private function assertUserLinks( $user, $cacheEntry ) {
195 $this->assertTag(
196 array(
197 'tag' => 'a',
198 'attributes' => array(
199 'class' => 'new mw-userlink'
200 ),
201 'content' => $user
202 ),
203 $cacheEntry->userlink,
204 'verify user link'
205 );
206
207 $this->assertTag(
208 array(
209 'tag' => 'span',
210 'attributes' => array(
211 'class' => 'mw-usertoollinks'
212 ),
213 'child' => array(
214 'tag' => 'a',
215 'content' => 'talk',
216 )
217 ),
218 $cacheEntry->usertalklink,
219 'verify user talk link'
220 );
221
222 $this->assertTag(
223 array(
224 'tag' => 'span',
225 'attributes' => array(
226 'class' => 'mw-usertoollinks'
227 ),
228 'child' => array(
229 'tag' => 'a',
230 'content' => 'contribs',
231 )
232 ),
233 $cacheEntry->usertalklink,
234 'verify user tool links'
235 );
236 }
237
238 private function assertDeleteLogLink( $cacheEntry ) {
239 $this->assertTag(
240 array(
241 'tag' => 'a',
242 'attributes' => array(
243 'href' => '/wiki/Special:Log/delete',
244 'title' => 'Special:Log/delete'
245 ),
246 'content' => 'Deletion log'
247 ),
248 $cacheEntry->link,
249 'verify deletion log link'
250 );
251 }
252
253 private function assertRevDel( $cacheEntry ) {
254 $this->assertTag(
255 array(
256 'tag' => 'span',
257 'attributes' => array(
258 'class' => 'history-deleted'
259 ),
260 'content' => '(username removed)'
261 ),
262 $cacheEntry->userlink,
263 'verify user link for change with deleted revision and user'
264 );
265 }
266
267 private function assertTitleLink( $title, $cacheEntry ) {
268 $this->assertTag(
269 array(
270 'tag' => 'a',
271 'attributes' => array(
272 'href' => '/wiki/' . $title,
273 'title' => $title
274 ),
275 'content' => $title
276 ),
277 $cacheEntry->link,
278 'verify title link'
279 );
280 }
281
282 private function assertQueryLink( $content, $params, $link ) {
283 $this->assertTag(
284 array(
285 'tag' => 'a',
286 'content' => $content
287 ),
288 $link,
289 'assert query link element'
290 );
291
292 foreach ( $params as $key => $value ) {
293 $this->assertRegExp( '/' . $key . '=' . $value . '/', $link, "verify $key link params" );
294 }
295 }
296
297 private function getMessages() {
298 return array(
299 'cur' => 'cur',
300 'diff' => 'diff',
301 'hist' => 'hist',
302 'enhancedrc-history' => 'history',
303 'last' => 'prev',
304 'blocklink' => 'block',
305 'history' => 'Page history',
306 'semicolon-separator' => '; ',
307 'pipe-separator' => ' | '
308 );
309 }
310
311 private function getTestUser() {
312 $user = User::newFromName( 'TestRecentChangesUser' );
313
314 if ( !$user->getId() ) {
315 $user->addToDatabase();
316 }
317
318 return $user;
319 }
320
321 private function getContext() {
322 $user = $this->getTestUser();
323 $context = $this->testRecentChangesHelper->getTestContext( $user );
324
325 $title = Title::newFromText( 'RecentChanges', NS_SPECIAL );
326 $context->setTitle( $title );
327
328 return $context;
329 }
330 }