From 7a40f4354b32809af3d0cfd6e3af0eda02ab0e0a Mon Sep 17 00:00:00 2001 From: friendica Date: Sat, 12 May 2012 17:57:41 -0700 Subject: some important stuff we'll need --- .../tests/HTMLPurifier/HTMLModule/FormsTest.php | 155 ++++++++++++++ .../tests/HTMLPurifier/HTMLModule/ImageTest.php | 55 +++++ .../tests/HTMLPurifier/HTMLModule/NameTest.php | 32 +++ .../tests/HTMLPurifier/HTMLModule/NofollowTest.php | 26 +++ .../tests/HTMLPurifier/HTMLModule/ObjectTest.php | 38 ++++ .../HTMLPurifier/HTMLModule/ProprietaryTest.php | 30 +++ .../tests/HTMLPurifier/HTMLModule/RubyTest.php | 55 +++++ .../HTMLPurifier/HTMLModule/SafeEmbedTest.php | 41 ++++ .../HTMLPurifier/HTMLModule/SafeObjectTest.php | 49 +++++ .../HTMLPurifier/HTMLModule/ScriptingTest.php | 55 +++++ .../HTMLPurifier/HTMLModule/TargetBlankTest.php | 20 ++ .../tests/HTMLPurifier/HTMLModule/TidyTest.php | 224 +++++++++++++++++++++ 12 files changed, 780 insertions(+) create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/FormsTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ImageTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/NameTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/NofollowTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ObjectTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ProprietaryTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/RubyTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/SafeEmbedTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/SafeObjectTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ScriptingTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/TargetBlankTest.php create mode 100644 lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/TidyTest.php (limited to 'lib/htmlpurifier/tests/HTMLPurifier/HTMLModule') diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/FormsTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/FormsTest.php new file mode 100644 index 000000000..5bc4c99c3 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/FormsTest.php @@ -0,0 +1,155 @@ +config->set('HTML.Trusted', true); + $this->config->set('Attr.EnableID', true); + } + + function testBasicUse() { + $this->config->set('HTML.Doctype', 'HTML 4.01 Strict'); + $this->assertResult( // need support for label for later + ' +
+

+ +
+ +
+ +
+ Male
+ Female
+ +

+
' + ); + } + + function testSelectOption() { + $this->config->set('HTML.Doctype', 'HTML 4.01 Strict'); + $this->assertResult(' +
+

+ + +

+
+ '); + } + + function testSelectOptgroup() { + $this->config->set('HTML.Doctype', 'HTML 4.01 Strict'); + $this->assertResult(' +
+

+ +

+
+ '); + } + + function testTextarea() { + $this->config->set('HTML.Doctype', 'HTML 4.01 Strict'); + $this->assertResult(' +
+

+ + +

+
+ '); + } + + // label tests omitted + + function testFieldset() { + $this->config->set('HTML.Doctype', 'HTML 4.01 Strict'); + $this->assertResult(' +
+
+ Personal Information + Last Name: + First Name: + Address: + ...more personal information... +
+
+ Medical History + Smallpox + Mumps + Dizziness + Sneezing + ...more medical history... +
+
+ Current Medication + Are you currently taking any medication? + Yes + No + + If you are currently taking medication, please indicate + it in the space below: + +
+
+ '); + } + + function testInputTransform() { + $this->config->set('HTML.Doctype', 'XHTML 1.0 Strict'); + $this->assertResult('', ''); + } + + function testTextareaTransform() { + $this->config->set('HTML.Doctype', 'HTML 4.01 Strict'); + $this->assertResult('', ''); + } + + function testTextInFieldset() { + $this->config->set('HTML.Doctype', 'HTML 4.01 Strict'); + $this->assertResult('
foo
'); + } + + function testStrict() { + $this->config->set('HTML.Doctype', 'HTML 4.01 Strict'); + $this->assertResult('
', ''); + } + + function testLegacy() { + $this->assertResult('
'); + $this->assertResult('
'); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ImageTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ImageTest.php new file mode 100644 index 000000000..30e36f1d1 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ImageTest.php @@ -0,0 +1,55 @@ +assertResult(''); + } + + function testLengthTooLarge() { + $this->assertResult( + '', + '' + ); + } + + function testLengthPercentage() { + $this->assertResult( + '', + '' + ); + } + + function testLengthCustomMax() { + $this->config->set('HTML.MaxImgLength', 20); + $this->assertResult( + '', + '' + ); + } + + function testLengthCrashFixDisabled() { + $this->config->set('HTML.MaxImgLength', null); + $this->assertResult( + '' + ); + $this->assertResult( + '' + ); + } + + function testLengthTrusted() { + $this->config->set('HTML.Trusted', true); + $this->assertResult( + '' + ); + $this->assertResult( + '' + ); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/NameTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/NameTest.php new file mode 100644 index 000000000..9152710bd --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/NameTest.php @@ -0,0 +1,32 @@ +config->set('Attr.EnableID', true); + $this->assertResult( + 'bar' + ); + } + + function testCDATA() { + $this->config->set('HTML.Attr.Name.UseCDATA', true); + $this->assertResult( + 'BazBar' + ); + } + + function testCDATAWithHeavyTidy() { + $this->config->set('HTML.Attr.Name.UseCDATA', true); + $this->config->set('HTML.TidyLevel', 'heavy'); + $this->assertResult('Baz'); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/NofollowTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/NofollowTest.php new file mode 100644 index 000000000..43084de3a --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/NofollowTest.php @@ -0,0 +1,26 @@ +config->set('HTML.Nofollow', true); + } + + function testNofollow() { + $this->assertResult( + 'abc', + 'abc' + ); + } + + function testNofollowDupe() { + $this->assertResult( + 'abc' + ); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ObjectTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ObjectTest.php new file mode 100644 index 000000000..f824d6021 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ObjectTest.php @@ -0,0 +1,38 @@ +config->set('HTML.Trusted', true); + } + + function testDefaultRemoval() { + $this->config->set('HTML.Trusted', false); + $this->assertResult( + '', '' + ); + } + + function testMinimal() { + $this->assertResult(''); + } + + function testStandardUseCase() { + $this->assertResult( +' + + + + +Windows Media player required +' + ); + } + + // more test-cases? + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ProprietaryTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ProprietaryTest.php new file mode 100644 index 000000000..21ebe8a5b --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ProprietaryTest.php @@ -0,0 +1,30 @@ +config->set('HTML.Proprietary', true); + } + + function testMarquee() { + $this->assertResult( + '
Block
InlineText
' + ); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/RubyTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/RubyTest.php new file mode 100644 index 000000000..23a1400d0 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/RubyTest.php @@ -0,0 +1,55 @@ +config->set('HTML.Doctype', 'XHTML 1.1'); + } + + function testBasicUse() { + $this->assertResult( + 'WWWWorld Wide Web' + ); + } + + function testRPUse() { + $this->assertResult( + 'WWW(World Wide Web)' + ); + } + + function testComplexUse() { + $this->assertResult( +' + + 10 + 31 + 2002 + + + Month + Day + Year + + + Expiration Date + +' + ); + + /* not implemented + function testBackwardsCompat() { + $this->assertResult( + 'A(aaa)', + 'A(aaa)' + ); + } + */ + + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/SafeEmbedTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/SafeEmbedTest.php new file mode 100644 index 000000000..779d34857 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/SafeEmbedTest.php @@ -0,0 +1,41 @@ +config->getHTMLDefinition(true); + $def->manager->addModule('SafeEmbed'); + } + + function testMinimal() { + $this->assertResult( + '', + '' + ); + } + + function testYouTube() { + $this->assertResult( + '', + '' + ); + } + + function testMalicious() { + $this->assertResult( + '', + '' + ); + } + + function testFull() { + $this->assertResult( + '' + ); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/SafeObjectTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/SafeObjectTest.php new file mode 100644 index 000000000..d8a23d50f --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/SafeObjectTest.php @@ -0,0 +1,49 @@ +config->set('HTML.DefinitionID', 'HTMLPurifier_HTMLModule_SafeObjectTest'); + $this->config->set('HTML.SafeObject', true); + } + + function testMinimal() { + $this->assertResult( + '', + '' + ); + } + + function testYouTube() { + // embed is purposely removed + $this->assertResult( + '', + '' + ); + } + + function testMalicious() { + $this->assertResult( + '', + '' + ); + } + + function testFull() { + $this->assertResult( + '' + ); + } + + function testFullScreen() { + $this->config->set('HTML.FlashAllowFullScreen', true); + $this->assertResult( + '' + ); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ScriptingTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ScriptingTest.php new file mode 100644 index 000000000..c844a4776 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/ScriptingTest.php @@ -0,0 +1,55 @@ +config->set('HTML.Trusted', true); + $this->config->set('Output.CommentScriptContents', false); + } + + function testDefaultRemoval() { + $this->config->set('HTML.Trusted', false); + $this->assertResult( + '', '' + ); + } + + function testPreserve() { + $this->assertResult( + '' + ); + } + + function testCDATAEnclosure() { + $this->assertResult( +'' + ); + } + + function testAllAttributes() { + $this->assertResult( + '' + ); + } + + function testUnsupportedAttributes() { + $this->assertResult( + '', + '' + ); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/TargetBlankTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/TargetBlankTest.php new file mode 100644 index 000000000..a757fecb3 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/TargetBlankTest.php @@ -0,0 +1,20 @@ +config->set('HTML.TargetBlank', true); + } + + function testTargetBlank() { + $this->assertResult( + 'abc', + 'abc' + ); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/TidyTest.php b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/TidyTest.php new file mode 100644 index 000000000..e80aeb16d --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/HTMLModule/TidyTest.php @@ -0,0 +1,224 @@ +fixesForLevel['light'][] = 'light-fix'; + $module->fixesForLevel['medium'][] = 'medium-fix'; + $module->fixesForLevel['heavy'][] = 'heavy-fix'; + + $this->assertIdentical( + array(), + $module->getFixesForLevel('none') + ); + $this->assertIdentical( + array('light-fix' => true), + $module->getFixesForLevel('light') + ); + $this->assertIdentical( + array('light-fix' => true, 'medium-fix' => true), + $module->getFixesForLevel('medium') + ); + $this->assertIdentical( + array('light-fix' => true, 'medium-fix' => true, 'heavy-fix' => true), + $module->getFixesForLevel('heavy') + ); + + $this->expectError('Tidy level turbo not recognized'); + $module->getFixesForLevel('turbo'); + + } + + function test_setup() { + + $i = 0; // counter, helps us isolate expectations + + // initialize partial mock + $module = new HTMLPurifier_HTMLModule_Tidy_TestForConstruct(); + $module->fixesForLevel['light'] = array('light-fix-1', 'light-fix-2'); + $module->fixesForLevel['medium'] = array('medium-fix-1', 'medium-fix-2'); + $module->fixesForLevel['heavy'] = array('heavy-fix-1', 'heavy-fix-2'); + + $j = 0; + $fixes = array( + 'light-fix-1' => $lf1 = $j++, + 'light-fix-2' => $lf2 = $j++, + 'medium-fix-1' => $mf1 = $j++, + 'medium-fix-2' => $mf2 = $j++, + 'heavy-fix-1' => $hf1 = $j++, + 'heavy-fix-2' => $hf2 = $j++ + ); + $module->setReturnValue('makeFixes', $fixes); + + $config = HTMLPurifier_Config::create(array( + 'HTML.TidyLevel' => 'none' + )); + $module->expectAt($i++, 'populate', array(array())); + $module->setup($config); + + // basic levels + + $config = HTMLPurifier_Config::create(array( + 'HTML.TidyLevel' => 'light' + )); + $module->expectAt($i++, 'populate', array(array( + 'light-fix-1' => $lf1, + 'light-fix-2' => $lf2 + ))); + $module->setup($config); + + $config = HTMLPurifier_Config::create(array( + 'HTML.TidyLevel' => 'heavy' + )); + $module->expectAt($i++, 'populate', array(array( + 'light-fix-1' => $lf1, + 'light-fix-2' => $lf2, + 'medium-fix-1' => $mf1, + 'medium-fix-2' => $mf2, + 'heavy-fix-1' => $hf1, + 'heavy-fix-2' => $hf2 + ))); + $module->setup($config); + + // fine grained tuning + + $config = HTMLPurifier_Config::create(array( + 'HTML.TidyLevel' => 'none', + 'HTML.TidyAdd' => array('light-fix-1', 'medium-fix-1') + )); + $module->expectAt($i++, 'populate', array(array( + 'light-fix-1' => $lf1, + 'medium-fix-1' => $mf1 + ))); + $module->setup($config); + + $config = HTMLPurifier_Config::create(array( + 'HTML.TidyLevel' => 'medium', + 'HTML.TidyRemove' => array('light-fix-1', 'medium-fix-1') + )); + $module->expectAt($i++, 'populate', array(array( + 'light-fix-2' => $lf2, + 'medium-fix-2' => $mf2 + ))); + $module->setup($config); + + } + + function test_makeFixesForLevel() { + + $module = new HTMLPurifier_HTMLModule_Tidy(); + $module->defaultLevel = 'heavy'; + + $module->makeFixesForLevel(array( + 'fix-1' => 0, + 'fix-2' => 1, + 'fix-3' => 2 + )); + + $this->assertIdentical($module->fixesForLevel['heavy'], array('fix-1', 'fix-2', 'fix-3')); + $this->assertIdentical($module->fixesForLevel['medium'], array()); + $this->assertIdentical($module->fixesForLevel['light'], array()); + + } + function test_makeFixesForLevel_undefinedLevel() { + + $module = new HTMLPurifier_HTMLModule_Tidy(); + $module->defaultLevel = 'bananas'; + + $this->expectError('Default level bananas does not exist'); + + $module->makeFixesForLevel(array( + 'fix-1' => 0 + )); + + } + + function test_getFixType() { + + // syntax needs documenting + + $module = new HTMLPurifier_HTMLModule_Tidy(); + + $this->assertIdentical( + $module->getFixType('a'), + array('tag_transform', array('element' => 'a')) + ); + + $this->assertIdentical( + $module->getFixType('a@href'), + $reuse = array('attr_transform_pre', array('element' => 'a', 'attr' => 'href')) + ); + + $this->assertIdentical( + $module->getFixType('a@href#pre'), + $reuse + ); + + $this->assertIdentical( + $module->getFixType('a@href#post'), + array('attr_transform_post', array('element' => 'a', 'attr' => 'href')) + ); + + $this->assertIdentical( + $module->getFixType('xml:foo@xml:bar'), + array('attr_transform_pre', array('element' => 'xml:foo', 'attr' => 'xml:bar')) + ); + + $this->assertIdentical( + $module->getFixType('blockquote#child'), + array('child', array('element' => 'blockquote')) + ); + + $this->assertIdentical( + $module->getFixType('@lang'), + array('attr_transform_pre', array('attr' => 'lang')) + ); + + $this->assertIdentical( + $module->getFixType('@lang#post'), + array('attr_transform_post', array('attr' => 'lang')) + ); + + } + + function test_populate() { + + $i = 0; + + $module = new HTMLPurifier_HTMLModule_Tidy(); + $module->populate(array( + 'element' => $element = $i++, + 'element@attr' => $attr = $i++, + 'element@attr#post' => $attr_post = $i++, + 'element#child' => $child = $i++, + 'element#content_model_type' => $content_model_type = $i++, + '@attr' => $global_attr = $i++, + '@attr#post' => $global_attr_post = $i++ + )); + + $module2 = new HTMLPurifier_HTMLModule_Tidy(); + $e = $module2->addBlankElement('element'); + $e->attr_transform_pre['attr'] = $attr; + $e->attr_transform_post['attr'] = $attr_post; + $e->child = $child; + $e->content_model_type = $content_model_type; + $module2->info_tag_transform['element'] = $element; + $module2->info_attr_transform_pre['attr'] = $global_attr; + $module2->info_attr_transform_post['attr'] = $global_attr_post; + + $this->assertEqual($module, $module2); + + } + +} + +// vim: et sw=4 sts=4 -- cgit v1.2.3