4088acaacf8f1418909bddb0bfef57dd61bf5ce8
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_div_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * Testcase for class t3lib_div
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 * @package TYPO3
31 * @subpackage t3lib
32 */
33 class t3lib_div_testcase extends tx_phpunit_testcase {
34
35 /**
36 * @test
37 */
38 public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
39 $testString = ' a , b , c ,d ,, e,f,';
40 $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
41 $actualArray = t3lib_div::trimExplode(',', $testString);
42
43 $this->assertEquals($expectedArray, $actualArray);
44 }
45
46 /**
47 * @test
48 */
49 public function checkTrimExplodeRemovesNewLines() {
50 $testString = ' a , b , ' . chr(10) . ' ,d ,, e,f,';
51 $expectedArray = array('a', 'b', 'd', 'e', 'f');
52 $actualArray = t3lib_div::trimExplode(',', $testString, true);
53
54 $this->assertEquals($expectedArray, $actualArray);
55 }
56
57 /**
58 * @test
59 */
60 public function checkTrimExplodeRemovesEmptyElements() {
61 $testString = 'a , b , c , ,d ,, ,e,f,';
62 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
63 $actualArray = t3lib_div::trimExplode(',', $testString, true);
64
65 $this->assertEquals($expectedArray, $actualArray);
66 }
67
68 /**
69 * @test
70 */
71 public function checkTrimExplodeLimitsResultsToFirstXElementsWithPositiveParameter() {
72 $testString = ' a , b , c , , d,, ,e ';
73 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
74 $actualArray = t3lib_div::trimExplode(',', $testString, false, 3);
75
76 $this->assertEquals($expectedArray, $actualArray);
77 }
78
79 /**
80 * @test
81 */
82 public function checkTrimExplodeLimitsResultsToLastXElementsWithNegativeParameter() {
83 $testString = ' a , b , c , d, ,e, f , , ';
84 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
85 $actualArray = t3lib_div::trimExplode(',', $testString, true, -3);
86
87 $this->assertEquals($expectedArray, $actualArray);
88 }
89
90 /**
91 * @test
92 */
93 public function checkTrimExplodeKeepsZeroAsString() {
94 $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
95 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
96 $actualArray = t3lib_div::trimExplode(',', $testString, true);
97
98 $this->assertEquals($expectedArray, $actualArray);
99 }
100
101 /**
102 * Checks whether measurement strings like "100k" return the accordant
103 * byte representation like 102400 in this case.
104 *
105 * @test
106 */
107 public function checkGetBytesFromSizeMeasurement() {
108 $this->assertEquals(
109 '102400',
110 t3lib_div::getBytesFromSizeMeasurement('100k')
111 );
112
113 $this->assertEquals(
114 '104857600',
115 t3lib_div::getBytesFromSizeMeasurement('100m')
116 );
117
118 $this->assertEquals(
119 '107374182400',
120 t3lib_div::getBytesFromSizeMeasurement('100g')
121 );
122 }
123
124 /**
125 * @test
126 */
127 public function checkIndpEnvTypo3SitePathNotEmpty() {
128 $actualEnv = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
129 $this->assertTrue(strlen($actualEnv) >= 1);
130 $this->assertEquals('/', $actualEnv{0});
131 $this->assertEquals('/', $actualEnv{strlen($actualEnv) - 1});
132 }
133
134 /**
135 * @test
136 * @see t3lib_div::underscoredToUpperCamelCase
137 */
138 public function canConvertFromUnderscoredToUpperCamelCase() {
139 $this->assertEquals('BlogExample', t3lib_div::underscoredToUpperCamelCase('blog_example'));
140 $this->assertEquals('Blogexample', t3lib_div::underscoredToUpperCamelCase('blogexample'));
141 }
142
143 /**
144 * @test
145 * @see t3lib_div::underscoredToLowerCamelCase
146 */
147 public function canConvertFromUnderscoredToLowerCamelCase() {
148 $this->assertEquals('minimalValue', t3lib_div::underscoredToLowerCamelCase('minimal_value'));
149 $this->assertEquals('minimalvalue', t3lib_div::underscoredToLowerCamelCase('minimalvalue'));
150 }
151
152 /**
153 * @test
154 * @see t3lib_div::camelCaseToLowerCaseUnderscored
155 */
156 public function canConvertFromCamelCaseToLowerCaseUnderscored() {
157 $this->assertEquals('blog_example', t3lib_div::camelCaseToLowerCaseUnderscored('BlogExample'));
158 $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('Blogexample'));
159 $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('blogexample'));
160
161 $this->assertEquals('minimal_value', t3lib_div::camelCaseToLowerCaseUnderscored('minimalValue'));
162 }
163
164 /**
165 * @test
166 * @see t3lib_div::lcfirst
167 */
168 public function canConvertFirstCharacterToBeLowerCase() {
169 $this->assertEquals('blogexample', t3lib_div::lcfirst('Blogexample'));
170 $this->assertEquals('blogExample', t3lib_div::lcfirst('BlogExample'));
171 $this->assertEquals('blogexample', t3lib_div::lcfirst('blogexample'));
172 }
173
174 /**
175 * Tests whether whitespaces are encoded correctly in a quoted-printable mail header.
176 * @test
177 */
178 public function areWhitespacesEncodedInQuotedPrintableMailHeader() {
179 $this->assertEquals(
180 '=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=',
181 t3lib_div::encodeHeader(
182 "We test whether the copyright character \xc2\xa9 is encoded correctly",
183 'quoted-printable',
184 'utf-8'
185 )
186 );
187 }
188
189 /**
190 * Tests whether question marks are encoded correctly in a quoted-printable mail header.
191 * @test
192 */
193 public function areQuestionMarksEncodedInQuotedPrintableMailHeader() {
194 $this->assertEquals(
195 '=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=',
196 t3lib_div::encodeHeader(
197 "Is the copyright character \xc2\xa9 really encoded correctly? Really?",
198 'quoted-printable',
199 'utf-8'
200 )
201 );
202 }
203
204 /**
205 * @test
206 * @see t3lib_div::isValidUrl()
207 */
208 public function checkisValidURLSucceedsWithWebRessource() {
209 $testUrl = 'http://www.example.org/';
210 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
211 }
212
213 /**
214 * @test
215 * @see t3lib_div::isValidUrl()
216 */
217 public function checkisValidURLSucceedsWithExtentedWebRessource() {
218 $testUrl = 'https://user:pw@www.example.org:80/path?arg=value#fragment';
219 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
220 }
221
222 /**
223 * @test
224 * @see t3lib_div::isValidUrl()
225 */
226 public function checkisValidURLSucceedsWithTelnetRessource() {
227 $testUrl = 'telnet://192.0.2.16:80/';
228 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
229 }
230
231 /**
232 * @test
233 */
234 public function checkisValidURLSucceedsWithLdapRessource() {
235 $testUrl = 'ldap://[2001:db8::7]/c=GB?objectClass?one';
236 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
237 }
238
239 /**
240 * @test
241 * @see t3lib_div::isValidUrl()
242 */
243 public function checkisValidURLSucceedsWithFileRessource() {
244 $testUrl = 'file:///etc/passwd';
245 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
246 }
247
248 /**
249 * @test
250 * @see t3lib_div::isValidUrl()
251 */
252 public function checkisValidURLFailsWithHostnameOnly() {
253 $testUrl = 'www.example.org/';
254 $this->assertFalse(t3lib_div::isValidUrl($testUrl));
255 }
256
257 /**
258 * @test
259 * @see t3lib_div::isOnCurrentHost()
260 */
261 public function checkisOnCurrentHostFailsWithLocalhostIPOnly() {
262 $testUrl = '127.0.0.1';
263 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
264 }
265
266 /**
267 * @test
268 * @see t3lib_div::isOnCurrentHost()
269 */
270 public function checkisOnCurrentHostFailsWithPathsOnly() {
271 $testUrl = './relpath/file.txt';
272 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
273 $testUrl = '/abspath/file.txt?arg=value';
274 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
275 }
276
277 /**
278 * @test
279 * @see t3lib_div::isOnCurrentHost()
280 */
281 public function checkisOnCurrentHostFailsWithArbitraryString() {
282 $testUrl = 'arbitrary string';
283 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
284 }
285
286 /**
287 * @test
288 * @see t3lib_div::isOnCurrentHost()
289 */
290 public function checkisOnCurrentHostFailsWithEmptyUrl() {
291 $testUrl = '';
292 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
293 }
294
295 /**
296 * @test
297 * @see t3lib_div::isOnCurrentHost()
298 */
299 public function checkisOnCurrentHostFailsWithDifferentHost() {
300 $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org';
301 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
302 }
303
304 /**
305 * @test
306 * @see t3lib_div::isOnCurrentHost()
307 */
308 public function checkisOnCurrentHostSucceedsWithCurrentHost() {
309 $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
310 $this->assertTrue(t3lib_div::isOnCurrentHost($testUrl));
311 }
312
313 /**
314 * Tests whether removeDotsFromTS() behaves correctly.
315 * @test
316 * @see t3lib_div::removeDotsFromTS()
317 */
318 public function doesRemoveDotsFromTypoScriptSucceed() {
319 $typoScript = array(
320 'propertyA.' => array(
321 'keyA.' => array(
322 'valueA' => 1,
323 ),
324 'keyB' => 2,
325 ),
326 'propertyB' => 3,
327 );
328
329 $expectedResult = array(
330 'propertyA' => array(
331 'keyA' => array(
332 'valueA' => 1,
333 ),
334 'keyB' => 2,
335 ),
336 'propertyB' => 3,
337 );
338
339 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
340 }
341
342 /**
343 * Tests whether removeDotsFromTS() behaves correctly.
344 * @test
345 * @see t3lib_div::removeDotsFromTS()
346 */
347 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithArray() {
348 $typoScript = array(
349 'propertyA.' => array(
350 'keyA' => 'getsOverridden',
351 'keyA.' => array(
352 'valueA' => 1,
353 ),
354 'keyB' => 2,
355 ),
356 'propertyB' => 3,
357 );
358
359 $expectedResult = array(
360 'propertyA' => array(
361 'keyA' => array(
362 'valueA' => 1,
363 ),
364 'keyB' => 2,
365 ),
366 'propertyB' => 3,
367 );
368
369 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
370 }
371
372 /**
373 * Tests whether removeDotsFromTS() behaves correctly.
374 * @test
375 * @see t3lib_div::removeDotsFromTS()
376 */
377 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithScalar() {
378 $typoScript = array(
379 'propertyA.' => array(
380 'keyA.' => array(
381 'valueA' => 1,
382 ),
383 'keyA' => 'willOverride',
384 'keyB' => 2,
385 ),
386 'propertyB' => 3,
387 );
388
389 $expectedResult = array(
390 'propertyA' => array(
391 'keyA' => 'willOverride',
392 'keyB' => 2,
393 ),
394 'propertyB' => 3,
395 );
396
397 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
398 }
399
400 /**
401 * Tests whether getDirs() returns an array of diretories from a given path
402 * @test
403 * @see t3lib_div::getDirs($path)
404 */
405 public function checkGetDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
406 $path = PATH_t3lib;
407 $directories = t3lib_div::get_dirs($path);
408
409 $this->assertType('array', $directories);
410 }
411
412 /**
413 * Tests whether getDirs() returns the string 'error' in case of problems reading from the given path
414 * @test
415 * @see t3lib_div::getDirs($path)
416 */
417 public function checkGetDirsReturnsStringErrorOnPathFailure() {
418 $path = 'foo';
419 $result = t3lib_div::get_dirs($path);
420 $expectedResult = 'error';
421
422 $this->assertEquals($expectedResult, $result);
423 }
424 }
425
426 ?>