Simple PHPUnit tests

This commit is contained in:
RainLoop Team 2016-05-20 22:02:39 +03:00
parent 72bf212f67
commit 2ed88f5d27
9 changed files with 85 additions and 7 deletions

2
.gitignore vendored
View file

@ -12,10 +12,10 @@
/rainloop/v/0.0.0/static/css/*.css /rainloop/v/0.0.0/static/css/*.css
/rainloop/v/0.0.0/static/js/*.js /rainloop/v/0.0.0/static/js/*.js
/rainloop/v/0.0.0/static/js/**/*.js /rainloop/v/0.0.0/static/js/**/*.js
/tests/_config.js
/node_modules /node_modules
/build/local /build/local
/build/dist /build/dist
/build/tmp /build/tmp
/data /data
.DS_Store .DS_Store
/tests/fix.php

5
.travis.yml Normal file
View file

@ -0,0 +1,5 @@
language: php
php:
- '5.3'
- '5.6'
- '7.0'

View file

@ -52,7 +52,9 @@ class HtmlUtils
$sText = \MailSo\Base\HtmlUtils::ClearBodyAndHtmlTag($sText, $sHtmlAttrs, $sBodyAttrs); $sText = \MailSo\Base\HtmlUtils::ClearBodyAndHtmlTag($sText, $sHtmlAttrs, $sBodyAttrs);
@$oDom->loadHTML('<'.'?xml version="1.0" encoding="utf-8"?'.'>'. @$oDom->loadHTML('<'.'?xml version="1.0" encoding="utf-8"?'.'>'.
'<html '.$sHtmlAttrs.'><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body '.$sBodyAttrs.'><div data-wrp="rainloop">'.$sText.'</div></body></html>'); '<html '.$sHtmlAttrs.'><head>'.
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>'.
'<body '.$sBodyAttrs.'><div data-wrp="rainloop">'.$sText.'</div></body></html>');
@$oDom->normalizeDocument(); @$oDom->normalizeDocument();
@ -141,6 +143,18 @@ class HtmlUtils
else else
{ {
$sResult = \trim($oDom->saveHTML($oDiv)); $sResult = \trim($oDom->saveHTML($oDiv));
if (0 === strpos($sResult, '<div>'))
{
$sResult = substr($sResult, 5);
if ('</div>' === substr($sResult, -6))
{
$sResult = substr($sResult, 0, -6);
}
$sResult = \trim($sResult);
}
} }
} }
else else
@ -175,7 +189,7 @@ class HtmlUtils
$sBodyAttrs = $aMatch[1]; $sBodyAttrs = $aMatch[1];
} }
$sHtml = \preg_replace('/<body([^>]*)>/im', '', $sHtml); $sHtml = \preg_replace('/^.*<body([^>]*)>/sim', '', $sHtml);
$sHtml = \preg_replace('/<\/body>/im', '', $sHtml); $sHtml = \preg_replace('/<\/body>/im', '', $sHtml);
$sHtml = \preg_replace('/<html([^>]*)>/im', '', $sHtml); $sHtml = \preg_replace('/<html([^>]*)>/im', '', $sHtml);
$sHtml = \preg_replace('/<\/html>/im', '', $sHtml); $sHtml = \preg_replace('/<\/html>/im', '', $sHtml);
@ -747,18 +761,24 @@ class HtmlUtils
* @param string $sHtml * @param string $sHtml
* @param bool $bDoNotReplaceExternalUrl = false * @param bool $bDoNotReplaceExternalUrl = false
* @param bool $bFindLinksInHtml = false * @param bool $bFindLinksInHtml = false
* @param bool $bWrapByFakeHtmlAndBodyDiv = true
* *
* @return string * @return string
*/ */
public static function ClearHtmlSimple($sHtml, $bDoNotReplaceExternalUrl = false, $bFindLinksInHtml = false) public static function ClearHtmlSimple($sHtml, $bDoNotReplaceExternalUrl = false, $bFindLinksInHtml = false, $bWrapByFakeHtmlAndBodyDiv = true)
{ {
$bHasExternals = false; $bHasExternals = false;
$aFoundCIDs = array(); $aFoundCIDs = array();
$aContentLocationUrls = array(); $aContentLocationUrls = array();
$aFoundedContentLocationUrls = array(); $aFoundedContentLocationUrls = array();
$fAdditionalExternalFilter = null;
$fAdditionalDomReader = null;
$bTryToDetectHiddenImages = false;
return \MailSo\Base\HtmlUtils::ClearHtml($sHtml, $bHasExternals, $aFoundCIDs, return \MailSo\Base\HtmlUtils::ClearHtml($sHtml, $bHasExternals, $aFoundCIDs,
$aContentLocationUrls, $aFoundedContentLocationUrls, $bDoNotReplaceExternalUrl, $bFindLinksInHtml); $aContentLocationUrls, $aFoundedContentLocationUrls, $bDoNotReplaceExternalUrl, $bFindLinksInHtml,
$fAdditionalExternalFilter, $fAdditionalDomReader, $bTryToDetectHiddenImages,
$bWrapByFakeHtmlAndBodyDiv);
} }
/** /**
@ -771,6 +791,8 @@ class HtmlUtils
* @param bool $bFindLinksInHtml = false * @param bool $bFindLinksInHtml = false
* @param callback|null $fAdditionalExternalFilter = null * @param callback|null $fAdditionalExternalFilter = null
* @param callback|null $fAdditionalDomReader = null * @param callback|null $fAdditionalDomReader = null
* @param bool $bTryToDetectHiddenImages = false
* @param bool $bWrapByFakeHtmlAndBodyDiv = true
* *
* @return string * @return string
*/ */
@ -778,7 +800,7 @@ class HtmlUtils
$aContentLocationUrls = array(), &$aFoundedContentLocationUrls = array(), $aContentLocationUrls = array(), &$aFoundedContentLocationUrls = array(),
$bDoNotReplaceExternalUrl = false, $bFindLinksInHtml = false, $bDoNotReplaceExternalUrl = false, $bFindLinksInHtml = false,
$fAdditionalExternalFilter = null, $fAdditionalDomReader = false, $fAdditionalExternalFilter = null, $fAdditionalDomReader = false,
$bTryToDetectHiddenImages = false) $bTryToDetectHiddenImages = false, $bWrapByFakeHtmlAndBodyDiv = true)
{ {
$sResult = ''; $sResult = '';
@ -1096,7 +1118,7 @@ class HtmlUtils
$oElement->removeAttribute('data-x-skip-style'); $oElement->removeAttribute('data-x-skip-style');
} }
$sResult = \MailSo\Base\HtmlUtils::GetTextFromDom($oDom); $sResult = \MailSo\Base\HtmlUtils::GetTextFromDom($oDom, $bWrapByFakeHtmlAndBodyDiv);
} }
unset($oDom); unset($oDom);

View file

@ -0,0 +1,21 @@
<?php
class HtmlUtilsTest extends PHPUnit_Framework_TestCase
{
public function testCommon()
{
$this->assertTrue(true);
$this->assertTrue(class_exists('\\RainLoop\\Api'));
$this->assertTrue(class_exists('\\MailSo\\Base\\HtmlUtils'));
}
public function testClearHtml()
{
$i = 0;
while (++$i < 3)
{
$this->assertEquals(file_get_contents(TEST_DATA_FOLDER."/html/{$i}-ok.html"),
\MailSo\Base\HtmlUtils::ClearHtmlSimple(file_get_contents(TEST_DATA_FOLDER."/html/{$i}.html"), false, false, false));
}
}
}

View file

@ -0,0 +1 @@
123

11
tests/_data/html/1.html Normal file
View file

@ -0,0 +1,11 @@
<html>
<head>
<style type="text/css">
body {color:red}
p {margin:0; padding:0}
</style>
</head>
<body>
123
</body>
</html>

View file

@ -0,0 +1 @@
123

12
tests/_data/html/2.html Normal file
View file

@ -0,0 +1,12 @@
<html>
<head>
<style type="text/css">
body {color:red}
p {margin:0; padding:0}
</style>
</head>
<body>
123
<script>alert(1)</script>
</body>
</html>

View file

@ -1 +1,6 @@
<?php <?php
define('TEST_DATA_FOLDER', __DIR__.'/_data');
$_ENV['RAINLOOP_INCLUDE_AS_API'] = true;
include __DIR__.'/../index.php';