MD_STD/tests/MD_STD_IN_Test.php

397 lines
13 KiB
PHP

<?PHP
/**
* Tests for MD_STD_IN.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
/**
* Tests for MD_STD_IN.
*/
final class MD_STD_IN_Test extends TestCase {
/**
* Function for testing sanitize_id().
*
* @small
* @covers \MD_STD_IN::sanitize_id
*
* @return void
*/
public function test_sanitize_id():void {
self::assertEquals(1, MD_STD_IN::sanitize_id(1));
self::assertEquals(1, MD_STD_IN::sanitize_id("1"));
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id(0);
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id(100000000000000000000000000000);
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id("1a2");
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id("12a");
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id("a");
}
/**
* Function for testing sanitize_id_or_zero().
*
* @small
* @covers \MD_STD_IN::sanitize_id_or_zero
*
* @return void
*/
public function test_sanitize_id_or_zero():void {
self::assertEquals(0, MD_STD_IN::sanitize_id_or_zero(""));
self::assertEquals(0, MD_STD_IN::sanitize_id_or_zero(0));
self::assertEquals(0, MD_STD_IN::sanitize_id_or_zero("0"));
self::assertEquals(1, MD_STD_IN::sanitize_id_or_zero(1));
self::assertEquals(1, MD_STD_IN::sanitize_id_or_zero("1"));
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_or_zero(100000000000000000000000000000);
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_or_zero("1a2");
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_or_zero("12a");
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_or_zero("a");
}
/**
* Function for testing sanitize_text().
*
* @small
* @covers \MD_STD_IN::sanitize_text
*
* @return void
*/
public function test_sanitize_text():void {
self::assertEquals("", MD_STD_IN::sanitize_text(""));
self::assertEquals("a", MD_STD_IN::sanitize_text("a"));
self::assertEquals("a a", MD_STD_IN::sanitize_text("a a"));
self::assertEquals("a", MD_STD_IN::sanitize_text("a "));
self::assertEquals("", MD_STD_IN::sanitize_text([]));
}
/**
* Function for testing sanitize_rgb_color().
*
* @small
* @covers \MD_STD_IN::sanitize_rgb_color
*
* @return void
*/
public function test_sanitize_rgb_color():void {
self::assertEquals("AAA", MD_STD_IN::sanitize_rgb_color("AAA"));
self::assertEquals("000", MD_STD_IN::sanitize_rgb_color("000"));
self::expectException(MDInvalidColorCode::class);
MD_STD_IN::sanitize_rgb_color("ZZZaa2343422342342323");
self::expectException(MDInvalidColorCode::class);
MD_STD_IN::sanitize_rgb_color("ZZZaa");
self::expectException(MDInvalidColorCode::class);
MD_STD_IN::sanitize_rgb_color("ZZZ");
}
/**
* Function for testing sanitize_id_array().
*
* @small
* @covers \MD_STD_IN::sanitize_id_array
*
* @return void
*/
public function test_sanitize_id_array():void {
self::assertEquals([1], MD_STD_IN::sanitize_id_array([1]));
self::assertEquals([1, 2], MD_STD_IN::sanitize_id_array(["1", 2]));
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_array([0]);
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_array([0, 1]);
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_array([100000000000000000000000000000]);
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_array(["1a2"]);
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_array(["12a"]);
self::expectException(MDpageParameterNotNumericException::class);
MD_STD_IN::sanitize_id_array(["a"]);
}
/**
* Function for testing get_http_input_text().
*
* @small
* @covers \MD_STD_IN::get_http_input_text
*
* @return void
*/
public function test_get_http_input_text():void {
$_GET['test'] = "a";
self::assertEquals("a", MD_STD_IN::get_http_input_text("test"));
unset($_GET['test']);
$_POST['test'] = "a";
self::assertEquals("a", MD_STD_IN::get_http_input_text("test"));
unset($_POST['test']);
$_POST['test'] = [];
self::assertEquals("", MD_STD_IN::get_http_input_text("test"));
unset($_POST['test']);
self::assertEquals("", MD_STD_IN::get_http_input_text("test"));
self::expectException(MDpageParameterNotFromListException::class);
MD_STD_IN::get_http_input_text("a", "", ['a']);
}
/**
* Function for testing get_http_post_text().
*
* @small
* @covers \MD_STD_IN::get_http_post_text
*
* @return void
*/
public function test_get_http_post_text():void {
$_POST['test'] = "a";
self::assertEquals("a", MD_STD_IN::get_http_post_text("test"));
unset($_POST['test']);
$_POST['test'] = [];
self::assertEquals("", MD_STD_IN::get_http_post_text("test"));
unset($_POST['test']);
self::assertEquals("", MD_STD_IN::get_http_post_text("test"));
self::expectException(MDpageParameterNotFromListException::class);
MD_STD_IN::get_http_post_text("a", "", ['a']);
}
/**
* Function for testing sanitize_url().
*
* @small
* @covers \MD_STD_IN::sanitize_url
*
* @return void
*/
public function test_sanitize_url():void {
// Ensure empty inputs return empty output
self::assertEquals("", MD_STD_IN::sanitize_url(""));
self::assertEquals("https://www.museum-digital.org", MD_STD_IN::sanitize_url("https://www.museum-digital.org"));
// Ensure that cyrillic characters are accepted
self::assertEquals("https://sr.wikipedia.org/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4", MD_STD_IN::sanitize_url("https://sr.wikipedia.org/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4"));
self::assertEquals("https://sr.wikipedia.org/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4", MD_STD_IN::sanitize_url("https://sr.wikipedia.org/wiki/Београд"));
self::assertEquals("https://username:password@sr.wikipedia.org:9000/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4", MD_STD_IN::sanitize_url("https://username:password@sr.wikipedia.org:9000/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4"));
self::assertEquals("https://username:password@sr.wikipedia.org:9000/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4", MD_STD_IN::sanitize_url("https://username:password@sr.wikipedia.org:9000/wiki/Београд"));
self::expectException(MDInvalidUrl::class);
MD_STD_IN::sanitize_url("h ttps://www.museum-digital.org");
}
/**
* Function for testing sanitize_email().
*
* @small
* @covers \MD_STD_IN::sanitize_email
*
* @return void
*/
public function test_sanitize_email():void {
self::assertEquals("", MD_STD_IN::sanitize_email(""));
self::assertEquals("test@example.org", MD_STD_IN::sanitize_email("test@example.org"));
self::expectException(MDInvalidEmail::class);
MD_STD_IN::sanitize_email("test");
}
/**
* Function for testing validate_password().
*
* @small
* @covers \MD_STD_IN::validate_password
*
* @return void
*/
public function test_validate_password():void {
self::assertEquals(['password_too_short', 'password_has_no_number_no_special_char'], MD_STD_IN::validate_password("a"));
self::assertEquals(['password_has_no_number_no_special_char'], MD_STD_IN::validate_password("aaaaaaaaaaaaaaaaaaaa"));
self::assertEquals(['password_too_short'], MD_STD_IN::validate_password("!a323!"));
self::assertEquals([], MD_STD_IN::validate_password("!a324324324123!"));
}
/**
* Function for testing validate_phone_number().
*
* @small
* @covers \MD_STD_IN::validate_phone_number
*
* @return void
*/
public function test_validate_phone_number():void {
self::assertEquals("", MD_STD_IN::validate_phone_number(""));
self::assertEquals("+1932-1321123", MD_STD_IN::validate_phone_number("+1932-1321123"));
self::assertEquals("+49 (030) 21321123", MD_STD_IN::validate_phone_number("+49 (030) 21321123"));
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::validate_phone_number("test@example.org");
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::validate_phone_number("+123456789 z");
}
/**
* Function for testing sanitize_float().
*
* @small
* @covers \MD_STD_IN::sanitize_float
*
* @return void
*/
public function test_sanitize_float():void {
self::assertEquals(0, MD_STD_IN::sanitize_float("0"));
self::assertEquals(12, MD_STD_IN::sanitize_float("12"));
self::assertEquals(12.12, MD_STD_IN::sanitize_float("12.12"));
self::assertEquals(12.12, MD_STD_IN::sanitize_float("12,12"));
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::sanitize_float("test");
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::sanitize_float("");
}
/**
* Function for testing validate_longitude().
*
* @small
* @covers \MD_STD_IN::validate_longitude
*
* @return void
*/
public function test_validate_longitude():void {
self::assertEquals(0, MD_STD_IN::validate_longitude("0"));
self::assertEquals(12, MD_STD_IN::validate_longitude("12"));
self::assertEquals(12, MD_STD_IN::validate_longitude(12));
self::assertEquals(12.12, MD_STD_IN::validate_longitude("12.12"));
self::assertEquals(12.12, MD_STD_IN::validate_longitude("12,12"));
self::assertEquals(12.12, MD_STD_IN::validate_longitude(12.12));
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::validate_longitude("test");
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::validate_longitude("");
self::expectException(MDCoordinateOutOfRange::class);
MD_STD_IN::validate_longitude(1900);
self::expectException(MDCoordinateOutOfRange::class);
MD_STD_IN::validate_longitude(-1900);
}
/**
* Function for testing validate_latitude().
*
* @small
* @covers \MD_STD_IN::validate_latitude
*
* @return void
*/
public function test_validate_latitude():void {
self::assertEquals(0, MD_STD_IN::validate_latitude("0"));
self::assertEquals(12, MD_STD_IN::validate_latitude("12"));
self::assertEquals(12, MD_STD_IN::validate_latitude(12));
self::assertEquals(12.12, MD_STD_IN::validate_latitude("12.12"));
self::assertEquals(12.12, MD_STD_IN::validate_latitude("12,12"));
self::assertEquals(12.12, MD_STD_IN::validate_latitude(12.12));
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::validate_latitude("test");
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::validate_latitude("");
self::expectException(MDCoordinateOutOfRange::class);
MD_STD_IN::validate_latitude(1900);
self::expectException(MDCoordinateOutOfRange::class);
MD_STD_IN::validate_latitude(-1900);
}
/**
* Function for testing validate_isbn().
*
* @small
* @covers \MD_STD_IN::validate_isbn
*
* @return void
*/
public function test_validate_isbn():void {
self::assertEquals("", MD_STD_IN::validate_isbn(""));
self::assertEquals("0943396042", MD_STD_IN::validate_isbn("0943396042"));
self::assertEquals("0943396042", MD_STD_IN::validate_isbn("0-943396-04-2"));
self::assertEquals("094339604X", MD_STD_IN::validate_isbn("0-943396-04-X"));
self::assertEquals("1230943396042", MD_STD_IN::validate_isbn("1230943396042"));
self::assertEquals("1230943396042", MD_STD_IN::validate_isbn("1230-943396-04-2"));
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::validate_isbn("X094339604");
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::validate_isbn("094339604a");
}
/**
* Function for testing ensureStringIsUtf8().
*
* @small
* @covers \MD_STD_IN::ensureStringIsUtf8
*
* @return void
*/
public function test_ensureStringIsUtf8():void {
self::assertEquals("ä", MD_STD_IN::ensureStringIsUtf8("ä"));
self::assertEquals("ä", MD_STD_IN::ensureStringIsUtf8(iconv("UTF-8", 'ISO-8859-1//TRANSLIT', "ä")));
self::assertEquals("a", MD_STD_IN::ensureStringIsUtf8(iconv("UTF-8", 'ISO-2022-JP//TRANSLIT', "ä")));
}
}