From 801b57b0f4bff63cf9ccda7d514660833eb8b9e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Thu, 15 Jul 2021 15:56:13 +0200 Subject: [PATCH] Add PHPUnit integration tests for EventDispatcher Bug: T286608 Change-Id: I711483be80d455f4439e96d37844ee4552619a92 --- includes/Notifications/EventDispatcher.php | 22 +++ .../EventDispatcher/emptysection/rev1.txt | 4 + .../EventDispatcher/emptysection/rev2.json | 16 ++ .../EventDispatcher/emptysection/rev2.txt | 5 + tests/cases/EventDispatcher/multiple/rev1.txt | 5 + .../cases/EventDispatcher/multiple/rev2.json | 30 +++ tests/cases/EventDispatcher/multiple/rev2.txt | 7 + .../cases/EventDispatcher/newsection/rev1.txt | 5 + .../EventDispatcher/newsection/rev2.json | 16 ++ .../cases/EventDispatcher/newsection/rev2.txt | 8 + .../EventDispatcher/notcomments/rev1.txt | 7 + .../EventDispatcher/notcomments/rev2.json | 1 + .../EventDispatcher/notcomments/rev2.txt | 11 ++ .../EventDispatcher/notcomments/rev3.json | 1 + .../EventDispatcher/notcomments/rev3.txt | 11 ++ .../EventDispatcher/notcomments/rev4.json | 1 + .../EventDispatcher/notcomments/rev4.txt | 11 ++ tests/cases/EventDispatcher/section0/rev1.txt | 5 + .../cases/EventDispatcher/section0/rev2.json | 16 ++ tests/cases/EventDispatcher/section0/rev2.txt | 7 + .../cases/EventDispatcher/section0/rev3.json | 16 ++ tests/cases/EventDispatcher/section0/rev3.txt | 8 + tests/cases/EventDispatcher/simple/rev1.txt | 5 + tests/cases/EventDispatcher/simple/rev2.json | 16 ++ tests/cases/EventDispatcher/simple/rev2.txt | 6 + tests/cases/EventDispatcher/simple/rev3.json | 16 ++ tests/cases/EventDispatcher/simple/rev3.txt | 7 + tests/cases/EventDispatcher/simple/rev4.json | 16 ++ tests/cases/EventDispatcher/simple/rev4.txt | 8 + tests/cases/EventDispatcher/simple/rev5.json | 16 ++ tests/cases/EventDispatcher/simple/rev5.txt | 9 + .../EventDispatcher/subsection-empty/rev1.txt | 6 + .../subsection-empty/rev2.json | 16 ++ .../EventDispatcher/subsection-empty/rev2.txt | 7 + .../subsection-empty/rev3.json | 16 ++ .../EventDispatcher/subsection-empty/rev3.txt | 8 + .../cases/EventDispatcher/subsection/rev1.txt | 7 + .../EventDispatcher/subsection/rev2.json | 16 ++ .../cases/EventDispatcher/subsection/rev2.txt | 8 + .../EventDispatcher/subsection/rev3.json | 16 ++ .../cases/EventDispatcher/subsection/rev3.txt | 9 + .../EventDispatcher/subsection/rev4.json | 16 ++ .../cases/EventDispatcher/subsection/rev4.txt | 10 + tests/phpunit/EventDispatcherTest.php | 182 ++++++++++++++++++ tests/phpunit/MockEventDispatcher.php | 45 +++++ 45 files changed, 674 insertions(+) create mode 100644 tests/cases/EventDispatcher/emptysection/rev1.txt create mode 100644 tests/cases/EventDispatcher/emptysection/rev2.json create mode 100644 tests/cases/EventDispatcher/emptysection/rev2.txt create mode 100644 tests/cases/EventDispatcher/multiple/rev1.txt create mode 100644 tests/cases/EventDispatcher/multiple/rev2.json create mode 100644 tests/cases/EventDispatcher/multiple/rev2.txt create mode 100644 tests/cases/EventDispatcher/newsection/rev1.txt create mode 100644 tests/cases/EventDispatcher/newsection/rev2.json create mode 100644 tests/cases/EventDispatcher/newsection/rev2.txt create mode 100644 tests/cases/EventDispatcher/notcomments/rev1.txt create mode 100644 tests/cases/EventDispatcher/notcomments/rev2.json create mode 100644 tests/cases/EventDispatcher/notcomments/rev2.txt create mode 100644 tests/cases/EventDispatcher/notcomments/rev3.json create mode 100644 tests/cases/EventDispatcher/notcomments/rev3.txt create mode 100644 tests/cases/EventDispatcher/notcomments/rev4.json create mode 100644 tests/cases/EventDispatcher/notcomments/rev4.txt create mode 100644 tests/cases/EventDispatcher/section0/rev1.txt create mode 100644 tests/cases/EventDispatcher/section0/rev2.json create mode 100644 tests/cases/EventDispatcher/section0/rev2.txt create mode 100644 tests/cases/EventDispatcher/section0/rev3.json create mode 100644 tests/cases/EventDispatcher/section0/rev3.txt create mode 100644 tests/cases/EventDispatcher/simple/rev1.txt create mode 100644 tests/cases/EventDispatcher/simple/rev2.json create mode 100644 tests/cases/EventDispatcher/simple/rev2.txt create mode 100644 tests/cases/EventDispatcher/simple/rev3.json create mode 100644 tests/cases/EventDispatcher/simple/rev3.txt create mode 100644 tests/cases/EventDispatcher/simple/rev4.json create mode 100644 tests/cases/EventDispatcher/simple/rev4.txt create mode 100644 tests/cases/EventDispatcher/simple/rev5.json create mode 100644 tests/cases/EventDispatcher/simple/rev5.txt create mode 100644 tests/cases/EventDispatcher/subsection-empty/rev1.txt create mode 100644 tests/cases/EventDispatcher/subsection-empty/rev2.json create mode 100644 tests/cases/EventDispatcher/subsection-empty/rev2.txt create mode 100644 tests/cases/EventDispatcher/subsection-empty/rev3.json create mode 100644 tests/cases/EventDispatcher/subsection-empty/rev3.txt create mode 100644 tests/cases/EventDispatcher/subsection/rev1.txt create mode 100644 tests/cases/EventDispatcher/subsection/rev2.json create mode 100644 tests/cases/EventDispatcher/subsection/rev2.txt create mode 100644 tests/cases/EventDispatcher/subsection/rev3.json create mode 100644 tests/cases/EventDispatcher/subsection/rev3.txt create mode 100644 tests/cases/EventDispatcher/subsection/rev4.json create mode 100644 tests/cases/EventDispatcher/subsection/rev4.txt create mode 100644 tests/phpunit/EventDispatcherTest.php create mode 100644 tests/phpunit/MockEventDispatcher.php diff --git a/includes/Notifications/EventDispatcher.php b/includes/Notifications/EventDispatcher.php index 309294659..cb787e3e1 100644 --- a/includes/Notifications/EventDispatcher.php +++ b/includes/Notifications/EventDispatcher.php @@ -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 ( diff --git a/tests/cases/EventDispatcher/emptysection/rev1.txt b/tests/cases/EventDispatcher/emptysection/rev1.txt new file mode 100644 index 000000000..e75efb66f --- /dev/null +++ b/tests/cases/EventDispatcher/emptysection/rev1.txt @@ -0,0 +1,4 @@ +== A == +B. --[[User:X]] 00:01, 1 January 2020 (UTC) + +== C == diff --git a/tests/cases/EventDispatcher/emptysection/rev2.json b/tests/cases/EventDispatcher/emptysection/rev2.json new file mode 100644 index 000000000..1c243763f --- /dev/null +++ b/tests/cases/EventDispatcher/emptysection/rev2.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/emptysection/rev2.txt b/tests/cases/EventDispatcher/emptysection/rev2.txt new file mode 100644 index 000000000..a514360b9 --- /dev/null +++ b/tests/cases/EventDispatcher/emptysection/rev2.txt @@ -0,0 +1,5 @@ +== A == +B. --[[User:X]] 00:01, 1 January 2020 (UTC) + +== C == +D. --[[User:Y]] 00:02, 1 January 2020 (UTC) diff --git a/tests/cases/EventDispatcher/multiple/rev1.txt b/tests/cases/EventDispatcher/multiple/rev1.txt new file mode 100644 index 000000000..a514360b9 --- /dev/null +++ b/tests/cases/EventDispatcher/multiple/rev1.txt @@ -0,0 +1,5 @@ +== A == +B. --[[User:X]] 00:01, 1 January 2020 (UTC) + +== C == +D. --[[User:Y]] 00:02, 1 January 2020 (UTC) diff --git a/tests/cases/EventDispatcher/multiple/rev2.json b/tests/cases/EventDispatcher/multiple/rev2.json new file mode 100644 index 000000000..46eeff95f --- /dev/null +++ b/tests/cases/EventDispatcher/multiple/rev2.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/multiple/rev2.txt b/tests/cases/EventDispatcher/multiple/rev2.txt new file mode 100644 index 000000000..41d56ff28 --- /dev/null +++ b/tests/cases/EventDispatcher/multiple/rev2.txt @@ -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) diff --git a/tests/cases/EventDispatcher/newsection/rev1.txt b/tests/cases/EventDispatcher/newsection/rev1.txt new file mode 100644 index 000000000..a514360b9 --- /dev/null +++ b/tests/cases/EventDispatcher/newsection/rev1.txt @@ -0,0 +1,5 @@ +== A == +B. --[[User:X]] 00:01, 1 January 2020 (UTC) + +== C == +D. --[[User:Y]] 00:02, 1 January 2020 (UTC) diff --git a/tests/cases/EventDispatcher/newsection/rev2.json b/tests/cases/EventDispatcher/newsection/rev2.json new file mode 100644 index 000000000..9f2b94290 --- /dev/null +++ b/tests/cases/EventDispatcher/newsection/rev2.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/newsection/rev2.txt b/tests/cases/EventDispatcher/newsection/rev2.txt new file mode 100644 index 000000000..1fbfd18c7 --- /dev/null +++ b/tests/cases/EventDispatcher/newsection/rev2.txt @@ -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) diff --git a/tests/cases/EventDispatcher/notcomments/rev1.txt b/tests/cases/EventDispatcher/notcomments/rev1.txt new file mode 100644 index 000000000..3fb5892f0 --- /dev/null +++ b/tests/cases/EventDispatcher/notcomments/rev1.txt @@ -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) diff --git a/tests/cases/EventDispatcher/notcomments/rev2.json b/tests/cases/EventDispatcher/notcomments/rev2.json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/tests/cases/EventDispatcher/notcomments/rev2.json @@ -0,0 +1 @@ +[] diff --git a/tests/cases/EventDispatcher/notcomments/rev2.txt b/tests/cases/EventDispatcher/notcomments/rev2.txt new file mode 100644 index 000000000..08c43cab8 --- /dev/null +++ b/tests/cases/EventDispatcher/notcomments/rev2.txt @@ -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) diff --git a/tests/cases/EventDispatcher/notcomments/rev3.json b/tests/cases/EventDispatcher/notcomments/rev3.json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/tests/cases/EventDispatcher/notcomments/rev3.json @@ -0,0 +1 @@ +[] diff --git a/tests/cases/EventDispatcher/notcomments/rev3.txt b/tests/cases/EventDispatcher/notcomments/rev3.txt new file mode 100644 index 000000000..08d2d44d2 --- /dev/null +++ b/tests/cases/EventDispatcher/notcomments/rev3.txt @@ -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) diff --git a/tests/cases/EventDispatcher/notcomments/rev4.json b/tests/cases/EventDispatcher/notcomments/rev4.json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/tests/cases/EventDispatcher/notcomments/rev4.json @@ -0,0 +1 @@ +[] diff --git a/tests/cases/EventDispatcher/notcomments/rev4.txt b/tests/cases/EventDispatcher/notcomments/rev4.txt new file mode 100644 index 000000000..dc5f0406f --- /dev/null +++ b/tests/cases/EventDispatcher/notcomments/rev4.txt @@ -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) diff --git a/tests/cases/EventDispatcher/section0/rev1.txt b/tests/cases/EventDispatcher/section0/rev1.txt new file mode 100644 index 000000000..a514360b9 --- /dev/null +++ b/tests/cases/EventDispatcher/section0/rev1.txt @@ -0,0 +1,5 @@ +== A == +B. --[[User:X]] 00:01, 1 January 2020 (UTC) + +== C == +D. --[[User:Y]] 00:02, 1 January 2020 (UTC) diff --git a/tests/cases/EventDispatcher/section0/rev2.json b/tests/cases/EventDispatcher/section0/rev2.json new file mode 100644 index 000000000..4f31c32ea --- /dev/null +++ b/tests/cases/EventDispatcher/section0/rev2.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/section0/rev2.txt b/tests/cases/EventDispatcher/section0/rev2.txt new file mode 100644 index 000000000..8f9c45814 --- /dev/null +++ b/tests/cases/EventDispatcher/section0/rev2.txt @@ -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) diff --git a/tests/cases/EventDispatcher/section0/rev3.json b/tests/cases/EventDispatcher/section0/rev3.json new file mode 100644 index 000000000..b5792eedb --- /dev/null +++ b/tests/cases/EventDispatcher/section0/rev3.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/section0/rev3.txt b/tests/cases/EventDispatcher/section0/rev3.txt new file mode 100644 index 000000000..9eda6027d --- /dev/null +++ b/tests/cases/EventDispatcher/section0/rev3.txt @@ -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) diff --git a/tests/cases/EventDispatcher/simple/rev1.txt b/tests/cases/EventDispatcher/simple/rev1.txt new file mode 100644 index 000000000..a514360b9 --- /dev/null +++ b/tests/cases/EventDispatcher/simple/rev1.txt @@ -0,0 +1,5 @@ +== A == +B. --[[User:X]] 00:01, 1 January 2020 (UTC) + +== C == +D. --[[User:Y]] 00:02, 1 January 2020 (UTC) diff --git a/tests/cases/EventDispatcher/simple/rev2.json b/tests/cases/EventDispatcher/simple/rev2.json new file mode 100644 index 000000000..c81050a55 --- /dev/null +++ b/tests/cases/EventDispatcher/simple/rev2.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/simple/rev2.txt b/tests/cases/EventDispatcher/simple/rev2.txt new file mode 100644 index 000000000..cac2f15d9 --- /dev/null +++ b/tests/cases/EventDispatcher/simple/rev2.txt @@ -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) diff --git a/tests/cases/EventDispatcher/simple/rev3.json b/tests/cases/EventDispatcher/simple/rev3.json new file mode 100644 index 000000000..8f6ca6d2e --- /dev/null +++ b/tests/cases/EventDispatcher/simple/rev3.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/simple/rev3.txt b/tests/cases/EventDispatcher/simple/rev3.txt new file mode 100644 index 000000000..f27e14cdd --- /dev/null +++ b/tests/cases/EventDispatcher/simple/rev3.txt @@ -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) diff --git a/tests/cases/EventDispatcher/simple/rev4.json b/tests/cases/EventDispatcher/simple/rev4.json new file mode 100644 index 000000000..0391beb30 --- /dev/null +++ b/tests/cases/EventDispatcher/simple/rev4.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/simple/rev4.txt b/tests/cases/EventDispatcher/simple/rev4.txt new file mode 100644 index 000000000..2d59f27c4 --- /dev/null +++ b/tests/cases/EventDispatcher/simple/rev4.txt @@ -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) diff --git a/tests/cases/EventDispatcher/simple/rev5.json b/tests/cases/EventDispatcher/simple/rev5.json new file mode 100644 index 000000000..9d137f4a6 --- /dev/null +++ b/tests/cases/EventDispatcher/simple/rev5.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/simple/rev5.txt b/tests/cases/EventDispatcher/simple/rev5.txt new file mode 100644 index 000000000..ad03ec181 --- /dev/null +++ b/tests/cases/EventDispatcher/simple/rev5.txt @@ -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) diff --git a/tests/cases/EventDispatcher/subsection-empty/rev1.txt b/tests/cases/EventDispatcher/subsection-empty/rev1.txt new file mode 100644 index 000000000..ce4ffbfd9 --- /dev/null +++ b/tests/cases/EventDispatcher/subsection-empty/rev1.txt @@ -0,0 +1,6 @@ +== A == + +=== B === +C. --[[User:Y]] 00:01, 1 January 2020 (UTC) + +=== D === diff --git a/tests/cases/EventDispatcher/subsection-empty/rev2.json b/tests/cases/EventDispatcher/subsection-empty/rev2.json new file mode 100644 index 000000000..5be96b5da --- /dev/null +++ b/tests/cases/EventDispatcher/subsection-empty/rev2.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/subsection-empty/rev2.txt b/tests/cases/EventDispatcher/subsection-empty/rev2.txt new file mode 100644 index 000000000..043239234 --- /dev/null +++ b/tests/cases/EventDispatcher/subsection-empty/rev2.txt @@ -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 === diff --git a/tests/cases/EventDispatcher/subsection-empty/rev3.json b/tests/cases/EventDispatcher/subsection-empty/rev3.json new file mode 100644 index 000000000..0a0c86303 --- /dev/null +++ b/tests/cases/EventDispatcher/subsection-empty/rev3.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/subsection-empty/rev3.txt b/tests/cases/EventDispatcher/subsection-empty/rev3.txt new file mode 100644 index 000000000..541903dc6 --- /dev/null +++ b/tests/cases/EventDispatcher/subsection-empty/rev3.txt @@ -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) diff --git a/tests/cases/EventDispatcher/subsection/rev1.txt b/tests/cases/EventDispatcher/subsection/rev1.txt new file mode 100644 index 000000000..9aab59911 --- /dev/null +++ b/tests/cases/EventDispatcher/subsection/rev1.txt @@ -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 === diff --git a/tests/cases/EventDispatcher/subsection/rev2.json b/tests/cases/EventDispatcher/subsection/rev2.json new file mode 100644 index 000000000..4cf0c11e1 --- /dev/null +++ b/tests/cases/EventDispatcher/subsection/rev2.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/subsection/rev2.txt b/tests/cases/EventDispatcher/subsection/rev2.txt new file mode 100644 index 000000000..1a062f7d2 --- /dev/null +++ b/tests/cases/EventDispatcher/subsection/rev2.txt @@ -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 === diff --git a/tests/cases/EventDispatcher/subsection/rev3.json b/tests/cases/EventDispatcher/subsection/rev3.json new file mode 100644 index 000000000..f3833691d --- /dev/null +++ b/tests/cases/EventDispatcher/subsection/rev3.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/subsection/rev3.txt b/tests/cases/EventDispatcher/subsection/rev3.txt new file mode 100644 index 000000000..7d7f58c3a --- /dev/null +++ b/tests/cases/EventDispatcher/subsection/rev3.txt @@ -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 === diff --git a/tests/cases/EventDispatcher/subsection/rev4.json b/tests/cases/EventDispatcher/subsection/rev4.json new file mode 100644 index 000000000..5ae901547 --- /dev/null +++ b/tests/cases/EventDispatcher/subsection/rev4.json @@ -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": {} + } +] diff --git a/tests/cases/EventDispatcher/subsection/rev4.txt b/tests/cases/EventDispatcher/subsection/rev4.txt new file mode 100644 index 000000000..ec9d242b0 --- /dev/null +++ b/tests/cases/EventDispatcher/subsection/rev4.txt @@ -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) diff --git a/tests/phpunit/EventDispatcherTest.php b/tests/phpunit/EventDispatcherTest.php new file mode 100644 index 000000000..97836860d --- /dev/null +++ b/tests/phpunit/EventDispatcherTest.php @@ -0,0 +1,182 @@ +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', + ], + ]; + } + +} diff --git a/tests/phpunit/MockEventDispatcher.php b/tests/phpunit/MockEventDispatcher.php new file mode 100644 index 000000000..ab024fccc --- /dev/null +++ b/tests/phpunit/MockEventDispatcher.php @@ -0,0 +1,45 @@ +