This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/tests/bootstrap.php

193 lines
5.2 KiB
PHP
Raw Normal View History

2015-08-09 11:14:28 +00:00
<?php
2015-08-23 15:09:52 +00:00
define("UNITTEST", true);
2015-08-09 11:14:28 +00:00
define("TIMEZONE", 'UTC');
define("EXTRA_EXTS", str_replace("ext/", "", implode(',', glob('ext/*'))));
define("BASE_HREF", "/");
define("CLI_LOG_LEVEL", 50);
$_SERVER['QUERY_STRING'] = '/';
2015-09-12 13:49:53 +00:00
chdir(dirname(dirname(__FILE__)));
require_once "core/_bootstrap.php";
2015-08-09 11:14:28 +00:00
abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase
{
private $images = [];
2019-11-14 18:24:09 +00:00
public function setUp(): void
{
$class = str_replace("Test", "", get_class($this));
if (!class_exists($class)) {
$this->markTestSkipped("$class not loaded");
2019-08-07 21:36:24 +00:00
} elseif (!ExtensionInfo::get_for_extension_class($class)->is_supported()) {
$this->markTestSkipped("$class not supported with this database");
}
2020-01-27 18:24:11 +00:00
$this->create_user("demo");
$this->create_user("test");
// things to do after bootstrap and before request
// log in as anon
$this->log_out();
}
2019-11-14 18:24:09 +00:00
public function tearDown(): void
{
foreach ($this->images as $image_id) {
$this->delete_image($image_id);
}
}
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");
}
}
protected function get_page($page_name, $args=null)
{
// use a fresh page
global $page;
if (!$args) {
$args = [];
}
$_GET = $args;
$_POST = [];
$page = class_exists("CustomPage") ? new CustomPage() : new Page();
send_event(new PageRequestEvent($page_name));
2019-06-19 01:58:28 +00:00
if ($page->mode == PageMode::REDIRECT) {
$page->code = 302;
}
}
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;
}
$_GET = [];
$_POST = $args;
$page = class_exists("CustomPage") ? new CustomPage() : new Page();
send_event(new PageRequestEvent($page_name));
2019-06-19 01:58:28 +00:00
if ($page->mode == PageMode::REDIRECT) {
$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);
}
2020-01-26 13:19:35 +00:00
protected function assert_title_matches($title)
{
global $page;
$this->assertStringMatchesFormat($title, $page->title);
}
protected function assert_no_title(string $title)
{
global $page;
2019-11-21 17:18:43 +00:00
$this->assertStringNotContainsString($title, $page->title);
}
protected function assert_response(int $code)
{
global $page;
$this->assertEquals($code, $page->code);
}
protected function page_to_text(string $section=null)
{
global $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";
}
}
return $text;
}
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));
}
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));
}
protected function assert_content(string $content)
{
global $page;
2019-11-21 17:16:11 +00:00
$this->assertStringContainsString($content, $page->data);
}
protected function assert_no_content(string $content)
{
global $page;
2019-11-21 17:18:43 +00:00
$this->assertStringNotContainsString($content, $page->data);
}
// user things
protected function log_in_as_admin()
{
global $user;
$user = User::by_name('demo');
$this->assertNotNull($user);
2019-07-04 17:48:33 +00:00
send_event(new UserLoginEvent($user));
}
protected function log_in_as_user()
{
global $user;
$user = User::by_name('test');
$this->assertNotNull($user);
2019-07-04 17:48:33 +00:00
send_event(new UserLoginEvent($user));
}
protected function log_out()
{
global $user, $config;
$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));
}
// 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);
$this->images[] = $dae->image_id;
return $dae->image_id;
}
protected function delete_image(int $image_id)
{
$img = Image::by_id($image_id);
if ($img) {
$ide = new ImageDeletionEvent($img, true);
send_event($ide);
}
}
2015-08-09 11:14:28 +00:00
}