From e21a1e09fdb83fde769ee34ccb697902eebbce50 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Thu, 7 Jul 2022 14:16:01 +0100 Subject: [PATCH] test(sticky): list discussions works as expected with stickies Signed-off-by: Sami Mazouz --- .github/workflows/flarum-sticky-backend.yml | 2 +- extensions/sticky/composer.json | 27 +++++- extensions/sticky/tests/fixtures/.gitkeep | 0 .../integration/api/ListDiscussionsTest.php | 90 +++++++++++++++++++ extensions/sticky/tests/integration/setup.php | 16 ++++ .../sticky/tests/phpunit.integration.xml | 25 ++++++ extensions/sticky/tests/phpunit.unit.xml | 27 ++++++ extensions/sticky/tests/unit/.gitkeep | 0 8 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 extensions/sticky/tests/fixtures/.gitkeep create mode 100644 extensions/sticky/tests/integration/api/ListDiscussionsTest.php create mode 100644 extensions/sticky/tests/integration/setup.php create mode 100644 extensions/sticky/tests/phpunit.integration.xml create mode 100644 extensions/sticky/tests/phpunit.unit.xml create mode 100644 extensions/sticky/tests/unit/.gitkeep diff --git a/.github/workflows/flarum-sticky-backend.yml b/.github/workflows/flarum-sticky-backend.yml index 3ec5a6dca..d86994abc 100644 --- a/.github/workflows/flarum-sticky-backend.yml +++ b/.github/workflows/flarum-sticky-backend.yml @@ -6,6 +6,6 @@ jobs: run: uses: ./.github/workflows/REUSABLE_backend.yml with: - enable_backend_testing: false + enable_backend_testing: true backend_directory: ./extensions/sticky diff --git a/extensions/sticky/composer.json b/extensions/sticky/composer.json index 6cadf3947..92e948ccc 100644 --- a/extensions/sticky/composer.json +++ b/extensions/sticky/composer.json @@ -51,7 +51,7 @@ "prettier": true, "typescript": false, "bundlewatch": false, - "backendTesting": false, + "backendTesting": true, "editorConfig": true, "styleci": true } @@ -64,5 +64,28 @@ } ], "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "autoload-dev": { + "psr-4": { + "Flarum\\Sticky\\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." + }, + "require-dev": { + "flarum/testing": "^1.0.0" + } } diff --git a/extensions/sticky/tests/fixtures/.gitkeep b/extensions/sticky/tests/fixtures/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/extensions/sticky/tests/integration/api/ListDiscussionsTest.php b/extensions/sticky/tests/integration/api/ListDiscussionsTest.php new file mode 100644 index 000000000..525531510 --- /dev/null +++ b/extensions/sticky/tests/integration/api/ListDiscussionsTest.php @@ -0,0 +1,90 @@ +extension('flarum-sticky'); + + $this->prepareDatabase([ + 'users' => [ + ['id' => 1, 'username' => 'Muralf', 'email' => 'muralf@machine.local', 'is_email_confirmed' => 1], + $this->normalUser() + ], + 'discussions' => [ + ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1, 'is_sticky' => true], + ['id' => 2, 'title' => __CLASS__, 'created_at' => Carbon::now()->addMinute(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1, 'is_sticky' => false], + ['id' => 3, 'title' => __CLASS__, 'created_at' => Carbon::now()->addMinutes(2), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1, 'is_sticky' => true], + ['id' => 4, 'title' => __CLASS__, 'created_at' => Carbon::now()->addMinutes(3), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1, 'is_sticky' => false], + ], + 'discussion_user' => [ + ['discussion_id' => 1, 'user_id' => 1, 'last_read_post_number' => 1], + ['discussion_id' => 3, 'user_id' => 1, 'last_read_post_number' => 1], + ] + ]); + } + + /** @test */ + public function list_discussions_shows_sticky_first_as_guest() + { + $response = $this->send( + $this->request('GET', '/api/discussions') + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $data = json_decode($response->getBody()->getContents(), true); + + $this->assertEqualsCanonicalizing([1, 3, 2, 4], Arr::pluck($data['data'], 'id')); + } + + /** @test */ + public function list_discussions_shows_sticky_unread_first_as_user() + { + $response = $this->send( + $this->request('GET', '/api/discussions', [ + 'authenticatedAs' => 2 + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $data = json_decode($response->getBody()->getContents(), true); + + $this->assertEqualsCanonicalizing([1, 3, 2, 4], Arr::pluck($data['data'], 'id')); + } + + /** @test */ + public function list_discussions_shows_normal_order_when_all_read_as_user() + { + $response = $this->send( + $this->request('GET', '/api/discussions', [ + 'authenticatedAs' => 1 + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $data = json_decode($response->getBody()->getContents(), true); + + $this->assertEqualsCanonicalizing([1, 2, 3, 4], Arr::pluck($data['data'], 'id')); + } +} diff --git a/extensions/sticky/tests/integration/setup.php b/extensions/sticky/tests/integration/setup.php new file mode 100644 index 000000000..67039c083 --- /dev/null +++ b/extensions/sticky/tests/integration/setup.php @@ -0,0 +1,16 @@ +run(); diff --git a/extensions/sticky/tests/phpunit.integration.xml b/extensions/sticky/tests/phpunit.integration.xml new file mode 100644 index 000000000..90fbbff37 --- /dev/null +++ b/extensions/sticky/tests/phpunit.integration.xml @@ -0,0 +1,25 @@ + + + + + ../src/ + + + + + ./integration + ./integration/tmp + + + diff --git a/extensions/sticky/tests/phpunit.unit.xml b/extensions/sticky/tests/phpunit.unit.xml new file mode 100644 index 000000000..d3a4a3e3d --- /dev/null +++ b/extensions/sticky/tests/phpunit.unit.xml @@ -0,0 +1,27 @@ + + + + + ../src/ + + + + + ./unit + + + + + + diff --git a/extensions/sticky/tests/unit/.gitkeep b/extensions/sticky/tests/unit/.gitkeep new file mode 100644 index 000000000..e69de29bb