From dd6c47484d76c18f8b4cb9a8912dc35dfcbde252 Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 21 Feb 2024 23:20:54 +0000 Subject: [PATCH] [core] UserClass tests --- core/tests/UserClassTest.php | 38 ++++++++++++++++++++++++++++++++++++ core/userclass.php | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 core/tests/UserClassTest.php diff --git a/core/tests/UserClassTest.php b/core/tests/UserClassTest.php new file mode 100644 index 00000000..84f06aee --- /dev/null +++ b/core/tests/UserClassTest.php @@ -0,0 +1,38 @@ + true, + Permissions::BIG_SEARCH => false, + ]); + $this->assertEquals("user2", $cls->name); + $this->assertTrue($cls->can(Permissions::CREATE_COMMENT)); + $this->assertFalse($cls->can(Permissions::BIG_SEARCH)); + } + + public function test_not_found(): void + { + $cls = UserClass::$known_classes['user']; + $this->assertException(ServerError::class, function () use ($cls) { + $cls->can("not_found"); + }); + } + + public function test_permissions(): void + { + $cls = UserClass::$known_classes['user']; + $ps = $cls->permissions(); + $this->assertContains(Permissions::CREATE_COMMENT, $ps); + } +} diff --git a/core/userclass.php b/core/userclass.php index 40fdc05d..530b9454 100644 --- a/core/userclass.php +++ b/core/userclass.php @@ -44,7 +44,6 @@ class UserClass #[Field(type: "[Permission!]!")] public function permissions(): array { - global $_all_false; $perms = []; foreach ((new \ReflectionClass(Permissions::class))->getConstants() as $k => $v) { if ($this->can($v)) { @@ -90,6 +89,7 @@ foreach ((new \ReflectionClass(Permissions::class))->getConstants() as $k => $v) $_all_true[Permissions::HELLBANNED] = false; new UserClass("base", null, $_all_false); new UserClass("admin", null, $_all_true); +unset($_all_true); unset($_all_false); // Ghost users can't do anything