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

161 lines
3.7 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__)));
2015-08-09 11:14:28 +00:00
require_once "core/_bootstrap.inc.php";
if(is_null(User::by_name("demo"))) {
$userPage = new UserPage();
$userPage->onUserCreation(new UserCreationEvent("demo", "demo", ""));
$userPage->onUserCreation(new UserCreationEvent("test", "test", ""));
}
2017-03-09 09:31:41 +00:00
abstract class ShimmiePHPUnitTestCase extends \PHPUnit_Framework_TestCase {
2015-08-09 11:14:28 +00:00
protected $backupGlobalsBlacklist = array('database', 'config');
private $images = array();
public function setUp() {
$class = str_replace("Test", "", get_class($this));
2016-06-17 22:41:57 +00:00
if(!class_exists($class)) {
$this->markTestSkipped("$class not loaded");
}
elseif(!ext_is_live($class)) {
$this->markTestSkipped("$class not supported with this database");
}
2015-08-09 11:14:28 +00:00
// things to do after bootstrap and before request
// log in as anon
$this->log_out();
}
public function tearDown() {
foreach($this->images as $image_id) {
$this->delete_image($image_id);
}
}
2015-09-12 11:24:18 +00:00
protected function get_page($page_name, $args=null) {
2015-08-09 11:14:28 +00:00
// use a fresh page
global $page;
2015-09-12 11:24:18 +00:00
if(!$args) $args = array();
$_GET = $args;
2015-08-09 11:14:28 +00:00
$page = class_exists("CustomPage") ? new CustomPage() : new Page();
send_event(new PageRequestEvent($page_name));
2016-06-06 10:57:54 +00:00
if($page->mode == "redirect") {
$page->code = 302;
}
2015-08-09 11:14:28 +00:00
}
// page things
protected function assert_title($title) {
global $page;
2016-06-06 10:57:54 +00:00
$this->assertContains($title, $page->title);
}
protected function assert_no_title($title) {
global $page;
$this->assertNotContains($title, $page->title);
2015-08-09 11:14:28 +00:00
}
/**
* @param integer $code
*/
2015-08-09 11:14:28 +00:00
protected function assert_response($code) {
global $page;
$this->assertEquals($code, $page->code);
}
2015-08-23 15:09:52 +00:00
protected function page_to_text($section=null) {
2015-08-09 11:14:28 +00:00
global $page;
2016-06-06 10:57:54 +00:00
$text = $page->title . "\n";
2015-08-09 11:14:28 +00:00
foreach($page->blocks as $block) {
2015-09-12 11:24:18 +00:00
if(is_null($section) || $section == $block->section) {
$text .= $block->header . "\n";
$text .= $block->body . "\n\n";
}
2015-08-09 11:14:28 +00:00
}
2015-08-23 15:09:52 +00:00
return $text;
2015-08-09 11:14:28 +00:00
}
2015-08-23 15:09:52 +00:00
protected function assert_text($text, $section=null) {
$this->assertContains($text, $this->page_to_text($section));
2015-08-09 11:14:28 +00:00
}
/**
* @param string $text
*/
2015-08-23 15:09:52 +00:00
protected function assert_no_text($text, $section=null) {
2015-09-12 11:24:18 +00:00
$this->assertNotContains($text, $this->page_to_text($section));
2015-08-23 15:09:52 +00:00
}
/**
* @param string $content
*/
2015-08-23 15:09:52 +00:00
protected function assert_content($content) {
2015-09-12 11:24:18 +00:00
global $page;
$this->assertContains($content, $page->data);
2015-08-23 15:09:52 +00:00
}
/**
* @param string $content
*/
2015-08-23 15:09:52 +00:00
protected function assert_no_content($content) {
2015-09-12 11:24:18 +00:00
global $page;
2015-08-23 15:09:52 +00:00
$this->assertNotContains($content, $page->data);
2015-08-09 11:14:28 +00:00
}
// user things
protected function log_in_as_admin() {
global $user;
$user = User::by_name('demo');
$this->assertNotNull($user);
}
protected function log_in_as_user() {
global $user;
$user = User::by_name('test');
$this->assertNotNull($user);
}
protected function log_out() {
global $user, $config;
$user = User::by_id($config->get_int("anon_id", 0));
$this->assertNotNull($user);
}
// post things
/**
* @param string $filename
* @param string $tags
2015-08-09 11:14:28 +00:00
* @return int
*/
protected function post_image($filename, $tags) {
$dae = new DataUploadEvent($filename, array(
2015-09-20 20:20:28 +00:00
"filename" => $filename,
"extension" => pathinfo($filename, PATHINFO_EXTENSION),
2016-07-30 22:08:08 +00:00
"tags" => Tag::explode($tags),
2015-09-20 20:20:28 +00:00
"source" => null,
2015-08-09 11:14:28 +00:00
));
send_event($dae);
$this->images[] = $dae->image_id;
return $dae->image_id;
}
/**
* @param int $image_id
*/
protected function delete_image($image_id) {
$img = Image::by_id($image_id);
if($img) {
$ide = new ImageDeletionEvent($img);
send_event($ide);
}
}
}