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