2013-05-14 22:22:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EchoNotificationFormatterTest extends MediaWikiTestCase {
|
|
|
|
|
2013-10-28 06:32:12 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$user = new User();
|
|
|
|
$user->setName( 'Notification-formatter-test' );
|
|
|
|
$user->addToDatabase();
|
|
|
|
$this->setMwGlobals( 'wgUser', $user );
|
|
|
|
}
|
|
|
|
|
2013-05-14 22:22:52 +00:00
|
|
|
public static function provider_editUserTalkEmail() {
|
|
|
|
return array(
|
|
|
|
array( '/Main_Page[^#]/', null ),
|
2013-06-24 00:22:08 +00:00
|
|
|
array( '/Main_Page#Section_8/', 'Section 8' ),
|
2013-05-14 22:22:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provider_editUserTalkEmail
|
|
|
|
*/
|
|
|
|
public function testEditUserTalkEmailNotificationLink( $pattern, $sectionTitle ) {
|
|
|
|
$event = $this->mockEvent( 'edit-user-talk', array(
|
|
|
|
'section-title' => $sectionTitle,
|
|
|
|
) );
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getTitle' )
|
|
|
|
->will( $this->returnValue( Title::newMainPage() ) );
|
|
|
|
$formatted = $this->format( $event, 'email' );
|
2013-06-24 00:22:08 +00:00
|
|
|
if ( is_array( $formatted['body'] ) ) {
|
|
|
|
$this->assertRegExp( $pattern, $formatted['body']['text'] );
|
|
|
|
$this->assertRegExp( $pattern, $formatted['body']['html'] );
|
|
|
|
} else {
|
|
|
|
$this->assertRegExp( $pattern, $formatted['body'] );
|
|
|
|
}
|
2013-05-14 22:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function provider_editUserTalk() {
|
|
|
|
return array(
|
2013-07-24 03:50:43 +00:00
|
|
|
// if there is a section-title, the message should be '[[User:user_name|user_name]] left a message on
|
2013-10-28 06:32:12 +00:00
|
|
|
// your talk page in '[[User talk:user_name#section_title|section_title]]'
|
2013-07-24 03:50:43 +00:00
|
|
|
array( '/[[User talk:[^#]+#moar_cowbell|moar_cowbell]]/', 'moar_cowbell', 'text' ),
|
2013-05-14 22:22:52 +00:00
|
|
|
array( '/#moar_cowbell/', 'moar_cowbell', 'html' ),
|
|
|
|
array( '/#moar_cowbell/', 'moar_cowbell', 'flyout' ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provider_editUserTalk
|
|
|
|
*/
|
|
|
|
public function testEditUserTalkFlyoutSectionLinkFragment( $pattern, $sectionTitle, $format ) {
|
|
|
|
// Required hack so parser doesnt turn the links into redlinks which contain no fragment
|
2013-10-28 06:32:12 +00:00
|
|
|
global $wgUser;
|
|
|
|
LinkCache::singleton()->addGoodLinkObj( 42, $wgUser->getTalkPage() );
|
2013-05-14 22:22:52 +00:00
|
|
|
|
|
|
|
$event = $this->mockEvent( 'edit-user-talk', array(
|
|
|
|
'section-title' => $sectionTitle,
|
|
|
|
) );
|
|
|
|
$this->assertRegExp( $pattern, $this->format( $event, $format ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provider_formatterDoesntFail() {
|
|
|
|
// Remove events from this array once they have specific tests for their formatting
|
|
|
|
$untested = array(
|
|
|
|
'welcome' => array(),
|
|
|
|
'reverted' => array(
|
|
|
|
'revid' => 42,
|
|
|
|
'reverted-user-id' => 77,
|
|
|
|
'reverted-revision-id' => 13,
|
|
|
|
'method' => 'undo',
|
|
|
|
),
|
|
|
|
'page-linked' => array(
|
2013-05-08 18:11:44 +00:00
|
|
|
'link-from-page-id' => 42,
|
2013-05-14 22:22:52 +00:00
|
|
|
),
|
|
|
|
'mention' => array(
|
|
|
|
'content' => 'lorem ipsum dolar sit amet',
|
|
|
|
'section-title' => 'Zombies',
|
|
|
|
'revid' => 42,
|
|
|
|
'mentionedusers' => array( 101 => 101 ),
|
|
|
|
),
|
|
|
|
'user-rights' => array(
|
|
|
|
'user' => 187,
|
|
|
|
'add' => array( 'aaa', 'bbb' ),
|
|
|
|
'remove' => array( 'other' ),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$formats = array( 'html', 'flyout', 'email', 'text' );
|
|
|
|
$tests = array();
|
|
|
|
foreach ( $untested as $type => $extra ) {
|
|
|
|
foreach ( $formats as $format ) {
|
|
|
|
// Run tests with blank extra data and with the provided extra data
|
|
|
|
$tests[] = array( $format, $type, $extra );
|
|
|
|
$tests[] = array( $format, $type, array() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
|
2013-05-08 18:11:44 +00:00
|
|
|
public static function provider_revisionSummary() {
|
2013-07-24 03:50:43 +00:00
|
|
|
$sectionText = '(dummy comment)';
|
2013-05-08 18:11:44 +00:00
|
|
|
|
|
|
|
// Test the 4 different events that reference the summary, although they should follow mostly
|
|
|
|
// the same code they may use different classes extended from the EchoNotificationFormatter
|
|
|
|
$tests = array();
|
2013-07-24 03:50:43 +00:00
|
|
|
$events = array( 'edit-user-talk' );
|
2013-05-08 18:11:44 +00:00
|
|
|
foreach ( $events as $eventType ) {
|
2013-07-24 03:50:43 +00:00
|
|
|
$tests[] = array( $eventType, $sectionText, 0);
|
|
|
|
$tests[] = array( $eventType, $sectionText, Revision::DELETED_TEXT );
|
2013-05-08 18:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provider_revisionSummary
|
|
|
|
*/
|
2013-07-24 03:50:43 +00:00
|
|
|
public function testRevisionSummarySuppression( $eventType, $text, $deleted ) {
|
2013-05-08 18:11:44 +00:00
|
|
|
// Revision needs a comment to attempt to format
|
|
|
|
$event = $this->mockEvent(
|
|
|
|
$eventType,
|
2013-07-24 03:50:43 +00:00
|
|
|
array( 'section-title' => 'Test Title', 'section-text' => $text ),
|
|
|
|
new Revision( compact( 'deleted' ) )
|
2013-05-08 18:11:44 +00:00
|
|
|
);
|
2013-07-24 03:50:43 +00:00
|
|
|
if ( $deleted === Revision::DELETED_TEXT ) {
|
|
|
|
$this->assertNotContains( $text, $this->format( $event, 'html' ) );
|
|
|
|
} else {
|
|
|
|
$this->assertContains( $text, $this->format( $event, 'html' ) );
|
|
|
|
}
|
2013-05-08 18:11:44 +00:00
|
|
|
}
|
2013-10-28 06:32:12 +00:00
|
|
|
|
2013-05-08 18:11:44 +00:00
|
|
|
public static function provider_revisionAgent() {
|
|
|
|
$userText = '10.2.3.4';
|
|
|
|
$suppressed = wfMessage( 'rev-deleted-user' )->text();
|
|
|
|
|
|
|
|
$tests = array();
|
|
|
|
$events = array( 'edit-user-talk', 'reverted', 'mention' );
|
|
|
|
foreach ( $events as $eventType ) {
|
|
|
|
$tests[] = array( $eventType, $userText, $userText, 0 );
|
|
|
|
$tests[] = array( $eventType, $suppressed, $userText, Revision::DELETED_USER );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provider_revisionAgent
|
|
|
|
*/
|
|
|
|
public function testAgentSuppression( $eventType, $expect, $user_text, $deleted ) {
|
|
|
|
$event = $this->mockEvent(
|
|
|
|
$eventType,
|
|
|
|
array(),
|
|
|
|
new Revision( compact( 'user_text', 'deleted' ) )
|
|
|
|
);
|
|
|
|
|
|
|
|
$user = new User;
|
|
|
|
$user->setName( $user_text );
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getAgent' )
|
|
|
|
->will( $this->returnValue( $user ) );
|
|
|
|
|
|
|
|
$this->assertContains( $expect, $this->format( $event, 'html' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function provider_sectionTitle() {
|
|
|
|
$message = "some_section_title"; // underscores simplifies the test, since it will transform ' ' to '_'
|
|
|
|
$suppressed = wfMessage( 'echo-rev-deleted-text-view')->text();
|
|
|
|
|
|
|
|
$tests = array();
|
|
|
|
$events = array( 'mention' ); // currently only mention uses sectionTitle, but likely edit-user-talk will soon as well
|
|
|
|
foreach ( $events as $eventType ) {
|
|
|
|
$tests[] = array( $eventType, $message, $message, 0);
|
|
|
|
$tests[] = array( $eventType, $suppressed, $message, Revision::DELETED_TEXT );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
|
2013-05-14 22:22:52 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provider_formatterDoesntFail
|
|
|
|
*/
|
|
|
|
public function testFormatterDoesntFail( $format, $type, array $extra ) {
|
|
|
|
$result = $this->format( $this->mockEvent( $type, $extra ), $format );
|
|
|
|
|
|
|
|
// generic assertion, could do better
|
|
|
|
if ( $format === 'email' ) {
|
|
|
|
$this->assertInternalType( 'array', $result );
|
2013-07-09 00:41:08 +00:00
|
|
|
$this->assertCount( 2, $result );
|
2013-05-14 22:22:52 +00:00
|
|
|
} else {
|
|
|
|
$this->assertInternalType( 'string', $result );
|
|
|
|
$this->assertGreaterThan( 0, strlen( $result ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-08 18:11:44 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provider_sectionTitle
|
|
|
|
*/
|
|
|
|
public function testMentionSubjectSectionTitleSuppression( $eventType, $expect, $sectionTitle, $deleted ) {
|
|
|
|
$event = $this->mockEvent(
|
|
|
|
$eventType,
|
|
|
|
array( 'section-title' => $sectionTitle ),
|
|
|
|
new Revision( compact( 'deleted' ) )
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertContains( $expect, $this->format( $event, 'html' ) );
|
|
|
|
}
|
|
|
|
|
2014-02-06 23:06:54 +00:00
|
|
|
protected function format( EchoEvent $event, $format, $user = false, $type = 'web' ) {
|
2013-10-28 06:32:12 +00:00
|
|
|
if ( $user === false ) {
|
|
|
|
$user = User::newFromName('Notification-formatter-test');
|
|
|
|
}
|
|
|
|
|
2013-06-24 00:22:08 +00:00
|
|
|
// Notification users can not be anonymous, use a fake user id
|
2014-02-06 23:06:54 +00:00
|
|
|
return EchoNotificationController::formatNotification( $event, $user, $format, $type );
|
2013-05-14 22:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockEvent( $type, array $extra = array(), Revision $rev = null ) {
|
2013-05-08 18:11:44 +00:00
|
|
|
$methods = get_class_methods( 'EchoEvent' );
|
2013-06-24 00:22:08 +00:00
|
|
|
$methods = array_diff( $methods, array( 'userCan', 'getLinkMessage', 'getLinkDestination' ) );
|
|
|
|
|
2013-05-14 22:22:52 +00:00
|
|
|
$event = $this->getMockBuilder( 'EchoEvent' )
|
|
|
|
->disableOriginalConstructor()
|
2013-05-08 18:11:44 +00:00
|
|
|
->setMethods( $methods )
|
2013-05-14 22:22:52 +00:00
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getType' )
|
|
|
|
->will( $this->returnValue( $type ) );
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getExtra' )
|
|
|
|
->will( $this->returnValue( $extra ) );
|
|
|
|
if ( $rev !== null ) {
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getRevision' )
|
|
|
|
->will( $this->returnValue( $rev ) );
|
|
|
|
}
|
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
}
|