Add PHPUnit integration tests for EventDispatcher

Bug: T286608
Change-Id: I711483be80d455f4439e96d37844ee4552619a92
This commit is contained in:
Bartosz Dziewoński 2021-07-15 15:56:13 +02:00
parent b0884b177c
commit 801b57b0f4
45 changed files with 674 additions and 0 deletions

View file

@ -18,6 +18,7 @@ use MediaWiki\Extension\DiscussionTools\CommentParser;
use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils;
use MediaWiki\Extension\DiscussionTools\SubscriptionItem;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\User\UserIdentity;
use ParserOptions;
@ -98,6 +99,27 @@ class EventDispatcher {
$oldParser = self::getParsedRevision( $oldRevRecord );
$newParser = self::getParsedRevision( $newRevRecord );
self::generateEventsFromParsers( $events, $oldParser, $newParser, $newRevRecord, $title, $user );
}
/**
* Helper for generateEventsForRevision(), separated out for easier testing.
*
* @param array &$events
* @param CommentParser $oldParser
* @param CommentParser $newParser
* @param RevisionRecord $newRevRecord
* @param PageIdentity $title
* @param UserIdentity $user
*/
protected static function generateEventsFromParsers(
array &$events,
CommentParser $oldParser,
CommentParser $newParser,
RevisionRecord $newRevRecord,
PageIdentity $title,
UserIdentity $user
) {
$newComments = [];
foreach ( $newParser->getCommentItems() as $newComment ) {
if (

View file

@ -0,0 +1,4 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-Y-2020-01-01T00:02:00.000Z",
"comment-id": "c-Y-2020-01-01T00:02:00.000Z-C",
"comment-name": "c-Y-2020-01-01T00:02:00.000Z",
"content": "D.",
"section-title": "C",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,5 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1,5 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1,30 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-X-2020-01-01T00:01:00.000Z",
"comment-id": "c-Z-2020-01-01T00:03:00.000Z-X-2020-01-01T00:01:00.000Z",
"comment-name": "c-Z-2020-01-01T00:03:00.000Z",
"content": "E.",
"section-title": "A",
"revid": null,
"mentioned-users": []
},
"agent": {}
},
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-Y-2020-01-01T00:02:00.000Z",
"comment-id": "c-Z-2020-01-01T00:03:00.000Z-Y-2020-01-01T00:02:00.000Z",
"comment-name": "c-Z-2020-01-01T00:03:00.000Z",
"content": "F.",
"section-title": "C",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,7 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
:E. --[[User:Z]] 00:03, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)
:F. --[[User:Z]] 00:03, 1 January 2020 (UTC)

View file

@ -0,0 +1,5 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-Z-2020-01-01T00:03:00.000Z",
"comment-id": "c-Z-2020-01-01T00:03:00.000Z-E",
"comment-name": "c-Z-2020-01-01T00:03:00.000Z",
"content": "F.",
"section-title": "E",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,8 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)
== E ==
F. --[[User:Z]] 00:03, 1 January 2020 (UTC)

View file

@ -0,0 +1,7 @@
Intro.
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1 @@
[]

View file

@ -0,0 +1,11 @@
Intro.
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== E ==
F. --[[User:X]] 00:03, 1 January 2020 (UTC)
:G. --[[User:Y]] 00:04, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1 @@
[]

View file

@ -0,0 +1,11 @@
Intro.
== A ==
B bbb. --[[User:X]] 00:01, 1 January 2020 (UTC)
== E ==
F. --[[User:X]] 00:03, 1 January 2020 (UTC)
:G. --[[User:Y]] 00:04, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1 @@
[]

View file

@ -0,0 +1,11 @@
Intro intro.
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== E ==
F. --[[User:X]] 00:03, 1 January 2020 (UTC)
:G. --[[User:Y]] 00:04, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1,5 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-X-2020-01-01T00:03:00.000Z",
"comment-id": "c-X-2020-01-01T00:03:00.000Z",
"comment-name": "c-X-2020-01-01T00:03:00.000Z",
"content": "E.",
"section-title": "",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,7 @@
E. --[[User:X]] 00:03, 1 January 2020 (UTC)
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-X-2020-01-01T00:03:00.000Z",
"comment-id": "c-Y-2020-01-01T00:04:00.000Z-X-2020-01-01T00:03:00.000Z",
"comment-name": "c-Y-2020-01-01T00:04:00.000Z",
"content": "F.",
"section-title": "",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,8 @@
E. --[[User:X]] 00:03, 1 January 2020 (UTC)
:F. --[[User:Y]] 00:04, 1 January 2020 (UTC)
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1,5 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-X-2020-01-01T00:01:00.000Z",
"comment-id": "c-Z-2020-01-01T00:03:00.000Z-X-2020-01-01T00:01:00.000Z",
"comment-name": "c-Z-2020-01-01T00:03:00.000Z",
"content": "E.",
"section-title": "A",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,6 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
:E. --[[User:Z]] 00:03, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-Y-2020-01-01T00:02:00.000Z",
"comment-id": "c-Z-2020-01-01T00:04:00.000Z-Y-2020-01-01T00:02:00.000Z",
"comment-name": "c-Z-2020-01-01T00:04:00.000Z",
"content": "F.",
"section-title": "C",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,7 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
:E. --[[User:Z]] 00:03, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)
:F. --[[User:Z]] 00:04, 1 January 2020 (UTC)

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-Y-2020-01-01T00:02:00.000Z",
"comment-id": "c-Y-2020-01-01T00:05:00.000Z-Z-2020-01-01T00:04:00.000Z",
"comment-name": "c-Y-2020-01-01T00:05:00.000Z",
"content": "G.",
"section-title": "C",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,8 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
:E. --[[User:Z]] 00:03, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)
:F. --[[User:Z]] 00:04, 1 January 2020 (UTC)
::G. --[[User:Y]] 00:05, 1 January 2020 (UTC)

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-X-2020-01-01T00:01:00.000Z",
"comment-id": "c-X-2020-01-01T00:06:00.000Z-Z-2020-01-01T00:03:00.000Z",
"comment-name": "c-X-2020-01-01T00:06:00.000Z",
"content": "H.",
"section-title": "A",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,9 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
:E. --[[User:Z]] 00:03, 1 January 2020 (UTC)
::H. --[[User:X]] 00:06, 1 January 2020 (UTC)
== C ==
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)
:F. --[[User:Z]] 00:04, 1 January 2020 (UTC)
::G. --[[User:Y]] 00:05, 1 January 2020 (UTC)

View file

@ -0,0 +1,6 @@
== A ==
=== B ===
C. --[[User:Y]] 00:01, 1 January 2020 (UTC)
=== D ===

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-Y-2020-01-01T00:01:00.000Z",
"comment-id": "c-Z-2020-01-01T00:02:00.000Z-Y-2020-01-01T00:01:00.000Z",
"comment-name": "c-Z-2020-01-01T00:02:00.000Z",
"content": "E.",
"section-title": "B",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,7 @@
== A ==
=== B ===
C. --[[User:Y]] 00:01, 1 January 2020 (UTC)
:E. --[[User:Z]] 00:02, 1 January 2020 (UTC)
=== D ===

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-Z-2020-01-01T00:03:00.000Z",
"comment-id": "c-Z-2020-01-01T00:03:00.000Z-D",
"comment-name": "c-Z-2020-01-01T00:03:00.000Z",
"content": "F.",
"section-title": "D",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,8 @@
== A ==
=== B ===
C. --[[User:Y]] 00:01, 1 January 2020 (UTC)
:E. --[[User:Z]] 00:02, 1 January 2020 (UTC)
=== D ===
F. --[[User:Z]] 00:03, 1 January 2020 (UTC)

View file

@ -0,0 +1,7 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
=== C ===
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)
=== E ===

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-X-2020-01-01T00:01:00.000Z",
"comment-id": "c-Z-2020-01-01T00:03:00.000Z-X-2020-01-01T00:01:00.000Z",
"comment-name": "c-Z-2020-01-01T00:03:00.000Z",
"content": "F.",
"section-title": "A",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,8 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
:F. --[[User:Z]] 00:03, 1 January 2020 (UTC)
=== C ===
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)
=== E ===

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-Y-2020-01-01T00:02:00.000Z",
"comment-id": "c-Z-2020-01-01T00:04:00.000Z-Y-2020-01-01T00:02:00.000Z",
"comment-name": "c-Z-2020-01-01T00:04:00.000Z",
"content": "G.",
"section-title": "C",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,9 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
:F. --[[User:Z]] 00:03, 1 January 2020 (UTC)
=== C ===
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)
:G. --[[User:Z]] 00:04, 1 January 2020 (UTC)
=== E ===

View file

@ -0,0 +1,16 @@
[
{
"type": "dt-subscribed-new-comment",
"title": {},
"extra": {
"subscribed-comment-name": "h-Z-2020-01-01T00:05:00.000Z",
"comment-id": "c-Z-2020-01-01T00:05:00.000Z-E",
"comment-name": "c-Z-2020-01-01T00:05:00.000Z",
"content": "H.",
"section-title": "E",
"revid": null,
"mentioned-users": []
},
"agent": {}
}
]

View file

@ -0,0 +1,10 @@
== A ==
B. --[[User:X]] 00:01, 1 January 2020 (UTC)
:F. --[[User:Z]] 00:03, 1 January 2020 (UTC)
=== C ===
D. --[[User:Y]] 00:02, 1 January 2020 (UTC)
:G. --[[User:Z]] 00:04, 1 January 2020 (UTC)
=== E ===
H. --[[User:Z]] 00:05, 1 January 2020 (UTC)

View file

@ -0,0 +1,182 @@
<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
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
* @covers ::generateEventsFromParsers
*/
public function testGenerateEventsFromParsers(
string $rev1, string $rev2, string $authorUsername, string $expected
): void {
$wikitext1 = self::getText( $rev1 );
$wikitext2 = self::getText( $rev2 );
$expectedEvents = self::getJson( $expected, false );
$config = self::getJson( "../data/enwiki-config.json" );
$data = self::getJson( "../data/enwiki-data.json" );
$dom1 = ( new RawMessage( $wikitext1 ) )->parse();
$doc1 = self::createDocument( $dom1 );
$body1 = $doc1->getElementsByTagName( 'body' )->item( 0 );
$dom2 = ( new RawMessage( $wikitext2 ) )->parse();
$doc2 = self::createDocument( $dom2 );
$body2 = $doc2->getElementsByTagName( 'body' )->item( 0 );
$this->setupEnv( $config, $data );
$parser1 = self::createParser( $body1, $data );
$parser2 = self::createParser( $body2, $data );
$events = [];
$fakeUser = new UserIdentityValue( 0, $authorUsername );
$fakeTitle = new PageIdentityValue( 0, NS_TALK, __CLASS__, PageIdentityValue::LOCAL );
$fakeRevRecord = new MutableRevisionRecord( $fakeTitle );
MockEventDispatcher::generateEventsFromParsers(
$events, $parser1, $parser2, $fakeRevRecord, $fakeTitle, $fakeUser
);
foreach ( $events as &$event ) {
$event = json_decode( json_encode( $event ), false );
}
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
self::overwriteJsonFile( $expected, $events );
}
self::assertEquals( $expectedEvents, $events );
}
public function provideGenerateCases(): array {
return [
// Several simple edits adding replies by different users.
[
'cases/EventDispatcher/simple/rev1.txt',
'cases/EventDispatcher/simple/rev2.txt',
'Z',
'../cases/EventDispatcher/simple/rev2.json',
],
[
'cases/EventDispatcher/simple/rev2.txt',
'cases/EventDispatcher/simple/rev3.txt',
'Z',
'../cases/EventDispatcher/simple/rev3.json',
],
[
'cases/EventDispatcher/simple/rev3.txt',
'cases/EventDispatcher/simple/rev4.txt',
'Y',
'../cases/EventDispatcher/simple/rev4.json',
],
[
'cases/EventDispatcher/simple/rev4.txt',
'cases/EventDispatcher/simple/rev5.txt',
'X',
'../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',
'../cases/EventDispatcher/newsection/rev2.json',
],
// Adding multiple replies in one edit.
[
'cases/EventDispatcher/multiple/rev1.txt',
'cases/EventDispatcher/multiple/rev2.txt',
'Z',
'../cases/EventDispatcher/multiple/rev2.json',
],
// Adding comments in section 0 (before first heading).
[
'cases/EventDispatcher/section0/rev1.txt',
'cases/EventDispatcher/section0/rev2.txt',
'X',
'../cases/EventDispatcher/section0/rev2.json',
],
[
'cases/EventDispatcher/section0/rev2.txt',
'cases/EventDispatcher/section0/rev3.txt',
'Y',
'../cases/EventDispatcher/section0/rev3.json',
],
// Adding a comment in a previously empty section.
[
'cases/EventDispatcher/emptysection/rev1.txt',
'cases/EventDispatcher/emptysection/rev2.txt',
'Y',
'../cases/EventDispatcher/emptysection/rev2.json',
],
// Adding comments in sub-sections, where the parent section has no comments (except in
// sub-sections).
[
'cases/EventDispatcher/subsection-empty/rev1.txt',
'cases/EventDispatcher/subsection-empty/rev2.txt',
'Z',
'../cases/EventDispatcher/subsection-empty/rev2.json',
],
[
'cases/EventDispatcher/subsection-empty/rev2.txt',
'cases/EventDispatcher/subsection-empty/rev3.txt',
'Z',
'../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',
'../cases/EventDispatcher/subsection/rev2.json',
],
[
'cases/EventDispatcher/subsection/rev2.txt',
'cases/EventDispatcher/subsection/rev3.txt',
'Z',
'../cases/EventDispatcher/subsection/rev3.json',
],
[
'cases/EventDispatcher/subsection/rev3.txt',
'cases/EventDispatcher/subsection/rev4.txt',
'Z',
'../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',
'../cases/EventDispatcher/notcomments/rev2.json',
],
[
// Editing a comment
'cases/EventDispatcher/notcomments/rev2.txt',
'cases/EventDispatcher/notcomments/rev3.txt',
'X',
'../cases/EventDispatcher/notcomments/rev3.json',
],
[
// Editing page intro section
'cases/EventDispatcher/notcomments/rev3.txt',
'cases/EventDispatcher/notcomments/rev4.txt',
'X',
'../cases/EventDispatcher/notcomments/rev4.json',
],
];
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
use MediaWiki\Extension\DiscussionTools\CommentParser;
use MediaWiki\Extension\DiscussionTools\Notifications\EventDispatcher;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\User\UserIdentity;
class MockEventDispatcher extends EventDispatcher {
/**
* Public for testing
*
* Note that we can't use TestingAccessWrapper instead of this, because it doesn't support passing
* arguments by reference (causes exceptions like "PHPUnit\Framework\Error\Warning: Parameter 1 to
* ... expected to be a reference, value given").
*
* @param array &$events
* @param CommentParser $oldParser
* @param CommentParser $newParser
* @param RevisionRecord $newRevRecord
* @param PageIdentity $title
* @param UserIdentity $user
*/
public static function generateEventsFromParsers(
array &$events,
CommentParser $oldParser,
CommentParser $newParser,
RevisionRecord $newRevRecord,
PageIdentity $title,
UserIdentity $user
) {
parent::generateEventsFromParsers(
$events,
$oldParser,
$newParser,
$newRevRecord,
$title,
$user
);
}
}