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