mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-22 05:15:29 +09:00
Reorganize unit tests
This commit is contained in:
parent
be83a34b1e
commit
670baecb73
26 changed files with 48 additions and 55 deletions
3
tests/unit/_bootstrap.php
Normal file
3
tests/unit/_bootstrap.php
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
// Here you can initialize variables that will be available to your tests
|
||||
$_SERVER['SCRIPT_NAME'] = '/xe/index.php';
|
||||
91
tests/unit/classes/ContextTest.php
Normal file
91
tests/unit/classes/ContextTest.php
Normal 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');
|
||||
}
|
||||
}
|
||||
44
tests/unit/classes/FileHandlerTest.php
Normal file
44
tests/unit/classes/FileHandlerTest.php
Normal 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));
|
||||
}
|
||||
}
|
||||
184
tests/unit/classes/FrontEndFileHandlerTest.php
Normal file
184
tests/unit/classes/FrontEndFileHandlerTest.php
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
<?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$/';
|
||||
FrontEndFileHandler::$minify = 'none';
|
||||
|
||||
$this->specify("js(head)", function() {
|
||||
$handler = new FrontEndFileHandler();
|
||||
$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'));
|
||||
$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() {
|
||||
$handler = new FrontEndFileHandler();
|
||||
$handler->loadFile(array('./common/js/xml_js_filter.js', 'head'));
|
||||
$expected = array();
|
||||
$this->assertEquals($handler->getJsFileList('body'), $expected);
|
||||
});
|
||||
|
||||
$this->specify("css", function() {
|
||||
$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() {
|
||||
$handler = new FrontEndFileHandler();
|
||||
$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/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));
|
||||
$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() {
|
||||
$handler = new FrontEndFileHandler();
|
||||
$handler->loadFile(array('./common/js/xml_handler.js', 'head', '', 1));
|
||||
$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));
|
||||
$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() {
|
||||
$handler = new FrontEndFileHandler();
|
||||
$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/js_app.js', '', 'all');
|
||||
$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() {
|
||||
$handler = new FrontEndFileHandler();
|
||||
$handler->loadFile(array('./common/js/js_app.js', 'head', 'ie6'));
|
||||
$handler->loadFile(array('./common/js/js_app.js', 'head', 'ie7'));
|
||||
$handler->loadFile(array('./common/js/js_app.js', 'head', 'ie8'));
|
||||
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => 'ie6');
|
||||
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => 'ie7');
|
||||
$expected[] = array('file' => '/xe/common/js/js_app.js' . $this->_filemtime('common/js/js_app.js'), 'targetie' => 'ie8');
|
||||
$this->assertEquals($handler->getJsFileList(), $expected);
|
||||
});
|
||||
|
||||
$this->specify("external file - schemaless", function() {
|
||||
$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() {
|
||||
$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() {
|
||||
$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() {
|
||||
$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);
|
||||
});
|
||||
|
||||
FrontEndFileHandler::$minify = 'all';
|
||||
|
||||
$this->specify("minify", function() {
|
||||
$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() {
|
||||
$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() {
|
||||
$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);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
122
tests/unit/classes/SecurityTest.php
Normal file
122
tests/unit/classes/SecurityTest.php
Normal 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('<strong>Hello, world</strong>', 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('<strong>Hello, world</strong>', Context::get('content1'));
|
||||
$this->assertEquals('Wow, >_< !', 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('<span class="first">F</span>irst','<u>S</u>econd','<b>T</b>hird'));
|
||||
$security->encodeHTML('array1.2'); // affects only third element
|
||||
$this->assertEquals(Context::get('array1'), array('<span class="first">F</span>irst','<u>S</u>econd','<b>T</b>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('<span class="first">F</span>irst','<u>S</u>econd','<b>T</b>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 <del>2</del>','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 <ins>1</ins>','elem2'=>'Two <del>2</del>','elem3'=>'Three <addr>3</addr>'));
|
||||
|
||||
// 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 = '<strong>Strong</strong> 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, "Very nice!"';
|
||||
$obj->prop3 = '<strong>Strong</strong> 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', '<b>Bold</b> 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'=>'<b>Bold</b> is not bald'));
|
||||
}
|
||||
}
|
||||
327
tests/unit/classes/TemplateHandlerTest.php
Normal file
327
tests/unit/classes/TemplateHandlerTest.php
Normal file
|
|
@ -0,0 +1,327 @@
|
|||
<?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;';
|
||||
|
||||
public function testParse()
|
||||
{
|
||||
$tests = 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>'
|
||||
),
|
||||
);
|
||||
|
||||
foreach ($tests as $test)
|
||||
{
|
||||
$tmpl = new TemplateHandlerWrapper;
|
||||
$tmpl->init(__DIR__ . '/template', 'no_file.html');
|
||||
$result = $tmpl->parse($test[0]);
|
||||
$this->assertEquals($this->prefix . $test[1], $result);
|
||||
}
|
||||
}
|
||||
|
||||
public function testParseNoContent()
|
||||
{
|
||||
$tmpl = new TemplateHandlerWrapper;
|
||||
$tmpl->init(__DIR__ . '/template', 'no_file.html');
|
||||
$result = $tmpl->parse($tpl);
|
||||
|
||||
$this->assertEquals('', $result);
|
||||
}
|
||||
|
||||
public function testCompileDirect()
|
||||
{
|
||||
$tmpl = TemplateHandler::getInstance();
|
||||
$result = $tmpl->compileDirect(__DIR__ . '/template', '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);
|
||||
}
|
||||
}
|
||||
208
tests/unit/classes/ValidatorTest.php
Normal file
208
tests/unit/classes/ValidatorTest.php
Normal 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'] = '$$ < 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(__DIR__ . '/validator/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(__DIR__ . '/validator/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(__DIR__ . '/validator');
|
||||
$js = $vd->getJsPath();
|
||||
$this->assertFileEquals($js, __DIR__ . '/validator/condition.en.js');
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
// remove cache directory
|
||||
$cache_dir = __DIR__ . '/validator/ruleset';
|
||||
if(is_dir($cache_dir))
|
||||
{
|
||||
$files = (array)glob($cache_dir.'/*');
|
||||
foreach($files as $file)
|
||||
{
|
||||
unlink($file);
|
||||
}
|
||||
rmdir($cache_dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
1
tests/unit/classes/template/css/style.css
Normal file
1
tests/unit/classes/template/css/style.css
Normal file
|
|
@ -0,0 +1 @@
|
|||
/* only for test */
|
||||
2
tests/unit/classes/template/sample.html
Normal file
2
tests/unit/classes/template/sample.html
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<a href="http://mygony.com" cond="$has_blog">Taggon's blog</a>
|
||||
<load target="//external.host/js.js" />
|
||||
8
tests/unit/classes/validator/condition.en.js
Normal file
8
tests/unit/classes/validator/condition.en.js
Normal 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);
|
||||
9
tests/unit/classes/validator/condition.xml
Normal file
9
tests/unit/classes/validator/condition.xml
Normal 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>
|
||||
15
tests/unit/classes/validator/customrule.xml
Normal file
15
tests/unit/classes/validator/customrule.xml
Normal 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="$$ < 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>
|
||||
14
tests/unit/classes/validator/insertDocument.xml
Normal file
14
tests/unit/classes/validator/insertDocument.xml
Normal 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>
|
||||
7
tests/unit/classes/validator/login.xml
Normal file
7
tests/unit/classes/validator/login.xml
Normal 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>
|
||||
26
tests/unit/framework/ConfigTest.php
Normal file
26
tests/unit/framework/ConfigTest.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
class ConfigTest extends \Codeception\TestCase\Test
|
||||
{
|
||||
public function testConfig()
|
||||
{
|
||||
if (!file_exists(RX_BASEDIR . 'files/config/config.php'))
|
||||
{
|
||||
mkdir(RX_BASEDIR . 'files/config', 0755, true);
|
||||
copy(RX_BASEDIR . 'common/defaults/config.php', RX_BASEDIR . 'files/config/config.php');
|
||||
}
|
||||
|
||||
Rhymix\Framework\Config::init();
|
||||
$this->assertTrue(version_compare(Rhymix\Framework\Config::get('config_version'), '2.0', '>='));
|
||||
$this->assertTrue(is_array(Rhymix\Framework\Config::get('db.master')));
|
||||
$this->assertNotEmpty(Rhymix\Framework\Config::get('db.master.host'));
|
||||
|
||||
Rhymix\Framework\Config::set('foo.bar', $rand = mt_rand());
|
||||
$this->assertEquals(array('bar' => $rand), Rhymix\Framework\Config::get('foo'));
|
||||
$this->assertEquals($rand, Rhymix\Framework\Config::get('foo.bar'));
|
||||
|
||||
$var = array('foo' => 'bar');
|
||||
$serialized = "array(\n\t'foo' => 'bar',\n)";
|
||||
$this->assertEquals($serialized, Rhymix\Framework\Config::serialize($var));
|
||||
}
|
||||
}
|
||||
170
tests/unit/framework/DateTimeTest.php
Normal file
170
tests/unit/framework/DateTimeTest.php
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
<?php
|
||||
|
||||
class DateTimeTest extends \Codeception\TestCase\Test
|
||||
{
|
||||
public function _before()
|
||||
{
|
||||
// Add some dummy data to system configuration. Asia/Seoul offset is 32400.
|
||||
Rhymix\Framework\Config::set('locale.default_timezone', 'Asia/Seoul');
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 10800);
|
||||
|
||||
// Set PHP time zone to the internal time zone.
|
||||
$old_timezone = @date_default_timezone_get();
|
||||
date_default_timezone_set('Etc/GMT-3');
|
||||
}
|
||||
|
||||
public function _after()
|
||||
{
|
||||
// Restore the old timezone.
|
||||
date_default_timezone_set($old_timezone);
|
||||
}
|
||||
|
||||
public function testZgap()
|
||||
{
|
||||
// Test zgap() when the current user's time zone is different from the system default.
|
||||
$_SESSION['timezone'] = 'Etc/UTC';
|
||||
$this->assertEquals(-10800, zgap());
|
||||
|
||||
// Test zgap() when the current user's time zone is the same as the system default.
|
||||
unset($_SESSION['timezone']);
|
||||
$this->assertEquals(21600, zgap());
|
||||
}
|
||||
|
||||
public function testZtime()
|
||||
{
|
||||
$timestamp = 1454000000;
|
||||
|
||||
// Test ztime() when the internal time zone is different from the default time zone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 10800);
|
||||
$this->assertEquals($timestamp, ztime('20160128195320'));
|
||||
|
||||
// Test ztime() when the internal time zone is the same as the default time zone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 32400);
|
||||
$this->assertEquals($timestamp, ztime('20160129015320'));
|
||||
|
||||
// Restore the internal timezone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 10800);
|
||||
}
|
||||
|
||||
public function testZdate()
|
||||
{
|
||||
$expected = '2016-01-29 01:53:20';
|
||||
|
||||
// Test zdate() when the internal time zone is different from the default time zone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 10800);
|
||||
$this->assertEquals($expected, zdate('20160128195320'));
|
||||
|
||||
// Test zdate() when the internal time zone is the same as the default time zone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 32400);
|
||||
$this->assertEquals($expected, zdate('20160129015320'));
|
||||
|
||||
// Restore the internal timezone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 10800);
|
||||
}
|
||||
|
||||
public function testGetInternalDateTime()
|
||||
{
|
||||
$timestamp = 1454000000;
|
||||
|
||||
// Test zdate() when the internal time zone is different from the default time zone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 10800);
|
||||
$this->assertEquals('20160128195320', getInternalDateTime($timestamp));
|
||||
|
||||
// Test zdate() when the internal time zone is the same as the default time zone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 32400);
|
||||
$this->assertEquals('20160129015320', getInternalDateTime($timestamp));
|
||||
}
|
||||
|
||||
public function testGetTimeGap()
|
||||
{
|
||||
Context::getInstance()->lang = Rhymix\Framework\Lang::getInstance('en');
|
||||
Context::getInstance()->lang->loadPlugin('common');
|
||||
|
||||
// Test getTimeGap() when the internal time zone is different from the default time zone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 10800);
|
||||
$this->assertEquals('30 minutes ago', getTimeGap(getInternalDateTime(RX_TIME - 1800)));
|
||||
$this->assertEquals('2 hours ago', getTimeGap(getInternalDateTime(RX_TIME - 6000)));
|
||||
|
||||
// Test getTimeGap() when the internal time zone is the same as the default time zone.
|
||||
Rhymix\Framework\Config::set('locale.internal_timezone', 32400);
|
||||
$this->assertEquals('30 minutes ago', getTimeGap(getInternalDateTime(RX_TIME - 1800)));
|
||||
$this->assertEquals('2 hours ago', getTimeGap(getInternalDateTime(RX_TIME - 6000)));
|
||||
}
|
||||
|
||||
public function testGetTimezoneForCurrentUser()
|
||||
{
|
||||
// Test when the current user's time zone is different from the system default.
|
||||
$_SESSION['timezone'] = 'Pacific/Auckland';
|
||||
$this->assertEquals('Pacific/Auckland', Rhymix\Framework\DateTime::getTimezoneForCurrentUser());
|
||||
|
||||
// Test when the current user's time zone is the same as the system default.
|
||||
unset($_SESSION['timezone']);
|
||||
$this->assertEquals('Asia/Seoul', Rhymix\Framework\DateTime::getTimezoneForCurrentUser());
|
||||
}
|
||||
|
||||
public function testFormatTimestampForCurrentUser()
|
||||
{
|
||||
$timestamp_winter = 1454000000;
|
||||
$timestamp_summer = $timestamp_winter - (86400 * 184);
|
||||
|
||||
// Test when the current user's time zone is in the Northern hemisphere with DST.
|
||||
$_SESSION['timezone'] = 'America/Chicago';
|
||||
$this->assertEquals('20160128 105320', Rhymix\Framework\DateTime::formatTimestampForCurrentUser('Ymd His', $timestamp_winter));
|
||||
$this->assertEquals('20150728 115320', Rhymix\Framework\DateTime::formatTimestampForCurrentUser('Ymd His', $timestamp_summer));
|
||||
$this->assertEquals('20150728 115320', getDisplayDateTime($timestamp_summer, 'Ymd His'));
|
||||
|
||||
// Test when the current user's time zone is in the Southern hemisphere with DST.
|
||||
$_SESSION['timezone'] = 'Pacific/Auckland';
|
||||
$this->assertEquals('20160129 055320', Rhymix\Framework\DateTime::formatTimestampForCurrentUser('Ymd His', $timestamp_winter));
|
||||
$this->assertEquals('20150729 045320', Rhymix\Framework\DateTime::formatTimestampForCurrentUser('Ymd His', $timestamp_summer));
|
||||
$this->assertEquals('20150729 045320', getDisplayDateTime($timestamp_summer, 'Ymd His'));
|
||||
|
||||
// Test when the current user's time zone is the same as the system default without DST.
|
||||
unset($_SESSION['timezone']);
|
||||
$this->assertEquals('20160129 015320', Rhymix\Framework\DateTime::formatTimestampForCurrentUser('Ymd His', $timestamp_winter));
|
||||
$this->assertEquals('20150729 015320', Rhymix\Framework\DateTime::formatTimestampForCurrentUser('Ymd His', $timestamp_summer));
|
||||
$this->assertEquals('20150729 015320', getDisplayDateTime($timestamp_summer, 'Ymd His'));
|
||||
}
|
||||
|
||||
public function testGetTimezoneList()
|
||||
{
|
||||
$tzlist = Rhymix\Framework\DateTime::getTimezoneList();
|
||||
$this->assertTrue(array_key_exists('Etc/UTC', $tzlist));
|
||||
$this->assertEquals('Asia/Seoul (+09:00)', $tzlist['Asia/Seoul']);
|
||||
}
|
||||
|
||||
public function testGetTimezoneOffset()
|
||||
{
|
||||
$this->assertEquals(32400, Rhymix\Framework\DateTime::getTimezoneOffset('Asia/Seoul'));
|
||||
$this->assertEquals(39600, Rhymix\Framework\DateTime::getTimezoneOffset('Australia/Sydney', strtotime('2016-01-01')));
|
||||
$this->assertEquals(36000, Rhymix\Framework\DateTime::getTimezoneOffset('Australia/Sydney', strtotime('2015-07-01')));
|
||||
$this->assertEquals(-18000, Rhymix\Framework\DateTime::getTimezoneOffset('America/New_York', strtotime('2016-01-01')));
|
||||
$this->assertEquals(-14400, Rhymix\Framework\DateTime::getTimezoneOffset('America/New_York', strtotime('2015-07-01')));
|
||||
}
|
||||
|
||||
public function testGetTimezoneOffsetFromInternal()
|
||||
{
|
||||
$this->assertEquals(21600, Rhymix\Framework\DateTime::getTimezoneOffsetFromInternal('Asia/Seoul'));
|
||||
$this->assertEquals(28800, Rhymix\Framework\DateTime::getTimezoneOffsetFromInternal('Australia/Sydney', strtotime('2016-01-01')));
|
||||
$this->assertEquals(25200, Rhymix\Framework\DateTime::getTimezoneOffsetFromInternal('Australia/Sydney', strtotime('2015-07-01')));
|
||||
$this->assertEquals(-28800, Rhymix\Framework\DateTime::getTimezoneOffsetFromInternal('America/New_York', strtotime('2016-01-01')));
|
||||
$this->assertEquals(-25200, Rhymix\Framework\DateTime::getTimezoneOffsetFromInternal('America/New_York', strtotime('2015-07-01')));
|
||||
}
|
||||
|
||||
public function testGetTimezoneOffsetByLegacyFormat()
|
||||
{
|
||||
$this->assertEquals(32400, Rhymix\Framework\DateTime::getTimezoneOffsetByLegacyFormat('+0900'));
|
||||
$this->assertEquals(-25200, Rhymix\Framework\DateTime::getTimezoneOffsetByLegacyFormat('-0700'));
|
||||
$this->assertEquals(19800, Rhymix\Framework\DateTime::getTimezoneOffsetByLegacyFormat('+0530'));
|
||||
$this->assertEquals(-38700, Rhymix\Framework\DateTime::getTimezoneOffsetByLegacyFormat('-1045'));
|
||||
}
|
||||
|
||||
public function testGetTimezoneNameByOffset()
|
||||
{
|
||||
$this->assertEquals('Etc/GMT-9', Rhymix\Framework\DateTime::getTimezoneNameByOffset(32400));
|
||||
$this->assertEquals('Etc/GMT+5', Rhymix\Framework\DateTime::getTimezoneNameByOffset(-18000));
|
||||
$this->assertEquals('Etc/UTC', Rhymix\Framework\DateTime::getTimezoneNameByOffset(0));
|
||||
$this->assertEquals('Asia/Kolkata', Rhymix\Framework\DateTime::getTimezoneNameByOffset(19800));
|
||||
$this->assertEquals('Australia/Eucla', Rhymix\Framework\DateTime::getTimezoneNameByOffset(31500));
|
||||
}
|
||||
}
|
||||
65
tests/unit/framework/DebugTest.php
Normal file
65
tests/unit/framework/DebugTest.php
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
class DebugTest extends \Codeception\TestCase\Test
|
||||
{
|
||||
public function testDebugEntry()
|
||||
{
|
||||
$file = __FILE__;
|
||||
$line = __LINE__ + 2;
|
||||
Rhymix\Framework\Debug::$write_to_error_log = false;
|
||||
Rhymix\Framework\Debug::addEntry('foobar entry');
|
||||
$entries = Rhymix\Framework\Debug::getEntries();
|
||||
$this->assertEquals(1, count($entries));
|
||||
$this->assertEquals('foobar entry', $entries[0]->message);
|
||||
$this->assertEquals($file, $entries[0]->file);
|
||||
$this->assertEquals($line, $entries[0]->line);
|
||||
}
|
||||
|
||||
public function testDebugError()
|
||||
{
|
||||
$file = __FILE__;
|
||||
$line = __LINE__ + 2;
|
||||
Rhymix\Framework\Debug::$write_to_error_log = false;
|
||||
Rhymix\Framework\Debug::addError(~0, 'Rhymix', $file, $line, null);
|
||||
$errors = Rhymix\Framework\Debug::getErrors();
|
||||
$this->assertEquals(1, count($errors));
|
||||
$this->assertContains('Rhymix', $errors[0]->message);
|
||||
$this->assertEquals($file, $errors[0]->file);
|
||||
$this->assertEquals($line, $errors[0]->line);
|
||||
}
|
||||
|
||||
public function testDebugQuery()
|
||||
{
|
||||
Rhymix\Framework\Debug::addQuery(array(
|
||||
'result' => 'fail',
|
||||
'errno' => 1234,
|
||||
'errstr' => 'This is a unit test',
|
||||
'connection' => 'foobar',
|
||||
'query_id' => 'rhymix.unitTest',
|
||||
'query' => 'SELECT foo FROM bar',
|
||||
'elapsed_time' => 0.1234,
|
||||
'called_file' => __FILE__,
|
||||
'called_line' => __LINE__,
|
||||
'called_method' => 'rhymix.unitTest',
|
||||
'backtrace' => array(),
|
||||
));
|
||||
$queries = Rhymix\Framework\Debug::getQueries();
|
||||
$this->assertEquals(1, count($queries));
|
||||
$this->assertEquals('SELECT foo FROM bar', $queries[0]->query_string);
|
||||
$this->assertEquals('This is a unit test', $queries[0]->message);
|
||||
$this->assertEquals(1234, $queries[0]->error_code);
|
||||
}
|
||||
|
||||
public function testDebugTranslateFilename()
|
||||
{
|
||||
$original_filename = __FILE__;
|
||||
$trans_filename = substr($original_filename, strlen(\RX_BASEDIR));
|
||||
$this->assertEquals($trans_filename, Rhymix\Framework\Debug::translateFilename($original_filename));
|
||||
|
||||
$original_filename = __FILE__;
|
||||
$alias_filename = $original_filename . '.foobar';
|
||||
$trans_filename = substr($alias_filename, strlen(\RX_BASEDIR));
|
||||
Rhymix\Framework\Debug::addFilenameAlias($alias_filename, $original_filename);
|
||||
$this->assertEquals($trans_filename, Rhymix\Framework\Debug::translateFilename($original_filename));
|
||||
}
|
||||
}
|
||||
77
tests/unit/framework/IpFilterTest.php
Normal file
77
tests/unit/framework/IpFilterTest.php
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
class IpFilterTest extends \Codeception\TestCase\Test
|
||||
{
|
||||
public function testIPv4CIDR()
|
||||
{
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::inRange('10.0.127.191', '10.0.127.191'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::inRange('10.1.131.177', '10.1.131.178'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::inRange('127.0.0.1', '127.0.0.0/8'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::inRange('172.34.0.0', '172.16.0.0/12'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::inRange('192.168.18.214', '192.168.16.0/22'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::inRange('192.168.18.214', '192.168.16.0/23'));
|
||||
}
|
||||
|
||||
public function testIPv6CIDR()
|
||||
{
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::inRange('::1', '::1/128'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::inRange('::1', '::2'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::inRange('2400:cb00::1234', '2400:cb00::/32'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::inRange('2405:8100::1234', '2400:cb00::/32'));
|
||||
}
|
||||
|
||||
public function testIPv4Wildcard()
|
||||
{
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::inRange('192.168.134.241', '192.168.134.*'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::inRange('192.168.134.241', '192.168.*.*'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::inRange('192.168.134.241', '192.168.136.*'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::inRange('192.168.134.241', '192.172.*.*'));
|
||||
}
|
||||
|
||||
public function testIPv4Hyphen()
|
||||
{
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::inRange('192.168.134.241', '192.168.134.0-192.168.134.255'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::inRange('192.168.134.241', '192.168.128.16-192.168.145.0'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::inRange('192.168.134.241', '192.168.134.242-192.168.244.7'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::inRange('192.168.134.241', '192.168.100.255-192.168.133.19'));
|
||||
}
|
||||
|
||||
public function testValidator()
|
||||
{
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::validateRange('192.168.0.1'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::validateRange('192.168.0.0/16'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::validateRange('192.168.*.*'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::validateRange('192.168.*'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::validateRange('192.168.0.0-192.168.255.255'));
|
||||
$this->assertTrue(Rhymix\Framework\IpFilter::validateRange('2400:cb00::/32'));
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::validateRange('192.168.0.0~192.168.255.255'));
|
||||
}
|
||||
|
||||
public function testLegacy()
|
||||
{
|
||||
$this->assertTrue(\IpFilter::filter(array('192.168.134.241'), '192.168.134.241'));
|
||||
$this->assertTrue(\IpFilter::filter(array('192.168.134.0-192.168.134.255'), '192.168.134.241'));
|
||||
$this->assertTrue(\IpFilter::filter(array('127.0.0.1', '192.168.134.241'), '192.168.134.241'));
|
||||
$this->assertTrue(\IpFilter::filter(array('192.168.134.*'), '192.168.134.241'));
|
||||
$this->assertTrue(\IpFilter::filter(array('192.168.*'), '192.168.134.241'));
|
||||
$this->assertFalse(\IpFilter::filter(array('127.0.0.1'), '192.168.134.241'));
|
||||
}
|
||||
|
||||
public function testCloudFlareRealIP()
|
||||
{
|
||||
$_SERVER['HTTP_CF_CONNECTING_IP'] = '192.168.134.241';
|
||||
|
||||
$_SERVER['REMOTE_ADDR'] = '192.168.10.1';
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::getCloudFlareRealIP());
|
||||
$this->assertEquals('192.168.10.1', $_SERVER['REMOTE_ADDR']);
|
||||
|
||||
$_SERVER['REMOTE_ADDR'] = '108.162.192.121';
|
||||
$this->assertEquals('192.168.134.241', Rhymix\Framework\IpFilter::getCloudFlareRealIP());
|
||||
$this->assertEquals('192.168.134.241', $_SERVER['REMOTE_ADDR']);
|
||||
|
||||
unset($_SERVER['HTTP_CF_CONNECTING_IP']);
|
||||
$_SERVER['REMOTE_ADDR'] = '192.168.10.1';
|
||||
$this->assertFalse(Rhymix\Framework\IpFilter::getCloudFlareRealIP());
|
||||
$this->assertEquals('192.168.10.1', $_SERVER['REMOTE_ADDR']);
|
||||
}
|
||||
}
|
||||
34
tests/unit/framework/LangTest.php
Normal file
34
tests/unit/framework/LangTest.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
class LangTest extends \Codeception\TestCase\Test
|
||||
{
|
||||
public function testLang()
|
||||
{
|
||||
$ko = Rhymix\Framework\Lang::getInstance('ko');
|
||||
$en = Rhymix\Framework\Lang::getInstance('en');
|
||||
$this->assertTrue($ko instanceof Rhymix\Framework\Lang);
|
||||
$this->assertTrue($en instanceof Rhymix\Framework\Lang);
|
||||
$this->assertFalse($ko === $en);
|
||||
|
||||
$ja = Rhymix\Framework\Lang::getInstance('ja');
|
||||
$jp = Rhymix\Framework\Lang::getInstance('jp');
|
||||
$this->assertTrue($ja === $jp);
|
||||
|
||||
$this->assertEquals('도움말', $ko->get('common.help'));
|
||||
$this->assertEquals('Help', $en->get('common.help'));
|
||||
$this->assertEquals('도움말', $ko->help);
|
||||
$this->assertEquals('Help', $en->help);
|
||||
|
||||
$this->assertEquals('nonexistent', $ko->get('common.nonexistent'));
|
||||
$this->assertEquals('nonexistent', $ko->get('common.nonexistent', 'foo', 'bar'));
|
||||
|
||||
$this->assertEquals('help', $ja->help);
|
||||
$ja->loadPlugin('common');
|
||||
$this->assertEquals('ヘルプ', $ja->help);
|
||||
|
||||
$ko->foobartestlang = '%s님 안녕하세요?';
|
||||
$this->assertEquals('Travis님 안녕하세요?', $ko->foobartestlang('Travis'));
|
||||
$en->foobartestlang = 'Hello, %s!';
|
||||
$this->assertEquals('Hello, Travis!', $en->get('foobartestlang', 'Travis'));
|
||||
}
|
||||
}
|
||||
69
tests/unit/functions/LegacyTest.php
Normal file
69
tests/unit/functions/LegacyTest.php
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
class LegacyTest extends \Codeception\TestCase\Test
|
||||
{
|
||||
public function testRemoveHackTag()
|
||||
{
|
||||
$tests = 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"><iframe src="path/to/file.html"></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	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"df dummy=" />*/" onerror="console.log(\'Yet another XSS\')">'
|
||||
),
|
||||
// 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" />'
|
||||
)
|
||||
);
|
||||
|
||||
foreach ($tests as $test)
|
||||
{
|
||||
$result = removeHackTag($test[0]);
|
||||
$this->assertEquals($test[1], $result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue