diff --git a/.github/workflows/flarum-suspend-backend.yml b/.github/workflows/flarum-suspend-backend.yml
index 193d35ea6..b65d7fb07 100644
--- a/.github/workflows/flarum-suspend-backend.yml
+++ b/.github/workflows/flarum-suspend-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/suspend
diff --git a/extensions/suspend/composer.json b/extensions/suspend/composer.json
index dba03bbbd..f0c381263 100644
--- a/extensions/suspend/composer.json
+++ b/extensions/suspend/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\\Suspend\\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/suspend/tests/fixtures/.gitkeep b/extensions/suspend/tests/fixtures/.gitkeep
new file mode 100644
index 000000000..e69de29bb
diff --git a/extensions/suspend/tests/integration/api/UseForumTest.php b/extensions/suspend/tests/integration/api/UseForumTest.php
new file mode 100644
index 000000000..2ef69c5cc
--- /dev/null
+++ b/extensions/suspend/tests/integration/api/UseForumTest.php
@@ -0,0 +1,86 @@
+extension('flarum-suspend');
+
+ $this->prepareDatabase([
+ 'users' => [
+ ['id' => 1, 'username' => 'Muralf', 'email' => 'muralf@machine.local', 'is_email_confirmed' => 1],
+ ['id' => 2, 'username' => 'SuspendedDonny', 'email' => 'acme@machine.local', 'is_email_confirmed' => 1, 'suspended_until' => Carbon::now()->addDay(), 'suspend_reason' => 'acme', 'suspend_message' => 'acme'],
+ ],
+ 'discussions' => [
+ ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1],
+ ],
+ 'posts' => [
+ ['id' => 1, 'number' => 1, 'created_at' => Carbon::now(), 'user_id' => 1, 'discussion_id' => 1, 'content' => 'Hello, world!
'],
+ ]
+ ]);
+ }
+
+ /** @test */
+ public function suspended_user_cannot_create_discussions()
+ {
+ $response = $this->send(
+ $this->request('POST', '/api/discussions', [
+ 'authenticatedAs' => 2,
+ 'json' => [
+ 'data' => [
+ 'attributes' => [
+ 'title' => 'Test post',
+ 'content' => 'Hello, world!
'
+ ],
+ ],
+ ],
+ ])
+ );
+
+ $this->assertEquals(403, $response->getStatusCode());
+ }
+
+ /** @test */
+ public function suspended_user_cannot_reply_to_discussions()
+ {
+ $response = $this->send(
+ $this->request('POST', '/api/posts', [
+ 'authenticatedAs' => 2,
+ 'json' => [
+ 'data' => [
+ 'attributes' => [
+ 'content' => 'Hello, world!
'
+ ],
+ 'relationships' => [
+ 'discussion' => [
+ 'data' => [
+ 'type' => 'discussions',
+ 'id' => 1,
+ ],
+ ],
+ ],
+ ],
+ ],
+ ])
+ );
+
+ $this->assertEquals(403, $response->getStatusCode());
+ }
+}
diff --git a/extensions/suspend/tests/integration/api/users/ListUsersTest.php b/extensions/suspend/tests/integration/api/users/ListUsersTest.php
new file mode 100644
index 000000000..f3a41d314
--- /dev/null
+++ b/extensions/suspend/tests/integration/api/users/ListUsersTest.php
@@ -0,0 +1,75 @@
+extension('flarum-suspend');
+
+ $this->prepareDatabase([
+ 'users' => [
+ ['id' => 1, 'username' => 'Muralf', 'email' => 'muralf@machine.local', 'is_email_confirmed' => 1],
+ ['id' => 2, 'username' => 'SuspendedDonny1', 'email' => 'acme1@machine.local', 'is_email_confirmed' => 1, 'suspended_until' => Carbon::now()->addDay(), 'suspend_reason' => 'acme', 'suspend_message' => 'acme'],
+ ['id' => 3, 'username' => 'SuspendedDonny2', 'email' => 'acme2@machine.local', 'is_email_confirmed' => 1, 'suspended_until' => Carbon::now()->addDay(), 'suspend_reason' => 'acme', 'suspend_message' => 'acme'],
+ ['id' => 4, 'username' => 'SuspendedDonny3', 'email' => 'acme3@machine.local', 'is_email_confirmed' => 1, 'suspended_until' => Carbon::now()->subDay(), 'suspend_reason' => 'acme', 'suspend_message' => 'acme'],
+ ['id' => 5, 'username' => 'SuspendedDonny4', 'email' => 'acme4@machine.local', 'is_email_confirmed' => 1, 'suspended_until' => Carbon::now()->addDay(), 'suspend_reason' => 'acme', 'suspend_message' => 'acme'],
+ ['id' => 6, 'username' => 'Acme', 'email' => 'acme5@machine.local', 'is_email_confirmed' => 1],
+ ]
+ ]);
+ }
+
+ public function can_view_default_users_list()
+ {
+ $response = $this->send(
+ $this->request('GET', '/api/users', [
+ 'authenticatedAs' => 1,
+ ])->withQueryParams([
+ 'filter' => [
+ 'suspended' => true,
+ ],
+ ])
+ );
+
+ $body = json_decode($response->getBody()->getContents(), true);
+
+ $this->assertEquals(200, $response->getStatusCode());
+ $this->assertEqualsCanonicalizing([1, 2, 3, 4, 5, 6], Arr::pluck($body['data'], 'id'));
+ }
+
+ /** @test */
+ public function can_filter_users_by_suspension()
+ {
+ $response = $this->send(
+ $this->request('GET', '/api/users', [
+ 'authenticatedAs' => 1,
+ ])->withQueryParams([
+ 'filter' => [
+ 'suspended' => true,
+ ],
+ ])
+ );
+
+ $body = json_decode($response->getBody()->getContents(), true);
+
+ $this->assertEquals(200, $response->getStatusCode());
+ $this->assertEqualsCanonicalizing([2, 3, 5], Arr::pluck($body['data'], 'id'));
+ }
+}
diff --git a/extensions/suspend/tests/integration/api/users/SuspendUserTest.php b/extensions/suspend/tests/integration/api/users/SuspendUserTest.php
new file mode 100644
index 000000000..86fd8f16d
--- /dev/null
+++ b/extensions/suspend/tests/integration/api/users/SuspendUserTest.php
@@ -0,0 +1,104 @@
+extension('flarum-suspend');
+
+ $this->prepareDatabase([
+ 'users' => [
+ ['id' => 1, 'username' => 'Muralf', 'email' => 'muralf@machine.local', 'is_email_confirmed' => 1],
+ $this->normalUser(),
+ ['id' => 3, 'username' => 'acme', 'email' => 'acme@machine.local', 'is_email_confirmed' => 1],
+ ],
+ 'groups' => [
+ ['id' => 5, 'name_singular' => 'Acme', 'name_plural' => 'Acme', 'is_hidden' => 0]
+ ],
+ 'group_user' => [
+ ['user_id' => 3, 'group_id' => 5]
+ ],
+ 'group_permission' => [
+ ['permission' => 'user.suspend', 'group_id' => 5]
+ ]
+ ]);
+ }
+
+ /**
+ * @dataProvider allowedToSuspendUser
+ * @test
+ */
+ public function can_suspend_user_if_allowed(?int $authenticatedAs, int $targetUserId, string $message)
+ {
+ $response = $this->sendSuspensionRequest($authenticatedAs, $targetUserId);
+
+ $this->assertEquals(200, $response->getStatusCode());
+ }
+
+ /**
+ * @dataProvider unallowedToSuspendUser
+ * @test
+ */
+ public function cannot_suspend_user_if_not_allowed(?int $authenticatedAs, int $targetUserId, string $message)
+ {
+ $response = $this->sendSuspensionRequest($authenticatedAs, $targetUserId);
+
+ $this->assertEquals(403, $response->getStatusCode());
+ }
+
+ public function allowedToSuspendUser(): array
+ {
+ return [
+ [1, 2, 'Admin can suspend any user'],
+ [1, 3, 'Admin can suspend any user'],
+ [3, 2, 'User with permission can suspend any user'],
+ ];
+ }
+
+ public function unallowedToSuspendUser(): array
+ {
+ return [
+ [1, 1, 'Admin cannot suspend self'],
+ [2, 2, 'User without permission cannot suspend self'],
+ [2, 3, 'User without permission cannot suspend other user'],
+ [3, 3, 'User with permission cannot suspend self'],
+ [3, 1, 'User with permission cannot suspend admin'],
+ ];
+ }
+
+ protected function sendSuspensionRequest(?int $authenticatedAs, int $targetUserId): ResponseInterface
+ {
+ return $this->send(
+ $this->request('PATCH', "/api/users/$targetUserId", [
+ 'authenticatedAs' => $authenticatedAs,
+ 'json' => [
+ 'data' => [
+ 'attributes' => [
+ 'suspendedUntil' => Carbon::now()->addDay(),
+ 'suspendReason' => 'Suspended for acme reasons.',
+ 'suspendMessage' => 'You have been suspended.',
+ ]
+ ]
+ ]
+ ])
+ );
+ }
+}
diff --git a/extensions/suspend/tests/integration/setup.php b/extensions/suspend/tests/integration/setup.php
new file mode 100644
index 000000000..67039c083
--- /dev/null
+++ b/extensions/suspend/tests/integration/setup.php
@@ -0,0 +1,16 @@
+run();
diff --git a/extensions/suspend/tests/phpunit.integration.xml b/extensions/suspend/tests/phpunit.integration.xml
new file mode 100644
index 000000000..90fbbff37
--- /dev/null
+++ b/extensions/suspend/tests/phpunit.integration.xml
@@ -0,0 +1,25 @@
+
+
+
+
+ ../src/
+
+
+
+
+ ./integration
+ ./integration/tmp
+
+
+
diff --git a/extensions/suspend/tests/phpunit.unit.xml b/extensions/suspend/tests/phpunit.unit.xml
new file mode 100644
index 000000000..d3a4a3e3d
--- /dev/null
+++ b/extensions/suspend/tests/phpunit.unit.xml
@@ -0,0 +1,27 @@
+
+
+
+
+ ../src/
+
+
+
+
+ ./unit
+
+
+
+
+
+
diff --git a/extensions/suspend/tests/unit/.gitkeep b/extensions/suspend/tests/unit/.gitkeep
new file mode 100644
index 000000000..e69de29bb