0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-19 18:55:19 +00:00

Revamp URITest to use the harness.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@214 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-12 16:32:57 +00:00
parent ebe01a0a24
commit 10ea44932a
2 changed files with 62 additions and 42 deletions

View File

@ -10,6 +10,8 @@ require_once 'HTMLPurifier/AttrDef/URI.php';
class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
{
var $scheme, $components, $return_components;
function testGenericURI() {
generate_mock_once('HTMLPurifier_URIScheme');
@ -18,7 +20,7 @@ class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
$old_registry = HTMLPurifier_URISchemeRegistry::instance();
// finally, lets get a copy of the actual class
$def = new HTMLPurifier_AttrDef_URI();
$this->def = new HTMLPurifier_AttrDef_URI();
// initialize test inputs
$uri = // input URI
@ -140,9 +142,8 @@ class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
$components[16] = array(null, '', null, null);
//$expect_uri[16] = ''; // munge scheme off
// test invalid scheme
// test invalid scheme, components shouldn't be passed
$uri[17] = 'javascript:alert("moo");';
$components[17] = false;
$expect_uri[17] = '';
// relative URIs
@ -153,44 +154,28 @@ class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
foreach ($uri as $i => $value) {
// $fake_registry isn't the real mock, because due to PHP 4 weirdness
// I cannot set a default value to function parameters that are passed
// by reference. So we use the value instance() returns.
$fake_registry = new HTMLPurifier_URISchemeRegistryMock($this);
$registry =& HTMLPurifier_URISchemeRegistry::instance($fake_registry);
// now, let's at a pseudo-scheme to the registry
$scheme =& new HTMLPurifier_URISchemeMock($this);
// here are the schemes we will support with overloaded mocks
$registry->setReturnReference('getScheme', $scheme, array('http'));
$registry->setReturnReference('getScheme', $scheme, array('mailto'));
// default return value is false (meaning no scheme defined: reject)
$registry->setReturnValue('getScheme', false, array('*'));
if (!isset($return_components[$i])) {
$return_components[$i] = $components[$i];
}
if (!isset($expect_uri[$i])) {
$expect_uri[$i] = $value;
}
if (!isset($config[$i])) {
$config[$i] = HTMLPurifier_Config::createDefault();
}
if (!isset($context[$i])) {
$context[$i] = new HTMLPurifier_AttrContext();
}
if ($components[$i] === false) {
$scheme->expectNever('validateComponents');
// setUpAssertDef
if ( isset($components[$i]) ) {
$this->components = $components[$i];
} else {
$scheme->setReturnValue(
'validateComponents', $return_components[$i], $components[$i]);
$scheme->expectOnce('validateComponents', $components[$i]);
$this->components = false;
}
$result = $def->validate($value, $config[$i], $context[$i]);
$scheme->tally();
$this->assertIdentical($expect_uri[$i], $result, "Test $i: %s");
if ( isset($return_components[$i]) ) {
$this->return_components = $return_components[$i];
} else {
$this->return_components = $this->components;
}
// parameters
if (!isset($expect_uri[$i])) {
$expect_uri[$i] = $value; // untouched
}
// the read in values
$this->config = isset($config[$i]) ? $config[$i] : null;
$this->context = isset($context[$i]) ? $context[$i] : null;
$this->assertDef($value, $expect_uri[$i], "Test $i: %s");
}
@ -199,6 +184,36 @@ class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
}
function setUpAssertDef() {
// $fake_registry isn't the real mock, because due to PHP 4 weirdness
// I cannot set a default value to function parameters that are passed
// by reference. So we use the value instance() returns.
$fake_registry = new HTMLPurifier_URISchemeRegistryMock($this);
$registry =& HTMLPurifier_URISchemeRegistry::instance($fake_registry);
// now, let's at a pseudo-scheme to the registry
$this->scheme =& new HTMLPurifier_URISchemeMock($this);
// here are the schemes we will support with overloaded mocks
$registry->setReturnReference('getScheme', $this->scheme, array('http'));
$registry->setReturnReference('getScheme', $this->scheme, array('mailto'));
// default return value is false (meaning no scheme defined: reject)
$registry->setReturnValue('getScheme', false, array('*'));
if ($this->components === false) {
$this->scheme->expectNever('validateComponents');
} else {
$this->scheme->setReturnValue(
'validateComponents', $this->return_components, $this->components);
$this->scheme->expectOnce('validateComponents', $this->components);
}
}
function tearDownAssertDef() {
$this->scheme->tally();
}
}
?>

View File

@ -8,18 +8,23 @@ class HTMLPurifier_AttrDefHarness extends UnitTestCase
var $config;
// cannot be used for accumulator
function assertDef($string, $expect = true) {
function assertDef($string, $expect = true, $message = '%s') {
// $expect can be a string or bool
$this->setUpAssertDef();
if (!$this->config) $this->config = HTMLPurifier_Config::createDefault();
if (!$this->context) $this->context = new HTMLPurifier_AttrContext();
$result = $this->def->validate($string, $this->config, $this->context);
if ($expect === true) {
$this->assertIdentical($string, $result);
$this->assertIdentical($string, $result, $message);
} else {
$this->assertIdentical($expect, $result);
$this->assertIdentical($expect, $result, $message);
}
$this->tearDownAssertDef();
}
function setUpAssertDef() {}
function tearDownAssertDef() {}
}
?>