diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index af3b776..5e033cf 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 9f26f96..9123089 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 1530320..d7b30eb 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 13b0be6..57126fa 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 0000000..e69de29 diff --git a/tests/integration/api/UserPrivacyTest.php b/tests/integration/api/UserPrivacyTest.php new file mode 100644 index 0000000..d5459f6 --- /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 0000000..bf1c357 --- /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 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