Debian Codegraph @

tests/phpunit/includes/mail/MailAddressTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\User\User;
4 use MediaWiki\User\UserIdentityValue;
5
6 /**
7 * @group Mail
8 * @covers \MailAddress
9 */
10 class MailAddressTest extends MediaWikiIntegrationTestCase {
11
12 public function testNewFromUser() {
13 if ( wfIsWindows() ) {
14 $this->markTestSkipped( 'This test only works on non-Windows platforms' );
15 }
16 $user = $this->createMock( User::class );
17 $user->method( 'getUser' )->willReturn( new UserIdentityValue( 42, 'UserName' ) );
18 $user->method( 'getEmail' )->willReturn( '[email protected]' );
19 $user->method( 'getRealName' )->willReturn( 'Real name' );
20
21 $ma = MailAddress::newFromUser( $user );
22 $this->assertInstanceOf( MailAddress::class, $ma );
23
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;
27
28 $wgEnotifUseRealName = true;
29 $this->assertEquals( '"Real name" <[email protected]>', $ma->toString() );
30
31 $wgEnotifUseRealName = false;
32 $this->assertEquals( '"UserName" <[email protected]>', $ma->toString() );
33 }
34
35 /**
36 * @dataProvider provideEquals
37 */
38 public function testEquals( MailAddress $first, MailAddress $second, bool $expected ) {
39 $this->assertSame( $expected, $first->equals( $second ) );
40 }
41
42 public static function provideEquals(): Generator {
43 $base = new MailAddress( '[email protected]', 'name', 'realname' );
44
45 yield 'Different addresses' => [ $base, new MailAddress( 'xxx', 'name', 'realname' ), false ];
46 yield 'Different names' => [ $base, new MailAddress( '[email protected]', 'other name', 'realname' ), false ];
47 yield 'Different real names' => [ $base, new MailAddress( '[email protected]', 'name', 'other realname' ), false ];
48 yield 'Equal' => [ $base, new MailAddress( '[email protected]', 'name', 'realname' ), true ];
49 }
50
51 /**
52 * @dataProvider provideToString
53 */
54 public function testToString( $useRealName, $address, $name, $realName, $expected ) {
55 if ( wfIsWindows() ) {
56 $this->markTestSkipped( 'This test only works on non-Windows platforms' );
57 }
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;
62
63 $ma = new MailAddress( $address, $name, $realName );
64 $this->assertEquals( $expected, $ma->toString() );
65 }
66
67 public static function provideToString() {
68 return [
69 [ true, '[email protected]', 'FooBar', 'Foo Bar', '"Foo Bar" <[email protected]>' ],
70 [ true, '[email protected]', 'UserName', null, '"UserName" <[email protected]>' ],
71 [ true, '[email protected]', 'AUser', 'My real name', '"My real name" <[email protected]>' ],
72 [ true, '[email protected]', 'AUser', 'My "real" name', '"My \"real\" name" <[email protected]>' ],
73 [ true, '[email protected]', 'AUser', 'My "A/B" test', '"My \"A/B\" test" <[email protected]>' ],
74 [ true, '[email protected]', 'AUser', 'E=MC2', '=?UTF-8?Q?E=3DMC2?= <[email protected]>' ],
75 // A backslash (\) should be escaped (\\). In a string literal that is \\\\ (4x).
76 [ true, '[email protected]', 'AUser', 'My "B\C" test', '"My \"B\\\\C\" test" <[email protected]>' ],
77 [ true, '[email protected]', 'A.user.name', '[email protected]', '"[email protected]" <[email protected]>' ],
78 [ false, '[email protected]', 'AUserName', 'Some real name', '"AUserName" <[email protected]>' ],
79 [ false, 'foo@example', 'Name, User', '', '"Name, User" <foo@example>' ],
80 [ false, 'foo@example', 'User Näme, With Umlauts', '', '=?UTF-8?Q?User=20N=C3=A4me=2C=20With=20Umlauts?= <foo@example>' ],
81 [ false, '[email protected]', '', '', '[email protected]' ],
82 [ true, '[email protected]', '', '', '[email protected]' ],
83 [ true, '', '', '', '' ],
84 ];
85 }
86
87 public function test__ToString() {
88 $ma = new MailAddress( '[email protected]', 'UserName', 'A real name' );
89 $this->assertEquals( $ma->toString(), (string)$ma );
90 }
91 }