From a5982e49c77d547ea23529fd1b002d815a172917 Mon Sep 17 00:00:00 2001 From: Tristian Kelly Date: Thu, 23 Feb 2023 15:45:09 -0600 Subject: [PATCH 1/7] Add notifications integration test --- .../integration/api/NotificationsTest.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 extensions/mentions/tests/integration/api/NotificationsTest.php diff --git a/extensions/mentions/tests/integration/api/NotificationsTest.php b/extensions/mentions/tests/integration/api/NotificationsTest.php new file mode 100644 index 0000000000..f5147c3531 --- /dev/null +++ b/extensions/mentions/tests/integration/api/NotificationsTest.php @@ -0,0 +1,112 @@ +extension('flarum-mentions'); + + $this->prepareDatabase([ + 'users' => [ + $this->normalUser(), + ['id' => 3, 'username' => 'acme', 'email' => 'acme@machine.local', 'is_email_confirmed' => 1, 'preferences' => json_encode(['flarum-subscriptions.notify_for_all_posts' => true])], + ['id' => 4, 'username' => 'acme2', 'email' => 'acme2@machine.local', 'is_email_confirmed' => 1], + ], + 'discussions' => [ + ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1, 'last_post_number' => 1, 'last_post_id' => 1], + ['id' => 2, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 2, 'comment_count' => 1, 'last_post_number' => 1, 'last_post_id' => 2], + + ['id' => 33, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 33, 'comment_count' => 6, 'last_post_number' => 6, 'last_post_id' => 38], + ], + 'posts' => [ + ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 1], + ['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 1], + + ['id' => 33, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 1], + ['id' => 34, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 2], + ['id' => 35, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 3], + ['id' => 36, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 4], + ['id' => 37, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 5], + ['id' => 38, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 6], + ], + 'discussion_user' => [ + ['discussion_id' => 1, 'user_id' => 1, 'last_read_post_number' => 1, 'subscription' => 'follow'], + ['discussion_id' => 1, 'user_id' => 2, 'last_read_post_number' => 1, 'subscription' => 'follow'], + ['discussion_id' => 2, 'user_id' => 1, 'last_read_post_number' => 1, 'subscription' => 'follow'], + + ['discussion_id' => 33, 'user_id' => 2, 'last_read_post_number' => 1, 'subscription' => 'follow'], + ['discussion_id' => 33, 'user_id' => 3, 'last_read_post_number' => 1, 'subscription' => 'follow'], + ] + ]); + } + + /** @test */ + public function approving_reply_sends_mention_notification() + { + $this->extensions = ['flarum-flags', 'flarum-approval', 'flarum-mentions']; + + $this->app(); + + $this->database() + ->table('group_permission') + ->where('group_id', Group::MEMBER_ID) + ->where('permission', 'discussion.replyWithoutApproval') + ->delete(); + + /** @var User $mainUser */ + $mainUser = User::query()->find(2); + + $this->assertEquals(0, $mainUser->getUnreadNotificationCount()); + + $response = $this->send( + $this->request('POST', '/api/posts', [ + 'authenticatedAs' => 4, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'content' => 'reply with predetermined content for automated testing - too-obscure', + ], + 'relationships' => [ + 'discussion' => ['data' => ['id' => 1]], + ], + ] + ] + ]) + ); + + $this->assertEquals(0, $mainUser->getUnreadNotificationCount()); + + $json = json_decode($response->getBody()->getContents(), true); + + $this->send( + $this->request('PATCH', '/api/posts'.$json['data']['id'], [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'isApproved' => 1, + ] + ] + ] + ]) + ); + } +} \ No newline at end of file From 03ba9f854ccc65df745b1e777a53b4f66fe7e970 Mon Sep 17 00:00:00 2001 From: Tristian Kelly Date: Tue, 7 Mar 2023 10:59:27 -0600 Subject: [PATCH 2/7] Add testing dependencies --- extensions/mentions/composer.json | 2 ++ .../mentions/tests/integration/api/NotificationsTest.php | 8 -------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/extensions/mentions/composer.json b/extensions/mentions/composer.json index 946c7c9f7b..9182a9fa5d 100644 --- a/extensions/mentions/composer.json +++ b/extensions/mentions/composer.json @@ -78,6 +78,8 @@ "require-dev": { "flarum/core": "*@dev", "flarum/tags": "*@dev", + "flarum/approval": "*@dev", + "flarum/flags": "*@dev", "flarum/testing": "^2.0" }, "repositories": [ diff --git a/extensions/mentions/tests/integration/api/NotificationsTest.php b/extensions/mentions/tests/integration/api/NotificationsTest.php index f5147c3531..ad8ed2a3b2 100644 --- a/extensions/mentions/tests/integration/api/NotificationsTest.php +++ b/extensions/mentions/tests/integration/api/NotificationsTest.php @@ -47,14 +47,6 @@ protected function setUp(): void ['id' => 37, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 5], ['id' => 38, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 6], ], - 'discussion_user' => [ - ['discussion_id' => 1, 'user_id' => 1, 'last_read_post_number' => 1, 'subscription' => 'follow'], - ['discussion_id' => 1, 'user_id' => 2, 'last_read_post_number' => 1, 'subscription' => 'follow'], - ['discussion_id' => 2, 'user_id' => 1, 'last_read_post_number' => 1, 'subscription' => 'follow'], - - ['discussion_id' => 33, 'user_id' => 2, 'last_read_post_number' => 1, 'subscription' => 'follow'], - ['discussion_id' => 33, 'user_id' => 3, 'last_read_post_number' => 1, 'subscription' => 'follow'], - ] ]); } From 005038423cb296f8cc48fc57022ca89333e30984 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sun, 16 Apr 2023 21:24:07 +0100 Subject: [PATCH 3/7] chore: apply styleci fixes Signed-off-by: Sami Mazouz --- .../mentions/tests/integration/api/NotificationsTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extensions/mentions/tests/integration/api/NotificationsTest.php b/extensions/mentions/tests/integration/api/NotificationsTest.php index ad8ed2a3b2..d440db8438 100644 --- a/extensions/mentions/tests/integration/api/NotificationsTest.php +++ b/extensions/mentions/tests/integration/api/NotificationsTest.php @@ -9,10 +9,10 @@ namespace Flarum\Mentions\tests\integration\api\NotificationsTest; use Carbon\Carbon; +use Flarum\Group\Group; use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\TestCase; use Flarum\User\User; -use Flarum\Group\Group; class NotificationsTest extends TestCase { @@ -62,7 +62,7 @@ public function approving_reply_sends_mention_notification() ->where('group_id', Group::MEMBER_ID) ->where('permission', 'discussion.replyWithoutApproval') ->delete(); - + /** @var User $mainUser */ $mainUser = User::query()->find(2); @@ -101,4 +101,4 @@ public function approving_reply_sends_mention_notification() ]) ); } -} \ No newline at end of file +} From 4de70ca6b888e8b7e6f12f29d58c50e4d5f3454c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 16 Apr 2023 20:24:22 +0000 Subject: [PATCH 4/7] Apply fixes from StyleCI --- extensions/mentions/tests/integration/api/NotificationsTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/extensions/mentions/tests/integration/api/NotificationsTest.php b/extensions/mentions/tests/integration/api/NotificationsTest.php index d440db8438..dea843a37b 100644 --- a/extensions/mentions/tests/integration/api/NotificationsTest.php +++ b/extensions/mentions/tests/integration/api/NotificationsTest.php @@ -6,6 +6,7 @@ * For detailed copyright and license information, please view the * LICENSE file that was distributed with this source code. */ + namespace Flarum\Mentions\tests\integration\api\NotificationsTest; use Carbon\Carbon; From 245ebcabef025ffa1e649017b6573582c3ff33ea Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Mon, 24 Apr 2023 15:54:49 +0100 Subject: [PATCH 5/7] chore: continue Signed-off-by: Sami Mazouz --- .../integration/api/NotificationsTest.php | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/extensions/mentions/tests/integration/api/NotificationsTest.php b/extensions/mentions/tests/integration/api/NotificationsTest.php index dea843a37b..376fd5c48c 100644 --- a/extensions/mentions/tests/integration/api/NotificationsTest.php +++ b/extensions/mentions/tests/integration/api/NotificationsTest.php @@ -28,25 +28,17 @@ protected function setUp(): void $this->prepareDatabase([ 'users' => [ $this->normalUser(), - ['id' => 3, 'username' => 'acme', 'email' => 'acme@machine.local', 'is_email_confirmed' => 1, 'preferences' => json_encode(['flarum-subscriptions.notify_for_all_posts' => true])], - ['id' => 4, 'username' => 'acme2', 'email' => 'acme2@machine.local', 'is_email_confirmed' => 1], + ['id' => 3, 'username' => 'acme', 'email' => 'acme@machine.local', 'is_email_confirmed' => 1], ], 'discussions' => [ - ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1, 'last_post_number' => 1, 'last_post_id' => 1], - ['id' => 2, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 2, 'comment_count' => 1, 'last_post_number' => 1, 'last_post_id' => 2], - - ['id' => 33, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 33, 'comment_count' => 6, 'last_post_number' => 6, 'last_post_id' => 38], + ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1, 'last_post_number' => 2, 'last_post_id' => 2], ], 'posts' => [ ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 1], - ['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 1], - - ['id' => 33, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 1], - ['id' => 34, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 2], - ['id' => 35, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 3], - ['id' => 36, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 4], - ['id' => 37, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 5], - ['id' => 38, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 6], + ['id' => 2, 'discussion_id' => 1, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 3, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 2], + ], + 'group_user' => [ + ['group_id' => Group::MEMBER_ID, 'user_id' => 2], ], ]); } @@ -65,17 +57,17 @@ public function approving_reply_sends_mention_notification() ->delete(); /** @var User $mainUser */ - $mainUser = User::query()->find(2); + $mainUser = User::query()->find(3); $this->assertEquals(0, $mainUser->getUnreadNotificationCount()); $response = $this->send( $this->request('POST', '/api/posts', [ - 'authenticatedAs' => 4, + 'authenticatedAs' => 2, 'json' => [ 'data' => [ 'attributes' => [ - 'content' => 'reply with predetermined content for automated testing - too-obscure', + 'content' => '@"mainUser"#p2', ], 'relationships' => [ 'discussion' => ['data' => ['id' => 1]], @@ -90,7 +82,7 @@ public function approving_reply_sends_mention_notification() $json = json_decode($response->getBody()->getContents(), true); $this->send( - $this->request('PATCH', '/api/posts'.$json['data']['id'], [ + $this->request('PATCH', '/api/posts/'.$json['data']['id'], [ 'authenticatedAs' => 1, 'json' => [ 'data' => [ @@ -101,5 +93,7 @@ public function approving_reply_sends_mention_notification() ] ]) ); + + $this->assertEquals(1, $mainUser->getUnreadNotificationCount()); } } From 526d76b35e1d688e9e01577a5c6daae00cc302ae Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Tue, 22 Oct 2024 09:54:58 +0100 Subject: [PATCH 6/7] chore --- .../mentions/tests/integration/api/NotificationsTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extensions/mentions/tests/integration/api/NotificationsTest.php b/extensions/mentions/tests/integration/api/NotificationsTest.php index 376fd5c48c..33fcc92c34 100644 --- a/extensions/mentions/tests/integration/api/NotificationsTest.php +++ b/extensions/mentions/tests/integration/api/NotificationsTest.php @@ -14,6 +14,7 @@ use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\TestCase; use Flarum\User\User; +use PHPUnit\Framework\Attributes\Test; class NotificationsTest extends TestCase { @@ -43,7 +44,7 @@ protected function setUp(): void ]); } - /** @test */ + #[Test] public function approving_reply_sends_mention_notification() { $this->extensions = ['flarum-flags', 'flarum-approval', 'flarum-mentions']; From 6793abdfeb958911531416a6a7f7adbb0d219d2d Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Tue, 22 Oct 2024 09:58:04 +0100 Subject: [PATCH 7/7] chore --- .../mentions/tests/integration/api/NotificationsTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/extensions/mentions/tests/integration/api/NotificationsTest.php b/extensions/mentions/tests/integration/api/NotificationsTest.php index 33fcc92c34..95f17b5e23 100644 --- a/extensions/mentions/tests/integration/api/NotificationsTest.php +++ b/extensions/mentions/tests/integration/api/NotificationsTest.php @@ -10,7 +10,9 @@ namespace Flarum\Mentions\tests\integration\api\NotificationsTest; use Carbon\Carbon; +use Flarum\Discussion\Discussion; use Flarum\Group\Group; +use Flarum\Post\Post; use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\TestCase; use Flarum\User\User; @@ -27,14 +29,14 @@ protected function setUp(): void $this->extension('flarum-mentions'); $this->prepareDatabase([ - 'users' => [ + User::class => [ $this->normalUser(), ['id' => 3, 'username' => 'acme', 'email' => 'acme@machine.local', 'is_email_confirmed' => 1], ], - 'discussions' => [ + Discussion::class => [ ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1, 'last_post_number' => 2, 'last_post_id' => 2], ], - 'posts' => [ + Post::class => [ ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 1], ['id' => 2, 'discussion_id' => 1, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 3, 'type' => 'comment', 'content' => '

foo bar

', 'number' => 2], ],