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