Skip to content

Commit 0c50453

Browse files
committed
write test for PI group disband (requires changes in phpopenldaper)
1 parent 92016c2 commit 0c50453

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

resources/autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
require_once __DIR__ . "/lib/UnityWebhook.php";
2121
require_once __DIR__ . "/lib/UnityRedis.php";
2222
require_once __DIR__ . "/lib/UnityGithub.php";
23+
require_once __DIR__ . "/lib/exception/RedirectException.php";
2324

2425
// run init script
2526
require __DIR__ . "/init.php";

resources/lib/UnitySite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static function redirect($destination)
1010
{
1111
if ($_SERVER["PHP_SELF"] != $destination) {
1212
header("Location: $destination");
13-
die("Redirect failed, click <a href='$destination'>here</a> to continue.");
13+
throw new RedirectException("Redirect failed, click <a href='$destination'>here</a> to continue.");
1414
}
1515
}
1616

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace UnityWebPortal\lib;
4+
5+
class RedirectException extends \Exception
6+
{
7+
}

test/functional/PiDisbandTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use PHPUnit\Framework\Attributes\DataProvider;
5+
use UnityWebPortal\lib\UnityUser;
6+
use UnityWebPortal\lib\UnityLDAP;
7+
use UnityWebPortal\lib\RedirectException;
8+
9+
class PiDisbandTest extends TestCase {
10+
static $requestUid;
11+
12+
public static function setUpBeforeClass(): void{
13+
global $USER;
14+
switchUser(...getNormalUser());
15+
self::$requestUid = $USER->getUID();
16+
}
17+
18+
private function disband()
19+
{
20+
// PI disband leads to redirect, redirect doesn't work during testing
21+
try {
22+
post(
23+
__DIR__ . "/../../webroot/panel/pi.php",
24+
["form_name" => "disband"]
25+
);
26+
} catch (RedirectException $e) {}
27+
}
28+
29+
public function testDisband()
30+
{
31+
global $USER, $SQL;
32+
switchUser(...getUserIsPIHasNoMembersNoMemberRequests());
33+
$pi = $USER;
34+
$piGroup = $USER->getPIGroup();
35+
$piGroup->requestGroup(true);
36+
$piGroup->approveGroup();
37+
$piGroupEntry = $piGroup->getLDAPPiGroup();
38+
$piGroupAttributesBefore = $piGroupEntry->getAttributes();
39+
error_log(json_encode($piGroupAttributesBefore, JSON_PRETTY_PRINT));
40+
$piGroupName = $piGroup->getPIUID();
41+
$this->assertTrue($piGroup->exists());
42+
$this->assertEquals([$pi->getUID()], $piGroup->getGroupMemberUIDs());
43+
$this->assertEmpty($piGroup->getRequests());
44+
try {
45+
$SQL->addRequest(self::$requestUid, $piGroupName);
46+
$this->disband();
47+
$this->assertFalse($piGroup->exists());
48+
$this->assertEmpty($SQL->getRequests($piGroup->getPIUID()));
49+
} finally {
50+
$piGroupEntry->setAttributes($piGroupAttributesBefore);
51+
if (!$piGroupEntry->write()) {
52+
throw new Exception("Failed to recreate POSIX group $piGroupName");
53+
}
54+
}
55+
}
56+
}

test/phpunit-bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require_once __DIR__ . "/../resources/lib/UnityWebhook.php";
1515
require_once __DIR__ . "/../resources/lib/UnityRedis.php";
1616
require_once __DIR__ . "/../resources/lib/UnityGithub.php";
17+
require_once __DIR__ . "/../resources/lib/exception/RedirectException.php";
1718

1819
global $HTTP_HEADER_TEST_INPUTS;
1920
$HTTP_HEADER_TEST_INPUTS = [

0 commit comments

Comments
 (0)