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