]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/UtilsTest.php
Merge commit '1826e383ecf501302974132fd443cf1ca06e10f6' into v0.10
[github/shaarli/Shaarli.git] / tests / UtilsTest.php
1 <?php
2 /**
3 * Utilities' tests
4 */
5
6 require_once 'application/Utils.php';
7 require_once 'application/Languages.php';
8
9
10 /**
11 * Unitary tests for Shaarli utilities
12 */
13 class 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 {
192 $ref = 'http://localhost/?test';
193 $this->assertEquals($ref, generateLocation($ref, 'localhost'));
194 $ref = 'http://localhost:8080/?test';
195 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
196 $ref = '?localreferer#hash';
197 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
198 }
199
200 /**
201 * Test generate location - anti loop.
202 */
203 public function testGenerateLocationLoop()
204 {
205 $ref = 'http://localhost/?test';
206 $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
207 }
208
209 /**
210 * Test generate location - from other domain.
211 */
212 public function testGenerateLocationOut()
213 {
214 $ref = 'http://somewebsite.com/?test';
215 $this->assertEquals('?', generateLocation($ref, 'localhost'));
216 }
217
218
219 /**
220 * Test generateSecretApi.
221 */
222 public function testGenerateSecretApi()
223 {
224 $this->assertEquals(12, strlen(generate_api_secret('foo', 'bar')));
225 }
226
227 /**
228 * Test generateSecretApi with invalid parameters.
229 */
230 public function testGenerateSecretApiInvalid()
231 {
232 $this->assertFalse(generate_api_secret('', ''));
233 $this->assertFalse(generate_api_secret(false, false));
234 }
235
236 /**
237 * Test normalize_spaces.
238 */
239 public function testNormalizeSpace()
240 {
241 $str = ' foo bar is important ';
242 $this->assertEquals('foo bar is important', normalize_spaces($str));
243 $this->assertEquals('foo', normalize_spaces('foo'));
244 $this->assertEquals('', normalize_spaces(''));
245 $this->assertEquals(null, normalize_spaces(null));
246 }
247
248 /**
249 * Test arrays_combine
250 */
251 public function testCartesianProductGenerator()
252 {
253 $arr = [['ab', 'cd'], ['ef', 'gh'], ['ij', 'kl'], ['m']];
254 $expected = [
255 ['ab', 'ef', 'ij', 'm'],
256 ['ab', 'ef', 'kl', 'm'],
257 ['ab', 'gh', 'ij', 'm'],
258 ['ab', 'gh', 'kl', 'm'],
259 ['cd', 'ef', 'ij', 'm'],
260 ['cd', 'ef', 'kl', 'm'],
261 ['cd', 'gh', 'ij', 'm'],
262 ['cd', 'gh', 'kl', 'm'],
263 ];
264 $this->assertEquals($expected, iterator_to_array(cartesian_product_generator($arr)));
265 }
266
267 /**
268 * Test date_format() with invalid parameter.
269 */
270 public function testDateFormatInvalid()
271 {
272 $this->assertFalse(format_date([]));
273 $this->assertFalse(format_date(null));
274 }
275
276 /**
277 * Test is_integer_mixed with valid values
278 */
279 public function testIsIntegerMixedValid()
280 {
281 $this->assertTrue(is_integer_mixed(12));
282 $this->assertTrue(is_integer_mixed('12'));
283 $this->assertTrue(is_integer_mixed(-12));
284 $this->assertTrue(is_integer_mixed('-12'));
285 $this->assertTrue(is_integer_mixed(0));
286 $this->assertTrue(is_integer_mixed('0'));
287 $this->assertTrue(is_integer_mixed(0x0a));
288 }
289
290 /**
291 * Test is_integer_mixed with invalid values
292 */
293 public function testIsIntegerMixedInvalid()
294 {
295 $this->assertFalse(is_integer_mixed(true));
296 $this->assertFalse(is_integer_mixed(false));
297 $this->assertFalse(is_integer_mixed([]));
298 $this->assertFalse(is_integer_mixed(['test']));
299 $this->assertFalse(is_integer_mixed([12]));
300 $this->assertFalse(is_integer_mixed(new DateTime()));
301 $this->assertFalse(is_integer_mixed('0x0a'));
302 $this->assertFalse(is_integer_mixed('12k'));
303 $this->assertFalse(is_integer_mixed('k12'));
304 $this->assertFalse(is_integer_mixed(''));
305 }
306
307 /**
308 * Test return_bytes
309 */
310 public function testReturnBytes()
311 {
312 $this->assertEquals(2 * 1024, return_bytes('2k'));
313 $this->assertEquals(2 * 1024, return_bytes('2K'));
314 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2m'));
315 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2M'));
316 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2g'));
317 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2G'));
318 $this->assertEquals(374, return_bytes('374'));
319 $this->assertEquals(374, return_bytes(374));
320 $this->assertEquals(0, return_bytes('0'));
321 $this->assertEquals(0, return_bytes(0));
322 $this->assertEquals(-1, return_bytes('-1'));
323 $this->assertEquals(-1, return_bytes(-1));
324 $this->assertEquals('', return_bytes(''));
325 }
326
327 /**
328 * Test human_bytes
329 */
330 public function testHumanBytes()
331 {
332 $this->assertEquals('2'. t('kiB'), human_bytes(2 * 1024));
333 $this->assertEquals('2'. t('kiB'), human_bytes(strval(2 * 1024)));
334 $this->assertEquals('2'. t('MiB'), human_bytes(2 * (pow(1024, 2))));
335 $this->assertEquals('2'. t('MiB'), human_bytes(strval(2 * (pow(1024, 2)))));
336 $this->assertEquals('2'. t('GiB'), human_bytes(2 * (pow(1024, 3))));
337 $this->assertEquals('2'. t('GiB'), human_bytes(strval(2 * (pow(1024, 3)))));
338 $this->assertEquals('374'. t('B'), human_bytes(374));
339 $this->assertEquals('374'. t('B'), human_bytes('374'));
340 $this->assertEquals('232'. t('kiB'), human_bytes(237481));
341 $this->assertEquals(t('Unlimited'), human_bytes('0'));
342 $this->assertEquals(t('Unlimited'), human_bytes(0));
343 $this->assertEquals(t('Setting not set'), human_bytes(''));
344 }
345
346 /**
347 * Test get_max_upload_size with formatting
348 */
349 public function testGetMaxUploadSize()
350 {
351 $this->assertEquals('1'. t('MiB'), get_max_upload_size(2097152, '1024k'));
352 $this->assertEquals('1'. t('MiB'), get_max_upload_size('1m', '2m'));
353 $this->assertEquals('100'. t('B'), get_max_upload_size(100, 100));
354 }
355
356 /**
357 * Test get_max_upload_size without formatting
358 */
359 public function testGetMaxUploadSizeRaw()
360 {
361 $this->assertEquals('1048576', get_max_upload_size(2097152, '1024k', false));
362 $this->assertEquals('1048576', get_max_upload_size('1m', '2m', false));
363 $this->assertEquals('100', get_max_upload_size(100, 100, false));
364 }
365
366 /**
367 * Test alphabetical_sort by value, not reversed, with php-intl.
368 */
369 public function testAlphabeticalSortByValue()
370 {
371 $arr = [
372 'zZz',
373 'éee',
374 'éae',
375 'eee',
376 'A',
377 'a',
378 'zzz',
379 ];
380 $expected = [
381 'a',
382 'A',
383 'éae',
384 'eee',
385 'éee',
386 'zzz',
387 'zZz',
388 ];
389
390 alphabetical_sort($arr);
391 $this->assertEquals($expected, $arr);
392 }
393
394 /**
395 * Test alphabetical_sort by value, reversed, with php-intl.
396 */
397 public function testAlphabeticalSortByValueReversed()
398 {
399 $arr = [
400 'zZz',
401 'éee',
402 'éae',
403 'eee',
404 'A',
405 'a',
406 'zzz',
407 ];
408 $expected = [
409 'zZz',
410 'zzz',
411 'éee',
412 'eee',
413 'éae',
414 'A',
415 'a',
416 ];
417
418 alphabetical_sort($arr, true);
419 $this->assertEquals($expected, $arr);
420 }
421
422 /**
423 * Test alphabetical_sort by keys, not reversed, with php-intl.
424 */
425 public function testAlphabeticalSortByKeys()
426 {
427 $arr = [
428 'zZz' => true,
429 'éee' => true,
430 'éae' => true,
431 'eee' => true,
432 'A' => true,
433 'a' => true,
434 'zzz' => true,
435 ];
436 $expected = [
437 'a' => true,
438 'A' => true,
439 'éae' => true,
440 'eee' => true,
441 'éee' => true,
442 'zzz' => true,
443 'zZz' => true,
444 ];
445
446 alphabetical_sort($arr, true, true);
447 $this->assertEquals($expected, $arr);
448 }
449
450 /**
451 * Test alphabetical_sort by keys, reversed, with php-intl.
452 */
453 public function testAlphabeticalSortByKeysReversed()
454 {
455 $arr = [
456 'zZz' => true,
457 'éee' => true,
458 'éae' => true,
459 'eee' => true,
460 'A' => true,
461 'a' => true,
462 'zzz' => true,
463 ];
464 $expected = [
465 'zZz' => true,
466 'zzz' => true,
467 'éee' => true,
468 'eee' => true,
469 'éae' => true,
470 'A' => true,
471 'a' => true,
472 ];
473
474 alphabetical_sort($arr, true, true);
475 $this->assertEquals($expected, $arr);
476 }
477 }