diff --git a/NEWS b/NEWS index 5e03cbe1..e80d03c5 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,9 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier ! Implemented Object module for trusted users - Fix non-visible parsing error in DirectLex with empty tags that have slashes inside attribute values. +. Unit test refactoring for one logical test per test function +. Config and context parameters in ComplexHarness deprecated: instead, edit + the $config and $context member variables 2.1.1, released 2007-08-04 - Fix show-stopper bug in %URI.MakeAbsolute functionality diff --git a/tests/HTMLPurifier/ComplexHarness.php b/tests/HTMLPurifier/ComplexHarness.php index 8ea7378d..19a4a480 100644 --- a/tests/HTMLPurifier/ComplexHarness.php +++ b/tests/HTMLPurifier/ComplexHarness.php @@ -67,41 +67,20 @@ class HTMLPurifier_ComplexHarness extends HTMLPurifier_Harness * @param $context_array Context array in form of Key => Value or an actual * context object. */ - function assertResult($input, $expect = true, - $config_array = array(), $context_array = array() - ) { - - // setup config - if ($this->config) { - $config = HTMLPurifier_Config::create($this->config); - $config->autoFinalize = false; - $config->loadArray($config_array); - } else { - $config = HTMLPurifier_Config::create($config_array); - } - - // setup context object. Note that we are operating on a copy of it! - // When necessary, extend the test harness to allow post-tests - // on the context object - if (empty($this->context)) { - $context = new HTMLPurifier_Context(); - $context->loadArray($context_array); - } else { - $context =& $this->context; - } + function assertResult($input, $expect = true) { if ($this->to_tokens && is_string($input)) { // $func may cause $input to change, so "clone" another copy // to sacrifice - $input = $this->lexer->tokenizeHTML($s = $input, $config, $context); - $input_c = $this->lexer->tokenizeHTML($s, $config, $context); + $input = $this->tokenize($temp = $input); + $input_c = $this->tokenize($temp); } else { $input_c = $input; } // call the function $func = $this->func; - $result = $this->obj->$func($input_c, $config, $context); + $result = $this->obj->$func($input_c, $this->config, $this->context); // test a bool result if (is_bool($result)) { @@ -112,11 +91,9 @@ class HTMLPurifier_ComplexHarness extends HTMLPurifier_Harness } if ($this->to_html) { - $result = $this->generator-> - generateFromTokens($result, $config, $context); + $result = $this->generate($result); if (is_array($expect)) { - $expect = $this->generator-> - generateFromTokens($expect, $config, $context); + $expect = $this->generate($expect); } } @@ -124,6 +101,20 @@ class HTMLPurifier_ComplexHarness extends HTMLPurifier_Harness } + /** + * Tokenize HTML into tokens, uses member variables for common variables + */ + function tokenize($html) { + return $this->lexer->tokenizeHTML($html, $this->config, $this->context); + } + + /** + * Generate textual HTML from tokens + */ + function generate($tokens) { + return $this->generator->generateFromTokens($tokens, $this->config, $this->context); + } + } diff --git a/tests/HTMLPurifier/Injector/AutoParagraphTest.php b/tests/HTMLPurifier/Injector/AutoParagraphTest.php index 0f0b5e5e..23743dff 100644 --- a/tests/HTMLPurifier/Injector/AutoParagraphTest.php +++ b/tests/HTMLPurifier/Injector/AutoParagraphTest.php @@ -8,29 +8,35 @@ class HTMLPurifier_Injector_AutoParagraphTest extends HTMLPurifier_InjectorHarne function setup() { parent::setup(); - $this->config = array('AutoFormat.AutoParagraph' => true); + $this->config->set('AutoFormat', 'AutoParagraph', true); } - function test() { + function testSingleParagraph() { $this->assertResult( 'Foobar', '

Foobar

' ); - + } + + function testSingleMultiLineParagraph() { $this->assertResult( 'Par 1 Par 1 still', '

Par 1 Par 1 still

' ); - + } + + function testTwoParagraphs() { $this->assertResult( 'Par1 Par2', '

Par1

Par2

' ); - + } + + function testTwoParagraphsWithLotsOfSpace() { $this->assertResult( 'Par1 @@ -39,15 +45,18 @@ Par2', Par2', '

Par1

Par2

' ); - + } + + function testTwoParagraphsWithInlineElements() { $this->assertResult( 'Par1 Par2', '

Par1

Par2

' ); - - + } + + function testSingleParagraphThatLooksLikeTwo() { $this->assertResult( 'Par1 @@ -56,29 +65,40 @@ Par2', Par2

' ); - + } + + function testAddParagraphAdjacentToParagraph() { $this->assertResult( 'Par1

Par2

', '

Par1

Par2

' ); - + } + + function testParagraphUnclosedInlineElement() { $this->assertResult( 'Par1', '

Par1

' ); - + } + + function testPreservePreTags() { $this->assertResult( '
Par1
 
 Par1
' ); - + } + + function testIgnoreTrailingWhitespace() { $this->assertResult( 'Par1 ', '

Par1

' ); + } + + function testDoNotParagraphBlockElements() { $this->assertResult( 'Par1 @@ -87,19 +107,25 @@ Par1' Par3', '

Par1

Par2

Par3

' ); - + } + + function testParagraphTextAndInlineNodes() { $this->assertResult( 'Par1', '

Par1

' ); - + } + + function testIgnoreLeadingWhitespace() { $this->assertResult( ' Par', '

Par

' ); - + } + + function testIgnoreSurroundingWhitespace() { $this->assertResult( ' @@ -108,69 +134,90 @@ Par ', '

Par

' ); - + } + + function testParagraphInsideBlockNode() { $this->assertResult( '
Par1 Par2
', '

Par1

Par2

' ); - + } + + function testParagraphInlineNodeInsideBlockNode() { $this->assertResult( '
Par1 Par2
', '

Par1

Par2

' ); - + } + + function testNoParagraphWhenOnlyOneInsideBlockNode() { $this->assertResult('
Par1
'); - + } + + function testParagraphTwoInlineNodesInsideBlockNode() { $this->assertResult( '
Par1 Par2
', '

Par1

Par2

' ); - + } + + function testPreserveInlineNodesInPreTag() { $this->assertResult( '
Par1
 
-Par2
', - true +Par2' ); - + } + + function testSplitUpInternalsOfPTagInBlockNode() { $this->assertResult( '

Foo Bar

', '

Foo

Bar

' ); - + } + + function testSplitUpInlineNodesInPTagInBlockNode() { $this->assertResult( '

Foo Bar

', '

Foo

Bar

' ); - + } + + function testNoParagraphSingleInlineNodeInBlockNode() { $this->assertResult( '
Foo
', '
Foo
' ); - + } + + function testParagraphInBlockquote() { $this->assertResult( '
Par1 Par2
', '

Par1

Par2

' ); - + } + + function testNoParagraphBetweenListItem() { $this->assertResult( '', true +
  • Bar
  • ' ); - + } + + function testParagraphSingleElementWithSurroundingSpace() { $this->assertResult( '
    @@ -179,7 +226,9 @@ Bar
    ', '

    Bar

    ' ); - + } + + function testIgnoreExtraSpaceWithLeadingInlineNode() { $this->assertResult( 'Par1a @@ -188,99 +237,124 @@ Bar Par2', '

    Par1a

    Par2

    ' ); - + } + + function testAbsorbExtraEndingPTag() { $this->assertResult( 'Par1 Par2

    ', '

    Par1

    Par2

    ' ); - + } + + function testAbsorbExtraEndingDivTag() { $this->assertResult( 'Par1 Par2', '

    Par1

    Par2

    ' ); - + } + + function testDoNotParagraphSingleSurroundingSpaceInBlockNode() { $this->assertResult( '
    Par1 -
    ', true +' ); - + } + + function testBlockNodeTextDelimeterInBlockNode() { $this->assertResult( '
    Par1
    Par2
    ', '

    Par1

    Par2
    ' ); - + } + + function testBlockNodeTextDelimeterWithoutDoublespaceInBlockNode() { $this->assertResult( '
    Par1
    Par2
    ', '

    Par1

    Par2
    ' ); - + } + + function testBlockNodeTextDelimeterWithoutDoublespace() { $this->assertResult( 'Par1
    Par2
    ', '

    Par1

    Par2
    ' ); - + } + + function testTwoParagraphsOfTextAndInlineNode() { $this->assertResult( 'Par1 Par2', '

    Par1

    Par2

    ' ); - + } + + function testLeadingInlineNodeParagraph() { $this->assertResult( ' Foo', '

    Foo

    ' ); - + } + + function testTrailingInlineNodeParagraph() { $this->assertResult( '
  • Foo bar
  • ' ); - + } + + function testTwoInlineNodeParagraph() { $this->assertResult( '
  • bazbar
  • ' ); - + } + + function testNoParagraphTrailingBlockNodeInBlockNode() { $this->assertResult( '
    asdf
    asdf
    ' ); - + } + + function testParagraphTrailingBlockNodeWithDoublespaceInBlockNode() { $this->assertResult( '
    asdf
    asdf
    ', '
    asdf

    asdf

    ' ); - + } + + function testParagraphTwoInlineNodesAndWhitespaceNode() { $this->assertResult( 'One Two', '

    One Two

    ' ); - } - function testInlineRootNode() { + function testNoParagraphWithInlineRootNode() { + $this->config->set('HTML', 'Parent', 'span'); $this->assertResult( 'Par -Par2', - true, - array('AutoFormat.AutoParagraph' => true, 'HTML.Parent' => 'span') +Par2' ); } - function testNeeded() { + function testErrorNeeded() { + $this->config->set('HTML', 'Allowed', 'b'); $this->expectError('Cannot enable AutoParagraph injector because p is not allowed'); - $this->assertResult('foobar', true, array('AutoFormat.AutoParagraph' => true, 'HTML.Allowed' => 'b')); + $this->assertResult('foobar'); } } diff --git a/tests/HTMLPurifier/Injector/LinkifyTest.php b/tests/HTMLPurifier/Injector/LinkifyTest.php index 66a06956..777cac9a 100644 --- a/tests/HTMLPurifier/Injector/LinkifyTest.php +++ b/tests/HTMLPurifier/Injector/LinkifyTest.php @@ -8,35 +8,40 @@ class HTMLPurifier_Injector_LinkifyTest extends HTMLPurifier_InjectorHarness function setup() { parent::setup(); - $this->config = array('AutoFormat.Linkify' => true); + $this->config->set('AutoFormat', 'Linkify', true); } - function testLinkify() { - + function testLinkifyURLInRootNode() { $this->assertResult( 'http://example.com', 'http://example.com' ); - + } + + function testLinkifyURLInInlineNode() { $this->assertResult( 'http://example.com', 'http://example.com' ); - + } + + function testBasicUsageCase() { $this->assertResult( 'This URL http://example.com is what you need', 'This URL http://example.com is what you need' ); - + } + + function testIgnoreURLInATag() { $this->assertResult( 'http://example.com/' ); - } function testNeeded() { + $this->config->set('HTML', 'Allowed', 'b'); $this->expectError('Cannot enable Linkify injector because a is not allowed'); - $this->assertResult('http://example.com/', true, array('AutoFormat.Linkify' => true, 'HTML.Allowed' => 'b')); + $this->assertResult('http://example.com/'); } } diff --git a/tests/HTMLPurifier/Injector/PurifierLinkifyTest.php b/tests/HTMLPurifier/Injector/PurifierLinkifyTest.php index e820d677..309733f3 100644 --- a/tests/HTMLPurifier/Injector/PurifierLinkifyTest.php +++ b/tests/HTMLPurifier/Injector/PurifierLinkifyTest.php @@ -8,39 +8,53 @@ class HTMLPurifier_Injector_PurifierLinkifyTest extends HTMLPurifier_InjectorHar function setup() { parent::setup(); - $this->config = array( - 'AutoFormat.PurifierLinkify' => true, - 'AutoFormatParam.PurifierLinkifyDocURL' => '#%s' - ); + $this->config->set('AutoFormat', 'PurifierLinkify', true); + $this->config->set('AutoFormatParam', 'PurifierLinkifyDocURL', '#%s'); } - function testLinkify() { - + function testNoTriggerCharacer() { $this->assertResult('Foobar'); + } + + function testTriggerCharacterInIrrelevantContext() { $this->assertResult('20% off!'); + } + + function testPreserveNamespace() { $this->assertResult('%Core namespace (not recognized)'); + } + + function testLinkifyBasic() { $this->assertResult( '%Namespace.Directive', '%Namespace.Directive' ); + } + + function testLinkifyWithAdjacentTextNodes() { $this->assertResult( 'This %Namespace.Directive thing', 'This %Namespace.Directive thing' ); + } + + function testLinkifyInBlock() { $this->assertResult( '
    This %Namespace.Directive thing
    ', '
    This %Namespace.Directive thing
    ' ); + } + + function testPreserveInATag() { $this->assertResult( '%Namespace.Directive' ); - - } function testNeeded() { + $this->config->set('HTML', 'Allowed', 'b'); $this->expectError('Cannot enable PurifierLinkify injector because a is not allowed'); - $this->assertResult('%Namespace.Directive', true, array('AutoFormat.PurifierLinkify' => true, 'HTML.Allowed' => 'b')); + $this->assertResult('%Namespace.Directive'); } }