2021-07-15 13:56:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Tests;
|
|
|
|
|
2021-11-08 18:00:25 +00:00
|
|
|
use DateTimeImmutable;
|
2022-02-21 22:07:38 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-07-15 13:56:13 +00:00
|
|
|
use MediaWiki\Page\PageIdentityValue;
|
|
|
|
use MediaWiki\Revision\MutableRevisionRecord;
|
|
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
use RawMessage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\Notifications\EventDispatcher
|
|
|
|
*
|
|
|
|
* @group DiscussionTools
|
|
|
|
*/
|
|
|
|
class EventDispatcherTest extends IntegrationTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideGenerateCases
|
2022-02-19 06:31:34 +00:00
|
|
|
* @covers ::generateEventsFromItemSets
|
2021-07-15 13:56:13 +00:00
|
|
|
*/
|
|
|
|
public function testGenerateEventsFromParsers(
|
2022-07-31 01:08:28 +00:00
|
|
|
string $rev1, string $rev2, string $authorUsername, ?string $other, string $expected
|
2021-07-15 13:56:13 +00:00
|
|
|
): void {
|
2022-06-09 13:51:33 +00:00
|
|
|
$wikitext1 = static::getText( $rev1 );
|
|
|
|
$wikitext2 = static::getText( $rev2 );
|
|
|
|
$expectedEvents = static::getJson( $expected, false );
|
|
|
|
$config = static::getJson( "../data/enwiki-config.json" );
|
|
|
|
$data = static::getJson( "../data/enwiki-data.json" );
|
2021-07-15 13:56:13 +00:00
|
|
|
|
|
|
|
$dom1 = ( new RawMessage( $wikitext1 ) )->parse();
|
2022-06-09 13:51:33 +00:00
|
|
|
$doc1 = static::createDocument( $dom1 );
|
|
|
|
$container1 = static::getThreadContainer( $doc1 );
|
2021-07-15 13:56:13 +00:00
|
|
|
|
|
|
|
$dom2 = ( new RawMessage( $wikitext2 ) )->parse();
|
2022-06-09 13:51:33 +00:00
|
|
|
$doc2 = static::createDocument( $dom2 );
|
|
|
|
$container2 = static::getThreadContainer( $doc2 );
|
2021-07-15 13:56:13 +00:00
|
|
|
|
|
|
|
$this->setupEnv( $config, $data );
|
2022-02-21 22:07:38 +00:00
|
|
|
$dummyTitle = MediaWikiServices::getInstance()->getTitleParser()->parseTitle( 'Dummy' );
|
2022-06-09 13:51:33 +00:00
|
|
|
$parser = static::createParser( $data );
|
2022-03-09 19:32:51 +00:00
|
|
|
$itemSet1 = $parser->parse( $container1, $dummyTitle );
|
|
|
|
$itemSet2 = $parser->parse( $container2, $dummyTitle );
|
2021-07-15 13:56:13 +00:00
|
|
|
|
2022-07-31 01:08:28 +00:00
|
|
|
$events = $other ? static::getJson( $other, true ) : [];
|
2021-07-15 13:56:13 +00:00
|
|
|
|
|
|
|
$fakeUser = new UserIdentityValue( 0, $authorUsername );
|
|
|
|
$fakeTitle = new PageIdentityValue( 0, NS_TALK, __CLASS__, PageIdentityValue::LOCAL );
|
|
|
|
$fakeRevRecord = new MutableRevisionRecord( $fakeTitle );
|
2021-11-08 18:00:25 +00:00
|
|
|
// All mock comments are posted between 00:00 and 00:10 on 2020-01-01
|
|
|
|
$fakeRevRecord->setTimestamp( ( new DateTimeImmutable( '2020-01-01T00:10' ) )->format( 'c' ) );
|
2022-02-19 06:31:34 +00:00
|
|
|
MockEventDispatcher::generateEventsFromItemSets(
|
|
|
|
$events, $itemSet1, $itemSet2, $fakeRevRecord, $fakeTitle, $fakeUser
|
2021-07-15 13:56:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $events as &$event ) {
|
|
|
|
$event = json_decode( json_encode( $event ), false );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally write updated content to the JSON files
|
|
|
|
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
|
2022-06-09 13:51:33 +00:00
|
|
|
static::overwriteJsonFile( $expected, $events );
|
2021-07-15 13:56:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals( $expectedEvents, $events );
|
2021-11-08 18:00:25 +00:00
|
|
|
|
|
|
|
// Assert that no events are generated for comments saved >10 minutes after their timestamps
|
2022-07-31 01:08:28 +00:00
|
|
|
$events = $other ? static::getJson( $other, true ) : [];
|
2021-11-08 18:00:25 +00:00
|
|
|
$fakeRevRecord->setTimestamp( ( new DateTimeImmutable( '2020-01-01T00:20' ) )->format( 'c' ) );
|
2022-02-19 06:31:34 +00:00
|
|
|
MockEventDispatcher::generateEventsFromItemSets(
|
|
|
|
$events, $itemSet1, $itemSet2, $fakeRevRecord, $fakeTitle, $fakeUser
|
2021-11-08 18:00:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $events as &$event ) {
|
|
|
|
$event = json_decode( json_encode( $event ), false );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other events (e.g. from Echo) are never removed
|
|
|
|
$events = array_filter( $events, static function ( $event ) {
|
|
|
|
return $event->type === 'dt-subscribed-new-comment';
|
|
|
|
} );
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
static::assertEquals( [], $events );
|
2021-07-15 13:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideGenerateCases(): array {
|
|
|
|
return [
|
|
|
|
// Several simple edits adding replies by different users.
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/simple/rev1.txt',
|
|
|
|
'cases/EventDispatcher/simple/rev2.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/simple/rev2.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/simple/rev2.txt',
|
|
|
|
'cases/EventDispatcher/simple/rev3.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/simple/rev3.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/simple/rev3.txt',
|
|
|
|
'cases/EventDispatcher/simple/rev4.txt',
|
|
|
|
'Y',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/simple/rev4.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/simple/rev4.txt',
|
|
|
|
'cases/EventDispatcher/simple/rev5.txt',
|
|
|
|
'X',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/simple/rev5.json',
|
|
|
|
],
|
|
|
|
// Adding a new section with heading and a top-level comment.
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/newsection/rev1.txt',
|
|
|
|
'cases/EventDispatcher/newsection/rev2.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/newsection/rev2.json',
|
|
|
|
],
|
|
|
|
// Adding multiple replies in one edit.
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/multiple/rev1.txt',
|
|
|
|
'cases/EventDispatcher/multiple/rev2.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/multiple/rev2.json',
|
|
|
|
],
|
2021-08-14 15:56:41 +00:00
|
|
|
// Adding comments in section 0 (before first heading).
|
|
|
|
// These do not generate notifications, because the interface doesn't allow subscribing to it.
|
2021-07-15 13:56:13 +00:00
|
|
|
[
|
|
|
|
'cases/EventDispatcher/section0/rev1.txt',
|
|
|
|
'cases/EventDispatcher/section0/rev2.txt',
|
|
|
|
'X',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/section0/rev2.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/section0/rev2.txt',
|
|
|
|
'cases/EventDispatcher/section0/rev3.txt',
|
|
|
|
'Y',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/section0/rev3.json',
|
|
|
|
],
|
2021-08-14 15:56:41 +00:00
|
|
|
// Adding comments in section starting with a heading with level 1.
|
|
|
|
// These do not generate notifications, because the interface doesn't allow subscribing to it.
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sectionlevel1/rev1.txt',
|
|
|
|
'cases/EventDispatcher/sectionlevel1/rev2.txt',
|
|
|
|
'Y',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-08-14 15:56:41 +00:00
|
|
|
'../cases/EventDispatcher/sectionlevel1/rev2.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sectionlevel1/rev2.txt',
|
|
|
|
'cases/EventDispatcher/sectionlevel1/rev3.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-08-14 15:56:41 +00:00
|
|
|
'../cases/EventDispatcher/sectionlevel1/rev3.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sectionlevel1/rev3.txt',
|
|
|
|
'cases/EventDispatcher/sectionlevel1/rev4.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-08-14 15:56:41 +00:00
|
|
|
'../cases/EventDispatcher/sectionlevel1/rev4.json',
|
|
|
|
],
|
|
|
|
// Adding comments in section starting with a heading with level 3 (not following level 2 headings).
|
|
|
|
// These do not generate notifications, because the interface doesn't allow subscribing to it.
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sectionlevel3/rev1.txt',
|
|
|
|
'cases/EventDispatcher/sectionlevel3/rev2.txt',
|
|
|
|
'Y',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-08-14 15:56:41 +00:00
|
|
|
'../cases/EventDispatcher/sectionlevel3/rev2.json',
|
|
|
|
],
|
2021-07-15 13:56:13 +00:00
|
|
|
// Adding a comment in a previously empty section.
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/emptysection/rev1.txt',
|
|
|
|
'cases/EventDispatcher/emptysection/rev2.txt',
|
|
|
|
'Y',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/emptysection/rev2.json',
|
|
|
|
],
|
|
|
|
// Adding comments in sub-sections, where the parent section has no comments (except in
|
2021-06-25 13:24:46 +00:00
|
|
|
// sub-sections). These do not generate notifications because of the fix for T286736,
|
|
|
|
// but maybe they should?
|
2021-07-15 13:56:13 +00:00
|
|
|
[
|
|
|
|
'cases/EventDispatcher/subsection-empty/rev1.txt',
|
|
|
|
'cases/EventDispatcher/subsection-empty/rev2.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/subsection-empty/rev2.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/subsection-empty/rev2.txt',
|
|
|
|
'cases/EventDispatcher/subsection-empty/rev3.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/subsection-empty/rev3.json',
|
|
|
|
],
|
|
|
|
// Adding comments in sub-sections, where the parent section also has comments.
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/subsection/rev1.txt',
|
|
|
|
'cases/EventDispatcher/subsection/rev2.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/subsection/rev2.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/subsection/rev2.txt',
|
|
|
|
'cases/EventDispatcher/subsection/rev3.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/subsection/rev3.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/subsection/rev3.txt',
|
|
|
|
'cases/EventDispatcher/subsection/rev4.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/subsection/rev4.json',
|
|
|
|
],
|
|
|
|
// Edits that do not add comments, and do not generate notifications.
|
|
|
|
[
|
|
|
|
// Copying a discussion from another page (note the author of revision)
|
|
|
|
'cases/EventDispatcher/notcomments/rev1.txt',
|
|
|
|
'cases/EventDispatcher/notcomments/rev2.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/notcomments/rev2.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
// Editing a comment
|
|
|
|
'cases/EventDispatcher/notcomments/rev2.txt',
|
|
|
|
'cases/EventDispatcher/notcomments/rev3.txt',
|
|
|
|
'X',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/notcomments/rev3.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
// Editing page intro section
|
|
|
|
'cases/EventDispatcher/notcomments/rev3.txt',
|
|
|
|
'cases/EventDispatcher/notcomments/rev4.txt',
|
|
|
|
'X',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-15 13:56:13 +00:00
|
|
|
'../cases/EventDispatcher/notcomments/rev4.json',
|
|
|
|
],
|
2021-07-16 16:01:18 +00:00
|
|
|
// Multiple edits within a minute adding comments by the same user.
|
|
|
|
// See T285528#7177220 for more detail about each case.
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sametime/rev1.txt',
|
|
|
|
'cases/EventDispatcher/sametime/rev2.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-16 16:01:18 +00:00
|
|
|
'../cases/EventDispatcher/sametime/rev2.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sametime/rev2.txt',
|
|
|
|
'cases/EventDispatcher/sametime/rev3-case1.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-16 16:01:18 +00:00
|
|
|
'../cases/EventDispatcher/sametime/rev3-case1.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sametime/rev2.txt',
|
|
|
|
'cases/EventDispatcher/sametime/rev3-case2.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-16 16:01:18 +00:00
|
|
|
'../cases/EventDispatcher/sametime/rev3-case2.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sametime/rev2.txt',
|
|
|
|
'cases/EventDispatcher/sametime/rev3-case3.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-16 16:01:18 +00:00
|
|
|
'../cases/EventDispatcher/sametime/rev3-case3.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sametime/rev2.txt',
|
|
|
|
'cases/EventDispatcher/sametime/rev3-case4.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-16 16:01:18 +00:00
|
|
|
'../cases/EventDispatcher/sametime/rev3-case4.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sametime/rev2.txt',
|
|
|
|
'cases/EventDispatcher/sametime/rev3-case5.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-16 16:01:18 +00:00
|
|
|
'../cases/EventDispatcher/sametime/rev3-case5.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sametime/rev1b.txt',
|
|
|
|
'cases/EventDispatcher/sametime/rev2b.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-16 16:01:18 +00:00
|
|
|
'../cases/EventDispatcher/sametime/rev2b.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sametime/rev2b.txt',
|
|
|
|
'cases/EventDispatcher/sametime/rev3b-case6.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-16 16:01:18 +00:00
|
|
|
'../cases/EventDispatcher/sametime/rev3b-case6.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'cases/EventDispatcher/sametime/rev2b.txt',
|
|
|
|
'cases/EventDispatcher/sametime/rev3b-case7.txt',
|
|
|
|
'Z',
|
2022-07-31 01:08:28 +00:00
|
|
|
null,
|
2021-07-16 16:01:18 +00:00
|
|
|
'../cases/EventDispatcher/sametime/rev3b-case7.json',
|
|
|
|
],
|
2021-07-20 22:25:21 +00:00
|
|
|
// Interactions with events generated by base Echo.
|
|
|
|
[
|
|
|
|
// User talk page edit
|
|
|
|
'cases/EventDispatcher/other-events/rev1.txt',
|
|
|
|
'cases/EventDispatcher/other-events/rev2a.txt',
|
|
|
|
'Y',
|
|
|
|
'../cases/EventDispatcher/other-events/rev2a-other.json',
|
|
|
|
'../cases/EventDispatcher/other-events/rev2a.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
// Mention
|
|
|
|
'cases/EventDispatcher/other-events/rev1.txt',
|
|
|
|
'cases/EventDispatcher/other-events/rev2b.txt',
|
|
|
|
'Y',
|
|
|
|
'../cases/EventDispatcher/other-events/rev2b-other.json',
|
|
|
|
'../cases/EventDispatcher/other-events/rev2b.json',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
// Mention in edit summary
|
|
|
|
'cases/EventDispatcher/other-events/rev1.txt',
|
|
|
|
'cases/EventDispatcher/other-events/rev2c.txt',
|
|
|
|
'Y',
|
|
|
|
'../cases/EventDispatcher/other-events/rev2c-other.json',
|
|
|
|
'../cases/EventDispatcher/other-events/rev2c.json',
|
|
|
|
],
|
2021-07-15 13:56:13 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|