mediawiki-extensions-Echo/tests/EmailFormatterTest.php
Kunal Mehta 61f1754d14 Unbreak the unit tests
Not all installations will have a user with the id of 2,
like jenkins. At some point User::getOption is called, which
requires loading the user from the database, except it
doesn't exist. At which point the user id is reset to 0,
so it becomes an anonymous user, breaking the test.

Since the MediaWiki test runner automatically
creates a test user, we can safely assume that there is
a user with the id 1.

The other failure was due to that $wgUser may not
always be 127.0.0.1. The test now directly just
calls $wgUser to avoid issues if another test has
altered the global state.

Change-Id: Iacf904d04f1ff5aaf584cb98c3083ef6d7d89cea
2013-11-01 08:13:11 +00:00

86 lines
3.2 KiB
PHP

<?php
class EchoEmailFormatterTest extends MediaWikiTestCase {
private $emailSingle;
private $emailDigest;
public function setUp() {
parent::setUp();
global $wgEchoNotifications;
$this->setMwGlobals( 'wgAllowHTMLEmail', true );
$event = $this->mockEvent( 'edit-user-talk' );
$event->expects( $this->any() )
->method( 'getTitle' )
->will( $this->returnValue( Title::newMainPage() ) );
$formatter = EchoNotificationFormatter::factory( $wgEchoNotifications[$event->getType()] );
$formatter->setOutputFormat( 'email' );
$user = User::newFromId( 1 );
$user->setName( 'Test' );
$user->setOption( 'echo-email-format', EchoHooks::EMAIL_FORMAT_HTML );
$this->emailSingle = new EchoEmailSingle( $formatter, $event, $user );
$content[$event->getCategory()][] = EchoNotificationController::formatNotification( $event, $user, 'email', 'emaildigest' );
$this->emailDigest = new EchoEmailDigest( User::newFromId( 2 ), $content );
}
public function testEmailFormatter() {
$pattern = '/%%(.*?)%%/is';
// Single email mode
$textFormatter = new EchoTextEmailFormatter( $this->emailSingle );
$this->assertRegExp( $pattern, $this->emailSingle->getTextTemplate() );
$this->assertEquals( 0, preg_match ( $pattern, $textFormatter->formatEmail() ) );
$htmlFormatter = new EchoHTMLEmailFormatter( $this->emailSingle );
$this->assertRegExp( $pattern, $this->emailSingle->getHTMLTemplate() );
$this->assertEquals( 0, preg_match ( $pattern, $htmlFormatter->formatEmail() ) );
// Digest email mode
$textFormatter = new EchoTextEmailFormatter( $this->emailDigest );
$this->assertRegExp( $pattern, $this->emailSingle->getTextTemplate() );
$this->assertEquals( 0, preg_match ( $pattern, $textFormatter->formatEmail() ) );
$htmlFormatter = new EchoHTMLEmailFormatter( $this->emailDigest );
$this->assertRegExp( $pattern, $this->emailSingle->getHTMLTemplate() );
$this->assertEquals( 0, preg_match ( $pattern, $htmlFormatter->formatEmail() ) );
}
public function testBuildAction() {
$this->emailSingle->attachDecorator( new EchoTextEmailDecorator() );
$this->assertEquals( 0, preg_match ( '/<a /i', $this->emailSingle->buildAction() ) );
$this->emailSingle->attachDecorator( new EchoHTMLEmailDecorator() );
$this->assertRegExp( '/<a /i', $this->emailSingle->buildAction() );
$this->emailDigest->attachDecorator( new EchoTextEmailDecorator() );
$this->assertEquals( 0, preg_match ( '/<a /i', $this->emailDigest->buildAction() ) );
$this->emailDigest->attachDecorator( new EchoHTMLEmailDecorator() );
$this->assertRegExp( '/<a /i', $this->emailDigest->buildAction() );
}
protected function mockEvent( $type ) {
$methods = get_class_methods( 'EchoEvent' );
$methods = array_diff( $methods, array( 'userCan', 'getLinkMessage', 'getLinkDestination' ) );
$event = $this->getMockBuilder( 'EchoEvent' )
->disableOriginalConstructor()
->setMethods( $methods )
->getMock();
$event->expects( $this->any() )
->method( 'getType' )
->will( $this->returnValue( $type ) );
$event->expects( $this->any() )
->method( 'getCategory' )
->will( $this->returnValue( EchoNotificationController::getNotificationCategory( $type ) ) );
return $event;
}
}