MediaWiki  REL1_23
RCCacheEntryFactoryTest.php
Go to the documentation of this file.
00001 <?php
00002 
00011 class RCCacheEntryFactoryTest extends MediaWikiLangTestCase {
00012     protected function setUp() {
00013         parent::setUp();
00014 
00015         $this->setMwGlobals( array(
00016             'wgArticlePath' => '/wiki/$1'
00017         ) );
00018     }
00019 
00023     public function testNewFromRecentChange( $expected, $context, $messages,
00024         $recentChange, $watched
00025     ) {
00026         $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
00027         $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
00028 
00029         $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
00030 
00031         $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
00032         $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
00033         $this->assertEquals(
00034             $expected['numberofWatchingusers'], $cacheEntry->numberofWatchingusers,
00035             'watching users'
00036         );
00037         $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
00038 
00039         $this->assertUserLinks( 'Mary', $cacheEntry );
00040         $this->assertTitleLink( 'Xyz', $cacheEntry );
00041 
00042         $this->assertQueryLink( 'cur', $expected['cur'], $cacheEntry->curlink, 'cur link' );
00043         $this->assertQueryLink( 'prev', $expected['diff'], $cacheEntry->lastlink, 'prev link' );
00044         $this->assertQueryLink( 'diff', $expected['diff'], $cacheEntry->difflink, 'diff link' );
00045     }
00046 
00047     public function editChangeProvider() {
00048         return array(
00049             array(
00050                 array(
00051                     'title' => 'Xyz',
00052                     'user' => 'Mary',
00053                     'diff' => array( 'curid' => 5, 'diff' => 191, 'oldid' => 190 ),
00054                     'cur' => array( 'curid' => 5, 'diff' => 0, 'oldid' => 191 ),
00055                     'timestamp' => '21:21',
00056                     'numberofWatchingusers' => 0,
00057                     'unpatrolled' => false
00058                 ),
00059                 $this->getContext(),
00060                 $this->getMessages(),
00061                 $this->makeEditRecentChange(
00062                     'Xyz',
00063                     $this->getTestUser(),
00064                     5, // curid
00065                     191, // thisid
00066                     190, // lastid
00067                     '20131103212153',
00068                     0, // counter
00069                     0 // number of watching users
00070                 ),
00071                 false,
00072                 'edit'
00073             )
00074         );
00075     }
00076 
00077     private function makeEditRecentChange( $title, $user, $curid, $thisid, $lastid,
00078         $timestamp, $counter, $watchingUsers
00079     ) {
00080 
00081         $attribs = array_merge(
00082             $this->getDefaultAttributes( $title, $timestamp ),
00083             array(
00084                 'rc_user' => $user->getId(),
00085                 'rc_user_text' => $user->getName(),
00086                 'rc_this_oldid' => $thisid,
00087                 'rc_last_oldid' => $lastid,
00088                 'rc_cur_id' => $curid
00089             )
00090         );
00091 
00092         return $this->makeRecentChange( $attribs, $counter, $watchingUsers );
00093     }
00094 
00098     public function testNewForDeleteChange( $expected, $context, $messages, $recentChange, $watched ) {
00099         $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
00100         $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
00101 
00102         $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
00103 
00104         $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
00105         $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
00106         $this->assertEquals(
00107             $expected['numberofWatchingusers'],
00108             $cacheEntry->numberofWatchingusers, 'watching users'
00109         );
00110         $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
00111 
00112         $this->assertDeleteLogLink( $cacheEntry );
00113         $this->assertUserLinks( 'Mary', $cacheEntry );
00114 
00115         $this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
00116         $this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
00117         $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
00118     }
00119 
00120     public function deleteChangeProvider() {
00121         return array(
00122             array(
00123                 array(
00124                     'title' => 'Abc',
00125                     'user' => 'Mary',
00126                     'timestamp' => '21:21',
00127                     'numberofWatchingusers' => 0,
00128                     'unpatrolled' => false
00129                 ),
00130                 $this->getContext(),
00131                 $this->getMessages(),
00132                 $this->makeLogRecentChange(
00133                     'Abc',
00134                     $this->getTestUser(),
00135                     '20131103212153',
00136                     0, // counter
00137                     0 // number of watching users
00138                 ),
00139                 false,
00140                 'delete'
00141             )
00142         );
00143     }
00144 
00145     private function makeLogRecentChange( $title, $user, $timestamp, $counter, $watchingUsers ) {
00146         $attribs = array_merge(
00147             $this->getDefaultAttributes( $title, $timestamp ),
00148             array(
00149                 'rc_cur_id' => 0,
00150                 'rc_user' => $user->getId(),
00151                 'rc_user_text' => $user->getName(),
00152                 'rc_this_oldid' => 0,
00153                 'rc_last_oldid' => 0,
00154                 'rc_old_len' => null,
00155                 'rc_new_len' => null,
00156                 'rc_type' => 3,
00157                 'rc_logid' => 25,
00158                 'rc_log_type' => 'delete',
00159                 'rc_log_action' => 'delete'
00160             )
00161         );
00162 
00163         return $this->makeRecentChange( $attribs, $counter, $watchingUsers );
00164     }
00165 
00169     public function testNewForRevUserDeleteChange( $expected, $context, $messages,
00170         $recentChange, $watched
00171     ) {
00172         $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
00173         $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
00174 
00175         $this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
00176 
00177         $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
00178         $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
00179         $this->assertEquals(
00180             $expected['numberofWatchingusers'],
00181             $cacheEntry->numberofWatchingusers, 'watching users'
00182         );
00183         $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
00184 
00185         $this->assertRevDel( $cacheEntry );
00186         $this->assertTitleLink( 'Zzz', $cacheEntry );
00187 
00188         $this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
00189         $this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
00190         $this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
00191     }
00192 
00193     public function revUserDeleteProvider() {
00194         return array(
00195             array(
00196                 array(
00197                     'title' => 'Zzz',
00198                     'user' => 'Mary',
00199                     'diff' => '',
00200                     'cur' => '',
00201                     'timestamp' => '21:21',
00202                     'numberofWatchingusers' => 0,
00203                     'unpatrolled' => false
00204                 ),
00205                 $this->getContext(),
00206                 $this->getMessages(),
00207                 $this->makeDeletedEditRecentChange(
00208                     'Zzz',
00209                     $this->getTestUser(),
00210                     '20131103212153',
00211                     191, // thisid
00212                     190, // lastid
00213                     '20131103212153',
00214                     0, // counter
00215                     0 // number of watching users
00216                 ),
00217                 false,
00218                 'deletedrevuser'
00219             )
00220         );
00221     }
00222 
00223     private function makeDeletedEditRecentChange( $title, $user, $timestamp, $curid, $thisid,
00224         $lastid, $counter, $watchingUsers
00225     ) {
00226         $attribs = array_merge(
00227             $this->getDefaultAttributes( $title, $timestamp ),
00228             array(
00229                 'rc_user' => $user->getId(),
00230                 'rc_user_text' => $user->getName(),
00231                 'rc_deleted' => 5,
00232                 'rc_cur_id' => $curid,
00233                 'rc_this_oldid' => $thisid,
00234                 'rc_last_oldid' => $lastid
00235             )
00236         );
00237 
00238         return $this->makeRecentChange( $attribs, $counter, $watchingUsers );
00239     }
00240 
00241     private function assertUserLinks( $user, $cacheEntry ) {
00242         $this->assertTag(
00243             array(
00244                 'tag' => 'a',
00245                 'attributes' => array(
00246                     'class' => 'new mw-userlink'
00247                 ),
00248                 'content' => $user
00249             ),
00250             $cacheEntry->userlink,
00251             'verify user link'
00252         );
00253 
00254         $this->assertTag(
00255             array(
00256                 'tag' => 'span',
00257                 'attributes' => array(
00258                     'class' => 'mw-usertoollinks'
00259                 ),
00260                 'child' => array(
00261                     'tag' => 'a',
00262                     'content' => 'Talk',
00263                 )
00264             ),
00265             $cacheEntry->usertalklink,
00266             'verify user talk link'
00267         );
00268 
00269         $this->assertTag(
00270             array(
00271                 'tag' => 'span',
00272                 'attributes' => array(
00273                     'class' => 'mw-usertoollinks'
00274                 ),
00275                 'child' => array(
00276                     'tag' => 'a',
00277                     'content' => 'contribs',
00278                 )
00279             ),
00280             $cacheEntry->usertalklink,
00281             'verify user tool links'
00282         );
00283     }
00284 
00285     private function assertDeleteLogLink( $cacheEntry ) {
00286         $this->assertTag(
00287             array(
00288                 'tag' => 'a',
00289                 'attributes' => array(
00290                     'href' => '/wiki/Special:Log/delete',
00291                     'title' => 'Special:Log/delete'
00292                 ),
00293                 'content' => 'Deletion log'
00294             ),
00295             $cacheEntry->link,
00296             'verify deletion log link'
00297         );
00298     }
00299 
00300     private function assertRevDel( $cacheEntry ) {
00301         $this->assertTag(
00302             array(
00303                 'tag' => 'span',
00304                 'attributes' => array(
00305                     'class' => 'history-deleted'
00306                 ),
00307                 'content' => '(username removed)'
00308             ),
00309             $cacheEntry->userlink,
00310             'verify user link for change with deleted revision and user'
00311         );
00312     }
00313 
00314     private function assertTitleLink( $title, $cacheEntry ) {
00315         $this->assertTag(
00316             array(
00317                 'tag' => 'a',
00318                 'attributes' => array(
00319                     'href' => '/wiki/' . $title,
00320                     'title' => $title
00321                 ),
00322                 'content' => $title
00323             ),
00324             $cacheEntry->link,
00325             'verify title link'
00326         );
00327     }
00328 
00329     private function assertQueryLink( $content, $params, $link ) {
00330         $this->assertTag(
00331             array(
00332                 'tag' => 'a',
00333                 'content' => $content
00334             ),
00335             $link,
00336             'assert query link element'
00337         );
00338 
00339         foreach ( $params as $key => $value ) {
00340             $this->assertRegExp( '/' . $key . '=' . $value . '/', $link, "verify $key link params" );
00341         }
00342     }
00343 
00344     private function makeRecentChange( $attribs, $counter, $watchingUsers ) {
00345         $change = new RecentChange();
00346         $change->setAttribs( $attribs );
00347         $change->counter = $counter;
00348         $change->numberofWatchingusers = $watchingUsers;
00349 
00350         return $change;
00351     }
00352 
00353     private function getDefaultAttributes( $title, $timestamp ) {
00354         return array(
00355             'rc_id' => 545,
00356             'rc_user' => 0,
00357             'rc_user_text' => '127.0.0.1',
00358             'rc_ip' => '127.0.0.1',
00359             'rc_title' => $title,
00360             'rc_namespace' => 0,
00361             'rc_timestamp' => $timestamp,
00362             'rc_cur_time' => $timestamp,
00363             'rc_old_len' => 212,
00364             'rc_new_len' => 188,
00365             'rc_comment' => '',
00366             'rc_minor' => 0,
00367             'rc_bot' => 0,
00368             'rc_type' => 0,
00369             'rc_patrolled' => 1,
00370             'rc_deleted' => 0,
00371             'rc_logid' => 0,
00372             'rc_log_type' => null,
00373             'rc_log_action' => '',
00374             'rc_params' => '',
00375             'rc_source' => 'mw.edit'
00376         );
00377     }
00378 
00379     private function getTestUser() {
00380         $user = User::newFromName( 'Mary' );
00381 
00382         if ( !$user->getId() ) {
00383             $user->addToDatabase();
00384         }
00385 
00386         return $user;
00387     }
00388 
00389     private function getMessages() {
00390         return array(
00391             'cur' => 'cur',
00392             'diff' => 'diff',
00393             'hist' => 'hist',
00394             'enhancedrc-history' => 'history',
00395             'last' => 'prev',
00396             'blocklink' => 'block',
00397             'history' => 'Page history',
00398             'semicolon-separator' => '; ',
00399             'pipe-separator' => ' | '
00400         );
00401     }
00402 
00403     private function getContext() {
00404         $title = Title::newFromText( 'RecentChanges', NS_SPECIAL );
00405 
00406         $context = new RequestContext();
00407         $context->setTitle( $title );
00408         $context->setLanguage( Language::factory( 'en' ) );
00409 
00410         $user = $this->getTestUser();
00411         $context->setUser( $user );
00412 
00413         return $context;
00414     }
00415 }