Add MailAddress::newFromUser()
authorKunal Mehta <legoktm@gmail.com>
Sat, 13 Sep 2014 03:25:19 +0000 (20:25 -0700)
committerAddshore <addshorewiki@gmail.com>
Sun, 14 Sep 2014 19:03:18 +0000 (19:03 +0000)
And tests!

Change-Id: I5214c50855f6bc756f6d748e435ae2124b2264c1

includes/User.php
includes/mail/EmailNotification.php
includes/mail/MailAddress.php
includes/specials/SpecialEmailuser.php
tests/phpunit/includes/mail/MailAddressTest.php

index 4557096..635b1e9 100644 (file)
@@ -3992,10 +3992,10 @@ class User implements IDBAccessObject {
                        $sender = new MailAddress( $wgPasswordSender,
                                wfMessage( 'emailsender' )->inContentLanguage()->text() );
                } else {
-                       $sender = new MailAddress( $from );
+                       $sender = MailAddress::newFromUser( $from );
                }
 
-               $to = new MailAddress( $this );
+               $to = MailAddress::newFromUser( $this );
                return UserMailer::send( $to, $sender, $subject, $body, $replyto );
        }
 
index ceb454f..9219c3a 100644 (file)
@@ -389,7 +389,7 @@ class EmailNotification {
                        && ( $this->editor->getEmail() != '' )
                        && $this->editor->getOption( 'enotifrevealaddr' )
                ) {
-                       $editorAddress = new MailAddress( $this->editor );
+                       $editorAddress = MailAddress::newFromUser( $this->editor );
                        if ( $wgEnotifFromEditor ) {
                                $this->from = $editorAddress;
                        } else {
@@ -417,7 +417,7 @@ class EmailNotification {
                }
 
                if ( $wgEnotifImpersonal ) {
-                       $this->mailTargets[] = new MailAddress( $user );
+                       $this->mailTargets[] = MailAddress::newFromUser( $user );
                } else {
                        $this->sendPersonalised( $user );
                }
@@ -448,7 +448,7 @@ class EmailNotification {
                //   Note: The to parameter cannot be an address in the form of
                //   "Something <someone@example.com>". The mail command will not parse
                //   this properly while talking with the MTA.
-               $to = new MailAddress( $watchingUser );
+               $to = MailAddress::newFromUser( $watchingUser );
 
                # $PAGEEDITDATE is the time and date of the page change
                # expressed in terms of individual local time of the notification
index 972d291..6817908 100644 (file)
  */
 class MailAddress {
        /**
-        * @param string|User $address String with an email address, or a User object
+        * @param string $address String with an email address, or a User object
         * @param string $name Human-readable name if a string address is given
         * @param string $realName Human-readable real name if a string address is given
         */
        function __construct( $address, $name = null, $realName = null ) {
                if ( is_object( $address ) && $address instanceof User ) {
+                       // Old calling format, now deprecated
+                       wfDeprecated( __METHOD__ . ' with a User object' , '1.24' );
                        $this->address = $address->getEmail();
                        $this->name = $address->getName();
                        $this->realName = $address->getRealName();
@@ -47,6 +49,17 @@ class MailAddress {
                }
        }
 
+       /**
+        * Create a new MailAddress object for the given user
+        *
+        * @since 1.24
+        * @param User $user
+        * @return MailAddress
+        */
+       public static function newFromUser( User $user ) {
+               return new MailAddress( $user->getEmail(), $user->getName(), $user->getRealName() );
+       }
+
        /**
         * Return formatted and quoted address to insert into SMTP headers
         * @return string
index a4e4c54..20532a9 100644 (file)
@@ -313,8 +313,8 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        return $context->msg( $target . 'text' )->parseAsBlock();
                }
 
-               $to = new MailAddress( $target );
-               $from = new MailAddress( $context->getUser() );
+               $to = MailAddress::newFromUser( $target );
+               $from = MailAddress::newFromUser( $context->getUser() );
                $subject = $data['Subject'];
                $text = $data['Text'];
 
index 437b135..2d07812 100644 (file)
@@ -2,6 +2,31 @@
 
 class MailAddressTest extends MediaWikiTestCase {
 
+       /**
+        * @covers MailAddress::__construct
+        */
+       public function testConstructor() {
+               $ma = new MailAddress( 'foo@bar.baz', 'UserName', 'Real name' );
+               $this->assertInstanceOf( 'MailAddress', $ma );
+       }
+
+       /**
+        * @covers MailAddress::newFromUser
+        */
+       public function testNewFromUser() {
+               $user = $this->getMock( 'User' );
+               $user->expects( $this->any() )->method( 'getName' )->will( $this->returnValue( 'UserName' ) );
+               $user->expects( $this->any() )->method( 'getEmail' )->will( $this->returnValue( 'foo@bar.baz' ) );
+               $user->expects( $this->any() )->method( 'getRealName' )->will( $this->returnValue( 'Real name' ) );
+
+               $ma = MailAddress::newFromUser( $user );
+               $this->assertInstanceOf( 'MailAddress', $ma );
+               $this->setMwGlobals( 'wgEnotifUseRealName', true );
+               $this->assertEquals( 'Real name <foo@bar.baz>', $ma->toString() );
+               $this->setMwGlobals( 'wgEnotifUseRealName', false );
+               $this->assertEquals( 'UserName <foo@bar.baz>', $ma->toString() );
+       }
+
        /**
         * @covers MailAddress::toString
         * @dataProvider provideToString