FilterTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. use PrivateBin\Filter;
  3. class FilterTest extends PHPUnit_Framework_TestCase
  4. {
  5. public function testFilterStripsSlashesDeeply()
  6. {
  7. $this->assertEquals(
  8. array("f'oo", "b'ar", array("fo'o", "b'ar")),
  9. Filter::stripslashesDeep(array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar")))
  10. );
  11. }
  12. public function testFilterMakesTimesHumanlyReadable()
  13. {
  14. $this->assertEquals('5 minutes', Filter::formatHumanReadableTime('5min'));
  15. $this->assertEquals('90 seconds', Filter::formatHumanReadableTime('90sec'));
  16. $this->assertEquals('1 week', Filter::formatHumanReadableTime('1week'));
  17. $this->assertEquals('6 months', Filter::formatHumanReadableTime('6months'));
  18. }
  19. /**
  20. * @expectedException Exception
  21. * @expectedExceptionCode 30
  22. */
  23. public function testFilterFailTimesHumanlyReadable()
  24. {
  25. Filter::formatHumanReadableTime('five_minutes');
  26. }
  27. public function testFilterMakesSizesHumanlyReadable()
  28. {
  29. $this->assertEquals('1 B', Filter::formatHumanReadableSize(1));
  30. $this->assertEquals('1 000 B', Filter::formatHumanReadableSize(1000));
  31. $this->assertEquals('1.00 KiB', Filter::formatHumanReadableSize(1024));
  32. $this->assertEquals('1.21 KiB', Filter::formatHumanReadableSize(1234));
  33. $exponent = 1024;
  34. $this->assertEquals('1 000.00 KiB', Filter::formatHumanReadableSize(1000 * $exponent));
  35. $this->assertEquals('1.00 MiB', Filter::formatHumanReadableSize(1024 * $exponent));
  36. $this->assertEquals('1.21 MiB', Filter::formatHumanReadableSize(1234 * $exponent));
  37. $exponent *= 1024;
  38. $this->assertEquals('1 000.00 MiB', Filter::formatHumanReadableSize(1000 * $exponent));
  39. $this->assertEquals('1.00 GiB', Filter::formatHumanReadableSize(1024 * $exponent));
  40. $this->assertEquals('1.21 GiB', Filter::formatHumanReadableSize(1234 * $exponent));
  41. $exponent *= 1024;
  42. $this->assertEquals('1 000.00 GiB', Filter::formatHumanReadableSize(1000 * $exponent));
  43. $this->assertEquals('1.00 TiB', Filter::formatHumanReadableSize(1024 * $exponent));
  44. $this->assertEquals('1.21 TiB', Filter::formatHumanReadableSize(1234 * $exponent));
  45. $exponent *= 1024;
  46. $this->assertEquals('1 000.00 TiB', Filter::formatHumanReadableSize(1000 * $exponent));
  47. $this->assertEquals('1.00 PiB', Filter::formatHumanReadableSize(1024 * $exponent));
  48. $this->assertEquals('1.21 PiB', Filter::formatHumanReadableSize(1234 * $exponent));
  49. $exponent *= 1024;
  50. $this->assertEquals('1 000.00 PiB', Filter::formatHumanReadableSize(1000 * $exponent));
  51. $this->assertEquals('1.00 EiB', Filter::formatHumanReadableSize(1024 * $exponent));
  52. $this->assertEquals('1.21 EiB', Filter::formatHumanReadableSize(1234 * $exponent));
  53. $exponent *= 1024;
  54. $this->assertEquals('1 000.00 EiB', Filter::formatHumanReadableSize(1000 * $exponent));
  55. $this->assertEquals('1.00 ZiB', Filter::formatHumanReadableSize(1024 * $exponent));
  56. $this->assertEquals('1.21 ZiB', Filter::formatHumanReadableSize(1234 * $exponent));
  57. $exponent *= 1024;
  58. $this->assertEquals('1 000.00 ZiB', Filter::formatHumanReadableSize(1000 * $exponent));
  59. $this->assertEquals('1.00 YiB', Filter::formatHumanReadableSize(1024 * $exponent));
  60. $this->assertEquals('1.21 YiB', Filter::formatHumanReadableSize(1234 * $exponent));
  61. }
  62. public function testSlowEquals()
  63. {
  64. $this->assertTrue(Filter::slowEquals('foo', 'foo'), 'same string');
  65. $this->assertFalse(Filter::slowEquals('foo', true), 'string and boolean');
  66. $this->assertFalse(Filter::slowEquals('foo', 0), 'string and integer');
  67. $this->assertFalse(Filter::slowEquals('123foo', 123), 'string and integer');
  68. $this->assertFalse(Filter::slowEquals('123foo', '123'), 'different strings');
  69. $this->assertFalse(Filter::slowEquals('6', ' 6'), 'strings with space');
  70. $this->assertFalse(Filter::slowEquals('4.2', '4.20'), 'floats as strings');
  71. $this->assertFalse(Filter::slowEquals('1e3', '1000'), 'integers as strings');
  72. $this->assertFalse(Filter::slowEquals('9223372036854775807', '9223372036854775808'), 'large integers as strings');
  73. $this->assertFalse(Filter::slowEquals('61529519452809720693702583126814', '61529519452809720000000000000000'), 'larger integers as strings');
  74. }
  75. }