Make sure wfProfileIn is available for testing, in case some silly function uses it
[lhc/web/wiklou.git] / tests / GlobalTest.php
1 <?php
2
3 require_once( 'PHPUnit.php' );
4 require_once( '../includes/Defines.php' );
5 require_once( '../includes/Profiling.php' );
6 require_once( '../includes/GlobalFunctions.php' );
7
8 class GlobalTest extends PHPUnit_TestCase {
9 function GlobalTest( $name ) {
10 $this->PHPUnit_TestCase( $name );
11 }
12
13 function setUp() {
14 $this->save = array();
15 $saveVars = array( 'wgReadOnlyFile' );
16 foreach( $saveVars as $var ) {
17 if( isset( $GLOBALS[$var] ) ) {
18 $this->save[$var] = $GLOBALS[$var];
19 }
20 }
21 $GLOBALS['wgReadOnlyFile'] = '/tmp/testReadOnly-' . mt_rand();
22 }
23
24 function tearDown() {
25 foreach( $this->save as $var => $data ) {
26 $GLOBALS[$var] = $data;
27 }
28 }
29
30 function testDecodeLatin() {
31 $this->assertEquals(
32 "\xe9cole",
33 do_html_entity_decode( '&eacute;cole', ENT_COMPAT, 'iso-8859-1' ) );
34 }
35
36 function testDecodeUnicode() {
37 $this->assertEquals(
38 "\xc3\xa9cole",
39 do_html_entity_decode( '&eacute;cole', ENT_COMPAT, 'utf-8' ) );
40 }
41
42 function testRandom() {
43 # This could hypothetically fail, but it shouldn't ;)
44 $this->assertFalse(
45 wfRandom() == wfRandom() );
46 }
47
48 function testUrlencode() {
49 $this->assertEquals(
50 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
51 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
52 }
53
54 function testUtf8Sequence1() {
55 $this->assertEquals(
56 'A',
57 wfUtf8Sequence( 65 ) );
58 }
59
60 function testUtf8Sequence2() {
61 $this->assertEquals(
62 "\xc4\x88",
63 wfUtf8Sequence( 0x108 ) );
64 }
65
66 function testUtf8Sequence3() {
67 $this->assertEquals(
68 "\xe3\x81\x8b",
69 wfUtf8Sequence( 0x304b ) );
70 }
71
72 function testUtf8Sequence4() {
73 $this->assertEquals(
74 "\xf0\x90\x91\x90",
75 wfUtf8Sequence( 0x10450 ) );
76 }
77
78 function testMungeToUtf8() {
79 $this->assertEquals(
80 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
81 wfMungeToUtf8( "&#x108;io bonas dans l'&#233;cole!" ) );
82 }
83
84 function testUtf8ToHTML() {
85 $this->assertEquals(
86 "&#264;io bonas dans l'&#233;cole!",
87 wfUtf8ToHTML( "\xc4\x88io bonas dans l'\xc3\xa9cole!" ) );
88 }
89
90 function testReadOnlyEmpty() {
91 $this->assertFalse( wfReadOnly() );
92 }
93
94 function testReadOnlySet() {
95 $f = fopen( $GLOBALS['wgReadOnlyFile'], "wt" );
96 fwrite( $f, 'Message' );
97 fclose( $f );
98 $this->assertTrue( wfReadOnly() );
99
100 unlink( $GLOBALS['wgReadOnlyFile'] );
101 $this->assertFalse( wfReadOnly() );
102 }
103
104 function testQuotedPrintable() {
105 $this->assertEquals(
106 "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
107 wfQuotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
108 }
109
110 function testTime() {
111 $start = wfTime();
112 $this->assertType( 'double', $start );
113 $end = wfTime();
114 $this->assertTrue( $end > $start, "Time is running backwards!" );
115 }
116
117 function testArrayToCGI() {
118 $this->assertEquals(
119 "baz=AT%26T&foo=bar",
120 wfArrayToCGI(
121 array( 'baz' => 'AT&T', 'ignore' => '' ),
122 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
123 }
124
125 function testMimeTypeMatch() {
126 $this->assertEquals(
127 'text/html',
128 mimeTypeMatch( 'text/html',
129 array( 'application/xhtml+xml' => 1.0,
130 'text/html' => 0.7,
131 'text/plain' => 0.3 ) ) );
132 $this->assertEquals(
133 'text/*',
134 mimeTypeMatch( 'text/html',
135 array( 'image/*' => 1.0,
136 'text/*' => 0.5 ) ) );
137 $this->assertEquals(
138 '*/*',
139 mimeTypeMatch( 'text/html',
140 array( '*/*' => 1.0 ) ) );
141 $this->assertNull(
142 mimeTypeMatch( 'text/html',
143 array( 'image/png' => 1.0,
144 'image/svg+xml' => 0.5 ) ) );
145 }
146
147 function testNegotiateType() {
148 $this->assertEquals(
149 'text/html',
150 wfNegotiateType(
151 array( 'application/xhtml+xml' => 1.0,
152 'text/html' => 0.7,
153 'text/plain' => 0.5,
154 'text/*' => 0.2 ),
155 array( 'text/html' => 1.0 ) ) );
156 $this->assertEquals(
157 'application/xhtml+xml',
158 wfNegotiateType(
159 array( 'application/xhtml+xml' => 1.0,
160 'text/html' => 0.7,
161 'text/plain' => 0.5,
162 'text/*' => 0.2 ),
163 array( 'application/xhtml+xml' => 1.0,
164 'text/html' => 0.5 ) ) );
165 $this->assertEquals(
166 'text/html',
167 wfNegotiateType(
168 array( 'text/html' => 1.0,
169 'text/plain' => 0.5,
170 'text/*' => 0.5,
171 'application/xhtml+xml' => 0.2 ),
172 array( 'application/xhtml+xml' => 1.0,
173 'text/html' => 0.5 ) ) );
174 $this->assertEquals(
175 'text/html',
176 wfNegotiateType(
177 array( 'text/*' => 1.0,
178 'image/*' => 0.7,
179 '*/*' => 0.3 ),
180 array( 'application/xhtml+xml' => 1.0,
181 'text/html' => 0.5 ) ) );
182 $this->assertNull(
183 wfNegotiateType(
184 array( 'text/*' => 1.0 ),
185 array( 'application/xhtml+xml' => 1.0 ) ) );
186 }
187
188 function testTimestamp() {
189 $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
190 $this->assertEquals(
191 '20010115123456',
192 wfTimestamp( TS_MW, $t ),
193 'TS_UNIX to TS_MW' );
194 $this->assertEquals(
195 979562096,
196 wfTimestamp( TS_UNIX, $t ),
197 'TS_UNIX to TS_UNIX' );
198 $this->assertEquals(
199 '2001-01-15 12:34:56',
200 wfTimestamp( TS_DB, $t ),
201 'TS_UNIX to TS_DB' );
202
203 $this->assertEquals(
204 '20010115123456',
205 wfTimestamp( TS_MW, '20010115123456' ),
206 'TS_MW to TS_MW' );
207 $this->assertEquals(
208 979562096,
209 wfTimestamp( TS_UNIX, '20010115123456' ),
210 'TS_MW to TS_UNIX' );
211 $this->assertEquals(
212 '2001-01-15 12:34:56',
213 wfTimestamp( TS_DB, '20010115123456' ),
214 'TS_MW to TS_DB' );
215
216 $this->assertEquals(
217 '20010115123456',
218 wfTimestamp( TS_MW, '2001-01-15 12:34:56' ),
219 'TS_DB to TS_MW' );
220 $this->assertEquals(
221 979562096,
222 wfTimestamp( TS_UNIX, '2001-01-15 12:34:56' ),
223 'TS_DB to TS_UNIX' );
224 $this->assertEquals(
225 '2001-01-15 12:34:56',
226 wfTimestamp( TS_DB, '2001-01-15 12:34:56' ),
227 'TS_DB to TS_DB' );
228 }
229
230 /* TODO: many more! */
231 }
232
233 ?>