mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-01-03 13:21:51 +00:00
[3.1.0] Refactor out validation framework for Interchange
- Implement IdExists validator git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1584 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
d682a59a68
commit
8bda0c4dfb
@ -129,6 +129,8 @@ require 'HTMLPurifier/ConfigSchema/StringHash.php';
|
|||||||
require 'HTMLPurifier/ConfigSchema/StringHashAdapter.php';
|
require 'HTMLPurifier/ConfigSchema/StringHashAdapter.php';
|
||||||
require 'HTMLPurifier/ConfigSchema/StringHashParser.php';
|
require 'HTMLPurifier/ConfigSchema/StringHashParser.php';
|
||||||
require 'HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php';
|
require 'HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php';
|
||||||
|
require 'HTMLPurifier/ConfigSchema/Interchange/Validator.php';
|
||||||
|
require 'HTMLPurifier/ConfigSchema/Interchange/Validator/IdExists.php';
|
||||||
require 'HTMLPurifier/DefinitionCache/Decorator.php';
|
require 'HTMLPurifier/DefinitionCache/Decorator.php';
|
||||||
require 'HTMLPurifier/DefinitionCache/Null.php';
|
require 'HTMLPurifier/DefinitionCache/Null.php';
|
||||||
require 'HTMLPurifier/DefinitionCache/Serializer.php';
|
require 'HTMLPurifier/DefinitionCache/Serializer.php';
|
||||||
|
@ -11,11 +11,11 @@ class HTMLPurifier_ConfigSchema_Interchange
|
|||||||
/**
|
/**
|
||||||
* Hash table of allowed types.
|
* Hash table of allowed types.
|
||||||
*/
|
*/
|
||||||
public $types = array(
|
protected $types = array(
|
||||||
'string' => 'String',
|
'string' => 'String',
|
||||||
'istring' => 'Case-insensitive string',
|
'istring' => 'Case-insensitive string',
|
||||||
'text' => 'Text',
|
'text' => 'Text',
|
||||||
'itext' => 'Case-insensitive text',
|
'itext' => 'Case-insensitive text',
|
||||||
'int' => 'Integer',
|
'int' => 'Integer',
|
||||||
'float' => 'Float',
|
'float' => 'Float',
|
||||||
'bool' => 'Boolean',
|
'bool' => 'Boolean',
|
||||||
@ -28,18 +28,34 @@ class HTMLPurifier_ConfigSchema_Interchange
|
|||||||
/**
|
/**
|
||||||
* Array of Namespace ID => array(namespace info)
|
* Array of Namespace ID => array(namespace info)
|
||||||
*/
|
*/
|
||||||
public $namespaces;
|
protected $namespaces;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Array of Directive ID => array(directive info)
|
* Array of Directive ID => array(directive info)
|
||||||
*/
|
*/
|
||||||
public $directives;
|
protected $directives;
|
||||||
|
|
||||||
|
/** Get all namespaces */
|
||||||
|
public function getNamespaces() {return $this->namespaces;}
|
||||||
|
/** Get a namespace */
|
||||||
|
public function getNamespace($id) {return $this->namespaces[$id];}
|
||||||
|
/** Check if a namespace exists */
|
||||||
|
public function namespaceExists($id) {return isset($this->namespaces[$id]);}
|
||||||
|
|
||||||
|
/** Get all directives */
|
||||||
|
public function getDirectives() {return $this->directives;}
|
||||||
|
/** Get a directive */
|
||||||
|
public function getDirective($id) {return $this->directives[$id];}
|
||||||
|
/** Check if a directive exists */
|
||||||
|
public function directiveExists($id) {return isset($this->directives[$id]);}
|
||||||
|
|
||||||
|
/** Get all types */
|
||||||
|
public function getTypes() {return $this->types;}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a namespace array to $namespaces
|
* Adds a namespace array to $namespaces
|
||||||
*/
|
*/
|
||||||
public function addNamespace($arr) {
|
public function addNamespace($arr) {
|
||||||
if (!isset($arr['ID'])) throw new HTMLPurifier_ConfigSchema_Exception('Namespace must have ID');
|
|
||||||
$this->namespaces[$arr['ID']] = $arr;
|
$this->namespaces[$arr['ID']] = $arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,8 +63,17 @@ class HTMLPurifier_ConfigSchema_Interchange
|
|||||||
* Adds a directive array to $directives
|
* Adds a directive array to $directives
|
||||||
*/
|
*/
|
||||||
public function addDirective($arr) {
|
public function addDirective($arr) {
|
||||||
if (!isset($arr['ID'])) throw new HTMLPurifier_ConfigSchema_Exception('Directive must have ID');
|
|
||||||
$this->directives[$arr['ID']] = $arr;
|
$this->directives[$arr['ID']] = $arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a version of this object wrapped in the validator adapter
|
||||||
|
* to be used for data-input.
|
||||||
|
*/
|
||||||
|
public function getValidatorAdapter() {
|
||||||
|
return
|
||||||
|
new HTMLPurifier_ConfigSchema_Interchange_Validator_IdExists(
|
||||||
|
$this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
43
library/HTMLPurifier/ConfigSchema/Interchange/Validator.php
Normal file
43
library/HTMLPurifier/ConfigSchema/Interchange/Validator.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base decorator class for HTMLPurifier_ConfigSchema_Interchange
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_ConfigSchema_Interchange_Validator extends HTMLPurifier_ConfigSchema_Interchange
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Interchange object this schema is wrapping.
|
||||||
|
*/
|
||||||
|
protected $interchange;
|
||||||
|
|
||||||
|
/** @param Object to decorate */
|
||||||
|
public function __construct($i = null) {
|
||||||
|
$this->decorate($i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Wrap this decorator around an object. */
|
||||||
|
public function decorate($i) {
|
||||||
|
$this->interchange = $i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getNamespaces() {
|
||||||
|
return $this->interchange->getNamespaces();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDirectives() {
|
||||||
|
return $this->interchange->getDirectives();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTypes() {
|
||||||
|
return $this->interchange->getTypes();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addNamespace($arr) {
|
||||||
|
$this->interchange->addNamespace($arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addDirective($arr) {
|
||||||
|
$this->interchange->addNamespace($arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class HTMLPurifier_ConfigSchema_Interchange_Validator_IdExists extends HTMLPurifier_ConfigSchema_Interchange_Validator
|
||||||
|
{
|
||||||
|
|
||||||
|
public function addNamespace($arr) {
|
||||||
|
if (!isset($arr['ID'])) {
|
||||||
|
throw new HTMLPurifier_ConfigSchema_Exception('Namespace must have ID');
|
||||||
|
}
|
||||||
|
parent::addNamespace($arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addDirective($arr) {
|
||||||
|
if (!isset($arr['ID'])) {
|
||||||
|
throw new HTMLPurifier_ConfigSchema_Exception('Directive must have ID');
|
||||||
|
}
|
||||||
|
parent::addDirective($arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class HTMLPurifier_ConfigSchema_Interchange_Validator_IdExistsTest extends HTMLPurifier_ConfigSchema_Interchange_ValidatorHarness
|
||||||
|
{
|
||||||
|
|
||||||
|
public function setup() {
|
||||||
|
parent::setup();
|
||||||
|
$this->validator = new HTMLPurifier_ConfigSchema_Interchange_Validator_IdExists($this->mock);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNamespace() {
|
||||||
|
$this->mock->expectNever('addNamespace');
|
||||||
|
$this->expectSchemaException('Namespace must have ID');
|
||||||
|
$this->validator->addNamespace();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDirective() {
|
||||||
|
$this->mock->expectNever('addDirective');
|
||||||
|
$this->expectSchemaException('Directive must have ID');
|
||||||
|
$this->validator->addDirective();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class HTMLPurifier_ConfigSchema_Interchange_ValidatorHarness extends UnitTestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $validator;
|
||||||
|
protected $mock;
|
||||||
|
|
||||||
|
public function setup() {
|
||||||
|
generate_mock_once('HTMLPurifier_ConfigSchema_Interchange');
|
||||||
|
$this->mock = new HTMLPurifier_ConfigSchema_InterchangeMock();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function expectSchemaException($msg) {
|
||||||
|
$this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -12,33 +12,17 @@ class HTMLPurifier_ConfigSchema_InterchangeTest extends UnitTestCase
|
|||||||
public function testAddNamespace() {
|
public function testAddNamespace() {
|
||||||
$this->interchange->addNamespace($v = array(
|
$this->interchange->addNamespace($v = array(
|
||||||
'ID' => 'Namespace',
|
'ID' => 'Namespace',
|
||||||
'Foo' => 'Bar',
|
'DESCRIPTION' => 'Bar',
|
||||||
));
|
));
|
||||||
$this->assertIdentical($v, $this->interchange->namespaces['Namespace']);
|
$this->assertIdentical($v, $this->interchange->getNamespace('Namespace'));
|
||||||
}
|
|
||||||
|
|
||||||
public function testAddNamespaceError() {
|
|
||||||
try {
|
|
||||||
$this->interchange->addNamespace(array());
|
|
||||||
} catch (HTMLPurifier_ConfigSchema_Exception $e) {
|
|
||||||
$this->assertIdentical($e->getMessage(), 'Namespace must have ID');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddDirective() {
|
public function testAddDirective() {
|
||||||
$this->interchange->addDirective($v = array(
|
$this->interchange->addDirective($v = array(
|
||||||
'ID' => 'Namespace.Directive',
|
'ID' => 'Namespace.Directive',
|
||||||
'Foo' => 'Bar',
|
'DESCRIPTION' => 'Bar',
|
||||||
));
|
));
|
||||||
$this->assertIdentical($v, $this->interchange->directives['Namespace.Directive']);
|
$this->assertIdentical($v, $this->interchange->getDirective('Namespace.Directive'));
|
||||||
}
|
|
||||||
|
|
||||||
public function testAddDirectiveError() {
|
|
||||||
try {
|
|
||||||
$this->interchange->addDirective(array());
|
|
||||||
} catch (HTMLPurifier_ConfigSchema_Exception $e) {
|
|
||||||
$this->assertIdentical($e->getMessage(), 'Directive must have ID');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,7 @@ $test_files[] = 'HTMLPurifier/ChildDef/StrictBlockquoteTest.php';
|
|||||||
$test_files[] = 'HTMLPurifier/ChildDef/TableTest.php';
|
$test_files[] = 'HTMLPurifier/ChildDef/TableTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/ConfigSchemaTest.php';
|
$test_files[] = 'HTMLPurifier/ConfigSchemaTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/ConfigSchema/InterchangeTest.php';
|
$test_files[] = 'HTMLPurifier/ConfigSchema/InterchangeTest.php';
|
||||||
|
$test_files[] = 'HTMLPurifier/ConfigSchema/Interchange/Validator/IdExistsTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/ConfigSchema/StringHashAdapterTest.php';
|
$test_files[] = 'HTMLPurifier/ConfigSchema/StringHashAdapterTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/ConfigSchema/StringHashReverseAdapterTest.php';
|
$test_files[] = 'HTMLPurifier/ConfigSchema/StringHashReverseAdapterTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/ConfigSchema/StringHashParserTest.php';
|
$test_files[] = 'HTMLPurifier/ConfigSchema/StringHashParserTest.php';
|
||||||
|
Loading…
Reference in New Issue
Block a user