diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index bf77423..edde91e 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@main with: - enable_backend_testing: false + enable_backend_testing: true enable_phpstan: true php_versions: '["8.0", "8.1", "8.2", "8.3"]' backend_directory: . diff --git a/.gitignore b/.gitignore index d34e123..528d586 100644 --- 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 5150fb2..f684100 100644 --- a/composer.json +++ b/composer.json @@ -71,13 +71,32 @@ "flarum/subscriptions": "*", "flarum/phpstan": "*", "flarum/mentions": "*", - "flarum/gdpr": "dev-main" + "flarum/gdpr": "dev-main", + "flarum/testing": "^1.0.0", + "flarum/tags":"*", + "fof/extend": "*" + }, + "autoload-dev": { + "psr-4": { + "FoF\\FollowTags\\Tests\\": "tests/" + } }, "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." } } diff --git a/tests/fixtures/.gitkeep b/tests/fixtures/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/ExtensionDepsTrait.php b/tests/integration/ExtensionDepsTrait.php new file mode 100644 index 0000000..e7e4320 --- /dev/null +++ b/tests/integration/ExtensionDepsTrait.php @@ -0,0 +1,22 @@ +extension('flarum-tags'); + $this->extension('fof-extend'); + $this->extension('fof-follow-tags'); + } +} diff --git a/tests/integration/TagsDefinitionTrait.php b/tests/integration/TagsDefinitionTrait.php new file mode 100644 index 0000000..007e9fb --- /dev/null +++ b/tests/integration/TagsDefinitionTrait.php @@ -0,0 +1,24 @@ + 1, 'name' => 'General', 'slug' => 'general', 'position' => 0, 'parent_id' => null], + ['id' => 2, 'name' => 'Testing', 'slug' => 'testing', 'position' => 1, 'parent_id' => null], + ['id' => 3, 'name' => 'Archive', 'slug' => 'archive', 'position' => 2, 'parent_id' => null, 'is_restricted' => true], + ]; + } +} diff --git a/tests/integration/notifications/NotificationsTest.php b/tests/integration/notifications/NotificationsTest.php new file mode 100644 index 0000000..9be1ce2 --- /dev/null +++ b/tests/integration/notifications/NotificationsTest.php @@ -0,0 +1,118 @@ +extensionDeps(); + + $this->prepareDatabase([ + 'users' => [ + $this->normalUser(), + ], + 'tags' => $this->tags(), + 'tag_user' => [ + ['user_id' => 2, 'tag_id' => 1, 'is_hidden' => 0, 'subscription' => 'follow', 'created_at' => Carbon::now()->toDateTimeString()], + ['user_id' => 2, 'tag_id' => 2, 'is_hidden' => 0, 'subscription' => 'lurking', 'created_at' => Carbon::now()->toDateTimeString()], + ], + 'discussion_tag' => [ + ['discussion_id' => 1, 'tag_id' => 1, 'created_at' => Carbon::now()->toDateTimeString()], + ['discussion_id' => 2, 'tag_id' => 2, 'created_at' => Carbon::now()->toDateTimeString()], + ], + 'discussions' => [ + ['id' => 1, 'title' => 'The quick brown fox jumps over the lazy dog', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'participant_count' => 1], + ['id' => 2, 'title' => 'The quick brown fox jumps over the lazy dog', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'participant_count' => 1], + ], + 'posts' => [ + ['id' => 1, 'discussion_id' => 1, 'user_id' => 2, 'type' => 'comment', 'content' => '

Following

', 'is_private' => 0, 'is_approved' => 1, 'number' => 1], + ['id' => 2, 'discussion_id' => 2, 'user_id' => 2, 'type' => 'comment', 'content' => '

Lurking

', 'is_private' => 0, 'is_approved' => 1, 'number' => 1], + ], + ]); + } + + /** + * @test + */ + public function notification_sent_when_new_discussion_in_followed_tag() + { + $response = $this->send( + $this->request('POST', '/api/discussions', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'title' => 'New discussion', + 'content' => '

New Post

', + ], + 'relationships' => [ + 'tags' => [ + 'data' => [ + ['type' => 'tags', 'id' => 1], + ], + ], + ], + ], + ], + ]) + ); + + $this->assertEquals(201, $response->getStatusCode()); + + $response = $this->send( + $this->request('GET', '/api/notifications', [ + 'authenticatedAs' => 2, + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $response = json_decode($response->getBody(), true); + + $this->assertEquals(1, count($response['data'])); + $this->assertEquals('newDiscussionInTag', $response['data'][0]['attributes']['contentType']); + } + + /** + * @test + */ + public function no_notification_sent_when_new_post_in_followed_tag() + { + } + + /** + * @test + */ + public function notification_sent_when_new_discussion_in_lurked_tag() + { + } + + /** + * @test + */ + public function notification_sent_when_new_post_in_lurked_tag() + { + } +} diff --git a/tests/integration/setup.php b/tests/integration/setup.php new file mode 100644 index 0000000..90eee37 --- /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