0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 23:28:42 +00:00

[3.1.0] Define *.vtest test hierarchy, and continue work on validator.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1625 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-03-23 01:06:35 +00:00
parent 848795d4a0
commit aedfbd1e93
21 changed files with 195 additions and 110 deletions

View File

@ -22,14 +22,20 @@ class HTMLPurifier_ConfigSchema_Interchange
* Adds a namespace array to $namespaces
*/
public function addNamespace($namespace) {
$this->namespaces[$namespace->namespace] = $namespace;
if (isset($this->namespaces[$i = $namespace->namespace])) {
throw new HTMLPurifier_ConfigSchema_Exception("Cannot redefine namespace '$i'");
}
$this->namespaces[$i] = $namespace;
}
/**
* Adds a directive array to $directives
*/
public function addDirective($directive) {
$this->directives[$directive->id->__toString()] = $directive;
if (isset($this->directives[$i = $directive->id->__toString()])) {
throw new HTMLPurifier_ConfigSchema_Exception("Cannot redefine directive '$i'");
}
$this->directives[$i] = $directive;
}
}

View File

@ -15,6 +15,12 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
* @param $hash HTMLPurifier_ConfigSchema_StringHash source data
*/
public function build($interchange, $hash) {
if (!$hash instanceof HTMLPurifier_StringHash) {
$hash = new HTMLPurifier_StringHash($hash);
}
if (!isset($hash['ID'])) {
throw new HTMLPurifier_ConfigSchema_Exception('Hash does not have any ID');
}
if (strpos($hash['ID'], '.') === false) {
$this->buildNamespace($interchange, $hash);
} else {
@ -26,7 +32,9 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
public function buildNamespace($interchange, $hash) {
$namespace = new HTMLPurifier_ConfigSchema_Interchange_Namespace();
$namespace->namespace = $hash->offsetGet('ID');
$namespace->description = $hash->offsetGet('DESCRIPTION');
if (isset($hash['DESCRIPTION'])) {
$namespace->description = $hash->offsetGet('DESCRIPTION');
}
$interchange->addNamespace($namespace);
}
@ -35,33 +43,43 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
// These are required elements:
$directive->id = $this->id($hash->offsetGet('ID'));
$type = explode('/', $hash->offsetGet('TYPE'));
if (isset($type[1])) $directive->typeAllowsNull = true;
$directive->type = $type[0];
$directive->description = $directive->offsetGet('DESCRIPTION');
// These are extras:
if (isset($directive['ALLOWED'])) {
$directive->allowed = $this->lookup($this->evalArray($directive->offsetGet('ALLOWED')));
if (isset($hash['TYPE'])) {
$type = explode('/', $hash->offsetGet('TYPE'));
if (isset($type[1])) $directive->typeAllowsNull = true;
$directive->type = $type[0];
}
if (isset($directive['VALUE-ALIASES'])) {
$directive->valueAliases = $this->evalArray($directive->offsetGet('VALUE-ALIASES'));
if (isset($hash['DESCRIPTION'])) {
$directive->description = $hash->offsetGet('DESCRIPTION');
}
if (isset($directive['ALIASES'])) {
if (isset($hash['ALLOWED'])) {
$directive->allowed = $this->lookup($this->evalArray($hash->offsetGet('ALLOWED')));
}
if (isset($hash['VALUE-ALIASES'])) {
$directive->valueAliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES'));
}
if (isset($hash['ALIASES'])) {
$raw_aliases = trim($hash->offsetGet('ALIASES'));
$aliases = preg_split('/\s*,\s*/', $raw_aliases);
foreach ($aliases as $alias) {
$this->aliases[] = $this->id($alias);
$directive->aliases[] = $this->id($alias);
}
}
if (isset($directive['VERSION'])) {
$directive->version = $directive->offsetGet('VERSION');
if (isset($hash['VERSION'])) {
$directive->version = $hash->offsetGet('VERSION');
}
if (isset($directive['DEPRECATED-USE'])) {
$directive->deprecatedUse = $this->id($directive->offsetGet('DEPRECATED-USE'));
if (isset($hash['DEPRECATED-USE'])) {
$directive->deprecatedUse = $this->id($hash->offsetGet('DEPRECATED-USE'));
}
if (isset($directive['DEPRECATED-VERSION'])) {
$directive->deprecatedVersion = $directive->offsetGet('DEPRECATED-VERSION');
if (isset($hash['DEPRECATED-VERSION'])) {
$directive->deprecatedVersion = $hash->offsetGet('DEPRECATED-VERSION');
}
$interchange->addDirective($directive);

View File

@ -11,7 +11,7 @@ class HTMLPurifier_ConfigSchema_Validator
/**
* Volatile context variables to provide a fluent interface.
*/
protected $context, $obj, $member;
protected $context = array(), $obj, $member;
/**
* Validates a fully-formed interchange object. Throws an
@ -28,38 +28,53 @@ class HTMLPurifier_ConfigSchema_Validator
}
public function validateNamespace($n) {
$this->context = "namespace '{$n->namespace}'";
$this->context[] = "namespace '{$n->namespace}'";
$this->with($n, 'namespace')
->assertNotEmpty()
->assertAlnum();
$this->with($n, 'description')
->assertIsString()
->assertNotEmpty();
->assertNotEmpty()
->assertIsString(); // technically redundant
array_pop($this->context);
}
public function validateDirective($d) {
$this->context = "directive '{$d->id}'";
$this->context[] = "directive '{$d->id}'";
$this->validateId($d->id);
$this->with($d, 'description')
->assertNotEmpty();
if (!isset(HTMLPurifier_VarParser::$types[$d->type])) {
$this->error('type', 'is invalid');
}
array_pop($this->context);
}
public function validateId($id) {
$this->context = "id '$id'";
$this->context[] = "id '$id'";
if (!isset($this->interchange->namespaces[$id->namespace])) {
$this->error('namespace', 'does not exist');
}
$this->with($id, 'namespace')
->assertNotEmpty()
->assertAlnum();
$this->with($id, 'directive')
->assertNotEmpty()
->assertAlnum();
array_pop($this->context);
}
// protected helper functions
protected function with($obj, $member) {
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->context, $obj, $member);
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member);
}
protected function error($msg) {
throw new HTMLPurifier_ConfigSchema_Exception($msg . ' in ' . $this->context);
protected function error($target, $msg) {
throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($target) . ' in ' . $this->getFormattedContext() . ' ' . $msg);
}
protected function getFormattedContext() {
return implode(' in ', array_reverse($this->context));
}
}

View File

@ -40,7 +40,7 @@ class HTMLPurifier_ConfigSchema_ValidatorAtom
}
protected function error($msg) {
throw new HTMLPurifier_ConfigSchema_Exception('Member variable \'' . $this->member . '\' in ' . $this->context . ' ' . $msg);
throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);
}
}

View File

@ -0,0 +1,7 @@
ERROR: Description in directive 'Ns.Dir' must not be empty
----
Ns
DESCRIPTION: Our namespace.
----
Ns.Dir
TYPE: int

View File

@ -0,0 +1,8 @@
ERROR: Directive in id 'Ns.+' in directive 'Ns.+' must be alphanumeric
----
Ns
DESCRIPTION: Generic namespace.
----
ID: Ns.+
TYPE: int
DESCRIPTION: Description

View File

@ -0,0 +1,8 @@
ERROR: Directive in id 'Ns.' in directive 'Ns.' must not be empty
----
Ns
DESCRIPTION: Our namespace
----
ID: Ns.
TYPE: int
DESCRIPTION: Description.

View File

@ -0,0 +1,5 @@
ERROR: Namespace in id 'Rd.Dir' in directive 'Rd.Dir' does not exist
----
ID: Rd.Dir
TYPE: int
DESCRIPTION: Description

View File

@ -0,0 +1,8 @@
ERROR: Type in directive 'Ns.Dir' is invalid
----
Ns
DESCRIPTION: Namespace
----
Ns.Dir
DESCRIPTION: Directive
TYPE: foobar

View File

@ -0,0 +1,9 @@
ERROR: Cannot redefine directive 'Ns.Dir'
----
ID: Ns.Dir
DESCRIPTION: Version 1
TYPE: int
----
ID: Ns.Dir
DESCRIPTION: Version 2
TYPE: int

View File

@ -0,0 +1,3 @@
ERROR: Description in namespace 'Ns' must not be empty
----
ID: Ns

View File

@ -0,0 +1,4 @@
ERROR: Namespace in namespace 'R&D' must be alphanumeric
----
ID: R&D
DESCRIPTION: ctype_alnum($ID) === false

View File

@ -0,0 +1,4 @@
ERROR: Namespace in namespace '0' must not be empty
----
ID: 0
DESCRIPTION: empty($ID) === true

View File

@ -0,0 +1,2 @@
Namespace
DESCRIPTION: This is a generic namespace.

View File

@ -0,0 +1,7 @@
ERROR: Cannot redefine namespace 'Ns'
----
ID: Ns
DESCRIPTION: Version 1
----
ID: Ns
DESCRIPTION: Version 2

View File

@ -19,7 +19,7 @@ class HTMLPurifier_ConfigSchema_ValidatorAtomTest extends UnitTestCase
}
public function testAssertIsStringFail() {
$this->expectValidationException("Member variable 'property' in context must be a string");
$this->expectValidationException("Property in context must be a string");
$this->makeAtom(3)->assertIsString();
}
@ -28,7 +28,7 @@ class HTMLPurifier_ConfigSchema_ValidatorAtomTest extends UnitTestCase
}
public function testAssertNotNullFail() {
$this->expectValidationException("Member variable 'property' in context must not be null");
$this->expectValidationException("Property in context must not be null");
$this->makeAtom(null)->assertNotNull();
}
@ -37,12 +37,12 @@ class HTMLPurifier_ConfigSchema_ValidatorAtomTest extends UnitTestCase
}
public function testAssertAlnumFail() {
$this->expectValidationException("Member variable 'property' in context must be alphanumeric");
$this->expectValidationException("Property in context must be alphanumeric");
$this->makeAtom('%a')->assertAlnum();
}
public function testAssertAlnumFailIsString() {
$this->expectValidationException("Member variable 'property' in context must be a string");
$this->expectValidationException("Property in context must be a string");
$this->makeAtom(3)->assertAlnum();
}
@ -51,7 +51,7 @@ class HTMLPurifier_ConfigSchema_ValidatorAtomTest extends UnitTestCase
}
public function testAssertNotEmptyFail() {
$this->expectValidationException("Member variable 'property' in context must not be empty");
$this->expectValidationException("Property in context must not be empty");
$this->makeAtom('')->assertNotEmpty();
}

View File

@ -1,75 +0,0 @@
<?php
class HTMLPurifier_ConfigSchema_ValidatorTest extends UnitTestCase
{
protected $interchange, $validator;
public function setup() {
$this->interchange = new HTMLPurifier_ConfigSchema_Interchange();
$this->validator = new HTMLPurifier_ConfigSchema_Validator();
}
/**
* Adds a namespace to our interchange object.
*/
protected function addNamespace($namespace, $description) {
$obj = new HTMLPurifier_ConfigSchema_Interchange_Namespace();
$obj->namespace = $namespace;
$obj->description = $description;
$this->interchange->addNamespace($obj);
return $obj;
}
protected function addDirective($namespace, $directive, $type = 'string', $description = 'Description') {
$obj = new HTMLPurifier_ConfigSchema_Interchange_Directive();
$obj->id = $this->makeId($namespace, $directive);
$obj->type = $type;
$obj->description = $description;
$this->interchange->addDirective($obj);
return $obj; // for future editing
}
protected function makeId($namespace, $directive) {
return new HTMLPurifier_ConfigSchema_Interchange_Id($namespace, $directive);
}
/**
* Invokes a validation, so we can fish for exceptions
*/
protected function validate() {
$this->validator->validate($this->interchange);
}
protected function expectValidationException($msg) {
$this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg));
}
public function testNamespace() {
$this->addNamespace('Namespace', 'This is a generic namespace');
$this->validate();
}
public function testNamespaceNamespaceNotEmpty() {
$this->addNamespace('0', 'Description');
$this->expectValidationException("Member variable 'namespace' in namespace '0' must not be empty");
$this->validate();
}
public function testNamespaceNamespaceAlnum() {
$this->addNamespace('%', 'Description');
$this->expectValidationException("Member variable 'namespace' in namespace '%' must be alphanumeric");
$this->validate();
}
public function testNamespaceDescriptionNotEmpty() {
$this->addNamespace('Ns', '');
$this->expectValidationException("Member variable 'description' in namespace 'Ns' must not be empty");
$this->validate();
}
public function testDirectiveIdNamespaceNotEmpty() {
$this->addDirective('', 'Dir');
}
}

View File

@ -0,0 +1,42 @@
<?php
/**
* Controller for validator test-cases.
*/
class HTMLPurifier_ConfigSchema_ValidatorTestCase extends UnitTestCase
{
protected $_path, $_parser, $_builder;
public $validator;
public function __construct($path) {
$this->_path = $path;
$this->_parser = new HTMLPurifier_StringHashParser();
$this->_builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder();
parent::UnitTestCase($path);
}
public function setup() {
$this->validator = new HTMLPurifier_ConfigSchema_Validator();
}
public function testValidator() {
$hashes = $this->_parser->parseMultiFile($this->_path);
$interchange = new HTMLPurifier_ConfigSchema_Interchange();
$error = null;
foreach ($hashes as $hash) {
if (!isset($hash['ID'])) {
if (isset($hash['ERROR'])) {
$this->expectException(
new HTMLPurifier_ConfigSchema_Exception($hash['ERROR'])
);
}
continue;
}
$this->_builder->build($interchange, new HTMLPurifier_StringHash($hash));
}
$this->validator->validate($interchange);
$this->pass();
}
}

View File

@ -156,6 +156,8 @@ function htmlpurifier_add_test($test, $test_file, $only_phpt = false) {
case '.php':
require_once $test_file;
return $test->addTestClass(path2class($test_file));
case '.vtest':
return $test->addTestCase(new HTMLPurifier_ConfigSchema_ValidatorTestCase($test_file));
default:
trigger_error("$test_file is an invalid file for testing", E_USER_ERROR);
}

View File

@ -110,6 +110,14 @@ foreach ($test_dirs as $dir) {
}
}
// handle vtest dirs
foreach ($vtest_dirs as $dir) {
$raw_files = $FS->globr($dir, '*.vtest');
foreach ($raw_files as $file) {
$test_files[] = str_replace('\\', '/', $file);
}
}
// handle phpt files
foreach ($phpt_dirs as $dir) {
$phpt_files = $FS->globr($dir, '*.phpt');

View File

@ -15,6 +15,10 @@ if (!$AC['only-phpt']) {
$test_files[] = 'HTMLPurifier/Filter/ExtractStyleBlocksTest.php';
}
// ConfigSchema Validator tests
$vtest_dirs = array();
$vtest_dirs[] = 'HTMLPurifier/ConfigSchema/Validator';
// ConfigDoc auxiliary library
if (version_compare(PHP_VERSION, '5.2', '>=')) {
$test_dirs[] = 'ConfigDoc';