MediaWiki  REL1_24
MagicVariableTest.php
Go to the documentation of this file.
00001 <?php
00015 class MagicVariableTest extends MediaWikiTestCase {
00019     private $testParser = null;
00020 
00028     private $expectedAsInteger = array(
00029         'revisionday',
00030         'revisionmonth1',
00031     );
00032 
00034     protected function setUp() {
00035         parent::setUp();
00036 
00037         $contLang = Language::factory( 'en' );
00038         $this->setMwGlobals( array(
00039             'wgLanguageCode' => 'en',
00040             'wgContLang' => $contLang,
00041         ) );
00042 
00043         $this->testParser = new Parser();
00044         $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) );
00045 
00046         # initialize parser output
00047         $this->testParser->clearState();
00048 
00049         # Needs a title to do magic word stuff
00050         $title = Title::newFromText( 'Tests' );
00051         # Else it needs a db connection just to check if it's a redirect
00052         # (when deciding the page language).
00053         $title->mRedirect = false;
00054 
00055         $this->testParser->setTitle( $title );
00056     }
00057 
00062     private static function createProviderUpTo( $num ) {
00063         $ret = array();
00064         for ( $i = 1; $i <= $num; $i++ ) {
00065             $ret[] = array( $i );
00066         }
00067 
00068         return $ret;
00069     }
00070 
00074     public static function provideMonths() {
00075         return self::createProviderUpTo( 12 );
00076     }
00077 
00081     public static function provideDays() {
00082         return self::createProviderUpTo( 31 );
00083     }
00084 
00085     ############### TESTS #############################################
00086     # @todo FIXME:
00087     #  - those got copy pasted, we can probably make them cleaner
00088     #  - tests are lacking useful messages
00089 
00090     # day
00091 
00093     public function testCurrentdayIsUnPadded( $day ) {
00094         $this->assertUnPadded( 'currentday', $day );
00095     }
00096 
00098     public function testCurrentdaytwoIsZeroPadded( $day ) {
00099         $this->assertZeroPadded( 'currentday2', $day );
00100     }
00101 
00103     public function testLocaldayIsUnPadded( $day ) {
00104         $this->assertUnPadded( 'localday', $day );
00105     }
00106 
00108     public function testLocaldaytwoIsZeroPadded( $day ) {
00109         $this->assertZeroPadded( 'localday2', $day );
00110     }
00111 
00112     # month
00113 
00115     public function testCurrentmonthIsZeroPadded( $month ) {
00116         $this->assertZeroPadded( 'currentmonth', $month );
00117     }
00118 
00120     public function testCurrentmonthoneIsUnPadded( $month ) {
00121         $this->assertUnPadded( 'currentmonth1', $month );
00122     }
00123 
00125     public function testLocalmonthIsZeroPadded( $month ) {
00126         $this->assertZeroPadded( 'localmonth', $month );
00127     }
00128 
00130     public function testLocalmonthoneIsUnPadded( $month ) {
00131         $this->assertUnPadded( 'localmonth1', $month );
00132     }
00133 
00134     # revision day
00135 
00137     public function testRevisiondayIsUnPadded( $day ) {
00138         $this->assertUnPadded( 'revisionday', $day );
00139     }
00140 
00142     public function testRevisiondaytwoIsZeroPadded( $day ) {
00143         $this->assertZeroPadded( 'revisionday2', $day );
00144     }
00145 
00146     # revision month
00147 
00149     public function testRevisionmonthIsZeroPadded( $month ) {
00150         $this->assertZeroPadded( 'revisionmonth', $month );
00151     }
00152 
00154     public function testRevisionmonthoneIsUnPadded( $month ) {
00155         $this->assertUnPadded( 'revisionmonth1', $month );
00156     }
00157 
00158     ############### HELPERS ############################################
00159 
00161     public function assertZeroPadded( $magic, $value ) {
00162         $this->assertMagicPadding( $magic, $value, '%02d' );
00163     }
00164 
00166     public function assertUnPadded( $magic, $value ) {
00167         $this->assertMagicPadding( $magic, $value, '%d' );
00168     }
00169 
00176     private function assertMagicPadding( $magic, $value, $format ) {
00177         # Initialize parser timestamp as year 2010 at 12h34 56s.
00178         # month and day are given by the caller ($value). Month < 12!
00179         if ( $value > 12 ) {
00180             $month = $value % 12;
00181         } else {
00182             $month = $value;
00183         }
00184 
00185         $this->setParserTS(
00186             sprintf( '2010%02d%02d123456', $month, $value )
00187         );
00188 
00189         # please keep the following commented line of code. It helps debugging.
00190         //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
00191 
00192         # format expectation and test it
00193         $expected = sprintf( $format, $value );
00194         $this->assertMagic( $expected, $magic );
00195     }
00196 
00201     private function setParserTS( $ts ) {
00202         $this->testParser->Options()->setTimestamp( $ts );
00203         $this->testParser->mRevisionTimestamp = $ts;
00204     }
00205 
00211     private function assertMagic( $expected, $magic ) {
00212         if ( in_array( $magic, $this->expectedAsInteger ) ) {
00213             $expected = (int)$expected;
00214         }
00215 
00216         # Generate a message for the assertion
00217         $msg = sprintf( "Magic %s should be <%s:%s>",
00218             $magic,
00219             $expected,
00220             gettype( $expected )
00221         );
00222 
00223         $this->assertSame(
00224             $expected,
00225             $this->testParser->getVariableValue( $magic ),
00226             $msg
00227         );
00228     }
00229 }