diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index 1640be1b..18023072 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -152,10 +152,15 @@ public function format($event) $elt = $dom->appendChild(new \DOMElement($this->_rootElement)); foreach ($dataToInsert as $key => $value) { - if($key == "message") { - $value = htmlspecialchars($value, ENT_COMPAT, $enc); + if (empty($value) + || is_scalar($value) + || (is_object($value) && method_exists($value,'__toString')) + ) { + if($key == "message") { + $value = htmlspecialchars($value, ENT_COMPAT, $enc); + } + $elt->appendChild(new \DOMElement($key, (string)$value)); } - $elt->appendChild(new \DOMElement($key, $value)); } $xml = $dom->saveXML(); diff --git a/test/Formatter/XmlTest.php b/test/Formatter/XmlTest.php index 16849c17..5890c4ad 100644 --- a/test/Formatter/XmlTest.php +++ b/test/Formatter/XmlTest.php @@ -21,7 +21,8 @@ namespace ZendTest\Log\Formatter; -use \Zend\Log\Formatter\Xml as XmlFormatter; +use ZendTest\Log\TestAsset\SerializableObject, + \Zend\Log\Formatter\Xml as XmlFormatter; /** * @category Zend @@ -127,4 +128,46 @@ public function testFactory() $formatter = XmlFormatter::factory($options); $this->assertInstanceOf('Zend\Log\Formatter\Xml', $formatter); } + + /** + * @group ZF-11161 + */ + public function testNonScalarValuesAreExcludedFromFormattedString() + { + $options = array( + 'rootElement' => 'log' + ); + $event = array( + 'message' => 'tottakai', + 'priority' => 4, + 'context' => array('test'=>'one'), + 'reference' => new XmlFormatter() + ); + $expected = 'tottakai4'; + + $formatter = XmlFormatter::factory($options); + $output = $formatter->format($event); + $this->assertContains($expected, $output); + } + + /** + * @group ZF-11161 + */ + public function testObjectsWithStringSerializationAreIncludedInFormattedString() + { + $options = array( + 'rootElement' => 'log' + ); + $event = array( + 'message' => 'tottakai', + 'priority' => 4, + 'context' => array('test'=>'one'), + 'reference' => new SerializableObject() + ); + $expected = 'tottakai4ZendTest\Log\TestAsset\SerializableObject'; + + $formatter = XmlFormatter::factory($options); + $output = $formatter->format($event); + $this->assertContains($expected, $output); + } } diff --git a/test/TestAsset/SerializableObject.php b/test/TestAsset/SerializableObject.php new file mode 100644 index 00000000..703f2db1 --- /dev/null +++ b/test/TestAsset/SerializableObject.php @@ -0,0 +1,10 @@ +_originaltimezone = date_default_timezone_get(); date_default_timezone_set('America/Los_Angeles'); // Reset front controller to reset registered plugins and @@ -74,6 +81,7 @@ public function tearDown() { Channel\HttpHeaders::destroyInstance(); FirePhp::destroyInstance(); + date_default_timezone_set($this->_originaltimezone); } /** @@ -206,9 +214,9 @@ public function testAdvancedLogging() $table = array( 'Summary line for the table', array( - array('Column 1', 'Column 2'), - array('Row 1 c 1',' Row 1 c 2'), - array('Row 2 c 1',' Row 2 c 2') + array('Column 1', 'Column 2'), + array('Row 1 c 1',' Row 1 c 2'), + array('Row 2 c 1',' Row 2 c 2') ) ); diff --git a/test/_files/layout.phtml b/test/_files/layout.phtml index 617726dd..0be65f43 100644 --- a/test/_files/layout.phtml +++ b/test/_files/layout.phtml @@ -1 +1 @@ -broker('layout')->direct()->events; \ No newline at end of file +plugin('layout')->direct()->events; \ No newline at end of file