Replaced all instances of <<<END (which breaks vim syntax highlighting), with a type...
[lhc/web/wiklou.git] / tests / GlobalTest.php
1 <?php
2
3 class GlobalTest extends PHPUnit_Framework_TestCase {
4 function setUp() {
5 global $wgReadOnlyFile;
6 $this->originals['wgReadOnlyFile'] = $wgReadOnlyFile;
7 $wgReadOnlyFile = tempnam(wfTempDir(), "mwtest_readonly");
8 unlink( $wgReadOnlyFile );
9 }
10
11 function tearDown() {
12 global $wgReadOnlyFile;
13 if( file_exists( $wgReadOnlyFile ) ) {
14 unlink( $wgReadOnlyFile );
15 }
16 $wgReadOnlyFile = $this->originals['wgReadOnlyFile'];
17 }
18
19 function testRandom() {
20 # This could hypothetically fail, but it shouldn't ;)
21 $this->assertFalse(
22 wfRandom() == wfRandom() );
23 }
24
25 function testUrlencode() {
26 $this->assertEquals(
27 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
28 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
29 }
30
31 function testReadOnlyEmpty() {
32 global $wgReadOnly;
33 $wgReadOnly = null;
34
35 $this->assertFalse( wfReadOnly() );
36 $this->assertFalse( wfReadOnly() );
37 }
38
39 function testReadOnlySet() {
40 global $wgReadOnly, $wgReadOnlyFile;
41
42 $f = fopen( $wgReadOnlyFile, "wt" );
43 fwrite( $f, 'Message' );
44 fclose( $f );
45 $wgReadOnly = null;
46
47 $this->assertTrue( wfReadOnly() );
48 $this->assertTrue( wfReadOnly() );
49
50 unlink( $wgReadOnlyFile );
51 $wgReadOnly = null;
52
53 $this->assertFalse( wfReadOnly() );
54 $this->assertFalse( wfReadOnly() );
55 }
56
57 function testQuotedPrintable() {
58 $this->assertEquals(
59 "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
60 wfQuotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
61 }
62
63 function testTime() {
64 $start = wfTime();
65 $this->assertType( 'float', $start );
66 $end = wfTime();
67 $this->assertTrue( $end > $start, "Time is running backwards!" );
68 }
69
70 function testArrayToCGI() {
71 $this->assertEquals(
72 "baz=AT%26T&foo=bar",
73 wfArrayToCGI(
74 array( 'baz' => 'AT&T', 'ignore' => '' ),
75 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
76 }
77
78 function testMimeTypeMatch() {
79 $this->assertEquals(
80 'text/html',
81 mimeTypeMatch( 'text/html',
82 array( 'application/xhtml+xml' => 1.0,
83 'text/html' => 0.7,
84 'text/plain' => 0.3 ) ) );
85 $this->assertEquals(
86 'text/*',
87 mimeTypeMatch( 'text/html',
88 array( 'image/*' => 1.0,
89 'text/*' => 0.5 ) ) );
90 $this->assertEquals(
91 '*/*',
92 mimeTypeMatch( 'text/html',
93 array( '*/*' => 1.0 ) ) );
94 $this->assertNull(
95 mimeTypeMatch( 'text/html',
96 array( 'image/png' => 1.0,
97 'image/svg+xml' => 0.5 ) ) );
98 }
99
100 function testNegotiateType() {
101 $this->assertEquals(
102 'text/html',
103 wfNegotiateType(
104 array( 'application/xhtml+xml' => 1.0,
105 'text/html' => 0.7,
106 'text/plain' => 0.5,
107 'text/*' => 0.2 ),
108 array( 'text/html' => 1.0 ) ) );
109 $this->assertEquals(
110 'application/xhtml+xml',
111 wfNegotiateType(
112 array( 'application/xhtml+xml' => 1.0,
113 'text/html' => 0.7,
114 'text/plain' => 0.5,
115 'text/*' => 0.2 ),
116 array( 'application/xhtml+xml' => 1.0,
117 'text/html' => 0.5 ) ) );
118 $this->assertEquals(
119 'text/html',
120 wfNegotiateType(
121 array( 'text/html' => 1.0,
122 'text/plain' => 0.5,
123 'text/*' => 0.5,
124 'application/xhtml+xml' => 0.2 ),
125 array( 'application/xhtml+xml' => 1.0,
126 'text/html' => 0.5 ) ) );
127 $this->assertEquals(
128 'text/html',
129 wfNegotiateType(
130 array( 'text/*' => 1.0,
131 'image/*' => 0.7,
132 '*/*' => 0.3 ),
133 array( 'application/xhtml+xml' => 1.0,
134 'text/html' => 0.5 ) ) );
135 $this->assertNull(
136 wfNegotiateType(
137 array( 'text/*' => 1.0 ),
138 array( 'application/xhtml+xml' => 1.0 ) ) );
139 }
140
141 function testTimestamp() {
142 $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
143 $this->assertEquals(
144 '20010115123456',
145 wfTimestamp( TS_MW, $t ),
146 'TS_UNIX to TS_MW' );
147 $this->assertEquals(
148 979562096,
149 wfTimestamp( TS_UNIX, $t ),
150 'TS_UNIX to TS_UNIX' );
151 $this->assertEquals(
152 '2001-01-15 12:34:56',
153 wfTimestamp( TS_DB, $t ),
154 'TS_UNIX to TS_DB' );
155
156 $this->assertEquals(
157 '20010115123456',
158 wfTimestamp( TS_MW, '20010115123456' ),
159 'TS_MW to TS_MW' );
160 $this->assertEquals(
161 979562096,
162 wfTimestamp( TS_UNIX, '20010115123456' ),
163 'TS_MW to TS_UNIX' );
164 $this->assertEquals(
165 '2001-01-15 12:34:56',
166 wfTimestamp( TS_DB, '20010115123456' ),
167 'TS_MW to TS_DB' );
168
169 $this->assertEquals(
170 '20010115123456',
171 wfTimestamp( TS_MW, '2001-01-15 12:34:56' ),
172 'TS_DB to TS_MW' );
173 $this->assertEquals(
174 979562096,
175 wfTimestamp( TS_UNIX, '2001-01-15 12:34:56' ),
176 'TS_DB to TS_UNIX' );
177 $this->assertEquals(
178 '2001-01-15 12:34:56',
179 wfTimestamp( TS_DB, '2001-01-15 12:34:56' ),
180 'TS_DB to TS_DB' );
181 }
182
183 function testBasename() {
184 $sets = array(
185 '' => '',
186 '/' => '',
187 '\\' => '',
188 '//' => '',
189 '\\\\' => '',
190 'a' => 'a',
191 'aaaa' => 'aaaa',
192 '/a' => 'a',
193 '\\a' => 'a',
194 '/aaaa' => 'aaaa',
195 '\\aaaa' => 'aaaa',
196 '/aaaa/' => 'aaaa',
197 '\\aaaa\\' => 'aaaa',
198 '\\aaaa\\' => 'aaaa',
199 '/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',
200 'C:\\Progra~1\\Wikime~1\\Wikipe~1\\VIEWER.EXE' => 'VIEWER.EXE',
201 'Östergötland_coat_of_arms.png' => 'Östergötland_coat_of_arms.png',
202 );
203 foreach( $sets as $from => $to ) {
204 $this->assertEquals( $to, wfBaseName( $from ),
205 "wfBaseName('$from') => '$to'");
206 }
207 }
208
209 /* TODO: many more! */
210 }
211
212