2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2020-03-13 09:23:54 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2020-03-13 09:23:54 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
2018-11-05 22:30:18 +00:00
|
|
|
require_once "core/polyfills.php";
|
2018-11-05 20:53:58 +00:00
|
|
|
|
2020-03-13 09:23:54 +00:00
|
|
|
class PolyfillsTest extends TestCase
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_html_escape(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2020-10-24 12:46:49 +00:00
|
|
|
"Foo & <main>",
|
|
|
|
html_escape("Foo & <main>")
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
2018-11-05 20:53:58 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->assertEquals(
|
2020-10-24 12:46:49 +00:00
|
|
|
"Foo & <main>",
|
|
|
|
html_unescape("Foo & <main>")
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
2018-11-05 20:53:58 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$x = "Foo & <waffles>";
|
|
|
|
$this->assertEquals(html_escape(html_unescape($x)), $x);
|
|
|
|
}
|
2018-11-05 20:53:58 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_int_escape(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-10-24 12:46:49 +00:00
|
|
|
$this->assertEquals(0, int_escape(""));
|
|
|
|
$this->assertEquals(1, int_escape("1"));
|
|
|
|
$this->assertEquals(-1, int_escape("-1"));
|
|
|
|
$this->assertEquals(-1, int_escape("-1.5"));
|
|
|
|
$this->assertEquals(0, int_escape(null));
|
2020-01-28 00:47:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_url_escape(): void
|
2020-01-28 00:47:30 +00:00
|
|
|
{
|
2020-10-24 12:46:49 +00:00
|
|
|
$this->assertEquals("%5E%5Co%2F%5E", url_escape("^\o/^"));
|
|
|
|
$this->assertEquals("", url_escape(null));
|
2020-01-28 00:47:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_bool_escape(): void
|
2020-01-28 00:47:30 +00:00
|
|
|
{
|
|
|
|
$this->assertTrue(bool_escape(true));
|
|
|
|
$this->assertFalse(bool_escape(false));
|
|
|
|
|
2024-06-19 14:12:07 +00:00
|
|
|
$this->assertFalse(bool_escape(null));
|
|
|
|
|
2020-01-28 00:47:30 +00:00
|
|
|
$this->assertTrue(bool_escape("true"));
|
|
|
|
$this->assertFalse(bool_escape("false"));
|
|
|
|
|
|
|
|
$this->assertTrue(bool_escape("t"));
|
|
|
|
$this->assertFalse(bool_escape("f"));
|
|
|
|
|
|
|
|
$this->assertTrue(bool_escape("T"));
|
|
|
|
$this->assertFalse(bool_escape("F"));
|
|
|
|
|
|
|
|
$this->assertTrue(bool_escape("yes"));
|
|
|
|
$this->assertFalse(bool_escape("no"));
|
|
|
|
|
|
|
|
$this->assertTrue(bool_escape("Yes"));
|
|
|
|
$this->assertFalse(bool_escape("No"));
|
|
|
|
|
|
|
|
$this->assertTrue(bool_escape("on"));
|
|
|
|
$this->assertFalse(bool_escape("off"));
|
|
|
|
|
|
|
|
$this->assertTrue(bool_escape(1));
|
|
|
|
$this->assertFalse(bool_escape(0));
|
2020-01-29 20:20:17 +00:00
|
|
|
|
|
|
|
$this->assertTrue(bool_escape("1"));
|
|
|
|
$this->assertFalse(bool_escape("0"));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2018-11-05 20:53:58 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_clamp(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-06-27 14:42:42 +00:00
|
|
|
$this->assertEquals(5, clamp(0, 5, 10)); // too small
|
|
|
|
$this->assertEquals(5, clamp(5, 5, 10)); // lower limit
|
|
|
|
$this->assertEquals(7, clamp(7, 5, 10)); // ok
|
|
|
|
$this->assertEquals(10, clamp(10, 5, 10)); // upper limit
|
|
|
|
$this->assertEquals(10, clamp(15, 5, 10)); // too large
|
|
|
|
$this->assertEquals(0, clamp(0, null, 10)); // no lower limit
|
|
|
|
$this->assertEquals(10, clamp(10, 0, null)); // no upper limit
|
|
|
|
$this->assertEquals(42, clamp(42, null, null)); // no limit
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2018-11-05 20:53:58 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_truncate(): void
|
2020-01-28 00:47:30 +00:00
|
|
|
{
|
2024-02-20 11:21:24 +00:00
|
|
|
$this->assertEquals("test words", truncate("test words", 10), "No truncation if string is short enough");
|
|
|
|
$this->assertEquals("test...", truncate("test words", 9), "Truncate when string is too long");
|
|
|
|
$this->assertEquals("test...", truncate("test words", 7), "Truncate to the same breakpoint");
|
|
|
|
$this->assertEquals("te...", truncate("test words", 5), "Breakpoints past the limit don't matter");
|
|
|
|
$this->assertEquals("o...", truncate("oneVeryLongWord", 4), "Hard-break if there are no breakpoints");
|
2020-01-28 00:47:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_to_shorthand_int(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-08 19:35:25 +00:00
|
|
|
// 0-9 should have 1 decimal place, 10+ should have none
|
2020-10-24 12:46:49 +00:00
|
|
|
$this->assertEquals("1.1GB", to_shorthand_int(1231231231));
|
2024-01-08 19:35:25 +00:00
|
|
|
$this->assertEquals("10KB", to_shorthand_int(10240));
|
|
|
|
$this->assertEquals("9.2KB", to_shorthand_int(9440));
|
2020-10-24 12:46:49 +00:00
|
|
|
$this->assertEquals("2", to_shorthand_int(2));
|
2020-02-01 23:03:23 +00:00
|
|
|
}
|
2018-11-05 20:53:58 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_parse_shorthand_int(): void
|
2020-02-01 23:03:23 +00:00
|
|
|
{
|
2020-10-24 12:46:49 +00:00
|
|
|
$this->assertEquals(-1, parse_shorthand_int("foo"));
|
|
|
|
$this->assertEquals(33554432, parse_shorthand_int("32M"));
|
|
|
|
$this->assertEquals(44441, parse_shorthand_int("43.4KB"));
|
|
|
|
$this->assertEquals(1231231231, parse_shorthand_int("1231231231"));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-07-05 16:07:27 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_format_milliseconds(): void
|
2020-02-01 23:03:23 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals("", format_milliseconds(5));
|
|
|
|
$this->assertEquals("5s", format_milliseconds(5000));
|
|
|
|
$this->assertEquals("1y 213d 16h 53m 20s", format_milliseconds(50000000000));
|
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_parse_to_milliseconds(): void
|
2020-10-26 13:45:25 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals(10, parse_to_milliseconds("10"));
|
|
|
|
$this->assertEquals(5000, parse_to_milliseconds("5s"));
|
|
|
|
$this->assertEquals(50000000000, parse_to_milliseconds("1y 213d 16h 53m 20s"));
|
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_autodate(): void
|
2020-01-29 00:49:21 +00:00
|
|
|
{
|
2020-01-28 00:47:30 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
"<time datetime='2012-06-23T16:14:22+00:00'>June 23, 2012; 16:14</time>",
|
|
|
|
autodate("2012-06-23 16:14:22")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_validate_input(): void
|
2020-01-29 00:49:21 +00:00
|
|
|
{
|
2020-01-28 00:47:30 +00:00
|
|
|
$_POST = [
|
|
|
|
"foo" => " bar ",
|
|
|
|
"to_null" => " ",
|
|
|
|
"num" => "42",
|
|
|
|
];
|
|
|
|
$this->assertEquals(
|
2023-11-11 21:49:12 +00:00
|
|
|
["foo" => "bar"],
|
|
|
|
validate_input(["foo" => "string,trim,lower"])
|
2020-01-28 00:47:30 +00:00
|
|
|
);
|
|
|
|
//$this->assertEquals(
|
|
|
|
// ["to_null"=>null],
|
|
|
|
// validate_input(["to_null"=>"string,trim,nullify"])
|
|
|
|
//);
|
|
|
|
$this->assertEquals(
|
2023-11-11 21:49:12 +00:00
|
|
|
["num" => 42],
|
|
|
|
validate_input(["num" => "int"])
|
2020-01-28 00:47:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_sanitize_path(): void
|
2019-07-05 16:07:27 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
"one",
|
|
|
|
sanitize_path("one")
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"one".DIRECTORY_SEPARATOR."two",
|
|
|
|
sanitize_path("one\\two")
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"one".DIRECTORY_SEPARATOR."two",
|
|
|
|
sanitize_path("one/two")
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"one".DIRECTORY_SEPARATOR."two",
|
|
|
|
sanitize_path("one\\\\two")
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"one".DIRECTORY_SEPARATOR."two",
|
|
|
|
sanitize_path("one//two")
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"one".DIRECTORY_SEPARATOR."two",
|
|
|
|
sanitize_path("one\\\\\\two")
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"one".DIRECTORY_SEPARATOR."two",
|
|
|
|
sanitize_path("one///two")
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
DIRECTORY_SEPARATOR."one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR,
|
|
|
|
sanitize_path("\\/one/\\/\\/two\\/")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_join_path(): void
|
2019-07-05 16:07:27 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
"one",
|
|
|
|
join_path("one")
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"one".DIRECTORY_SEPARATOR."two",
|
2019-09-29 13:30:55 +00:00
|
|
|
join_path("one", "two")
|
2019-07-05 16:07:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three",
|
2019-09-29 13:30:55 +00:00
|
|
|
join_path("one", "two", "three")
|
2019-07-05 16:07:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three",
|
2019-09-29 13:30:55 +00:00
|
|
|
join_path("one/two", "three")
|
2019-07-05 16:07:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
DIRECTORY_SEPARATOR."one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three".DIRECTORY_SEPARATOR,
|
2019-09-29 13:30:55 +00:00
|
|
|
join_path("\\/////\\\\one/\///"."\\//two\/\\//\\//", "//\/\\\/three/\\/\/")
|
2019-07-05 16:07:27 +00:00
|
|
|
);
|
|
|
|
}
|
2020-01-28 00:47:30 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_stringer(): void
|
2020-01-29 00:49:21 +00:00
|
|
|
{
|
2020-01-28 00:47:30 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
'["foo"=>"bar", "baz"=>[1, 2, 3], "qux"=>["a"=>"b"]]',
|
2023-11-11 21:49:12 +00:00
|
|
|
stringer(["foo" => "bar", "baz" => [1,2,3], "qux" => ["a" => "b"]])
|
2020-01-28 00:47:30 +00:00
|
|
|
);
|
|
|
|
}
|
2024-01-04 15:07:07 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_ip_in_range(): void
|
2024-01-04 15:07:07 +00:00
|
|
|
{
|
|
|
|
$this->assertTrue(ip_in_range("1.2.3.4", "1.2.0.0/16"));
|
|
|
|
$this->assertFalse(ip_in_range("4.3.2.1", "1.2.0.0/16"));
|
|
|
|
|
|
|
|
// A single IP should be interpreted as a /32
|
|
|
|
$this->assertTrue(ip_in_range("1.2.3.4", "1.2.3.4"));
|
|
|
|
}
|
2024-01-05 15:30:32 +00:00
|
|
|
|
2024-01-15 14:31:51 +00:00
|
|
|
public function test_deltree(): void
|
2024-01-05 15:30:32 +00:00
|
|
|
{
|
|
|
|
$tmp = sys_get_temp_dir();
|
|
|
|
$dir = "$tmp/test_deltree";
|
|
|
|
mkdir($dir);
|
|
|
|
file_put_contents("$dir/foo", "bar");
|
|
|
|
mkdir("$dir/baz");
|
|
|
|
file_put_contents("$dir/baz/.qux", "quux");
|
|
|
|
$this->assertTrue(file_exists($dir));
|
|
|
|
$this->assertTrue(file_exists("$dir/foo"));
|
|
|
|
$this->assertTrue(file_exists("$dir/baz"));
|
|
|
|
$this->assertTrue(file_exists("$dir/baz/.qux"));
|
|
|
|
deltree($dir);
|
|
|
|
$this->assertFalse(file_exists($dir));
|
|
|
|
}
|
2018-11-05 20:53:58 +00:00
|
|
|
}
|