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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
}
},
"require-dev": {
"flarum/audit": "2.x-dev",
"flarum/bbcode": "^2.0.0",
"flarum/testing": "^2.0.0",
"flarum/phpstan": "^2.0.0",
Expand Down
8 changes: 8 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Flarum\Settings\Event\Saved;
use Flarum\User\Event\Saving;
use Flarum\User\User;
use FoF\UserBio\Event\BioChanged;

return [
(new Flarum\Frontend('forum'))
Expand Down Expand Up @@ -56,4 +57,11 @@
(new \Flarum\Gdpr\Extend\UserData())
->addType(Data\UserBioData::class),
]),

(new Flarum\Conditional())
->whenExtensionEnabled('flarum-audit', fn () => [
(new \Flarum\Audit\Extend\Audit())
->group('fof-user-bio')
->listen(BioChanged::class, 'user.bio_changed', fn ($e) => ['user_id' => $e->user->id]),
]),
];
9 changes: 9 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ fof-user-bio:
cancel_button: Cancel
save_button: Save
edit_bio_label: Edit bio

# 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:
user:
bio_changed: Edited {username}'s bio
68 changes: 68 additions & 0 deletions tests/integration/AuditTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of fof/user-bio.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\UserBio\Tests\integration;

use Flarum\Audit\AuditLog;
use Flarum\Audit\AuditLogger;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;
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-user-bio');

$this->prepareDatabase([
'audit_log' => [],
User::class => [
[
'id' => 3,
'username' => 'user3',
'email' => 'user3@example.com',
],
],
]);
}

#[Test]
public function update()
{
$response = $this->send($this->request('PATCH', '/api/users/3', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'attributes' => [
'bio' => 'Hello World',
],
],
],
]));

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

$log = AuditLog::query()->where('action', 'user.bio_changed')->first();
$this->assertNotNull($log);
$this->assertEquals(1, $log->actor_id);
$this->assertEquals(['user_id' => 3], $log->payload);
$this->assertEquals('127.0.0.1', $log->ip_address);
}
}
Loading