2012-07-31 00:29:49 +00:00
< ? php
2016-05-04 08:45:49 +00:00
use MediaWiki\MediaWikiServices ;
2014-07-25 20:19:15 +00:00
/**
* @ group Echo
2015-01-31 01:00:06 +00:00
* @ group Database
2014-07-25 20:19:15 +00:00
*/
2012-07-31 00:29:49 +00:00
class EchoDiscussionParserTest extends MediaWikiTestCase {
2015-01-31 01:00:06 +00:00
/**
* @ var array
*/
2015-02-05 19:45:40 +00:00
protected $tablesUsed = array ( 'user' , 'revision' , 'text' , 'page' );
2015-01-31 01:00:06 +00:00
/**
2015-02-05 19:45:40 +00:00
* Users used in these tests : signature extraction , mentioned users , ... all
* assume a user exists .
*
* @ var array [ username => [ user preference => preference value ]]
2015-01-31 01:00:06 +00:00
*/
2015-02-05 19:45:40 +00:00
protected $testUsers = array (
2015-01-31 01:00:06 +00:00
// username
'Werdna' => array (
// user preferences
'nickname' => '' ,
'fancysig' => '0' ,
),
'Werdna2' => array (
'nickname' => '[[User:Werdna2|Andrew]]' ,
'fancysig' => '1' ,
),
'Werdna3' => array (
'nickname' => '[[User talk:Werdna3|Andrew]]' ,
'fancysig' => '1' ,
),
'Werdna4' => array (
'nickname' => '[[User:Werdna4|wer]dna]]' ,
'fancysig' => '1' ,
),
'We buried our secrets in the garden' => array (
'nickname' => '[[User talk:We buried our secrets in the garden#top|wbositg]]' ,
'fancysig' => '1' ,
),
'I Heart Spaces' => array (
'nickname' => '[[User:I_Heart_Spaces]] ([[User_talk:I_Heart_Spaces]])' ,
'fancysig' => '1' ,
),
'Jam' => array (
'nickname' => '[[User:Jam]]' ,
'fancysig' => '1' ,
),
'Reverta-me' => array (
'nickname' => " [[User:Reverta-me|<span style= \" font-size:13px; color:blue;font-family:Lucida Handwriting;text-shadow:aqua 5px 3px 12px; \" >Aaaaa Bbbbbbb</span>]]'' <sup>[[User Talk:Reverta-me|<font color= \" gold \" face= \" Lucida Calligraphy \" >Discussão</font>]]</sup>'' " ,
'fancysig' => '1' ,
),
'Jorm' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'Jdforrester' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'DarTar' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'Bsitu' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'JarJar' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'Schnark' => array (
2015-02-05 19:45:40 +00:00
'nickname' => '[[Benutzer:Schnark]] ([[Benutzer:Schnark/js|js]])' ,
2015-01-31 01:00:06 +00:00
'fancysig' => '1' ,
),
'Cwobeel' => array (
'nickname' => '[[User:Cwobeel|<span style="color:#339966">Cwobeel</span>]] [[User_talk:Cwobeel|<span style="font-size:80%">(talk)</span>]]' ,
'fancysig' => '1' ,
),
2015-02-05 19:45:40 +00:00
'Bob K31416' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'X" onclick="alert(\'XSS\');" title="y' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'He7d3r' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'PauloEduardo' => array (
'nickname' => " [[User:PauloEduardo|<span style= \" font-size:13px; color:blue;font-family:Lucida Handwriting;text-shadow:aqua 5px 3px 12px; \" >Paulo Eduardo</span>]]'' <sup>[[User Talk:PauloEduardo|<font color= \" gold \" face= \" Lucida Calligraphy \" >Discussão</font>]]</sup>'' " ,
'fancysig' => '1' ,
),
2015-02-17 10:10:49 +00:00
'PatHadley' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'Samwalton9' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'Kudpung' => array (
'nickname' => '[[User:Kudpung|Kudpung กุดผึ้ง]] ([[User talk:Kudpung#top|talk]])' ,
'fancysig' => '1' ,
),
'Jim Carter' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
'Buster7' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
2015-07-07 00:31:08 +00:00
'Admin' => array (
'nickname' => '[[:User:Admin|Admin]]' ,
'fancysig' => '1' ,
),
'Test11' => array (
'nickname' => '' ,
'fancysig' => '0' ,
),
2015-01-31 01:00:06 +00:00
);
protected function setUp () {
parent :: setUp ();
2016-08-19 12:32:46 +00:00
$this -> setMwGlobals ( array ( 'wgDiff' => false ) );
2015-05-21 05:17:41 +00:00
2016-04-12 15:42:22 +00:00
// users need to be added for each test, resetDB() removes them
// TODO: Only add users needed for each test, instead of adding them
// all for every one.
2015-02-05 19:45:40 +00:00
foreach ( $this -> testUsers as $username => $preferences ) {
2015-01-31 01:00:06 +00:00
$user = User :: createNew ( $username );
// set signature preferences
if ( $user ) {
foreach ( $preferences as $option => $value ) {
$user -> setOption ( $option , $value );
}
$user -> saveSettings ();
}
}
}
2015-02-05 19:45:40 +00:00
protected function tearDown () {
parent :: tearDown ();
global $wgHooks ;
unset ( $wgHooks [ 'BeforeEchoEventInsert' ][ 999 ] );
}
2016-08-03 12:41:04 +00:00
public function provideHeaderExtractions () {
return array (
array ( '' , false ),
array ( '== Grand jury no bill reception ==' , 'Grand jury no bill reception' ),
array ( '=== Echo-Test ===' , 'Echo-Test' ),
array ( '==== Notificações ====' , 'Notificações' ),
array ( '=====Me?=====' , 'Me?' ),
);
}
/**
* @ dataProvider provideHeaderExtractions
*/
public function testExtractHeader ( $text , $expected ) {
$this -> assertEquals ( $expected , EchoDiscussionParser :: extractHeader ( $text ) );
}
2015-02-05 19:45:40 +00:00
public function generateEventsForRevisionData () {
return array (
array (
'new' => 637638133 ,
'old' => 637637213 ,
'username' => 'Cwobeel' ,
'lang' => 'en' ,
'pages' => array (
// pages expected to exist (e.g. templates to be expanded)
'Template:u' => '[[User:{{{1}}}|{{<includeonly>safesubst:</includeonly>#if:{{{2|}}}|{{{2}}}|{{{1}}}}}]]<noinclude>{{documentation}}</noinclude>' ,
),
2015-02-17 10:10:49 +00:00
'title' => 'UTPage' , // can't remember, not important here
2015-02-05 19:45:40 +00:00
'expected' => array (
// events expected to be fired going from old revision to new
array (
'type' => 'mention' ,
'agent' => 'Cwobeel' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Grand jury no bill reception' ,
2015-02-05 19:45:40 +00:00
/*
* I wish I could also compare EchoEvent :: $extra data to
* compare user ids of mentioned users . However , due to
* How PHPUnit works , setUp won ' t be run by the time
* this dataset is generated , so we don ' t yet know the
* user ids of the folks we ' re about to insert ...
* I ' ll skip that part for now .
*/
),
),
),
array (
'new' => 138275105 ,
'old' => 138274875 ,
'username' => 'Schnark' ,
'lang' => 'de' ,
'pages' => array (),
2015-02-17 10:10:49 +00:00
'title' => 'UTPage' , // can't remember, not important here
2015-02-05 19:45:40 +00:00
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'Schnark' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Echo-Test' ,
2015-02-05 19:45:40 +00:00
),
),
),
array (
'new' => 40610292 ,
'old' => 40608353 ,
'username' => 'PauloEduardo' ,
'lang' => 'pt' ,
'pages' => array (
'Predefinição:U' => '[[User:{{{1|<noinclude>Exemplo</noinclude>}}}|{{{{{|safesubst:}}}#if:{{{2|}}}|{{{2}}}|{{{1|<noinclude>Exemplo</noinclude>}}}}}]]<noinclude>{{Atalho|Predefinição:U}}{{Documentação|Predefinição:Usuário/doc}}</noinclude>' ,
),
2015-02-17 10:10:49 +00:00
'title' => 'UTPage' , // can't remember, not important here
2015-02-05 19:45:40 +00:00
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'PauloEduardo' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Notificações' ,
2015-02-05 19:45:40 +00:00
),
),
),
2015-02-17 10:10:49 +00:00
array (
'new' => 646792804 ,
'old' => 646790570 ,
'username' => 'PatHadley' ,
'lang' => 'en' ,
'pages' => array (
'Template:ping' => ' {{ SAFESUBST :< noinclude /> #if:{{{1|<noinclude>$</noinclude>}}}
|< span class = " template-ping " >@ [[ : User : {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ 1 | Example }}}}} | {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ label1 | {{{ 1 | Example }}}}}}}}]]{{ SAFESUBST :< noinclude /> #if:{{{2|}}}
| , [[ : User : {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ 2 | Example }}}}} | {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ label2 | {{{ 2 | Example }}}}}}}}]]{{ SAFESUBST :< noinclude /> #if:{{{3|}}}
| , [[ : User : {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ 3 | Example }}}}} | {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ label3 | {{{ 3 | Example }}}}}}}}]]{{ SAFESUBST :< noinclude /> #if:{{{4|}}}
| , [[ : User : {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ 4 | Example }}}}} | {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ label4 | {{{ 4 | Example }}}}}}}}]]{{ SAFESUBST :< noinclude /> #if:{{{5|}}}
| , [[ : User : {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ 5 | Example }}}}} | {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ label5 | {{{ 5 | Example }}}}}}}}]]{{ SAFESUBST :< noinclude /> #if:{{{6|}}}
| , [[ : User : {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ 6 | Example }}}}} | {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ label6 | {{{ 6 | Example }}}}}}}}]]{{ SAFESUBST :< noinclude /> #if:{{{7|}}}
| , [[ : User : {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ 7 | Example }}}}} | {{ SAFESUBST :< noinclude /> BASEPAGENAME : {{{ label7 | {{{ 7 | Example }}}}}}}}]]
}}
}}
}}
}}
}}
}}{{{ p |: }}} </ span >
| {{ SAFESUBST :< noinclude /> Error | Error in [[ Template : Replyto ]] : Username not given . }}
}} < noinclude >
{{ documentation }}
</ noinclude > ' ,
'MediaWiki:Signature' => '[[User:$1|$2]] {{#ifeq:{{FULLPAGENAME}}|User talk:$1|([[User talk:$1#top|talk]])|([[User talk:$1|talk]])}}' ,
),
'title' => 'User_talk:PatHadley' ,
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'PatHadley' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Wizardry required' ,
2015-02-17 10:10:49 +00:00
),
array (
'type' => 'edit-user-talk' ,
'agent' => 'PatHadley' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Wizardry required' ,
2015-02-17 10:10:49 +00:00
),
),
2015-02-17 21:23:26 +00:00
'precondition' => 'isParserFunctionsInstalled' ,
2015-02-17 10:10:49 +00:00
),
array (
'new' => 647260329 ,
'old' => 647258025 ,
'username' => 'Kudpung' ,
'lang' => 'en' ,
'pages' => array (
'Template:U' => '[[User:{{{1}}}|{{<includeonly>safesubst:</includeonly>#if:{{{2|}}}|{{{2}}}|{{{1}}}}}]]<noinclude>{{documentation}}</noinclude>' ,
),
'title' => 'User_talk:Kudpung' ,
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'Kudpung' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Me?' ,
2015-02-17 10:10:49 +00:00
),
array (
'type' => 'edit-user-talk' ,
'agent' => 'Kudpung' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Me?' ,
2015-02-17 10:10:49 +00:00
),
),
),
2015-07-07 00:31:08 +00:00
// T68512, leading colon in user page link in signature
array (
'new' => 612485855 ,
'old' => 612485595 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'User_talk:Admin' ,
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'Admin' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Hi' ,
2015-07-07 00:31:08 +00:00
),
array (
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Hi' ,
2015-07-07 00:31:08 +00:00
),
),
'precondition' => 'isParserFunctionsInstalled' ,
),
2016-07-21 13:00:54 +00:00
);
}
/**
* @ dataProvider generateEventsForRevisionData
*/
public function testGenerateEventsForRevision (
$newId , $oldId , $username , $lang , $pages , $title , $expected , $precondition = ''
) {
if ( $precondition !== '' ) {
$result = $this -> $precondition ();
if ( $result !== true ) {
$this -> markTestSkipped ( $result );
return ;
}
}
$revision = $this -> setupTestRevisionsForEventGeneration (
$newId , $oldId , $username , $lang , $pages , $title
);
$events = array ();
$this -> setupEventCallbackForEventGeneration (
function ( EchoEvent $event ) use ( & $events ) {
$events [] = array (
'type' => $event -> getType (),
'agent' => $event -> getAgent () -> getName (),
'section-title' => $event -> getExtraParam ( 'section-title' ),
);
return false ;
}
);
// disable mention failure and success notifications
$this -> setMwGlobals ( 'wgEchoMentionStatusNotifications' , false );
EchoDiscussionParser :: generateEventsForRevision ( $revision );
$this -> assertEquals ( $expected , $events );
}
public function provider_generateEventsForRevision_mentionStatus () {
return array (
2016-06-21 14:42:56 +00:00
array (
'new' => 747747748 ,
'old' => 747747747 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'UTPage' ,
'expected' => array (
array (
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Hello Users' ,
2016-08-15 12:03:45 +00:00
'notifyAgent' => true ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'Ping' ,
2016-06-21 14:42:56 +00:00
),
array (
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Hello Users' ,
2016-08-15 12:03:45 +00:00
'notifyAgent' => true ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'Po?ng' ,
2016-06-21 14:42:56 +00:00
),
),
),
2016-07-21 13:00:54 +00:00
array (
'new' => 747747750 ,
'old' => 747747747 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'UTPage' ,
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Hello Users' ,
2016-08-15 12:03:45 +00:00
'notifyAgent' => null ,
2016-08-09 16:20:50 +00:00
'subject-name' => null ,
2016-07-21 13:00:54 +00:00
),
array (
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Hello Users' ,
2016-08-15 12:03:45 +00:00
'notifyAgent' => true ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'Test11' ,
2016-07-21 13:00:54 +00:00
),
),
),
2016-08-02 15:14:48 +00:00
array (
'new' => 747798766 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'UTPage' ,
'expected' => array (
array (
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
2016-08-15 12:03:45 +00:00
'notifyAgent' => true ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'NoUser' ,
2016-08-02 15:14:48 +00:00
),
),
),
array (
'new' => 747798767 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'UTPage' ,
'expected' => array (
array (
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
2016-08-15 12:03:45 +00:00
'notifyAgent' => true ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'NoUser' ,
2016-08-02 15:14:48 +00:00
),
),
),
array (
'new' => 747798768 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'UTPage' ,
'expected' => array (),
),
2016-08-09 16:20:50 +00:00
array (
'new' => 747798770 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'UTPage' ,
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1.5' ,
'subject-name' => null ,
'notifyAgent' => null ,
),
array (
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1.5' ,
'subject-name' => 'Test11' ,
'notifyAgent' => true ,
),
),
),
array (
'new' => 747798771 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'UTPage' ,
'expected' => array (
array (
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1.5' ,
'subject-name' => 'NoUser1.5' ,
'notifyAgent' => true ,
),
array (
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => 'NoUser2' ,
'notifyAgent' => true ,
),
),
),
array (
'new' => 747798772 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'UTPage' ,
'expected' => array (
array (
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => 'NoUser1' ,
'notifyAgent' => true ,
),
array (
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1.75' ,
'subject-name' => 'NoUser1.75' ,
'notifyAgent' => true ,
),
array (
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => 'NoUser2' ,
'notifyAgent' => true ,
),
),
2016-09-05 12:53:30 +00:00
array (
'new' => 987654322 ,
'old' => 987654321 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'User_talk:Admin' ,
'expected' => array ( array (
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
'section-title' => false ,
'subject-name' => null ,
'notifyAgent' => null ,
) ),
),
array (
'new' => 987654323 ,
'old' => 987654321 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'User_talk:Admin' ,
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => null ,
'notifyAgent' => null ,
),
array (
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => 'Test11' ,
'notifyAgent' => true ,
),
array (
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => null ,
'notifyAgent' => null ,
),
),
),
),
array (
'new' => 987654324 ,
'old' => 987654321 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'User_talk:Admin' ,
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => null ,
'notifyAgent' => null ,
),
array (
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => 'Test11' ,
'notifyAgent' => true ,
),
array (
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
'section-title' => false ,
'subject-name' => null ,
'notifyAgent' => null ,
),
),
),
array (
'new' => 987654325 ,
'old' => 987654321 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => array (),
'title' => 'User_talk:Admin' ,
'expected' => array (
array (
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => null ,
'notifyAgent' => null ,
),
array (
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => 'Test11' ,
'notifyAgent' => true ,
),
array (
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => null ,
'notifyAgent' => null ,
),
),
2016-08-09 16:20:50 +00:00
),
2015-02-05 19:45:40 +00:00
);
}
/**
2016-07-21 13:00:54 +00:00
* @ dataProvider provider_generateEventsForRevision_mentionStatus
2015-02-05 19:45:40 +00:00
*/
2016-07-21 13:00:54 +00:00
public function testGenerateEventsForRevision_mentionStatus (
$newId , $oldId , $username , $lang , $pages , $title , $expected
) {
$revision = $this -> setupTestRevisionsForEventGeneration (
$newId , $oldId , $username , $lang , $pages , $title
);
2016-06-21 14:42:56 +00:00
$events = array ();
$this -> setupEventCallbackForEventGeneration (
function ( EchoEvent $event ) use ( & $events ) {
$events [] = array (
'type' => $event -> getType (),
'agent' => $event -> getAgent () -> getName (),
2016-07-05 14:45:14 +00:00
'section-title' => $event -> getExtraParam ( 'section-title' ),
2016-08-09 16:20:50 +00:00
'notifyAgent' => $event -> getExtraParam ( 'notifyAgent' ),
'subject-name' => $event -> getExtraParam ( 'subject-name' ),
2016-06-21 14:42:56 +00:00
);
return false ;
}
);
// enable mention failure and success notifications
$this -> setMwGlobals ( 'wgEchoMentionStatusNotifications' , true );
2016-08-09 16:20:50 +00:00
// enable multiple sections mentions
$this -> setMwGlobals ( 'wgEchoMentionsOnMultipleSectionEdits' , true );
2016-06-21 14:42:56 +00:00
EchoDiscussionParser :: generateEventsForRevision ( $revision );
$this -> assertEquals ( $expected , $events );
}
2016-08-09 16:20:50 +00:00
public function provider_extractSections () {
return array (
array (
'content' => 'Just Text.' ,
'result' => array (
array (
'header' => false ,
'content' => 'Just Text.' ,
),
),
),
array (
'content' =>
<<< TEXT
Text and a
== Headline ==
with text
TEXT
,
'result' => array (
array (
'header' => false ,
'content' =>
<<< TEXT
Text and a
TEXT
,
),
array (
2016-08-09 16:20:50 +00:00
'header' => 'Headline' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
== Headline ==
with text
TEXT
,
),
),
),
array (
'content' =>
<<< TEXT
== Headline ==
Text and a [[ User : Test ]]
TEXT
,
'result' => array (
array (
2016-08-09 16:20:50 +00:00
'header' => 'Headline' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
== Headline ==
Text and a [[ User : Test ]]
TEXT
,
),
),
),
array (
'content' =>
<<< TEXT
Content 0
== Headline 1 ==
Content 1
=== Headline 2 ===
Content 2
TEXT
,
'result' => array (
array (
'header' => false ,
'content' => 'Content 0' ,
),
array (
2016-08-09 16:20:50 +00:00
'header' => 'Headline 1' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
== Headline 1 ==
Content 1
TEXT
,
),
array (
2016-08-09 16:20:50 +00:00
'header' => 'Headline 2' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
=== Headline 2 ===
Content 2
TEXT
,
),
),
),
array (
'content' =>
<<< TEXT
== Headline 1 ==
مرحبا كيف حالك
=== Headline 2 ===
انا بخير شكرا
TEXT
,
'result' => array (
array (
2016-08-09 16:20:50 +00:00
'header' => 'Headline 1' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
== Headline 1 ==
مرحبا كيف حالك
TEXT
,
),
array (
2016-08-09 16:20:50 +00:00
'header' => 'Headline 2' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
=== Headline 2 ===
انا بخير شكرا
TEXT
,
),
),
),
array (
'content' =>
<<< TEXT
مرحبا كيف حالك
=== Headline 1 ===
انا بخير شكرا
TEXT
,
'result' => array (
array (
'header' => false ,
'content' =>
<<< TEXT
مرحبا كيف حالك
TEXT
,
),
array (
2016-08-09 16:20:50 +00:00
'header' => 'Headline 1' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
=== Headline 1 ===
انا بخير شكرا
TEXT
,
),
),
),
);
}
/**
* @ dataProvider provider_extractSections
*/
public function testExtractSections ( $content , $result ) {
$discussionParser = TestingAccessWrapper :: newFromClass ( 'EchoDiscussionParser' );
$sections = $discussionParser -> extractSections ( $content );
$this -> assertEquals ( $result , $sections );
}
2016-06-21 14:42:56 +00:00
public function testGenerateEventsForRevision_tooManyMentionsFailure () {
$expected = array (
array (
2016-07-27 15:04:49 +00:00
'type' => 'mention-failure-too-many' ,
2016-06-21 14:42:56 +00:00
'agent' => 'Admin' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Hello Users' ,
2016-06-21 14:42:56 +00:00
'max-mentions' => 5 ,
),
);
$revision = $this -> setupTestRevisionsForEventGeneration ( 747747749 , 747747747 , 'Admin' , 'en' , array (), 'UTPage' );
$events = array ();
$this -> setupEventCallbackForEventGeneration (
function ( EchoEvent $event ) use ( & $events ) {
$events [] = array (
'type' => $event -> getType (),
'agent' => $event -> getAgent () -> getName (),
2016-07-05 14:45:14 +00:00
'section-title' => $event -> getExtraParam ( 'section-title' ),
2016-06-21 14:42:56 +00:00
'max-mentions' => $event -> getExtraParam ( 'max-mentions' ),
);
return false ;
}
);
$this -> setMwGlobals ( array (
// enable mention failure and success notifications
'wgEchoMentionStatusNotifications' => true ,
2016-07-27 15:04:49 +00:00
// lower limit for the mention-failure-too-many notification
2016-06-21 14:42:56 +00:00
'wgEchoMaxMentionsCount' => 5
) );
EchoDiscussionParser :: generateEventsForRevision ( $revision );
$this -> assertEquals ( $expected , $events );
}
private function setupTestRevisionsForEventGeneration ( $newId , $oldId , $username , $lang , $pages , $title ) {
2016-05-04 08:45:49 +00:00
$langObj = Language :: factory ( $lang );
2015-02-17 10:10:49 +00:00
$this -> setMwGlobals ( array (
// this global is used by the code that interprets the namespace part of
// titles (Title::getTitleParser), so should be the fake language ;)
2016-05-04 08:45:49 +00:00
'wgContLang' => $langObj ,
2015-02-17 10:10:49 +00:00
// this one allows Mediawiki:xyz pages to be set as messages
'wgUseDatabaseMessages' => true
) );
2015-02-05 19:45:40 +00:00
2016-05-04 08:45:49 +00:00
// Since we reset the $wgContLang global, reset the TitleParser service
$services = MediaWikiServices :: getInstance ();
if ( is_callable ( [ $services , 'getTitleParser' ] ) ) {
2016-05-11 17:13:01 +00:00
// TODO: All of this should use $this->setService()
$services -> resetServiceForTesting ( 'TitleParser' );
$services -> redefineService ( 'TitleParser' , function () use ( $langObj ) {
global $wgLocalInterwikis ;
return new MediaWikiTitleCodec (
2016-05-04 08:45:49 +00:00
$langObj ,
new GenderCache (),
2016-05-11 17:13:01 +00:00
$wgLocalInterwikis
);
} );
// Cleanup
$lock = new ScopedCallback ( function () use ( $services ) {
$services -> resetServiceForTesting ( 'TitleParser' );
} );
2016-05-04 08:45:49 +00:00
}
2015-02-05 19:45:40 +00:00
// pages to be created: templates may be used to ping users (e.g.
// {{u|...}}) but if we don't have that template, it just won't work!
2015-02-17 10:10:49 +00:00
$pages += array ( $title => '' );
foreach ( $pages as $pageTitle => $pageText ) {
$template = WikiPage :: factory ( Title :: newFromText ( $pageTitle ) );
$template -> doEditContent ( new WikitextContent ( $pageText ), '' );
2015-02-05 19:45:40 +00:00
}
2015-02-17 10:10:49 +00:00
// force i18n messages to be reloaded (from DB, where a new message
// might have been created as page)
MessageCache :: destroyInstance ();
2015-02-05 19:45:40 +00:00
// grab revision excerpts (didn't include them in this src file because
// they can be pretty long)
$oldText = file_get_contents ( __DIR__ . '/revision_txt/' . $oldId . '.txt' );
$newText = file_get_contents ( __DIR__ . '/revision_txt/' . $newId . '.txt' );
// revision texts can be in different languages, where links etc are
// different (e.g. User: becomes Benutzer: in German), so let's pretend
// the page they belong to is from that language
2015-02-17 10:10:49 +00:00
$title = Title :: newFromText ( $title );
2015-02-05 19:45:40 +00:00
$object = new ReflectionObject ( $title );
$property = $object -> getProperty ( 'mDbPageLanguage' );
$property -> setAccessible ( true );
$property -> setValue ( $title , $lang );
// create stub Revision object
$row = array (
'id' => $newId ,
'user_text' => $username ,
'user' => User :: newFromName ( $username ) -> getId (),
'parent_id' => $oldId ,
'text' => $newText ,
'title' => $title ,
);
$revision = Revision :: newFromRow ( $row );
// generate diff between 2 revisions
$changes = EchoDiscussionParser :: getMachineReadableDiff ( $oldText , $newText );
2015-02-17 10:10:49 +00:00
$output = EchoDiscussionParser :: interpretDiff ( $changes , $revision -> getUserText (), $title );
2015-02-05 19:45:40 +00:00
// store diff in some local cache var, to circumvent
// EchoDiscussionParser::getChangeInterpretationForRevision's attempt to
// retrieve parent revision from DB
$class = new ReflectionClass ( 'EchoDiscussionParser' );
$property = $class -> getProperty ( 'revisionInterpretationCache' );
$property -> setAccessible ( true );
$property -> setValue ( array ( $revision -> getId () => $output ) );
2016-06-21 14:42:56 +00:00
return $revision ;
}
2015-02-05 19:45:40 +00:00
2016-06-21 14:42:56 +00:00
private function setupEventCallbackForEventGeneration ( callable $callback ) {
2015-02-05 19:45:40 +00:00
// to catch the generated event, I'm going to attach a callback to the
// hook that's being run just prior to sending the notifications out
// can't use setMwGlobals here, so I'll just re-attach to the same key
// for every dataProvider value (and don't worry, I'm removing it on
// tearDown too - I just felt the attaching should be happening here
// instead of on setUp, or code would get too messy)
global $wgHooks ;
$wgHooks [ 'BeforeEchoEventInsert' ][ 999 ] = $callback ;
}
2012-07-31 00:29:49 +00:00
// TODO test cases for:
// - stripHeader
// - stripIndents
// - stripSignature
2013-04-29 23:24:46 +00:00
public function testDiscussionParserAcceptsInternalDiff () {
global $wgDiff ;
$origWgDiff = $wgDiff ;
$wgDiff = '/does/not/exist/or/at/least/we/hope/not' ;
try {
$res = EchoDiscussionParser :: getMachineReadableDiff (
<<< TEXT
line 1
line 2
line 3
line 4
TEXT
2015-10-29 11:19:49 +00:00
,
2013-04-29 23:24:46 +00:00
<<< TEXT
line 1
line c
line 4
TEXT
);
} catch ( MWException $e ) {
$wgDiff = $origWgDiff ;
throw $e ;
}
$wgDiff = $origWgDiff ;
// Test failure occurs when MWException is thrown due to parsing failure
$this -> assertTrue ( true );
}
2012-07-31 00:29:49 +00:00
public function testTimestampRegex () {
$exemplarTimestamp = self :: getExemplarTimestamp ();
$timestampRegex = EchoDiscussionParser :: getTimestampRegex ();
2012-08-31 21:50:46 +00:00
$match = preg_match ( '/' . $timestampRegex . '/u' , $exemplarTimestamp );
2012-07-31 00:29:49 +00:00
$this -> assertEquals ( 1 , $match );
}
2014-01-19 13:41:22 +00:00
public function testGetTimestampPosition () {
2015-10-01 13:48:52 +00:00
$line = 'Hello World. ' . self :: getExemplarTimestamp ();
2014-01-19 13:41:22 +00:00
$pos = EchoDiscussionParser :: getTimestampPosition ( $line );
$this -> assertEquals ( 13 , $pos );
}
2012-07-31 00:29:49 +00:00
/**
* @ dataProvider signingDetectionData
* FIXME some of the app logic is in the test ...
*/
public function testSigningDetection ( $line , $expectedUser ) {
2012-08-31 21:50:46 +00:00
if ( ! EchoDiscussionParser :: isSignedComment ( $line ) ) {
2012-07-31 00:29:49 +00:00
$this -> assertEquals ( $expectedUser , false );
2015-10-01 13:48:52 +00:00
2012-07-31 00:29:49 +00:00
return ;
}
2015-01-31 01:00:06 +00:00
$output = EchoDiscussionParser :: getUserFromLine ( $line );
2012-07-31 00:29:49 +00:00
if ( $output === false ) {
$this -> assertEquals ( false , $expectedUser );
} elseif ( is_array ( $expectedUser ) ) {
// Sometimes testing for correct user detection,
// sometimes testing for offset detection
$this -> assertEquals ( $expectedUser , $output );
} else {
$this -> assertEquals ( $expectedUser , $output [ 1 ] );
}
}
public function signingDetectionData () {
$ts = self :: getExemplarTimestamp ();
2015-10-01 13:48:52 +00:00
2012-07-31 00:29:49 +00:00
return array (
// Basic
2014-09-29 19:38:01 +00:00
array (
2015-01-31 01:00:06 +00:00
" I like this. [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2014-09-29 19:38:01 +00:00
array (
13 ,
'Werdna'
),
),
2012-07-31 00:29:49 +00:00
// Confounding
2014-09-29 19:38:01 +00:00
array (
2015-01-31 01:00:06 +00:00
" [[User:Jorm]] is a meanie. --[[User:Werdna2|Andrew]] $ts " ,
2014-09-29 19:38:01 +00:00
array (
29 ,
2015-01-31 01:00:06 +00:00
" Werdna2 "
2014-09-29 19:38:01 +00:00
),
),
2012-07-31 00:29:49 +00:00
// Talk page link only
2014-09-29 19:38:01 +00:00
array (
2015-01-31 01:00:06 +00:00
" [[User:Swalling|Steve]] is the best person I have ever met. --[[User talk:Werdna3|Andrew]] $ts " ,
2014-09-29 19:38:01 +00:00
array (
62 ,
2015-01-31 01:00:06 +00:00
'Werdna3'
2014-09-29 19:38:01 +00:00
),
),
2012-07-31 00:29:49 +00:00
// Anonymous user
2014-09-29 19:38:01 +00:00
array (
2015-01-31 01:00:06 +00:00
" I am anonymous because I like my IP address. --[[Special:Contributions/127.0.0.1|127.0.0.1]] $ts " ,
2014-09-29 19:38:01 +00:00
array (
47 ,
'127.0.0.1'
),
),
2012-07-31 00:29:49 +00:00
// No signature
2014-09-29 19:38:01 +00:00
array (
2015-10-01 13:48:52 +00:00
" Well, \n I do think that [[User:Newyorkbrad]] is pretty cool, but what do I know? " ,
2014-09-29 19:38:01 +00:00
false
),
2012-07-31 00:29:49 +00:00
// Hash symbols in usernames
2014-09-29 19:38:01 +00:00
array (
" What do you think? [[User talk:We buried our secrets in the garden#top|wbositg]] $ts " ,
array (
19 ,
'We buried our secrets in the garden'
),
),
// Title that gets normalized different than it is provided in the wikitext
array (
" Beep boop [[User:I_Heart_Spaces]] ([[User_talk:I_Heart_Spaces]]) $ts " ,
array (
2014-11-14 23:23:52 +00:00
strlen ( " Beep boop " ),
2014-09-29 19:38:01 +00:00
'I Heart Spaces'
),
),
// Accepts ] in the pipe
array (
2015-01-31 01:00:06 +00:00
" Shake n Bake --[[User:Werdna4|wer]dna]] $ts " ,
2014-09-29 19:38:01 +00:00
array (
2014-11-14 23:23:52 +00:00
strlen ( " Shake n Bake -- " ),
2015-01-31 01:00:06 +00:00
'Werdna4' ,
2014-09-29 19:38:01 +00:00
),
),
2014-11-14 23:23:52 +00:00
array (
" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? [[User:Jam]] $ts " ,
array (
strlen ( " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? " ),
" Jam "
),
),
// extra long signature
array (
2015-01-31 01:00:06 +00:00
" { { U|He7d3r}}, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? [[User:Reverta-me|<span style= \" font-size:13px; color:blue;font-family:Lucida Handwriting;text-shadow:aqua 5px 3px 12px; \" >Aaaaa Bbbbbbb</span>]]'' <sup>[[User Talk:Reverta-me|<font color= \" gold \" face= \" Lucida Calligraphy \" >Discussão</font>]]</sup>'' " ,
2014-11-14 23:23:52 +00:00
array (
strlen ( " { { U|He7d3r}}, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? " ),
'Reverta-me' ,
),
),
2015-01-31 01:00:06 +00:00
// Bug: T87852
array (
2015-02-05 19:45:40 +00:00
" Test --[[Benutzer:Schnark]] ([[Benutzer:Schnark/js|js]]) " ,
2015-01-31 01:00:06 +00:00
array (
strlen ( " Test -- " ),
'Schnark' ,
),
),
// when adding additional tests, make sure to add the non-anon users
// to EchoDiscussionParserTest::$testusers - the DiscussionParser
// needs the users to exist, because it'll generate a comparison
// signature, which is different when the user is considered anon
2012-07-31 00:29:49 +00:00
);
}
/** @dataProvider diffData */
public function testDiff ( $oldText , $newText , $expected ) {
$actual = EchoDiscussionParser :: getMachineReadableDiff ( $oldText , $newText );
unset ( $actual [ '_info' ] );
unset ( $expected [ '_info' ] );
$this -> assertEquals ( $expected , $actual );
}
public function diffData () {
return array (
array (
<<< TEXT
line 1
line 2
line 3
line 4
TEXT
2015-10-01 13:48:52 +00:00
, <<< TEXT
2012-07-31 00:29:49 +00:00
line 1
line 3
line 4
TEXT
2015-10-01 13:48:52 +00:00
,
2012-07-31 00:29:49 +00:00
array ( array (
'action' => 'subtract' ,
'content' => 'line 2' ,
'left-pos' => 2 ,
'right-pos' => 2 ,
) )
),
array (
<<< TEXT
line 1
line 2
line 3
line 4
TEXT
2015-10-01 13:48:52 +00:00
, <<< TEXT
2012-07-31 00:29:49 +00:00
line 1
line 2
line 2.5
line 3
line 4
TEXT
2015-10-01 13:48:52 +00:00
,
2012-07-31 00:29:49 +00:00
array ( array (
'action' => 'add' ,
'content' => 'line 2.5' ,
'left-pos' => 3 ,
'right-pos' => 3 ,
) )
),
array (
<<< TEXT
line 1
line 2
line 3
line 4
TEXT
2015-10-01 13:48:52 +00:00
, <<< TEXT
2012-07-31 00:29:49 +00:00
line 1
line b
line 3
line 4
TEXT
2015-10-01 13:48:52 +00:00
,
2012-07-31 00:29:49 +00:00
array ( array (
'action' => 'change' ,
'old_content' => 'line 2' ,
'new_content' => 'line b' ,
'left-pos' => 2 ,
'right-pos' => 2 ,
) )
),
array (
<<< TEXT
line 1
line 2
line 3
line 4
TEXT
2015-10-01 13:48:52 +00:00
, <<< TEXT
2012-07-31 00:29:49 +00:00
line 1
line b
line c
line d
line 3
line 4
TEXT
2015-10-01 13:48:52 +00:00
,
2012-07-31 00:29:49 +00:00
array (
array (
'action' => 'change' ,
'old_content' => 'line 2' ,
'new_content' => 'line b' ,
'left-pos' => 2 ,
'right-pos' => 2 ,
),
array (
'action' => 'add' ,
'content' => ' line c
line d ' ,
'left-pos' => 3 ,
'right-pos' => 3 ,
),
),
),
);
}
/** @dataProvider annotationData */
2013-05-19 04:15:45 +00:00
public function testAnnotation ( $message , $diff , $user , $expectedAnnotation ) {
2012-07-31 00:29:49 +00:00
$actual = EchoDiscussionParser :: interpretDiff ( $diff , $user );
2013-05-19 04:15:45 +00:00
$this -> assertEquals ( $expectedAnnotation , $actual , $message );
2012-07-31 00:29:49 +00:00
}
public function annotationData () {
$ts = self :: getExemplarTimestamp ();
return array (
2013-05-19 04:15:45 +00:00
2012-07-31 00:29:49 +00:00
array (
2013-05-19 04:15:45 +00:00
'Must detect added comments' ,
2012-07-31 00:29:49 +00:00
// Diff
array (
// Action
array (
'action' => 'add' ,
2015-01-31 01:00:06 +00:00
'content' => " :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
'left-pos' => 3 ,
'right-pos' => 3 ,
),
'_info' => array (
'lhs' => array (
'== Section 1 ==' ,
2015-01-31 01:00:06 +00:00
" I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
),
'rhs' => array (
'== Section 1 ==' ,
2015-01-31 01:00:06 +00:00
" I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts " ,
" :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
),
),
),
// User
'Werdna' ,
// Expected annotation
array (
array (
'type' => 'add-comment' ,
2015-01-31 01:00:06 +00:00
'content' => " :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
'full-section' => <<< TEXT
== Section 1 ==
2015-01-31 01:00:06 +00:00
I do not like you . [[ User : Jorm | Jorm ]] ([[ User talk : Jorm | talk ]]) $ts
: What do you think ? [[ User : Werdna | Werdna ]] ([[ User talk : Werdna | talk ]]) $ts
2012-07-31 00:29:49 +00:00
TEXT
),
),
),
2013-05-19 04:15:45 +00:00
2012-07-31 00:29:49 +00:00
array (
2013-05-19 04:15:45 +00:00
'Full Section must not include the following pre-existing section' ,
2012-07-31 00:29:49 +00:00
// Diff
array (
// Action
array (
'action' => 'add' ,
2015-01-31 01:00:06 +00:00
'content' => " :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
'left-pos' => 3 ,
'right-pos' => 3 ,
),
'_info' => array (
'lhs' => array (
'== Section 1 ==' ,
2015-01-31 01:00:06 +00:00
" I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
'== Section 2 ==' ,
2015-01-31 01:00:06 +00:00
" Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
),
'rhs' => array (
'== Section 1 ==' ,
2015-01-31 01:00:06 +00:00
" I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts " ,
" :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
'== Section 2 ==' ,
2015-01-31 01:00:06 +00:00
" Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
),
),
),
// User
'Werdna' ,
// Expected annotation
array (
array (
'type' => 'add-comment' ,
2015-01-31 01:00:06 +00:00
'content' => " :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
'full-section' => <<< TEXT
== Section 1 ==
2015-01-31 01:00:06 +00:00
I do not like you . [[ User : Jorm | Jorm ]] ([[ User talk : Jorm | talk ]]) $ts
: What do you think ? [[ User : Werdna | Werdna ]] ([[ User talk : Werdna | talk ]]) $ts
2012-07-31 00:29:49 +00:00
TEXT
),
),
),
2013-05-19 04:15:45 +00:00
2012-07-31 00:29:49 +00:00
array (
2013-05-19 04:15:45 +00:00
'Must detect new-section-with-comment when a new section is added' ,
2012-07-31 00:29:49 +00:00
// Diff
array (
// Action
array (
'action' => 'add' ,
'content' => <<< TEXT
== Section 1 a ==
2015-01-31 01:00:06 +00:00
Hmmm ? [[ User : Jdforrester | Jdforrester ]] ([[ User talk : Jdforrester | talk ]]) $ts
2012-07-31 00:29:49 +00:00
TEXT
2015-10-01 13:48:52 +00:00
,
2012-07-31 00:29:49 +00:00
'left-pos' => 4 ,
'right-pos' => 4 ,
),
'_info' => array (
'lhs' => array (
'== Section 1 ==' ,
2015-01-31 01:00:06 +00:00
" I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts " ,
" :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
'== Section 2 ==' ,
2015-01-31 01:00:06 +00:00
" Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
),
'rhs' => array (
'== Section 1 ==' ,
2015-01-31 01:00:06 +00:00
" I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts " ,
" :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
'== Section 1a ==' ,
2015-01-31 01:00:06 +00:00
'Hmmm? [[User:Jdforrester|Jdforrester]] ([[User talk:Jdforrested|talk]]) $ts' ,
2012-07-31 00:29:49 +00:00
'== Section 2 ==' ,
2015-01-31 01:00:06 +00:00
" Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts " ,
2012-07-31 00:29:49 +00:00
),
),
),
// User
'Jdforrester' ,
// Expected annotation
array (
array (
'type' => 'new-section-with-comment' ,
'content' => <<< TEXT
== Section 1 a ==
2015-01-31 01:00:06 +00:00
Hmmm ? [[ User : Jdforrester | Jdforrester ]] ([[ User talk : Jdforrester | talk ]]) $ts
2012-07-31 00:29:49 +00:00
TEXT
2015-10-01 13:48:52 +00:00
,
2012-07-31 00:29:49 +00:00
),
),
),
2013-05-19 04:15:45 +00:00
array (
'Must detect multiple added comments when multiple sections are edited' ,
EchoDiscussionParser :: getMachineReadableDiff (
<<< TEXT
== Section 1 ==
2015-01-31 01:00:06 +00:00
I do not like you . [[ User : Jorm | Jorm ]] ([[ User talk : Jorm | talk ]]) $ts
: What do you think ? [[ User : Werdna | Werdna ]] ([[ User talk : Werdna | talk ]]) $ts
2013-05-19 04:15:45 +00:00
== Section 2 ==
2015-01-31 01:00:06 +00:00
Well well well . [[ User : DarTar | DarTar ]] ([[ User talk : DarTar | talk ]]) $ts
2013-05-19 04:15:45 +00:00
== Section 3 ==
2015-01-31 01:00:06 +00:00
Hai [[ User : Bsitu | Bsitu ]] ([[ User talk : Bsitu | talk ]]) $ts
2013-05-19 04:15:45 +00:00
TEXT
2015-10-29 11:19:49 +00:00
,
2013-05-19 04:15:45 +00:00
<<< TEXT
== Section 1 ==
2015-01-31 01:00:06 +00:00
I do not like you . [[ User : Jorm | Jorm ]] ([[ User talk : Jorm | talk ]]) $ts
: What do you think ? [[ User : Werdna | Werdna ]] ([[ User talk : Werdna | talk ]]) $ts
: New Comment [[ User : JarJar | JarJar ]] ([[ User talk : JarJar | talk ]]) $ts
2013-05-19 04:15:45 +00:00
== Section 2 ==
2015-01-31 01:00:06 +00:00
Well well well . [[ User : DarTar | DarTar ]] ([[ User talk : DarTar | talk ]]) $ts
2013-05-19 04:15:45 +00:00
== Section 3 ==
2015-01-31 01:00:06 +00:00
Hai [[ User : Bsitu | Bsitu ]] ([[ User talk : Bsitu | talk ]]) $ts
: Other New Comment [[ User : JarJar | JarJar ]] ([[ User talk : JarJar | talk ]]) $ts
2013-05-19 04:15:45 +00:00
TEXT
),
// User
'JarJar' ,
// Expected annotation
array (
array (
'type' => 'add-comment' ,
2015-01-31 01:00:06 +00:00
'content' => " :New Comment [[User:JarJar|JarJar]] ([[User talk:JarJar|talk]]) $ts " ,
2013-05-19 04:15:45 +00:00
'full-section' => <<< TEXT
== Section 1 ==
2015-01-31 01:00:06 +00:00
I do not like you . [[ User : Jorm | Jorm ]] ([[ User talk : Jorm | talk ]]) $ts
: What do you think ? [[ User : Werdna | Werdna ]] ([[ User talk : Werdna | talk ]]) $ts
: New Comment [[ User : JarJar | JarJar ]] ([[ User talk : JarJar | talk ]]) $ts
2013-05-19 04:15:45 +00:00
TEXT
),
array (
'type' => 'add-comment' ,
2015-01-31 01:00:06 +00:00
'content' => " :Other New Comment [[User:JarJar|JarJar]] ([[User talk:JarJar|talk]]) $ts " ,
2013-05-19 04:15:45 +00:00
'full-section' => <<< TEXT
== Section 3 ==
2015-01-31 01:00:06 +00:00
Hai [[ User : Bsitu | Bsitu ]] ([[ User talk : Bsitu | talk ]]) $ts
: Other New Comment [[ User : JarJar | JarJar ]] ([[ User talk : JarJar | talk ]]) $ts
2013-05-19 04:15:45 +00:00
TEXT
),
),
2015-01-31 01:00:06 +00:00
),
2013-05-19 04:15:45 +00:00
2015-01-31 01:00:06 +00:00
array (
'Bug T78424' ,
EchoDiscussionParser :: getMachineReadableDiff (
<<< TEXT
== Washington Post Reception Source ==
'' The Boston Post '' source that was used in the reception section has a couple of problems . First , it 's actually a repost of ' 'The Washington Post' ', but ' 'The Washington Post' ' doesn' t allow the Internet Archive to preserve it . Should it still be sourced to Boston or to Washington ? Second , it seems to be a lot of analysis that can 't be summed up easily without trimming it out, and doesn' t really fit with the reception section and should probably moved next to Wilson ' s testimony . Any suggestions ? -- [[ User : RAN1 | RAN1 ]] ([[ User talk : RAN1 | talk ]]) 01 : 44 , 11 December 2014 ( UTC )
TEXT
2015-10-29 11:19:49 +00:00
,
2015-01-31 01:00:06 +00:00
<<< TEXT
== Washington Post Reception Source ==
'' The Boston Post '' source that was used in the reception section has a couple of problems . First , it 's actually a repost of ' 'The Washington Post' ', but ' 'The Washington Post' ' doesn' t allow the Internet Archive to preserve it . Should it still be sourced to Boston or to Washington ? Second , it seems to be a lot of analysis that can 't be summed up easily without trimming it out, and doesn' t really fit with the reception section and should probably moved next to Wilson ' s testimony . Any suggestions ? -- [[ User : RAN1 | RAN1 ]] ([[ User talk : RAN1 | talk ]]) 01 : 44 , 11 December 2014 ( UTC )
== Grand jury no bill reception ==
{{ u | Bob K31416 }} has started a process of summarizing that section , in a manner that I believe it to be counter productive . We have expert opinions from legal , law enforcement , politicians , and media outlets all of which are notable and informative . [[ WP : NOTPAPER | Wikipedia is not paper ]] – If the section is too long , the correct process to avoid losing good content that is well sources , is to create a sub - article with all the detail , and summarize here per [[ WP : SUMMARY ]] . But deleting useful and well sourced material , is not acceptable . We are here to build an encyclopedia . - [[ User : Cwobeel |< span style = " color:#339966 " > Cwobeel </ span > ]] [[ User_talk : Cwobeel |< span style = " font-size:80% " > ( talk ) </ span > ]] 16 : 02 , 11 December 2014 ( UTC )
TEXT
),
// User
'Cwobeel' ,
// Expected annotation
array (
array (
'type' => 'new-section-with-comment' ,
'content' => ' == Grand jury no bill reception ==
{{ u | Bob K31416 }} has started a process of summarizing that section , in a manner that I believe it to be counter productive . We have expert opinions from legal , law enforcement , politicians , and media outlets all of which are notable and informative . [[ WP : NOTPAPER | Wikipedia is not paper ]] – If the section is too long , the correct process to avoid losing good content that is well sources , is to create a sub - article with all the detail , and summarize here per [[ WP : SUMMARY ]] . But deleting useful and well sourced material , is not acceptable . We are here to build an encyclopedia . - [[ User : Cwobeel |< span style = " color:#339966 " > Cwobeel </ span > ]] [[ User_talk : Cwobeel |< span style = " font-size:80% " > ( talk ) </ span > ]] 16 : 02 , 11 December 2014 ( UTC ) ' ,
),
),
2013-05-19 04:15:45 +00:00
),
2015-01-31 01:00:06 +00:00
// when adding additional tests, make sure to add the non-anon users
// to EchoDiscussionParserTest::$testusers - the DiscussionParser
// needs the users to exist, because it'll generate a comparison
// signature, which is different when the user is considered anon
2012-07-31 00:29:49 +00:00
);
}
public static function getExemplarTimestamp () {
$title = Title :: newMainPage ();
2012-08-30 16:04:39 +00:00
$user = User :: newFromName ( 'Test' );
2012-07-31 00:29:49 +00:00
$options = new ParserOptions ;
global $wgParser ;
$exemplarTimestamp =
$wgParser -> preSaveTransform ( '~~~~~' , $title , $user , $options );
return $exemplarTimestamp ;
}
2013-05-17 19:16:46 +00:00
2015-10-29 11:25:14 +00:00
public static function provider_detectSectionTitleAndText () {
2015-01-31 01:00:06 +00:00
$name = 'Werdna' ; // See EchoDiscussionParserTest::$testusers
2013-05-17 19:16:46 +00:00
$comment = self :: signedMessage ( $name );
return array (
array (
'Must detect first sub heading when inserting in the middle of two sub headings' ,
2014-09-29 19:38:01 +00:00
// expected header content
2013-05-17 19:16:46 +00:00
'Sub Heading 1' ,
2014-09-29 19:38:01 +00:00
// test content format
2013-05-17 19:16:46 +00:00
"
== Heading ==
$comment
== Sub Heading 1 ==
$comment
% s
== Sub Heading 2 ==
$comment
" ,
2014-09-29 19:38:01 +00:00
// user signing new comment
2013-05-17 19:16:46 +00:00
$name
),
array (
'Must detect second sub heading when inserting in the end of two sub headings' ,
2014-09-29 19:38:01 +00:00
// expected header content
2013-05-17 19:16:46 +00:00
'Sub Heading 2' ,
2014-09-29 19:38:01 +00:00
// test content format
2013-05-17 19:16:46 +00:00
"
== Heading ==
$comment
== Sub Heading 1 ==
$comment
== Sub Heading 2 ==
$comment
% s
" ,
2014-09-29 19:38:01 +00:00
// user signing new comment
2013-05-17 19:16:46 +00:00
$name
),
array (
'Commenting in multiple sub-headings must result in no section link' ,
2014-09-29 19:38:01 +00:00
// expected header content
2013-05-17 19:16:46 +00:00
'' ,
2014-09-29 19:38:01 +00:00
// test content format
2013-05-17 19:16:46 +00:00
"
== Heading ==
$comment
== Sub Heading 1 ==
$comment
% s
== Sub Heading 2 ==
$comment
% s
" ,
2014-09-29 19:38:01 +00:00
// user signing new comment
2013-05-17 19:16:46 +00:00
$name
),
array (
'Must accept headings without a space between the = and the section name' ,
2014-09-29 19:38:01 +00:00
// expected header content
2013-05-17 19:16:46 +00:00
'Heading' ,
2014-09-29 19:38:01 +00:00
// test content format
2013-05-17 19:16:46 +00:00
"
== Heading ==
$comment
% s
" ,
2014-09-29 19:38:01 +00:00
// user signing new comment
2013-05-17 19:16:46 +00:00
$name
),
array (
'Must not accept invalid headings split with a return' ,
2014-09-29 19:38:01 +00:00
// expected header content
2013-05-17 19:16:46 +00:00
'' ,
2014-09-29 19:38:01 +00:00
// test content format
2013-05-17 19:16:46 +00:00
"
== Some
Heading ==
$comment
% s
" ,
2014-09-29 19:38:01 +00:00
// user signing new comment
2013-05-17 19:16:46 +00:00
$name
),
);
}
/**
2013-07-24 03:50:43 +00:00
* @ dataProvider provider_detectSectionTitleAndText
2013-05-17 19:16:46 +00:00
*/
2014-09-29 19:38:01 +00:00
public function testDetectSectionTitleAndText ( $message , $expect , $format , $name ) {
2015-01-31 01:00:06 +00:00
// str_replace because we want to replace multiple instances of '%s' with the same value
2013-05-17 19:16:46 +00:00
$before = str_replace ( '%s' , '' , $format );
$after = str_replace ( '%s' , self :: signedMessage ( $name ), $format );
$diff = EchoDiscussionParser :: getMachineReadableDiff ( $before , $after );
$interp = EchoDiscussionParser :: interpretDiff ( $diff , $name );
2013-07-24 03:50:43 +00:00
// There should be a section-text only if there is section-title
$expectText = $expect ? self :: message ( $name ) : '' ;
$this -> assertEquals (
array ( 'section-title' => $expect , 'section-text' => $expectText ),
2015-01-31 01:00:06 +00:00
EchoDiscussionParser :: detectSectionTitleAndText ( $interp ),
$message
2013-07-24 03:50:43 +00:00
);
2013-05-17 19:16:46 +00:00
}
protected static function signedMessage ( $name ) {
2013-07-24 03:50:43 +00:00
return " : " . self :: message () . " [[User: $name | $name ]] ([[User talk: $name |talk]]) 00:17, 7 May 2013 (UTC) " ;
}
protected static function message () {
return 'foo' ;
2013-05-17 19:16:46 +00:00
}
2015-10-29 11:25:14 +00:00
public static function provider_getFullSection () {
2013-05-17 19:16:46 +00:00
$tests = array (
array (
'Extracts full section' ,
// Full document content
<<< TEXT
== Header 1 ==
foo
=== Header 2 ===
bar
== Header 3 ==
baz
TEXT
2015-10-01 13:48:52 +00:00
,
2013-05-17 19:16:46 +00:00
// Map of Line numbers to expanded section content
array (
1 => " ==Header 1== \n foo " ,
2 => " ==Header 1== \n foo " ,
3 => " ===Header 2=== \n bar " ,
4 => " ===Header 2=== \n bar " ,
5 => " ==Header 3== \n baz " ,
6 => " ==Header 3== \n baz " ,
),
),
);
// Allow for setting an array of line numbers to expand from rather than
// just a single line number
$retval = array ();
foreach ( $tests as $test ) {
foreach ( $test [ 2 ] as $lineNum => $expected ) {
$retval [] = array (
$test [ 0 ],
$expected ,
$test [ 1 ],
$lineNum ,
);
}
}
return $retval ;
}
/**
* @ dataProvider provider_getFullSection
*/
public function testGetFullSection ( $message , $expect , $lines , $startLineNum ) {
$section = EchoDiscussionParser :: getFullSection ( explode ( " \n " , $lines ), $startLineNum );
$this -> assertEquals ( $expect , $section , $message );
}
public function testGetSectionCount () {
$one = " ==Zomg== \n foobar \n " ;
$two = " ===SubZomg=== \n Hi there \n " ;
$three = " ==Header== \n Oh Hai! \n " ;
$this -> assertEquals ( 1 , EchoDiscussionParser :: getSectionCount ( $one ) );
$this -> assertEquals ( 2 , EchoDiscussionParser :: getSectionCount ( $one . $two ) );
$this -> assertEquals ( 2 , EchoDiscussionParser :: getSectionCount ( $one . $three ) );
$this -> assertEquals ( 3 , EchoDiscussionParser :: getSectionCount ( $one . $two . $three ) );
2016-08-03 13:02:59 +00:00
$this -> assertEquals ( 30 , EchoDiscussionParser :: getSectionCount (
file_get_contents ( __DIR__ . '/revision_txt/637638133.txt' )
) );
2013-05-17 19:16:46 +00:00
}
2015-02-17 21:23:26 +00:00
2016-07-05 14:45:14 +00:00
public function testGetOverallUserMentionsCount () {
$userMentions = array (
'validMentions' => array ( 1 => 1 ),
'unknownUsers' => array ( 'NotKnown1' , 'NotKnown2' ),
'anonymousUsers' => array ( '127.0.0.1' ),
);
$discussionParser = TestingAccessWrapper :: newFromClass ( 'EchoDiscussionParser' );
$this -> assertEquals ( 4 , $discussionParser -> getOverallUserMentionsCount ( $userMentions ) );
}
public function provider_getUserMentions () {
return array (
array (
array ( 'NotKnown1' => 0 ),
array (
'validMentions' => array (),
'unknownUsers' => array ( 'NotKnown1' ),
'anonymousUsers' => array (),
),
1
),
array (
array ( '127.0.0.1' => 0 ),
array (
'validMentions' => array (),
'unknownUsers' => array (),
'anonymousUsers' => array ( '127.0.0.1' ),
),
1
),
);
}
/**
* @ dataProvider provider_getUserMentions
*/
public function testGetUserMentions ( $userLinks , $expectedUserMentions , $agent ) {
$title = Title :: newFromText ( 'Test' );
$discussionParser = TestingAccessWrapper :: newFromClass ( 'EchoDiscussionParser' );
$this -> assertEquals ( $expectedUserMentions , $discussionParser -> getUserMentions ( $title , $agent , $userLinks ) );
}
public function testGetUserMentions_validMention () {
$userName = 'Admin' ;
$userId = User :: newFromName ( $userName ) -> getId ();
$expectedUserMentions = array (
'validMentions' => array ( $userId => $userId ),
'unknownUsers' => array (),
'anonymousUsers' => array (),
);
$userLinks = array ( $userName => $userId );
$this -> testGetUserMentions ( $userLinks , $expectedUserMentions , 1 );
}
public function testGetUserMentions_ownMention () {
$userName = 'Admin' ;
2016-08-12 01:41:08 +00:00
$userId = User :: newFromName ( 'Admin' ) -> getId ();
2016-07-05 14:45:14 +00:00
$expectedUserMentions = array (
2016-08-12 01:41:08 +00:00
'validMentions' => array (),
2016-07-05 14:45:14 +00:00
'unknownUsers' => array (),
'anonymousUsers' => array (),
);
$userLinks = array ( $userName => $userId );
$this -> testGetUserMentions ( $userLinks , $expectedUserMentions , $userId );
}
public function testGetUserMentions_tooManyMentions () {
$userLinks = array (
'NotKnown1' => 0 ,
'NotKnown2' => 0 ,
'NotKnown3' => 0 ,
'127.0.0.1' => 0 ,
'127.0.0.2' => 0 ,
);
$this -> setMwGlobals ( array (
// lower limit for the mention-too-many notification
'wgEchoMaxMentionsCount' => 3
) );
$title = Title :: newFromText ( 'Test' );
$discussionParser = TestingAccessWrapper :: newFromClass ( 'EchoDiscussionParser' );
$this -> assertEquals ( 4 , $discussionParser -> getOverallUserMentionsCount ( $discussionParser -> getUserMentions ( $title , 1 , $userLinks ) ) );
}
2015-02-17 21:23:26 +00:00
protected function isParserFunctionsInstalled () {
if ( class_exists ( 'ExtParserFunctions' ) ) {
return true ;
} else {
return " ParserFunctions not enabled " ;
}
}
2016-09-01 20:48:56 +00:00
public function testGetTextSnippet () {
$this -> assertEquals (
'Page001' ,
EchoDiscussionParser :: getTextSnippet (
'[[:{{BASEPAGENAME}}]]' ,
Language :: factory ( 'en' ),
150 ,
Title :: newFromText ( 'Page001' )
)
);
}
2012-08-30 16:04:39 +00:00
}