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