ReadTestFile($sFileToTest, self::INPUT_DIRECTORY); $sOutputHtml = $this->ReadTestFile($sFileToTest, self::OUTPUT_DIRECTORY); $sOutputHtml = $this->RemoveNewLines($sOutputHtml); $oSanitizer = new HTMLDOMSanitizer(); $sRes = $oSanitizer->DoSanitize($sInputHtml); // Removing newlines as the parser gives different results depending on the PHP version // Didn't manage to get it right : // - no php.ini difference // - playing with the parser preserveWhitespace/formatOutput parser options didn't help // So we're removing new lines on both sides :/ $sOutputHtml = $this->RemoveNewLines($sOutputHtml); $sRes = $this->RemoveNewLines($sRes); $this->debug($sRes); $this->assertEquals($sOutputHtml, $sRes); } public function DoSanitizeProvider() { return array( array( 'scripts.html', ), ); } /** * @dataProvider WhiteListProvider * * @param string $sHtmlToTest HTML content */ public function testDoSanitizeWhiteList($sHtmlToTest) { $oSanitizer = new HTMLDOMSanitizer(); $sRes = $oSanitizer->DoSanitize($sHtmlToTest); // Removing newlines as the parser gives different results depending on the PHP version // Didn't manage to get it right : // - no php.ini difference // - playing with the parser preserveWhitespace/formatOutput parser options didn't help // So we're removing new lines on both sides :/ $sHtmlToTest = $this->RemoveNewLines($sHtmlToTest); $sRes = $this->RemoveNewLines($sRes); $this->debug($sRes); $this->assertEquals($sHtmlToTest, $sRes); } public function WhiteListProvider() { // This is a copy of \HTMLDOMSanitizer::$aTagsWhiteList // should stay a copy as we want to check we're not removing something by mistake as it was done with the CENTER tag (N°2558) $aTagsWhiteList = array( // we don't test HTML and BODY as the parser removes them if context isn't appropriate 'a' => array('href', 'name', 'style', 'target', 'title'), 'p' => array('style'), 'blockquote' => array('style'), 'br' => array(), 'span' => array('style'), 'div' => array('style'), 'b' => array(), 'i' => array(), 'u' => array(), 'em' => array(), 'strong' => array(), 'img' => array('src', 'style', 'alt', 'title'), 'ul' => array('style'), 'ol' => array('style'), 'li' => array('style'), 'h1' => array('style'), 'h2' => array('style'), 'h3' => array('style'), 'h4' => array('style'), 'nav' => array('style'), 'section' => array('style'), 'code' => array('style'), 'table' => array('style', 'width', 'summary', 'align', 'border', 'cellpadding', 'cellspacing'), 'thead' => array('style'), 'tbody' => array('style'), 'tr' => array('style', 'colspan', 'rowspan'), 'td' => array('style', 'colspan', 'rowspan'), 'th' => array('style', 'colspan', 'rowspan'), 'fieldset' => array('style'), 'legend' => array('style'), 'font' => array('face', 'color', 'style', 'size'), 'big' => array(), 'small' => array(), 'tt' => array(), 'kbd' => array(), 'samp' => array(), 'var' => array(), 'del' => array(), 's' => array(), // strikethrough 'ins' => array(), 'cite' => array(), 'q' => array(), 'hr' => array('style'), 'pre' => array(), 'center' => array(), ); $aTestCaseArray = array(); $sInputText = $this->ReadTestFile('whitelist_test.html', self::INPUT_DIRECTORY); foreach ($aTagsWhiteList as $sTag => $aTagAttributes) { $sTestCaseText = $sInputText; $sStartTag = "<$sTag"; $iAttrCounter = 0; foreach ($aTagAttributes as $sTagAttribute) { $sStartTag .= $this->GetTagAttributeValue($sTagAttribute, $iAttrCounter); $iAttrCounter++; } $sStartTag .= '>'; $sTestCaseText = str_replace('##START_TAG##', $sStartTag, $sTestCaseText); $sClosingTag = $this->IsClosingTag($sTag) ? "" : ''; $sTestCaseText = str_replace('##END_TAG##', $sClosingTag, $sTestCaseText); $aTestCaseArray[$sTag] = array($sTestCaseText); } return $aTestCaseArray; } /** * @dataProvider RemoveBlackListedTagContentProvider */ public function testDoSanitizeRemoveBlackListedTagContent($html, $expected) { $oSanitizer = new HTMLDOMSanitizer(); $sSanitizedHtml = $oSanitizer->DoSanitize($html); $this->assertEquals($expected, str_replace("\n", '', $sSanitizedHtml)); } public function RemoveBlackListedTagContentProvider() { return array( 'basic' => array( 'html' => 'foobaz', 'expected' => '

foobaz

', ), 'basic with body' => array( 'html' => 'foobaz', 'expected' => 'foobaz', ), 'basic with html and body tags' => array( 'html' => 'foobaz', 'expected' => 'foobaz', ), 'basic with attributes' => array( 'html' => 'foobaz', 'expected' => '

foobaz

', ), 'basic with comment' => array( 'html' => 'foobaz', 'expected' => '

foobaz

', ), 'basic with contentRemovable tag' => array( 'html' => 'foobaz', 'expected' => '

foobaz

', ), 'nested' => array( 'html' => 'beforeoofafter', 'expected' => '

beforeafter

', ), 'nested with not closed br' => array( 'html' => 'beforeoofafter', 'expected' => '

beforeafter

', ), 'nested with allowed' => array( 'html' => 'beforeafter', 'expected' => '

beforeafter

', ), 'nested with spaces' => array( 'html' => 'beforeafter', 'expected' => '

beforeafter

', ), 'nested with attributes' => array( 'html' => 'beforeafter', 'expected' => '

beforeafter

', ), 'nested with allowed and attributes and spaces ' => array( 'html' => 'beforeafter', 'expected' => 'beforeafter', ), 'nested with allowed and contentRemovable tags' => array( 'html' => 'beforemiddleafter', 'expected' => 'beforemiddleafter', ), 'regression: if head present => body is not trimmed' => array( 'html' => 'bar', 'expected' => 'bar', ), ); } /** * @dataProvider CallInlineImageProcessImageTagProvider * @uses \InlineImageMock */ public function testDoSanitizeCallInlineImageProcessImageTag($sHtml, $iExpectedCount) { require_once APPROOT.'test/core/sanitizer/InlineImageMock.php'; InlineImageMock::ResetCallCounter(); $oSanitizer = new HTMLDOMSanitizer(InlineImageMock::class); $oSanitizer->DoSanitize($sHtml); $iCalledCount = \InlineImageMock::GetCallCounter(); $this->assertEquals($iExpectedCount, $iCalledCount); } public function CallInlineImageProcessImageTagProvider() { return array( 'no image' => array( 'html' => '

bar

', 'expected' => 0, ), 'basic image' => array( 'html' => '', 'expected' => 1, ), 'nested images within forbidden tags' => array( 'html' => '', 'expected' => 2, ), // This test will be restored with the ticket n°2556 // 'nested images within forbidden and removed tags' => array( // 'html' => '', // 'expected' => 2, // ), ); } }