MediaWiki
master
|
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( 'wgContLang', $contLang ); 00037 00038 $this->testParser = new Parser(); 00039 $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) ); 00040 00041 # initialize parser output 00042 $this->testParser->clearState(); 00043 00044 # Needs a title to do magic word stuff 00045 $title = Title::newFromText( 'Tests' ); 00046 $title->mRedirect = false; # Else it needs a db connection just to check if it's a redirect (when deciding the page language) 00047 00048 $this->testParser->setTitle( $title ); 00049 } 00050 00052 protected function tearDown() { 00053 unset( $this->testParser ); 00054 00055 parent::tearDown(); 00056 } 00057 00058 ############### TESTS ############################################# 00059 # @todo FIXME: 00060 # - those got copy pasted, we can probably make them cleaner 00061 # - tests are lacking useful messages 00062 00063 # day 00064 00066 function testCurrentdayIsUnPadded( $day ) { 00067 $this->assertUnPadded( 'currentday', $day ); 00068 } 00070 function testCurrentdaytwoIsZeroPadded( $day ) { 00071 $this->assertZeroPadded( 'currentday2', $day ); 00072 } 00074 function testLocaldayIsUnPadded( $day ) { 00075 $this->assertUnPadded( 'localday', $day ); 00076 } 00078 function testLocaldaytwoIsZeroPadded( $day ) { 00079 $this->assertZeroPadded( 'localday2', $day ); 00080 } 00081 00082 # month 00083 00085 function testCurrentmonthIsZeroPadded( $month ) { 00086 $this->assertZeroPadded( 'currentmonth', $month ); 00087 } 00089 function testCurrentmonthoneIsUnPadded( $month ) { 00090 $this->assertUnPadded( 'currentmonth1', $month ); 00091 } 00093 function testLocalmonthIsZeroPadded( $month ) { 00094 $this->assertZeroPadded( 'localmonth', $month ); 00095 } 00097 function testLocalmonthoneIsUnPadded( $month ) { 00098 $this->assertUnPadded( 'localmonth1', $month ); 00099 } 00100 00101 00102 # revision day 00103 00105 function testRevisiondayIsUnPadded( $day ) { 00106 $this->assertUnPadded( 'revisionday', $day ); 00107 } 00109 function testRevisiondaytwoIsZeroPadded( $day ) { 00110 $this->assertZeroPadded( 'revisionday2', $day ); 00111 } 00112 00113 # revision month 00114 00116 function testRevisionmonthIsZeroPadded( $month ) { 00117 $this->assertZeroPadded( 'revisionmonth', $month ); 00118 } 00120 function testRevisionmonthoneIsUnPadded( $month ) { 00121 $this->assertUnPadded( 'revisionmonth1', $month ); 00122 } 00123 00128 function testServernameFromDifferentProtocols() { 00129 global $wgServer; 00130 $saved_wgServer= $wgServer; 00131 00132 $wgServer = 'http://localhost/'; 00133 $this->assertMagic( 'localhost', 'servername' ); 00134 $wgServer = 'https://localhost/'; 00135 $this->assertMagic( 'localhost', 'servername' ); 00136 $wgServer = '//localhost/'; # bug 31176 00137 $this->assertMagic( 'localhost', 'servername' ); 00138 00139 $wgServer = $saved_wgServer; 00140 } 00141 00142 ############### HELPERS ############################################ 00143 00145 PUBLIC function assertZeroPadded( $magic, $value ) { 00146 $this->assertMagicPadding( $magic, $value, '%02d' ); 00147 } 00148 00150 PUBLIC function assertUnPadded( $magic, $value ) { 00151 $this->assertMagicPadding( $magic, $value, '%d' ); 00152 } 00153 00160 private function assertMagicPadding( $magic, $value, $format ) { 00161 # Initialize parser timestamp as year 2010 at 12h34 56s. 00162 # month and day are given by the caller ($value). Month < 12! 00163 if( $value > 12 ) { $month = $value % 12; } 00164 else { $month = $value; } 00165 00166 $this->setParserTS( 00167 sprintf( '2010%02d%02d123456', $month, $value ) 00168 ); 00169 00170 # please keep the following commented line of code. It helps debugging. 00171 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n"; 00172 00173 # format expectation and test it 00174 $expected = sprintf( $format, $value ); 00175 $this->assertMagic( $expected, $magic ); 00176 } 00177 00179 private function setParserTS( $ts ) { 00180 $this->testParser->Options()->setTimestamp( $ts ); 00181 $this->testParser->mRevisionTimestamp = $ts; 00182 } 00183 00187 private function assertMagic( $expected, $magic ) { 00188 if( in_array( $magic, $this->expectedAsInteger ) ) { 00189 $expected = (int) $expected; 00190 } 00191 00192 # Generate a message for the assertion 00193 $msg = sprintf( "Magic %s should be <%s:%s>", 00194 $magic, 00195 $expected, 00196 gettype( $expected ) 00197 ); 00198 00199 $this->assertSame( 00200 $expected, 00201 $this->testParser->getVariableValue( $magic ), 00202 $msg 00203 ); 00204 } 00205 }