2015-08-09 11:14:28 +00:00
|
|
|
<?php
|
2020-01-27 19:28:58 +00:00
|
|
|
chdir(dirname(dirname(__FILE__)));
|
|
|
|
require_once "vendor/autoload.php";
|
|
|
|
require_once "tests/defines.php";
|
|
|
|
require_once "core/sys_config.php";
|
|
|
|
require_once "core/polyfills.php";
|
|
|
|
require_once "core/util.php";
|
2015-08-09 11:14:28 +00:00
|
|
|
|
|
|
|
$_SERVER['QUERY_STRING'] = '/';
|
2020-01-28 23:57:43 +00:00
|
|
|
if(file_exists("tests/trace.json")) unlink("tests/trace.json");
|
2015-08-09 11:14:28 +00:00
|
|
|
|
2020-01-27 19:28:58 +00:00
|
|
|
global $cache, $config, $database, $user, $page, $_tracer;
|
|
|
|
_sanitise_environment();
|
2020-01-28 23:57:43 +00:00
|
|
|
$tracer_enabled = true;
|
|
|
|
$_tracer->begin("bootstrap");
|
2020-01-27 19:28:58 +00:00
|
|
|
_load_core_files();
|
|
|
|
$cache = new Cache(CACHE_DSN);
|
2020-01-27 19:40:14 +00:00
|
|
|
$dsn = getenv("DSN");
|
|
|
|
$database = new Database($dsn ? $dsn : "sqlite::memory:");
|
2020-01-27 19:28:58 +00:00
|
|
|
create_dirs();
|
|
|
|
create_tables($database);
|
|
|
|
$config = new DatabaseConfig($database);
|
|
|
|
ExtensionInfo::load_all_extension_info();
|
|
|
|
Extension::determine_enabled_extensions();
|
|
|
|
require_all(zglob("ext/{".Extension::get_enabled_extensions_as_string()."}/main.php"));
|
|
|
|
_load_theme_files();
|
|
|
|
$page = new Page();
|
|
|
|
_load_event_listeners();
|
2020-01-28 22:23:03 +00:00
|
|
|
$config->set_string("thumb_engine", "static"); # GD has less overhead per-call
|
2020-01-27 19:52:54 +00:00
|
|
|
send_event(new DatabaseUpgradeEvent());
|
2020-01-27 19:05:43 +00:00
|
|
|
send_event(new InitExtEvent());
|
2020-01-28 23:57:43 +00:00
|
|
|
$_tracer->end();
|
2015-08-09 11:14:28 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase
|
|
|
|
{
|
2020-01-28 21:19:59 +00:00
|
|
|
protected $anon_name = "anonymous";
|
|
|
|
protected $admin_name = "demo";
|
|
|
|
protected $user_name = "test";
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-11-14 18:24:09 +00:00
|
|
|
public function setUp(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-29 00:37:31 +00:00
|
|
|
global $database, $_tracer;
|
2020-01-28 23:57:43 +00:00
|
|
|
$_tracer->begin($this->getName());
|
2020-01-28 21:19:59 +00:00
|
|
|
$_tracer->begin("setUp");
|
2019-05-28 16:59:38 +00:00
|
|
|
$class = str_replace("Test", "", get_class($this));
|
2020-01-28 21:19:59 +00:00
|
|
|
if (!ExtensionInfo::get_for_extension_class($class)->is_supported()) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->markTestSkipped("$class not supported with this database");
|
|
|
|
}
|
|
|
|
|
2020-01-28 21:19:59 +00:00
|
|
|
$this->create_user($this->admin_name);
|
|
|
|
$this->create_user($this->user_name);
|
2020-01-27 18:24:11 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// things to do after bootstrap and before request
|
|
|
|
// log in as anon
|
|
|
|
$this->log_out();
|
2020-01-28 21:19:59 +00:00
|
|
|
|
2020-01-29 00:37:31 +00:00
|
|
|
$_tracer->begin("tearDown");
|
|
|
|
foreach ($database->get_col("SELECT id FROM images") as $image_id) {
|
|
|
|
send_event(new ImageDeletionEvent(Image::by_id($image_id)));
|
|
|
|
}
|
|
|
|
$_tracer->end();
|
|
|
|
|
2020-01-28 21:19:59 +00:00
|
|
|
$_tracer->end();
|
2020-01-28 23:57:43 +00:00
|
|
|
$_tracer->begin("test");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 18:24:09 +00:00
|
|
|
public function tearDown(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-28 21:19:59 +00:00
|
|
|
global $_tracer;
|
|
|
|
$_tracer->end();
|
2020-01-28 23:57:43 +00:00
|
|
|
$_tracer->end();
|
2020-01-28 21:19:59 +00:00
|
|
|
$_tracer->clear();
|
|
|
|
$_tracer->flush("tests/trace.json");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 18:24:11 +00:00
|
|
|
protected function create_user(string $name)
|
|
|
|
{
|
|
|
|
if (is_null(User::by_name($name))) {
|
|
|
|
$userPage = new UserPage();
|
|
|
|
$userPage->onUserCreation(new UserCreationEvent($name, $name, ""));
|
|
|
|
assert(!is_null(User::by_name($name)), "Creation of user $name failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
protected function get_page($page_name, $args=null)
|
|
|
|
{
|
|
|
|
// use a fresh page
|
|
|
|
global $page;
|
|
|
|
if (!$args) {
|
|
|
|
$args = [];
|
|
|
|
}
|
|
|
|
$_GET = $args;
|
|
|
|
$_POST = [];
|
2020-01-28 21:19:59 +00:00
|
|
|
$page = new Page();
|
2019-05-28 16:59:38 +00:00
|
|
|
send_event(new PageRequestEvent($page_name));
|
2019-06-19 01:58:28 +00:00
|
|
|
if ($page->mode == PageMode::REDIRECT) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->code = 302;
|
|
|
|
}
|
2020-01-28 21:19:59 +00:00
|
|
|
return $page;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function post_page($page_name, $args=null)
|
|
|
|
{
|
|
|
|
// use a fresh page
|
|
|
|
global $page;
|
|
|
|
if (!$args) {
|
|
|
|
$args = [];
|
|
|
|
}
|
2020-01-26 16:38:26 +00:00
|
|
|
foreach ($args as $k=>$v) {
|
2020-01-26 13:19:35 +00:00
|
|
|
$args[$k] = (string)$v;
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
$_GET = [];
|
|
|
|
$_POST = $args;
|
2020-01-28 21:19:59 +00:00
|
|
|
$page = new Page();
|
2019-05-28 16:59:38 +00:00
|
|
|
send_event(new PageRequestEvent($page_name));
|
2019-06-19 01:58:28 +00:00
|
|
|
if ($page->mode == PageMode::REDIRECT) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->code = 302;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// page things
|
|
|
|
protected function assert_title(string $title)
|
|
|
|
{
|
|
|
|
global $page;
|
2019-11-21 17:16:11 +00:00
|
|
|
$this->assertStringContainsString($title, $page->title);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 13:19:35 +00:00
|
|
|
protected function assert_title_matches($title)
|
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
$this->assertStringMatchesFormat($title, $page->title);
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
protected function assert_no_title(string $title)
|
|
|
|
{
|
|
|
|
global $page;
|
2019-11-21 17:18:43 +00:00
|
|
|
$this->assertStringNotContainsString($title, $page->title);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function assert_response(int $code)
|
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
$this->assertEquals($code, $page->code);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function page_to_text(string $section=null)
|
|
|
|
{
|
|
|
|
global $page;
|
2020-01-28 21:19:59 +00:00
|
|
|
if($page->mode == PageMode::PAGE) {
|
|
|
|
$text = $page->title . "\n";
|
|
|
|
foreach ($page->blocks as $block) {
|
|
|
|
if (is_null($section) || $section == $block->section) {
|
|
|
|
$text .= $block->header . "\n";
|
|
|
|
$text .= $block->body . "\n\n";
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-01-28 21:19:59 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
elseif($page->mode == PageMode::DATA) {
|
|
|
|
return $page->data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->assertTrue(false, "Page mode is not PAGE or DATA");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function assert_text(string $text, string $section=null)
|
|
|
|
{
|
2019-11-21 17:16:11 +00:00
|
|
|
$this->assertStringContainsString($text, $this->page_to_text($section));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function assert_no_text(string $text, string $section=null)
|
|
|
|
{
|
2019-11-21 17:18:43 +00:00
|
|
|
$this->assertStringNotContainsString($text, $this->page_to_text($section));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function assert_content(string $content)
|
|
|
|
{
|
|
|
|
global $page;
|
2019-11-21 17:16:11 +00:00
|
|
|
$this->assertStringContainsString($content, $page->data);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function assert_no_content(string $content)
|
|
|
|
{
|
|
|
|
global $page;
|
2019-11-21 17:18:43 +00:00
|
|
|
$this->assertStringNotContainsString($content, $page->data);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// user things
|
|
|
|
protected function log_in_as_admin()
|
|
|
|
{
|
2020-01-28 21:19:59 +00:00
|
|
|
send_event(new UserLoginEvent(User::by_name($this->admin_name)));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function log_in_as_user()
|
|
|
|
{
|
2020-01-28 21:19:59 +00:00
|
|
|
send_event(new UserLoginEvent(User::by_name($this->user_name)));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function log_out()
|
|
|
|
{
|
2020-01-28 21:19:59 +00:00
|
|
|
global $config;
|
2019-05-28 16:59:38 +00:00
|
|
|
$user = User::by_id($config->get_int("anon_id", 0));
|
|
|
|
$this->assertNotNull($user);
|
2019-07-04 17:48:33 +00:00
|
|
|
send_event(new UserLoginEvent($user));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// post things
|
|
|
|
protected function post_image(string $filename, string $tags): int
|
|
|
|
{
|
|
|
|
$dae = new DataUploadEvent($filename, [
|
|
|
|
"filename" => $filename,
|
|
|
|
"extension" => pathinfo($filename, PATHINFO_EXTENSION),
|
|
|
|
"tags" => Tag::explode($tags),
|
|
|
|
"source" => null,
|
|
|
|
]);
|
|
|
|
send_event($dae);
|
|
|
|
return $dae->image_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function delete_image(int $image_id)
|
|
|
|
{
|
|
|
|
$img = Image::by_id($image_id);
|
|
|
|
if ($img) {
|
2019-06-27 18:34:25 +00:00
|
|
|
$ide = new ImageDeletionEvent($img, true);
|
2019-05-28 16:59:38 +00:00
|
|
|
send_event($ide);
|
|
|
|
}
|
|
|
|
}
|
2015-08-09 11:14:28 +00:00
|
|
|
}
|