Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions test/functional/AccountDeletionRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
<?php

use PHPUnit\Framework\TestCase;

class AccountDeletionRequestTest extends TestCase
class AccountDeletionRequestTest extends UnityWebPortalTestCase
{
private 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());
}

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;
Expand Down
3 changes: 1 addition & 2 deletions test/functional/InvalidEPPNTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

use UnityWebPortal\lib\exceptions\SSOException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class InvalidEPPNTest extends TestCase
class InvalidEPPNTest extends UnityWebPortalTestCase
{
public static function provider()
{
Expand Down
3 changes: 1 addition & 2 deletions test/functional/LoginShellSetTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class LoginShellSetTest extends TestCase
class LoginShellSetTest extends UnityWebPortalTestCase
{
private static $_initialLoginShell;

Expand Down
12 changes: 1 addition & 11 deletions test/functional/PIBecomeApproveTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
<?php

use PHPUnit\Framework\TestCase;
use UnityWebPortal\lib\UnityOrg;
use UnityWebPortal\lib\UnitySQL;

class PIBecomeApproveTest extends TestCase
class PIBecomeApproveTest extends UnityWebPortalTestCase
{
private function assertRequestedPIGroup(bool $expected)
{
global $USER, $SQL;
$this->assertEquals(
$expected,
$SQL->requestExists($USER->uid, UnitySQL::REQUEST_BECOME_PI),
);
}

private function requestGroupCreation()
{
http_post(__DIR__ . "/../../webroot/panel/account.php", [
Expand Down
6 changes: 2 additions & 4 deletions test/functional/PIMemberRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

use PHPUnit\Framework\TestCase;
use UnityWebPortal\lib\UnityHTTPDMessageLevel;
use UnityWebPortal\lib\UnitySQL;
use UnityWebPortal\lib\UnityHTTPD;

class PIMemberRequestTest extends TestCase
class PIMemberRequestTest extends UnityWebPortalTestCase
{
private function requestMembership(string $gid_or_mail)
{
Expand Down Expand Up @@ -47,8 +46,7 @@ public function testRequestMembership()
$this->assertFalse($SQL->requestExists($uid, $gid));
UnityHTTPD::clearMessages();
$this->requestMembership("asdlkjasldkj");
assertMessageExists(
$this,
$this->assertMessageExists(
UnityHTTPDMessageLevel::ERROR,
"/.*/",
"/^This PI doesn't exist$/",
Expand Down
3 changes: 1 addition & 2 deletions test/functional/PageLoadTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class PageLoadTest extends TestCase
class PageLoadTest extends UnityWebPortalTestCase
{
public static function provider()
{
Expand Down
30 changes: 1 addition & 29 deletions test/functional/PiBecomeRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
<?php

use PHPUnit\Framework\TestCase;
use UnityWebPortal\lib\UnitySQL;

class PIBecomeRequestTest extends TestCase
class PIBecomeRequestTest extends UnityWebPortalTestCase
{
private 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());
}

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;
Expand Down
17 changes: 1 addition & 16 deletions test/functional/PiMemberApproveTest.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
<?php

use PHPUnit\Framework\TestCase;
use UnityWebPortal\lib\UnitySQL;
use UnityWebPortal\lib\UnityGroup;

class PIMemberApproveTest extends TestCase
class PIMemberApproveTest extends UnityWebPortalTestCase
{
private function assertGroupMembers(UnityGroup $group, array $expected_members)
{
sort($expected_members);
$found_members = $group->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", [
Expand Down
3 changes: 1 addition & 2 deletions test/functional/PiMemberDenyTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use UnityWebPortal\lib\UnityUser;
use function PHPUnit\Framework\assertEquals;

class PIMemberDenyTest extends TestCase
class PIMemberDenyTest extends UnityWebPortalTestCase
{
static $requestUid;

Expand Down
3 changes: 1 addition & 2 deletions test/functional/PiRemoveUserTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use UnityWebPortal\lib\UnityUser;

class PIRemoveUserTest extends TestCase
class PIRemoveUserTest extends UnityWebPortalTestCase
{
private function removeUser(string $uid)
{
Expand Down
3 changes: 1 addition & 2 deletions test/functional/RegisterUserTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use UnityWebPortal\lib\UnityOrg;

class RegisterUserTest extends TestCase
class RegisterUserTest extends UnityWebPortalTestCase
{
public static function provider()
{
Expand Down
3 changes: 1 addition & 2 deletions test/functional/SSHKeyAddTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

use PHPUnit\Framework\TestCase;
use UnityWebPortal\lib\UnityGithub;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockBuilder;

class SSHKeyAddTest extends TestCase
class SSHKeyAddTest extends UnityWebPortalTestCase
{
private function addSshKeysPaste(array $keys): void
{
Expand Down
3 changes: 1 addition & 2 deletions test/functional/SSHKeyDeleteTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class SSHKeyDeleteTest extends TestCase
class SSHKeyDeleteTest extends UnityWebPortalTestCase
{
static $initialKeys;

Expand Down
3 changes: 1 addition & 2 deletions test/functional/ViewAsUserTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

use UnityWebPortal\lib\UnityHTTPD;
use PHPUnit\Framework\TestCase;

class ViewAsUserTest extends TestCase
class ViewAsUserTest extends UnityWebPortalTestCase
{
public function _testViewAsUser(array $beforeUser, array $afterUser)
{
Expand Down
126 changes: 102 additions & 24 deletions test/phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use UnityWebPortal\lib\UnityGroup;
use UnityWebPortal\lib\UnityHTTPD;
use UnityWebPortal\lib\UnitySQL;
use UnityWebPortal\lib\UnityHTTPDMessageLevel;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -323,28 +324,105 @@ function getAdminUser()
return ["[email protected]", "foo", "bar", "[email protected]"];
}

function assertMessageExists(
TestCase $test_case,
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,
);
$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());
}
}
Loading