MediaWiki  REL1_22
LogFormatterTest.php
Go to the documentation of this file.
00001 <?php
00005 class LogFormatterTest extends MediaWikiLangTestCase {
00006 
00010     protected $user;
00011 
00015     protected $title;
00016 
00020     protected $context;
00021 
00022     protected function setUp() {
00023         parent::setUp();
00024 
00025         global $wgLang;
00026 
00027         $this->setMwGlobals( array(
00028             'wgLogTypes' => array( 'phpunit' ),
00029             'wgLogActionsHandlers' => array( 'phpunit/test' => 'LogFormatter',
00030                 'phpunit/param' => 'LogFormatter' ),
00031             'wgUser' => User::newFromName( 'Testuser' ),
00032             'wgExtensionMessagesFiles' => array( 'LogTests' => __DIR__ . '/LogTests.i18n.php' ),
00033         ) );
00034 
00035         $wgLang->getLocalisationCache()->recache( $wgLang->getCode() );
00036 
00037         $this->user = User::newFromName( 'Testuser' );
00038         $this->title = Title::newMainPage();
00039 
00040         $this->context = new RequestContext();
00041         $this->context->setUser( $this->user );
00042         $this->context->setTitle( $this->title );
00043         $this->context->setLanguage( $wgLang );
00044     }
00045 
00046     protected function tearDown() {
00047         parent::tearDown();
00048 
00049         global $wgLang;
00050         $wgLang->getLocalisationCache()->recache( $wgLang->getCode() );
00051     }
00052 
00053     public function newLogEntry( $action, $params ) {
00054         $logEntry = new ManualLogEntry( 'phpunit', $action );
00055         $logEntry->setPerformer( $this->user );
00056         $logEntry->setTarget( $this->title );
00057         $logEntry->setComment( 'A very good reason' );
00058 
00059         $logEntry->setParameters( $params );
00060 
00061         return $logEntry;
00062     }
00063 
00064     public function testNormalLogParams() {
00065         $entry = $this->newLogEntry( 'test', array() );
00066         $formatter = LogFormatter::newFromEntry( $entry );
00067         $formatter->setContext( $this->context );
00068 
00069         $formatter->setShowUserToolLinks( false );
00070         $paramsWithoutTools = $formatter->getMessageParametersForTesting();
00071         unset( $formatter->parsedParameters );
00072 
00073         $formatter->setShowUserToolLinks( true );
00074         $paramsWithTools = $formatter->getMessageParametersForTesting();
00075 
00076         $userLink = Linker::userLink(
00077             $this->user->getId(),
00078             $this->user->getName()
00079         );
00080 
00081         $userTools = Linker::userToolLinksRedContribs(
00082             $this->user->getId(),
00083             $this->user->getName(),
00084             $this->user->getEditCount()
00085         );
00086 
00087         $titleLink = Linker::link( $this->title, null, array(), array() );
00088 
00089         // $paramsWithoutTools and $paramsWithTools should be only different
00090         // in index 0
00091         $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
00092         $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
00093 
00094         $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
00095         $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
00096 
00097         $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
00098 
00099         $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
00100     }
00101 
00102     public function testLogParamsTypeRaw() {
00103         $params = array( '4:raw:raw' => Linker::link( $this->title, null, array(), array() ) );
00104         $expected = Linker::link( $this->title, null, array(), array() );
00105 
00106         $entry = $this->newLogEntry( 'param', $params );
00107         $formatter = LogFormatter::newFromEntry( $entry );
00108         $formatter->setContext( $this->context );
00109 
00110         $logParam = $formatter->getActionText();
00111 
00112         $this->assertEquals( $expected, $logParam );
00113     }
00114 
00115     public function testLogParamsTypeMsg() {
00116         $params = array( '4:msg:msg' => 'log-description-phpunit' );
00117         $expected = wfMessage( 'log-description-phpunit' )->text();
00118 
00119         $entry = $this->newLogEntry( 'param', $params );
00120         $formatter = LogFormatter::newFromEntry( $entry );
00121         $formatter->setContext( $this->context );
00122 
00123         $logParam = $formatter->getActionText();
00124 
00125         $this->assertEquals( $expected, $logParam );
00126     }
00127 
00128     public function testLogParamsTypeMsgContent() {
00129         $params = array( '4:msg-content:msgContent' => 'log-description-phpunit' );
00130         $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
00131 
00132         $entry = $this->newLogEntry( 'param', $params );
00133         $formatter = LogFormatter::newFromEntry( $entry );
00134         $formatter->setContext( $this->context );
00135 
00136         $logParam = $formatter->getActionText();
00137 
00138         $this->assertEquals( $expected, $logParam );
00139     }
00140 
00141     public function testLogParamsTypeNumber() {
00142         global $wgLang;
00143 
00144         $params = array( '4:number:number' => 123456789 );
00145         $expected = $wgLang->formatNum( 123456789 );
00146 
00147         $entry = $this->newLogEntry( 'param', $params );
00148         $formatter = LogFormatter::newFromEntry( $entry );
00149         $formatter->setContext( $this->context );
00150 
00151         $logParam = $formatter->getActionText();
00152 
00153         $this->assertEquals( $expected, $logParam );
00154     }
00155 
00156     public function testLogParamsTypeUserLink() {
00157         $params = array( '4:user-link:userLink' => $this->user->getName() );
00158         $expected = Linker::userLink(
00159             $this->user->getId(),
00160             $this->user->getName()
00161         );
00162 
00163         $entry = $this->newLogEntry( 'param', $params );
00164         $formatter = LogFormatter::newFromEntry( $entry );
00165         $formatter->setContext( $this->context );
00166 
00167         $logParam = $formatter->getActionText();
00168 
00169         $this->assertEquals( $expected, $logParam );
00170     }
00171 
00172     public function testLogParamsTypeTitleLink() {
00173         $params = array( '4:title-link:titleLink' => $this->title->getText() );
00174         $expected = Linker::link( $this->title, null, array(), array() );
00175 
00176         $entry = $this->newLogEntry( 'param', $params );
00177         $formatter = LogFormatter::newFromEntry( $entry );
00178         $formatter->setContext( $this->context );
00179 
00180         $logParam = $formatter->getActionText();
00181 
00182         $this->assertEquals( $expected, $logParam );
00183     }
00184 
00185     public function testLogParamsTypePlain() {
00186         $params = array( '4:plain:plain' => 'Some plain text' );
00187         $expected = 'Some plain text';
00188 
00189         $entry = $this->newLogEntry( 'param', $params );
00190         $formatter = LogFormatter::newFromEntry( $entry );
00191         $formatter->setContext( $this->context );
00192 
00193         $logParam = $formatter->getActionText();
00194 
00195         $this->assertEquals( $expected, $logParam );
00196     }
00197 
00198     public function testLogComment() {
00199         $entry = $this->newLogEntry( 'test', array() );
00200         $formatter = LogFormatter::newFromEntry( $entry );
00201         $formatter->setContext( $this->context );
00202 
00203         $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
00204 
00205         $this->assertEquals( $comment, $formatter->getComment() );
00206     }
00207 }