-
-
Notifications
You must be signed in to change notification settings - Fork 16
fix: discussion split when user deleted #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grimur82
wants to merge
2
commits into
FriendsOfFlarum:1.x
Choose a base branch
from
grimur82:gvn/fix_discussion_split_when_user_deleted
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+288
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please gitignore this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"version":1,"defects":{"FoF\\Split\\Tests\\integration\\api\\SplitDiscussionTest::it_can_split_posts_when_the_selected_start_post_author_has_been_deleted":3,"FoF\\Split\\Tests\\integration\\api\\SplitDiscussionTest::it_still_uses_the_normal_creation_path_when_the_start_post_author_exists":3,"FoF\\Split\\Tests\\integration\\api\\SplitDiscussionTest::it_refreshes_original_discussion_metadata_and_creates_split_event_posts":3},"times":{"FoF\\Split\\Tests\\integration\\api\\SplitDiscussionTest::it_can_split_posts_when_the_selected_start_post_author_has_been_deleted":0.529,"FoF\\Split\\Tests\\integration\\api\\SplitDiscussionTest::it_still_uses_the_normal_creation_path_when_the_start_post_author_exists":0.44,"FoF\\Split\\Tests\\integration\\api\\SplitDiscussionTest::it_refreshes_original_discussion_metadata_and_creates_split_event_posts":0.436}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of fof/split. | ||
| * | ||
| * Copyright (c) Flagrow. | ||
| * Copyright (c) 2020 FriendsOfFlarum | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE.md | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace FoF\Split\Tests\integration\api; | ||
|
|
||
| use Flarum\Testing\integration\TestCase; | ||
|
|
||
| class SplitDiscussionTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->extension('fof-split'); | ||
|
|
||
| $this->prepareDatabase([ | ||
| 'discussions' => [ | ||
| [ | ||
| 'id' => 1, | ||
| 'title' => 'Original Discussion', | ||
| 'slug' => 'original-discussion', | ||
| 'comment_count' => 5, | ||
| 'participant_count' => 1, | ||
| 'created_at' => '2024-01-01 00:00:00', | ||
| 'user_id' => 1, | ||
| 'first_post_id' => 1, | ||
| 'last_posted_at' => '2024-01-01 00:04:00', | ||
| 'last_posted_user_id' => 1, | ||
| 'last_post_id' => 5, | ||
| 'last_post_number' => 5, | ||
| ], | ||
| ], | ||
| 'posts' => [ | ||
| [ | ||
| 'id' => 1, | ||
| 'discussion_id' => 1, | ||
| 'number' => 1, | ||
| 'created_at' => '2024-01-01 00:00:00', | ||
| 'user_id' => 1, | ||
| 'type' => 'comment', | ||
| 'content' => '<p>Opening post</p>', | ||
| 'ip_address' => '127.0.0.1', | ||
| 'is_private' => 0, | ||
| ], | ||
| [ | ||
| 'id' => 2, | ||
| 'discussion_id' => 1, | ||
| 'number' => 2, | ||
| 'created_at' => '2024-01-01 00:01:00', | ||
| 'user_id' => null, | ||
| 'type' => 'comment', | ||
| 'content' => '<p>Deleted author reply</p>', | ||
| 'ip_address' => '127.0.0.1', | ||
| 'is_private' => 0, | ||
| ], | ||
| [ | ||
| 'id' => 3, | ||
| 'discussion_id' => 1, | ||
| 'number' => 3, | ||
| 'created_at' => '2024-01-01 00:02:00', | ||
| 'user_id' => null, | ||
| 'type' => 'comment', | ||
| 'content' => '<p>Deleted author follow-up</p>', | ||
| 'ip_address' => '127.0.0.1', | ||
| 'is_private' => 0, | ||
| ], | ||
| [ | ||
| 'id' => 4, | ||
| 'discussion_id' => 1, | ||
| 'number' => 4, | ||
| 'created_at' => '2024-01-01 00:03:00', | ||
| 'user_id' => 1, | ||
| 'type' => 'comment', | ||
| 'content' => '<p>Existing author reply</p>', | ||
| 'ip_address' => '127.0.0.1', | ||
| 'is_private' => 0, | ||
| ], | ||
| [ | ||
| 'id' => 5, | ||
| 'discussion_id' => 1, | ||
| 'number' => 5, | ||
| 'created_at' => '2024-01-01 00:04:00', | ||
| 'user_id' => 1, | ||
| 'type' => 'comment', | ||
| 'content' => '<p>Existing author follow-up</p>', | ||
| 'ip_address' => '127.0.0.1', | ||
| 'is_private' => 0, | ||
| ], | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_can_split_posts_when_the_selected_start_post_author_has_been_deleted(): void | ||
| { | ||
| $response = $this->sendSplitRequest('Split Discussion', 2, 3); | ||
|
|
||
| $this->assertSame(200, $response->getStatusCode()); | ||
|
|
||
| $payload = json_decode((string) $response->getBody(), true); | ||
|
|
||
| $this->assertIsArray($payload); | ||
| $this->assertSame('Split Discussion', $payload['data']['attributes']['title']); | ||
|
|
||
| $newDiscussionId = (int) $payload['data']['id']; | ||
|
|
||
| $discussion = $this->database()->table('discussions')->where('id', $newDiscussionId)->first(); | ||
|
|
||
| $this->assertNotNull($discussion); | ||
| $this->assertSame(2, $discussion->first_post_id); | ||
| $this->assertNull($discussion->user_id); | ||
| $this->assertSame('2024-01-01 00:01:00', $discussion->created_at); | ||
|
|
||
| $splitPosts = $this->database()->table('posts') | ||
| ->where('discussion_id', $newDiscussionId) | ||
| ->where('type', 'comment') | ||
| ->orderBy('number') | ||
| ->pluck('id') | ||
| ->all(); | ||
|
|
||
| $this->assertSame([2, 3], $splitPosts); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_still_uses_the_normal_creation_path_when_the_start_post_author_exists(): void | ||
| { | ||
| $response = $this->sendSplitRequest('Normal Split Discussion', 4, 5); | ||
|
|
||
| $this->assertSame(200, $response->getStatusCode()); | ||
|
|
||
| $payload = json_decode((string) $response->getBody(), true); | ||
|
|
||
| $this->assertIsArray($payload); | ||
|
|
||
| $newDiscussionId = (int) $payload['data']['id']; | ||
|
|
||
| $discussion = $this->database()->table('discussions')->where('id', $newDiscussionId)->first(); | ||
|
|
||
| $this->assertNotNull($discussion); | ||
| $this->assertSame(4, $discussion->first_post_id); | ||
| $this->assertSame(1, $discussion->user_id); | ||
| $this->assertSame('2024-01-01 00:03:00', $discussion->created_at); | ||
|
|
||
| $renumberedPosts = $this->database()->table('posts') | ||
| ->where('discussion_id', $newDiscussionId) | ||
| ->where('type', 'comment') | ||
| ->orderBy('id') | ||
| ->pluck('number') | ||
| ->all(); | ||
|
|
||
| $this->assertSame([1, 2], $renumberedPosts); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_refreshes_original_discussion_metadata_and_creates_split_event_posts(): void | ||
| { | ||
| $response = $this->sendSplitRequest('Split Discussion', 2, 3); | ||
|
|
||
| $this->assertSame(200, $response->getStatusCode()); | ||
|
|
||
| $payload = json_decode((string) $response->getBody(), true); | ||
|
|
||
| $this->assertIsArray($payload); | ||
|
|
||
| $newDiscussionId = (int) $payload['data']['id']; | ||
|
|
||
| $originalDiscussion = $this->database()->table('discussions')->where('id', 1)->first(); | ||
|
|
||
| $this->assertNotNull($originalDiscussion); | ||
| $this->assertSame(3, $originalDiscussion->comment_count); | ||
| $this->assertSame(1, $originalDiscussion->participant_count); | ||
| $this->assertSame(5, $originalDiscussion->last_post_id); | ||
| $this->assertSame(5, $originalDiscussion->last_post_number); | ||
|
|
||
| $originalSplitPosts = $this->database()->table('posts') | ||
| ->where('discussion_id', 1) | ||
| ->where('type', 'discussionSplit') | ||
| ->count(); | ||
|
|
||
| $newSplitPosts = $this->database()->table('posts') | ||
| ->where('discussion_id', $newDiscussionId) | ||
| ->where('type', 'discussionSplit') | ||
| ->count(); | ||
|
|
||
| $this->assertSame(1, $originalSplitPosts); | ||
| $this->assertSame(1, $newSplitPosts); | ||
| } | ||
|
|
||
| protected function sendSplitRequest(string $title, int $startPostId, int $endPostNumber) | ||
| { | ||
| $request = $this->request('POST', '/api/split', [ | ||
| 'authenticatedAs' => 1, | ||
| 'json' => [ | ||
| 'title' => $title, | ||
| 'start_post_id' => $startPostId, | ||
| 'end_post_number' => $endPostNumber, | ||
| ], | ||
| ]); | ||
|
|
||
| return $this->send($this->requestWithCsrfToken($request)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of fof/split. | ||
| * | ||
| * Copyright (c) Flagrow. | ||
| * Copyright (c) 2020 FriendsOfFlarum | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE.md | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| use Flarum\Testing\integration\Setup\SetupScript; | ||
|
|
||
| require __DIR__.'/../../vendor/autoload.php'; | ||
|
|
||
| $setup = new SetupScript(); | ||
|
|
||
| $setup->run(); |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also use the CLI here so that we have this consistent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?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> | ||
| </testsuite> | ||
| </testsuites> | ||
| </phpunit> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just so that we have a consistent setup across the board, could you please use the same composer.json setup for tests like in other extensions? Iirc the Flarum CLI adds it as desired