Merge branch 'master' of http://git.tine20.org/git/Syncope
[tine20] / tine20 / library / Syncope / tests / Syncope / Backend / ContentTests.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Backend
6  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
7  * @copyright   Copyright (c) 2011-2012 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Lars Kneschke <l.kneschke@metaways.de>
9  */
10
11 /**
12  * Test class for Syncope_Command_Sync
13  * 
14  * @package     Backend
15  */
16 class Syncope_Backend_ContentTests extends PHPUnit_Framework_TestCase
17 {
18     /**
19      * @var Syncope_Model_Device
20      */
21     protected $_device;
22     
23     /**
24      * @var Syncope_Model_Folder
25      */
26     protected $_folder;
27     
28     /**
29      * @var Syncope_Backend_Content
30      */
31     protected $_contentBackend;
32     
33     /**
34      * @var Syncope_Backend_Device
35      */
36     protected $_deviceBackend;
37
38     /**
39      * @var Syncope_Backend_Folder
40      */
41     protected $_folderBackend;
42     
43     /**
44      * @var Syncope_Backend_SyncState
45      */
46     protected $_syncStateBackend;
47     
48     /**
49      * @var Zend_Db_Adapter_Abstract
50      */
51     protected $_db;
52     
53     /**
54      * Runs the test methods of this class.
55      *
56      * @access public
57      * @static
58      */
59     public static function main()
60     {
61         $suite  = new PHPUnit_Framework_TestSuite('Syncope Content backend tests');
62         PHPUnit_TextUI_TestRunner::run($suite);
63     }
64     
65     /**
66      * (non-PHPdoc)
67      * @see ActiveSync/ActiveSync_TestCase::setUp()
68      */
69     protected function setUp()
70     {
71         $this->_db = getTestDatabase();
72         
73         $this->_db->beginTransaction();
74
75         $this->_contentBackend   = new Syncope_Backend_Content($this->_db);
76         $this->_deviceBackend    = new Syncope_Backend_Device($this->_db);
77         $this->_folderBackend    = new Syncope_Backend_Folder($this->_db);
78         $this->_syncStateBackend = new Syncope_Backend_SyncState($this->_db);
79
80         $this->_device = $this->_deviceBackend->create(
81             Syncope_Backend_DeviceTests::getTestDevice()
82         );
83         $this->_folder = $this->_folderBackend->create(
84             Syncope_Backend_FolderTests::getTestFolder($this->_device)
85         );
86     }
87
88     /**
89      * Tears down the fixture
90      * This method is called after a test is executed.
91      *
92      * @access protected
93      */
94     protected function tearDown()
95     {
96         $this->_db->rollBack();
97     }
98     
99     /**
100      * @return Syncope_Model_ISyncState
101      */
102     public function testCreate()
103     {
104         $content = self::getTestContent($this->_device, $this->_folder);
105         
106         $content = $this->_contentBackend->create($content);
107         
108         $this->assertTrue($content->creation_time instanceof DateTime);
109         
110         return $content;
111     }
112     
113     /**
114      * 
115      */
116     public function testDelete()
117     {
118         $content = $this->testCreate();
119         
120         $this->_contentBackend->delete($content);
121         
122         $content = $this->_contentBackend->get($content->id);
123         
124         $this->assertTrue($content instanceof Syncope_Model_IContent);
125         $this->assertEquals(1, $content->is_deleted);
126         $this->assertTrue($content->creation_time instanceof DateTime);
127     }  
128
129     public function testGetFolderState()
130     {
131         $content = $this->testCreate();
132         
133         $state = $this->_contentBackend->getFolderState($this->_device, $this->_folder);
134         
135         $this->assertContains($content->contentid, $state);
136     }
137     
138     public function testResetState()
139     {
140         $content = $this->testCreate();
141     
142         $this->_contentBackend->resetState($this->_device, $this->_folder);
143         $state = $this->_contentBackend->getFolderState($this->_device, $this->_folder);
144     
145         $this->assertTrue(empty($state));
146     }
147     
148     public function testGetExceptionNotFound()
149     {
150         $this->setExpectedException('Syncope_Exception_NotFound');
151     
152         $this->_contentBackend->get('invalidId');
153     }
154     
155     public function testGetContentStateExceptionNotFound()
156     {
157         $this->setExpectedException('Syncope_Exception_NotFound');
158     
159         $this->_contentBackend->getContentState('invalidId', 'invalidId', 'invalidId');
160     }
161     
162     public static function getTestContent(Syncope_Model_IDevice $_device, Syncope_Model_IFolder $_folder)
163     {
164         return new Syncope_Model_Content(array(
165             'device_id'        => $_device,
166             'folder_id'        => $_folder,
167             'contentid'        => 'abc1234',
168             'creation_time'    => new DateTime(null, new DateTimeZone('utc')),
169             'creation_synckey' => 1
170         ));
171     }
172 }