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 * Add a directory full of images
* *
* @param $base string * @param $base string
* @return string * @return array
*/ */
function add_dir(/*string*/ $base) { function add_dir(/*string*/ $base) {
$list = ""; $results = array();
foreach(list_files($base) as $full_path) { foreach(list_files($base) as $full_path) {
$short_path = str_replace($base, "", $full_path); $short_path = str_replace($base, "", $full_path);
$filename = basename($full_path); $filename = basename($full_path);
$tags = path_to_tags($short_path); $tags = path_to_tags($short_path);
$list .= "<br>".html_escape("$short_path (".str_replace(" ", ", ", $tags).")... "); $result = "$short_path (".str_replace(" ", ", ", $tags).")... ";
try { try {
add_image($full_path, $filename, $tags); add_image($full_path, $filename, $tags);
$list .= "ok\n"; $result .= "ok";
} }
catch(UploadException $ex) { 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 <?php
/** /**
* \page themes Themes * \page themes Themes
* *
* Each extension has a theme with a specific name -- eg. the extension Setup * 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 * 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 * 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 * in the file themes/mytheme/setup.theme.php called CustomSetupTheme which
* extends SetupTheme and overrides some of its methods. * extends SetupTheme and overrides some of its methods.
* *
* Generally an extension should only deal with processing data; whenever it * 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 * 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 * theme object, and the theme will add the data into the global $page
@ -65,11 +65,11 @@ class Page {
/** @name "data" mode */ /** @name "data" mode */
//@{ //@{
/** @var string */ /** @var string; public only for unit test */
private $data = ""; public $data = "";
/** @var string */ /** @var string; public only for unit test */
private $filename = null; public $filename = null;
/** /**
* Set the raw data to be sent. * Set the raw data to be sent.
@ -226,7 +226,7 @@ class Page {
} }
return $data; return $data;
} }
/** /**
* Removes all currently set HTML headers (Be careful..). * Removes all currently set HTML headers (Be careful..).
*/ */
@ -251,7 +251,8 @@ class Page {
*/ */
public function display() { public function display() {
global $page, $user; global $page, $user;
header("HTTP/1.0 {$this->code} Shimmie");
header("Content-type: ".$this->type); header("Content-type: ".$this->type);
header("X-Powered-By: SCore-".SCORE_VERSION); header("X-Powered-By: SCore-".SCORE_VERSION);
@ -268,7 +269,6 @@ class Page {
switch($this->mode) { switch($this->mode) {
case "page": case "page":
header("HTTP/1.0 {$this->code} Shimmie");
if(CACHE_HTTP) { if(CACHE_HTTP) {
header("Vary: Cookie, Accept-Encoding"); header("Vary: Cookie, Accept-Encoding");
if($user->is_anonymous() && $_SERVER["REQUEST_METHOD"] == "GET") { if($user->is_anonymous() && $_SERVER["REQUEST_METHOD"] == "GET") {
@ -309,16 +309,16 @@ class Page {
break; break;
} }
} }
/** /**
* This function grabs all the CSS and JavaScript files sprinkled throughout Shimmie's folders, * 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 * 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. * them in the /cache/ directory for serving to the user.
* *
* Why do this? Two reasons: * Why do this? Two reasons:
* 1. Reduces the number of files the user's browser needs to download. * 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. * 2. Allows these cached files to be compressed/minified by the admin.
* *
* TODO: This should really be configurable somehow... * TODO: This should really be configurable somehow...
*/ */
public function add_auto_html_headers() { public function add_auto_html_headers() {
@ -378,4 +378,3 @@ class Page {
class MockPage extends Page { class MockPage extends Page {
} }

View File

@ -15,21 +15,25 @@
* <p><b>Note:</b> requires the "admin" extension to be enabled * <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 { class BulkAdd extends Extension {
public function onPageRequest(PageRequestEvent $event) { public function onPageRequest(PageRequestEvent $event) {
global $page, $user; global $page, $user;
if($event->page_matches("bulk_add")) { if($event->page_matches("bulk_add")) {
if($user->is_admin() && $user->check_auth_token() && isset($_POST['dir'])) { if($user->is_admin() && $user->check_auth_token() && isset($_POST['dir'])) {
set_time_limit(0); set_time_limit(0);
$list = add_dir($_POST['dir']); $bae = new BulkAddEvent($_POST['dir']);
if(strlen($list) > 0) { send_event($bae);
$this->theme->add_status("Adding files", $list); if(strlen($bae->results) > 0) {
} else { $this->theme->add_status("Adding files", $bae->results);
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']}).");
}
} }
$this->theme->display_upload_results($page); $this->theme->display_upload_results($page);
} }
@ -38,15 +42,14 @@ class BulkAdd extends Extension {
public function onCommand(CommandEvent $event) { public function onCommand(CommandEvent $event) {
if($event->cmd == "help") { if($event->cmd == "help") {
print " bulk-add [directory]\n"; print "\tbulk-add [directory]\n";
print " Import this directory\n\n"; print "\t\tImport this directory\n\n";
} }
if($event->cmd == "bulk-add") { if($event->cmd == "bulk-add") {
if(count($event->args) == 1) { if(count($event->args) == 1) {
$list = add_dir($event->args[0]); $bae = new BulkAddEvent($event->args[0]);
if(strlen($list) > 0) { send_event($bae);
$this->theme->add_status("Adding files", $list); print(implode("\n", $bae->results));
}
} }
} }
} }
@ -54,5 +57,14 @@ class BulkAdd extends Extension {
public function onAdminBuilding(AdminBuildingEvent $event) { public function onAdminBuilding(AdminBuildingEvent $event) {
$this->theme->display_admin_block(); $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 <?php
class BulkAddTest { class BulkAddTest extends ShimmiePHPUnitTestCase {
function testBulkAdd() { function testBulkAdd() {
$this->log_in_as_admin(); $this->log_in_as_admin();
$this->get_page('admin'); $this->get_page('admin');
$this->assert_title("Admin Tools"); $this->assert_title("Admin Tools");
$this->set_field('dir', "asdf");
$this->click("Add"); $bae = new BulkAddEvent('asdf');
$this->assert_text("is not a directory"); 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->get_page('admin');
$this->assert_title("Admin Tools"); $this->assert_title("Admin Tools");
$this->set_field('dir', "tests"); send_event(new BulkAddEvent('tests'));
$this->click("Add");
# FIXME: test that the output here makes sense, no "adding foo.php ... ok" # FIXME: test that the output here makes sense, no "adding foo.php ... ok"
@ -31,4 +34,3 @@ class BulkAddTest {
$this->log_out(); $this->log_out();
} }
} }

View File

@ -10,9 +10,11 @@ class BulkAddTheme extends Themelet {
$page->set_title("Adding folder"); $page->set_title("Adding folder");
$page->set_heading("Adding folder"); $page->set_heading("Adding folder");
$page->add_block(new NavBlock()); $page->add_block(new NavBlock());
$html = "";
foreach($this->messages as $block) { 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 * directory full of images
*/ */
public function display_admin_block() { public function display_admin_block() {
global $page, $user; global $page;
$html = " $html = "
Add a folder full of images; any subfolders will have their names Add a folder full of images; any subfolders will have their names
used as tags for the images within. used as tags for the images within.
@ -42,4 +44,3 @@ class BulkAddTheme extends Themelet {
$this->messages[] = new Block($title, $body); $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)) { if(!$user->can("ignore_downtime") && !$this->is_safe_page($event)) {
$msg = $config->get_string("downtime_message"); $msg = $config->get_string("downtime_message");
$this->theme->display_message($msg); $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); $this->theme->display_notification($page);
} }
@ -40,4 +44,3 @@ class Downtime extends Extension {
else return false; else return false;
} }
} }

View File

@ -1,23 +1,39 @@
<?php <?php
class DowntimeTest { class DowntimeTest extends ShimmiePHPUnitTestCase {
function tearDown() {
global $config;
$config->set_bool("downtime", false);
}
function testDowntime() { function testDowntime() {
$this->log_in_as_admin(); global $config;
$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();
$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->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->log_in_as_admin();
$this->get_page("setup"); $this->get_page("post/list");
$this->set_field("_config_downtime", false);
$this->click("Save Settings");
$this->assert_no_text("DOWNTIME MODE IS ON!"); $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 * @param string $message
*/ */
public function display_message(/*string*/ $message) { public function display_message(/*string*/ $message) {
global $config, $user; global $config, $user, $page;
$theme_name = $config->get_string('theme'); $theme_name = $config->get_string('theme');
$data_href = get_base_href(); $data_href = get_base_href();
$login_link = make_link("user_admin/login"); $login_link = make_link("user_admin/login");
header("HTTP/1.0 503 Service Temporarily Unavailable");
$auth = $user->get_auth_html(); $auth = $user->get_auth_html();
print <<<EOD
$page->set_mode('data');
$page->set_code(503);
$page->set_data(<<<EOD
<html> <html>
<head> <head>
<title>Downtime</title> <title>Downtime</title>
@ -60,7 +61,7 @@ class DowntimeTheme extends Themelet {
</div> </div>
</body> </body>
</html> </html>
EOD; EOD
);
} }
} }

View File

@ -135,8 +135,8 @@ class ExtManager extends Extension {
public function onCommand(CommandEvent $event) { public function onCommand(CommandEvent $event) {
if($event->cmd == "help") { if($event->cmd == "help") {
print " disable-all-ext\n"; print "\tdisable-all-ext\n";
print " disable all extensions\n\n"; print "\t\tdisable all extensions\n\n";
} }
if($event->cmd == "disable-all-ext") { if($event->cmd == "disable-all-ext") {
$this->write_config(array()); $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('%f', $event->tmpname, $cmd);
$cmd = str_replace('%d', $tmpdir, $cmd); $cmd = str_replace('%d', $tmpdir, $cmd);
exec($cmd); exec($cmd);
$list = add_dir($tmpdir); $results = add_dir($tmpdir);
if(strlen($list) > 0) { if(count($results) > 0) {
$this->theme->add_status("Adding files", $list); // FIXME no theme?
$this->theme->add_status("Adding files", $results);
} }
deltree($tmpdir); deltree($tmpdir);
$event->image_id = -2; // default -1 = upload wasn't handled $event->image_id = -2; // default -1 = upload wasn't handled

View File

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