mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-09 23:28:42 +00:00
- Added Composite and Core strategies.
- Added generate_mock() function for testing - Factored out inputs/output tests to StrategyAbstractTest git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@124 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
39836fe278
commit
9b7ad89ab5
23
library/HTMLPurifier/Strategy/Composite.php
Normal file
23
library/HTMLPurifier/Strategy/Composite.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Strategy.php';
|
||||
|
||||
class HTMLPurifier_Strategy_Composite
|
||||
{
|
||||
|
||||
var $strategies = array();
|
||||
|
||||
function HTMLPurifier_Strategy_Composite() {
|
||||
trigger_error('Attempt to instantiate abstract object', E_USER_ERROR);
|
||||
}
|
||||
|
||||
function execute($tokens) {
|
||||
foreach ($this->strategies as $strategy) {
|
||||
$tokens = $strategy->execute($tokens);
|
||||
}
|
||||
return $tokens;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
20
library/HTMLPurifier/Strategy/Core.php
Normal file
20
library/HTMLPurifier/Strategy/Core.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Strategy/Composite.php';
|
||||
|
||||
require_once 'HTMLPurifier/Strategy/RemoveForeignElements.php';
|
||||
require_once 'HTMLPurifier/Strategy/MakeWellFormed.php';
|
||||
require_once 'HTMLPurifier/Strategy/FixNesting.php';
|
||||
|
||||
class HTMLPurifier_Strategy_Core extends HTMLPurifier_Strategy_Composite
|
||||
{
|
||||
|
||||
function HTMLPurifier_Strategy_Core() {
|
||||
$this->strategies[] = new HTMLPurifier_Strategy_RemoveForeignElements();
|
||||
$this->strategies[] = new HTMLPurifier_Strategy_MakeWellFormed();
|
||||
$this->strategies[] = new HTMLPurifier_Strategy_FixNesting();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
66
tests/HTMLPurifier/Strategy/CompositeTest.php
Normal file
66
tests/HTMLPurifier/Strategy/CompositeTest.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
require_once('HTMLPurifier/Strategy.php');
|
||||
require_once('HTMLPurifier/Strategy/Composite.php');
|
||||
|
||||
class HTMLPurifier_Strategy_Composite_Test
|
||||
extends HTMLPurifier_Strategy_Composite
|
||||
{
|
||||
|
||||
function HTMLPurifier_Strategy_Composite_Test(&$strategies) {
|
||||
$this->strategies =& $strategies;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// doesn't use Strategy harness
|
||||
class HTMLPurifier_Strategy_CompositeTest extends UnitTestCase
|
||||
{
|
||||
|
||||
function test() {
|
||||
|
||||
generate_mock_once('HTMLPurifier_Strategy');
|
||||
|
||||
// setup a bunch of mock strategies to inject into our composite test
|
||||
|
||||
$mock_1 = new HTMLPurifier_StrategyMock($this);
|
||||
$mock_2 = new HTMLPurifier_StrategyMock($this);
|
||||
$mock_3 = new HTMLPurifier_StrategyMock($this);
|
||||
|
||||
// setup the object
|
||||
|
||||
$strategies = array($mock_1, $mock_2, $mock_3);
|
||||
$composite = new HTMLPurifier_Strategy_Composite_Test($strategies);
|
||||
|
||||
// setup expectations
|
||||
|
||||
$input_1 = 'This is raw data';
|
||||
$input_2 = 'Processed by 1';
|
||||
$input_3 = 'Processed by 1 and 2';
|
||||
$input_4 = 'Processed by 1, 2 and 3'; // expected output
|
||||
|
||||
$mock_1->expectOnce('execute', array($input_1));
|
||||
$mock_1->setReturnValue('execute', $input_2, array($input_1));
|
||||
|
||||
$mock_2->expectOnce('execute', array($input_2));
|
||||
$mock_2->setReturnValue('execute', $input_3, array($input_2));
|
||||
|
||||
$mock_3->expectOnce('execute', array($input_3));
|
||||
$mock_3->setReturnValue('execute', $input_4, array($input_3));
|
||||
|
||||
// perform test
|
||||
|
||||
$output = $composite->execute($input_1);
|
||||
$this->assertIdentical($input_4, $output);
|
||||
|
||||
// tally the calls
|
||||
|
||||
$mock_1->tally();
|
||||
$mock_2->tally();
|
||||
$mock_3->tally();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
39
tests/HTMLPurifier/Strategy/CoreTest.php
Normal file
39
tests/HTMLPurifier/Strategy/CoreTest.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/StrategyAbstractTest.php';
|
||||
require_once 'HTMLPurifier/Strategy/Core.php';
|
||||
|
||||
class HTMLPurifier_Strategy_CoreTest
|
||||
extends HTMLPurifier_StrategyAbstractTest
|
||||
{
|
||||
|
||||
function test() {
|
||||
$strategy = new HTMLPurifier_Strategy_Core();
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
$inputs[0] = '';
|
||||
$expect[0] = '';
|
||||
|
||||
$inputs[1] = '<b>Make well formed.';
|
||||
$expect[1] = '<b>Make well formed.</b>';
|
||||
|
||||
// behavior may change
|
||||
$inputs[2] = '<b><div>Fix nesting.</div></b>';
|
||||
$expect[2] = '<b><div>Fix nesting.</div></b>';
|
||||
|
||||
// behavior may change
|
||||
$inputs[3] = '<asdf>Foreign element removal.</asdf>';
|
||||
$expect[3] = '<asdf>Foreign element removal.</asdf>';
|
||||
|
||||
// behavior may change
|
||||
$inputs[4] = '<foo><b><div>All three.</div></b>';
|
||||
$expect[4] = '<foo><b><div>All three.</div></b>';
|
||||
|
||||
$this->assertStrategyWorks($strategy, $inputs, $expect);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -37,13 +37,7 @@ class HTMLPurifier_Strategy_FixNestingTest
|
||||
$inputs[4] = '<ul>Illegal text<li>Legal item</li></ul>';
|
||||
$expect[4] = '<ul><li>Legal item</li></ul>';
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$tokens = $this->lex->tokenizeHTML($input);
|
||||
$result_tokens = $strategy->execute($tokens);
|
||||
$result = $this->gen->generateFromTokens($result_tokens);
|
||||
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
$this->assertStrategyWorks($strategy, $inputs, $expect);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -49,14 +49,7 @@ class HTMLPurifier_Strategy_MakeWellFormedTest
|
||||
$inputs[9] = '<ol><li>Item 1<li>Item 2</ol>';
|
||||
$expect[9] = '<ol><li>Item 1</li><li>Item 2</li></ol>';
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$tokens = $this->lex->tokenizeHTML($input);
|
||||
$result_tokens = $strategy->execute($tokens);
|
||||
$result = $this->gen->generateFromTokens($result_tokens);
|
||||
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
$this->assertStrategyWorks($strategy, $inputs, $expect);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,14 +24,7 @@ class HTMLPurifier_Strategy_RemoveForeignElementsTest
|
||||
$inputs[2] = '<asdf>Bling</asdf><d href="bang">Bong</d><foobar />';
|
||||
$expect[2] = htmlspecialchars($inputs[2]);
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$tokens = $this->lex->tokenizeHTML($input);
|
||||
$result_tokens = $strategy->execute($tokens);
|
||||
$result = $this->gen->generateFromTokens($result_tokens);
|
||||
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
$this->assertStrategyWorks($strategy, $inputs, $expect);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,6 +25,16 @@ class HTMLPurifier_StrategyAbstractTest extends UnitTestCase
|
||||
$this->gen = new HTMLPurifier_Generator();
|
||||
}
|
||||
|
||||
function assertStrategyWorks($strategy, $inputs, $expect) {
|
||||
foreach ($inputs as $i => $input) {
|
||||
$tokens = $this->lex->tokenizeHTML($input);
|
||||
$result_tokens = $strategy->execute($tokens);
|
||||
$result = $this->gen->generateFromTokens($result_tokens);
|
||||
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -11,6 +11,14 @@ require_once 'Debugger.php';
|
||||
// emulates inserting a dir called HTMLPurifier into your class dir
|
||||
set_include_path(get_include_path() . PATH_SEPARATOR . '../library');
|
||||
|
||||
// since Mocks can't be called from within test files, we need to do
|
||||
// a little jumping through hoops to generate them
|
||||
function generate_mock_once($name) {
|
||||
$mock_name = $name . 'Mock';
|
||||
if (class_exists($mock_name)) return false;
|
||||
Mock::generate($name, $mock_name);
|
||||
}
|
||||
|
||||
$test = new GroupTest('HTMLPurifier');
|
||||
|
||||
$test->addTestFile('HTMLPurifier/LexerTest.php');
|
||||
@ -22,6 +30,8 @@ $test->addTestFile('HTMLPurifier/EntityLookupTest.php');
|
||||
$test->addTestFile('HTMLPurifier/Strategy/RemoveForeignElementsTest.php');
|
||||
$test->addTestFile('HTMLPurifier/Strategy/MakeWellFormedTest.php');
|
||||
$test->addTestFile('HTMLPurifier/Strategy/FixNestingTest.php');
|
||||
$test->addTestFile('HTMLPurifier/Strategy/CompositeTest.php');
|
||||
$test->addTestFile('HTMLPurifier/Strategy/CoreTest.php');
|
||||
|
||||
$test->run( new HtmlReporter() );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user