[core] UserClass tests

This commit is contained in:
Shish 2024-02-21 23:20:54 +00:00 committed by Shish
parent 00b96c313f
commit dd6c47484d
2 changed files with 39 additions and 1 deletions

View file

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Shimmie2;
use PHPUnit\Framework\TestCase;
require_once "core/userclass.php";
class UserClassTest extends ShimmiePHPUnitTestCase
{
public function test_new_class(): void
{
$cls = new UserClass("user2", "user", [
Permissions::CREATE_COMMENT => 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);
}
}

View file

@ -44,7 +44,6 @@ class UserClass
#[Field(type: "[Permission!]!")] #[Field(type: "[Permission!]!")]
public function permissions(): array public function permissions(): array
{ {
global $_all_false;
$perms = []; $perms = [];
foreach ((new \ReflectionClass(Permissions::class))->getConstants() as $k => $v) { foreach ((new \ReflectionClass(Permissions::class))->getConstants() as $k => $v) {
if ($this->can($v)) { if ($this->can($v)) {
@ -90,6 +89,7 @@ foreach ((new \ReflectionClass(Permissions::class))->getConstants() as $k => $v)
$_all_true[Permissions::HELLBANNED] = false; $_all_true[Permissions::HELLBANNED] = false;
new UserClass("base", null, $_all_false); new UserClass("base", null, $_all_false);
new UserClass("admin", null, $_all_true); new UserClass("admin", null, $_all_true);
unset($_all_true);
unset($_all_false); unset($_all_false);
// Ghost users can't do anything // Ghost users can't do anything