MailAddressTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. class MailAddressTest extends MediaWikiTestCase {
  3. /**
  4. * @covers MailAddress::__construct
  5. */
  6. public function testConstructor() {
  7. $ma = new MailAddress( 'foo@bar.baz', 'UserName', 'Real name' );
  8. $this->assertInstanceOf( MailAddress::class, $ma );
  9. }
  10. /**
  11. * @covers MailAddress::newFromUser
  12. */
  13. public function testNewFromUser() {
  14. if ( wfIsWindows() ) {
  15. $this->markTestSkipped( 'This test only works on non-Windows platforms' );
  16. }
  17. $user = $this->createMock( User::class );
  18. $user->expects( $this->any() )->method( 'getName' )->will(
  19. $this->returnValue( 'UserName' )
  20. );
  21. $user->expects( $this->any() )->method( 'getEmail' )->will(
  22. $this->returnValue( 'foo@bar.baz' )
  23. );
  24. $user->expects( $this->any() )->method( 'getRealName' )->will(
  25. $this->returnValue( 'Real name' )
  26. );
  27. $ma = MailAddress::newFromUser( $user );
  28. $this->assertInstanceOf( MailAddress::class, $ma );
  29. $this->setMwGlobals( 'wgEnotifUseRealName', true );
  30. $this->assertEquals( '"Real name" <foo@bar.baz>', $ma->toString() );
  31. $this->setMwGlobals( 'wgEnotifUseRealName', false );
  32. $this->assertEquals( '"UserName" <foo@bar.baz>', $ma->toString() );
  33. }
  34. /**
  35. * @covers MailAddress::toString
  36. * @dataProvider provideToString
  37. */
  38. public function testToString( $useRealName, $address, $name, $realName, $expected ) {
  39. if ( wfIsWindows() ) {
  40. $this->markTestSkipped( 'This test only works on non-Windows platforms' );
  41. }
  42. $this->setMwGlobals( 'wgEnotifUseRealName', $useRealName );
  43. $ma = new MailAddress( $address, $name, $realName );
  44. $this->assertEquals( $expected, $ma->toString() );
  45. }
  46. public static function provideToString() {
  47. return [
  48. [ true, 'foo@bar.baz', 'FooBar', 'Foo Bar', '"Foo Bar" <foo@bar.baz>' ],
  49. [ true, 'foo@bar.baz', 'UserName', null, '"UserName" <foo@bar.baz>' ],
  50. [ true, 'foo@bar.baz', 'AUser', 'My real name', '"My real name" <foo@bar.baz>' ],
  51. [ true, 'foo@bar.baz', 'AUser', 'My "real" name', '"My \"real\" name" <foo@bar.baz>' ],
  52. [ true, 'foo@bar.baz', 'AUser', 'My "A/B" test', '"My \"A/B\" test" <foo@bar.baz>' ],
  53. [ true, 'foo@bar.baz', 'AUser', 'E=MC2', '=?UTF-8?Q?E=3DMC2?= <foo@bar.baz>' ],
  54. // A backslash (\) should be escaped (\\). In a string literal that is \\\\ (4x).
  55. [ true, 'foo@bar.baz', 'AUser', 'My "B\C" test', '"My \"B\\\\C\" test" <foo@bar.baz>' ],
  56. [ true, 'foo@bar.baz', 'A.user.name', 'my@real.name', '"my@real.name" <foo@bar.baz>' ],
  57. [ false, 'foo@bar.baz', 'AUserName', 'Some real name', '"AUserName" <foo@bar.baz>' ],
  58. [ false, 'foo@bar.baz', '', '', 'foo@bar.baz' ],
  59. [ true, 'foo@bar.baz', '', '', 'foo@bar.baz' ],
  60. [ true, '', '', '', '' ],
  61. ];
  62. }
  63. /**
  64. * @covers MailAddress::__toString
  65. */
  66. public function test__ToString() {
  67. $ma = new MailAddress( 'some@email.com', 'UserName', 'A real name' );
  68. $this->assertEquals( $ma->toString(), (string)$ma );
  69. }
  70. }