]> git.immae.eu Git - github/shaarli/Shaarli.git/blame_incremental - tests/UtilsTest.php
doc: server config: basic usage of robots.txt/HTML robots meta-tag/crawler control...
[github/shaarli/Shaarli.git] / tests / UtilsTest.php
... / ...
CommitLineData
1<?php
2/**
3 * Utilities' tests
4 */
5
6require_once 'application/Utils.php';
7require_once 'application/Languages.php';
8
9
10/**
11 * Unitary tests for Shaarli utilities
12 */
13class UtilsTest extends PHPUnit_Framework_TestCase
14{
15 // Log file
16 protected static $testLogFile = 'tests.log';
17
18 // Expected log date format
19 protected static $dateFormat = 'Y/m/d H:i:s';
20
21 /**
22 * @var string Save the current timezone.
23 */
24 protected static $defaultTimeZone;
25
26 /**
27 * Assign reference data
28 */
29 public static function setUpBeforeClass()
30 {
31 self::$defaultTimeZone = date_default_timezone_get();
32 // Timezone without DST for test consistency
33 date_default_timezone_set('Africa/Nairobi');
34 }
35
36 /**
37 * Reset the timezone
38 */
39 public static function tearDownAfterClass()
40 {
41 date_default_timezone_set(self::$defaultTimeZone);
42 }
43
44 /**
45 * Resets test data before each test
46 */
47 protected function setUp()
48 {
49 if (file_exists(self::$testLogFile)) {
50 unlink(self::$testLogFile);
51 }
52 }
53
54 /**
55 * Returns a list of the elements from the last logged entry
56 *
57 * @return list (date, ip address, message)
58 */
59 protected function getLastLogEntry()
60 {
61 $logFile = file(self::$testLogFile);
62 return explode(' - ', trim(array_pop($logFile), PHP_EOL));
63 }
64
65 /**
66 * Log a message to a file - IPv4 client address
67 */
68 public function testLogmIp4()
69 {
70 $logMessage = 'IPv4 client connected';
71 logm(self::$testLogFile, '127.0.0.1', $logMessage);
72 list($date, $ip, $message) = $this->getLastLogEntry();
73
74 $this->assertInstanceOf(
75 'DateTime',
76 DateTime::createFromFormat(self::$dateFormat, $date)
77 );
78 $this->assertTrue(
79 filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false
80 );
81 $this->assertEquals($logMessage, $message);
82 }
83
84 /**
85 * Log a message to a file - IPv6 client address
86 */
87 public function testLogmIp6()
88 {
89 $logMessage = 'IPv6 client connected';
90 logm(self::$testLogFile, '2001:db8::ff00:42:8329', $logMessage);
91 list($date, $ip, $message) = $this->getLastLogEntry();
92
93 $this->assertInstanceOf(
94 'DateTime',
95 DateTime::createFromFormat(self::$dateFormat, $date)
96 );
97 $this->assertTrue(
98 filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false
99 );
100 $this->assertEquals($logMessage, $message);
101 }
102
103 /**
104 * Represent a link by its hash
105 */
106 public function testSmallHash()
107 {
108 $this->assertEquals('CyAAJw', smallHash('http://test.io'));
109 $this->assertEquals(6, strlen(smallHash('https://github.com')));
110 }
111
112 /**
113 * Look for a substring at the beginning of a string
114 */
115 public function testStartsWithCaseInsensitive()
116 {
117 $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
118 $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
119 }
120
121 /**
122 * Look for a substring at the beginning of a string (case-sensitive)
123 */
124 public function testStartsWithCaseSensitive()
125 {
126 $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true));
127 $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true));
128 $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true));
129 }
130
131 /**
132 * Look for a substring at the beginning of a string (Unicode)
133 */
134 public function testStartsWithSpecialChars()
135 {
136 $this->assertTrue(startsWith('å!ùµ', 'å!', false));
137 $this->assertTrue(startsWith('µ$åù', 'µ$', true));
138 }
139
140 /**
141 * Look for a substring at the end of a string
142 */
143 public function testEndsWithCaseInsensitive()
144 {
145 $this->assertTrue(endsWith('Lorem ipsum', 'ipsum', false));
146 $this->assertTrue(endsWith('Lorem ipsum', 'm IpsUM', false));
147 }
148
149 /**
150 * Look for a substring at the end of a string (case-sensitive)
151 */
152 public function testEndsWithCaseSensitive()
153 {
154 $this->assertTrue(endsWith('lorem Ipsum', 'Ipsum', true));
155 $this->assertFalse(endsWith('lorem Ipsum', 'ipsum', true));
156 $this->assertFalse(endsWith('lorem Ipsum', 'M IPsuM', true));
157 }
158
159 /**
160 * Look for a substring at the end of a string (Unicode)
161 */
162 public function testEndsWithSpecialChars()
163 {
164 $this->assertTrue(endsWith('å!ùµ', 'ùµ', false));
165 $this->assertTrue(endsWith('µ$åù', 'åù', true));
166 }
167
168 /**
169 * Check valid date strings, according to a DateTime format
170 */
171 public function testCheckValidDateFormat()
172 {
173 $this->assertTrue(checkDateFormat('Ymd', '20150627'));
174 $this->assertTrue(checkDateFormat('Y-m-d', '2015-06-27'));
175 }
176
177 /**
178 * Check erroneous date strings, according to a DateTime format
179 */
180 public function testCheckInvalidDateFormat()
181 {
182 $this->assertFalse(checkDateFormat('Ymd', '2015'));
183 $this->assertFalse(checkDateFormat('Y-m-d', '2015-06'));
184 $this->assertFalse(checkDateFormat('Ymd', 'DeLorean'));
185 }
186
187 /**
188 * Test generate location with valid data.
189 */
190 public function testGenerateLocation() {
191 $ref = 'http://localhost/?test';
192 $this->assertEquals($ref, generateLocation($ref, 'localhost'));
193 $ref = 'http://localhost:8080/?test';
194 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
195 $ref = '?localreferer#hash';
196 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
197 }
198
199 /**
200 * Test generate location - anti loop.
201 */
202 public function testGenerateLocationLoop() {
203 $ref = 'http://localhost/?test';
204 $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
205 }
206
207 /**
208 * Test generate location - from other domain.
209 */
210 public function testGenerateLocationOut() {
211 $ref = 'http://somewebsite.com/?test';
212 $this->assertEquals('?', generateLocation($ref, 'localhost'));
213 }
214
215
216 /**
217 * Test generateSecretApi.
218 */
219 public function testGenerateSecretApi()
220 {
221 $this->assertEquals(12, strlen(generate_api_secret('foo', 'bar')));
222 }
223
224 /**
225 * Test generateSecretApi with invalid parameters.
226 */
227 public function testGenerateSecretApiInvalid()
228 {
229 $this->assertFalse(generate_api_secret('', ''));
230 $this->assertFalse(generate_api_secret(false, false));
231 }
232
233 /**
234 * Test normalize_spaces.
235 */
236 public function testNormalizeSpace()
237 {
238 $str = ' foo bar is important ';
239 $this->assertEquals('foo bar is important', normalize_spaces($str));
240 $this->assertEquals('foo', normalize_spaces('foo'));
241 $this->assertEquals('', normalize_spaces(''));
242 $this->assertEquals(null, normalize_spaces(null));
243 }
244
245 /**
246 * Test arrays_combine
247 */
248 public function testCartesianProductGenerator()
249 {
250 $arr = [['ab', 'cd'], ['ef', 'gh'], ['ij', 'kl'], ['m']];
251 $expected = [
252 ['ab', 'ef', 'ij', 'm'],
253 ['ab', 'ef', 'kl', 'm'],
254 ['ab', 'gh', 'ij', 'm'],
255 ['ab', 'gh', 'kl', 'm'],
256 ['cd', 'ef', 'ij', 'm'],
257 ['cd', 'ef', 'kl', 'm'],
258 ['cd', 'gh', 'ij', 'm'],
259 ['cd', 'gh', 'kl', 'm'],
260 ];
261 $this->assertEquals($expected, iterator_to_array(cartesian_product_generator($arr)));
262 }
263
264 /**
265 * Test date_format() with invalid parameter.
266 */
267 public function testDateFormatInvalid()
268 {
269 $this->assertFalse(format_date([]));
270 $this->assertFalse(format_date(null));
271 }
272
273 /**
274 * Test is_integer_mixed with valid values
275 */
276 public function testIsIntegerMixedValid()
277 {
278 $this->assertTrue(is_integer_mixed(12));
279 $this->assertTrue(is_integer_mixed('12'));
280 $this->assertTrue(is_integer_mixed(-12));
281 $this->assertTrue(is_integer_mixed('-12'));
282 $this->assertTrue(is_integer_mixed(0));
283 $this->assertTrue(is_integer_mixed('0'));
284 $this->assertTrue(is_integer_mixed(0x0a));
285 }
286
287 /**
288 * Test is_integer_mixed with invalid values
289 */
290 public function testIsIntegerMixedInvalid()
291 {
292 $this->assertFalse(is_integer_mixed(true));
293 $this->assertFalse(is_integer_mixed(false));
294 $this->assertFalse(is_integer_mixed([]));
295 $this->assertFalse(is_integer_mixed(['test']));
296 $this->assertFalse(is_integer_mixed([12]));
297 $this->assertFalse(is_integer_mixed(new DateTime()));
298 $this->assertFalse(is_integer_mixed('0x0a'));
299 $this->assertFalse(is_integer_mixed('12k'));
300 $this->assertFalse(is_integer_mixed('k12'));
301 $this->assertFalse(is_integer_mixed(''));
302 }
303
304 /**
305 * Test return_bytes
306 */
307 public function testReturnBytes()
308 {
309 $this->assertEquals(2 * 1024, return_bytes('2k'));
310 $this->assertEquals(2 * 1024, return_bytes('2K'));
311 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2m'));
312 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2M'));
313 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2g'));
314 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2G'));
315 $this->assertEquals(374, return_bytes('374'));
316 $this->assertEquals(374, return_bytes(374));
317 $this->assertEquals(0, return_bytes('0'));
318 $this->assertEquals(0, return_bytes(0));
319 $this->assertEquals(-1, return_bytes('-1'));
320 $this->assertEquals(-1, return_bytes(-1));
321 $this->assertEquals('', return_bytes(''));
322 }
323
324 /**
325 * Test human_bytes
326 */
327 public function testHumanBytes()
328 {
329 $this->assertEquals('2'. t('kiB'), human_bytes(2 * 1024));
330 $this->assertEquals('2'. t('kiB'), human_bytes(strval(2 * 1024)));
331 $this->assertEquals('2'. t('MiB'), human_bytes(2 * (pow(1024, 2))));
332 $this->assertEquals('2'. t('MiB'), human_bytes(strval(2 * (pow(1024, 2)))));
333 $this->assertEquals('2'. t('GiB'), human_bytes(2 * (pow(1024, 3))));
334 $this->assertEquals('2'. t('GiB'), human_bytes(strval(2 * (pow(1024, 3)))));
335 $this->assertEquals('374'. t('B'), human_bytes(374));
336 $this->assertEquals('374'. t('B'), human_bytes('374'));
337 $this->assertEquals('232'. t('kiB'), human_bytes(237481));
338 $this->assertEquals(t('Unlimited'), human_bytes('0'));
339 $this->assertEquals(t('Unlimited'), human_bytes(0));
340 $this->assertEquals(t('Setting not set'), human_bytes(''));
341 }
342
343 /**
344 * Test get_max_upload_size with formatting
345 */
346 public function testGetMaxUploadSize()
347 {
348 $this->assertEquals('1'. t('MiB'), get_max_upload_size(2097152, '1024k'));
349 $this->assertEquals('1'. t('MiB'), get_max_upload_size('1m', '2m'));
350 $this->assertEquals('100'. t('B'), get_max_upload_size(100, 100));
351 }
352
353 /**
354 * Test get_max_upload_size without formatting
355 */
356 public function testGetMaxUploadSizeRaw()
357 {
358 $this->assertEquals('1048576', get_max_upload_size(2097152, '1024k', false));
359 $this->assertEquals('1048576', get_max_upload_size('1m', '2m', false));
360 $this->assertEquals('100', get_max_upload_size(100, 100, false));
361 }
362
363 /**
364 * Test alphabetical_sort by value, not reversed, with php-intl.
365 */
366 public function testAlphabeticalSortByValue()
367 {
368 $arr = [
369 'zZz',
370 'éee',
371 'éae',
372 'eee',
373 'A',
374 'a',
375 'zzz',
376 ];
377 $expected = [
378 'a',
379 'A',
380 'éae',
381 'eee',
382 'éee',
383 'zzz',
384 'zZz',
385 ];
386
387 alphabetical_sort($arr);
388 $this->assertEquals($expected, $arr);
389 }
390
391 /**
392 * Test alphabetical_sort by value, reversed, with php-intl.
393 */
394 public function testAlphabeticalSortByValueReversed()
395 {
396 $arr = [
397 'zZz',
398 'éee',
399 'éae',
400 'eee',
401 'A',
402 'a',
403 'zzz',
404 ];
405 $expected = [
406 'zZz',
407 'zzz',
408 'éee',
409 'eee',
410 'éae',
411 'A',
412 'a',
413 ];
414
415 alphabetical_sort($arr, true);
416 $this->assertEquals($expected, $arr);
417 }
418
419 /**
420 * Test alphabetical_sort by keys, not reversed, with php-intl.
421 */
422 public function testAlphabeticalSortByKeys()
423 {
424 $arr = [
425 'zZz' => true,
426 'éee' => true,
427 'éae' => true,
428 'eee' => true,
429 'A' => true,
430 'a' => true,
431 'zzz' => true,
432 ];
433 $expected = [
434 'a' => true,
435 'A' => true,
436 'éae' => true,
437 'eee' => true,
438 'éee' => true,
439 'zzz' => true,
440 'zZz' => true,
441 ];
442
443 alphabetical_sort($arr, true, true);
444 $this->assertEquals($expected, $arr);
445 }
446
447 /**
448 * Test alphabetical_sort by keys, reversed, with php-intl.
449 */
450 public function testAlphabeticalSortByKeysReversed()
451 {
452 $arr = [
453 'zZz' => true,
454 'éee' => true,
455 'éae' => true,
456 'eee' => true,
457 'A' => true,
458 'a' => true,
459 'zzz' => true,
460 ];
461 $expected = [
462 'zZz' => true,
463 'zzz' => true,
464 'éee' => true,
465 'eee' => true,
466 'éae' => true,
467 'A' => true,
468 'a' => true,
469 ];
470
471 alphabetical_sort($arr, true, true);
472 $this->assertEquals($expected, $arr);
473 }
474}