From e768f845f47d2353b7a47394a23276185b5686b5 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Mon, 24 Jul 2006 02:28:40 +0000 Subject: [PATCH] Extract common functionality to StrategyAbstractTest. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@115 48356398-32a2-884e-a903-53898d9a118a --- tests/HTMLPurifier/StrategyAbstractTest.php | 143 ++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 tests/HTMLPurifier/StrategyAbstractTest.php diff --git a/tests/HTMLPurifier/StrategyAbstractTest.php b/tests/HTMLPurifier/StrategyAbstractTest.php new file mode 100644 index 00000000..f1c168e8 --- /dev/null +++ b/tests/HTMLPurifier/StrategyAbstractTest.php @@ -0,0 +1,143 @@ +UnitTestCase(); + $this->def = new HTMLPurifier_Definition(); + $this->def->loadData(); + + // 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 test_removeForeignElements() { + + $inputs = array(); + $expect = array(); + + $inputs[0] = ''; + $expect[0] = $inputs[0]; + + $inputs[1] = 'This is bold text.'; + $expect[1] = $inputs[1]; + + // [INVALID] + $inputs[2] = 'BlingBong'; + $expect[2] = htmlspecialchars($inputs[2]); + + foreach ($inputs as $i => $input) { + $tokens = $this->lex->tokenizeHTML($input); + $result_tokens = $this->def->removeForeignElements($tokens); + $result = $this->gen->generateFromTokens($result_tokens); + $this->assertEqual($expect[$i], $result, "Test $i: %s"); + paintIf($result, $result != $expect[$i]); + } + + } + + function test_makeWellFormed() { + + $inputs = array(); + $expect = array(); + + $inputs[0] = ''; + $expect[0] = $inputs[0]; + + $inputs[1] = 'This is bold text.'; + $expect[1] = $inputs[1]; + + $inputs[2] = 'Unclosed tag, gasp!'; + $expect[2] = 'Unclosed tag, gasp!'; + + $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!</b>'; + + $inputs[5] = '
'; + $expect[5] = '
'; + + $inputs[6] = '
'; + $expect[6] = '
'; + + // test automatic paragraph closing + + $inputs[7] = '

Paragraph 1

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

Paragraph 1

Paragraph 2

'; + + $inputs[8] = '

Paragraphs

In

A

Div

'; + $expect[8] = '

Paragraphs

In

A

Div

'; + + // automatic list closing + + $inputs[9] = '
  1. Item 1
  2. Item 2
'; + $expect[9] = '
  1. Item 1
  2. Item 2
'; + + foreach ($inputs as $i => $input) { + $tokens = $this->lex->tokenizeHTML($input); + $result_tokens = $this->def->makeWellFormed($tokens); + $result = $this->gen->generateFromTokens($result_tokens); + $this->assertEqual($expect[$i], $result, "Test $i: %s"); + paintIf($result, $result != $expect[$i]); + } + + } + + function test_fixNesting() { + $inputs = array(); + $expect = array(); + + // next id = 4 + + // legal inline nesting + $inputs[0] = 'Bold text'; + $expect[0] = $inputs[0]; + + // legal inline and block + // as the parent element is considered FLOW + $inputs[1] = 'Blank
Block
'; + $expect[1] = $inputs[1]; + + // illegal block in inline, element -> text + $inputs[2] = '
Illegal div.
'; + $expect[2] = '<div>Illegal div.</div>'; + + // test of empty set that's required, resulting in removal of node + $inputs[3] = '
    '; + $expect[3] = ''; + + // test illegal text which gets removed + $inputs[4] = '
      Illegal text
    • Legal item
    '; + $expect[4] = '
    • Legal item
    '; + + foreach ($inputs as $i => $input) { + $tokens = $this->lex->tokenizeHTML($input); + $result_tokens = $this->def->fixNesting($tokens); + $result = $this->gen->generateFromTokens($result_tokens); + $this->assertEqual($expect[$i], $result, "Test $i: %s"); + paintIf($result, $result != $expect[$i]); + } + } + +} + +?> \ No newline at end of file