From 3da85acb2a8f3841058cd2608c181a57de4c6c00 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 4 Mar 2007 02:50:39 +0000 Subject: [PATCH] - Add test folder - Move config back to outside folder git-svn-id: http://htmlpurifier.org/svnroot@793 48356398-32a2-884e-a903-53898d9a118a --- common.php | 1 + functions.php | 28 +++++++++++------------ singleton.php | 52 ++++++++++++++++++++++++++++++++++++++++++ tests/functions-test.php | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/index.php | 31 +++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 14 deletions(-) create mode 100644 singleton.php create mode 100644 tests/functions-test.php create mode 100644 tests/index.php diff --git a/common.php b/common.php index 3b8e335..a9e33cd 100644 --- a/common.php +++ b/common.php @@ -3,6 +3,7 @@ if (!version_compare(PHP_VERSION, "5", ">=")) exit('Requires PHP 5.'); chdir('../'); // set working directory to parent directory +require 'singleton.php'; require 'functions.php'; require 'config.default.php'; require 'config.php'; diff --git a/functions.php b/functions.php index bf88e2a..9ff996e 100644 --- a/functions.php +++ b/functions.php @@ -8,11 +8,13 @@ * @todo Valid markup, user-defined custom error-pages */ function display_error_and_quit($code, $text = false, $details = false) { + $xc = XHTMLCompiler::getInstance(); $default = set_response_code($code); if (!$text) $text = $default; - echo "

Error: $text

"; - if ($details) echo $details; - exit; // essential + $out = "

Error: $text

"; + if ($details) $out .= $details; + $xc->paint($out); + $xc->quit(); // essential, will exit } /** @@ -21,7 +23,8 @@ function display_error_and_quit($code, $text = false, $details = false) { * @return string HTTP status code's name */ function set_response_code($code) { - $code_descriptions = array( + $xc = XHTMLCompiler::getInstance(); + $code_descriptions = array( // could be factored out 200 => 'Okay', 304 => 'Not Modified', 401 => 'Unauthorized', @@ -34,8 +37,8 @@ function set_response_code($code) { if (isset($code_descriptions[$code])) { $text .= ' ' . $code_descriptions[$code]; } - header($_SERVER['SERVER_PROTOCOL'] . ' ' . $text, true, $code); - header('Status: ' . $text); + $xc->header($_SERVER['SERVER_PROTOCOL'] . ' ' . $text, true, $code); + $xc->header('Status: ' . $text); return $text; } @@ -44,15 +47,12 @@ function set_response_code($code) { * @return string page name */ function get_page_from_server() { + $xc = XHTMLCompiler::getInstance(); // load up page from environment variables, if using ErrorDocument impl. - $page = $_SERVER['REQUEST_URI']; - $this_file = basename(__FILE__); - if ($_SERVER['PHP_SELF'] !== '/' . $this_file) { - // we're in a web subdirectory: normalize the name - $prefix_length = strlen($_SERVER['PHP_SELF']) - strlen($this_file) - 1; - $page = substr($page, $prefix_length); - } - $page = substr($page, 1); // remove leading slash, so it's a relative + $page = $xc->getRequestURI(); + $self = dirname($xc->getPHPSelf()); + $root = strlen(substr($self, 0, strrpos($self, '/'))); + $page = substr($page, $root + 1); // remove leading slash and root return $page; } diff --git a/singleton.php b/singleton.php new file mode 100644 index 0000000..fa16454 --- /dev/null +++ b/singleton.php @@ -0,0 +1,52 @@ + \ No newline at end of file diff --git a/tests/functions-test.php b/tests/functions-test.php new file mode 100644 index 0000000..11a9e08 --- /dev/null +++ b/tests/functions-test.php @@ -0,0 +1,59 @@ +mock = new XHTMLCompilerMock(); + XHTMLCompiler::setInstance($this->mock); + } + + function tearDown() { + $this->mock->tally(); + } + + function test_set_response_code() { + $this->mock->expectAt(0, 'header', array($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404)); + $this->mock->expectAt(1, 'header', array('Status: 404 Not Found')); + set_response_code(404); + } + + function test_display_error_and_quit() { + $this->mock->expectCallCount('quit', 3); + $this->mock->expect('paint', array(new PatternExpectation('/404 Not Found/'))); + display_error_and_quit(404); + $this->mock->expect('paint', array(new PatternExpectation('/WTF/'))); + display_error_and_quit(404, 'WTF'); + $this->mock->expect('paint', array(new PatternExpectation('/404 Not Found.+?Could not find the file/'))); + display_error_and_quit(404, false, 'Could not find the file'); + } + + function test_get_page_from_server() { + $this->mock->setReturnValue('getRequestURI', '/index.html'); + $this->mock->setReturnValue('getPHPSelf', '/xhtml-compiler/main.php'); + $this->assertEqual(get_page_from_server(), 'index.html'); + } + + function testInSubdir_get_page_from_server() { + $this->mock->setReturnValue('getRequestURI', '/subdir/foobar.html'); + $this->mock->setReturnValue('getPHPSelf', '/subdir/xhtml-compiler/main.php'); + $this->assertEqual(get_page_from_server(), 'foobar.html'); + } + + function testInSubdirDifferentAppDir_get_page_from_server() { + $this->mock->setReturnValue('getRequestURI', '/subdir/foobar.html'); + $this->mock->setReturnValue('getPHPSelf', '/subdir/xc/main.php'); + $this->assertEqual(get_page_from_server(), 'foobar.html'); + } + + function testInSubdirPageInDirectory_get_page_from_server() { + $this->mock->setReturnValue('getRequestURI', '/subdir/foo/foobar.html'); + $this->mock->setReturnValue('getPHPSelf', '/subdir/xhtml-compiler/main.php'); + $this->assertEqual(get_page_from_server(), 'foo/foobar.html'); + } + +} + +?> \ No newline at end of file diff --git a/tests/index.php b/tests/index.php new file mode 100644 index 0000000..ef75224 --- /dev/null +++ b/tests/index.php @@ -0,0 +1,31 @@ +addTestFile('functions-test.php'); + +if (SimpleReporter::inCli()) $reporter = new TextReporter(); +else $reporter = new HTMLReporter('UTF-8'); + +$test->run($reporter); + +?> -- 2.11.4.GIT