diff --git a/test/functional/AccountDeletionRequestTest.php b/test/functional/AccountDeletionRequestTest.php index 877c6b91..1c7c33b9 100644 --- a/test/functional/AccountDeletionRequestTest.php +++ b/test/functional/AccountDeletionRequestTest.php @@ -1,34 +1,7 @@ assertFalse($USER->hasRequestedAccountDeletion()); - $this->assertFalse($SQL->accDeletionRequestExists($USER->uid)); - } elseif ($x > 0) { - $this->assertTrue($USER->hasRequestedAccountDeletion()); - $this->assertTrue($SQL->accDeletionRequestExists($USER->uid)); - } else { - throw new RuntimeException("x must not be negative"); - } - $this->assertEquals($x, $this->getNumberAccountDeletionRequests()); - } - - private function getNumberAccountDeletionRequests() - { - global $USER, $SQL; - $stmt = $SQL->getConn()->prepare("SELECT * FROM account_deletion_requests WHERE uid=:uid"); - $uid = $USER->uid; - $stmt->bindParam(":uid", $uid); - $stmt->execute(); - return count($stmt->fetchAll()); - } - public function testRequestAccountDeletionUserHasNoGroups() { global $USER, $SQL; diff --git a/test/functional/InvalidEPPNTest.php b/test/functional/InvalidEPPNTest.php index 40cc454d..d9c3af82 100644 --- a/test/functional/InvalidEPPNTest.php +++ b/test/functional/InvalidEPPNTest.php @@ -1,10 +1,9 @@ assertEquals( - $expected, - $SQL->requestExists($USER->uid, UnitySQL::REQUEST_BECOME_PI), - ); - } - private function requestGroupCreation() { http_post(__DIR__ . "/../../webroot/panel/account.php", [ diff --git a/test/functional/PIMemberRequestTest.php b/test/functional/PIMemberRequestTest.php index a4bb3826..f42c600a 100644 --- a/test/functional/PIMemberRequestTest.php +++ b/test/functional/PIMemberRequestTest.php @@ -1,11 +1,10 @@ assertFalse($SQL->requestExists($uid, $gid)); UnityHTTPD::clearMessages(); $this->requestMembership("asdlkjasldkj"); - assertMessageExists( - $this, + $this->assertMessageExists( UnityHTTPDMessageLevel::ERROR, "/.*/", "/^This PI doesn't exist$/", diff --git a/test/functional/PageLoadTest.php b/test/functional/PageLoadTest.php index 646620b4..3675dcba 100644 --- a/test/functional/PageLoadTest.php +++ b/test/functional/PageLoadTest.php @@ -1,9 +1,8 @@ assertFalse($SQL->requestExists($USER->uid, UnitySQL::REQUEST_BECOME_PI)); - } elseif ($x > 0) { - $this->assertTrue($SQL->requestExists($USER->uid, UnitySQL::REQUEST_BECOME_PI)); - } else { - throw new RuntimeException("x must not be negative"); - } - $this->assertEquals($x, $this->getNumberPiBecomeRequests()); - } - - private function getNumberPiBecomeRequests() - { - global $USER, $SQL; - // FIXME table name, "admin" are private constants in UnitySQL - // FIXME "admin" should be something else - $stmt = $SQL - ->getConn() - ->prepare("SELECT * FROM requests WHERE uid=:uid and request_for='admin'"); - $uid = $USER->uid; - $stmt->bindParam(":uid", $uid); - $stmt->execute(); - return count($stmt->fetchAll()); - } - public function testRequestBecomePi() { global $USER, $SQL; diff --git a/test/functional/PiMemberApproveTest.php b/test/functional/PiMemberApproveTest.php index 4dfd6d4e..db9cb2b0 100644 --- a/test/functional/PiMemberApproveTest.php +++ b/test/functional/PiMemberApproveTest.php @@ -1,25 +1,10 @@ getGroupMemberUIDs(); - sort($found_members); - $this->assertEqualsCanonicalizing($expected_members, $found_members); - } - - private function assertRequestedMembership(bool $expected, string $gid) - { - global $USER, $SQL; - $this->assertEquals($expected, $SQL->requestExists($USER->uid, $gid)); - } - private function requestGroupMembership(string $gid_or_mail) { http_post(__DIR__ . "/../../webroot/panel/groups.php", [ diff --git a/test/functional/PiMemberDenyTest.php b/test/functional/PiMemberDenyTest.php index be8f5c81..f93a1fd5 100644 --- a/test/functional/PiMemberDenyTest.php +++ b/test/functional/PiMemberDenyTest.php @@ -1,11 +1,10 @@ value, - $title_regex, - $body_regex, - jsonEncode($messages), - ); - $messages_with_title = array_filter($messages, fn($x) => preg_match($title_regex, $x[0])); - $messages_with_title_and_body = array_filter( - $messages_with_title, - fn($x) => preg_match($body_regex, $x[1]), - ); - $messages_with_title_and_body_and_level = array_filter( - $messages_with_title_and_body, - fn($x) => $x[2] == $level, - ); - $test_case->assertNotEmpty($messages_with_title_and_body_and_level, $error_msg); +class UnityWebPortalTestCase extends TestCase +{ + public function assertMessageExists( + UnityHTTPDMessageLevel $level, + string $title_regex, + string $body_regex, + ) { + $messages = UnityHTTPD::getMessages(); + $error_msg = sprintf( + "message(level='%s' title_regex='%s' body_regex='%s'), not found. found messages: %s", + $level->value, + $title_regex, + $body_regex, + jsonEncode($messages), + ); + $messages_with_title = array_filter($messages, fn($x) => preg_match($title_regex, $x[0])); + $messages_with_title_and_body = array_filter( + $messages_with_title, + fn($x) => preg_match($body_regex, $x[1]), + ); + $messages_with_title_and_body_and_level = array_filter( + $messages_with_title_and_body, + fn($x) => $x[2] == $level, + ); + $this->assertNotEmpty($messages_with_title_and_body_and_level, $error_msg); + } + + public function assertGroupMembers(UnityGroup $group, array $expected_members) + { + sort($expected_members); + $found_members = $group->getGroupMemberUIDs(); + sort($found_members); + $this->assertEqualsCanonicalizing($expected_members, $found_members); + } + + public function assertRequestedMembership(bool $expected, string $gid) + { + global $USER, $SQL; + $this->assertEquals($expected, $SQL->requestExists($USER->uid, $gid)); + } + + public function getNumberAccountDeletionRequests() + { + global $USER, $SQL; + $stmt = $SQL->getConn()->prepare("SELECT * FROM account_deletion_requests WHERE uid=:uid"); + $uid = $USER->uid; + $stmt->bindParam(":uid", $uid); + $stmt->execute(); + return count($stmt->fetchAll()); + } + + public function assertNumberAccountDeletionRequests(int $x) + { + global $USER, $SQL; + if ($x == 0) { + $this->assertFalse($USER->hasRequestedAccountDeletion()); + $this->assertFalse($SQL->accDeletionRequestExists($USER->uid)); + } elseif ($x > 0) { + $this->assertTrue($USER->hasRequestedAccountDeletion()); + $this->assertTrue($SQL->accDeletionRequestExists($USER->uid)); + } else { + throw new RuntimeException("x must not be negative"); + } + $this->assertEquals($x, $this->getNumberAccountDeletionRequests()); + } + + public function assertRequestedPIGroup(bool $expected) + { + global $USER, $SQL; + $this->assertEquals( + $expected, + $SQL->requestExists($USER->uid, UnitySQL::REQUEST_BECOME_PI), + ); + } + + public function getNumberPiBecomeRequests() + { + global $USER, $SQL; + // FIXME table name, "admin" are public constants in UnitySQL + // FIXME "admin" should be something else + $stmt = $SQL + ->getConn() + ->prepare("SELECT * FROM requests WHERE uid=:uid and request_for='admin'"); + $uid = $USER->uid; + $stmt->bindParam(":uid", $uid); + $stmt->execute(); + return count($stmt->fetchAll()); + } + + public function assertNumberPiBecomeRequests(int $x) + { + global $USER, $SQL; + if ($x == 0) { + $this->assertFalse($SQL->requestExists($USER->uid, UnitySQL::REQUEST_BECOME_PI)); + } elseif ($x > 0) { + $this->assertTrue($SQL->requestExists($USER->uid, UnitySQL::REQUEST_BECOME_PI)); + } else { + throw new RuntimeException("x must not be negative"); + } + $this->assertEquals($x, $this->getNumberPiBecomeRequests()); + } } diff --git a/test/unit/AjaxSshValidateTest.php b/test/unit/AjaxSshValidateTest.php index e22cb0b2..6a3b3bae 100644 --- a/test/unit/AjaxSshValidateTest.php +++ b/test/unit/AjaxSshValidateTest.php @@ -1,11 +1,8 @@