34b918552b037556782e8b7c70164ed9e381477d
[tine20] / tests / tine20 / Addressbook / ListControllerTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Addressbook
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2010-2012 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Lars Kneschke <l.kneschke@metaways.de>
9  */
10
11 /**
12  * Test helper
13  */
14 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestHelper.php';
15
16 /**
17  * Test class for Tinebase_Group
18  */
19 class Addressbook_ListControllerTest extends PHPUnit_Framework_TestCase
20 {
21     /**
22      * @var array test objects
23      */
24     protected $objects = array();
25
26     /**
27      * set geodata for contacts
28      * 
29      * @var boolean
30      */
31     protected $_geodata = FALSE;
32     
33     /**
34      * Runs the test methods of this class.
35      *
36      * @access public
37      * @static
38      */
39     public static function main()
40     {
41         $suite  = new PHPUnit_Framework_TestSuite('Tine 2.0 Addressbook List Controller Tests');
42         PHPUnit_TextUI_TestRunner::run($suite);
43     }
44
45     /**
46      * Sets up the fixture.
47      * This method is called before a test is executed.
48      *
49      * @access protected
50      */
51     protected function setUp()
52     {
53         Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb());
54         $this->_geodata = Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts($this->_geodata);
55         
56         $personalContainer = Tinebase_Container::getInstance()->getPersonalContainer(
57             Zend_Registry::get('currentAccount'), 
58             'Addressbook', 
59             Zend_Registry::get('currentAccount'), 
60             Tinebase_Model_Grants::GRANT_EDIT
61         );
62         
63         $container = $personalContainer[0];
64
65         $this->objects['contact1'] = new Addressbook_Model_Contact(array(
66             'adr_one_countryname'   => 'DE',
67             'adr_one_locality'      => 'Hamburg',
68             'adr_one_postalcode'    => '24xxx',
69             'adr_one_region'        => 'Hamburg',
70             'adr_one_street'        => 'Pickhuben 4',
71             'adr_one_street2'       => 'no second street',
72             'adr_two_countryname'   => 'DE',
73             'adr_two_locality'      => 'Hamburg',
74             'adr_two_postalcode'    => '24xxx',
75             'adr_two_region'        => 'Hamburg',
76             'adr_two_street'        => 'Pickhuben 4',
77             'adr_two_street2'       => 'no second street2',
78             'assistent'             => 'Cornelius Weiß',
79             'email'                 => 'unittests@tine20.org',
80             'email_home'            => 'unittests@tine20.org',
81             'note'                  => 'Bla Bla Bla',
82             'container_id'          => $container->getId(),
83             'role'                  => 'Role',
84             'title'                 => 'Title',
85             'url'                   => 'http://www.tine20.org',
86             'url_home'              => 'http://www.tine20.com',
87             'n_family'              => 'Contact1',
88             'n_fileas'              => 'Contact1, List',
89             'n_given'               => 'List',
90             'n_middle'              => 'no middle name',
91             'n_prefix'              => 'no prefix',
92             'n_suffix'              => 'no suffix',
93             'org_name'              => 'Metaways Infosystems GmbH',
94             'org_unit'              => 'Tine 2.0',
95             'tel_assistent'         => '+49TELASSISTENT',
96             'tel_car'               => '+49TELCAR',
97             'tel_cell'              => '+49TELCELL',
98             'tel_cell_private'      => '+49TELCELLPRIVATE',
99             'tel_fax'               => '+49TELFAX',
100             'tel_fax_home'          => '+49TELFAXHOME',
101             'tel_home'              => '+49TELHOME',
102             'tel_pager'             => '+49TELPAGER',
103             'tel_work'              => '+49TELWORK',
104         ));
105         $this->objects['contact1'] = Addressbook_Controller_Contact::getInstance()->create($this->objects['contact1'], FALSE);
106         
107         $this->objects['contact2'] = new Addressbook_Model_Contact(array(
108             'adr_one_countryname'   => 'DE',
109             'adr_one_locality'      => 'Hamburg',
110             'adr_one_postalcode'    => '24xxx',
111             'adr_one_region'        => 'Hamburg',
112             'adr_one_street'        => 'Pickhuben 4',
113             'adr_one_street2'       => 'no second street',
114             'adr_two_countryname'   => 'DE',
115             'adr_two_locality'      => 'Hamburg',
116             'adr_two_postalcode'    => '24xxx',
117             'adr_two_region'        => 'Hamburg',
118             'adr_two_street'        => 'Pickhuben 4',
119             'adr_two_street2'       => 'no second street2',
120             'assistent'             => 'Cornelius Weiß',
121             'bday'                  => '1975-01-02 03:04:05',
122             'email'                 => 'unittests@tine20.org',
123             'email_home'            => 'unittests@tine20.org',
124             'note'                  => 'Bla Bla Bla',
125             'container_id'          => $container->getId(),
126             'role'                  => 'Role',
127             'title'                 => 'Title',
128             'url'                   => 'http://www.tine20.org',
129             'url_home'              => 'http://www.tine20.com',
130             'n_family'              => 'Contact2',
131             'n_fileas'              => 'Contact2, List',
132             'n_given'               => 'List',
133             'n_middle'              => 'no middle name',
134             'n_prefix'              => 'no prefix',
135             'n_suffix'              => 'no suffix',
136             'org_name'              => 'Metaways Infosystems GmbH',
137             'org_unit'              => 'Tine 2.0',
138             'tel_assistent'         => '+49TELASSISTENT',
139             'tel_car'               => '+49TELCAR',
140             'tel_cell'              => '+49TELCELL',
141             'tel_cell_private'      => '+49TELCELLPRIVATE',
142             'tel_fax'               => '+49TELFAX',
143             'tel_fax_home'          => '+49TELFAXHOME',
144             'tel_home'              => '+49TELHOME',
145             'tel_pager'             => '+49TELPAGER',
146             'tel_work'              => '+49TELWORK',
147         ));
148         $this->objects['contact2'] = Addressbook_Controller_Contact::getInstance()->create($this->objects['contact2'], FALSE);
149         
150         $this->objects['initialList'] = Addressbook_Controller_List::getInstance()->create(new Addressbook_Model_List(array(
151             'name'         => 'initial list',
152             'container_id' => $container->getId(),
153             'members'      => array($this->objects['contact1'], $this->objects['contact2'])
154         )));
155     }
156
157     /**
158      * Tears down the fixture
159      * This method is called after a test is executed.
160      *
161      * @access protected
162      */
163     protected function tearDown()
164     {
165         Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts($this->_geodata);
166         Tinebase_TransactionManager::getInstance()->rollBack();
167     }
168     
169     /**
170      * try to add a list
171      */
172     public function testAddList()
173     {
174         $list = $this->objects['initialList'];
175         $this->assertEquals($this->objects['initialList']->name, $list->name);
176     }
177     
178     /**
179      * try to get a list
180      */
181     public function testGetList()
182     {
183         $list = Addressbook_Controller_List::getInstance()->get($this->objects['initialList']);
184         
185         $this->assertEquals($this->objects['initialList']->name, $list->name);
186         $this->assertEquals($this->objects['initialList']->getId(), $list->getId());
187     }
188     
189     /**
190      * try to update a list
191      *
192      * @todo add assertions
193      */
194     public function testUpdateList()
195     {
196         $list = $this->objects['initialList'];
197         $list->members = array($this->objects['contact2']);
198         
199         $list = Addressbook_Controller_List::getInstance()->update($list);
200     }
201
202     /**
203      * try to add list member
204      */
205     public function testAddListMember()
206     {
207         $list = $this->objects['initialList'];
208         $list->members = array($this->objects['contact2']);
209         
210         $list = Addressbook_Controller_List::getInstance()->update($list);
211         $list = Addressbook_Controller_List::getInstance()->addListMember($list, $this->objects['contact1']);
212         
213         $this->assertTrue(in_array($this->objects['contact1']->getId(), $list->members));
214         $this->assertTrue(in_array($this->objects['contact2']->getId(), $list->members));
215     }
216     
217     /**
218      * testInternalAddressbookConfig
219      * 
220      * @see http://forge.tine20.org/mantisbt/view.php?id=5846
221      */
222     public function testInternalAddressbookConfig()
223     {
224         $list = $this->objects['initialList'];
225         $list->container_id = NULL;
226         $listBackend = new Addressbook_Backend_List();
227         $listBackend->update($list);
228         
229         Admin_Config::getInstance()->delete(Tinebase_Config::APPDEFAULTS);
230         $list = Addressbook_Controller_List::getInstance()->addListMember($list, $this->objects['contact1']);
231         $appConfigDefaults = Admin_Controller::getInstance()->getConfigSettings();
232         
233         $this->assertTrue(! empty($appConfigDefaults[Admin_Model_Config::DEFAULTINTERNALADDRESSBOOK]), print_r($appConfigDefaults, TRUE));
234     }
235
236     /**
237      * try to remove list member
238      */
239     public function testRemoveListMember()
240     {
241         $list = $this->objects['initialList'];
242         $list->members = array($this->objects['contact1'], $this->objects['contact2']);
243         
244         $list = Addressbook_Controller_List::getInstance()->update($list);
245         
246         $list = Addressbook_Controller_List::getInstance()->removeListMember($list, $this->objects['contact1']);
247         $this->assertEquals($list->members, array($this->objects['contact2']->getId()));
248     }
249
250     /**
251      * try to delete a list
252      */
253     public function testDeleteList()
254     {
255         Addressbook_Controller_List::getInstance()->delete($this->objects['initialList']->getId());
256
257         $this->setExpectedException('Tinebase_Exception_NotFound');
258         $list = Addressbook_Controller_List::getInstance()->get($this->objects['initialList']);
259     }
260
261     /**
262      * try to delete a contact
263      */
264     public function _testDeleteUserAccountContact()
265     {
266         $this->setExpectedException('Addressbook_Exception_AccessDenied');
267         $userContact = Addressbook_Controller_Contact::getInstance()->getContactByUserId(Tinebase_Core::getUser()->getId());
268         Addressbook_Controller_Contact::getInstance()->delete($userContact->getId());
269     }
270 }