aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/UtilsTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/UtilsTest.php')
-rw-r--r--tests/UtilsTest.php286
1 files changed, 252 insertions, 34 deletions
diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php
index 6a7870c4..6cd37a7a 100644
--- a/tests/UtilsTest.php
+++ b/tests/UtilsTest.php
@@ -4,10 +4,7 @@
4 */ 4 */
5 5
6require_once 'application/Utils.php'; 6require_once 'application/Utils.php';
7require_once 'tests/utils/ReferenceSessionIdHashes.php'; 7require_once 'application/Languages.php';
8
9// Initialize reference data before PHPUnit starts a session
10ReferenceSessionIdHashes::genAllHashes();
11 8
12 9
13/** 10/**
@@ -15,22 +12,33 @@ ReferenceSessionIdHashes::genAllHashes();
15 */ 12 */
16class UtilsTest extends PHPUnit_Framework_TestCase 13class UtilsTest extends PHPUnit_Framework_TestCase
17{ 14{
18 // Session ID hashes
19 protected static $sidHashes = null;
20
21 // Log file 15 // Log file
22 protected static $testLogFile = 'tests.log'; 16 protected static $testLogFile = 'tests.log';
23 17
24 // Expected log date format 18 // Expected log date format
25 protected static $dateFormat = 'Y/m/d H:i:s'; 19 protected static $dateFormat = 'Y/m/d H:i:s';
26 20
21 /**
22 * @var string Save the current timezone.
23 */
24 protected static $defaultTimeZone;
27 25
28 /** 26 /**
29 * Assign reference data 27 * Assign reference data
30 */ 28 */
31 public static function setUpBeforeClass() 29 public static function setUpBeforeClass()
32 { 30 {
33 self::$sidHashes = ReferenceSessionIdHashes::getHashes(); 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);
34 } 42 }
35 43
36 /** 44 /**
@@ -204,53 +212,263 @@ class UtilsTest extends PHPUnit_Framework_TestCase
204 $this->assertEquals('?', generateLocation($ref, 'localhost')); 212 $this->assertEquals('?', generateLocation($ref, 'localhost'));
205 } 213 }
206 214
215
207 /** 216 /**
208 * Test is_session_id_valid with a valid ID - TEST ALL THE HASHES! 217 * Test generateSecretApi.
209 *
210 * This tests extensively covers all hash algorithms / bit representations
211 */ 218 */
212 public function testIsAnyHashSessionIdValid() 219 public function testGenerateSecretApi()
213 { 220 {
214 foreach (self::$sidHashes as $algo => $bpcs) { 221 $this->assertEquals(12, strlen(generate_api_secret('foo', 'bar')));
215 foreach ($bpcs as $bpc => $hash) {
216 $this->assertTrue(is_session_id_valid($hash));
217 }
218 }
219 } 222 }
220 223
221 /** 224 /**
222 * Test is_session_id_valid with a valid ID - SHA-1 hashes 225 * Test generateSecretApi with invalid parameters.
223 */ 226 */
224 public function testIsSha1SessionIdValid() 227 public function testGenerateSecretApiInvalid()
225 { 228 {
226 $this->assertTrue(is_session_id_valid(sha1('shaarli'))); 229 $this->assertFalse(generate_api_secret('', ''));
230 $this->assertFalse(generate_api_secret(false, false));
227 } 231 }
228 232
229 /** 233 /**
230 * Test is_session_id_valid with a valid ID - SHA-256 hashes 234 * Test normalize_spaces.
231 */ 235 */
232 public function testIsSha256SessionIdValid() 236 public function testNormalizeSpace()
233 { 237 {
234 $this->assertTrue(is_session_id_valid(hash('sha256', 'shaarli'))); 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));
235 } 243 }
236 244
237 /** 245 /**
238 * Test is_session_id_valid with a valid ID - SHA-512 hashes 246 * Test arrays_combine
239 */ 247 */
240 public function testIsSha512SessionIdValid() 248 public function testCartesianProductGenerator()
241 { 249 {
242 $this->assertTrue(is_session_id_valid(hash('sha512', 'shaarli'))); 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)));
243 } 262 }
244 263
245 /** 264 /**
246 * Test is_session_id_valid with invalid IDs. 265 * Test date_format() with invalid parameter.
247 */ 266 */
248 public function testIsSessionIdInvalid() 267 public function testDateFormatInvalid()
249 { 268 {
250 $this->assertFalse(is_session_id_valid('')); 269 $this->assertFalse(format_date([]));
251 $this->assertFalse(is_session_id_valid(array())); 270 $this->assertFalse(format_date(null));
252 $this->assertFalse( 271 }
253 is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=') 272
254 ); 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);
255 } 473 }
256} 474}