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 testEncodeHtmlDefaultContext() { $security = new Security(); $this->assertTrue(true); // normal string - one $this->_before(); $this->assertEquals('Hello, world', Context::get('content1')); $security->encodeHTML('content1'); $this->assertEquals('<strong>Hello, world</strong>', Context::get('content1')); // normal string - two $this->_before(); $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->_before(); // 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->_before(); // 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->_before(); // 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 testEncodeHtmlCustomContext() { $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')); // associative array $array = array('first'=>'Hello', 'second'=>'World', '3rd'=>'Bold 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')); } }