more tests

This commit is contained in:
Shish 2024-02-11 14:51:55 +00:00
parent 72474b18dc
commit 278286da9f
8 changed files with 89 additions and 24 deletions

View file

@ -52,4 +52,12 @@ class BasePageTest extends TestCase
ob_end_clean(); ob_end_clean();
$this->assertTrue(true); // doesn't crash $this->assertTrue(true); // doesn't crash
} }
public function test_subNav(): void
{
// the default theme doesn't send this, so let's have
// a random test manually
send_event(new PageSubNavBuildingEvent("system"));
$this->assertTrue(true); // doesn't crash
}
} }

View file

@ -154,6 +154,15 @@ class AdminPage extends Extension
}); });
} }
public function onAdminAction(AdminActionEvent $event): void
{
global $page;
if ($event->action === "test") {
$page->set_mode(PageMode::DATA);
$page->set_data("test");
}
}
public function onAdminBuilding(AdminBuildingEvent $event): void public function onAdminBuilding(AdminBuildingEvent $event): void
{ {
$this->theme->display_page(); $this->theme->display_page();

View file

@ -8,19 +8,34 @@ class AdminPageTest extends ShimmiePHPUnitTestCase
{ {
public function testAuth(): void public function testAuth(): void
{ {
send_event(new UserLoginEvent(User::by_name(self::$anon_name))); $this->log_out();
$this->assertException(PermissionDeniedException::class, function () { $this->assertException(PermissionDeniedException::class, function () {
$this->get_page('admin'); $this->get_page('admin');
}); });
send_event(new UserLoginEvent(User::by_name(self::$user_name))); $this->log_in_as_user();
$this->assertException(PermissionDeniedException::class, function () { $this->assertException(PermissionDeniedException::class, function () {
$this->get_page('admin'); $this->get_page('admin');
}); });
send_event(new UserLoginEvent(User::by_name(self::$admin_name))); $this->log_in_as_admin();
$page = $this->get_page('admin'); $page = $this->get_page('admin');
$this->assertEquals(200, $page->code); $this->assertEquals(200, $page->code);
$this->assertEquals("Admin Tools", $page->title); $this->assertEquals("Admin Tools", $page->title);
} }
public function testAct(): void
{
$this->log_in_as_admin();
$page = $this->post_page('admin/test');
$this->assertEquals("test", $page->data);
}
// does this belong here??
public function testCliGen(): void
{
$app = new CliApp();
send_event(new CliGenEvent($app));
$this->assertTrue(true); // TODO: check for more than "no crash"?
}
} }

View file

@ -52,7 +52,7 @@ class DeleteAliasEvent extends Event
} }
} }
class AddAliasException extends SCoreException class AddAliasException extends UserErrorException
{ {
} }
@ -67,13 +67,9 @@ class AliasEditor extends Extension
if ($event->page_matches("alias/add", method: "POST", permission: Permissions::MANAGE_ALIAS_LIST)) { if ($event->page_matches("alias/add", method: "POST", permission: Permissions::MANAGE_ALIAS_LIST)) {
$input = validate_input(["c_oldtag" => "string", "c_newtag" => "string"]); $input = validate_input(["c_oldtag" => "string", "c_newtag" => "string"]);
try { send_event(new AddAliasEvent($input['c_oldtag'], $input['c_newtag']));
send_event(new AddAliasEvent($input['c_oldtag'], $input['c_newtag'])); $page->set_mode(PageMode::REDIRECT);
$page->set_mode(PageMode::REDIRECT); $page->set_redirect(make_link("alias/list"));
$page->set_redirect(make_link("alias/list"));
} catch (AddAliasException $ex) {
$this->theme->display_error(500, "Error adding alias", $ex->getMessage());
}
} }
if ($event->page_matches("alias/remove", method: "POST", permission: Permissions::MANAGE_ALIAS_LIST)) { if ($event->page_matches("alias/remove", method: "POST", permission: Permissions::MANAGE_ALIAS_LIST)) {
$input = validate_input(["d_oldtag" => "string"]); $input = validate_input(["d_oldtag" => "string"]);

View file

@ -33,7 +33,7 @@ class AliasEditorTest extends ShimmiePHPUnitTestCase
$this->get_page("alias/export/aliases.csv"); $this->get_page("alias/export/aliases.csv");
$this->assert_no_text("test1"); $this->assert_no_text("test1");
send_event(new AddAliasEvent("test1", "test2")); $this->post_page('alias/add', ['c_oldtag' => 'test1', 'c_newtag' => 'test2']);
$this->get_page('alias/list'); $this->get_page('alias/list');
$this->assert_text("test1"); $this->assert_text("test1");
$this->get_page("alias/export/aliases.csv"); $this->get_page("alias/export/aliases.csv");
@ -48,7 +48,7 @@ class AliasEditorTest extends ShimmiePHPUnitTestCase
$this->assert_response(302); $this->assert_response(302);
$this->delete_image($image_id); $this->delete_image($image_id);
send_event(new DeleteAliasEvent("test1")); $this->post_page('alias/remove', ['d_oldtag' => 'test1']);
$this->get_page('alias/list'); $this->get_page('alias/list');
$this->assert_title("Alias List"); $this->assert_title("Alias List");
$this->assert_no_text("test1"); $this->assert_no_text("test1");

View file

@ -57,7 +57,8 @@ class Approval extends Extension
public function onSetupBuilding(SetupBuildingEvent $event): void public function onSetupBuilding(SetupBuildingEvent $event): void
{ {
$this->theme->display_admin_block($event); $sb = $event->panel->create_new_block("Approval");
$sb->add_bool_option(ApprovalConfig::IMAGES, "Posts: ");
} }
public function onAdminBuilding(AdminBuildingEvent $event): void public function onAdminBuilding(AdminBuildingEvent $event): void
@ -89,7 +90,6 @@ class Approval extends Extension
); );
break; break;
default: default:
break; break;
} }
} }
@ -99,7 +99,7 @@ class Approval extends Extension
{ {
global $page; global $page;
if (!$this->check_permissions(($event->image))) { if (!$this->check_permissions($event->image)) {
$page->set_mode(PageMode::REDIRECT); $page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link()); $page->set_redirect(make_link());
} }
@ -207,7 +207,7 @@ class Approval extends Extension
* Deny images upon insufficient permissions. * Deny images upon insufficient permissions.
**/ **/
if (!$this->check_permissions($event->image)) { if (!$this->check_permissions($event->image)) {
throw new SCoreException("Access denied"); throw new PermissionDeniedException("Access denied");
} }
} }

43
ext/approval/test.php Normal file
View file

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Shimmie2;
class ApprovalTest extends ShimmiePHPUnitTestCase
{
public function testNoApprovalNeeded(): void
{
$this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "some_tag");
$this->assert_search_results(["some_tag"], [$image_id]);
}
public function testApprovalNeeded(): void
{
global $config;
$config->set_bool(ApprovalConfig::IMAGES, true);
// use can post but not see what they posted
$this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "some_tag");
$this->assert_search_results(["some_tag"], []);
// admin can approve
$this->log_in_as_admin();
$this->assert_search_results(["some_tag"], []);
$this->post_page("approve_image/$image_id");
$this->assert_search_results(["some_tag"], [$image_id]);
// use then sees the image
$this->log_in_as_user();
$this->assert_search_results(["some_tag"], [$image_id]);
}
public function tearDown(): void
{
global $config;
$config->set_bool(ApprovalConfig::IMAGES, false);
parent::tearDown();
}
}

View file

@ -8,7 +8,7 @@ use MicroHTML\HTMLElement;
use function MicroHTML\emptyHTML; use function MicroHTML\emptyHTML;
use function MicroHTML\{BUTTON,INPUT,P}; use function MicroHTML\{BUTTON,P};
class ApprovalTheme extends Themelet class ApprovalTheme extends Themelet
{ {
@ -21,12 +21,6 @@ class ApprovalTheme extends Themelet
); );
} }
public function display_admin_block(SetupBuildingEvent $event): void
{
$sb = $event->panel->create_new_block("Approval");
$sb->add_bool_option(ApprovalConfig::IMAGES, "Posts: ");
}
public function display_admin_form(): void public function display_admin_form(): void
{ {
global $page; global $page;