mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-12 16:38:40 +00:00
Refactor validators so that they can be reused between directives and namespaces.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1589 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
18320d59a4
commit
d8cb360f3b
@ -131,7 +131,8 @@ 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/Validator.php';
|
require 'HTMLPurifier/ConfigSchema/Validator.php';
|
||||||
require 'HTMLPurifier/ConfigSchema/Validator/IdExists.php';
|
require 'HTMLPurifier/ConfigSchema/Validator/Alnum.php';
|
||||||
|
require 'HTMLPurifier/ConfigSchema/Validator/Exists.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';
|
||||||
|
@ -55,7 +55,13 @@ class HTMLPurifier_ConfigSchema_Interchange
|
|||||||
*/
|
*/
|
||||||
public function getValidatorAdapter() {
|
public function getValidatorAdapter() {
|
||||||
$validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this);
|
$validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this);
|
||||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_IdExists());
|
// Common validators
|
||||||
|
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ID'));
|
||||||
|
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('DESCRIPTION'));
|
||||||
|
// Namespace validators
|
||||||
|
|
||||||
|
// Directive validators
|
||||||
|
|
||||||
return $validator;
|
return $validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,9 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
|||||||
{
|
{
|
||||||
protected $interchange;
|
protected $interchange;
|
||||||
protected $validators = array();
|
protected $validators = array();
|
||||||
|
protected $namespaceValidators = array();
|
||||||
|
protected $directiveVaildators = array();
|
||||||
|
protected $index = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $interchange Instance of HTMLPurifier_ConfigSchema_Interchange
|
* @param $interchange Instance of HTMLPurifier_ConfigSchema_Interchange
|
||||||
@ -20,15 +23,32 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
|||||||
* Registers a HTMLPurifier_ConfigSchema_Validator to run when adding.
|
* Registers a HTMLPurifier_ConfigSchema_Validator to run when adding.
|
||||||
*/
|
*/
|
||||||
public function addValidator($validator) {
|
public function addValidator($validator) {
|
||||||
$this->validators[] = $validator;
|
$this->validators[$this->index++] = $validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register validators to be used only on directives
|
||||||
|
*/
|
||||||
|
public function addDirectiveValidator($validator) {
|
||||||
|
$this->directiveValidators[$this->index++] = $validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register validators to be used only on namespaces
|
||||||
|
*/
|
||||||
|
public function addNamespaceValidator($validator) {
|
||||||
|
$this->namespaceValidators[$this->index++] = $validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates and adds a namespace hash
|
* Validates and adds a namespace hash
|
||||||
*/
|
*/
|
||||||
public function addNamespace($hash) {
|
public function addNamespace($hash) {
|
||||||
foreach ($this->validators as $validator) {
|
for ($i = 0; $i < $this->index; $i++) {
|
||||||
$validator->validateNamespace($hash, $this->interchange);
|
if (isset($this->validators[$i])) $validator = $this->validators[$i];
|
||||||
|
elseif (isset($this->namespaceValidators[$i])) $validator = $this->namespaceValidators[$i];
|
||||||
|
else continue;
|
||||||
|
$validator->validate($hash, $this->interchange);
|
||||||
}
|
}
|
||||||
$this->interchange->addNamespace($hash);
|
$this->interchange->addNamespace($hash);
|
||||||
}
|
}
|
||||||
@ -37,8 +57,11 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
|||||||
* Validates and adds a directive hash
|
* Validates and adds a directive hash
|
||||||
*/
|
*/
|
||||||
public function addDirective($hash) {
|
public function addDirective($hash) {
|
||||||
foreach ($this->validators as $validator) {
|
for ($i = 0; $i < $this->index; $i++) {
|
||||||
$validator->validateDirective($hash, $this->interchange);
|
if (isset($this->validators[$i])) $validator = $this->validators[$i];
|
||||||
|
elseif (isset($this->directiveValidators[$i])) $validator = $this->directiveValidators[$i];
|
||||||
|
else continue;
|
||||||
|
$validator->validate($hash, $this->interchange);
|
||||||
}
|
}
|
||||||
$this->interchange->addDirective($hash);
|
$this->interchange->addDirective($hash);
|
||||||
}
|
}
|
||||||
|
@ -6,34 +6,14 @@
|
|||||||
class HTMLPurifier_ConfigSchema_Validator
|
class HTMLPurifier_ConfigSchema_Validator
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates and filters a namespace.
|
|
||||||
*/
|
|
||||||
public function validateNamespace(&$arr, $interchange) {
|
|
||||||
$this->validate($arr, $interchange, 'namespace');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates and filters a directive.
|
|
||||||
*/
|
|
||||||
public function validateDirective(&$arr, $interchange) {
|
|
||||||
$this->validate($arr, $interchange, 'directive');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common validator, throwing an exception on error. It can
|
* Common validator, throwing an exception on error. It can
|
||||||
* also performing filtering or evaluation functions.
|
* also performing filtering or evaluation functions.
|
||||||
*
|
*
|
||||||
* @note This is strictly for convenience reasons when subclasing.
|
|
||||||
*
|
|
||||||
* @param $arr Array to validate.
|
* @param $arr Array to validate.
|
||||||
* @param $interchange HTMLPurifier_ConfigSchema_Interchange object
|
* @param $interchange HTMLPurifier_ConfigSchema_Interchange object
|
||||||
* that is being processed.
|
* that is being processed.
|
||||||
* @param $type Type of object being validated, this saves a little work
|
|
||||||
* if only cosmetic changes are being made between namespaces
|
|
||||||
* and directives.
|
|
||||||
*/
|
*/
|
||||||
protected function validate(&$arr, $interchange, $type) {}
|
public function validate(&$arr, $interchange) {}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
22
library/HTMLPurifier/ConfigSchema/Validator/Alnum.php
Normal file
22
library/HTMLPurifier/ConfigSchema/Validator/Alnum.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates that a field is alphanumeric in the array (does not check
|
||||||
|
* existence!)
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_ConfigSchema_Validator_Alnum extends HTMLPurifier_ConfigSchema_Validator
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $index;
|
||||||
|
|
||||||
|
public function __construct($index) {
|
||||||
|
$this->index = $index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate(&$arr, $interchange) {
|
||||||
|
if (!ctype_alnum($arr[$this->index])) {
|
||||||
|
throw new HTMLPurifier_ConfigSchema_Exception($arr[$this->index] . ' in '. $this->index .' must be alphanumeric');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
21
library/HTMLPurifier/ConfigSchema/Validator/Exists.php
Normal file
21
library/HTMLPurifier/ConfigSchema/Validator/Exists.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates that an field exists in the array
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_ConfigSchema_Validator_Exists extends HTMLPurifier_ConfigSchema_Validator
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $index;
|
||||||
|
|
||||||
|
public function __construct($index) {
|
||||||
|
$this->index = $index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate(&$arr, $interchange) {
|
||||||
|
if (empty($arr[$this->index])) {
|
||||||
|
throw new HTMLPurifier_ConfigSchema_Exception($this->index . ' must exist');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates that an ID field exists in the array
|
|
||||||
*/
|
|
||||||
class HTMLPurifier_ConfigSchema_Validator_IdExists extends HTMLPurifier_ConfigSchema_Validator
|
|
||||||
{
|
|
||||||
|
|
||||||
public function validate(&$arr, $interchange, $type) {
|
|
||||||
if (!isset($arr['ID'])) {
|
|
||||||
throw new HTMLPurifier_ConfigSchema_Exception('ID must exist in ' . $type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -27,7 +27,7 @@ class HTMLPurifier_ConfigSchema_InterchangeTest extends UnitTestCase
|
|||||||
|
|
||||||
public function testValidator() {
|
public function testValidator() {
|
||||||
$adapter = $this->interchange->getValidatorAdapter();
|
$adapter = $this->interchange->getValidatorAdapter();
|
||||||
$this->expectException(new HTMLPurifier_ConfigSchema_Exception('ID must exist in directive'));
|
$this->expectException(new HTMLPurifier_ConfigSchema_Exception('ID must exist'));
|
||||||
$adapter->addDirective(array());
|
$adapter->addDirective(array());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,10 +9,11 @@ class HTMLPurifier_ConfigSchema_InterchangeValidatorTest extends UnitTestCase
|
|||||||
$this->validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this->mock);
|
$this->validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this->mock);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function makeValidator($expect_method, $expect_params) {
|
protected function makeValidator($expect_params = null) {
|
||||||
generate_mock_once('HTMLPurifier_ConfigSchema_Validator');
|
generate_mock_once('HTMLPurifier_ConfigSchema_Validator');
|
||||||
$validator = new HTMLPurifier_ConfigSchema_ValidatorMock();
|
$validator = new HTMLPurifier_ConfigSchema_ValidatorMock();
|
||||||
$validator->expectOnce($expect_method, $expect_params);
|
if ($expect_params !== null) $validator->expectOnce('validate', $expect_params);
|
||||||
|
else $validator->expectNever('validate');
|
||||||
return $validator;
|
return $validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,10 +25,20 @@ class HTMLPurifier_ConfigSchema_InterchangeValidatorTest extends UnitTestCase
|
|||||||
|
|
||||||
public function testAddNamespaceWithValidators() {
|
public function testAddNamespaceWithValidators() {
|
||||||
$hash = array('ID' => 'Namespace');
|
$hash = array('ID' => 'Namespace');
|
||||||
$this->validator->addValidator($this->makeValidator('validateNamespace', array($hash, $this->mock)));
|
$this->validator->addValidator($this->makeValidator(array($hash, $this->mock)));
|
||||||
$this->validator->addValidator($this->makeValidator('validateNamespace', array($hash, $this->mock)));
|
$this->validator->addNamespaceValidator($this->makeValidator(array($hash, $this->mock)));
|
||||||
|
$this->validator->addDirectiveValidator($this->makeValidator()); // not called
|
||||||
$this->mock->expectOnce('addNamespace', array($hash));
|
$this->mock->expectOnce('addNamespace', array($hash));
|
||||||
$this->validator->addNamespace($hash);
|
$this->validator->addNamespace($hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAddDirectiveWithValidators() {
|
||||||
|
$hash = array('ID' => 'Namespace.Directive');
|
||||||
|
$this->validator->addValidator($this->makeValidator(array($hash, $this->mock)));
|
||||||
|
$this->validator->addNamespaceValidator($this->makeValidator()); // not called
|
||||||
|
$this->validator->addDirectiveValidator($this->makeValidator(array($hash, $this->mock)));
|
||||||
|
$this->mock->expectOnce('addDirective', array($hash));
|
||||||
|
$this->validator->addDirective($hash);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
17
tests/HTMLPurifier/ConfigSchema/Validator/AlnumTest.php
Normal file
17
tests/HTMLPurifier/ConfigSchema/Validator/AlnumTest.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class HTMLPurifier_ConfigSchema_Validator_AlnumTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
|
||||||
|
{
|
||||||
|
|
||||||
|
public function setup() {
|
||||||
|
parent::setup();
|
||||||
|
$this->validator = new HTMLPurifier_ConfigSchema_Validator_Alnum('ID');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidate() {
|
||||||
|
$this->expectSchemaException('R&D in ID must be alphanumeric');
|
||||||
|
$arr = array('ID' => 'R&D');
|
||||||
|
$this->validator->validate($arr, $this->interchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
17
tests/HTMLPurifier/ConfigSchema/Validator/ExistsTest.php
Normal file
17
tests/HTMLPurifier/ConfigSchema/Validator/ExistsTest.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class HTMLPurifier_ConfigSchema_Validator_ExistsTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
|
||||||
|
{
|
||||||
|
|
||||||
|
public function setup() {
|
||||||
|
parent::setup();
|
||||||
|
$this->validator = new HTMLPurifier_ConfigSchema_Validator_Exists('ID');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidate() {
|
||||||
|
$this->expectSchemaException('ID must exist');
|
||||||
|
$arr = array();
|
||||||
|
$this->validator->validate($arr, $this->interchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
class HTMLPurifier_ConfigSchema_Validator_IdExistsTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
|
|
||||||
{
|
|
||||||
|
|
||||||
public function setup() {
|
|
||||||
parent::setup();
|
|
||||||
$this->validator = new HTMLPurifier_ConfigSchema_Validator_IdExists();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testValidateNamespace() {
|
|
||||||
$this->expectSchemaException('ID must exist in namespace');
|
|
||||||
$arr = array();
|
|
||||||
$this->validator->validateNamespace($arr, $this->interchange);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testValidateDirective() {
|
|
||||||
$this->expectSchemaException('ID must exist in directive');
|
|
||||||
$arr = array();
|
|
||||||
$this->validator->validateDirective($arr, $this->interchange);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user