From 9ac888a2dc21f32244c22d528667f55047db71c9 Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Tue, 3 Oct 2023 07:32:54 +0100 Subject: [PATCH 1/5] chore: add bbcode as a dev dep --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index ae8af9d..d7ab17c 100644 --- a/composer.json +++ b/composer.json @@ -56,5 +56,8 @@ "flagrow": { "discuss": "https://discuss.flarum.org/d/17775" } + }, + "require-dev": { + "flarum/bbcode": "*" } } From 0e9a5f56a7e26a37e31bf4824fe7da106b938484 Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Tue, 3 Oct 2023 07:33:21 +0100 Subject: [PATCH 2/5] fix: unable to provide a bio at the time of user creation --- src/Listeners/SaveUserBio.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Listeners/SaveUserBio.php b/src/Listeners/SaveUserBio.php index 2ec4d28..79d4898 100644 --- a/src/Listeners/SaveUserBio.php +++ b/src/Listeners/SaveUserBio.php @@ -71,8 +71,6 @@ public function handle(Saving $event) if ($user->bio != $user->getOriginal('bio')) { $user->raise(new BioChanged($user)); } - - $user->save(); } } } From 521dd29253b2e25128c364e1297467fd0699c90e Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Tue, 3 Oct 2023 08:04:11 +0100 Subject: [PATCH 3/5] chore: basic function tests, more still needed --- .github/workflows/backend.yml | 11 + .github/workflows/{build.yml => frontend.yml} | 5 +- .gitignore | 1 + composer.json | 43 +++- tests/fixtures/.gitkeep | 0 tests/integration/api/BioTest.php | 198 ++++++++++++++++++ tests/integration/setup.php | 16 ++ tests/phpunit.integration.xml | 25 +++ tests/phpunit.unit.xml | 27 +++ tests/unit/.gitkeep | 0 10 files changed, 316 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/backend.yml rename .github/workflows/{build.yml => frontend.yml} (88%) create mode 100644 tests/fixtures/.gitkeep create mode 100644 tests/integration/api/BioTest.php create mode 100644 tests/integration/setup.php create mode 100644 tests/phpunit.integration.xml create mode 100644 tests/phpunit.unit.xml create mode 100644 tests/unit/.gitkeep diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml new file mode 100644 index 0000000..6f8de9a --- /dev/null +++ b/.github/workflows/backend.yml @@ -0,0 +1,11 @@ +name: FoF User Bio PHP + +on: [workflow_dispatch, push, pull_request] + +jobs: + run: + uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main + with: + enable_backend_testing: true + + backend_directory: . diff --git a/.github/workflows/build.yml b/.github/workflows/frontend.yml similarity index 88% rename from .github/workflows/build.yml rename to .github/workflows/frontend.yml index ca3526b..5bc60f5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/frontend.yml @@ -1,4 +1,4 @@ -name: Javascript +name: FoF User Bio JS on: [workflow_dispatch, push, pull_request] @@ -8,11 +8,12 @@ jobs: with: enable_bundlewatch: false enable_prettier: true - enable_typescript: false + enable_typescript: true frontend_directory: ./js backend_directory: . js_package_manager: npm main_git_branch: master + secrets: bundlewatch_github_token: ${{ secrets.BUNDLEWATCH_GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 3d1cafa..5f77c4a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ js/node_modules vendor/ composer.lock js/dist +.phpunit.result.cache diff --git a/composer.json b/composer.json index d7ab17c..c4c0017 100644 --- a/composer.json +++ b/composer.json @@ -11,14 +11,14 @@ "issues": "https://github.com/FriendsOfFlarum/user-bio/issues", "source": "https://github.com/FriendsOfFlarum/user-bio", "forum": "https://discuss.flarum.org/d/17775" - }, - "homepage": "https://friendsofflarum.org", - "funding": [ + }, + "homepage": "https://friendsofflarum.org", + "funding": [ { - "type": "website", - "url": "https://opencollective.com/fof/donate" + "type": "website", + "url": "https://opencollective.com/fof/donate" } - ], + ], "require": { "flarum/core": "^1.2.0" }, @@ -31,7 +31,7 @@ }, { "name": "IanM", - "email": "ian@blomstra.net", + "email": "ian@flarum.org", "role": "Developer" } ], @@ -55,9 +55,36 @@ }, "flagrow": { "discuss": "https://discuss.flarum.org/d/17775" + }, + "flarum-cli": { + "modules": { + "backendTesting": true, + "githubActions": true + } } }, "require-dev": { - "flarum/bbcode": "*" + "flarum/bbcode": "*", + "flarum/testing": "^1.0.0" + }, + "autoload-dev": { + "psr-4": { + "FoF\\UserBio\\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." } } diff --git a/tests/fixtures/.gitkeep b/tests/fixtures/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/api/BioTest.php b/tests/integration/api/BioTest.php new file mode 100644 index 0000000..d90d7e6 --- /dev/null +++ b/tests/integration/api/BioTest.php @@ -0,0 +1,198 @@ +setting('mail_driver', 'log'); + + $this->extension('fof-user-bio'); + + $this->prepareDatabase([ + 'users' => [ + $this->normalUser(), + [ + 'id' => 3, + 'username' => 'normal2', + 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', // BCrypt hash for "too-obscure" + 'email' => 'normal2@machine.local', + 'is_email_confirmed' => 1, + 'last_seen_at' => Carbon::now()->subSecond(), + 'bio' => 'This is a test bio for normal2.' + ], + [ + 'id' => 4, + 'username' => 'normal3', + 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', // BCrypt hash for "too-obscure" + 'email' => 'normal3@machine.local', + 'is_email_confirmed' => 1, + 'last_seen_at' => Carbon::now()->subHour(), + ] + ], + ]); + } + + protected function giveNormalUsersEditOwnPerms() + { + $this->prepareDatabase([ + 'group_permission' => [ + ['permission' => 'fof-user-bio.editOwn', 'group_id' => 3], + ], + ]); + } + + protected function giveNormalUserViewBioPerms() + { + $this->prepareDatabase([ + 'group_permission' => [ + ['permission' => 'fof-user-bio.view', 'group_id' => 3], + ], + ]); + } + + /** + * @test + */ + public function admin_can_create_user_with_bio() + { + $response = $this->send( + $this->request( + 'POST', + '/api/users', + [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'username' => 'test', + 'password' => 'too-obscure', + 'email' => 'test@machine.local', + 'bio' => 'This is a test bio.' + ], + ] + ], + ] + ) + ); + + $this->assertEquals(201, $response->getStatusCode()); + + /** @var User $user */ + $user = User::where('username', 'test')->firstOrFail(); + + $this->assertEquals(0, $user->is_email_confirmed); + $this->assertEquals('test', $user->username); + $this->assertEquals('test@machine.local', $user->email); + $this->assertEquals('This is a test bio.', $user->bio); + } + + /** + * @test + */ + public function cannot_update_own_bio_when_permission_not_granted() + { + $response = $this->send( + $this->request( + 'PATCH', + '/api/users/2', + [ + 'authenticatedAs' => 2, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'bio' => 'This is a test bio.' + ], + ] + ], + ] + ) + ); + + $this->assertEquals(403, $response->getStatusCode()); + } + + /** + * @test + */ + public function can_update_own_user_with_added_bio_when_permission_granted() + { + $this->giveNormalUsersEditOwnPerms(); + + $response = $this->send( + $this->request( + 'PATCH', + '/api/users/2', + [ + 'authenticatedAs' => 2, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'bio' => 'This is a test bio.' + ], + ] + ], + ] + ) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + /** @var User $user */ + $user = User::where('id', 2)->firstOrFail(); + + $this->assertEquals('This is a test bio.', $user->bio); + } + + /** + * @test + */ + public function normal_user_cannot_see_bio_of_other_user_without_permission() + { + $response = $this->send( + $this->request( + 'GET', + '/api/users/3', + [ + 'authenticatedAs' => 2, + ] + ) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $this->assertArrayNotHasKey('bio', json_decode($response->getBody()->getContents(), true)['data']['attributes']); + } + + public function normal_user_can_see_others_bio_with_permission() + { + $this->giveNormalUserViewBioPerms(); + + $response = $this->send( + $this->request( + 'GET', + '/api/users/3', + [ + 'authenticatedAs' => 2, + ] + ) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $data = json_decode($response->getBody()->getContents(), true); + + $this->assertArrayHasKey('bio', $data['data']['attributes']); + $this->assertEquals('This is a test bio for normal2.', $data['data']['attributes']['bio']); + } +} diff --git a/tests/integration/setup.php b/tests/integration/setup.php new file mode 100644 index 0000000..67039c0 --- /dev/null +++ b/tests/integration/setup.php @@ -0,0 +1,16 @@ +run(); diff --git a/tests/phpunit.integration.xml b/tests/phpunit.integration.xml new file mode 100644 index 0000000..90fbbff --- /dev/null +++ b/tests/phpunit.integration.xml @@ -0,0 +1,25 @@ + + + + + ../src/ + + + + + ./integration + ./integration/tmp + + + diff --git a/tests/phpunit.unit.xml b/tests/phpunit.unit.xml new file mode 100644 index 0000000..d3a4a3e --- /dev/null +++ b/tests/phpunit.unit.xml @@ -0,0 +1,27 @@ + + + + + ../src/ + + + + + ./unit + + + + + + diff --git a/tests/unit/.gitkeep b/tests/unit/.gitkeep new file mode 100644 index 0000000..e69de29 From 1aeff970143c053815874a9dd6cec8a906f56af9 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 3 Oct 2023 07:04:28 +0000 Subject: [PATCH 4/5] Apply fixes from StyleCI --- tests/integration/api/BioTest.php | 55 ++++++++++++++++++------------- tests/integration/setup.php | 8 +++-- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/tests/integration/api/BioTest.php b/tests/integration/api/BioTest.php index d90d7e6..b8db192 100644 --- a/tests/integration/api/BioTest.php +++ b/tests/integration/api/BioTest.php @@ -1,5 +1,14 @@ [ $this->normalUser(), [ - 'id' => 3, - 'username' => 'normal2', - 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', // BCrypt hash for "too-obscure" - 'email' => 'normal2@machine.local', + 'id' => 3, + 'username' => 'normal2', + 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', // BCrypt hash for "too-obscure" + 'email' => 'normal2@machine.local', 'is_email_confirmed' => 1, - 'last_seen_at' => Carbon::now()->subSecond(), - 'bio' => 'This is a test bio for normal2.' + 'last_seen_at' => Carbon::now()->subSecond(), + 'bio' => 'This is a test bio for normal2.', ], [ - 'id' => 4, - 'username' => 'normal3', - 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', // BCrypt hash for "too-obscure" - 'email' => 'normal3@machine.local', + 'id' => 4, + 'username' => 'normal3', + 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', // BCrypt hash for "too-obscure" + 'email' => 'normal3@machine.local', 'is_email_confirmed' => 1, - 'last_seen_at' => Carbon::now()->subHour(), - ] + 'last_seen_at' => Carbon::now()->subHour(), + ], ], ]); } @@ -72,15 +81,15 @@ public function admin_can_create_user_with_bio() '/api/users', [ 'authenticatedAs' => 1, - 'json' => [ + 'json' => [ 'data' => [ 'attributes' => [ 'username' => 'test', 'password' => 'too-obscure', - 'email' => 'test@machine.local', - 'bio' => 'This is a test bio.' + 'email' => 'test@machine.local', + 'bio' => 'This is a test bio.', ], - ] + ], ], ] ) @@ -108,12 +117,12 @@ public function cannot_update_own_bio_when_permission_not_granted() '/api/users/2', [ 'authenticatedAs' => 2, - 'json' => [ + 'json' => [ 'data' => [ 'attributes' => [ - 'bio' => 'This is a test bio.' + 'bio' => 'This is a test bio.', ], - ] + ], ], ] ) @@ -128,19 +137,19 @@ public function cannot_update_own_bio_when_permission_not_granted() public function can_update_own_user_with_added_bio_when_permission_granted() { $this->giveNormalUsersEditOwnPerms(); - + $response = $this->send( $this->request( 'PATCH', '/api/users/2', [ 'authenticatedAs' => 2, - 'json' => [ + 'json' => [ 'data' => [ 'attributes' => [ - 'bio' => 'This is a test bio.' + 'bio' => 'This is a test bio.', ], - ] + ], ], ] ) diff --git a/tests/integration/setup.php b/tests/integration/setup.php index 67039c0..db9a157 100644 --- a/tests/integration/setup.php +++ b/tests/integration/setup.php @@ -1,10 +1,12 @@ Date: Tue, 3 Oct 2023 08:05:38 +0100 Subject: [PATCH 5/5] test annotation --- tests/integration/api/BioTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integration/api/BioTest.php b/tests/integration/api/BioTest.php index b8db192..5442a05 100644 --- a/tests/integration/api/BioTest.php +++ b/tests/integration/api/BioTest.php @@ -183,6 +183,9 @@ public function normal_user_cannot_see_bio_of_other_user_without_permission() $this->assertArrayNotHasKey('bio', json_decode($response->getBody()->getContents(), true)['data']['attributes']); } + /** + * @test + */ public function normal_user_can_see_others_bio_with_permission() { $this->giveNormalUserViewBioPerms();