Standardize on uppercase

This commit is contained in:
Kijin Sung 2016-01-11 16:44:10 +09:00
parent 4449d4ad72
commit d51ad8b4e5
16 changed files with 19 additions and 19 deletions

View file

@ -0,0 +1,73 @@
<?php
require_once( _XE_PATH_.'config/func.inc.php');
class FuncIncTest extends \Codeception\TestCase\Test
{
static public function provider()
{
return array(
// remove iframe
array(
'<div class="frame"><iframe src="path/to/file.html"></iframe><p><a href="#iframe">IFrame</a></p></div>',
// '<div class="frame">&lt;iframe src="path/to/file.html">&lt;/iframe><p><a href="#iframe">IFrame</a></p></div>'
'<div class="frame"><iframe></iframe><p><a href="#iframe">IFrame</a></p></div>'
),
// expression
array(
'<div class="dummy" style="xss:expr/*XSS*/ession(alert(\'XSS\'))">',
'<div class="dummy"></div>'
),
// no quotes and no semicolon - http://ha.ckers.org/xss.html
array(
'<img src=javascript:alert(\'xss\')>',
''
),
// embedded encoded tab to break up XSS - http://ha.ckers.org/xss.html
array(
'<IMG SRC="jav&#x09;ascript:alert(\'XSS\');">',
'<img src="jav%20ascript%3Aalert(\'XSS\');" alt="jav ascript:alert(\'XSS\');" />'
),
// issue 178
array(
'<img src="invalid.jpg"\nonerror="alert(1)" />',
'<img src="invalid.jpg" alt="invalid.jpg" />'
),
// issue 534
array(
'<img src=\'as"df dummy=\'"1234\'" 4321\' asdf/*/>*/" onerror="console.log(\'Yet another XSS\')">',
'<img src="as" alt="as&quot;df dummy=" />*/" onerror="console.log(\'Yet another XSS\')"&gt;'
),
// issue 602
array(
'<img alt="test" src="(http://static.naver.com/www/u/2010/0611/nmms_215646753.gif" onload="eval(String.fromCharCode(105,61,49,48,48,59,119,104,105,108,101, 40,105,62,48,41,97,108,101,114,116,40,40,105,45,45,41,43,39,48264,47564,32, 45908,32,53364,47533,54616,49464,50836,39,41,59));">',
'<img alt="test" src="(http%3A//static.naver.com/www/u/2010/0611/nmms_215646753.gif" />'
),
// issue #1813 https://github.com/xpressengine/xe-core/issues/1813
array(
'<img src="?act=dispLayoutPreview" alt="dummy" />',
'<img alt="dummy" />'
),
array(
'<img src="?act =dispLayoutPreview" alt="dummy" />',
'<img alt="dummy" />'
),
array(
"<img src=\"?act\n=dispLayoutPreview\" alt=\"dummy\" />",
'<img alt="dummy" />'
),
array(
"<img src=\"?pam=act&a\nct =\r\n\tdispLayoutPreview\" alt=\"dummy\" />",
'<img alt="dummy" />'
)
);
}
/**
* @dataProvider provider
*/
public function testXss($source, $expected)
{
$result = removeHackTag($source);
$this->assertEquals($result, $expected);
}
}

View file

@ -0,0 +1,2 @@
<?php
$_SERVER['SCRIPT_NAME'] = '/xe/index.php';

View file

@ -0,0 +1,91 @@
<?php
require_once _XE_PATH_.'classes/context/Context.class.php';
require_once _XE_PATH_.'classes/handler/Handler.class.php';
require_once _XE_PATH_.'classes/frontendfile/FrontEndFileHandler.class.php';
class ContextTest extends \Codeception\TestCase\Test
{
/**
* @var \UnitTester
*/
protected $tester;
protected function _before()
{
}
protected function _after()
{
}
public function testGetInstance()
{
$this->assertInstanceOf('Context', Context::getInstance());
$this->assertSame(Context::getInstance(), Context::getInstance());
}
public function testSetGetVars()
{
$this->assertEquals(Context::get('var1'), null);
Context::getInstance()->context = new stdClass;
Context::set('var1', 'val1');
$this->assertEquals(Context::get('var1'), 'val1');
Context::set('var2', 'val2');
$this->assertSame(Context::get('var2'), 'val2');
Context::set('var3', 'val3');
$data = new stdClass();
$data->var1 = 'val1';
$data->var2 = 'val2';
$this->assertEquals(Context::gets('var1','var2'), $data);
$data->var3 = 'val3';
$this->assertEquals(Context::getAll(), $data);
}
public function testAddGetBodyClass()
{
$this->assertEquals(Context::getBodyClass(), '');
Context::addBodyClass('red');
$this->assertEquals(Context::getBodyClass(), ' class="red"');
Context::addBodyClass('green');
$this->assertEquals(Context::getBodyClass(), ' class="red green"');
Context::addBodyClass('blue');
$this->assertEquals(Context::getBodyClass(), ' class="red green blue"');
// remove duplicated class
Context::addBodyClass('red');
$this->assertEquals(Context::getBodyClass(), ' class="red green blue"');
}
public function testRequsetResponseMethod()
{
$this->assertEquals(Context::getRequestMethod(), 'GET');
$_SERVER['REQUEST_METHOD'] = 'POST';
Context::setRequestMethod();
$this->assertEquals(Context::getRequestMethod(), 'POST');
$GLOBALS['HTTP_RAW_POST_DATA'] = 'abcde';
Context::setRequestMethod();
$this->assertEquals(Context::getRequestMethod(), 'XMLRPC');
$_SERVER['CONTENT_TYPE'] = 'application/json';
Context::setRequestMethod();
$this->assertEquals(Context::getRequestMethod(), 'JSON');
Context::setRequestMethod('POST');
$this->assertEquals(Context::getRequestMethod(), 'POST');
$this->assertEquals(Context::getResponseMethod(), 'HTML');
Context::setRequestMethod('JSON');
$this->assertEquals(Context::getResponseMethod(), 'JSON');
Context::setResponseMethod('WRONG_TYPE');
$this->assertEquals(Context::getResponseMethod(), 'HTML');
Context::setResponseMethod('XMLRPC');
$this->assertEquals(Context::getResponseMethod(), 'XMLRPC');
Context::setResponseMethod('HTML');
$this->assertEquals(Context::getResponseMethod(), 'HTML');
}
}

View file

@ -0,0 +1,44 @@
<?php
require_once _XE_PATH_.'classes/file/FileHandler.class.php';
class FileHandlerTest extends \Codeception\TestCase\Test
{
/**
* @var \UnitTester
*/
protected $tester;
protected function _before()
{
}
protected function _after()
{
}
// tests
public function testFileSize()
{
// file size
$this->assertEquals(FileHandler::filesize(0), '0Byte');
$this->assertEquals(FileHandler::filesize(1), '1Byte');
$this->assertEquals(FileHandler::filesize(386), '386Bytes');
$this->assertEquals(FileHandler::filesize(1023), '1023Bytes');
$this->assertEquals(FileHandler::filesize(1024), '1.0KB');
$this->assertEquals(FileHandler::filesize(2480), '2.4KB');
$this->assertEquals(FileHandler::filesize(1024*1024-1), '1024.0KB');
$this->assertEquals(FileHandler::filesize(1024*1024), '1.00MB');
$this->assertEquals(FileHandler::filesize(3*1024*1024+210*1024), '3.21MB');
// return bytes
$this->assertEquals(FileHandler::returnBytes('0B'), 0);
$this->assertEquals(FileHandler::returnBytes('1024B'), 1024);
$this->assertEquals(FileHandler::returnBytes('1K'), 1024);
$this->assertEquals(FileHandler::returnBytes('102.48K'), round(1024*102.48));
$this->assertEquals(FileHandler::returnBytes('1M'), 1024*1024);
$this->assertEquals(FileHandler::returnBytes('1.12M'), round(1024*1024*1.12));
$this->assertEquals(FileHandler::returnBytes('1023.99M'), round(1024*1024*1023.99));
$this->assertEquals(FileHandler::returnBytes('1G'), 1024*1024*1024);
$this->assertEquals(FileHandler::returnBytes('12.02G'), round(1024*1024*1024*12.02));
}
}

View file

@ -0,0 +1,241 @@
<?php
require_once _XE_PATH_.'classes/frontendfile/FrontEndFileHandler.class.php';
class FrontEndFileHandlerTest extends \Codeception\TestCase\Test
{
use \Codeception\Specify;
private function _filemtime($file)
{
return '?' . date('YmdHis', filemtime(_XE_PATH_ . $file));
}
public function testFrontEndFileHandler()
{
$handler = new FrontEndFileHandler();
HTMLDisplayHandler::$reservedCSS = '/xxx$/';
HTMLDisplayHandler::$reservedJS = '/xxx$/';
$db_info = Context::getDBInfo() ?: new stdClass;
$db_info->minify_scripts = 'none';
Context::setDBInfo($db_info);
$this->specify("js(head)", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/js/jquery.js'));
$handler->loadFile(array('./common/js/js_app.js', 'head'));
$handler->loadFile(array('./common/js/common.js', 'body'));
$handler->loadFile(array('./common/js/common.js', 'head'));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'body'));
if($db_info->minify_scripts === 'none')
{
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null);
}
else
{
$expected[] = array('file' => '/xe/common/js/jquery.min.js' . $this->_filemtime('common/js/jquery.min.js'), 'targetie' => null);
}
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected);
});
$this->specify("js(body)", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/js/jquery.js', 'body'));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head'));
if($db_info->minify_scripts === 'none')
{
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null);
}
else
{
$expected[] = array('file' => '/xe/common/js/jquery.min.js' . $this->_filemtime('common/js/jquery.min.js'), 'targetie' => null);
}
$this->assertEquals($handler->getJsFileList('body'), $expected);
});
$this->specify("css", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/css/xe.css'));
$handler->loadFile(array('./common/css/mobile.css'));
$expected[] = array('file' => '/xe/common/css/xe.css' . $this->_filemtime('common/css/xe.css'), 'media' => 'all', 'targetie' => null);
$expected[] = array('file' => '/xe/common/css/mobile.css' . $this->_filemtime('common/css/mobile.css'), 'media' => 'all', 'targetie' => null);
$this->assertEquals($handler->getCssFileList(), $expected);
});
$this->specify("order (duplicate)", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
if($db_info->minify_scripts === 'none')
{
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null);
}
else
{
$expected[] = array('file' => '/xe/common/js/jquery.min.js' . $this->_filemtime('common/js/jquery.min.js'), 'targetie' => null);
}
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_handler.js' . $this->_filemtime('common/js/xml_handler.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_js_filter.js' . $this->_filemtime('common/js/xml_js_filter.js'), 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected);
});
$this->specify("order (redefine)", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/js/xml_handler.js', 'head', '', 1));
$handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
if($db_info->minify_scripts === 'none')
{
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => null);
}
else
{
$expected[] = array('file' => '/xe/common/js/jquery.min.js' . $this->_filemtime('common/js/jquery.min.js'), 'targetie' => null);
}
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_js_filter.js' . $this->_filemtime('common/js/xml_js_filter.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_handler.js' . $this->_filemtime('common/js/xml_handler.js'), 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected);
});
$this->specify("unload", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
$handler->unloadFile('./common/js/jquery.js', '', 'all');
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/common.js' . $this->_filemtime('common/js/common.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_handler.js' . $this->_filemtime('common/js/xml_handler.js'), 'targetie' => null);
$expected[] = array('file' => '/xe/common/js/xml_js_filter.js' . $this->_filemtime('common/js/xml_js_filter.js'), 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected);
});
$this->specify("target IE(js)", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/js/jquery.js', 'head', 'ie6'));
$handler->loadFile(array('./common/js/jquery.js', 'head', 'ie7'));
$handler->loadFile(array('./common/js/jquery.js', 'head', 'ie8'));
if($db_info->minify_scripts === 'none')
{
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => 'ie6');
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => 'ie7');
$expected[] = array('file' => '/xe/common/js/jquery.js' . $this->_filemtime('common/js/jquery.js'), 'targetie' => 'ie8');
}
else
{
$expected[] = array('file' => '/xe/common/js/jquery.min.js' . $this->_filemtime('common/js/jquery.min.js'), 'targetie' => 'ie6');
$expected[] = array('file' => '/xe/common/js/jquery.min.js' . $this->_filemtime('common/js/jquery.min.js'), 'targetie' => 'ie7');
$expected[] = array('file' => '/xe/common/js/jquery.min.js' . $this->_filemtime('common/js/jquery.min.js'), 'targetie' => 'ie8');
}
$this->assertEquals($handler->getJsFileList(), $expected);
});
$this->specify("external file - schemaless", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('http://external.host/js/script.js'));
$handler->loadFile(array('https://external.host/js/script.js'));
$handler->loadFile(array('//external.host/js/script1.js'));
$handler->loadFile(array('///external.host/js/script2.js'));
$expected[] = array('file' => 'http://external.host/js/script.js', 'targetie' => null);
$expected[] = array('file' => 'https://external.host/js/script.js', 'targetie' => null);
$expected[] = array('file' => '//external.host/js/script1.js', 'targetie' => null);
$expected[] = array('file' => '//external.host/js/script2.js', 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected);
});
$this->specify("external file - schemaless", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('//external.host/js/script.js'));
$handler->loadFile(array('///external.host/js/script.js'));
$expected[] = array('file' => '//external.host/js/script.js', 'targetie' => null);
$this->assertEquals($handler->getJsFileList(), $expected);
});
$this->specify("target IE(css)", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/css/common.css', null, 'ie6'));
$handler->loadFile(array('./common/css/common.css', null, 'ie7'));
$handler->loadFile(array('./common/css/common.css', null, 'ie8'));
$expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => 'ie6');
$expected[] = array('file' => '/xe/common/css/common.css','media'=>'all', 'targetie' => 'ie7');
$expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => 'ie8');
$this->assertEquals($handler->getCssFileList(), $expected);
});
$this->specify("media", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/css/common.css', 'all'));
$handler->loadFile(array('./common/css/common.css', 'screen'));
$handler->loadFile(array('./common/css/common.css', 'handled'));
$expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => null);
$expected[] = array('file' => '/xe/common/css/common.css','media'=>'screen', 'targetie' => null);
$expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'handled', 'targetie' => null);
$this->assertEquals($handler->getCssFileList(), $expected);
});
$db_info->minify_scripts = 'all';
Context::setDBInfo($db_info);
FrontEndFileHandler::$minify = null;
$this->specify("minify", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('./common/css/xe.css'));
$handler->loadFile(array('./common/css/mobile.css'));
$expected[] = array('file' => '/xe/files/cache/minify/common.css.xe.min.css', 'media' => 'all', 'targetie' => null);
$expected[] = array('file' => '/xe/files/cache/minify/common.css.mobile.min.css', 'media' => 'all', 'targetie' => null);
$result = $handler->getCssFileList();
$result[0]['file'] = preg_replace('/\?\d+$/', '', $result[0]['file']);
$result[1]['file'] = preg_replace('/\?\d+$/', '', $result[1]['file']);
$this->assertEquals($result, $expected);
});
$this->specify("external file", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('http://external.host/css/style1.css'));
$handler->loadFile(array('https://external.host/css/style2.css'));
$expected[] = array('file' => 'http://external.host/css/style1.css', 'media'=>'all', 'targetie' => null);
$expected[] = array('file' => 'https://external.host/css/style2.css', 'media'=>'all', 'targetie' => null);
$this->assertEquals($handler->getCssFileList(), $expected);
});
$this->specify("external file - schemaless", function() use($db_info) {
$handler = new FrontEndFileHandler();
$handler->loadFile(array('//external.host/css/style.css'));
$handler->loadFile(array('///external.host/css2/style2.css'));
$expected[] = array('file' => '//external.host/css/style.css', 'media'=>'all', 'targetie' => null);
$expected[] = array('file' => '//external.host/css2/style2.css', 'media'=>'all', 'targetie' => null);
$this->assertEquals($handler->getCssFileList(), $expected);
});
}
}

View file

@ -0,0 +1,122 @@
<?php
require_once _XE_PATH_.'classes/security/Security.class.php';
class SecurityTest extends \Codeception\TestCase\Test
{
public function _before()
{
/**
* Setup mock data
**/
// string
Context::set('content1', '<strong>Hello, world</strong>');
Context::set('content2', 'Wow, >_< !');
// object
$args = new stdClass;
$args->prop1 = 'Normal string';
$args->prop2 = 'He said, "Very nice!"';
$args->prop3 = '<strong>Strong</strong> Baby';
Context::set('object1', $args);
// array
$arr = array();
$arr[] = '<span class="first">F</span>irst';
$arr[] = '<u>S</u>econd';
$arr[] = '<b>T</b>hird';
Context::set('array1', $arr);
// associative array
$aarr = array();
$aarr['elem1'] = 'One <ins>1</ins>';
$aarr['elem2'] = 'Two <del>2</del>';
$aarr['elem3'] = 'Three <addr>3</addr>';
Context::set('array2', $aarr);
}
public function testEncodeHtmlDefaultContext()
{
$security = new Security();
$this->assertTrue(true);
// normal string - one
$this->_before();
$this->assertEquals('<strong>Hello, world</strong>', Context::get('content1'));
$security->encodeHTML('content1');
$this->assertEquals('&lt;strong&gt;Hello, world&lt;/strong&gt;', Context::get('content1'));
// normal string - two
$this->_before();
$this->assertEquals('<strong>Hello, world</strong>', Context::get('content1'));
$this->assertEquals('Wow, >_< !', Context::get('content2'));
$security->encodeHTML('content1','content2');
$this->assertEquals('&lt;strong&gt;Hello, world&lt;/strong&gt;', Context::get('content1'));
$this->assertEquals('Wow, &gt;_&lt; !', Context::get('content2'));
// array
$this->assertEquals(Context::get('array1'), array('<span class="first">F</span>irst','<u>S</u>econd','<b>T</b>hird'));
$security->encodeHTML('array1'); // should ignore this
$this->assertEquals(Context::get('array1'), array('<span class="first">F</span>irst','<u>S</u>econd','<b>T</b>hird'));
$security->encodeHTML('array1.0'); // affect only first element
$this->assertEquals(Context::get('array1'), array('&lt;span class=&quot;first&quot;&gt;F&lt;/span&gt;irst','<u>S</u>econd','<b>T</b>hird'));
$security->encodeHTML('array1.2'); // affects only third element
$this->assertEquals(Context::get('array1'), array('&lt;span class=&quot;first&quot;&gt;F&lt;/span&gt;irst','<u>S</u>econd','&lt;b&gt;T&lt;/b&gt;hird'));
$this->_before(); // reset;
$this->assertEquals(Context::get('array1'), array('<span class="first">F</span>irst','<u>S</u>econd','<b>T</b>hird'));
$security->encodeHTML('array1.'); // affects all items
$this->assertEquals(Context::get('array1'), array('&lt;span class=&quot;first&quot;&gt;F&lt;/span&gt;irst','&lt;u&gt;S&lt;/u&gt;econd','&lt;b&gt;T&lt;/b&gt;hird'));
// associated array
$this->assertEquals(Context::get('array2'), array('elem1'=>'One <ins>1</ins>','elem2'=>'Two <del>2</del>','elem3'=>'Three <addr>3</addr>'));
$security->encodeHTML('array2'); // should ignore this
$this->assertEquals(Context::get('array2'), array('elem1'=>'One <ins>1</ins>','elem2'=>'Two <del>2</del>','elem3'=>'Three <addr>3</addr>'));
$security->encodeHTML('array2.0'); // should ignore this
$this->assertEquals(Context::get('array2'), array('elem1'=>'One <ins>1</ins>','elem2'=>'Two <del>2</del>','elem3'=>'Three <addr>3</addr>'));
$security->encodeHTML('array2.elem2'); // affects only 'elem2'
$this->assertEquals(Context::get('array2'), array('elem1'=>'One <ins>1</ins>','elem2'=>'Two &lt;del&gt;2&lt;/del&gt;','elem3'=>'Three <addr>3</addr>'));
$this->_before(); // reset;
$this->assertEquals(Context::get('array2'), array('elem1'=>'One <ins>1</ins>','elem2'=>'Two <del>2</del>','elem3'=>'Three <addr>3</addr>'));
$security->encodeHTML('array2.'); // affects all items
$this->assertEquals(Context::get('array2'), array('elem1'=>'One &lt;ins&gt;1&lt;/ins&gt;','elem2'=>'Two &lt;del&gt;2&lt;/del&gt;','elem3'=>'Three &lt;addr&gt;3&lt;/addr&gt;'));
// object
$obj = new stdClass;
$obj->prop1 = 'Normal string';
$obj->prop2 = 'He said, "Very nice!"';
$obj->prop3 = '<strong>Strong</strong> Baby';
$this->assertEquals(Context::get('object1'), $obj);
$security->encodeHTML('object1'); // should ignore this
$this->assertEquals(Context::get('object1'), $obj);
$security->encodeHTML('object1.0'); // should ignore this
$this->assertEquals(Context::get('object1'), $obj);
$security->encodeHTML('object1.prop1'); // affects only 'prop1' property - no changes
$this->assertEquals(Context::get('object1'), $obj);
$security->encodeHTML('object1.prop3'); // affects only 'prop3' property
$obj->prop3 = '&lt;strong&gt;Strong&lt;/strong&gt; Baby';
$this->assertEquals(Context::get('object1'), $obj);
$this->_before(); // reset
$obj->prop3 = '<strong>Strong</strong> Baby';
$this->assertEquals(Context::get('object1'), $obj);
$security->encodeHTML('object1.'); // affects all properties
$obj->prop2 = 'He said, &quot;Very nice!&quot;';
$obj->prop3 = '&lt;strong&gt;Strong&lt;/strong&gt; Baby';
$this->assertEquals(Context::get('object1'), $obj);
}
public function testEncodeHtmlCustomContext()
{
$array = array('Hello', 'World', '<b>Bold</b> is not bald');
// array with no nested objects or arrays
$security = new Security($array);
$returned = $security->encodeHTML('.');
$this->assertEquals($returned, array('Hello', 'World', '&lt;b&gt;Bold&lt;/b&gt; is not bald'));
// associative array
$array = array('first'=>'Hello', 'second'=>'World', '3rd'=>'<b>Bold</b> is not bald');
$security = new Security($array);
$returned = $security->encodeHTML('first','3rd');
$this->assertEquals($returned, array('first'=>'Hello', 'second'=>'World', '3rd'=>'&lt;b&gt;Bold&lt;/b&gt; is not bald'));
}
}

View file

@ -0,0 +1,331 @@
<?php
require_once _XE_PATH_.'classes/file/FileHandler.class.php';
require_once _XE_PATH_.'classes/template/TemplateHandler.class.php';
$_SERVER['SCRIPT_NAME'] = '/xe/tests/Unit/classes/template/index.php';
class TemplateHandlerTest extends \Codeception\TestCase\Test
{
var $prefix = '<?php if(!defined("__XE__"))exit;';
static public function provider()
{
return array(
// pipe cond
array(
'<a href="#" class="active"|cond="$cond > 10">Link</a>',
'?><a href="#"<?php if($__Context->cond > 10){ ?> class="active"<?php } ?>>Link</a>'
),
// cond
array(
'<a href="#">Link1</a><a href="#cond"><span cond="$cond">say, hello</span></a>',
'?><a href="#">Link1</a><a href="#cond"><?php if($__Context->cond){ ?><span>say, hello</span><?php } ?></a>'
),
// cond
array(
'<a href="#">Link1</a><a href="#cond" cond="$v==$k">Link2</a>',
'?><a href="#">Link1</a><?php if($__Context->v==$__Context->k){ ?><a href="#cond">Link2</a><?php } ?>'
),
// for loop
array(
'<ul><li loop="$i=0;$i<$len;$i++" class="sample"><a>Link</a></li></ul>',
'?><ul><?php for($__Context->i=0;$__Context->i<$__Context->len;$__Context->i++){ ?><li class="sample"><a>Link</a></li><?php } ?></ul>'
),
// foreach loop
array(
'<ul><li loop="$arr=>$key,$val" class="sample"><a>Link</a><ul><li loop="$arr2=>$key2,$val2"></li></ul></li></ul>',
'?><ul><?php $tba7582b53a5d9d2bfecc5c52511ef7ee=$__Context->arr;if($tba7582b53a5d9d2bfecc5c52511ef7ee&&count($tba7582b53a5d9d2bfecc5c52511ef7ee))foreach($tba7582b53a5d9d2bfecc5c52511ef7ee as $__Context->key=>$__Context->val){ ?><li class="sample"><a>Link</a><ul><?php $tf92ab3139c8d388f9a5f63e0fdd8f516=$__Context->arr2;if($tf92ab3139c8d388f9a5f63e0fdd8f516&&count($tf92ab3139c8d388f9a5f63e0fdd8f516))foreach($tf92ab3139c8d388f9a5f63e0fdd8f516 as $__Context->key2=>$__Context->val2){ ?><li></li><?php } ?></ul></li><?php } ?></ul>'
),
// while loop
array(
'<ul><li loop="$item=get_loop_item()" class="sample"><a>Link</a></li></ul>',
'?><ul><?php while($__Context->item=get_loop_item()){ ?><li class="sample"><a>Link</a></li><?php } ?></ul>'
),
// <!--@if--> ~ <!--@end-->
array(
'<a>Link</a><!--@if($cond)--><strong>Hello, world</strong><!--@end--> <dummy />',
'?><a>Link</a><?php if($__Context->cond){ ?><strong>Hello, world</strong><?php } ?> <dummy />'
),
// <!--@if--> ~ <!--@endif-->
array(
'<a>Link</a><!--@if($cond)--><strong>Hello, {$world}</strong><!--@endif--><dummy />',
'?><a>Link</a><?php if($__Context->cond){ ?><strong>Hello, <?php echo $__Context->world ?></strong><?php } ?><dummy />'
),
// <!--@if--> ~ <!--@else--> ~ <!--@endif-->
array(
'<a>Link</a><!--@if($cond)--><strong>Hello, world</strong><!--@else--><em>Wow</em><!--@endif--><dummy />',
'?><a>Link</a><?php if($__Context->cond){ ?><strong>Hello, world</strong><?php }else{ ?><em>Wow</em><?php } ?><dummy />'
),
// <!--@if--> ~ <!--@elseif--> ~ <!--@else--> ~ <!--@endif-->
array(
'<a>Link</a><!--@if($cond)--><strong>Hello, world</strong><!--@elseif($cond2)--><u>HaHa</u><!--@else--><em>Wow</em><!--@endif--><dummy />',
'?><a>Link</a><?php if($__Context->cond){ ?><strong>Hello, world</strong><?php }elseif($__Context->cond2){ ?><u>HaHa</u><?php }else{ ?><em>Wow</em><?php } ?><dummy />'
),
// <!--@for--> ~ <!--@endfor-->
array(
'<!--@for($i=0;$i<$len;$i++)--><li>Repeat this</li><!--@endfor-->',
PHP_EOL.'for($__Context->i=0;$__Context->i<$__Context->len;$__Context->i++){ ?><li>Repeat this</li><?php } ?>'
),
// <!--@foreach--> ~ <!--@endforeach-->
array(
'<!--@foreach($arr as $key=>$val)--><li>item{$key} : {$val}</li><!--@endfor-->',
PHP_EOL . 'if($__Context->arr&&count($__Context->arr))foreach($__Context->arr as $__Context->key=>$__Context->val){ ?><li>item<?php echo $__Context->key ?> : <?php echo $__Context->val ?></li><?php } ?>'
),
// <!--@while--> ~ <!--@endwhile-->
array(
'<!--@while($item=$list->getItem())--><a href="{$v->link}">{$v->text}</a><!--@endwhile-->',
PHP_EOL.'while($__Context->item=$__Context->list->getItem()){ ?><a href="<?php echo $__Context->v->link ?>"><?php echo $__Context->v->text ?></a><?php } ?>'
),
// <!--@switch--> ~ <!--@case--> ~ <!--@break--> ~ <!--@default --> ~ <!--@endswitch-->
array(
'<dummy /><!--@switch($var)--> <!--@case("A")--> A<!--@break--> <!--@case(\'B\')-->B<!--@break--><!--@default-->C<!--@endswitch--><dummy />',
'?><dummy /><?php switch($__Context->var){;'.PHP_EOL.'case "A": ?> A<?php break;'.PHP_EOL.'case \'B\': ?>B<?php break;'.PHP_EOL.'default : ?>C<?php } ?><dummy />'
),
// invalid block statement
array(
'<dummy /><!--@xe($var)--><dummy />',
'?><dummy /><dummy />'
),
// {@ ...PHP_CODE...}
array(
'<before />{@$list_page = $page_no}<after />',
'?><before /><?php $__Context->list_page = $__Context->page_no ?><after />'
),
// %load_js_plugin
array(
'<dummy /><!--%load_js_plugin("ui")--><dummy />',
'?><dummy /><!--#JSPLUGIN:ui--><?php Context::loadJavascriptPlugin(\'ui\'); ?><dummy />'
),
// #include
array(
'<dummy /><!--#include("sample.html")--><div>This is another dummy</div>',
'?><dummy /><?php $__tpl=TemplateHandler::getInstance();echo $__tpl->compile(\'tests/Unit/classes/template\',\'sample.html\') ?><div>This is another dummy</div>'
),
// <include target="file">
array(
'<dummy /><include target="../sample.html" /><div>This is another dummy</div>',
'?><dummy /><?php $__tpl=TemplateHandler::getInstance();echo $__tpl->compile(\'tests/Unit/classes\',\'sample.html\') ?><div>This is another dummy</div>'
),
// <load target="../../modules/page/lang/lang.xml">
array(
'<dummy /><load target="../../../../modules/page/lang/lang.xml" /><dummy />',
'?><dummy /><?php Context::loadLang(\'modules/page/lang\'); ?><dummy />'
),
// <load target="style.css">
array(
'<dummy /><load target="css/style.css" /><dummy />',
'?><dummy /><!--#Meta:tests/Unit/classes/template/css/style.css--><?php $__tmp=array(\'tests/Unit/classes/template/css/style.css\',\'\',\'\',\'\');Context::loadFile($__tmp);unset($__tmp); ?><dummy />'
),
// <unload target="style.css">
array(
'<dummy /><unload target="css/style.css" /><dummy />',
'?><dummy /><?php Context::unloadFile(\'tests/Unit/classes/template/css/style.css\',\'\',\'\'); ?><dummy />'
),
// <!--%import("../../modules/page/tpl/filter/insert_config.xml")-->
array(
'<dummy /><!--%import("../../../../modules/page/tpl/filter/insert_config.xml")--><dummy />',
'?><dummy /><?php require_once(\'./classes/xml/XmlJsFilter.class.php\');$__xmlFilter=new XmlJsFilter(\'modules/page/tpl/filter\',\'insert_config.xml\');$__xmlFilter->compile(); ?><dummy />'
),
// <!--%import("../script.js",type="body")-->
array(
'<dummy /><!--%import("../script.js",type="body")--><dummy />',
'?><dummy /><!--#Meta:tests/Unit/classes/script.js--><?php $__tmp=array(\'tests/Unit/classes/script.js\',\'body\',\'\',\'\');Context::loadFile($__tmp);unset($__tmp); ?><dummy />'
),
// <!--%unload("../script.js",type="body")-->
array(
'<dummy /><!--%unload("../script.js",type="body")--><dummy />',
'?><dummy /><?php Context::unloadFile(\'tests/Unit/classes/script.js\',\'\'); ?><dummy />'
),
// comment
array(
'<dummy_before /><!--// this is a comment--><dummy_after />',
'?><dummy_before /><dummy_after />'
),
// self-closing tag
array(
'<meta charset="utf-8" cond="$foo">',
PHP_EOL . 'if($__Context->foo){ ?><meta charset="utf-8"><?php } ?>'
),
// relative path1
array(
'<img src="http://naver.com/naver.gif"><input type="image" src="../local.gif" />',
'?><img src="http://naver.com/naver.gif"><input type="image" src="/xe/tests/Unit/classes/local.gif" />'
),
// relative path2
array(
'<img src="http://naver.com/naver.gif"><input type="image" src="../../../dir/local.gif" />',
'?><img src="http://naver.com/naver.gif"><input type="image" src="/xe/tests/dir/local.gif" />'
),
// error case
array(
'<a href="{$layout_info->index_url}" cond="$layout_info->logo_image"><img src="{$layout_info->logo_image}" alt="logo" border="0" /></a>',
PHP_EOL . 'if($__Context->layout_info->logo_image){ ?><a href="<?php echo $__Context->layout_info->index_url ?>"><img src="<?php echo $__Context->layout_info->logo_image ?>" alt="logo" border="0" /></a><?php } ?>'
),
// error case - ignore stylesheets
array(
'<style>body{background-color:black}</style>',
'?><style>body{background-color:black}</style>'
),
// error case - ignore json
array(
'<script>var json = {hello:"world"};</script>',
'?><script>var json = {hello:"world"};</script>'
),
// error case - inline javascript
array(
'<form onsubmit="jQuery(this).find(\'input\').each(function(){if(this.title==this.value)this.value=\'\';}); return procFilter(this, insert_comment)"></form>',
'?><form onsubmit="jQuery(this).find(\'input\').each(function(){if(this.title==this.value)this.value=\'\';}); return procFilter(this, insert_comment)"><input type="hidden" name="error_return_url" value="<?php echo htmlspecialchars(getRequestUriByServerEnviroment(), ENT_COMPAT | ENT_HTML401, \'UTF-8\', false) ?>" /><input type="hidden" name="act" value="<?php echo $__Context->act ?>" /><input type="hidden" name="mid" value="<?php echo $__Context->mid ?>" /><input type="hidden" name="vid" value="<?php echo $__Context->vid ?>" /></form>'
),
// issue 103
array(
'<load target="http://aaa.com/aaa.js" />',
'?><!--#Meta:http://aaa.com/aaa.js--><?php $__tmp=array(\'http://aaa.com/aaa.js\',\'\',\'\',\'\');Context::loadFile($__tmp);unset($__tmp); ?>'
),
// issue 135
array(
'<block loop="$_m_list_all=>$key,$val"><p>{$key}</p><div>Loop block {$val}</div></block>',
PHP_EOL . '$t1b443c9e474abcf95c0ff70798d10a4c=$__Context->_m_list_all;if($t1b443c9e474abcf95c0ff70798d10a4c&&count($t1b443c9e474abcf95c0ff70798d10a4c))foreach($t1b443c9e474abcf95c0ff70798d10a4c as $__Context->key=>$__Context->val){ ?><p><?php echo $__Context->key ?></p><div>Loop block <?php echo $__Context->val ?></div><?php } ?>'
),
// issue 136
array(
'<br cond="$var==\'foo\'" />bar',
PHP_EOL . 'if($__Context->var==\'foo\'){ ?><br /><?php } ?>bar'
),
// issue 188
array(
'<div cond="$ii < $nn" loop="$dummy => $k, $v">Hello, world!</div>',
PHP_EOL . 'if($__Context->ii < $__Context->nn){;' . PHP_EOL . '$te536ab402425aa07f37ece1808639b07=$__Context->dummy;if($te536ab402425aa07f37ece1808639b07&&count($te536ab402425aa07f37ece1808639b07))foreach($te536ab402425aa07f37ece1808639b07 as $__Context->k=>$__Context->v){ ?><div>Hello, world!</div><?php }} ?>'
),
// issue 190
array(
'<div cond="!($i >= $n)" loop="$dummy => $k, $v">Hello, world!</div>',
PHP_EOL . 'if(!($__Context->i >= $__Context->n)){;' . PHP_EOL . '$tcf235db237866cd5f280e0a7fdb2dca8=$__Context->dummy;if($tcf235db237866cd5f280e0a7fdb2dca8&&count($tcf235db237866cd5f280e0a7fdb2dca8))foreach($tcf235db237866cd5f280e0a7fdb2dca8 as $__Context->k=>$__Context->v){ ?><div>Hello, world!</div><?php }} ?>'
),
// issue 183
array(
'<table><thead><tr><th loop="$vvvls => $vvv">{$vvv}</th></tr></thead>'."\n".'<tbody><tr><td>C</td><td>D</td></tr></tbody></table>',
'?><table><thead><tr><?php $t605f930a4a05d6efbed70a9059f688fe=$__Context->vvvls;if($t605f930a4a05d6efbed70a9059f688fe&&count($t605f930a4a05d6efbed70a9059f688fe))foreach($t605f930a4a05d6efbed70a9059f688fe as $__Context->vvv){ ?><th><?php echo $__Context->vvv ?></th><?php } ?></tr></thead>'."\n".'<tbody><tr><td>C</td><td>D</td></tr></tbody></table>'
),
// issue 512 - ignores <marquee>
array(
'<div class="topimgContex"><marquee direction="up" scrollamount="1" height="130" loop="infinity" behavior="lscro">{$lang->sl_show_topimgtext}</marquee></div>',
'?><div class="topimgContex"><marquee direction="up" scrollamount="1" height="130" loop="infinity" behavior="lscro"><?php echo $__Context->lang->sl_show_topimgtext ?></marquee></div>'
),
// issue 584
array(
'<img cond="$oBodex->display_extra_images[\'mobile\'] && $arr_extra && $arr_extra->bodex->mobile" src="./images/common/mobile.gif" title="mobile" alt="mobile" />',
PHP_EOL . 'if($__Context->oBodex->display_extra_images[\'mobile\'] && $__Context->arr_extra && $__Context->arr_extra->bodex->mobile){ ?><img src="/xe/tests/Unit/classes/template/images/common/mobile.gif" title="mobile" alt="mobile" /><?php } ?>'
),
// issue 831
array(
"<li <!--@if(in_array(\$act, array(\n'dispNmsAdminGroupList',\n'dispNmsAdminInsertGroup',\n'dispNmsAdminGroupInfo',\n'dispNmsAdminDeleteGroup')))-->class=\"on\"<!--@endif-->>",
"?><li <?php if(in_array(\$__Context->act, array(\n'dispNmsAdminGroupList',\n'dispNmsAdminInsertGroup',\n'dispNmsAdminGroupInfo',\n'dispNmsAdminDeleteGroup'))){ ?>class=\"on\"<?php } ?>>"
),
// issue 746
array(
'<img src="../myxe/xe/img.png" />',
'?><img src="/xe/tests/Unit/classes/myxe/xe/img.png" />'
),
// issue 696
array(
'{@ eval(\'$val = $document_srl;\')}',
PHP_EOL . 'eval(\'$__Context->val = $__Context->document_srl;\') ?>'
),
// https://github.com/xpressengine/xe-core/issues/1510
array(
'<img cond="$foo->bar" src="../common/mobile.gif" />',
PHP_EOL . 'if($__Context->foo->bar){ ?><img src="/xe/tests/Unit/classes/common/mobile.gif" /><?php } ?>'
),
// https://github.com/xpressengine/xe-core/issues/1510
array(
'<img cond="$foo->bar > 100" alt="a!@#$%^&*()_-=[]{}?/" src="../common/mobile.gif" />',
PHP_EOL . 'if($__Context->foo->bar > 100){ ?><img alt="a!@#$%^&*()_-=[]{}?/" src="/xe/tests/Unit/classes/common/mobile.gif" /><?php } ?>'
),
// https://github.com/xpressengine/xe-core/issues/1510
array(
'<img src="../common/mobile.gif" cond="$foo->bar" />',
PHP_EOL . 'if($__Context->foo->bar){ ?><img src="/xe/tests/Unit/classes/common/mobile.gif" /><?php } ?>'
),
// https://github.com/xpressengine/xe-core/issues/1510
array(
'<img class="tmp_class" cond="!$module_info->title" src="../img/common/blank.gif" />',
PHP_EOL . 'if(!$__Context->module_info->title){ ?><img class="tmp_class" src="/xe/tests/Unit/classes/img/common/blank.gif" /><?php } ?>'
),
// https://github.com/xpressengine/xe-core/issues/1510
array(
'<img cond="$mi->title" class="tmp_class"|cond="$mi->use" src="../img/common/blank.gif" />',
PHP_EOL . 'if($__Context->mi->title){ ?><img<?php if($__Context->mi->use){ ?> class="tmp_class"<?php } ?> src="/xe/tests/Unit/classes/img/common/blank.gif" /><?php } ?>'
),
array(
'<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />',
'?><input foo="bar" /> <?php if($__Context->foo->bar){ ?><img alt="alt" src="/xe/tests/Unit/classes/common/mobile.gif" /><?php } ?>'
),
array(
'<input foo="bar" />' . "\n" . '<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />',
'?><input foo="bar" />' . PHP_EOL . '<input foo="bar" /> <?php if($__Context->foo->bar){ ?><img alt="alt" src="/xe/tests/Unit/classes/common/mobile.gif" /><?php } ?>'
),
array(
'asf <img src="{$foo->bar}" />',
'?>asf <img src="<?php echo $__Context->foo->bar ?>" />'
),
array(
'<img alt="" '.PHP_EOL.' src="../myxe/xe/img.png" />',
'?><img alt="" '.PHP_EOL.' src="/xe/tests/Unit/classes/myxe/xe/img.png" />'
),
array(
'<input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="../myxe/xe/img.png" /> <input>asdf src="../img/img.gif" asdf</input>',
'?><input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="/xe/tests/Unit/classes/myxe/xe/img.png" /> <input>asdf src="../img/img.gif" asdf</input>'
),
array(
'<input>asdf src="../img/img.gif" asdf</input>',
'?><input>asdf src="../img/img.gif" asdf</input>'
),
);
}
/**
* @dataProvider provider
*/
public function testParse($tpl, $expected)
{
$tmpl = new TemplateHandlerWrapper;
$tmpl->init(dirname(__FILE__), 'no_file.html');
$result = $tmpl->parse($tpl);
$this->assertEquals($result, $this->prefix.$expected);
}
public function testParse2()
{
$tmpl = new TemplateHandlerWrapper;
$tmpl->init(dirname(__FILE__), 'no_file.html');
$result = $tmpl->parse($tpl);
$this->assertEquals($result, '');
}
public function testCompileDirect()
{
$tmpl = TemplateHandler::getInstance();
$result = $tmpl->compileDirect(dirname(__FILE__), 'sample.html');
$result = trim($result);
$this->assertEquals($result, $this->prefix.PHP_EOL.'if($__Context->has_blog){ ?><a href="http://mygony.com">Taggon\'s blog</a><?php } ?>'.PHP_EOL.'<!--#Meta://external.host/js.js--><?php $__tmp=array(\'//external.host/js.js\',\'\',\'\',\'\');Context::loadFile($__tmp);unset($__tmp); ?>');
}
}
class TemplateHandlerWrapper extends \TemplateHandler {
private $inst;
function __construct() {
$this->inst = parent::getInstance();
}
public function init($tpl_path, $tpl_filename, $tpl_file = '') {
call_user_func(array($this->inst, 'init'), $tpl_path, $tpl_filename, $tpl_file);
}
public function parse($buff = null) {
return call_user_func(array($this->inst, 'parse'), $buff);
}
}

View file

@ -0,0 +1 @@
/* only for test */

View file

@ -0,0 +1,2 @@
<a href="http://mygony.com" cond="$has_blog">Taggon's blog</a>
<load target="//external.host/js.js" />

View file

@ -0,0 +1,208 @@
<?php
require_once _XE_PATH_.'classes/context/Context.class.php';
require_once _XE_PATH_.'classes/xml/XmlParser.class.php';
require_once _XE_PATH_.'classes/handler/Handler.class.php';
require_once _XE_PATH_.'classes/file/FileHandler.class.php';
require_once _XE_PATH_.'classes/validator/Validator.class.php';
class ValidatorTest extends \Codeception\TestCase\Test
{
public function _before()
{
global $lang;
if(!$lang) $lang = new stdClass();
$lang->filter = new stdClass();
$lang->filter->isnull = 'isnull';
$lang->filter->outofrange = 'outofrange';
$lang->filter->equalto = 'equalto';
}
public function testRequired()
{
$vd = new Validator();
$vd->addFilter('userid', array('required'=>'true'));
// given data
$this->assertFalse( $vd->validate(array('no-userid'=>'hello')) );
$this->assertTrue( $vd->validate(array('userid'=>'myuserid')) );
$this->assertFalse( $vd->validate(array('userid'=>'')) );
// context data
$this->assertFalse( $vd->validate() );
Context::set('userid', '', true);
$this->assertFalse( $vd->validate() );
Context::set('userid', 'myuserid', true);
$this->assertTrue( $vd->validate() );
$vd->removeFilter('userid');
$this->assertTrue( $vd->validate() );
}
public function testNamePattern()
{
$vd = new Validator();
$vd->addFilter('^user_', array('length'=>'5:'));
Context::set('user_123', 'abcd', true);
Context::set('user_456', '123', true);
$this->assertFalse( $vd->validate() );
Context::set('user_123', 'abcdefg', true);
$this->assertFalse( $vd->validate() );
Context::set('user_456', '123456', true);
$this->assertTrue( $vd->validate() );
}
public function testEqualTo()
{
$vd = new Validator();
$vd->addFilter('pass1', array('equalto'=>'pass2'));
Context::set('pass1', 'MyPassword', true);
$this->assertFalse( $vd->validate() );
Context::set('pass2', 'WorngPassword', true);
$this->assertFalse( $vd->validate() );
Context::set('pass2', 'MyPassword', true);
$this->assertTrue( $vd->validate() );
}
public function testArrayTrim()
{
$vd = new Validator();
$arr = array('red'=>'apple', 'yellow'=>'banana ', 'green'=>' papaya ');
$this->assertEquals($vd->arrayTrim($arr), array('red'=>'apple', 'yellow'=>'banana', 'green'=>'papaya'));
$this->assertEquals($vd->arrayTrim(' string '), 'string');
}
public function testLength()
{
$vd = new Validator();
$vd->addFilter('field1', array('length'=>'3:'));
$this->assertFalse( $vd->validate(array('field1'=>'ab')) );
$this->assertTrue( $vd->validate(array('field1'=>'abc')) );
$this->assertTrue( $vd->validate(array('field1'=>'abcd')) );
}
public function testCustomRule()
{
// regex
$vd = new Validator();
$customRules['regex_rule']['type'] = 'regex';
$customRules['regex_rule']['test'] = '/^[a-z]+$/';
$vd->addRule($customRules);
$vd->addFilter('regex_field', array('rule' => 'regex_rule'));
$this->assertTrue($vd->validate(array('regex_field' => 'abc')));
$this->assertFalse($vd->validate(array('regex_field' => 'ABC')));
// enum
$vd = new Validator();
$customRules['enum_rule']['type'] = 'enum';
$customRules['enum_rule']['test'] = 'a,b,c';
$vd->addRule($customRules);
$vd->addFilter('enum_field', array('rule' => 'enum_rule'));
$this->assertTrue($vd->validate(array('enum_field' => 'a')));
$this->assertFalse($vd->validate(array('enum_field' => 'd')));
// enum with custom delimiter
$vd = new Validator();
$customRules['enum_rule2']['type'] = 'enum';
$customRules['enum_rule2']['test'] = 'a@b@c';
$customRules['enum_rule2']['delim'] = '@';
$vd->addRule($customRules);
$vd->addFilter('enum_field2', array('rule' => 'enum_rule2'));
$this->assertTrue($vd->validate(array('enum_field2' => 'a')));
$this->assertFalse($vd->validate(array('enum_field2' => 'd')));
// expr
$vd = new Validator();
$customRules['expr_rule']['type'] = 'expr';
$customRules['expr_rule']['test'] = '$$ &lt; 10';
$vd->addRule($customRules);
$vd->addFilter('expr_field', array('rule' => 'expr_rule'));
$this->assertTrue($vd->validate(array('expr_field' => '5')));
$this->assertFalse($vd->validate(array('expr_field' => '15')));
}
public function testCustomRuleXml()
{
$vd = new Validator(dirname(__FILE__).'/customrule.xml');
$this->assertTrue($vd->validate(array('regex_field' => 'abc')));
$this->assertFalse($vd->validate(array('regex_field' => 'ABC')));
$this->assertTrue($vd->validate(array('enum_field' => 'a')));
$this->assertFalse($vd->validate(array('enum_field' => 'd')));
$this->assertTrue($vd->validate(array('enum_field2' => 'a')));
$this->assertFalse($vd->validate(array('enum_field2' => 'd')));
$this->assertTrue($vd->validate(array('expr_field' => '5')));
$this->assertFalse($vd->validate(array('expr_field' => '15')));
}
public function testCondition()
{
$vd = new Validator();
$data = array('greeting1'=>'hello');
// No condition
$vd->addFilter('greeting1', array('required'=>'true'));
$this->assertTrue($vd->validate($data));
// Now greeting2 being mandatory if greeting1 is 'Hello'
$vd->addFilter('greeting2', array('if'=>array('test'=>'$greeting1 == "Hello"', 'attr'=>'required', 'value'=>'true')));
// Because greeting1 is 'hello', including lowercase 'h', greeting2 isn't required yet.
$this->assertTrue($vd->validate($data));
// Change the value of greeting1. Greeting2 is required now
$data['greeting1'] = 'Hello';
$this->assertFalse($vd->validate($data));
$data['greeting2'] = 'World';
$this->assertTrue($vd->validate($data));
}
public function testConditionXml()
{
$vd = new Validator(dirname(__FILE__).'/condition.xml');
$data = array('greeting1'=>'hello');
$this->assertTrue($vd->validate($data));
// Change the value of greeting1. Greeting2 is required now
$data['greeting1'] = 'Hello';
$this->assertFalse($vd->validate($data));
$data['greeting2'] = 'World';
$this->assertTrue($vd->validate($data));
// javascript
$vd->setCacheDir(dirname(__FILE__));
$js = $vd->getJsPath();
$this->assertFileEquals($js, dirname(__FILE__).'/condition.en.js');
}
protected function tearDown()
{
// remove cache directory
$cache_dir = dirname(__FILE__).'/ruleset';
if(is_dir($cache_dir))
{
$files = (array)glob($cache_dir.'/*');
foreach($files as $file)
{
unlink($file);
}
rmdir($cache_dir);
}
}
}

View file

@ -0,0 +1,8 @@
(function($,v){
v=xe.getApp('validator')[0];if(!v)return;
v.cast('ADD_FILTER',['condition', {'greeting1':{required:true},'greeting2':{'if':[{test:'$greeting1 == \'Hello\'', attr:'required', value:'true'}]}}]);
v.cast('ADD_MESSAGE',['isnull','isnull']);
v.cast('ADD_MESSAGE',['outofrange','outofrange']);
v.cast('ADD_MESSAGE',['equalto','equalto']);
})(jQuery);

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<ruleset version="1.5.0">
<fields>
<field name="greeting1" required="true" />
<field name="greeting2">
<if test="$greeting1 == 'Hello'" attr="required" value="true" />
</field>
</fields>
</ruleset>

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<ruleset version="1.5.0">
<customrules>
<rule name="regex_rule" type="regex" test="/^[a-z]+$/" />
<rule name="enum_rule" type="enum" test="a,b,c" />
<rule name="enum_rule2" type="enum" test="a@b@c" delim="@" />
<rule name="expr_rule" type="expr" test="$$ &lt; 10" />
</customrules>
<fields>
<field name="regex_field" rule="regex_rule" />
<field name="enum_field" rule="enum_rule" />
<field name="enum_field2" rule="enum_rule2" />
<field name="expr_field" rule="expr_rule" />
</fields>
</ruleset>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<ruleset version="1.5.0">
<customrules>
</customrules>
<fields>
<field name="nick_name" length="1:20"/>
<field name="password" length="1:20"/>
<field name="email_address" rule="email" length=":250" />
<field name="homepage" rule="url" length=":250" />
<field name="title" required="true" length="1:250"/>
<field name="content" required="true"/>
<field name="category_srl" required="true" rule="number" default="0" />
</fields>
</ruleset>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<ruleset>
<fields>
<field name="userid" required="true" length="5:10" rule="userid" default="idididid" />
<field name="password" required="true" />
</fields>
</ruleset>