2012-07-31 00:29:49 +00:00
< ? php
2024-02-22 15:31:00 +00:00
namespace MediaWiki\Extension\Notifications\Test ;
2018-08-25 10:51:14 +00:00
// phpcs:disable Generic.Files.LineLength -- Long html test examples
2018-08-22 14:13:10 +00:00
2023-12-11 15:33:08 +00:00
use MediaWiki\CommentStore\CommentStoreComment ;
2024-02-22 15:31:00 +00:00
use MediaWiki\Content\WikitextContent ;
2022-11-12 07:19:00 +00:00
use MediaWiki\Extension\Notifications\DiscussionParser ;
2022-11-02 21:34:17 +00:00
use MediaWiki\Extension\Notifications\Model\Event ;
2023-08-24 21:31:13 +00:00
use MediaWiki\MainConfigNames ;
2024-02-22 15:31:00 +00:00
use MediaWiki\Registration\ExtensionRegistry ;
2021-02-02 17:34:47 +00:00
use MediaWiki\Revision\MutableRevisionRecord ;
use MediaWiki\Revision\SlotRecord ;
2023-12-11 15:33:08 +00:00
use MediaWiki\Title\Title ;
use MediaWiki\User\User ;
2024-02-22 15:31:00 +00:00
use MediaWikiIntegrationTestCase ;
use ReflectionObject ;
2017-04-19 21:51:08 +00:00
use Wikimedia\TestingAccessWrapper ;
2016-05-04 08:45:49 +00:00
2014-07-25 20:19:15 +00:00
/**
2022-11-12 07:19:00 +00:00
* @ covers \MediaWiki\Extension\Notifications\DiscussionParser
2014-07-25 20:19:15 +00:00
* @ group Echo
2015-01-31 01:00:06 +00:00
* @ group Database
2022-11-12 06:37:37 +00:00
*
* TODO test cases for :
* - stripHeader
* - stripSignature
2014-07-25 20:19:15 +00:00
*/
2022-11-12 07:19:00 +00:00
class DiscussionParserTest extends MediaWikiIntegrationTestCase {
2022-11-02 20:34:04 +00:00
/**
* @ var string
*/
private const EXEMPLAR_TIMESTAMP = '20:47, 2 November 2022 (UTC)' ;
2015-01-31 01:00:06 +00:00
/**
2017-01-23 16:19:17 +00:00
* Convenience users for use in these tests .
* Can be setup one by one using the setupTestUser () method
* Or all at once using the setupAllTestUsers () method
2015-02-05 19:45:40 +00:00
*
2018-08-13 07:25:22 +00:00
* @ var array [] [ username => [ user preference => preference value ]]
2015-01-31 01:00:06 +00:00
*/
2016-12-05 18:51:07 +00:00
protected $testUsers = [
'Werdna' => [
2015-01-31 01:00:06 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'Werdna2' => [
2015-01-31 01:00:06 +00:00
'nickname' => '[[User:Werdna2|Andrew]]' ,
'fancysig' => '1' ,
2016-12-05 18:51:07 +00:00
],
'Werdna3' => [
2015-01-31 01:00:06 +00:00
'nickname' => '[[User talk:Werdna3|Andrew]]' ,
'fancysig' => '1' ,
2016-12-05 18:51:07 +00:00
],
'Werdna4' => [
2015-01-31 01:00:06 +00:00
'nickname' => '[[User:Werdna4|wer]dna]]' ,
'fancysig' => '1' ,
2016-12-05 18:51:07 +00:00
],
'We buried our secrets in the garden' => [
2015-01-31 01:00:06 +00:00
'nickname' => '[[User talk:We buried our secrets in the garden#top|wbositg]]' ,
'fancysig' => '1' ,
2016-12-05 18:51:07 +00:00
],
'I Heart Spaces' => [
2015-01-31 01:00:06 +00:00
'nickname' => '[[User:I_Heart_Spaces]] ([[User_talk:I_Heart_Spaces]])' ,
'fancysig' => '1' ,
2016-12-05 18:51:07 +00:00
],
'Jam' => [
2015-01-31 01:00:06 +00:00
'nickname' => '[[User:Jam]]' ,
'fancysig' => '1' ,
2016-12-05 18:51:07 +00:00
],
'Reverta-me' => [
2015-01-31 01:00:06 +00:00
'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' ,
2016-12-05 18:51:07 +00:00
],
'Jorm' => [
2015-01-31 01:00:06 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'Jdforrester' => [
2015-01-31 01:00:06 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'DarTar' => [
2015-01-31 01:00:06 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'Bsitu' => [
2015-01-31 01:00:06 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'JarJar' => [
2015-01-31 01:00:06 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'Schnark' => [
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' ,
2016-12-05 18:51:07 +00:00
],
'Cwobeel' => [
2015-01-31 01:00:06 +00:00
'nickname' => '[[User:Cwobeel|<span style="color:#339966">Cwobeel</span>]] [[User_talk:Cwobeel|<span style="font-size:80%">(talk)</span>]]' ,
'fancysig' => '1' ,
2016-12-05 18:51:07 +00:00
],
'Bob K31416' => [
2015-02-05 19:45:40 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'X" onclick="alert(\'XSS\');" title="y' => [
2015-02-05 19:45:40 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'He7d3r' => [
2015-02-05 19:45:40 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'PauloEduardo' => [
2015-02-05 19:45:40 +00:00
'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' ,
2016-12-05 18:51:07 +00:00
],
'PatHadley' => [
2015-02-17 10:10:49 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'Samwalton9' => [
2015-02-17 10:10:49 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'Kudpung' => [
2015-02-17 10:10:49 +00:00
'nickname' => '[[User:Kudpung|Kudpung กุดผึ้ง]] ([[User talk:Kudpung#top|talk]])' ,
'fancysig' => '1' ,
2016-12-05 18:51:07 +00:00
],
'Jim Carter' => [
2015-02-17 10:10:49 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'Buster7' => [
2015-02-17 10:10:49 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
'Admin' => [
2015-07-07 00:31:08 +00:00
'nickname' => '[[:User:Admin|Admin]]' ,
'fancysig' => '1' ,
2016-12-05 18:51:07 +00:00
],
'Test11' => [
2015-07-07 00:31:08 +00:00
'nickname' => '' ,
'fancysig' => '0' ,
2016-12-05 18:51:07 +00:00
],
];
2015-01-31 01:00:06 +00:00
2017-01-23 16:19:17 +00:00
private function setupAllTestUsers () {
2023-08-24 21:31:13 +00:00
foreach ( $this -> testUsers as $username => $_ ) {
2017-01-23 16:19:17 +00:00
$this -> setupTestUser ( $username );
}
}
private function setupTestUser ( $username ) {
// Skip user creation requests that are not in the list (such as IPs)
if ( ! array_key_exists ( $username , $this -> testUsers ) ) {
return ;
}
$preferences = $this -> testUsers [ $username ];
$user = User :: createNew ( $username );
// Set preferences
if ( $user ) {
2021-03-30 22:14:16 +00:00
$userOptionsManager = $this -> getServiceContainer () -> getUserOptionsManager ();
2017-01-23 16:19:17 +00:00
foreach ( $preferences as $option => $value ) {
2021-03-30 22:14:16 +00:00
$userOptionsManager -> setOption ( $user , $option , $value );
2017-01-23 16:19:17 +00:00
}
2023-08-24 21:31:13 +00:00
$userOptionsManager -> saveOptions ( $user );
2017-01-23 16:19:17 +00:00
}
}
2023-05-20 18:35:53 +00:00
public static function provideHeaderExtractions () {
2016-12-05 18:51:07 +00:00
return [
[ '' , false ],
[ '== Grand jury no bill reception ==' , 'Grand jury no bill reception' ],
[ '=== Echo-Test ===' , 'Echo-Test' ],
[ '==== Notificações ====' , 'Notificações' ],
[ '=====Me?=====' , 'Me?' ],
];
2016-08-03 12:41:04 +00:00
}
/**
* @ dataProvider provideHeaderExtractions
*/
public function testExtractHeader ( $text , $expected ) {
2022-11-12 07:19:00 +00:00
$this -> assertEquals ( $expected , DiscussionParser :: extractHeader ( $text ) );
2016-08-03 12:41:04 +00:00
}
2023-05-20 18:35:53 +00:00
public static function generateEventsForRevisionDataProvider () {
2016-12-05 18:51:07 +00:00
return [
[
2015-02-05 19:45:40 +00:00
'new' => 637638133 ,
'old' => 637637213 ,
'username' => 'Cwobeel' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [
2015-02-05 19:45:40 +00:00
// pages expected to exist (e.g. templates to be expanded)
'Template:u' => '[[User:{{{1}}}|{{<includeonly>safesubst:</includeonly>#if:{{{2|}}}|{{{2}}}|{{{1}}}}}]]<noinclude>{{documentation}}</noinclude>' ,
2016-12-05 18:51:07 +00:00
],
2022-11-12 06:37:37 +00:00
// can't remember, not important here
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
2015-02-05 19:45:40 +00:00
// events expected to be fired going from old revision to new
2016-12-05 18:51:07 +00:00
[
2015-02-05 19:45:40 +00:00
'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
/*
2022-11-02 21:34:17 +00:00
* I wish I could also compare Event :: $extra data to
2015-02-05 19:45:40 +00:00
* 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 .
*/
2016-12-05 18:51:07 +00:00
],
],
],
[
2015-02-05 19:45:40 +00:00
'new' => 138275105 ,
'old' => 138274875 ,
'username' => 'Schnark' ,
'lang' => 'de' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2022-11-12 06:37:37 +00:00
// can't remember, not important here
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2015-02-05 19:45:40 +00:00
'type' => 'mention' ,
'agent' => 'Schnark' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Echo-Test' ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2015-02-05 19:45:40 +00:00
'new' => 40610292 ,
'old' => 40608353 ,
'username' => 'PauloEduardo' ,
'lang' => 'pt' ,
2016-12-05 18:51:07 +00:00
'pages' => [
2015-02-05 19:45:40 +00:00
'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>' ,
2016-12-05 18:51:07 +00:00
],
2022-11-12 06:37:37 +00:00
// can't remember, not important here
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2015-02-05 19:45:40 +00:00
'type' => 'mention' ,
'agent' => 'PauloEduardo' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Notificações' ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2015-02-17 10:10:49 +00:00
'new' => 646792804 ,
'old' => 646790570 ,
'username' => 'PatHadley' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [
2015-02-17 10:10:49 +00:00
'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]])}}' ,
2016-12-05 18:51:07 +00:00
],
2015-02-17 10:10:49 +00:00
'title' => 'User_talk:PatHadley' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2015-02-17 10:10:49 +00:00
'type' => 'mention' ,
'agent' => 'PatHadley' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Wizardry required' ,
2016-12-05 18:51:07 +00:00
],
[
2015-02-17 10:10:49 +00:00
'type' => 'edit-user-talk' ,
'agent' => 'PatHadley' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Wizardry required' ,
2016-12-05 18:51:07 +00:00
],
],
2015-02-17 21:23:26 +00:00
'precondition' => 'isParserFunctionsInstalled' ,
2016-12-05 18:51:07 +00:00
],
[
2015-02-17 10:10:49 +00:00
'new' => 647260329 ,
'old' => 647258025 ,
'username' => 'Kudpung' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [
2015-02-17 10:10:49 +00:00
'Template:U' => '[[User:{{{1}}}|{{<includeonly>safesubst:</includeonly>#if:{{{2|}}}|{{{2}}}|{{{1}}}}}]]<noinclude>{{documentation}}</noinclude>' ,
2016-12-05 18:51:07 +00:00
],
2015-02-17 10:10:49 +00:00
'title' => 'User_talk:Kudpung' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2015-02-17 10:10:49 +00:00
'type' => 'mention' ,
'agent' => 'Kudpung' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Me?' ,
2016-12-05 18:51:07 +00:00
],
[
2015-02-17 10:10:49 +00:00
'type' => 'edit-user-talk' ,
'agent' => 'Kudpung' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Me?' ,
2016-12-05 18:51:07 +00:00
],
],
],
2015-07-07 00:31:08 +00:00
// T68512, leading colon in user page link in signature
2016-12-05 18:51:07 +00:00
[
2015-07-07 00:31:08 +00:00
'new' => 612485855 ,
'old' => 612485595 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2015-07-07 00:31:08 +00:00
'title' => 'User_talk:Admin' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2015-07-07 00:31:08 +00:00
'type' => 'mention' ,
'agent' => 'Admin' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Hi' ,
2016-12-05 18:51:07 +00:00
],
[
2015-07-07 00:31:08 +00:00
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Hi' ,
2016-12-05 18:51:07 +00:00
],
],
2015-07-07 00:31:08 +00:00
'precondition' => 'isParserFunctionsInstalled' ,
2016-12-05 18:51:07 +00:00
],
2017-02-07 18:34:51 +00:00
// T154406 unintended mentions when changing content
[
'new' => 987667999 ,
'old' => 987667998 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => [],
'title' => 'MultipleSignatureMentions' ,
'expected' => [],
],
2018-01-19 21:26:49 +00:00
[
'new' => 1234 ,
'old' => 123 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => [],
'title' => 'Pings in summary' ,
'expected' => [
[
'type' => 'mention-summary' ,
'agent' => 'Admin' ,
'section-title' => null ,
]
],
'precondition' => '' ,
'summary' => 'Hey [[User:Werdna|Werdna]] and [[User:Jorm]], [[User:Admin]] here' ,
],
2016-12-05 18:51:07 +00:00
];
2016-07-21 13:00:54 +00:00
}
/**
2023-05-20 18:35:53 +00:00
* @ dataProvider generateEventsForRevisionDataProvider
2016-07-21 13:00:54 +00:00
*/
public function testGenerateEventsForRevision (
2018-01-19 21:26:49 +00:00
$newId , $oldId , $username , $lang , $pages , $title , $expected , $precondition = '' ,
$summary = ''
2016-07-21 13:00:54 +00:00
) {
if ( $precondition !== '' ) {
$result = $this -> $precondition ();
if ( $result !== true ) {
$this -> markTestSkipped ( $result );
return ;
}
}
2017-01-23 16:19:17 +00:00
$this -> setupAllTestUsers ();
2016-07-21 13:00:54 +00:00
$revision = $this -> setupTestRevisionsForEventGeneration (
2018-01-19 21:26:49 +00:00
$newId , $oldId , $username , $lang , $pages , $title , $summary
2016-07-21 13:00:54 +00:00
);
2016-12-05 18:51:07 +00:00
$events = [];
2016-07-21 13:00:54 +00:00
$this -> setupEventCallbackForEventGeneration (
2022-11-02 21:34:17 +00:00
static function ( Event $event ) use ( & $events ) {
2016-12-05 18:51:07 +00:00
$events [] = [
2016-07-21 13:00:54 +00:00
'type' => $event -> getType (),
'agent' => $event -> getAgent () -> getName (),
'section-title' => $event -> getExtraParam ( 'section-title' ),
2016-12-05 18:51:07 +00:00
];
2016-07-21 13:00:54 +00:00
return false ;
}
);
2022-11-02 20:34:04 +00:00
$this -> overrideConfigValues ( [
2018-01-19 21:26:49 +00:00
// disable mention failure and success notifications
2022-11-02 20:34:04 +00:00
'EchoMentionStatusNotifications' => false ,
2018-01-19 21:26:49 +00:00
// enable pings from summary
2022-11-02 20:34:04 +00:00
'EchoMaxMentionsInEditSummary' => 5 ,
2018-01-19 21:26:49 +00:00
] );
2021-07-26 11:56:44 +00:00
$this -> clearHook ( 'EchoGetEventsForRevision' );
2016-07-21 13:00:54 +00:00
2022-11-12 07:19:00 +00:00
DiscussionParser :: generateEventsForRevision ( $revision , false );
2016-07-21 13:00:54 +00:00
$this -> assertEquals ( $expected , $events );
}
2023-05-20 18:35:53 +00:00
public static function provider_generateEventsForRevision_mentionStatus () {
2016-12-05 18:51:07 +00:00
return [
[
2016-06-21 14:42:56 +00:00
'new' => 747747748 ,
'old' => 747747747 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-06-21 14:42:56 +00:00
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-06-21 14:42:56 +00:00
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Hello Users' ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'Ping' ,
2016-12-05 18:51:07 +00:00
],
[
2016-06-21 14:42:56 +00:00
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
2016-07-05 14:45:14 +00:00
'section-title' => 'Hello Users' ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'Po?ng' ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-07-21 13:00:54 +00:00
'new' => 747747750 ,
'old' => 747747747 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-07-21 13:00:54 +00:00
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-07-21 13:00:54 +00:00
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Hello Users' ,
2016-08-09 16:20:50 +00:00
'subject-name' => null ,
2016-12-05 18:51:07 +00:00
],
[
2016-07-21 13:00:54 +00:00
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Hello Users' ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'Test11' ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-02 15:14:48 +00:00
'new' => 747798766 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-08-02 15:14:48 +00:00
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-08-02 15:14:48 +00:00
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'NoUser' ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-02 15:14:48 +00:00
'new' => 747798767 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-08-02 15:14:48 +00:00
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-08-02 15:14:48 +00:00
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
2016-08-09 16:20:50 +00:00
'subject-name' => 'NoUser' ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-02 15:14:48 +00:00
'new' => 747798768 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-08-02 15:14:48 +00:00
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [],
],
[
2016-08-09 16:20:50 +00:00
'new' => 747798770 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-08-09 16:20:50 +00:00
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-08-09 16:20:50 +00:00
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1.5' ,
'subject-name' => null ,
2016-12-05 18:51:07 +00:00
],
[
2016-08-09 16:20:50 +00:00
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1.5' ,
'subject-name' => 'Test11' ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-09 16:20:50 +00:00
'new' => 747798771 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-08-09 16:20:50 +00:00
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-08-09 16:20:50 +00:00
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1.5' ,
'subject-name' => 'NoUser1.5' ,
2016-12-05 18:51:07 +00:00
],
[
2016-08-09 16:20:50 +00:00
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => 'NoUser2' ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-09 16:20:50 +00:00
'new' => 747798772 ,
'old' => 747798765 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-08-09 16:20:50 +00:00
'title' => 'UTPage' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-08-09 16:20:50 +00:00
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => 'NoUser1' ,
2016-12-05 18:51:07 +00:00
],
[
2016-08-09 16:20:50 +00:00
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1.75' ,
'subject-name' => 'NoUser1.75' ,
2016-12-05 18:51:07 +00:00
],
[
2016-08-09 16:20:50 +00:00
'type' => 'mention-failure' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => 'NoUser2' ,
2016-12-05 18:51:07 +00:00
],
],
[
2016-09-05 12:53:30 +00:00
'new' => 987654322 ,
'old' => 987654321 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-09-05 12:53:30 +00:00
'title' => 'User_talk:Admin' ,
2016-12-05 18:51:07 +00:00
'expected' => [ [
2016-09-05 12:53:30 +00:00
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
'section-title' => false ,
'subject-name' => null ,
2016-12-05 18:51:07 +00:00
] ],
],
[
2016-09-05 12:53:30 +00:00
'new' => 987654323 ,
'old' => 987654321 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-09-05 12:53:30 +00:00
'title' => 'User_talk:Admin' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-09-05 12:53:30 +00:00
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => null ,
2016-12-05 18:51:07 +00:00
],
[
2016-09-05 12:53:30 +00:00
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => 'Test11' ,
2016-12-05 18:51:07 +00:00
],
[
2016-09-05 12:53:30 +00:00
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => null ,
2016-12-05 18:51:07 +00:00
],
],
],
],
[
2016-09-05 12:53:30 +00:00
'new' => 987654324 ,
'old' => 987654321 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-09-05 12:53:30 +00:00
'title' => 'User_talk:Admin' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-09-05 12:53:30 +00:00
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => null ,
2016-12-05 18:51:07 +00:00
],
[
2016-09-05 12:53:30 +00:00
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Section 1' ,
'subject-name' => 'Test11' ,
2016-12-05 18:51:07 +00:00
],
[
2016-09-05 12:53:30 +00:00
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
'section-title' => false ,
'subject-name' => null ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-09-05 12:53:30 +00:00
'new' => 987654325 ,
'old' => 987654321 ,
'username' => 'Admin' ,
'lang' => 'en' ,
2016-12-05 18:51:07 +00:00
'pages' => [],
2016-09-05 12:53:30 +00:00
'title' => 'User_talk:Admin' ,
2016-12-05 18:51:07 +00:00
'expected' => [
[
2016-09-05 12:53:30 +00:00
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => null ,
2016-12-05 18:51:07 +00:00
],
[
2016-09-05 12:53:30 +00:00
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => 'Test11' ,
2016-12-05 18:51:07 +00:00
],
[
2016-09-05 12:53:30 +00:00
'type' => 'edit-user-talk' ,
'agent' => 'Admin' ,
'section-title' => 'Section 2' ,
'subject-name' => null ,
2016-12-05 18:51:07 +00:00
],
],
],
2017-01-24 10:33:37 +00:00
[
'new' => 987654401 ,
'old' => 987654400 ,
'username' => 'Admin' ,
'lang' => 'en' ,
'pages' => [],
'title' => 'UTPage2' ,
'expected' => [
[
'type' => 'mention' ,
'agent' => 'Admin' ,
'section-title' => false ,
'subject-name' => null ,
],
[
'type' => 'mention-success' ,
'agent' => 'Admin' ,
'section-title' => false ,
'subject-name' => 'Test11' ,
],
],
],
2016-12-05 18:51:07 +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
) {
2017-01-23 16:19:17 +00:00
$this -> setupAllTestUsers ();
2016-07-21 13:00:54 +00:00
$revision = $this -> setupTestRevisionsForEventGeneration (
$newId , $oldId , $username , $lang , $pages , $title
);
2016-12-05 18:51:07 +00:00
$events = [];
2016-06-21 14:42:56 +00:00
$this -> setupEventCallbackForEventGeneration (
2022-11-02 21:34:17 +00:00
static function ( Event $event ) use ( & $events ) {
2016-12-05 18:51:07 +00:00
$events [] = [
2016-06-21 14:42:56 +00:00
'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
'subject-name' => $event -> getExtraParam ( 'subject-name' ),
2016-12-05 18:51:07 +00:00
];
2016-06-21 14:42:56 +00:00
return false ;
}
);
2023-08-24 21:31:13 +00:00
$this -> overrideConfigValues ( [
// enable mention failure and success notifications
'EchoMentionStatusNotifications' => true ,
// enable multiple sections mentions
'EchoMentionsOnMultipleSectionEdits' => true ,
] );
2021-07-26 11:56:44 +00:00
$this -> clearHook ( 'EchoGetEventsForRevision' );
2016-06-21 14:42:56 +00:00
2022-11-12 07:19:00 +00:00
DiscussionParser :: generateEventsForRevision ( $revision , false );
2016-06-21 14:42:56 +00:00
$this -> assertEquals ( $expected , $events );
}
2023-05-20 18:35:53 +00:00
public static function provider_extractSections () {
2016-12-05 18:51:07 +00:00
return [
[
2016-08-09 16:20:50 +00:00
'content' => 'Just Text.' ,
2016-12-05 18:51:07 +00:00
'result' => [
[
2016-08-09 16:20:50 +00:00
'header' => false ,
'content' => 'Just Text.' ,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
Text and a
== Headline ==
with text
TEXT
,
2016-12-05 18:51:07 +00:00
'result' => [
[
2016-08-09 16:20:50 +00:00
'header' => false ,
'content' =>
<<< TEXT
Text and a
TEXT
,
2016-12-05 18:51:07 +00:00
],
[
2016-08-09 16:20:50 +00:00
'header' => 'Headline' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
== Headline ==
with text
TEXT
,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
== Headline ==
Text and a [[ User : Test ]]
TEXT
,
2016-12-05 18:51:07 +00:00
'result' => [
[
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
,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
Content 0
== Headline 1 ==
Content 1
=== Headline 2 ===
Content 2
TEXT
,
2016-12-05 18:51:07 +00:00
'result' => [
[
2016-08-09 16:20:50 +00:00
'header' => false ,
'content' => 'Content 0' ,
2016-12-05 18:51:07 +00:00
],
[
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
,
2016-12-05 18:51:07 +00:00
],
[
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
,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
== Headline 1 ==
2017-08-11 17:52:18 +00:00
مرحبا كيف حالك
2016-08-09 16:20:50 +00:00
=== Headline 2 ===
انا بخير شكرا
TEXT
,
2016-12-05 18:51:07 +00:00
'result' => [
[
2016-08-09 16:20:50 +00:00
'header' => 'Headline 1' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
== Headline 1 ==
مرحبا كيف حالك
TEXT
,
2016-12-05 18:51:07 +00:00
],
[
2016-08-09 16:20:50 +00:00
'header' => 'Headline 2' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
=== Headline 2 ===
انا بخير شكرا
TEXT
,
2016-12-05 18:51:07 +00:00
],
],
],
[
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
مرحبا كيف حالك
=== Headline 1 ===
انا بخير شكرا
TEXT
,
2016-12-05 18:51:07 +00:00
'result' => [
[
2016-08-09 16:20:50 +00:00
'header' => false ,
'content' =>
<<< TEXT
مرحبا كيف حالك
TEXT
,
2016-12-05 18:51:07 +00:00
],
[
2016-08-09 16:20:50 +00:00
'header' => 'Headline 1' ,
2016-08-09 16:20:50 +00:00
'content' =>
<<< TEXT
=== Headline 1 ===
انا بخير شكرا
TEXT
,
2016-12-05 18:51:07 +00:00
],
],
],
];
2016-08-09 16:20:50 +00:00
}
/**
* @ dataProvider provider_extractSections
*/
public function testExtractSections ( $content , $result ) {
2022-11-12 07:19:00 +00:00
$discussionParser = TestingAccessWrapper :: newFromClass ( DiscussionParser :: class );
2016-08-09 16:20:50 +00:00
$sections = $discussionParser -> extractSections ( $content );
$this -> assertEquals ( $result , $sections );
}
2016-06-21 14:42:56 +00:00
public function testGenerateEventsForRevision_tooManyMentionsFailure () {
2016-12-05 18:51:07 +00:00
$expected = [
[
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 ,
2016-12-05 18:51:07 +00:00
],
];
2016-06-21 14:42:56 +00:00
2017-01-23 16:19:17 +00:00
$this -> setupTestUser ( 'Admin' );
2016-12-05 18:51:07 +00:00
$revision = $this -> setupTestRevisionsForEventGeneration ( 747747749 , 747747747 , 'Admin' , 'en' , [], 'UTPage' );
2016-06-21 14:42:56 +00:00
2016-12-05 18:51:07 +00:00
$events = [];
2016-06-21 14:42:56 +00:00
$this -> setupEventCallbackForEventGeneration (
2022-11-02 21:34:17 +00:00
static function ( Event $event ) use ( & $events ) {
2016-12-05 18:51:07 +00:00
$events [] = [
2016-06-21 14:42:56 +00:00
'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' ),
2016-12-05 18:51:07 +00:00
];
2016-06-21 14:42:56 +00:00
return false ;
}
);
2022-11-02 20:34:04 +00:00
$this -> overrideConfigValues ( [
2016-06-21 14:42:56 +00:00
// enable mention failure and success notifications
2022-11-02 20:34:04 +00:00
'EchoMentionStatusNotifications' => true ,
2016-07-27 15:04:49 +00:00
// lower limit for the mention-failure-too-many notification
2022-11-02 20:34:04 +00:00
'EchoMaxMentionsCount' => 5
2016-12-05 18:51:07 +00:00
] );
2021-07-26 11:56:44 +00:00
$this -> clearHook ( 'EchoGetEventsForRevision' );
2016-06-21 14:42:56 +00:00
2022-11-12 07:19:00 +00:00
DiscussionParser :: generateEventsForRevision ( $revision , false );
2016-06-21 14:42:56 +00:00
$this -> assertEquals ( $expected , $events );
}
2018-01-19 21:26:49 +00:00
private function setupTestRevisionsForEventGeneration ( $newId , $oldId , $username , $lang , $pages ,
$title , $summary = ''
) {
2023-08-24 21:31:13 +00:00
$services = $this -> getServiceContainer ();
$store = $services -> getRevisionStore ();
2018-07-25 16:12:09 +00:00
// Content language is used by the code that interprets the namespace part of titles
// (Title::getTitleParser), so should be the fake language ;)
$this -> setContentLang ( $lang );
2022-11-02 20:34:04 +00:00
$this -> overrideConfigValues ( [
2024-03-12 15:24:58 +00:00
// this one allows MediaWiki:xyz pages to be set as messages
2023-08-24 21:31:13 +00:00
MainConfigNames :: UseDatabaseMessages => true
2016-12-05 18:51:07 +00:00
] );
2019-04-09 08:16:37 +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!
2016-12-05 18:51:07 +00:00
$pages += [ $title => '' ];
2021-06-24 04:54:45 +00:00
$user = $this -> getTestUser () -> getUser ();
2023-08-24 21:31:13 +00:00
$wikiPageFactory = $services -> getWikiPageFactory ();
2015-02-17 10:10:49 +00:00
foreach ( $pages as $pageTitle => $pageText ) {
2022-06-03 21:00:26 +00:00
$template = $wikiPageFactory -> newFromTitle ( Title :: newFromText ( $pageTitle ) );
2021-06-24 04:54:45 +00:00
$template -> doUserEditContent ( new WikitextContent ( $pageText ), $user , '' );
2015-02-05 19:45:40 +00:00
}
2020-03-14 13:04:32 +00:00
// force i18n messages to be reloaded from MessageCache (from DB, where a new message
2015-02-17 10:10:49 +00:00
// might have been created as page)
2023-08-24 21:31:13 +00:00
$services -> resetServiceForTesting ( 'MessageCache' );
2015-02-17 10:10:49 +00:00
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 );
2019-04-17 15:46:06 +00:00
// create stub MutableRevisionRecord object
2021-02-02 17:34:47 +00:00
$revision = new MutableRevisionRecord ( $title );
$content = new WikitextContent ( $newText );
$revision -> setId ( $newId );
$revision -> setUser ( User :: newFromName ( $username ) );
$revision -> setParentId ( $oldId );
$comment = CommentStoreComment :: newUnsavedComment (
$summary ,
null
);
$revision -> setComment ( $comment );
$revision -> setContent ( SlotRecord :: MAIN , $content );
2019-04-17 15:46:06 +00:00
$userName = $revision -> getUser () -> getName ();
2015-02-05 19:45:40 +00:00
// generate diff between 2 revisions
2022-11-12 07:19:00 +00:00
$changes = DiscussionParser :: getMachineReadableDiff ( $oldText , $newText );
$output = DiscussionParser :: interpretDiff ( $changes , $userName , $title );
2015-02-05 19:45:40 +00:00
// store diff in some local cache var, to circumvent
2022-11-12 07:19:00 +00:00
// DiscussionParser::getChangeInterpretationForRevision's attempt to
2015-02-05 19:45:40 +00:00
// retrieve parent revision from DB
2023-08-18 13:49:23 +00:00
$cacheKey = $revision -> getId () . '|' . $revision -> getPage () -> getNamespace () . '|' . $revision -> getPage () -> getDBkey ();
2023-12-13 19:43:18 +00:00
TestingAccessWrapper :: newFromClass ( DiscussionParser :: class ) -> revisionInterpretationCache = [ $cacheKey => $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 ) {
2022-11-02 20:34:04 +00:00
$this -> setTemporaryHook ( 'BeforeEchoEventInsert' , $callback );
2015-02-05 19:45:40 +00:00
}
2012-07-31 00:29:49 +00:00
public function testTimestampRegex () {
2022-11-02 20:34:04 +00:00
$exemplarTimestamp = self :: EXEMPLAR_TIMESTAMP ;
2022-11-12 07:19:00 +00:00
$timestampRegex = DiscussionParser :: getTimestampRegex ();
2012-07-31 00:29:49 +00:00
2012-08-31 21:50:46 +00:00
$match = preg_match ( '/' . $timestampRegex . '/u' , $exemplarTimestamp );
2019-10-23 10:28:30 +00:00
$this -> assertSame ( 1 , $match );
2012-07-31 00:29:49 +00:00
}
2020-10-27 18:59:26 +00:00
public function testTimestampRegex_T264922 () {
2023-08-24 21:31:13 +00:00
$this -> overrideConfigValue ( MainConfigNames :: LanguageCode , 'skr' );
2022-11-12 07:19:00 +00:00
$this -> assertIsString ( DiscussionParser :: getTimestampRegex (), 'does not fail' );
2020-10-27 18:59:26 +00:00
}
2014-01-19 13:41:22 +00:00
public function testGetTimestampPosition () {
2022-11-02 20:34:04 +00:00
$line = 'Hello World. ' . self :: EXEMPLAR_TIMESTAMP ;
2022-11-12 07:19:00 +00:00
$pos = DiscussionParser :: getTimestampPosition ( $line );
2019-10-23 10:28:30 +00:00
$this -> assertSame ( 13 , $pos );
2014-01-19 13:41:22 +00:00
}
2012-07-31 00:29:49 +00:00
/**
2023-05-20 18:35:53 +00:00
* @ dataProvider signingDetectionDataProvider
2012-07-31 00:29:49 +00:00
* FIXME some of the app logic is in the test ...
*/
public function testSigningDetection ( $line , $expectedUser ) {
2017-01-23 16:19:17 +00:00
if ( is_array ( $expectedUser ) ) {
$this -> setupTestUser ( $expectedUser [ 1 ] );
}
2022-11-12 07:19:00 +00:00
if ( ! DiscussionParser :: isSignedComment ( $line ) ) {
2020-08-10 10:54:10 +00:00
$this -> assertFalse ( $expectedUser );
2012-07-31 00:29:49 +00:00
return ;
}
2022-11-12 07:19:00 +00:00
$output = DiscussionParser :: getUserFromLine ( $line );
2012-07-31 00:29:49 +00:00
if ( $output === false ) {
2019-10-23 10:28:30 +00:00
$this -> assertFalse ( $expectedUser );
2012-07-31 00:29:49 +00:00
} elseif ( is_array ( $expectedUser ) ) {
// Sometimes testing for correct user detection,
2017-06-20 02:41:30 +00:00
// sometimes testing for offset detection
2012-07-31 00:29:49 +00:00
$this -> assertEquals ( $expectedUser , $output );
} else {
$this -> assertEquals ( $expectedUser , $output [ 1 ] );
}
}
2023-05-20 18:35:53 +00:00
public static function signingDetectionDataProvider () {
2022-11-02 20:34:04 +00:00
$ts = self :: EXEMPLAR_TIMESTAMP ;
2015-10-01 13:48:52 +00:00
2016-12-05 18:51:07 +00:00
return [
2012-07-31 00:29:49 +00:00
// Basic
2016-12-05 18:51:07 +00:00
[
2015-01-31 01:00:06 +00:00
" I like this. [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts " ,
2016-12-05 18:51:07 +00:00
[
2014-09-29 19:38:01 +00:00
13 ,
'Werdna'
2016-12-05 18:51:07 +00:00
],
],
2012-07-31 00:29:49 +00:00
// Confounding
2016-12-05 18:51:07 +00:00
[
2015-01-31 01:00:06 +00:00
" [[User:Jorm]] is a meanie. --[[User:Werdna2|Andrew]] $ts " ,
2016-12-05 18:51:07 +00:00
[
2014-09-29 19:38:01 +00:00
29 ,
2015-01-31 01:00:06 +00:00
" Werdna2 "
2016-12-05 18:51:07 +00:00
],
],
2012-07-31 00:29:49 +00:00
// Talk page link only
2016-12-05 18:51:07 +00:00
[
2015-01-31 01:00:06 +00:00
" [[User:Swalling|Steve]] is the best person I have ever met. --[[User talk:Werdna3|Andrew]] $ts " ,
2016-12-05 18:51:07 +00:00
[
2014-09-29 19:38:01 +00:00
62 ,
2015-01-31 01:00:06 +00:00
'Werdna3'
2016-12-05 18:51:07 +00:00
],
],
2012-07-31 00:29:49 +00:00
// Anonymous user
2016-12-05 18:51:07 +00:00
[
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 " ,
2016-12-05 18:51:07 +00:00
[
2014-09-29 19:38:01 +00:00
47 ,
'127.0.0.1'
2016-12-05 18:51:07 +00:00
],
],
2012-07-31 00:29:49 +00:00
// No signature
2016-12-05 18:51:07 +00:00
[
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
2016-12-05 18:51:07 +00:00
],
2012-07-31 00:29:49 +00:00
// Hash symbols in usernames
2016-12-05 18:51:07 +00:00
[
2014-09-29 19:38:01 +00:00
" What do you think? [[User talk:We buried our secrets in the garden#top|wbositg]] $ts " ,
2016-12-05 18:51:07 +00:00
[
2014-09-29 19:38:01 +00:00
19 ,
'We buried our secrets in the garden'
2016-12-05 18:51:07 +00:00
],
],
2014-09-29 19:38:01 +00:00
// Title that gets normalized different than it is provided in the wikitext
2016-12-05 18:51:07 +00:00
[
2014-09-29 19:38:01 +00:00
" Beep boop [[User:I_Heart_Spaces]] ([[User_talk:I_Heart_Spaces]]) $ts " ,
2016-12-05 18:51:07 +00:00
[
2014-11-14 23:23:52 +00:00
strlen ( " Beep boop " ),
2014-09-29 19:38:01 +00:00
'I Heart Spaces'
2016-12-05 18:51:07 +00:00
],
],
2014-09-29 19:38:01 +00:00
// Accepts ] in the pipe
2016-12-05 18:51:07 +00:00
[
2015-01-31 01:00:06 +00:00
" Shake n Bake --[[User:Werdna4|wer]dna]] $ts " ,
2016-12-05 18:51:07 +00:00
[
2014-11-14 23:23:52 +00:00
strlen ( " Shake n Bake -- " ),
2015-01-31 01:00:06 +00:00
'Werdna4' ,
2016-12-05 18:51:07 +00:00
],
],
2014-11-14 23:23:52 +00:00
2016-12-05 18:51:07 +00:00
[
2014-11-14 23:23:52 +00:00
" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? [[User:Jam]] $ts " ,
2016-12-05 18:51:07 +00:00
[
2014-11-14 23:23:52 +00:00
strlen ( " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? " ),
" Jam "
2016-12-05 18:51:07 +00:00
],
],
2014-11-14 23:23:52 +00:00
// extra long signature
2016-12-05 18:51:07 +00:00
[
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>'' " ,
2016-12-05 18:51:07 +00:00
[
2014-11-14 23:23:52 +00:00
strlen ( " { { U|He7d3r}}, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? " ),
'Reverta-me' ,
2016-12-05 18:51:07 +00:00
],
],
2019-03-27 21:38:01 +00:00
// Bug: T87852
[
" Test --[[Benutzer:Schnark]] ([[Benutzer:Schnark/js|js]]) " ,
[
strlen ( " Test -- " ),
'Schnark' ,
],
],
2015-01-31 01:00:06 +00:00
// when adding additional tests, make sure to add the non-anon users
2022-11-12 07:19:00 +00:00
// to DiscussionParserTest::$testUsers - the DiscussionParser
2015-01-31 01:00:06 +00:00
// needs the users to exist, because it'll generate a comparison
// signature, which is different when the user is considered anon
2016-12-05 18:51:07 +00:00
];
2012-07-31 00:29:49 +00:00
}
2023-05-20 18:35:53 +00:00
/** @dataProvider diffDataProvider */
2012-07-31 00:29:49 +00:00
public function testDiff ( $oldText , $newText , $expected ) {
2022-11-12 07:19:00 +00:00
$actual = DiscussionParser :: getMachineReadableDiff ( $oldText , $newText );
2012-07-31 00:29:49 +00:00
unset ( $actual [ '_info' ] );
unset ( $expected [ '_info' ] );
$this -> assertEquals ( $expected , $actual );
}
2023-05-20 18:35:53 +00:00
public static function diffDataProvider () {
2016-12-05 18:51:07 +00:00
return [
[
2012-07-31 00:29:49 +00:00
<<< 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
,
2016-12-05 18:51:07 +00:00
[ [
2012-07-31 00:29:49 +00:00
'action' => 'subtract' ,
'content' => 'line 2' ,
'left-pos' => 2 ,
'right-pos' => 2 ,
2016-12-05 18:51:07 +00:00
] ]
],
[
2012-07-31 00:29:49 +00:00
<<< 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
,
2016-12-05 18:51:07 +00:00
[ [
2012-07-31 00:29:49 +00:00
'action' => 'add' ,
'content' => 'line 2.5' ,
'left-pos' => 3 ,
'right-pos' => 3 ,
2016-12-05 18:51:07 +00:00
] ]
],
[
2012-07-31 00:29:49 +00:00
<<< 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
,
2016-12-05 18:51:07 +00:00
[ [
2012-07-31 00:29:49 +00:00
'action' => 'change' ,
'old_content' => 'line 2' ,
'new_content' => 'line b' ,
'left-pos' => 2 ,
'right-pos' => 2 ,
2016-12-05 18:51:07 +00:00
] ]
],
[
2012-07-31 00:29:49 +00:00
<<< 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
,
2016-12-05 18:51:07 +00:00
[
[
2012-07-31 00:29:49 +00:00
'action' => 'change' ,
'old_content' => 'line 2' ,
'new_content' => 'line b' ,
'left-pos' => 2 ,
'right-pos' => 2 ,
2016-12-05 18:51:07 +00:00
],
[
2012-07-31 00:29:49 +00:00
'action' => 'add' ,
'content' => ' line c
line d ' ,
'left-pos' => 3 ,
'right-pos' => 3 ,
2016-12-05 18:51:07 +00:00
],
],
],
];
2012-07-31 00:29:49 +00:00
}
2023-05-20 18:35:53 +00:00
/** @dataProvider annotationDataProvider */
2013-05-19 04:15:45 +00:00
public function testAnnotation ( $message , $diff , $user , $expectedAnnotation ) {
2017-01-23 16:19:17 +00:00
$this -> setupTestUser ( $user );
2022-11-12 07:19:00 +00:00
$actual = DiscussionParser :: interpretDiff ( $diff , $user );
2013-05-19 04:15:45 +00:00
$this -> assertEquals ( $expectedAnnotation , $actual , $message );
2012-07-31 00:29:49 +00:00
}
2023-05-20 18:35:53 +00:00
public static function annotationDataProvider () {
2022-11-02 20:34:04 +00:00
$ts = self :: EXEMPLAR_TIMESTAMP ;
2012-07-31 00:29:49 +00:00
2016-12-05 18:51:07 +00:00
return [
2013-05-19 04:15:45 +00:00
2016-12-05 18:51:07 +00:00
[
2013-05-19 04:15:45 +00:00
'Must detect added comments' ,
2012-07-31 00:29:49 +00:00
// Diff
2016-12-05 18:51:07 +00:00
[
2012-07-31 00:29:49 +00:00
// Action
2016-12-05 18:51:07 +00:00
[
2012-07-31 00:29:49 +00:00
'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 ,
2016-12-05 18:51:07 +00:00
],
'_info' => [
'lhs' => [
2012-07-31 00:29:49 +00:00
'== Section 1 ==' ,
2015-01-31 01:00:06 +00:00
" I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts " ,
2016-12-05 18:51:07 +00:00
],
'rhs' => [
2012-07-31 00:29:49 +00:00
'== 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 " ,
2016-12-05 18:51:07 +00:00
],
],
],
2012-07-31 00:29:49 +00:00
// User
'Werdna' ,
// Expected annotation
2016-12-05 18:51:07 +00:00
[
[
2012-07-31 00:29:49 +00:00
'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
2016-12-05 18:51:07 +00:00
],
],
],
2013-05-19 04:15:45 +00:00
2016-12-05 18:51:07 +00:00
[
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
2016-12-05 18:51:07 +00:00
[
2012-07-31 00:29:49 +00:00
// Action
2016-12-05 18:51:07 +00:00
[
2012-07-31 00:29:49 +00:00
'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 ,
2016-12-05 18:51:07 +00:00
],
'_info' => [
'lhs' => [
2012-07-31 00:29:49 +00:00
'== 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 " ,
2016-12-05 18:51:07 +00:00
],
'rhs' => [
2012-07-31 00:29:49 +00:00
'== 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 " ,
2016-12-05 18:51:07 +00:00
],
],
],
2012-07-31 00:29:49 +00:00
// User
'Werdna' ,
// Expected annotation
2016-12-05 18:51:07 +00:00
[
[
2012-07-31 00:29:49 +00:00
'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
2016-12-05 18:51:07 +00:00
],
],
],
2013-05-19 04:15:45 +00:00
2016-12-05 18:51:07 +00:00
[
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
2016-12-05 18:51:07 +00:00
[
2012-07-31 00:29:49 +00:00
// Action
2016-12-05 18:51:07 +00:00
[
2012-07-31 00:29:49 +00:00
'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 ,
2016-12-05 18:51:07 +00:00
],
'_info' => [
'lhs' => [
2012-07-31 00:29:49 +00:00
'== 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 " ,
2016-12-05 18:51:07 +00:00
],
'rhs' => [
2012-07-31 00:29:49 +00:00
'== 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 " ,
2016-12-05 18:51:07 +00:00
],
],
],
2012-07-31 00:29:49 +00:00
// User
'Jdforrester' ,
// Expected annotation
2016-12-05 18:51:07 +00:00
[
[
2012-07-31 00:29:49 +00:00
'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
,
2016-12-05 18:51:07 +00:00
],
],
],
2013-05-19 04:15:45 +00:00
2016-12-05 18:51:07 +00:00
[
2013-05-19 04:15:45 +00:00
'Must detect multiple added comments when multiple sections are edited' ,
2022-11-12 07:19:00 +00:00
DiscussionParser :: getMachineReadableDiff (
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
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
2016-12-05 18:51:07 +00:00
[
[
2013-05-19 04:15:45 +00:00
'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
2016-12-05 18:51:07 +00:00
],
[
2013-05-19 04:15:45 +00:00
'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
2016-12-05 18:51:07 +00:00
],
],
],
2013-05-19 04:15:45 +00:00
2016-12-05 18:51:07 +00:00
[
2015-01-31 01:00:06 +00:00
'Bug T78424' ,
2022-11-12 07:19:00 +00:00
DiscussionParser :: getMachineReadableDiff (
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 )
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
2016-12-05 18:51:07 +00:00
[
[
2015-01-31 01:00:06 +00:00
'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 ) ' ,
2016-12-05 18:51:07 +00:00
],
],
],
2015-01-31 01:00:06 +00:00
// when adding additional tests, make sure to add the non-anon users
2022-11-12 07:19:00 +00:00
// to DiscussionParserTest::$testusers - the DiscussionParser
2015-01-31 01:00:06 +00:00
// needs the users to exist, because it'll generate a comparison
// signature, which is different when the user is considered anon
2016-12-05 18:51:07 +00:00
];
2012-07-31 00:29:49 +00:00
}
2015-10-29 11:25:14 +00:00
public static function provider_detectSectionTitleAndText () {
2022-11-12 07:19:00 +00:00
// See DiscussionParserTest::$testUsers
2022-11-12 06:37:37 +00:00
$name = 'Werdna' ;
2013-05-17 19:16:46 +00:00
$comment = self :: signedMessage ( $name );
2016-12-05 18:51:07 +00:00
return [
[
2013-05-17 19:16:46 +00:00
'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
2016-12-05 18:51:07 +00:00
],
2013-05-17 19:16:46 +00:00
2016-12-05 18:51:07 +00:00
[
2013-05-17 19:16:46 +00:00
'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
2016-12-05 18:51:07 +00:00
],
2013-05-17 19:16:46 +00:00
2016-12-05 18:51:07 +00:00
[
2013-05-17 19:16:46 +00:00
'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
2016-12-05 18:51:07 +00:00
],
2013-05-17 19:16:46 +00:00
2016-12-05 18:51:07 +00:00
[
2013-05-17 19:16:46 +00:00
'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
2016-12-05 18:51:07 +00:00
],
2013-05-17 19:16:46 +00:00
2016-12-05 18:51:07 +00:00
[
2013-05-17 19:16:46 +00:00
'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
2016-12-05 18:51:07 +00:00
],
];
2013-05-17 19:16:46 +00:00
}
/**
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 ) {
2017-01-23 16:19:17 +00:00
$this -> setupTestUser ( $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 );
2022-11-12 07:19:00 +00:00
$diff = DiscussionParser :: getMachineReadableDiff ( $before , $after );
$interp = DiscussionParser :: 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 (
2016-12-05 18:51:07 +00:00
[ 'section-title' => $expect , 'section-text' => $expectText ],
2022-11-12 07:19:00 +00:00
DiscussionParser :: detectSectionTitleAndText ( $interp ),
2015-01-31 01:00:06 +00:00
$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 () {
2016-12-05 18:51:07 +00:00
$tests = [
[
2013-05-17 19:16:46 +00:00
'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
2016-12-05 18:51:07 +00:00
[
2013-05-17 19:16:46 +00:00
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 " ,
2016-12-05 18:51:07 +00:00
],
],
];
2013-05-17 19:16:46 +00:00
// Allow for setting an array of line numbers to expand from rather than
// just a single line number
2016-12-05 18:51:07 +00:00
$retval = [];
2013-05-17 19:16:46 +00:00
foreach ( $tests as $test ) {
foreach ( $test [ 2 ] as $lineNum => $expected ) {
2016-12-05 18:51:07 +00:00
$retval [] = [
2013-05-17 19:16:46 +00:00
$test [ 0 ],
$expected ,
$test [ 1 ],
$lineNum ,
2016-12-05 18:51:07 +00:00
];
2013-05-17 19:16:46 +00:00
}
}
return $retval ;
}
/**
* @ dataProvider provider_getFullSection
*/
public function testGetFullSection ( $message , $expect , $lines , $startLineNum ) {
2022-11-12 07:19:00 +00:00
$section = DiscussionParser :: getFullSection ( explode ( " \n " , $lines ), $startLineNum );
2013-05-17 19:16:46 +00:00
$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 " ;
2022-11-12 07:19:00 +00:00
$this -> assertSame ( 1 , DiscussionParser :: getSectionCount ( $one ) );
$this -> assertSame ( 2 , DiscussionParser :: getSectionCount ( $one . $two ) );
$this -> assertSame ( 2 , DiscussionParser :: getSectionCount ( $one . $three ) );
$this -> assertSame ( 3 , DiscussionParser :: getSectionCount ( $one . $two . $three ) );
$this -> assertSame ( 30 , DiscussionParser :: getSectionCount (
2016-08-03 13:02:59 +00:00
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 () {
2016-12-05 18:51:07 +00:00
$userMentions = [
'validMentions' => [ 1 => 1 ],
'unknownUsers' => [ 'NotKnown1' , 'NotKnown2' ],
'anonymousUsers' => [ '127.0.0.1' ],
];
2016-07-05 14:45:14 +00:00
2022-11-12 07:19:00 +00:00
$discussionParser = TestingAccessWrapper :: newFromClass ( DiscussionParser :: class );
2019-10-23 10:28:30 +00:00
$this -> assertSame ( 4 , $discussionParser -> getOverallUserMentionsCount ( $userMentions ) );
2016-07-05 14:45:14 +00:00
}
2023-05-20 18:35:53 +00:00
public static function provider_getUserMentions () {
2016-12-05 18:51:07 +00:00
return [
[
[ 'NotKnown1' => 0 ],
[
'validMentions' => [],
'unknownUsers' => [ 'NotKnown1' ],
'anonymousUsers' => [],
],
2016-07-05 14:45:14 +00:00
1
2016-12-05 18:51:07 +00:00
],
[
[ '127.0.0.1' => 0 ],
[
'validMentions' => [],
'unknownUsers' => [],
'anonymousUsers' => [ '127.0.0.1' ],
],
2016-07-05 14:45:14 +00:00
1
2016-12-05 18:51:07 +00:00
],
];
2016-07-05 14:45:14 +00:00
}
/**
* @ dataProvider provider_getUserMentions
*/
public function testGetUserMentions ( $userLinks , $expectedUserMentions , $agent ) {
$title = Title :: newFromText ( 'Test' );
2022-11-12 07:19:00 +00:00
$discussionParser = TestingAccessWrapper :: newFromClass ( DiscussionParser :: class );
2016-07-05 14:45:14 +00:00
$this -> assertEquals ( $expectedUserMentions , $discussionParser -> getUserMentions ( $title , $agent , $userLinks ) );
}
public function testGetUserMentions_validMention () {
$userName = 'Admin' ;
2017-01-23 16:19:17 +00:00
$this -> setupTestUser ( $userName );
2016-07-05 14:45:14 +00:00
$userId = User :: newFromName ( $userName ) -> getId ();
2023-08-07 14:27:28 +00:00
$otherUserId = $userId + 1 ;
2016-12-05 18:51:07 +00:00
$expectedUserMentions = [
'validMentions' => [ $userId => $userId ],
'unknownUsers' => [],
'anonymousUsers' => [],
];
$userLinks = [ $userName => $userId ];
2023-08-07 14:27:28 +00:00
$this -> testGetUserMentions ( $userLinks , $expectedUserMentions , $otherUserId );
2016-07-05 14:45:14 +00:00
}
public function testGetUserMentions_ownMention () {
$userName = 'Admin' ;
2017-01-23 16:19:17 +00:00
$this -> setupTestUser ( $userName );
2016-08-12 01:41:08 +00:00
$userId = User :: newFromName ( 'Admin' ) -> getId ();
2016-12-05 18:51:07 +00:00
$expectedUserMentions = [
'validMentions' => [],
'unknownUsers' => [],
'anonymousUsers' => [],
];
$userLinks = [ $userName => $userId ];
2016-07-05 14:45:14 +00:00
$this -> testGetUserMentions ( $userLinks , $expectedUserMentions , $userId );
}
public function testGetUserMentions_tooManyMentions () {
2016-12-05 18:51:07 +00:00
$userLinks = [
2016-07-05 14:45:14 +00:00
'NotKnown1' => 0 ,
'NotKnown2' => 0 ,
'NotKnown3' => 0 ,
'127.0.0.1' => 0 ,
'127.0.0.2' => 0 ,
2016-12-05 18:51:07 +00:00
];
2016-07-05 14:45:14 +00:00
2022-11-02 20:34:04 +00:00
$this -> overrideConfigValues ( [
2016-07-05 14:45:14 +00:00
// lower limit for the mention-too-many notification
2022-11-02 20:34:04 +00:00
'EchoMaxMentionsCount' => 3
2016-12-05 18:51:07 +00:00
] );
2016-07-05 14:45:14 +00:00
$title = Title :: newFromText ( 'Test' );
2022-11-12 07:19:00 +00:00
$discussionParser = TestingAccessWrapper :: newFromClass ( DiscussionParser :: class );
2019-10-23 10:28:30 +00:00
$this -> assertSame ( 4 , $discussionParser -> getOverallUserMentionsCount ( $discussionParser -> getUserMentions ( $title , 1 , $userLinks ) ) );
2016-07-05 14:45:14 +00:00
}
2015-02-17 21:23:26 +00:00
protected function isParserFunctionsInstalled () {
2017-12-27 12:13:37 +00:00
if ( ExtensionRegistry :: getInstance () -> isLoaded ( 'ParserFunctions' ) ) {
2015-02-17 21:23:26 +00:00
return true ;
} else {
return " ParserFunctions not enabled " ;
}
}
2016-09-01 20:48:56 +00:00
public function testGetTextSnippet () {
2022-12-28 19:19:01 +00:00
$lang = $this -> getServiceContainer () -> getLanguageFactory () -> getLanguage ( 'en' );
2016-09-01 20:48:56 +00:00
$this -> assertEquals (
'Page001' ,
2022-11-12 07:19:00 +00:00
DiscussionParser :: getTextSnippet (
2016-09-01 20:48:56 +00:00
'[[:{{BASEPAGENAME}}]]' ,
2022-12-28 19:19:01 +00:00
$lang ,
2022-11-12 07:19:00 +00:00
DiscussionParser :: DEFAULT_SNIPPET_LENGTH ,
2016-09-01 20:48:56 +00:00
Title :: newFromText ( 'Page001' )
)
);
2022-01-20 22:52:01 +00:00
$this -> assertEquals (
'Hello' ,
2022-11-12 07:19:00 +00:00
DiscussionParser :: getTextSnippet (
2022-01-20 22:52:01 +00:00
'* Hello' ,
2022-12-28 19:19:01 +00:00
$lang ,
2022-11-12 07:19:00 +00:00
DiscussionParser :: DEFAULT_SNIPPET_LENGTH ,
2022-01-20 22:52:01 +00:00
null ,
true
)
);
$this -> assertEquals (
'* Hello' ,
2022-11-12 07:19:00 +00:00
DiscussionParser :: getTextSnippet (
2022-01-20 22:52:01 +00:00
'* Hello' ,
2022-12-28 19:19:01 +00:00
$lang ,
2022-11-12 07:19:00 +00:00
DiscussionParser :: DEFAULT_SNIPPET_LENGTH ,
2022-01-20 22:52:01 +00:00
null ,
false
)
);
2016-09-01 20:48:56 +00:00
}
2012-08-30 16:04:39 +00:00
}