Improve coverage of MD_STD_IN

This commit is contained in:
Joshua Ramon Enslin 2023-11-07 22:50:18 +01:00
parent 1f2f63c9af
commit 2c58e0554b
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
4 changed files with 373 additions and 8 deletions

View File

@ -3,10 +3,13 @@
backupGlobals="false" backupStaticAttributes="false" backupGlobals="false" backupStaticAttributes="false"
beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" beStrictAboutResourceUsageDuringSmallTests="true" beStrictAboutTodoAnnotatedTests="true" beStrictAboutCoversAnnotation="false" beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" beStrictAboutResourceUsageDuringSmallTests="true" beStrictAboutTodoAnnotatedTests="true" beStrictAboutCoversAnnotation="false"
bootstrap="tests/bootstrap.php" bootstrap="tests/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
cacheResult="false" cacheResult="false"
colors="true" colors="true"
convertErrorsToExceptions="true" convertDeprecationsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" convertErrorsToExceptions="true" convertDeprecationsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true"
enforceTimeLimit="true" enforceTimeLimit="true"
executionOrder="depends,defects"
failOnRisky="true"
failOnWarning="true" failOnWarning="true"
forceCoversAnnotation="false" forceCoversAnnotation="false"
processIsolation="true" processIsolation="true"
@ -19,4 +22,9 @@
<directory>tests/</directory> <directory>tests/</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit> </phpunit>

View File

@ -99,7 +99,8 @@ final class MD_STD_IN {
$output = \filter_var($input, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH); $output = \filter_var($input, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
if ($output === false if ($output === false
|| (preg_match('/^[a-zA-Z0-9]{3}$/', $output) === false && preg_match('/^[a-zA-Z0-9]{6}$/', $output) === false) || !in_array(strlen($output), [3, 6], true)
|| (preg_match('/^[a-fA-F0-9]{3}$/', $output) === false && preg_match('/^[a-fA-F0-9]{6}$/', $output) === false)
) { ) {
throw new MDInvalidColorCode("Invalid color code provided: " . $output); throw new MDInvalidColorCode("Invalid color code provided: " . $output);
} }
@ -243,8 +244,7 @@ final class MD_STD_IN {
return ""; return "";
} }
$output = \filter_var($input, FILTER_SANITIZE_EMAIL); if (($output = \filter_var($input, FILTER_VALIDATE_EMAIL)) === false) {
if (($output = \filter_var($output, FILTER_VALIDATE_EMAIL)) === false) {
throw new MDInvalidEmail("Invalid input email address"); throw new MDInvalidEmail("Invalid input email address");
} }
@ -321,7 +321,7 @@ final class MD_STD_IN {
* *
* @return float * @return float
*/ */
public static function validate_longitude(string|int $input):float { public static function validate_longitude(string|int|float $input):float {
if (is_string($input)) $output = self::sanitize_float($input); if (is_string($input)) $output = self::sanitize_float($input);
else $output = $input; else $output = $input;
@ -341,7 +341,7 @@ final class MD_STD_IN {
* *
* @return float * @return float
*/ */
public static function validate_latitude(string|int $input):float { public static function validate_latitude(string|int|float $input):float {
if (is_string($input)) $output = self::sanitize_float($input); if (is_string($input)) $output = self::sanitize_float($input);
else $output = $input; else $output = $input;

View File

@ -13,13 +13,193 @@ use PHPUnit\Framework\TestCase;
*/ */
final class MD_STD_IN_Test extends TestCase { final class MD_STD_IN_Test extends TestCase {
/** /**
* Function for testing sanitize_url(). * Function for testing sanitize_id().
* *
* @small * @small
* @covers \MD_STD_IN::sanitize_id
* *
* @return void * @return void
*/ */
public function testSanitizeUrlWorksBasically():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 // Ensure empty inputs return empty output
self::assertEquals("", MD_STD_IN::sanitize_url("")); self::assertEquals("", MD_STD_IN::sanitize_url(""));
@ -36,4 +216,181 @@ final class MD_STD_IN_Test extends TestCase {
MD_STD_IN::sanitize_url("h ttps://www.museum-digital.org"); 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', "ä")));
}
} }

View File

@ -12,7 +12,7 @@ declare(strict_types = 1);
// Try using class map as defined through /scripts/buildClassMap.php // Try using class map as defined through /scripts/buildClassMap.php
foreach (array_merge([__DIR__ . '/../tests', __DIR__ . '/../src', __DIR__ . '/../../MDErrorReporter', __DIR__ . '/../../MDErrorReporter/exceptions', __DIR__ . '/../../MDErrorReporter/exceptions/generic', __DIR__ . '/../../MDErrorReporter/exceptions/updates']) as $classDir) { foreach (array_merge([__DIR__ . '/../tests', __DIR__ . '/../src', __DIR__ . '/../../MDErrorReporter', __DIR__ . '/../../MDErrorReporter/exceptions', __DIR__ . '/../../MDErrorReporter/exceptions/generic', __DIR__ . '/../../MDErrorReporter/exceptions/updates', __DIR__ . '/../../MDErrorReporter/exceptions/page']) as $classDir) {
if (\file_exists("$classDir/$className.php")) { if (\file_exists("$classDir/$className.php")) {
include "$classDir/$className.php"; include "$classDir/$className.php";