diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index 7ef33c1c..6d12ae42 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 backend_directory: . diff --git a/.gitignore b/.gitignore index d34e123e..528d5861 100755 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules vendor composer.lock js/dist +.phpunit.result.cache diff --git a/composer.json b/composer.json index 88f48a2d..6578f369 100755 --- a/composer.json +++ b/composer.json @@ -52,18 +52,36 @@ }, "flarum-cli": { "modules": { - "githubActions": true + "githubActions": true, + "backendTesting": true } } }, "require-dev": { - "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\\Polls\\Tests\\": "tests/" + } } } diff --git a/tests/fixtures/.gitkeep b/tests/fixtures/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/tests/integration/api/ForumSerializerTest.php b/tests/integration/api/ForumSerializerTest.php new file mode 100644 index 00000000..73155929 --- /dev/null +++ b/tests/integration/api/ForumSerializerTest.php @@ -0,0 +1,83 @@ +extension('fof-polls'); + + $this->prepareDatabase([ + 'users' => [ + $this->normalUser(), + ['id' => 3, 'username' => 'pollsuser', 'email' => 'polls@machine.local', 'password' => 'too-obscure', 'is_email_confirmed' => 1] + ], + 'group_user' => [ + ['user_id' => 3, 'group_id' => 4] + ], + 'group_permission' => [ + ['permission' => 'discussion.polls.start', 'group_id' => 4] + ], + ]); + } + + /** + * @test + */ + public function guest_does_not_have_discussion_polls_start_permission() + { + $response = $this->send( + $this->request('GET', '/api') + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $body = json_decode($response->getBody()->getContents()); + + $this->assertFalse($body->data->attributes->canStartPolls); + } + + /** + * @test + */ + public function normal_user_does_not_have_discussion_polls_start_permission() + { + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 2 + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $body = json_decode($response->getBody()->getContents()); + + $this->assertFalse($body->data->attributes->canStartPolls); + } + + /** + * @test + */ + public function user_with_discussion_polls_start_permission_has_discussion_polls_start_permission() + { + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 3 + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $body = json_decode($response->getBody()->getContents()); + + $this->assertTrue($body->data->attributes->canStartPolls); + } +} diff --git a/tests/integration/setup.php b/tests/integration/setup.php new file mode 100644 index 00000000..67039c08 --- /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 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