Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions composer.json

Copy link
Copy Markdown
Member

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

Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,24 @@
"FoF\\Split\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"FoF\\Split\\Tests\\": "tests/"
}
},
"require-dev": {
"flarum/testing": "^1.0.0",
"flarum/phpstan": "*"
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
"clear-cache:phpstan": "phpstan clear-result-cache"
"clear-cache:phpstan": "phpstan clear-result-cache",
"test:integration": "phpunit -c tests/phpunit.integration.xml",
"test:setup": "@php tests/integration/setup.php"
},
"scripts-descriptions": {
"analyse:phpstan": "Run static analysis"
"analyse:phpstan": "Run static analysis",
"test:integration": "Runs integration tests.",
"test:setup": "Sets up a database for use with integration tests. Execute this only once."
}
}
16 changes: 15 additions & 1 deletion src/Api/Commands/SplitDiscussionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace FoF\Split\Api\Commands;

use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Started;
use Flarum\Post\Post;
use Flarum\Post\PostRepository;
use Flarum\Settings\SettingsRepositoryInterface;
Expand Down Expand Up @@ -94,7 +95,7 @@ public function handle(SplitDiscussion $command)
$originalDiscussion = $startPost->discussion;

// create a new discussion for the user of the first splitted reply.
$discussion = Discussion::start($command->title, $startPost->user);
$discussion = $this->createDiscussionFromStartPost($command, $startPost);
$discussion->setFirstPost($startPost);

// persist the new discussion.
Expand All @@ -121,6 +122,19 @@ public function handle(SplitDiscussion $command)
return $discussion;
}

protected function createDiscussionFromStartPost(SplitDiscussion $command, Post $startPost): Discussion
{
if ($startPost->user) {
return Discussion::start($command->title, $startPost->user);
}

$discussion = new Discussion();
$discussion->title = $command->title;
$discussion->raise(new Started($discussion));

return $discussion;
}

/**
* Assign the specific range to a new Discussion without resetting post numbers.
*
Expand Down
1 change: 1 addition & 0 deletions tests/.phpunit.result.cache

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please gitignore this

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}}
217 changes: 217 additions & 0 deletions tests/integration/api/SplitDiscussionTest.php
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));
}
}
19 changes: 19 additions & 0 deletions tests/integration/setup.php
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();
24 changes: 24 additions & 0 deletions tests/phpunit.integration.xml

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also use the CLI here so that we have this consistent

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>
Loading