added first validation tests

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2019-06-05 09:07:21 +02:00
parent 13bfd62ac5
commit 4917b9c057
3 changed files with 104 additions and 5 deletions

View File

@@ -35,8 +35,8 @@ class Validate
}
// Check if the $str is one of the values which represent the default for an 'empty' value
if (is_array($emptydefault) && ! empty($emptydefault) && in_array($str, $emptydefault) && isset($emptydefault[0])) {
return $emptydefault[0];
if (is_array($emptydefault) && ! empty($emptydefault) && in_array($str, $emptydefault)) {
return $str;
}
if ($pattern == '') {
@@ -61,7 +61,6 @@ class Validate
}
\Froxlor\UI\Response::standard_error($lng, $fieldname, $throw_exception);
exit();
}
/**
@@ -99,7 +98,6 @@ class Validate
return false;
} else {
\Froxlor\UI\Response::standard_error($lng, $ip, $throw_exception);
exit();
}
}
@@ -118,7 +116,6 @@ class Validate
return false;
} else {
\Froxlor\UI\Response::standard_error($lng, $ip, $throw_exception);
exit();
}
}

View File

@@ -0,0 +1,100 @@
<?php
use PHPUnit\Framework\TestCase;
use Froxlor\Validate\Validate;
/**
*
* @covers \Froxlor\Validate\Validate
* @covers \Froxlor\UI\Response
* @covers \Froxlor\FroxlorLogger
*/
class ValidateTest extends TestCase
{
public function testValidate()
{
$teststr = Validate::validate("user input", "test-field", '', '', [], true);
$this->assertEquals("user input", $teststr);
}
public function testValidateStrInEmptyDefault()
{
$teststr = Validate::validate("user input", "test-field", '', '', [
"user test",
"user input",
"user bla"
], true);
$this->assertEquals("user input", $teststr);
}
public function testValidateEmptyDefaultNoArray()
{
$teststr = Validate::validate("user input", "test-field", '', '', "user input", true);
$this->assertEquals("user input", $teststr);
}
public function testValidateRemoveNotAllowedChar()
{
$teststr = Validate::validate("user " . PHP_EOL . "input", "test-field", '', '', [], true);
$this->assertEquals("user input", $teststr);
}
public function testValidateStringFormatError()
{
$this->expectException("Exception");
$this->expectExceptionCode(400);
Validate::validate("user input", "test-field", '/^[A-Z]+$/i', '', [], true);
}
public function testValidateIp()
{
$result = Validate::validate_ip2("12.34.56.78", false, 'invalidip', false, false, false, true);
$this->assertEquals("12.34.56.78", $result);
}
public function testValidateIpPrivNotAllowed()
{
$this->expectException("Exception");
$this->expectExceptionCode(400);
Validate::validate_ip2("10.0.0.1", false, 'invalidip', false, false, false, true);
}
public function testValidateIpPrivNotAllowedBool()
{
$result = Validate::validate_ip2("10.0.0.1", true, 'invalidip', false, false, false, true);
$this->assertFalse($result);
}
public function testValidateIpCidrNotAllowed()
{
$this->expectException("Exception");
$this->expectExceptionCode(400);
Validate::validate_ip2("12.34.56.78/24", false, 'invalidip', false, false, false, true);
}
public function testValidateIpCidrNotAllowedBool()
{
$result = Validate::validate_ip2("12.34.56.78/24", true, 'invalidip', false, false, false, true);
$this->assertFalse($result);
}
public function testValidateIpCidr()
{
$result = Validate::validate_ip2("12.34.56.78/24", false, 'invalidip', false, false, true, true);
$this->assertEquals("12.34.56.78/24", $result);
}
public function testValidateIpLocalhostAllowed()
{
$result = Validate::validate_ip2("127.0.0.1/32", false, 'invalidip', true, false, true, true);
$this->assertEquals("127.0.0.1/32", $result);
}
public function testValidateIpLocalhostAllowedWrongIp()
{
$this->expectException("Exception");
$this->expectExceptionCode(400);
Validate::validate_ip2("127.0.0.2", false, 'invalidip', true, false, false, true);
}
}

View File

@@ -148,6 +148,8 @@ $sel_stmt = Database::prepare("SELECT * FROM `" . TABLE_PANEL_ADMINS . "` WHERE
$admin_userdata = Database::pexecute_first($sel_stmt);
$admin_userdata['adminsession'] = 1;
$log = \Froxlor\FroxlorLogger::getInstanceOf($admin_userdata);
Settings::Set('panel.standardlanguage', 'English', true);
Settings::Set('panel.adminmail', 'admin@dev.froxlor.org', true);
Settings::Set('panel.allow_domain_change_admin', '1', true);