mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-13 16:34:52 +09:00
Add Security class
git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8815 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
3b0660c969
commit
5bfd3e6b61
2 changed files with 250 additions and 0 deletions
141
tests/classes/security/SecurityTest.php
Normal file
141
tests/classes/security/SecurityTest.php
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
define('__DEBUG__', 1);
|
||||
$xe_path = realpath(dirname(__FILE__).'/../../../');
|
||||
require "{$xe_path}/classes/security/Security.class.php";
|
||||
|
||||
error_reporting(E_ALL & ~E_NOTICE);
|
||||
|
||||
class SecurityTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
/**
|
||||
* 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 testEncodeHTML_DefaultContext()
|
||||
{
|
||||
$security = new Security();
|
||||
|
||||
// normal string - one
|
||||
$this->setUp();
|
||||
$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->setUp();
|
||||
$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->setUp(); // 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->setUp(); // 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->setUp(); // 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$mock_vars = array();
|
||||
|
||||
class Context
|
||||
{
|
||||
public function gets() {
|
||||
global $mock_vars;
|
||||
|
||||
$args = func_get_args();
|
||||
$output = new stdClass;
|
||||
|
||||
foreach($args as $name) {
|
||||
$output->{$name} = $mock_vars[$name];
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public function get($name) {
|
||||
global $mock_vars;
|
||||
return array_key_exists($name, $mock_vars)?$mock_vars[$name]:'';
|
||||
}
|
||||
|
||||
public function set($name, $value) {
|
||||
global $mock_vars;
|
||||
|
||||
$mock_vars[$name] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue