From eab5a7d6ef68c7f801fcba9619d6c9b95c6cb267 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Fri, 12 Sep 2014 20:25:19 -0700 Subject: [PATCH] Add MailAddress::newFromUser() And tests! Change-Id: I5214c50855f6bc756f6d748e435ae2124b2264c1 --- includes/User.php | 4 +-- includes/mail/EmailNotification.php | 6 ++--- includes/mail/MailAddress.php | 15 ++++++++++- includes/specials/SpecialEmailuser.php | 4 +-- .../phpunit/includes/mail/MailAddressTest.php | 25 +++++++++++++++++++ 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/includes/User.php b/includes/User.php index 45570966e8..635b1e9258 100644 --- a/includes/User.php +++ b/includes/User.php @@ -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 ); } diff --git a/includes/mail/EmailNotification.php b/includes/mail/EmailNotification.php index ceb454f540..9219c3aaa2 100644 --- a/includes/mail/EmailNotification.php +++ b/includes/mail/EmailNotification.php @@ -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 ". 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 diff --git a/includes/mail/MailAddress.php b/includes/mail/MailAddress.php index 972d291d62..6817908860 100644 --- a/includes/mail/MailAddress.php +++ b/includes/mail/MailAddress.php @@ -31,12 +31,14 @@ */ 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 diff --git a/includes/specials/SpecialEmailuser.php b/includes/specials/SpecialEmailuser.php index a4e4c548a6..20532a92ae 100644 --- a/includes/specials/SpecialEmailuser.php +++ b/includes/specials/SpecialEmailuser.php @@ -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']; diff --git a/tests/phpunit/includes/mail/MailAddressTest.php b/tests/phpunit/includes/mail/MailAddressTest.php index 437b135f68..2d0781204f 100644 --- a/tests/phpunit/includes/mail/MailAddressTest.php +++ b/tests/phpunit/includes/mail/MailAddressTest.php @@ -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 ', $ma->toString() ); + $this->setMwGlobals( 'wgEnotifUseRealName', false ); + $this->assertEquals( 'UserName ', $ma->toString() ); + } + /** * @covers MailAddress::toString * @dataProvider provideToString -- 2.20.1