added some phpunit tests and fixed address/domain detection in email validation.
This commit is contained in:
parent
8ef02b49cd
commit
e6482b1731
|
@ -8,7 +8,7 @@
|
||||||
<component name="PhpProjectSharedConfiguration" php_language_level="7" />
|
<component name="PhpProjectSharedConfiguration" php_language_level="7" />
|
||||||
<component name="PhpUnit">
|
<component name="PhpUnit">
|
||||||
<phpunit_settings>
|
<phpunit_settings>
|
||||||
<PhpUnitSettings />
|
<PhpUnitSettings load_method="PHPUNIT_PHAR" custom_loader_path="" phpunit_phar_path="C:/php7/pear/phpunit-5.3.4.phar" />
|
||||||
</phpunit_settings>
|
</phpunit_settings>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -19,16 +19,17 @@ class Validate
|
||||||
$results = true;
|
$results = true;
|
||||||
$email = trim($email); // sanitize before validation
|
$email = trim($email); // sanitize before validation
|
||||||
$split = explode('@', $email);
|
$split = explode('@', $email);
|
||||||
|
$address = $split[count($split)-1];
|
||||||
|
|
||||||
// Whitelisted?
|
// Whitelisted?
|
||||||
if(isset($lists['whitelist'])) {
|
if(isset($lists['whitelist'])) {
|
||||||
if(in_array($email, $lists['whitelist']) || in_array($split[1], $lists['whitelist']))
|
if(in_array($email, $lists['whitelist']) || in_array($address, $lists['whitelist']))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blacklisted?
|
// Blacklisted?
|
||||||
if(isset($lists['blacklist'])) {
|
if(isset($lists['blacklist'])) {
|
||||||
if(in_array($email, $lists['blacklist']) || in_array($split[1], $lists['blacklist']))
|
if(in_array($email, $lists['blacklist']) || in_array($address, $lists['blacklist']))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,8 +44,8 @@ class Validate
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for dns MX entry
|
// check for dns MX entry
|
||||||
if($results && $level >= 2) {
|
if($results && $level >= 2 && !Validate::ip($address)) {
|
||||||
$results = checkdnsrr($split[1], "MX");
|
$results = checkdnsrr($address, "MX");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
|
|
|
@ -0,0 +1,135 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once "Validate.php";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: Marcel 'CoNfu5eD Naeve <confu5ed@serious-pro.de>
|
||||||
|
* Date: 14.05.2016
|
||||||
|
* Time: 11:25
|
||||||
|
*/
|
||||||
|
class ValidateTest extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Validate::email(); Tests
|
||||||
|
*/
|
||||||
|
public function testEmail_myEmail_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de");
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_myEmail_level2()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de", 2);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_myEmail_level1_whitelisted()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de", 1,["whitelist"=>["confu5ed@serious-pro.de"]]);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_myEmail_level1_not_in_whitelist()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de", 1,["whitelist"=>["confu5ed@serious-pro.eu"]]);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_myEmail_level1_blacklisted()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de",1,["blacklist"=>["confu5ed@serious-pro.de"]]);
|
||||||
|
$this->assertFalse($result);
|
||||||
|
}
|
||||||
|
public function testEmail_myEmail_level1_not_in_blacklist()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de", 1,["blacklist"=>["confu5ed@serious-pro.eu"]]);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_myEmail_level2_whitelisted()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de", 2,["whitelist"=>["confu5ed@serious-pro.de"]]);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_myEmail_level2_not_in_whitelist()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de", 2,["whitelist"=>["confu5ed@serious-pro.eu"]]);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_myEmail_level2_blacklisted()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de", 2,["blacklist"=>["confu5ed@serious-pro.de"]]);
|
||||||
|
$this->assertFalse($result);
|
||||||
|
}
|
||||||
|
public function testEmail_myEmail_level2_not_in_blacklist()
|
||||||
|
{
|
||||||
|
$result = Validate::email("confu5ed@serious-pro.de", 2,["blacklist"=>["confu5ed@serious-pro.eu"]]);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEmail_random1_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email("tempus.lorem.fringilla@vitae.co.uk");
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_random2_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email("Etiam.ligula@et.com");
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_random3_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email("orci.tincidunt.adipiscing@famesacturpis.edu");
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_random4_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email("_______@example.com");
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_random5_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email("nonummy.Fusce.fermentum@ut.org");
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_random6_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email('"email"@example.com');
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_random7_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email('much."more\ unusual"@example.com');
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_random8_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email('very.unusual."@".unusual.com@example.com');
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_random9_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email('very."(),:;<>[]".VERY."very@\\"very".unusual@strange.example.com');
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
public function testEmail_random10_level2()
|
||||||
|
{
|
||||||
|
$result = Validate::email('very."(),:;<>[]".VERY."very@\\"very".unusual@serious-pro.de', 2);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEmail_invalid1_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email("me@me@serious-pro.de");
|
||||||
|
$this->assertFalse($result);
|
||||||
|
}
|
||||||
|
public function testEmail_invalid2_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email("me me@serious-pro.de");
|
||||||
|
$this->assertFalse($result);
|
||||||
|
}
|
||||||
|
public function testEmail_invalid3_level1()
|
||||||
|
{
|
||||||
|
$result = Validate::email("me@serious pro.de");
|
||||||
|
$this->assertFalse($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue