This commit is contained in:
Shish 2020-01-29 00:49:21 +00:00
parent e8a72b0291
commit 6d3ca01424
23 changed files with 95 additions and 55 deletions

View file

@ -260,7 +260,8 @@ class BasePage
* Find a block which contains the given text * Find a block which contains the given text
* (Useful for unit tests) * (Useful for unit tests)
*/ */
public function find_block(string $text): ?Block { public function find_block(string $text): ?Block
{
foreach ($this->blocks as $block) { foreach ($this->blocks as $block) {
if ($block->header == $text) { if ($block->header == $text) {
return $block; return $block;

View file

@ -46,7 +46,8 @@ class Database
*/ */
public $query_count = 0; public $query_count = 0;
public function __construct(string $dsn) { public function __construct(string $dsn)
{
$this->dsn = $dsn; $this->dsn = $dsn;
} }

View file

@ -3,7 +3,8 @@ require_once "core/block.php";
class BlockTest extends \PHPUnit\Framework\TestCase class BlockTest extends \PHPUnit\Framework\TestCase
{ {
public function test_basic() { public function test_basic()
{
$b = new Block("head", "body"); $b = new Block("head", "body");
$this->assertEquals( $this->assertEquals(
"<section id='headmain'><h3 data-toggle-sel='#headmain' class=''>head</h3><div class='blockbody'>body</div></section>\n", "<section id='headmain'><h3 data-toggle-sel='#headmain' class=''>head</h3><div class='blockbody'>body</div></section>\n",

View file

@ -97,14 +97,16 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(parse_shorthand_int("1231231231"), 1231231231); $this->assertEquals(parse_shorthand_int("1231231231"), 1231231231);
} }
public function test_autodate() { public function test_autodate()
{
$this->assertEquals( $this->assertEquals(
"<time datetime='2012-06-23T16:14:22+00:00'>June 23, 2012; 16:14</time>", "<time datetime='2012-06-23T16:14:22+00:00'>June 23, 2012; 16:14</time>",
autodate("2012-06-23 16:14:22") autodate("2012-06-23 16:14:22")
); );
} }
public function test_validate_input() { public function test_validate_input()
{
$_POST = [ $_POST = [
"foo" => " bar ", "foo" => " bar ",
"to_null" => " ", "to_null" => " ",
@ -195,7 +197,8 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase
); );
} }
public function test_stringer() { public function test_stringer()
{
$this->assertEquals( $this->assertEquals(
'["foo"=>"bar", "baz"=>[1, 2, 3], "qux"=>["a"=>"b"]]', '["foo"=>"bar", "baz"=>[1, 2, 3], "qux"=>["a"=>"b"]]',
stringer(["foo"=>"bar", "baz"=>[1,2,3], "qux"=>["a"=>"b"]]) stringer(["foo"=>"bar", "baz"=>[1,2,3], "qux"=>["a"=>"b"]])

View file

@ -468,7 +468,8 @@ function get_debug_info(): string
/** @privatesection */ /** @privatesection */
function require_all(array $files): void { function require_all(array $files): void
{
foreach ($files as $filename) { foreach ($files as $filename) {
if (basename($filename)[0] != "_") { if (basename($filename)[0] != "_") {
require_once $filename; require_once $filename;
@ -476,7 +477,8 @@ function require_all(array $files): void {
} }
} }
function _load_core_files() { function _load_core_files()
{
require_all(array_merge( require_all(array_merge(
zglob("core/*.php"), zglob("core/*.php"),
zglob("core/imageboard/*.php"), zglob("core/imageboard/*.php"),
@ -484,7 +486,8 @@ function _load_core_files() {
)); ));
} }
function _load_theme_files() { function _load_theme_files()
{
require_all(_get_themelet_files(get_theme())); require_all(_get_themelet_files(get_theme()));
} }

View file

@ -39,10 +39,12 @@ class AddAliasEvent extends Event
} }
} }
class DeleteAliasEvent extends Event { class DeleteAliasEvent extends Event
{
public $oldtag; public $oldtag;
public function __construct(string $oldtag) { public function __construct(string $oldtag)
{
$this->oldtag = $oldtag; $this->oldtag = $oldtag;
} }
} }
@ -142,7 +144,8 @@ class AliasEditor extends Extension
log_info("alias_editor", "Added alias for {$event->oldtag} -> {$event->newtag}", "Added alias"); log_info("alias_editor", "Added alias for {$event->oldtag} -> {$event->newtag}", "Added alias");
} }
public function onDeleteAlias(DeleteAliasEvent $event) { public function onDeleteAlias(DeleteAliasEvent $event)
{
global $database; global $database;
$database->execute("DELETE FROM aliases WHERE oldtag=:oldtag", ["oldtag" => $event->oldtag]); $database->execute("DELETE FROM aliases WHERE oldtag=:oldtag", ["oldtag" => $event->oldtag]);
log_info("alias_editor", "Deleted alias for {$event->oldtag}", "Deleted alias"); log_info("alias_editor", "Deleted alias for {$event->oldtag}", "Deleted alias");

View file

@ -21,7 +21,8 @@ class AliasEditorTest extends ShimmiePHPUnitTestCase
$this->assert_no_text("Add"); $this->assert_no_text("Add");
} }
public function testAliasOneToOne() { public function testAliasOneToOne()
{
$this->log_in_as_admin(); $this->log_in_as_admin();
$this->get_page("alias/export/aliases.csv"); $this->get_page("alias/export/aliases.csv");
@ -48,7 +49,8 @@ class AliasEditorTest extends ShimmiePHPUnitTestCase
$this->assert_no_text("test1"); $this->assert_no_text("test1");
} }
public function testAliasOneToMany() { public function testAliasOneToMany()
{
$this->log_in_as_admin(); $this->log_in_as_admin();
$this->get_page("alias/export/aliases.csv"); $this->get_page("alias/export/aliases.csv");

View file

@ -1,8 +1,10 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
class AutoCompleteTest extends ShimmiePHPUnitTestCase { class AutoCompleteTest extends ShimmiePHPUnitTestCase
public function testAuth() { {
public function testAuth()
{
send_event(new UserLoginEvent(User::by_name($this->anon_name))); send_event(new UserLoginEvent(User::by_name($this->anon_name)));
$page = $this->get_page('api/internal/autocomplete', ["s"=>"not-a-tag"]); $page = $this->get_page('api/internal/autocomplete', ["s"=>"not-a-tag"]);
$this->assertEquals(200, $page->code); $this->assertEquals(200, $page->code);

View file

@ -2,7 +2,8 @@
class BulkAddTest extends ShimmiePHPUnitTestCase class BulkAddTest extends ShimmiePHPUnitTestCase
{ {
public function testInvalidDir() { public function testInvalidDir()
{
send_event(new UserLoginEvent(User::by_name($this->admin_name))); send_event(new UserLoginEvent(User::by_name($this->admin_name)));
$bae = send_event(new BulkAddEvent('asdf')); $bae = send_event(new BulkAddEvent('asdf'));
$this->assertContains( $this->assertContains(
@ -12,7 +13,8 @@ class BulkAddTest extends ShimmiePHPUnitTestCase
); );
} }
public function testValidDir() { public function testValidDir()
{
send_event(new UserLoginEvent(User::by_name($this->admin_name))); send_event(new UserLoginEvent(User::by_name($this->admin_name)));
send_event(new BulkAddEvent('tests')); send_event(new BulkAddEvent('tests'));
$page = $this->get_page("post/list/hash=17fc89f372ed3636e28bd25cc7f3bac1/1"); $page = $this->get_page("post/list/hash=17fc89f372ed3636e28bd25cc7f3bac1/1");

View file

@ -23,8 +23,7 @@ class ImageBanTest extends ShimmiePHPUnitTestCase
try { try {
$this->post_image("tests/pbx_screenshot.jpg", "pbx"); $this->post_image("tests/pbx_screenshot.jpg", "pbx");
$this->assertTrue(false); $this->assertTrue(false);
} } catch (UploadException $e) {
catch(UploadException $e) {
$this->assertTrue(true); $this->assertTrue(true);
} }

View file

@ -4,13 +4,15 @@ class IPBanTest extends ShimmiePHPUnitTestCase
{ {
# FIXME: test that the IP is actually banned # FIXME: test that the IP is actually banned
public function testAccess() { public function testAccess()
{
$page = $this->get_page('ip_ban/list'); $page = $this->get_page('ip_ban/list');
$this->assertEquals(403, $page->code); $this->assertEquals(403, $page->code);
$this->assertEquals("Permission Denied", $page->title); $this->assertEquals("Permission Denied", $page->title);
} }
public function testIPBan() { public function testIPBan()
{
global $database; global $database;
$this->log_in_as_admin(); $this->log_in_as_admin();
@ -46,7 +48,8 @@ class IPBanTest extends ShimmiePHPUnitTestCase
); );
} }
public function test_all() { public function test_all()
{
$this->log_in_as_admin(); $this->log_in_as_admin();
$this->get_page('ip_ban/list?r_all=on'); // just test it doesn't crash for now $this->get_page('ip_ban/list?r_all=on'); // just test it doesn't crash for now
} }

View file

@ -1,7 +1,8 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
class PoolsTest extends ShimmiePHPUnitTestCase class PoolsTest extends ShimmiePHPUnitTestCase
{ {
public function testAnon() { public function testAnon()
{
$this->get_page('pool/list'); $this->get_page('pool/list');
$this->assert_title("Pools"); $this->assert_title("Pools");

View file

@ -1,5 +1,7 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
use function MicroHTML\{DIV,A,IMG}; use function MicroHTML\DIV;
use function MicroHTML\A;
use function MicroHTML\IMG;
class RandomImageTheme extends Themelet class RandomImageTheme extends Themelet
{ {

View file

@ -1,7 +1,8 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
class TagEditTest extends ShimmiePHPUnitTestCase class TagEditTest extends ShimmiePHPUnitTestCase
{ {
public function testValidChange() { public function testValidChange()
{
$this->log_in_as_user(); $this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx"); $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
$image = Image::by_id($image_id); $image = Image::by_id($image_id);
@ -25,8 +26,7 @@ class TagEditTest extends ShimmiePHPUnitTestCase
try { try {
send_event(new TagSetEvent($image, [])); send_event(new TagSetEvent($image, []));
$this->assertTrue(false); $this->assertTrue(false);
} } catch (SCoreException $e) {
catch(SCoreException $e) {
$this->assertEquals("Tried to set zero tags", $e->error); $this->assertEquals("Tried to set zero tags", $e->error);
} }
} }

View file

@ -1,10 +1,12 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
class CreateTipEvent extends Event { class CreateTipEvent extends Event
{
public $enable; public $enable;
public $image; public $image;
public $text; public $text;
public function __construct(bool $enable, string $image, string $text) { public function __construct(bool $enable, string $image, string $text)
{
parent::__construct(); parent::__construct();
$this->enable = $enable; $this->enable = $enable;
$this->image = $image; $this->image = $image;
@ -12,9 +14,11 @@ class CreateTipEvent extends Event {
} }
} }
class DeleteTipEvent extends Event { class DeleteTipEvent extends Event
{
public $tip_id; public $tip_id;
public function __construct(int $tip_id) { public function __construct(int $tip_id)
{
parent::__construct(); parent::__construct();
$this->tip_id = $tip_id; $this->tip_id = $tip_id;
} }
@ -121,7 +125,8 @@ class Tips extends Extension
$this->theme->manageTips($url, $images); $this->theme->manageTips($url, $images);
} }
public function onCreateTip(CreateTipEvent $event) { public function onCreateTip(CreateTipEvent $event)
{
global $database; global $database;
$database->execute( $database->execute(
" "
@ -176,7 +181,8 @@ class Tips extends Extension
$database->execute("UPDATE tips SET enable = :enable WHERE id = :id", ["enable"=>$enable, "id"=>$tipID]); $database->execute("UPDATE tips SET enable = :enable WHERE id = :id", ["enable"=>$enable, "id"=>$tipID]);
} }
public function onDeleteTip(DeleteTipEvent $event) { public function onDeleteTip(DeleteTipEvent $event)
{
global $database; global $database;
$database->execute("DELETE FROM tips WHERE id = :id", ["id"=>$event->tip_id]); $database->execute("DELETE FROM tips WHERE id = :id", ["id"=>$event->tip_id]);
} }

View file

@ -42,8 +42,7 @@ class UploadTest extends ShimmiePHPUnitTestCase
try { try {
$this->post_image("data/huge.jpg", "test"); $this->post_image("data/huge.jpg", "test");
$this->assertTrue(false, "Uploading huge.jpg didn't fail..."); $this->assertTrue(false, "Uploading huge.jpg didn't fail...");
} } catch (UploadException $e) {
catch (UploadException $e) {
$this->assertEquals("File too large (3.0MB > 1.0MB)", $e->error); $this->assertEquals("File too large (3.0MB > 1.0MB)", $e->error);
} }
unlink("data/huge.jpg"); unlink("data/huge.jpg");

View file

@ -15,21 +15,25 @@ class WikiUpdateEvent extends Event
} }
} }
class WikiDeleteRevisionEvent extends Event { class WikiDeleteRevisionEvent extends Event
{
public $title; public $title;
public $revision; public $revision;
public function __construct($title, $revision) { public function __construct($title, $revision)
{
parent::__construct(); parent::__construct();
$this->title = $title; $this->title = $title;
$this->revision = $revision; $this->revision = $revision;
} }
} }
class WikiDeletePageEvent extends Event { class WikiDeletePageEvent extends Event
{
public $title; public $title;
public function __construct($title) { public function __construct($title)
{
parent::__construct(); parent::__construct();
$this->title = $title; $this->title = $title;
} }
@ -216,7 +220,8 @@ class Wiki extends Extension
} }
} }
public function onWikiDeleteRevision(WikiDeleteRevisionEvent $event) { public function onWikiDeleteRevision(WikiDeleteRevisionEvent $event)
{
global $database; global $database;
$database->Execute( $database->Execute(
"DELETE FROM wiki_pages WHERE title=:title AND revision=:rev", "DELETE FROM wiki_pages WHERE title=:title AND revision=:rev",
@ -224,7 +229,8 @@ class Wiki extends Extension
); );
} }
public function onWikiDeletePage(WikiDeletePageEvent $event) { public function onWikiDeletePage(WikiDeletePageEvent $event)
{
global $database; global $database;
$database->Execute( $database->Execute(
"DELETE FROM wiki_pages WHERE title=:title", "DELETE FROM wiki_pages WHERE title=:title",

View file

@ -7,7 +7,9 @@ require_once "core/polyfills.php";
require_once "core/util.php"; require_once "core/util.php";
$_SERVER['QUERY_STRING'] = '/'; $_SERVER['QUERY_STRING'] = '/';
if(file_exists("tests/trace.json")) unlink("tests/trace.json"); if (file_exists("tests/trace.json")) {
unlink("tests/trace.json");
}
global $cache, $config, $database, $user, $page, $_tracer; global $cache, $config, $database, $user, $page, $_tracer;
_sanitise_environment(); _sanitise_environment();
@ -155,11 +157,9 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase
} }
} }
return $text; return $text;
} } elseif ($page->mode == PageMode::DATA) {
elseif($page->mode == PageMode::DATA) {
return $page->data; return $page->data;
} } else {
else {
$this->assertTrue(false, "Page mode is not PAGE or DATA"); $this->assertTrue(false, "Page mode is not PAGE or DATA");
} }
} }

View file

@ -1,2 +1,4 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
class Page extends BasePage {} class Page extends BasePage
{
}

View file

@ -1,2 +1,4 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
class Page extends BasePage {} class Page extends BasePage
{
}

View file

@ -1,2 +1,4 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
class Page extends BasePage {} class Page extends BasePage
{
}