diff --git a/NEWS b/NEWS index c5988571..82ee502c 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier 1.2.0, unknown projected release date . Switched to purify()-wide Context object registry +. Refactored unit tests to minimize duplication 1.1.3, unknown projected release date (bugfix release, may be dropped if no major bugs are found before features) diff --git a/library/HTMLPurifier/AttrContext.php b/library/HTMLPurifier/AttrContext.php deleted file mode 100644 index 5f0c1679..00000000 --- a/library/HTMLPurifier/AttrContext.php +++ /dev/null @@ -1,26 +0,0 @@ - \ No newline at end of file diff --git a/library/HTMLPurifier/AttrDef.php b/library/HTMLPurifier/AttrDef.php index 3d04c752..4abe3acc 100644 --- a/library/HTMLPurifier/AttrDef.php +++ b/library/HTMLPurifier/AttrDef.php @@ -1,7 +1,5 @@ id_accumulator->ids[$id])) return false; + + $id_accumulator = $context->get('IDAccumulator'); + if (isset($id_accumulator->ids[$id])) return false; // we purposely avoid using regex, hopefully this is faster @@ -35,7 +37,7 @@ class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef $result = ($trim === ''); } - if ($result) $context->id_accumulator->add($id); + if ($result) $id_accumulator->add($id); // if no change was made to the ID, return the result // else, return the new id if stripping whitespace made it diff --git a/library/HTMLPurifier/Generator.php b/library/HTMLPurifier/Generator.php index 72ca3921..4f2b9cf6 100644 --- a/library/HTMLPurifier/Generator.php +++ b/library/HTMLPurifier/Generator.php @@ -60,9 +60,8 @@ class HTMLPurifier_Generator * @param $tokens Array of HTMLPurifier_Token * @param $config HTMLPurifier_Config object * @return Generated HTML - * @note Only unit tests may omit configuration: internals MUST pass config */ - function generateFromTokens($tokens, $config = null) { + function generateFromTokens($tokens, $config, &$context) { $html = ''; if (!$config) $config = HTMLPurifier_Config::createDefault(); $this->_clean_utf8 = $config->get('Core', 'CleanUTF8DuringGeneration'); diff --git a/library/HTMLPurifier/Strategy/ValidateAttributes.php b/library/HTMLPurifier/Strategy/ValidateAttributes.php index c4cd02b8..2aae1bd7 100644 --- a/library/HTMLPurifier/Strategy/ValidateAttributes.php +++ b/library/HTMLPurifier/Strategy/ValidateAttributes.php @@ -3,8 +3,6 @@ require_once 'HTMLPurifier/Strategy.php'; require_once 'HTMLPurifier/HTMLDefinition.php'; require_once 'HTMLPurifier/IDAccumulator.php'; -require_once 'HTMLPurifier/ConfigSchema.php'; -require_once 'HTMLPurifier/AttrContext.php'; HTMLPurifier_ConfigSchema::define( 'Attr', 'IDBlacklist', array(), 'list', @@ -21,14 +19,10 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy $definition = $config->getHTMLDefinition(); - // setup StrategyContext - $attr_context = new HTMLPurifier_AttrContext(); - - // setup ID accumulator and load it with blacklisted IDs - // eventually, we'll have a dedicated context object to hold - // all these accumulators and caches. For now, just an IDAccumulator - $attr_context->id_accumulator = new HTMLPurifier_IDAccumulator(); - $attr_context->id_accumulator->load($config->get('Attr', 'IDBlacklist')); + // setup id_accumulator context + $id_accumulator = new HTMLPurifier_IDAccumulator(); + $id_accumulator->load($config->get('Attr', 'IDBlacklist')); + $context->register('IDAccumulator', $id_accumulator); // create alias to global definition array, see also $defs // DEFINITION CALL @@ -81,14 +75,14 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy } else { // validate according to the element's definition $result = $defs[$attr_key]->validate( - $value, $config, $attr_context + $value, $config, $context ); } } elseif ( isset($d_defs[$attr_key]) ) { // there is a global definition defined, validate according // to the global definition $result = $d_defs[$attr_key]->validate( - $value, $config, $attr_context + $value, $config, $context ); } else { // system never heard of the attribute? DELETE! @@ -123,6 +117,8 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy // could interfere with flyweight implementation $tokens[$key]->attributes = $attr; } + $context->destroy('IDAccumulator'); + return $tokens; } diff --git a/tests/HTMLPurifier/AttrDef/CompositeTest.php b/tests/HTMLPurifier/AttrDef/CompositeTest.php index c2ac8319..9c49a289 100644 --- a/tests/HTMLPurifier/AttrDef/CompositeTest.php +++ b/tests/HTMLPurifier/AttrDef/CompositeTest.php @@ -23,7 +23,7 @@ class HTMLPurifier_AttrDef_CompositeTest extends HTMLPurifier_AttrDefHarness generate_mock_once('HTMLPurifier_AttrDef'); $config = HTMLPurifier_Config::createDefault(); - $context = new HTMLPurifier_AttrContext(); + $context = new HTMLPurifier_Context(); // first test: value properly validates on first definition // so second def is never called diff --git a/tests/HTMLPurifier/AttrDef/IDTest.php b/tests/HTMLPurifier/AttrDef/IDTest.php index 4248102a..87b5670c 100644 --- a/tests/HTMLPurifier/AttrDef/IDTest.php +++ b/tests/HTMLPurifier/AttrDef/IDTest.php @@ -9,8 +9,9 @@ class HTMLPurifier_AttrDef_IDTest extends HTMLPurifier_AttrDefHarness function test() { - $this->context = new HTMLPurifier_AttrContext(); - $this->context->id_accumulator = new HTMLPurifier_IDAccumulator(); + $this->context = new HTMLPurifier_Context(); + $id_accumulator = new HTMLPurifier_IDAccumulator(); + $this->context->register('IDAccumulator', $id_accumulator); $this->def = new HTMLPurifier_AttrDef_ID(); // valid ID names diff --git a/tests/HTMLPurifier/AttrDefHarness.php b/tests/HTMLPurifier/AttrDefHarness.php index 727c5db6..d98eb9bf 100644 --- a/tests/HTMLPurifier/AttrDefHarness.php +++ b/tests/HTMLPurifier/AttrDefHarness.php @@ -11,7 +11,7 @@ class HTMLPurifier_AttrDefHarness extends UnitTestCase function assertDef($string, $expect = true, $ini = false, $message = '%s') { // $expect can be a string or bool if (!$this->config) $this->config = HTMLPurifier_Config::createDefault(); - if (!$this->context) $this->context = new HTMLPurifier_AttrContext(); + if (!$this->context) $this->context = new HTMLPurifier_Context(); if ($ini) $this->setUpAssertDef(); $result = $this->def->validate($string, $this->config, $this->context); if ($expect === true) { diff --git a/tests/HTMLPurifier/GeneratorTest.php b/tests/HTMLPurifier/GeneratorTest.php index a6ca4043..6bd1b59d 100644 --- a/tests/HTMLPurifier/GeneratorTest.php +++ b/tests/HTMLPurifier/GeneratorTest.php @@ -3,7 +3,9 @@ require_once 'HTMLPurifier/Generator.php'; require_once 'HTMLPurifier/EntityLookup.php'; -class HTMLPurifier_GeneratorTest extends UnitTestCase +require_once 'HTMLPurifier/Harness.php'; + +class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness { var $gen; @@ -15,11 +17,16 @@ class HTMLPurifier_GeneratorTest extends UnitTestCase $this->_entity_lookup = HTMLPurifier_EntityLookup::instance(); } + function setUp() { + $this->obj = new HTMLPurifier_Generator(); + $this->func = null; + $this->to_tokens = false; + $this->to_html = false; + } + function test_generateFromToken() { - $inputs = array(); - $expect = array(); - $config = array(); + $inputs = $expect = array(); $inputs[0] = new HTMLPurifier_Token_Text('Foobar.<>'); $expect[0] = 'Foobar.<>'; @@ -53,7 +60,7 @@ class HTMLPurifier_GeneratorTest extends UnitTestCase $expect[7] = $theta_char; foreach ($inputs as $i => $input) { - $result = $this->gen->generateFromToken($input); + $result = $this->obj->generateFromToken($input); $this->assertEqual($result, $expect[$i]); paintIf($result, $result != $expect[$i]); } @@ -62,9 +69,7 @@ class HTMLPurifier_GeneratorTest extends UnitTestCase function test_generateAttributes() { - $inputs = array(); - $expect = array(); - $config = array(); + $inputs = $expect = array(); $inputs[0] = array(); $expect[0] = ''; @@ -83,10 +88,8 @@ class HTMLPurifier_GeneratorTest extends UnitTestCase $inputs[4] = array('title' => 'Theta is ' . $theta_char); $expect[4] = 'title="Theta is ' . $theta_char . '"'; - $default_config = HTMLPurifier_Config::createDefault(); foreach ($inputs as $i => $input) { - if (!isset($config[$i])) $config[$i] = $default_config; - $result = $this->gen->generateAttributes($input, $config[$i]); + $result = $this->obj->generateAttributes($input); $this->assertEqual($result, $expect[$i]); paintIf($result, $result != $expect[$i]); } @@ -95,34 +98,26 @@ class HTMLPurifier_GeneratorTest extends UnitTestCase function test_generateFromTokens() { - $inputs = array(); - $expect = array(); - $config = array(); + $this->func = 'generateFromTokens'; - $inputs[0] = array( - new HTMLPurifier_Token_Start('b'), - new HTMLPurifier_Token_Text('Foobar!'), - new HTMLPurifier_Token_End('b') - ); - $expect[0] = 'Foobar!'; - - $inputs[1] = array(); - $expect[1] = ''; - - $default_config = HTMLPurifier_Config::createDefault(); - foreach ($inputs as $i => $input) { - if (!isset($config[$i])) $config[$i] = $default_config; - $result = $this->gen->generateFromTokens($input, $config[$i]); - $this->assertEqual($expect[$i], $result); - paintIf($result, $result != $expect[$i]); - } + $this->assertResult( + array( + new HTMLPurifier_Token_Start('b'), + new HTMLPurifier_Token_Text('Foobar!'), + new HTMLPurifier_Token_End('b') + ), + 'Foobar!' + ); + $this->assertResult(array(), ''); } var $config; function assertGeneration($tokens, $expect) { - $result = $this->gen->generateFromTokens($tokens, $this->config); + $context = new HTMLPurifier_Context(); + $result = $this->gen->generateFromTokens( + $tokens, $this->config, $context); // normalized newlines, this probably should be put somewhere else $result = str_replace("\r\n", "\n", $result); $result = str_replace("\r", "\n", $result); diff --git a/tests/HTMLPurifier/Harness.php b/tests/HTMLPurifier/Harness.php index 69c242f5..ff59d997 100644 --- a/tests/HTMLPurifier/Harness.php +++ b/tests/HTMLPurifier/Harness.php @@ -1,5 +1,7 @@ obj = new HTMLPurifier_Strategy_Core(); + } + function test() { - $strategy = new HTMLPurifier_Strategy_Core(); - $inputs = array(); - $expect = array(); - $config = array(); + $this->assertResult(''); + $this->assertResult( + 'Make well formed.', + 'Make well formed.' + ); + $this->assertResult( + '
Fix nesting.
', + 'Fix nesting.' + ); + $this->assertResult( + 'Foreign element removal.', + 'Foreign element removal.' + ); + $this->assertResult( + '
All three.
', + 'All three.' + ); - $config_escape = HTMLPurifier_Config::createDefault(); - $config_escape->set('Core', 'EscapeInvalidChildren', true); - - $inputs[0] = ''; - $expect[0] = ''; - - $inputs[1] = 'Make well formed.'; - $expect[1] = 'Make well formed.'; - - $inputs[2] = '
Fix nesting.
'; - $expect[2] = 'Fix nesting.'; - - $inputs[3] = 'Foreign element removal.'; - $expect[3] = 'Foreign element removal.'; - - $inputs[4] = '
All three.
'; - $expect[4] = 'All three.'; - - $this->assertStrategyWorks($strategy, $inputs, $expect, $config); } } diff --git a/tests/HTMLPurifier/Strategy/FixNestingTest.php b/tests/HTMLPurifier/Strategy/FixNestingTest.php index 182b11e8..ff88cc09 100644 --- a/tests/HTMLPurifier/Strategy/FixNestingTest.php +++ b/tests/HTMLPurifier/Strategy/FixNestingTest.php @@ -3,89 +3,86 @@ require_once 'HTMLPurifier/StrategyHarness.php'; require_once 'HTMLPurifier/Strategy/FixNesting.php'; -class HTMLPurifier_Strategy_FixNestingTest - extends HTMLPurifier_StrategyHarness +class HTMLPurifier_Strategy_FixNestingTest extends HTMLPurifier_StrategyHarness { + function setUp() { + parent::setUp(); + $this->obj = new HTMLPurifier_Strategy_FixNesting(); + } + function test() { - $strategy = new HTMLPurifier_Strategy_FixNesting(); - - $inputs = array(); - $expect = array(); - $config = array(); - - $config_escape = HTMLPurifier_Config::createDefault(); - $config_escape->set('Core', 'EscapeInvalidChildren', true); - - // next id = 4 - - // legal inline nesting - $inputs[0] = 'Bold text'; - $expect[0] = $inputs[0]; + // legal inline + $this->assertResult('Bold text'); // legal inline and block // as the parent element is considered FLOW - $inputs[1] = 'Blank
Block
'; - $expect[1] = $inputs[1]; + $this->assertResult('Blank
Block
'); // illegal block in inline - $inputs[2] = '
Illegal div.
'; - $expect[2] = 'Illegal div.'; + $this->assertResult( + '
Illegal div.
', + 'Illegal div.' + ); // same test with different configuration (fragile) - $inputs[13] = '
Illegal div.
'; - $expect[13] = '<div>Illegal div.</div>'; - $config[13] = $config_escape; + $this->assertResult( + '
Illegal div.
', + '<div>Illegal div.</div>', + array('Core.EscapeInvalidChildren' => true) + ); // test of empty set that's required, resulting in removal of node - $inputs[3] = '
    '; - $expect[3] = ''; + $this->assertResult('
      ', ''); // test illegal text which gets removed - $inputs[4] = '
        Illegal text
      • Legal item
      '; - $expect[4] = '
      • Legal item
      '; + $this->assertResult( + '
        Illegal text
      • Legal item
      ', + '
      • Legal item
      ' + ); // test custom table definition - - $inputs[5] = '
      Cell 1
      '; - $expect[5] = '
      Cell 1
      '; - - $inputs[6] = '
      '; - $expect[6] = ''; + $this->assertResult( + '
      Cell 1
      ', + '
      Cell 1
      ' + ); + $this->assertResult('
      ', ''); // breaks without the redundant checking code - $inputs[7] = '
      '; - $expect[7] = ''; + $this->assertResult('
      ', ''); // special case, prevents scrolling one back to find parent - $inputs[8] = '
      '; - $expect[8] = ''; + $this->assertResult('
      ', ''); // cascading rollbacks - $inputs[9] = '
      '; - $expect[9] = ''; + $this->assertResult( + '
      ', + '' + ); // rollbacks twice - $inputs[10] = '
      '; - $expect[10] = ''; + $this->assertResult('
      ', ''); // block in inline ins not allowed - $inputs[11] = '
      Not allowed!
      '; - $expect[11] = 'Not allowed!'; + $this->assertResult( + '
      Not allowed!
      ', + 'Not allowed!' + ); // block in inline ins not allowed - $inputs[14] = '
      Not allowed!
      '; - $expect[14] = '<div>Not allowed!</div>'; - $config[14] = $config_escape; + $this->assertResult( + '
      Not allowed!
      ', + '<div>Not allowed!</div>', + array('Core.EscapeInvalidChildren' => true) + ); // test exclusions - $inputs[12] = 'Not allowed'; - $expect[12] = ''; + $this->assertResult( + 'Not allowed', + '' + ); - // next test is *15* - - $this->assertStrategyWorks($strategy, $inputs, $expect, $config); } } diff --git a/tests/HTMLPurifier/Strategy/MakeWellFormedTest.php b/tests/HTMLPurifier/Strategy/MakeWellFormedTest.php index 526b22a0..07e9202d 100644 --- a/tests/HTMLPurifier/Strategy/MakeWellFormedTest.php +++ b/tests/HTMLPurifier/Strategy/MakeWellFormedTest.php @@ -3,53 +3,62 @@ require_once 'HTMLPurifier/StrategyHarness.php'; require_once 'HTMLPurifier/Strategy/MakeWellFormed.php'; -class HTMLPurifier_Strategy_MakeWellFormedTest - extends HTMLPurifier_StrategyHarness +class HTMLPurifier_Strategy_MakeWellFormedTest extends HTMLPurifier_StrategyHarness { + function setUp() { + parent::setUp(); + $this->obj = new HTMLPurifier_Strategy_MakeWellFormed(); + } + function test() { - $strategy = new HTMLPurifier_Strategy_MakeWellFormed(); + $this->assertResult(''); + $this->assertResult('This is bold text.'); - $inputs = array(); - $expect = array(); + $this->assertResult( + 'Unclosed tag, gasp!', + 'Unclosed tag, gasp!' + ); - $inputs[0] = ''; - $expect[0] = $inputs[0]; + $this->assertResult( + 'Bold and italic?', + 'Bold and italic?' + ); - $inputs[1] = 'This is bold text.'; - $expect[1] = $inputs[1]; + $this->assertResult( + 'Unused end tags... recycle!', + 'Unused end tags... recycle!' + ); - $inputs[2] = 'Unclosed tag, gasp!'; - $expect[2] = 'Unclosed tag, gasp!'; + $this->assertResult( + '
      ', + '
      ' + ); - $inputs[3] = 'Bold and italic?'; - $expect[3] = 'Bold and italic?'; - - // CHANGE THIS BEHAVIOR! - $inputs[4] = 'Unused end tags... recycle!
      '; - $expect[4] = 'Unused end tags... recycle!'; - - $inputs[5] = '
      '; - $expect[5] = '
      '; - - $inputs[6] = '
      '; - $expect[6] = '
      '; + $this->assertResult( + '
      ', + '
      ' + ); // test automatic paragraph closing - $inputs[7] = '

      Paragraph 1

      Paragraph 2'; - $expect[7] = '

      Paragraph 1

      Paragraph 2

      '; + $this->assertResult( + '

      Paragraph 1

      Paragraph 2', + '

      Paragraph 1

      Paragraph 2

      ' + ); - $inputs[8] = '

      Paragraphs

      In

      A

      Div

      '; - $expect[8] = '

      Paragraphs

      In

      A

      Div

      '; + $this->assertResult( + '

      Paragraphs

      In

      A

      Div

      ', + '

      Paragraphs

      In

      A

      Div

      ' + ); // automatic list closing - $inputs[9] = '
      1. Item 1
      2. Item 2
      '; - $expect[9] = '
      1. Item 1
      2. Item 2
      '; - - $this->assertStrategyWorks($strategy, $inputs, $expect); + $this->assertResult( + '
      1. Item 1
      2. Item 2
      ', + '
      1. Item 1
      2. Item 2
      ' + ); } } diff --git a/tests/HTMLPurifier/Strategy/RemoveForeignElementsTest.php b/tests/HTMLPurifier/Strategy/RemoveForeignElementsTest.php index 894e1a6a..1f212bf6 100644 --- a/tests/HTMLPurifier/Strategy/RemoveForeignElementsTest.php +++ b/tests/HTMLPurifier/Strategy/RemoveForeignElementsTest.php @@ -4,39 +4,44 @@ require_once 'HTMLPurifier/StrategyHarness.php'; require_once 'HTMLPurifier/Strategy/RemoveForeignElements.php'; class HTMLPurifier_Strategy_RemoveForeignElementsTest - extends HTMLPurifier_StrategyHarness + extends HTMLPurifier_StrategyHarness { + function setUp() { + parent::setUp(); + $this->obj = new HTMLPurifier_Strategy_RemoveForeignElements(); + } + function test() { - $strategy = new HTMLPurifier_Strategy_RemoveForeignElements(); - $inputs = array(); - $expect = array(); + $this->assertResult(''); - $inputs[0] = ''; - $expect[0] = $inputs[0]; + $this->assertResult('This is bold text.'); - $inputs[1] = 'This is bold text.'; - $expect[1] = $inputs[1]; + $this->assertResult( + 'BlingBong', + 'BlingBong' + ); - // [INVALID] - $inputs[2] = 'BlingBong'; - $expect[2] = 'BlingBong'; - - // test simple transform - $inputs[3] = '
    • Item 1
    • '; - $expect[3] = '
      • Item 1
      '; + $this->assertResult( + '
    • Item 1
    • ', + '
      • Item 1
      ' + ); // test center transform - $inputs[4] = '
      Look I am Centered!
      '; - $expect[4] = '
      Look I am Centered!
      '; + $this->assertResult( + '
      Look I am Centered!
      ', + '
      Look I am Centered!
      ' + ); // test font transform - $inputs[5] = 'Big Warning!'; - $expect[5] = 'Big Warning!'; + $this->assertResult( + 'Big Warning!', + 'Big'. + ' Warning!' + ); - $this->assertStrategyWorks($strategy, $inputs, $expect); } } diff --git a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php index 82f87607..60183558 100644 --- a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php +++ b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php @@ -8,71 +8,83 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends HTMLPurifier_StrategyHarness { + function setUp() { + parent::setUp(); + $this->obj = new HTMLPurifier_Strategy_ValidateAttributes(); + } + function test() { - $strategy = new HTMLPurifier_Strategy_ValidateAttributes(); - // attribute order is VERY fragile, perhaps we should define // an ordering scheme! - $inputs = array(); - $expect = array(); - $config = array(); - - $inputs[0] = ''; - $expect[0] = ''; + $this->assertResult(''); // test ids + $this->assertResult('
      Preserve the ID.
      '); - $inputs[1] = '
      Preserve the ID.
      '; - $expect[1] = $inputs[1]; - - $inputs[2] = '
      Kill the ID.
      '; - $expect[2] = '
      Kill the ID.
      '; + $this->assertResult( + '
      Kill the ID.
      ', + '
      Kill the ID.
      ' + ); // test id accumulator - $inputs[3] = '
      Valid
      Invalid
      '; - $expect[3] = '
      Valid
      Invalid
      '; + $this->assertResult( + '
      Valid
      Invalid
      ', + '
      Valid
      Invalid
      ' + ); - $inputs[4] = 'Bad dir.'; - $expect[4] = 'Bad dir.'; + $this->assertResult( + 'Bad dir.', + 'Bad dir.' + ); - // test attribute case sensitivity - $inputs[5] = '
      Convert ID to lowercase.
      '; - $expect[5] = '
      Convert ID to lowercase.
      '; + // test attribute key case sensitivity + $this->assertResult( + '
      Convert ID to lowercase.
      ', + '
      Convert ID to lowercase.
      ' + ); // test simple attribute substitution - $inputs[6] = '
      Trim whitespace.
      '; - $expect[6] = '
      Trim whitespace.
      '; + $this->assertResult( + '
      Trim whitespace.
      ', + '
      Trim whitespace.
      ' + ); // test configuration id blacklist - $inputs[7] = '
      Invalid
      '; - $expect[7] = '
      Invalid
      '; - $config[7] = HTMLPurifier_Config::createDefault(); - $config[7]->set('Attr', 'IDBlacklist', array('invalid')); + $this->assertResult( + '
      Invalid
      ', + '
      Invalid
      ', + array('Attr.IDBlacklist' => array('invalid')) + ); // test classes - $inputs[8] = '
      Valid
      '; - $expect[8] = $inputs[8]; + $this->assertResult('
      Valid
      '); - $inputs[9] = '
      Keep valid.
      '; - $expect[9] = '
      Keep valid.
      '; + $this->assertResult( + '
      Keep valid.
      ', + '
      Keep valid.
      ' + ); // test title - $inputs[10] = 'PHP'; - $expect[10] = $inputs[10]; + $this->assertResult( + 'PHP' + ); // test lang - $inputs[11] = 'La soupe.'; - $expect[11] = 'La soupe.'; + $this->assertResult( + 'La soupe.', + 'La soupe.' + ); - // test align (won't work till CSS validation is implemented) - $inputs[12] = '

      Centered Headline

      '; - $expect[12] = '

      Centered Headline

      '; + // test align + $this->assertResult( + '

      Centered Headline

      ', + '

      Centered Headline

      ' + ); // test table - $inputs[13] = - + $this->assertResult( '@@ -87,44 +99,56 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends -
      Taken off the market
      '; - - $expect[13] = $inputs[13]; +' + ); // test URI - $inputs[14] = 'Google'; - $expect[14] = $inputs[14]; + $this->assertResult('Google'); // test invalid URI - $inputs[15] = 'Google'; - $expect[15] = 'Google'; + $this->assertResult( + 'Google', + 'Google' + ); // test required attributes for img - $inputs[16] = ''; - $expect[16] = 'Invalid image'; + $this->assertResult( + '', + 'Invalid image' + ); - $inputs[17] = ''; - $expect[17] = 'foobar.jpg'; + $this->assertResult( + '', + 'foobar.jpg' + ); - $inputs[18] = 'pretty picture'; - $expect[18] = 'pretty picture'; + $this->assertResult( + 'pretty picture', + 'pretty picture' + ); // test required attributes for bdo - $inputs[19] = 'Go left.'; - $expect[19] = 'Go left.'; + $this->assertResult( + 'Go left.', + 'Go left.' + ); - $inputs[20] = 'Invalid value!'; - $expect[20] = 'Invalid value!'; + $this->assertResult( + 'Invalid value!', + 'Invalid value!' + ); // comparison check for test 20 - $inputs[21] = 'Invalid value!'; - $expect[21] = 'Invalid value!'; + $this->assertResult( + 'Invalid value!', + 'Invalid value!' + ); // test col.span is non-zero - $inputs[22] = ''; - $expect[22] = ''; - - $this->assertStrategyWorks($strategy, $inputs, $expect, $config); + $this->assertResult( + '', + '' + ); } diff --git a/tests/HTMLPurifier/StrategyHarness.php b/tests/HTMLPurifier/StrategyHarness.php index 3ca07753..9b452a8b 100644 --- a/tests/HTMLPurifier/StrategyHarness.php +++ b/tests/HTMLPurifier/StrategyHarness.php @@ -1,41 +1,14 @@ UnitTestCase(); - - // we can't use the DOM lexer since it does too much stuff - // automatically, however, we should be able to use it - // interchangeably if we wanted to... - - if (true) { - $this->lex = new HTMLPurifier_Lexer_DirectLex(); - } else { - require_once 'HTMLPurifier/Lexer/DOMLex.php'; - $this->lex = new HTMLPurifier_Lexer_DOMLex(); - } - - $this->gen = new HTMLPurifier_Generator(); - } - - function assertStrategyWorks($strategy, $inputs, $expect, $config = array()) { - $context = new HTMLPurifier_Context(); - foreach ($inputs as $i => $input) { - if (!isset($config[$i])) { - $config[$i] = HTMLPurifier_Config::createDefault(); - } - $tokens = $this->lex->tokenizeHTML($input, $config[$i], $context); - $result_tokens = $strategy->execute($tokens, $config[$i], $context); - $result = $this->gen->generateFromTokens($result_tokens, $config[$i]); - $this->assertEqual($expect[$i], $result, "Test $i: %s"); - paintIf($result, $result != $expect[$i]); - } + function setUp() { + $this->func = 'execute'; + $this->to_tokens = true; + $this->to_html = true; } }