more phpunit-ing

This commit is contained in:
Shish 2015-08-23 16:09:52 +01:00
parent 2d622cf908
commit ec484c1144
11 changed files with 130 additions and 81 deletions

View file

@ -1282,27 +1282,28 @@ function move_upload_to_archive(DataUploadEvent $event) {
* Add a directory full of images
*
* @param $base string
* @return string
* @return array
*/
function add_dir(/*string*/ $base) {
$list = "";
$results = array();
foreach(list_files($base) as $full_path) {
$short_path = str_replace($base, "", $full_path);
$filename = basename($full_path);
$tags = path_to_tags($short_path);
$list .= "<br>".html_escape("$short_path (".str_replace(" ", ", ", $tags).")... ");
$result = "$short_path (".str_replace(" ", ", ", $tags).")... ";
try {
add_image($full_path, $filename, $tags);
$list .= "ok\n";
$result .= "ok";
}
catch(UploadException $ex) {
$list .= "failed: ".$ex->getMessage()."\n";
$result .= "failed: ".$ex->getMessage();
}
$results[] = $result;
}
return $list;
return $results;
}
/**

View file

@ -1,13 +1,13 @@
<?php
/**
* \page themes Themes
*
*
* Each extension has a theme with a specific name -- eg. the extension Setup
* which is stored in ext/setup/main.php will have a theme called SetupTheme
* stored in ext/setup/theme.php. If you want to customise it, create a class
* in the file themes/mytheme/setup.theme.php called CustomSetupTheme which
* extends SetupTheme and overrides some of its methods.
*
*
* Generally an extension should only deal with processing data; whenever it
* wants to display something, it should pass the data to be displayed to the
* theme object, and the theme will add the data into the global $page
@ -65,11 +65,11 @@ class Page {
/** @name "data" mode */
//@{
/** @var string */
private $data = "";
/** @var string; public only for unit test */
public $data = "";
/** @var string */
private $filename = null;
/** @var string; public only for unit test */
public $filename = null;
/**
* Set the raw data to be sent.
@ -226,7 +226,7 @@ class Page {
}
return $data;
}
/**
* Removes all currently set HTML headers (Be careful..).
*/
@ -251,7 +251,8 @@ class Page {
*/
public function display() {
global $page, $user;
header("HTTP/1.0 {$this->code} Shimmie");
header("Content-type: ".$this->type);
header("X-Powered-By: SCore-".SCORE_VERSION);
@ -268,7 +269,6 @@ class Page {
switch($this->mode) {
case "page":
header("HTTP/1.0 {$this->code} Shimmie");
if(CACHE_HTTP) {
header("Vary: Cookie, Accept-Encoding");
if($user->is_anonymous() && $_SERVER["REQUEST_METHOD"] == "GET") {
@ -309,16 +309,16 @@ class Page {
break;
}
}
/**
* This function grabs all the CSS and JavaScript files sprinkled throughout Shimmie's folders,
* concatenates them together into two large files (one for CSS and one for JS) and then stores
* them in the /cache/ directory for serving to the user.
*
*
* Why do this? Two reasons:
* 1. Reduces the number of files the user's browser needs to download.
* 2. Allows these cached files to be compressed/minified by the admin.
*
*
* TODO: This should really be configurable somehow...
*/
public function add_auto_html_headers() {
@ -378,4 +378,3 @@ class Page {
class MockPage extends Page {
}

View file

@ -15,21 +15,25 @@
* <p><b>Note:</b> requires the "admin" extension to be enabled
*/
class BulkAddEvent extends Event {
public $dir, $results;
public function __construct($dir) {
$this->dir = $dir;
$this->results = array();
}
}
class BulkAdd extends Extension {
public function onPageRequest(PageRequestEvent $event) {
global $page, $user;
if($event->page_matches("bulk_add")) {
if($user->is_admin() && $user->check_auth_token() && isset($_POST['dir'])) {
set_time_limit(0);
$list = add_dir($_POST['dir']);
if(strlen($list) > 0) {
$this->theme->add_status("Adding files", $list);
} else {
if(is_dir($_POST['dir'])) {
$this->theme->add_status("No files in directory", "No files exists in specified directory ({$_POST['dir']}).");
} else {
$this->theme->add_status("Directory does not exist", "Specified directory does not exist ({$_POST['dir']}).");
}
$bae = new BulkAddEvent($_POST['dir']);
send_event($bae);
if(strlen($bae->results) > 0) {
$this->theme->add_status("Adding files", $bae->results);
}
$this->theme->display_upload_results($page);
}
@ -38,15 +42,14 @@ class BulkAdd extends Extension {
public function onCommand(CommandEvent $event) {
if($event->cmd == "help") {
print " bulk-add [directory]\n";
print " Import this directory\n\n";
print "\tbulk-add [directory]\n";
print "\t\tImport this directory\n\n";
}
if($event->cmd == "bulk-add") {
if(count($event->args) == 1) {
$list = add_dir($event->args[0]);
if(strlen($list) > 0) {
$this->theme->add_status("Adding files", $list);
}
$bae = new BulkAddEvent($event->args[0]);
send_event($bae);
print(implode("\n", $bae->results));
}
}
}
@ -54,5 +57,14 @@ class BulkAdd extends Extension {
public function onAdminBuilding(AdminBuildingEvent $event) {
$this->theme->display_admin_block();
}
}
public function onBulkAdd(BulkAddEvent $event) {
if(is_dir($event->dir) && is_readable($event->dir)) {
$event->results = add_dir($event->dir);
}
else {
$h_dir = html_escape($event->dir);
$event->results[] = "Error, $h_dir is not a readable directory";
}
}
}

View file

@ -1,18 +1,21 @@
<?php
class BulkAddTest {
class BulkAddTest extends ShimmiePHPUnitTestCase {
function testBulkAdd() {
$this->log_in_as_admin();
$this->get_page('admin');
$this->assert_title("Admin Tools");
$this->set_field('dir', "asdf");
$this->click("Add");
$this->assert_text("is not a directory");
$bae = new BulkAddEvent('asdf');
send_event($bae);
$this->assertContains("Error, asdf is not a readable directory",
$bae->results, implode("\n", $bae->results));
return; // FIXME: have BAE return a list of successes as well as errors?
$this->get_page('admin');
$this->assert_title("Admin Tools");
$this->set_field('dir', "tests");
$this->click("Add");
send_event(new BulkAddEvent('tests'));
# FIXME: test that the output here makes sense, no "adding foo.php ... ok"
@ -31,4 +34,3 @@ class BulkAddTest {
$this->log_out();
}
}

View file

@ -10,9 +10,11 @@ class BulkAddTheme extends Themelet {
$page->set_title("Adding folder");
$page->set_heading("Adding folder");
$page->add_block(new NavBlock());
$html = "";
foreach($this->messages as $block) {
$page->add_block($block);
$html .= "<br/>" . html_escape($html);
}
$page->add_block(new Block("Results", $block));
}
/*
@ -21,7 +23,7 @@ class BulkAddTheme extends Themelet {
* directory full of images
*/
public function display_admin_block() {
global $page, $user;
global $page;
$html = "
Add a folder full of images; any subfolders will have their names
used as tags for the images within.
@ -42,4 +44,3 @@ class BulkAddTheme extends Themelet {
$this->messages[] = new Block($title, $body);
}
}

View file

@ -29,7 +29,11 @@ class Downtime extends Extension {
if(!$user->can("ignore_downtime") && !$this->is_safe_page($event)) {
$msg = $config->get_string("downtime_message");
$this->theme->display_message($msg);
exit;
if(!defined("UNITTEST")) { // hax D:
header("HTTP/1.0 {$page->code} Downtime");
print($page->data);
exit;
}
}
$this->theme->display_notification($page);
}
@ -40,4 +44,3 @@ class Downtime extends Extension {
else return false;
}
}

View file

@ -1,23 +1,39 @@
<?php
class DowntimeTest {
class DowntimeTest extends ShimmiePHPUnitTestCase {
function tearDown() {
global $config;
$config->set_bool("downtime", false);
}
function testDowntime() {
$this->log_in_as_admin();
$this->get_page("setup");
$this->set_field("_config_downtime", true);
$this->set_field("_config_downtime_message", "brb, unit testing");
$this->click("Save Settings");
$this->assert_text("DOWNTIME MODE IS ON!");
$this->log_out();
global $config;
$config->set_string("downtime_message", "brb, unit testing");
// downtime on
$config->set_bool("downtime", true);
$this->log_in_as_admin();
$this->get_page("post/list");
$this->assert_text("brb, unit testing");
$this->assert_text("DOWNTIME MODE IS ON!");
$this->assert_response(200);
$this->log_in_as_user();
$this->get_page("post/list");
$this->assert_content("brb, unit testing");
$this->assert_response(503);
// downtime off
$config->set_bool("downtime", false);
$this->log_in_as_admin();
$this->get_page("setup");
$this->set_field("_config_downtime", false);
$this->click("Save Settings");
$this->get_page("post/list");
$this->assert_no_text("DOWNTIME MODE IS ON!");
$this->log_out();
$this->assert_response(200);
$this->log_in_as_user();
$this->get_page("post/list");
$this->assert_no_content("brb, unit testing");
$this->assert_response(200);
}
}

View file

@ -17,14 +17,15 @@ class DowntimeTheme extends Themelet {
* @param string $message
*/
public function display_message(/*string*/ $message) {
global $config, $user;
global $config, $user, $page;
$theme_name = $config->get_string('theme');
$data_href = get_base_href();
$login_link = make_link("user_admin/login");
header("HTTP/1.0 503 Service Temporarily Unavailable");
$auth = $user->get_auth_html();
print <<<EOD
$page->set_mode('data');
$page->set_code(503);
$page->set_data(<<<EOD
<html>
<head>
<title>Downtime</title>
@ -60,7 +61,7 @@ class DowntimeTheme extends Themelet {
</div>
</body>
</html>
EOD;
EOD
);
}
}

View file

@ -135,8 +135,8 @@ class ExtManager extends Extension {
public function onCommand(CommandEvent $event) {
if($event->cmd == "help") {
print " disable-all-ext\n";
print " disable all extensions\n\n";
print "\tdisable-all-ext\n";
print "\t\tdisable all extensions\n\n";
}
if($event->cmd == "disable-all-ext") {
$this->write_config(array());
@ -206,4 +206,3 @@ class ExtManager extends Extension {
}
}
}

View file

@ -33,9 +33,10 @@ class ArchiveFileHandler extends Extension {
$cmd = str_replace('%f', $event->tmpname, $cmd);
$cmd = str_replace('%d', $tmpdir, $cmd);
exec($cmd);
$list = add_dir($tmpdir);
if(strlen($list) > 0) {
$this->theme->add_status("Adding files", $list);
$results = add_dir($tmpdir);
if(count($results) > 0) {
// FIXME no theme?
$this->theme->add_status("Adding files", $results);
}
deltree($tmpdir);
$event->image_id = -2; // default -1 = upload wasn't handled

View file

@ -1,4 +1,5 @@
<?php
define("UNITTEST", true);
define("TIMEZONE", 'UTC');
define("EXTRA_EXTS", str_replace("ext/", "", implode(',', glob('ext/*'))));
define("BASE_HREF", "/");
@ -48,21 +49,34 @@ abstract class ShimmiePHPUnitTestCase extends PHPUnit_Framework_TestCase {
$this->assertEquals($code, $page->code);
}
protected function has_text($text) {
protected function page_to_text($section=null) {
global $page;
$text = "";
foreach($page->blocks as $block) {
if(strpos($block->header, $text) !== false) return true;
if(strpos($block->body, $text) !== false) return true;
if(is_null($section) || $section == $block->section) {
$text .= $block->header . "\n";
$text .= $block->body . "\n\n";
}
}
return false;
return $text;
}
protected function assert_text($text) {
$this->assertTrue($this->has_text($text));
protected function assert_text($text, $section=null) {
$this->assertContains($text, $this->page_to_text($section));
}
protected function assert_no_text($text) {
$this->assertFalse($this->has_text($text));
protected function assert_no_text($text, $section=null) {
$this->assertNotContains($text, $this->page_to_text($section));
}
protected function assert_content($content) {
global $page;
$this->assertContains($content, $page->data);
}
protected function assert_no_content($content) {
global $page;
$this->assertNotContains($content, $page->data);
}
// user things