3 use MediaWiki\User\User
;
4 use MediaWiki\User\UserIdentityValue
;
10 class MailAddressTest
extends MediaWikiIntegrationTestCase
{
12 public function testNewFromUser() {
13 if ( wfIsWindows() ) {
14 $this->markTestSkipped( 'This test only works on non-Windows platforms' );
16 $user = $this->createMock( User
::class );
17 $user->method( 'getUser' )->willReturn( new UserIdentityValue( 42, 'UserName' ) );
18 $user->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
19 $user->method( 'getRealName' )->willReturn( 'Real name' );
21 $ma = MailAddress
::newFromUser( $user );
22 $this->assertInstanceOf( MailAddress
::class, $ma );
24 // No setMwGlobals() in a unit test, need some manual logic
25 // Don't worry about messing with the actual value, MediaWikiUnitTestCase restores it
26 global $wgEnotifUseRealName;
28 $wgEnotifUseRealName = true;
29 $this->assertEquals( '"Real name" <foo@bar.baz>', $ma->toString() );
31 $wgEnotifUseRealName = false;
32 $this->assertEquals( '"UserName" <foo@bar.baz>', $ma->toString() );
36 * @dataProvider provideEquals
38 public function testEquals( MailAddress
$first, MailAddress
$second, bool $expected ) {
39 $this->assertSame( $expected, $first->equals( $second ) );
42 public static function provideEquals(): Generator
{
43 $base = new MailAddress( 'a@b.c', 'name', 'realname' );
45 yield
'Different addresses' => [ $base, new MailAddress( 'xxx', 'name', 'realname' ), false ];
46 yield
'Different names' => [ $base, new MailAddress( 'a@b.c', 'other name', 'realname' ), false ];
47 yield
'Different real names' => [ $base, new MailAddress( 'a@b.c', 'name', 'other realname' ), false ];
48 yield
'Equal' => [ $base, new MailAddress( 'a@b.c', 'name', 'realname' ), true ];
52 * @dataProvider provideToString
54 public function testToString( $useRealName, $address, $name, $realName, $expected ) {
55 if ( wfIsWindows() ) {
56 $this->markTestSkipped( 'This test only works on non-Windows platforms' );
58 // No setMwGlobals() in a unit test, need some manual logic
59 // Don't worry about messing with the actual value, MediaWikiUnitTestCase restores it
60 global $wgEnotifUseRealName;
61 $wgEnotifUseRealName = $useRealName;
63 $ma = new MailAddress( $address, $name, $realName );
64 $this->assertEquals( $expected, $ma->toString() );
67 public static function provideToString() {
69 [ true, 'foo@bar.baz', 'FooBar', 'Foo Bar', '"Foo Bar" <foo@bar.baz>' ],
70 [ true, 'foo@bar.baz', 'UserName', null, '"UserName" <foo@bar.baz>' ],
71 [ true, 'foo@bar.baz', 'AUser', 'My real name', '"My real name" <foo@bar.baz>' ],
72 [ true, 'foo@bar.baz', 'AUser', 'My "real" name', '"My \"real\" name" <foo@bar.baz>' ],
73 [ true, 'foo@bar.baz', 'AUser', 'My "A/B" test', '"My \"A/B\" test" <foo@bar.baz>' ],
74 [ true, 'foo@bar.baz', 'AUser', 'E=MC2', '=?UTF-8?Q?E=3DMC2?= <foo@bar.baz>' ],
75 // A backslash (\) should be escaped (\\). In a string literal that is \\\\ (4x).
76 [ true, 'foo@bar.baz', 'AUser', 'My "B\C" test', '"My \"B\\\\C\" test" <foo@bar.baz>' ],
77 [ true, 'foo@bar.baz', 'A.user.name', 'my@real.name', '"my@real.name" <foo@bar.baz>' ],
78 [ false, 'foo@bar.baz', 'AUserName', 'Some real name', '"AUserName" <foo@bar.baz>' ],
79 [ false, 'foo@bar.baz', '', '', 'foo@bar.baz' ],
80 [ true, 'foo@bar.baz', '', '', 'foo@bar.baz' ],
81 [ true, '', '', '', '' ],
85 public function test__ToString() {
86 $ma = new MailAddress( 'some@email.com', 'UserName', 'A real name' );
87 $this->assertEquals( $ma->toString(), (string)$ma );