Skip to content

Commit

Permalink
fix(subscriptions): reply notifications not working (#3445)
Browse files Browse the repository at this point in the history
* chore: subscriptions backend test infrastructure

* test: reply notification

* fix: fix reply notifications not working
  • Loading branch information
SychO9 committed Jun 3, 2022
1 parent b30c5a9 commit bcd702c
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 3 deletions.
27 changes: 25 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"prettier": true,
"typescript": false,
"bundlewatch": false,
"backendTesting": false,
"backendTesting": true,
"editorConfig": true,
"styleci": true
}
Expand All @@ -64,5 +64,28 @@
}
],
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"autoload-dev": {
"psr-4": {
"Flarum\\Subscriptions\\Tests\\": "tests/"
}
},
"scripts": {
"test": [
"@test:unit",
"@test:integration"
],
"test:unit": "phpunit -c tests/phpunit.unit.xml",
"test:integration": "phpunit -c tests/phpunit.integration.xml",
"test:setup": "@php tests/integration/setup.php"
},
"scripts-descriptions": {
"test": "Runs all tests.",
"test:unit": "Runs all unit tests.",
"test:integration": "Runs all integration tests.",
"test:setup": "Sets up a database for use with integration tests. Execute this only once."
},
"require-dev": {
"flarum/testing": "^1.0.0"
}
}
2 changes: 1 addition & 1 deletion src/Job/SendReplyNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function handle(NotificationSyncer $notifications)
$notify = $discussion->readers()
->where('users.id', '!=', $post->user_id)
->where('discussion_user.subscription', 'follow')
->where('discussion_user.last_read_post_number', $this->lastPostNumber)
->where('discussion_user.last_read_post_number', $this->lastPostNumber - 1)
->get();

$notifications->sync(
Expand Down
Empty file added tests/fixtures/.gitkeep
Empty file.
142 changes: 142 additions & 0 deletions tests/integration/api/discussions/ReplyNotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Subscriptions\tests\integration\api\discussions;

use Carbon\Carbon;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;

class ReplyNotificationTest extends TestCase
{
use RetrievesAuthorizedUsers;

protected function setUp(): void
{
parent::setUp();

$this->extension('flarum-subscriptions');

$this->prepareDatabase([
'users' => [
$this->normalUser(),
],
'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],
],
'posts' => [
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 1],
['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 1],
],
'discussion_user' => [
['discussion_id' => 1, 'user_id' => 1, 'last_read_post_number' => 1, 'subscription' => 'follow'],
['discussion_id' => 2, 'user_id' => 1, 'last_read_post_number' => 1, 'subscription' => 'follow'],
]
]);
}

/** @test */
public function replying_to_a_discussion_with_comment_post_as_last_post_sends_reply_notification()
{
$this->app();

/** @var User $mainUser */
$mainUser = User::query()->find(1);

$this->assertEquals(0, $this->getUnreadNotificationCount($mainUser));

$this->send(
$this->request('POST', '/api/posts', [
'authenticatedAs' => 2,
'json' => [
'data' => [
'attributes' => [
'content' => 'reply with predetermined content for automated testing - too-obscure',
],
'relationships' => [
'discussion' => ['data' => ['id' => 1]],
],
],
],
])
);

$this->assertEquals(1, $this->getUnreadNotificationCount($mainUser));
}

/** @test */
public function replying_to_a_discussion_with_event_post_as_last_post_sends_reply_notification()
{
$this->app();

/** @var User $mainUser */
$mainUser = User::query()->find(1);

// Rename the discussion to trigger an event post.
$this->send(
$this->request('POST', '/api/discussions/2', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'attributes' => [
'title' => 'ACME',
],
],
],
])
);

// Mark as read
$this->send(
$this->request('POST', '/api/discussions/2', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'attributes' => [
'lastReadPostNumber' => 2,
],
],
],
])
);

$this->assertEquals(0, $this->getUnreadNotificationCount($mainUser));

$this->send(
$this->request('POST', '/api/posts', [
'authenticatedAs' => 2,
'json' => [
'data' => [
'attributes' => [
'content' => 'reply with predetermined content for automated testing - too-obscure',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
],
],
],
])
);

$this->assertEquals(1, $this->getUnreadNotificationCount($mainUser));
}

/** @todo change after core no longer statically caches unread notification in the User class */
protected function getUnreadNotificationCount(User $user)
{
return $user->notifications()
->where('type', 'newPost')
->whereNull('read_at')
->where('is_deleted', false)
->whereSubjectVisibleTo($user)
->count();
}
}
16 changes: 16 additions & 0 deletions tests/integration/setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

use Flarum\Testing\integration\Setup\SetupScript;

require __DIR__.'/../../vendor/autoload.php';

$setup = new SetupScript();

$setup->run();
25 changes: 25 additions & 0 deletions tests/phpunit.integration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
stopOnFailure="false"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">../src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Flarum Integration Tests">
<directory suffix="Test.php">./integration</directory>
<exclude>./integration/tmp</exclude>
</testsuite>
</testsuites>
</phpunit>
27 changes: 27 additions & 0 deletions tests/phpunit.unit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">../src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Flarum Unit Tests">
<directory suffix="Test.php">./unit</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener" />
</listeners>
</phpunit>
Empty file added tests/unit/.gitkeep
Empty file.

0 comments on commit bcd702c

Please sign in to comment.