Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
run:
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@2.x
with:
enable_backend_testing: false
enable_backend_testing: true
enable_phpstan: true

backend_directory: .
24 changes: 21 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,35 @@
}
},
"require-dev": {
"flarum/audit": "2.x-dev",
"flarum/phpstan": "^2.0.0",
"flarum/testing": "^2.0.0",
"flarum/tags": "*",
"flarum/flags": "*"
"flarum/flags": "*",
"flarum/realtime": "*"
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
"clear-cache:phpstan": "phpstan clear-result-cache"
"clear-cache:phpstan": "phpstan clear-result-cache",
"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": {
"analyse:phpstan": "Run static analysis"
"analyse:phpstan": "Run static analysis",
"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."
},
"autoload-dev": {
"psr-4": {
"FoF\\Split\\Tests\\": "tests/"
}
},
"minimum-stability": "beta",
"prefer-stable": true
Expand Down
23 changes: 23 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Flarum\Api\Context;
use Flarum\Api\Resource;
use Flarum\Api\Schema;
use Flarum\Audit\AuditLogger;
use Flarum\Audit\Extend\Audit;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Renamed;
use Flarum\Extend;
Expand Down Expand Up @@ -46,4 +48,25 @@
Schema\Boolean::make('canSplit')
->get(fn (Discussion $discussion, Context $context) => $context->getActor()->can('split', $discussion)),
]),

(new Extend\Conditional())
->whenExtensionEnabled('flarum-audit', fn () => [
(new Audit())
->group('fof-split')
->register('discussion.split_away', 'discussion.split_into')
->using(function () {
resolve('events')->listen(DiscussionWasSplit::class, function (DiscussionWasSplit $event) {
AuditLogger::log('discussion.split_away', [
'discussion_id' => $event->originalDiscussion->id,
'new_discussion_id' => $event->newDiscussion->id,
'post_count' => $event->posts->count(),
]);
AuditLogger::log('discussion.split_into', [
'discussion_id' => $event->newDiscussion->id,
'original_discussion_id' => $event->originalDiscussion->id,
'post_count' => $event->posts->count(),
]);
});
}),
]),
];
10 changes: 10 additions & 0 deletions locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ fof-split:
title: Split discussion
new_discussion_label: Specify the title for the new discussion
submit_button: Split

# Labels for the audit log entries this extension produces, rendered by the (optional)
# flarum/audit browser. Defined under the flarum-audit namespace so the audit frontend
# resolves them, but shipped here so they travel with the extension that owns the actions.
flarum-audit:
lib:
browser:
discussion:
split_away: Split {post_count} posts from {discussion} into {new_discussion}
split_into: Split {post_count} posts into new discussion {discussion}
Empty file added tests/fixtures/.gitkeep
Empty file.
92 changes: 92 additions & 0 deletions tests/integration/AuditTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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;

use Carbon\Carbon;
use Flarum\Audit\AuditLog;
use Flarum\Audit\AuditLogger;
use Flarum\Discussion\Discussion;
use Flarum\Post\Post;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use Illuminate\Support\Arr;
use PHPUnit\Framework\Attributes\Test;

class AuditTest extends TestCase
{
use RetrievesAuthorizedUsers;

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

// Lifecycle events fired outside the test transaction shouldn't create stray entries.
AuditLogger::$testMode = true;

$this->extension('flarum-audit', 'fof-split');

$date = Carbon::parse('2021-01-01T12:00:00+00:00');

$this->prepareDatabase([
'audit_log' => [],
Discussion::class => [
['id' => 10, 'title' => 'A', 'created_at' => $date, 'last_posted_at' => $date, 'first_post_id' => 1, 'comment_count' => 4],
],
Post::class => [
['id' => 1, 'number' => 1, 'discussion_id' => 10, 'created_at' => $date, 'type' => 'comment', 'content' => '<t><p>A</p></t>'],
['id' => 2, 'number' => 2, 'discussion_id' => 10, 'created_at' => $date, 'type' => 'comment', 'content' => '<t><p>B</p></t>', 'user_id' => 1],
['id' => 3, 'number' => 3, 'discussion_id' => 10, 'created_at' => $date, 'type' => 'comment', 'content' => '<t><p>C</p></t>'],
['id' => 4, 'number' => 4, 'discussion_id' => 10, 'created_at' => $date, 'type' => 'comment', 'content' => '<t><p>D</p></t>'],
],
]);
}

#[Test]
public function split()
{
$response = $this->send($this->request('POST', '/api/split', [
'authenticatedAs' => 1,
'json' => [
'title' => 'Split',
'start_post_id' => 2,
'end_post_number' => 3,
],
]));

$body = $response->getBody()->getContents();

$this->assertEquals(200, $response->getStatusCode(), $body);

$newDiscussionId = Arr::get(json_decode($body, true), 'data.id');

$log = AuditLog::query()->where('action', 'discussion.split_away')->first();
$this->assertNotNull($log);
$this->assertEquals(1, $log->actor_id);
$this->assertEquals([
'discussion_id' => 10,
'new_discussion_id' => $newDiscussionId,
'post_count' => 2,
], $log->payload);
$this->assertEquals('127.0.0.1', $log->ip_address);

$log = AuditLog::query()->where('action', 'discussion.split_into')->first();
$this->assertNotNull($log);
$this->assertEquals(1, $log->actor_id);
$this->assertEquals([
'discussion_id' => $newDiscussionId,
'original_discussion_id' => 10,
'post_count' => 2,
], $log->payload);
$this->assertEquals('127.0.0.1', $log->ip_address);
}
}
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();
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.
Loading