MDL-21475 weblib: Simplify validate_email() to use existing function.
This commit is contained in:
+137
-10
@@ -484,19 +484,146 @@ EXPECTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for validate_email() function.
|
||||
* Data provider for validate_email() function.
|
||||
*
|
||||
* @return array Returns aray of test data for the test_validate_email function
|
||||
*/
|
||||
public function test_validate_email() {
|
||||
public function data_validate_email() {
|
||||
return [
|
||||
// Test addresses that should pass.
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => "but_potentially'dangerous'[email protected]",
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => 'posts+AAAAAAAAAAIAAAAAAAAGQQAAAAABFSXz1eM/P/[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
|
||||
$this->assertTrue(validate_email('[email protected]'));
|
||||
$this->assertTrue(validate_email('[email protected]'));
|
||||
$this->assertTrue(validate_email('[email protected]'));
|
||||
$this->assertTrue(validate_email("but_potentially'dangerous'[email protected]"));
|
||||
$this->assertTrue(validate_email('posts+AAAAAAAAAAIAAAAAAAAGQQAAAAABFSXz1eM/P/[email protected]'));
|
||||
// Test addresses that should NOT pass.
|
||||
[
|
||||
'email' => 'moodle@localhost',
|
||||
'result' => false
|
||||
],
|
||||
[
|
||||
'email' => '"attacker\\" -oQ/tmp/ -X/var/www/vhost/moodle/backdoor.php some"@email.com',
|
||||
'result' => false
|
||||
],
|
||||
[
|
||||
'email' => "[email protected]>\r\nRCPT TO:<[email protected]",
|
||||
'result' => false
|
||||
],
|
||||
|
||||
$this->assertFalse(validate_email('moodle@localhost'));
|
||||
$this->assertFalse(validate_email('"attacker\\" -oQ/tmp/ -X/var/www/vhost/moodle/backdoor.php some"@email.com'));
|
||||
$this->assertFalse(validate_email("[email protected]>\r\nRCPT TO:<[email protected]"));
|
||||
// Extra email addresses from Wikipedia page on Email Addresses.
|
||||
// Valid.
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
// One-letter local-part.
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
// See the List of Internet top-level domains.
|
||||
[
|
||||
'email' => '[email protected]',
|
||||
'result' => true
|
||||
],
|
||||
// Quoted double dot.
|
||||
[
|
||||
'email' => '"john..doe"@example.org',
|
||||
'result' => true
|
||||
],
|
||||
|
||||
// Invalid.
|
||||
// No @ character.
|
||||
[
|
||||
'email' => 'Abc.example.com',
|
||||
'result' => false
|
||||
],
|
||||
// Only one @ is allowed outside quotation marks.
|
||||
[
|
||||
'email' => 'A@b@[email protected]',
|
||||
'result' => false
|
||||
],
|
||||
// None of the special characters in this local-part are allowed outside quotation marks.
|
||||
[
|
||||
'email' => 'a"b(c)d,e:f;g<h>i[j\k][email protected]',
|
||||
'result' => false
|
||||
],
|
||||
// Quoted strings must be dot separated or the only element making up the local-part.
|
||||
[
|
||||
'email' => 'just"not"[email protected]',
|
||||
'result' => false
|
||||
],
|
||||
// Spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash.
|
||||
[
|
||||
'email' => 'this is"not\[email protected]',
|
||||
'result' => false
|
||||
],
|
||||
// Even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes.
|
||||
[
|
||||
'email' => 'this\ still\"not\\[email protected]',
|
||||
'result' => false
|
||||
],
|
||||
// Local part is longer than 64 characters.
|
||||
[
|
||||
'email' => '1234567890123456789012345678901234567890123456789012345678901234+x@example.com',
|
||||
'result' => false
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests valid and invalid email address using the validate_email() function.
|
||||
*
|
||||
* @param string $email the email address to test
|
||||
* @param boolean $result Expected result (true or false)
|
||||
* @dataProvider data_validate_email
|
||||
*/
|
||||
public function test_validate_email($email, $result) {
|
||||
if ($result) {
|
||||
$this->assertTrue(validate_email($email));
|
||||
} else {
|
||||
$this->assertFalse(validate_email($email));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+3
-6
@@ -1104,13 +1104,10 @@ function page_get_doc_link_path(moodle_page $page) {
|
||||
* @return boolean
|
||||
*/
|
||||
function validate_email($address) {
|
||||
global $CFG;
|
||||
require_once($CFG->libdir.'/phpmailer/moodle_phpmailer.php');
|
||||
|
||||
return (bool)preg_match('#^[-!\#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+'.
|
||||
'(\.[-!\#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+)*'.
|
||||
'@'.
|
||||
'[-!\#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
|
||||
'[-!\#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$#',
|
||||
$address);
|
||||
return moodle_phpmailer::validateAddress($address);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user