Hello, world'); Context::set('content2', 'Wow, >_< !'); // object $args = new stdClass; $args->prop1 = 'Normal string'; $args->prop2 = 'He said, "Very nice!"'; $args->prop3 = 'Strong Baby'; Context::set('object1', $args); // array $arr = array(); $arr[] = 'First'; $arr[] = 'Second'; $arr[] = 'Third'; Context::set('array1', $arr); // associative array $aarr = array(); $aarr['elem1'] = 'One 1'; $aarr['elem2'] = 'Two 2'; $aarr['elem3'] = 'Three 3'; Context::set('array2', $aarr); } public function testEncodeHTML_DefaultContext() { $security = new Security(); // normal string - one $this->setUp(); $this->assertEquals('Hello, world', Context::get('content1')); $security->encodeHTML('content1'); $this->assertEquals('<strong>Hello, world</strong>', Context::get('content1')); // normal string - two $this->setUp(); $this->assertEquals('Hello, world', 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('First','Second','Third')); $security->encodeHTML('array1'); // should ignore this $this->assertEquals(Context::get('array1'), array('First','Second','Third')); $security->encodeHTML('array1.0'); // affect only first element $this->assertEquals(Context::get('array1'), array('<span class="first">F</span>irst','Second','Third')); $security->encodeHTML('array1.2'); // affects only third element $this->assertEquals(Context::get('array1'), array('<span class="first">F</span>irst','Second','<b>T</b>hird')); $this->setUp(); // reset; $this->assertEquals(Context::get('array1'), array('First','Second','Third')); $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 1','elem2'=>'Two 2','elem3'=>'Three 3')); $security->encodeHTML('array2'); // should ignore this $this->assertEquals(Context::get('array2'), array('elem1'=>'One 1','elem2'=>'Two 2','elem3'=>'Three 3')); $security->encodeHTML('array2.0'); // should ignore this $this->assertEquals(Context::get('array2'), array('elem1'=>'One 1','elem2'=>'Two 2','elem3'=>'Three 3')); $security->encodeHTML('array2.elem2'); // affects only 'elem2' $this->assertEquals(Context::get('array2'), array('elem1'=>'One 1','elem2'=>'Two <del>2</del>','elem3'=>'Three 3')); $this->setUp(); // reset; $this->assertEquals(Context::get('array2'), array('elem1'=>'One 1','elem2'=>'Two 2','elem3'=>'Three 3')); $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 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 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 testEncodeHTML_CustomContext() { $array = array('Hello', 'World', 'Bold 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')); } } $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; } }