Skip to content

Commit f60c432

Browse files
committed
add test
1 parent fc86809 commit f60c432

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use UnityWebPortal\lib\UnityHTTPD;
5+
use UnityWebPortal\lib\UnityHTTPDMessageLevel;
6+
7+
class DeleteMessageTest extends TestCase
8+
{
9+
public function testDeleteMessage(): void
10+
{
11+
switchUser(...getBlankUser());
12+
$initial = UnityHTTPD::getMessages();
13+
$this->assertEmpty($initial);
14+
UnityHTTPD::messageDebug("foo1", "bar1");
15+
UnityHTTPD::messageDebug("foo2", "bar2");
16+
UnityHTTPD::messageDebug("foo3", "bar3");
17+
UnityHTTPD::messageError("foo", "bar");
18+
UnityHTTPD::messageInfo("foo", "bar");
19+
UnityHTTPD::messageSuccess("foo", "bar");
20+
UnityHTTPD::messageWarning("foo", "bar");
21+
try {
22+
$before = array_map("jsonEncode", UnityHTTPD::getMessages());
23+
http_post(
24+
__DIR__ . "/../../webroot/panel/ajax/delete_message.php",
25+
[
26+
"level" => "debug",
27+
"title_regex" => "/^.*2$/",
28+
"body_regex" => "/^.*2$/",
29+
],
30+
enforce_PRG: false,
31+
);
32+
$after = array_map("jsonEncode", UnityHTTPD::getMessages());
33+
$difference = array_diff($before, $after);
34+
$message_expected_removed = ["foo2", "bar2", UnityHTTPDMessageLevel::DEBUG];
35+
$this->assertEqualsCanonicalizing([jsonEncode($message_expected_removed)], $difference);
36+
} finally {
37+
UnityHTTPD::clearMessages();
38+
}
39+
}
40+
}

test/phpunit-bootstrap.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function switchUser(
9696
ensure(!is_null($USER));
9797
}
9898

99-
function http_post(string $phpfile, array $post_data): void
99+
function http_post(string $phpfile, array $post_data, bool $enforce_PRG = true): void
100100
{
101101
global $LDAP,
102102
$SQL,
@@ -126,8 +126,10 @@ function http_post(string $phpfile, array $post_data): void
126126
unset($_POST);
127127
$_SERVER = $_PREVIOUS_SERVER;
128128
}
129-
// https://en.wikipedia.org/wiki/Post/Redirect/Get
130-
ensure($post_did_redirect_or_die, "post did not redirect or die!");
129+
if ($enforce_PRG) {
130+
// https://en.wikipedia.org/wiki/Post/Redirect/Get
131+
ensure($post_did_redirect_or_die, "post did not redirect or die!");
132+
}
131133
}
132134

133135
function http_get(string $phpfile, array $get_data = []): void

0 commit comments

Comments
 (0)