MDL-57531 mail: Make validate_email return actual boolean as expected
authorDavid Mudrák <david@moodle.com>
Wed, 4 Jan 2017 12:13:31 +0000 (13:13 +0100)
committerDavid Mudrák <david@moodle.com>
Wed, 4 Jan 2017 12:16:07 +0000 (13:16 +0100)
It was discovered by unit tests that the return value is not a real
boolean as was intuitively expected and documented. To avoid potential
issues with the truthyness in the future, we explicitly cast the return
value to boolean now.

lib/tests/moodlelib_test.php
lib/tests/weblib_test.php
lib/upgrade.txt
lib/weblib.php

index a65436e..c681b2b 100644 (file)
@@ -3442,10 +3442,10 @@ class core_moodlelib_testcase extends advanced_testcase {
 
         $CFG->maildomain = 'example.com';
         $CFG->mailprefix = 'mdl+';
-        $this->assertEquals(1, validate_email(generate_email_processing_address(0, $modargs)));
+        $this->assertTrue(validate_email(generate_email_processing_address(0, $modargs)));
 
         $CFG->maildomain = 'mail.example.com';
         $CFG->mailprefix = 'mdl-';
-        $this->assertEquals(1, validate_email(generate_email_processing_address(23, $modargs)));
+        $this->assertTrue(validate_email(generate_email_processing_address(23, $modargs)));
     }
 }
index c248b98..1cfc020 100644 (file)
@@ -670,14 +670,14 @@ EXPECTED;
      */
     public function test_validate_email() {
 
-        $this->assertEquals(1, validate_email('moodle@example.com'));
-        $this->assertEquals(1, validate_email('moodle@localhost.local'));
-        $this->assertEquals(1, validate_email('verp_email+is=mighty@moodle.org'));
-        $this->assertEquals(1, validate_email("but_potentially'dangerous'too@example.org"));
-        $this->assertEquals(1, validate_email('posts+AAAAAAAAAAIAAAAAAAAGQQAAAAABFSXz1eM/P/lR2bYyljM+@posts.moodle.org'));
-
-        $this->assertEquals(0, validate_email('moodle@localhost'));
-        $this->assertEquals(0, validate_email('"attacker\\" -oQ/tmp/ -X/var/www/vhost/moodle/backdoor.php  some"@email.com'));
-        $this->assertEquals(0, validate_email("moodle@example.com>\r\nRCPT TO:<victim@example.com"));
+        $this->assertTrue(validate_email('moodle@example.com'));
+        $this->assertTrue(validate_email('moodle@localhost.local'));
+        $this->assertTrue(validate_email('verp_email+is=mighty@moodle.org'));
+        $this->assertTrue(validate_email("but_potentially'dangerous'too@example.org"));
+        $this->assertTrue(validate_email('posts+AAAAAAAAAAIAAAAAAAAGQQAAAAABFSXz1eM/P/lR2bYyljM+@posts.moodle.org'));
+
+        $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("moodle@example.com>\r\nRCPT TO:<victim@example.com"));
     }
 }
index 2ab1711..aade1be 100644 (file)
@@ -2,8 +2,10 @@ This files describes API changes in core libraries and APIs,
 information provided here is intended especially for developers.
 
 === 3.3 ===
+
 * YUI module moodle-core-formautosubmit has been removed, use jquery .change() instead (see lib/templates/url_select.mustache for
   an example)
+* Return value of the validate_email() is now proper boolean as documented. Previously the function could return 1, 0 or false.
 
 === 3.2 ===
 
index 97da57a..0d9ce7f 100644 (file)
@@ -1088,12 +1088,12 @@ function page_get_doc_link_path(moodle_page $page) {
  */
 function validate_email($address) {
 
-    return (preg_match('#^[-!\#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+'.
+    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));
+                  $address);
 }
 
 /**