From 725636c2b51c1a6e6276052cd61929805b3cabbd Mon Sep 17 00:00:00 2001 From: IanM <16573496+imorland@users.noreply.github.com> Date: Fri, 15 Dec 2023 20:39:03 +0000 Subject: [PATCH] fix: users cannot enable block pd option (#192) * chore: create test for toggle block pds * Apply fixes from StyleCI * chore: enable tests * fix: toggling blockpd fails to save * fix: test disable setting * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot --- .github/workflows/backend.yml | 2 +- .gitignore | 1 + composer.json | 26 ++++++- src/Listeners/SaveUserPreferences.php | 2 +- tests/fixtures/.gitkeep | 0 tests/integration/api/UserPrivacyTest.php | 92 +++++++++++++++++++++++ tests/integration/setup.php | 18 +++++ tests/phpunit.integration.xml | 25 ++++++ tests/phpunit.unit.xml | 27 +++++++ tests/unit/.gitkeep | 0 10 files changed, 187 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/.gitkeep create mode 100644 tests/integration/api/UserPrivacyTest.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 index af3b776b..5e033cf4 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -6,7 +6,7 @@ jobs: run: uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@1.x with: - enable_backend_testing: false + enable_backend_testing: true enable_phpstan: true php_versions: '["8.0", "8.1", "8.2", "8.3"]' diff --git a/.gitignore b/.gitignore index 9f26f96b..9123089c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ js/dist js/node_modules vendor/ composer.lock +.phpunit.result.cache diff --git a/composer.json b/composer.json index 15303203..d7b30eb9 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,8 @@ }, "flarum-cli": { "modules": { - "githubActions": true + "githubActions": true, + "backendTesting": true } } }, @@ -76,13 +77,30 @@ "require-dev": { "fof/split": "*", "flarum/flags": "*", - "flarum/phpstan": "*" + "flarum/phpstan": "*", + "flarum/testing": "^1.0.0" }, "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\\Byobu\\Tests\\": "tests/" + } } } diff --git a/src/Listeners/SaveUserPreferences.php b/src/Listeners/SaveUserPreferences.php index 13b0be62..57126fab 100644 --- a/src/Listeners/SaveUserPreferences.php +++ b/src/Listeners/SaveUserPreferences.php @@ -26,7 +26,7 @@ public function handle(Saving $event) if ($blocksPd !== null) { $actor->assertPermission($actor->id === $user->id); - $user->blocks_byobu_pd = (bool) $user->blocks_byobu_pd; + $user->blocks_byobu_pd = (bool) $blocksPd; } } } diff --git a/tests/fixtures/.gitkeep b/tests/fixtures/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/tests/integration/api/UserPrivacyTest.php b/tests/integration/api/UserPrivacyTest.php new file mode 100644 index 00000000..d5459f6e --- /dev/null +++ b/tests/integration/api/UserPrivacyTest.php @@ -0,0 +1,92 @@ +extension('fof-byobu'); + + $this->prepareDatabase([ + 'users' => [ + $this->normalUser(), + ['id' => 3, 'username' => 'normal2', 'email' => 'normal2@machine.local', 'password' => 'too-obscure', 'blocks_byobu_pd' => true], + ], + ]); + } + + /** + * @test + */ + public function user_can_set_block_pd_setting() + { + $response = $this->send( + $this->request( + 'PATCH', + '/api/users/2', + [ + 'authenticatedAs' => 2, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'blocksPd' => true, + ], + ], + ], + ] + ) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $json = json_decode($response->getBody()->getContents(), true); + + $this->assertTrue($json['data']['attributes']['blocksPd']); + } + + /** + * @test + */ + public function user_can_disable_block_pd_setting() + { + $response = $this->send( + $this->request( + 'PATCH', + '/api/users/3', + [ + 'authenticatedAs' => 3, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'blocksPd' => false, + ], + ], + ], + ] + ) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $json = json_decode($response->getBody()->getContents(), true); + + $this->assertFalse($json['data']['attributes']['blocksPd']); + } +} diff --git a/tests/integration/setup.php b/tests/integration/setup.php new file mode 100644 index 00000000..bf1c3576 --- /dev/null +++ b/tests/integration/setup.php @@ -0,0 +1,18 @@ +run(); diff --git a/tests/phpunit.integration.xml b/tests/phpunit.integration.xml new file mode 100644 index 00000000..90fbbff3 --- /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 00000000..d3a4a3e3 --- /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 00000000..e69de29b