MediaWiki
REL1_21
|
00001 <?php 00015 class MagicVariableTest extends MediaWikiTestCase { 00017 private $testParser = null; 00018 00026 private $expectedAsInteger = array( 00027 'revisionday', 00028 'revisionmonth1', 00029 ); 00030 00032 protected function setUp() { 00033 parent::setUp(); 00034 00035 $contLang = Language::factory( 'en' ); 00036 $this->setMwGlobals( array( 00037 'wgLanguageCode' => 'en', 00038 'wgContLang' => $contLang, 00039 ) ); 00040 00041 $this->testParser = new Parser(); 00042 $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) ); 00043 00044 # initialize parser output 00045 $this->testParser->clearState(); 00046 00047 # Needs a title to do magic word stuff 00048 $title = Title::newFromText( 'Tests' ); 00049 $title->mRedirect = false; # Else it needs a db connection just to check if it's a redirect (when deciding the page language) 00050 00051 $this->testParser->setTitle( $title ); 00052 } 00053 00055 protected function tearDown() { 00056 unset( $this->testParser ); 00057 00058 parent::tearDown(); 00059 } 00060 00061 ############### TESTS ############################################# 00062 # @todo FIXME: 00063 # - those got copy pasted, we can probably make them cleaner 00064 # - tests are lacking useful messages 00065 00066 # day 00067 00069 function testCurrentdayIsUnPadded( $day ) { 00070 $this->assertUnPadded( 'currentday', $day ); 00071 } 00072 00074 function testCurrentdaytwoIsZeroPadded( $day ) { 00075 $this->assertZeroPadded( 'currentday2', $day ); 00076 } 00077 00079 function testLocaldayIsUnPadded( $day ) { 00080 $this->assertUnPadded( 'localday', $day ); 00081 } 00082 00084 function testLocaldaytwoIsZeroPadded( $day ) { 00085 $this->assertZeroPadded( 'localday2', $day ); 00086 } 00087 00088 # month 00089 00091 function testCurrentmonthIsZeroPadded( $month ) { 00092 $this->assertZeroPadded( 'currentmonth', $month ); 00093 } 00094 00096 function testCurrentmonthoneIsUnPadded( $month ) { 00097 $this->assertUnPadded( 'currentmonth1', $month ); 00098 } 00099 00101 function testLocalmonthIsZeroPadded( $month ) { 00102 $this->assertZeroPadded( 'localmonth', $month ); 00103 } 00104 00106 function testLocalmonthoneIsUnPadded( $month ) { 00107 $this->assertUnPadded( 'localmonth1', $month ); 00108 } 00109 00110 00111 # revision day 00112 00114 function testRevisiondayIsUnPadded( $day ) { 00115 $this->assertUnPadded( 'revisionday', $day ); 00116 } 00117 00119 function testRevisiondaytwoIsZeroPadded( $day ) { 00120 $this->assertZeroPadded( 'revisionday2', $day ); 00121 } 00122 00123 # revision month 00124 00126 function testRevisionmonthIsZeroPadded( $month ) { 00127 $this->assertZeroPadded( 'revisionmonth', $month ); 00128 } 00129 00131 function testRevisionmonthoneIsUnPadded( $month ) { 00132 $this->assertUnPadded( 'revisionmonth1', $month ); 00133 } 00134 00139 function testServernameFromDifferentProtocols() { 00140 global $wgServer; 00141 $saved_wgServer = $wgServer; 00142 00143 $wgServer = 'http://localhost/'; 00144 $this->assertMagic( 'localhost', 'servername' ); 00145 $wgServer = 'https://localhost/'; 00146 $this->assertMagic( 'localhost', 'servername' ); 00147 $wgServer = '//localhost/'; # bug 31176 00148 $this->assertMagic( 'localhost', 'servername' ); 00149 00150 $wgServer = $saved_wgServer; 00151 } 00152 00153 ############### HELPERS ############################################ 00154 00156 PUBLIC function assertZeroPadded( $magic, $value ) { 00157 $this->assertMagicPadding( $magic, $value, '%02d' ); 00158 } 00159 00161 PUBLIC function assertUnPadded( $magic, $value ) { 00162 $this->assertMagicPadding( $magic, $value, '%d' ); 00163 } 00164 00171 private function assertMagicPadding( $magic, $value, $format ) { 00172 # Initialize parser timestamp as year 2010 at 12h34 56s. 00173 # month and day are given by the caller ($value). Month < 12! 00174 if ( $value > 12 ) { 00175 $month = $value % 12; 00176 } else { 00177 $month = $value; 00178 } 00179 00180 $this->setParserTS( 00181 sprintf( '2010%02d%02d123456', $month, $value ) 00182 ); 00183 00184 # please keep the following commented line of code. It helps debugging. 00185 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n"; 00186 00187 # format expectation and test it 00188 $expected = sprintf( $format, $value ); 00189 $this->assertMagic( $expected, $magic ); 00190 } 00191 00193 private function setParserTS( $ts ) { 00194 $this->testParser->Options()->setTimestamp( $ts ); 00195 $this->testParser->mRevisionTimestamp = $ts; 00196 } 00197 00201 private function assertMagic( $expected, $magic ) { 00202 if ( in_array( $magic, $this->expectedAsInteger ) ) { 00203 $expected = (int)$expected; 00204 } 00205 00206 # Generate a message for the assertion 00207 $msg = sprintf( "Magic %s should be <%s:%s>", 00208 $magic, 00209 $expected, 00210 gettype( $expected ) 00211 ); 00212 00213 $this->assertSame( 00214 $expected, 00215 $this->testParser->getVariableValue( $magic ), 00216 $msg 00217 ); 00218 } 00219 }