Extend tests for MD_STD_IN considerably, fix some edge cases
This commit is contained in:
@ -12,29 +12,84 @@ use PHPUnit\Framework\TestCase;
|
||||
* Tests for MD_STD_IN.
|
||||
*/
|
||||
final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Data provider for valid IDs.
|
||||
*
|
||||
* @return array<array{0: mixed, 1: int}>
|
||||
*/
|
||||
public static function valid_id_provider():array {
|
||||
|
||||
$values = [
|
||||
[1, 1],
|
||||
["1", 1],
|
||||
["1111111", 1111111],
|
||||
];
|
||||
|
||||
$output = [];
|
||||
foreach ($values as $value) {
|
||||
$output[gettype($value[0]) . ': ' . var_export($value[0], true)] = $value;
|
||||
}
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_id().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_id
|
||||
* @dataProvider \MD_STD_IN_Test::valid_id_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param integer $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_id():void {
|
||||
public function test_sanitize_id_works(mixed $to_validate, int $expected):void {
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_id($to_validate));
|
||||
}
|
||||
|
||||
self::assertEquals(1, MD_STD_IN::sanitize_id(1));
|
||||
self::assertEquals(1, MD_STD_IN::sanitize_id("1"));
|
||||
/**
|
||||
* Data provider for valid IDs.
|
||||
*
|
||||
* @return array<array{0: mixed}>
|
||||
*/
|
||||
public static function invalid_id_provider():array {
|
||||
|
||||
$output = self::invalid_id_provider_without_zero();
|
||||
$output['Number 0'] = [0];
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_id().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_id
|
||||
* @dataProvider \MD_STD_IN_Test::invalid_id_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_id_fails(mixed $to_validate):void {
|
||||
|
||||
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");
|
||||
MD_STD_IN::sanitize_id($to_validate);
|
||||
|
||||
}
|
||||
/**
|
||||
* Data provider for valid IDs.
|
||||
*
|
||||
* @return array<array{0: mixed, 1: int}>
|
||||
*/
|
||||
public static function valid_id_or_zero_provider():array {
|
||||
|
||||
$output = self::valid_id_provider();
|
||||
$output['Integer 0'] = [0, 0];
|
||||
$output['String 0'] = [0, 0];
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
@ -43,25 +98,71 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_id_or_zero
|
||||
* @dataProvider \MD_STD_IN_Test::valid_id_or_zero_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param integer $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_id_or_zero():void {
|
||||
public function test_sanitize_id_or_zero_works(mixed $to_validate, int $expected):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::assertEquals($expected, MD_STD_IN::sanitize_id_or_zero($to_validate));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for valid IDs.
|
||||
*
|
||||
* @return array<array{0: mixed}>
|
||||
*/
|
||||
public static function invalid_id_provider_without_zero():array {
|
||||
|
||||
return [
|
||||
'Number too high' => [1000000000000000000000000000000000000],
|
||||
'Number with character in the middle' => ["1a2"],
|
||||
'Number with suffixed string' => ["12a"],
|
||||
'String character' => ["a"],
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_id_or_zero().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_id_or_zero
|
||||
* @dataProvider \MD_STD_IN_Test::invalid_id_provider_without_zero
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_id_or_zero_fails(mixed $to_validate):void {
|
||||
|
||||
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");
|
||||
MD_STD_IN::sanitize_id_or_zero($to_validate);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for text with its expected cleaned values.
|
||||
*
|
||||
* @return array<array{0: mixed, 1: string}>
|
||||
*/
|
||||
public static function text_with_expected_return_value_provider():array {
|
||||
|
||||
return [
|
||||
'Empty string' => ['', ''],
|
||||
'Integer 0' => [0, '0'],
|
||||
'String 0' => ['0', '0'],
|
||||
'Regular string' => ['a', 'a'],
|
||||
'String with double whitespace in between' => ['a a', 'a a'],
|
||||
'String to be trimmed (spaces)' => ['a ', 'a'],
|
||||
'String to be trimmed (newline)' => ['a ' . PHP_EOL, 'a'],
|
||||
'Empty array' => [[], ''],
|
||||
'Array with content' => [['test' => 'test'], ''],
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
@ -70,16 +171,57 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_text
|
||||
* @dataProvider \MD_STD_IN_Test::text_with_expected_return_value_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param string $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_text():void {
|
||||
public function test_sanitize_text(mixed $to_validate, string $expected):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([]));
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_text($to_validate));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for working RGB colors.
|
||||
*
|
||||
* @return array<array{0: mixed, 1: string}>
|
||||
*/
|
||||
public static function valid_rgb_colors_provider():array {
|
||||
|
||||
return [
|
||||
'Three character version' => ['AAA', 'AAA'],
|
||||
'Three character version (int)' => ['111', '111'],
|
||||
'Three character version (mixed)' => ['1a1', '1A1'],
|
||||
'Six character version' => ['AAAAAA', 'AAAAAA'],
|
||||
'Six character version (int)' => ['111111', '111111'],
|
||||
'Six character version (mixed)' => ['1a1AAA', '1A1AAA'],
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for strings that are not rgb colors.
|
||||
*
|
||||
* @return array<array{0: mixed}>
|
||||
*/
|
||||
public static function invalid_rgb_colors_provider():array {
|
||||
|
||||
$output = [
|
||||
'Array' => [[]],
|
||||
'Three characters, but invalid ones' => ['ZZZ'],
|
||||
'Six characters, but invalid ones' => ['111ZZZ'],
|
||||
'Three characters, but with spaces' => ['ZZZ '],
|
||||
];
|
||||
|
||||
for ($i = 0; $i++; $i < 10) {
|
||||
if ($i === 3 || $i === 6) continue;
|
||||
$output['Valid characters repeated ' . $i . ' times'] = [$i];
|
||||
}
|
||||
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
@ -88,22 +230,34 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_rgb_color
|
||||
* @dataProvider \MD_STD_IN_Test::valid_rgb_colors_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param string $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_rgb_color():void {
|
||||
public function test_sanitize_rgb_color_works(mixed $to_validate, string $expected):void {
|
||||
|
||||
self::assertEquals("AAA", MD_STD_IN::sanitize_rgb_color("AAA"));
|
||||
self::assertEquals("000", MD_STD_IN::sanitize_rgb_color("000"));
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_rgb_color($to_validate));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_rgb_color()'s failure modes.
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_rgb_color
|
||||
* @dataProvider \MD_STD_IN_Test::invalid_rgb_colors_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_rgb_color_fails(mixed $to_validate):void {
|
||||
|
||||
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");
|
||||
MD_STD_IN::sanitize_rgb_color($to_validate);
|
||||
|
||||
}
|
||||
|
||||
@ -199,21 +353,47 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_url():void {
|
||||
public function test_sanitize_url_with_empty_string():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/Београд"));
|
||||
/**
|
||||
* Function for testing sanitize_url().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_url
|
||||
* @dataProvider \MD_STD_TEST_PROVIDERS::valid_url_provider
|
||||
*
|
||||
* @param string $to_validate Input to validate.
|
||||
* @param string $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_url_works(string $to_validate, string $expected):void {
|
||||
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_url($to_validate));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_url().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_url
|
||||
* @dataProvider \MD_STD_TEST_PROVIDERS::invalid_url_provider
|
||||
*
|
||||
* @param string $to_validate Input to validate.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_url_fails(string $to_validate):void {
|
||||
|
||||
self::expectException(MDInvalidUrl::class);
|
||||
MD_STD_IN::sanitize_url("h ttps://www.museum-digital.org");
|
||||
MD_STD_IN::sanitize_url($to_validate);
|
||||
|
||||
}
|
||||
|
||||
@ -225,13 +405,43 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_email():void {
|
||||
public function test_sanitize_email_empty():void {
|
||||
|
||||
self::assertEquals("", MD_STD_IN::sanitize_email(""));
|
||||
self::assertEquals("test@example.org", MD_STD_IN::sanitize_email("test@example.org"));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_email().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_email
|
||||
* @dataProvider \MD_STD_TEST_PROVIDERS::valid_email_provider
|
||||
*
|
||||
* @param string $to_validate Input to validate.
|
||||
* @param string $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_email_works(string $to_validate, string $expected):void {
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_email($to_validate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_email() fails when it should.
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_email
|
||||
* @dataProvider \MD_STD_TEST_PROVIDERS::invalid_email_provider
|
||||
*
|
||||
* @param string $to_validate Input to validate.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_email_fails(string $to_validate):void {
|
||||
|
||||
self::expectException(MDInvalidEmail::class);
|
||||
MD_STD_IN::sanitize_email("test");
|
||||
MD_STD_IN::sanitize_email($to_validate);
|
||||
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ declare(strict_types = 1);
|
||||
|
||||
// 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', __DIR__ . '/../../MDErrorReporter/exceptions/page']) as $classDir) {
|
||||
foreach (array_merge([__DIR__ . '/../tests', __DIR__ . '/../src', __DIR__ . '/../src/testing', __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")) {
|
||||
include "$classDir/$className.php";
|
||||
|
Reference in New Issue
Block a user