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