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