00001 <?php
00002
00003 class GlobalTest extends PHPUnit_Framework_TestCase {
00004 function setUp() {
00005 global $wgReadOnlyFile;
00006 $this->originals['wgReadOnlyFile'] = $wgReadOnlyFile;
00007 $wgReadOnlyFile = tempnam(wfTempDir(), "mwtest_readonly");
00008 unlink( $wgReadOnlyFile );
00009 }
00010
00011 function tearDown() {
00012 global $wgReadOnlyFile;
00013 if( file_exists( $wgReadOnlyFile ) ) {
00014 unlink( $wgReadOnlyFile );
00015 }
00016 $wgReadOnlyFile = $this->originals['wgReadOnlyFile'];
00017 }
00018
00019 function testRandom() {
00020 # This could hypothetically fail, but it shouldn't ;)
00021 $this->assertFalse(
00022 wfRandom() == wfRandom() );
00023 }
00024
00025 function testUrlencode() {
00026 $this->assertEquals(
00027 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
00028 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
00029 }
00030
00031 function testReadOnlyEmpty() {
00032 global $wgReadOnly;
00033 $wgReadOnly = null;
00034
00035 $this->assertFalse( wfReadOnly() );
00036 $this->assertFalse( wfReadOnly() );
00037 }
00038
00039 function testReadOnlySet() {
00040 global $wgReadOnly, $wgReadOnlyFile;
00041
00042 $f = fopen( $wgReadOnlyFile, "wt" );
00043 fwrite( $f, 'Message' );
00044 fclose( $f );
00045 $wgReadOnly = null;
00046
00047 $this->assertTrue( wfReadOnly() );
00048 $this->assertTrue( wfReadOnly() );
00049
00050 unlink( $wgReadOnlyFile );
00051 $wgReadOnly = null;
00052
00053 $this->assertFalse( wfReadOnly() );
00054 $this->assertFalse( wfReadOnly() );
00055 }
00056
00057 function testQuotedPrintable() {
00058 $this->assertEquals(
00059 "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
00060 wfQuotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
00061 }
00062
00063 function testTime() {
00064 $start = wfTime();
00065 $this->assertType( 'float', $start );
00066 $end = wfTime();
00067 $this->assertTrue( $end > $start, "Time is running backwards!" );
00068 }
00069
00070 function testArrayToCGI() {
00071 $this->assertEquals(
00072 "baz=AT%26T&foo=bar",
00073 wfArrayToCGI(
00074 array( 'baz' => 'AT&T', 'ignore' => '' ),
00075 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
00076 }
00077
00078 function testMimeTypeMatch() {
00079 $this->assertEquals(
00080 'text/html',
00081 mimeTypeMatch( 'text/html',
00082 array( 'application/xhtml+xml' => 1.0,
00083 'text/html' => 0.7,
00084 'text/plain' => 0.3 ) ) );
00085 $this->assertEquals(
00086 'text/*',
00087 mimeTypeMatch( 'text/html',
00088 array( 'image/*' => 1.0,
00089 'text/*' => 0.5 ) ) );
00090 $this->assertEquals(
00091 '*/*',
00092 mimeTypeMatch( 'text/html',
00093 array( '*/*' => 1.0 ) ) );
00094 $this->assertNull(
00095 mimeTypeMatch( 'text/html',
00096 array( 'image/png' => 1.0,
00097 'image/svg+xml' => 0.5 ) ) );
00098 }
00099
00100 function testNegotiateType() {
00101 $this->assertEquals(
00102 'text/html',
00103 wfNegotiateType(
00104 array( 'application/xhtml+xml' => 1.0,
00105 'text/html' => 0.7,
00106 'text/plain' => 0.5,
00107 'text/*' => 0.2 ),
00108 array( 'text/html' => 1.0 ) ) );
00109 $this->assertEquals(
00110 'application/xhtml+xml',
00111 wfNegotiateType(
00112 array( 'application/xhtml+xml' => 1.0,
00113 'text/html' => 0.7,
00114 'text/plain' => 0.5,
00115 'text/*' => 0.2 ),
00116 array( 'application/xhtml+xml' => 1.0,
00117 'text/html' => 0.5 ) ) );
00118 $this->assertEquals(
00119 'text/html',
00120 wfNegotiateType(
00121 array( 'text/html' => 1.0,
00122 'text/plain' => 0.5,
00123 'text/*' => 0.5,
00124 'application/xhtml+xml' => 0.2 ),
00125 array( 'application/xhtml+xml' => 1.0,
00126 'text/html' => 0.5 ) ) );
00127 $this->assertEquals(
00128 'text/html',
00129 wfNegotiateType(
00130 array( 'text/*' => 1.0,
00131 'image/*' => 0.7,
00132 '*/*' => 0.3 ),
00133 array( 'application/xhtml+xml' => 1.0,
00134 'text/html' => 0.5 ) ) );
00135 $this->assertNull(
00136 wfNegotiateType(
00137 array( 'text/*' => 1.0 ),
00138 array( 'application/xhtml+xml' => 1.0 ) ) );
00139 }
00140
00141 function testTimestamp() {
00142 $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
00143 $this->assertEquals(
00144 '20010115123456',
00145 wfTimestamp( TS_MW, $t ),
00146 'TS_UNIX to TS_MW' );
00147 $this->assertEquals(
00148 979562096,
00149 wfTimestamp( TS_UNIX, $t ),
00150 'TS_UNIX to TS_UNIX' );
00151 $this->assertEquals(
00152 '2001-01-15 12:34:56',
00153 wfTimestamp( TS_DB, $t ),
00154 'TS_UNIX to TS_DB' );
00155
00156 $this->assertEquals(
00157 '20010115123456',
00158 wfTimestamp( TS_MW, '20010115123456' ),
00159 'TS_MW to TS_MW' );
00160 $this->assertEquals(
00161 979562096,
00162 wfTimestamp( TS_UNIX, '20010115123456' ),
00163 'TS_MW to TS_UNIX' );
00164 $this->assertEquals(
00165 '2001-01-15 12:34:56',
00166 wfTimestamp( TS_DB, '20010115123456' ),
00167 'TS_MW to TS_DB' );
00168
00169 $this->assertEquals(
00170 '20010115123456',
00171 wfTimestamp( TS_MW, '2001-01-15 12:34:56' ),
00172 'TS_DB to TS_MW' );
00173 $this->assertEquals(
00174 979562096,
00175 wfTimestamp( TS_UNIX, '2001-01-15 12:34:56' ),
00176 'TS_DB to TS_UNIX' );
00177 $this->assertEquals(
00178 '2001-01-15 12:34:56',
00179 wfTimestamp( TS_DB, '2001-01-15 12:34:56' ),
00180 'TS_DB to TS_DB' );
00181 }
00182
00183 function testBasename() {
00184 $sets = array(
00185 '' => '',
00186 '/' => '',
00187 '\\' => '',
00188 '//' => '',
00189 '\\\\' => '',
00190 'a' => 'a',
00191 'aaaa' => 'aaaa',
00192 '/a' => 'a',
00193 '\\a' => 'a',
00194 '/aaaa' => 'aaaa',
00195 '\\aaaa' => 'aaaa',
00196 '/aaaa/' => 'aaaa',
00197 '\\aaaa\\' => 'aaaa',
00198 '\\aaaa\\' => 'aaaa',
00199 '/mnt/upload3/wikipedia/en/thumb/8/8b/Zork_Grand_Inquisitor_box_cover.jpg/93px-Zork_Grand_Inquisitor_box_cover.jpg' => '93px-Zork_Grand_Inquisitor_box_cover.jpg',
00200 'C:\\Progra~1\\Wikime~1\\Wikipe~1\\VIEWER.EXE' => 'VIEWER.EXE',
00201 'Östergötland_coat_of_arms.png' => 'Östergötland_coat_of_arms.png',
00202 );
00203 foreach( $sets as $from => $to ) {
00204 $this->assertEquals( $to, wfBaseName( $from ),
00205 "wfBaseName('$from') => '$to'");
00206 }
00207 }
00208
00209
00210 }
00211
00212