MediaWiki  REL1_22
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         $title->mRedirect = false; # Else it needs a db connection just to check if it's a redirect (when deciding the page language)
00052 
00053         $this->testParser->setTitle( $title );
00054     }
00055 
00060     private static function createProviderUpTo( $num ) {
00061         $ret = array();
00062         for ( $i = 1; $i <= $num; $i++ ) {
00063             $ret[] = array( $i );
00064         }
00065 
00066         return $ret;
00067     }
00068 
00072     public static function provideMonths() {
00073         return self::createProviderUpTo( 12 );
00074     }
00075 
00079     public static function provideDays() {
00080         return self::createProviderUpTo( 31 );
00081     }
00082 
00083     ############### TESTS #############################################
00084     # @todo FIXME:
00085     #  - those got copy pasted, we can probably make them cleaner
00086     #  - tests are lacking useful messages
00087 
00088     # day
00089 
00091     public function testCurrentdayIsUnPadded( $day ) {
00092         $this->assertUnPadded( 'currentday', $day );
00093     }
00094 
00096     public function testCurrentdaytwoIsZeroPadded( $day ) {
00097         $this->assertZeroPadded( 'currentday2', $day );
00098     }
00099 
00101     public function testLocaldayIsUnPadded( $day ) {
00102         $this->assertUnPadded( 'localday', $day );
00103     }
00104 
00106     public function testLocaldaytwoIsZeroPadded( $day ) {
00107         $this->assertZeroPadded( 'localday2', $day );
00108     }
00109 
00110     # month
00111 
00113     public function testCurrentmonthIsZeroPadded( $month ) {
00114         $this->assertZeroPadded( 'currentmonth', $month );
00115     }
00116 
00118     public function testCurrentmonthoneIsUnPadded( $month ) {
00119         $this->assertUnPadded( 'currentmonth1', $month );
00120     }
00121 
00123     public function testLocalmonthIsZeroPadded( $month ) {
00124         $this->assertZeroPadded( 'localmonth', $month );
00125     }
00126 
00128     public function testLocalmonthoneIsUnPadded( $month ) {
00129         $this->assertUnPadded( 'localmonth1', $month );
00130     }
00131 
00132     # revision day
00133 
00135     public function testRevisiondayIsUnPadded( $day ) {
00136         $this->assertUnPadded( 'revisionday', $day );
00137     }
00138 
00140     public function testRevisiondaytwoIsZeroPadded( $day ) {
00141         $this->assertZeroPadded( 'revisionday2', $day );
00142     }
00143 
00144     # revision month
00145 
00147     public function testRevisionmonthIsZeroPadded( $month ) {
00148         $this->assertZeroPadded( 'revisionmonth', $month );
00149     }
00150 
00152     public function testRevisionmonthoneIsUnPadded( $month ) {
00153         $this->assertUnPadded( 'revisionmonth1', $month );
00154     }
00155 
00162     public function testServernameFromDifferentProtocols( $server ) {
00163         $this->setMwGlobals( 'wgServer', $server );
00164 
00165         $this->assertMagic( 'localhost', 'servername' );
00166     }
00167 
00168     public static function provideDataServernameFromDifferentProtocols() {
00169         return array(
00170             array( 'http://localhost/' ),
00171             array( 'https://localhost/' ),
00172             array( '//localhost/' ), # bug 31176
00173         );
00174     }
00175 
00176     ############### HELPERS ############################################
00177 
00179     public function assertZeroPadded( $magic, $value ) {
00180         $this->assertMagicPadding( $magic, $value, '%02d' );
00181     }
00182 
00184     public function assertUnPadded( $magic, $value ) {
00185         $this->assertMagicPadding( $magic, $value, '%d' );
00186     }
00187 
00194     private function assertMagicPadding( $magic, $value, $format ) {
00195         # Initialize parser timestamp as year 2010 at 12h34 56s.
00196         # month and day are given by the caller ($value). Month < 12!
00197         if ( $value > 12 ) {
00198             $month = $value % 12;
00199         } else {
00200             $month = $value;
00201         }
00202 
00203         $this->setParserTS(
00204             sprintf( '2010%02d%02d123456', $month, $value )
00205         );
00206 
00207         # please keep the following commented line of code. It helps debugging.
00208         //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
00209 
00210         # format expectation and test it
00211         $expected = sprintf( $format, $value );
00212         $this->assertMagic( $expected, $magic );
00213     }
00214 
00216     private function setParserTS( $ts ) {
00217         $this->testParser->Options()->setTimestamp( $ts );
00218         $this->testParser->mRevisionTimestamp = $ts;
00219     }
00220 
00224     private function assertMagic( $expected, $magic ) {
00225         if ( in_array( $magic, $this->expectedAsInteger ) ) {
00226             $expected = (int)$expected;
00227         }
00228 
00229         # Generate a message for the assertion
00230         $msg = sprintf( "Magic %s should be <%s:%s>",
00231             $magic,
00232             $expected,
00233             gettype( $expected )
00234         );
00235 
00236         $this->assertSame(
00237             $expected,
00238             $this->testParser->getVariableValue( $magic ),
00239             $msg
00240         );
00241     }
00242 }