diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Exception.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Exception.php new file mode 100644 index 00000000000..238de08835a --- /dev/null +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Exception.php @@ -0,0 +1,30 @@ + + * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 + * @link http://pear.horde.org/index.php?package=Kolab_Storage + */ + +/** + * The exception marker for data specific Horde_Kolab_Storage errors. + * + * Copyright 2012 Horde LLC (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.horde.org/licenses/lgpl21. + * + * @category Kolab + * @package Kolab_Storage + * @author Gunnar Wrobel + * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 + * @link http://pear.horde.org/index.php?package=Kolab_Storage + */ +class Horde_Kolab_Storage_Data_Exception extends Horde_Exception_Wrapped +{ +} diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Format/Mime.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Format/Mime.php index eecbf9a5e6f..271d8bee620 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Format/Mime.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Format/Mime.php @@ -240,17 +240,19 @@ public function createKolabPart($object, array $options) $kolab = new Horde_Mime_Part(); $kolab->setType($this->getMimeType($options['type'])); if (empty($options['raw'])) { + $format = new Horde_Kolab_Storage_Data_Object_Content( + $this->_factory->createFormat( + 'Xml', $options['type'], $options['version'] + ) + ); if (isset($options['previous'])) { - $previous = array('previous' => $options['previous']); + $content = $format->modify($object, $options['previous']); } else { - $previous = array(); + $content = $format->create($object); } try { $kolab->setContents( - $this->_factory->createFormat( - 'Xml', $options['type'], $options['version'] - )->save($object, $previous), - array('encoding' => 'quoted-printable') + $content, array('encoding' => 'quoted-printable') ); } catch (Horde_Kolab_Format_Exception $e) { throw new Horde_Kolab_Storage_Exception( diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Object/Content.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Object/Content.php new file mode 100644 index 00000000000..0b2a8e32471 --- /dev/null +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Data/Object/Content.php @@ -0,0 +1,85 @@ + + * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 + * @link http://pear.horde.org/index.php?package=Kolab_Storage + */ + +/** + * Generates the core Kolab content. + * + * Copyright 2011-2012 Horde LLC (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.horde.org/licenses/lgpl21. + * + * @category Kolab + * @package Kolab_Storage + * @author Gunnar Wrobel + * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 + * @link http://pear.horde.org/index.php?package=Kolab_Storage + */ +class Horde_Kolab_Storage_Data_Object_Content +{ + /** + * Kolab format handler. + * + * @var Horde_Kolab_Format + */ + private $_format; + + /** + * Constructor. + * + * @param Horde_Kolab_Format $format The Kolab format handler. + */ + public function __construct(Horde_Kolab_Format $format) + { + $this->_format = $format; + } + + /** + * Create the Kolab content as a string. + * + * @param array $object The object data. + * + * @return string The Kolab content. + */ + public function create(array $object) + { + try { + return $this->_format->save($object); + } catch (Horde_Kolab_Format_Exception $e) { + throw new Horde_Kolab_Storage_Data_Exception( + 'Failed saving Kolab object!', 0, $e + ); + } + } + + /** + * Modify a previous Kolab object. + * + * @param array $object The new object data. + * @param string $previous The previous data. + * + * @return string The new Kolab content. + */ + public function modify(array $object, $previous) + { + try { + return $this->_format->save( + $object, array('previous' => $previous) + ); + } catch (Horde_Kolab_Format_Exception $e) { + throw new Horde_Kolab_Storage_Data_Exception( + 'Failed saving Kolab object!', 0, $e + ); + } + } +} \ No newline at end of file diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/Data/Object/ContentTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/Data/Object/ContentTest.php new file mode 100644 index 00000000000..9ef5095e359 --- /dev/null +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/Data/Object/ContentTest.php @@ -0,0 +1,93 @@ + + * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 + * @link http://pear.horde.org/index.php?package=Kolab_Storage + */ + +/** + * Prepare the test setup. + */ +require_once __DIR__ . '/../../../Autoload.php'; + +/** + * Test the Kolab content handler. + * + * Copyright 2011-2012 Horde LLC (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.horde.org/licenses/lgpl21. + * + * @category Kolab + * @package Kolab_Storage + * @subpackage UnitTests + * @author Gunnar Wrobel + * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 + * @link http://pear.horde.org/index.php?package=Kolab_Storage + */ +class Horde_Kolab_Storage_Unit_Data_Object_ContentTest +extends PHPUnit_Framework_TestCase +{ + public function testCreate() + { + $format = $this->getMock('Horde_Kolab_Format'); + $format->expects($this->once()) + ->method('save') + ->with(array('foo' => 'foo')) + ->will($this->returnValue('')); + $content = new Horde_Kolab_Storage_Data_Object_Content($format); + $this->assertEquals( + '', $content->create(array('foo' => 'foo')) + ); + } + + /** + * @expectedException Horde_Kolab_Storage_Data_Exception + */ + public function testCreateWithException() + { + $format = $this->getMock('Horde_Kolab_Format'); + $format->expects($this->once()) + ->method('save') + ->with(array('foo' => 'foo')) + ->will($this->throwException(new Horde_Kolab_Format_Exception())); + $content = new Horde_Kolab_Storage_Data_Object_Content($format); + $content->create(array('foo' => 'foo')); + } + + public function testModify() + { + $format = $this->getMock('Horde_Kolab_Format'); + $format->expects($this->once()) + ->method('save') + ->with(array('foo' => 'foo'), array('previous' => '')) + ->will($this->returnValue('')); + $content = new Horde_Kolab_Storage_Data_Object_Content($format); + $this->assertEquals( + '', + $content->modify(array('foo' => 'foo'), '') + ); + } + + /** + * @expectedException Horde_Kolab_Storage_Data_Exception + */ + public function testModifyWithException() + { + $format = $this->getMock('Horde_Kolab_Format'); + $format->expects($this->once()) + ->method('save') + ->with(array('foo' => 'foo'), array('previous' => '')) + ->will($this->throwException(new Horde_Kolab_Format_Exception())); + $content = new Horde_Kolab_Storage_Data_Object_Content($format); + $content->modify(array('foo' => 'foo'), ''); + } + +}