Update test cases

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@9862 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
taggon 2011-11-23 06:47:07 +00:00
parent 5ae6ca1829
commit 9b5d4c7bac
4 changed files with 135 additions and 11 deletions

View file

@ -13,6 +13,71 @@ class ContextTest extends PHPUnit_Framework_TestCase
*/ */
public function testGetInstance() public function testGetInstance()
{ {
$this->assertInstanceOf('Context', Context::getInstance());
$this->assertSame(Context::getInstance(), Context::getInstance());
}
public function testSetGetVars()
{
$this->assertSame(Context::get('var1'), null);
Context::set('var1', 'val1');
$this->assertSame(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

@ -116,6 +116,12 @@ class SecurityTest extends PHPUnit_Framework_TestCase
$security = new Security($array); $security = new Security($array);
$returned = $security->encodeHTML('.'); $returned = $security->encodeHTML('.');
$this->assertEquals($returned, array('Hello', 'World', '<b>Bold</b> is not bald')); $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'=>'&lt;b&gt;Bold&lt;/b&gt; is not bald'));
} }
} }

View file

@ -8,6 +8,15 @@ require_once _XE_PATH_.'classes/validator/Validator.class.php';
class ValidatorTest extends PHPUnit_Framework_TestCase class ValidatorTest extends PHPUnit_Framework_TestCase
{ {
protected function setUp() {
global $lang;
$lang->filter = new stdClass;
$lang->filter->isnull = 'isnull';
$lang->filter->outofrange = 'outofrange';
$lang->filter->equalto = 'equalto';
}
public function testRequired() { public function testRequired() {
$vd = new Validator(); $vd = new Validator();
$vd->addFilter('userid', array('required'=>'true')); $vd->addFilter('userid', array('required'=>'true'));
@ -42,6 +51,26 @@ class ValidatorTest extends PHPUnit_Framework_TestCase
$this->assertTrue( $vd->validate() ); $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 testDefault() { public function testDefault() {
$vd = new Validator(); $vd = new Validator();
$vd->addFilter('userid', array('default'=>'ididid')); $vd->addFilter('userid', array('default'=>'ididid'));
@ -60,18 +89,20 @@ class ValidatorTest extends PHPUnit_Framework_TestCase
$this->assertEquals( $arr, array('userid'=>'ownid') ); $this->assertEquals( $arr, array('userid'=>'ownid') );
if(defined('MOCK_CONTEXT')) { if(defined('MOCK_CONTEXT')) {
Context::truncate();
// context data // context data
$mock_vars = array(); // empty context variables
$vd->validate(); $vd->validate();
$this->assertEquals( 'ididid', Context::get('userid') ); $this->assertEquals( 'ididid', Context::get('userid') );
}
$vd->load(dirname(__FILE__).'/login.xml'); $vd->load(dirname(__FILE__).'/login.xml');
Context::set('userid', '', true); Context::set('userid', '', true);
$vd->validate(); $vd->validate();
$this->assertEquals( 'idididid', Context::get('userid') ); $this->assertEquals( 'idididid', Context::get('userid') );
} }
}
public function testLength() { public function testLength() {
$vd = new Validator(); $vd = new Validator();
@ -85,11 +116,6 @@ class ValidatorTest extends PHPUnit_Framework_TestCase
public function testCustomRule() { public function testCustomRule() {
} }
public function testJSCompile() {
$vd = new Validator();
$vd->setCacheDir(dirname(__FILE__));
}
public function testCondition() { public function testCondition() {
$vd = new Validator(); $vd = new Validator();
$data = array('greeting1'=>'hello'); $data = array('greeting1'=>'hello');
@ -113,6 +139,7 @@ class ValidatorTest extends PHPUnit_Framework_TestCase
} }
public function testConditionXml() { public function testConditionXml() {
$vd = new Validator(dirname(__FILE__).'/condition.xml'); $vd = new Validator(dirname(__FILE__).'/condition.xml');
$data = array('greeting1'=>'hello'); $data = array('greeting1'=>'hello');
@ -124,6 +151,24 @@ class ValidatorTest extends PHPUnit_Framework_TestCase
$data['greeting2'] = 'World'; $data['greeting2'] = 'World';
$this->assertTrue($vd->validate($data)); $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);