dc1e50e735510b36f5b9f2c6823c91396a64cbb5
[tine20] / tests / tine20 / Addressbook / ControllerTest.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) 2008-2013 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Lars Kneschke <l.kneschke@metaways.de>
9  * 
10  */
11
12 /**
13  * Test helper
14  */
15 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestHelper.php';
16
17 /**
18  * Test class for Addressbook_Controller
19  */
20 class Addressbook_ControllerTest extends PHPUnit_Framework_TestCase
21 {
22     /**
23      * set geodata for contacts
24      * 
25      * @var boolean
26      */
27     protected $_geodata = FALSE;
28     
29     /**
30      * @var array test objects
31      */
32     protected $objects = array();
33
34     /**
35      * @var Addressbook_Controller_Contact
36      */
37     protected $_instance = NULL;
38     
39     /**
40      * Runs the test methods of this class.
41      *
42      * @access public
43      * @static
44      */
45     public static function main()
46     {
47         $suite  = new PHPUnit_Framework_TestSuite('Tine 2.0 Addressbook Controller Tests');
48         PHPUnit_TextUI_TestRunner::run($suite);
49     }
50
51     /**
52      * Sets up the fixture.
53      * This method is called before a test is executed.
54      *
55      * @access protected
56      */
57     protected function setUp()
58     {
59         $this->_geodata = Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts(false);
60         
61         $personalContainer = Tinebase_Container::getInstance()->getPersonalContainer(
62             Zend_Registry::get('currentAccount'), 
63             'Addressbook', 
64             Zend_Registry::get('currentAccount'), 
65             Tinebase_Model_Grants::GRANT_EDIT
66         );
67         
68         $container = $personalContainer[0];
69         
70         $this->objects['initialContact'] = new Addressbook_Model_Contact(array(
71             'adr_one_countryname'   => 'DE',
72             'adr_one_locality'      => 'Hamburg',
73             'adr_one_postalcode'    => '24xxx',
74             'adr_one_region'        => 'Hamburg',
75             'adr_one_street'        => 'Pickhuben 4',
76             'adr_one_street2'       => 'no second street',
77             'adr_two_countryname'   => 'DE',
78             'adr_two_locality'      => 'Hamburg',
79             'adr_two_postalcode'    => '24xxx',
80             'adr_two_region'        => 'Hamburg',
81             'adr_two_street'        => 'Pickhuben 4',
82             'adr_two_street2'       => 'no second street2',
83             'assistent'             => 'Cornelius Weiß',
84             'bday'                  => '1975-01-02 03:04:05', // new Tinebase_DateTime???
85             'email'                 => 'unittests@tine20.org',
86             'email_home'            => 'unittests@tine20.org',
87             'jpegphoto'             => file_get_contents(dirname(__FILE__) . '/../Tinebase/ImageHelper/phpunit-logo.gif'),
88             'note'                  => 'Bla Bla Bla',
89             'container_id'          => $container->id,
90             'role'                  => 'Role',
91             'title'                 => 'Title',
92             'url'                   => 'http://www.tine20.org',
93             'url_home'              => 'http://www.mundundzähne.de',
94             'n_family'              => 'Kneschke',
95             'n_fileas'              => 'Kneschke, Lars',
96             'n_given'               => 'Laars',
97             'n_middle'              => 'no middle name',
98             'n_prefix'              => 'no prefix',
99             'n_suffix'              => 'no suffix',
100             'org_name'              => 'Metaways Infosystems GmbH',
101             'org_unit'              => 'Tine 2.0',
102             'tel_assistent'         => '+49TELASSISTENT',
103             'tel_car'               => '+49TELCAR',
104             'tel_cell'              => '+49TELCELL',
105             'tel_cell_private'      => '+49TELCELLPRIVATE',
106             'tel_fax'               => '+49TELFAX',
107             'tel_fax_home'          => '+49TELFAXHOME',
108             'tel_home'              => '+49TELHOME',
109             'tel_pager'             => '+49TELPAGER',
110             'tel_work'              => '+49TELWORK',
111         ));
112         
113         $this->objects['updatedContact'] = new Addressbook_Model_Contact(array(
114             'adr_one_countryname'   => 'DE',
115             'adr_one_locality'      => 'Hamburg',
116             'adr_one_postalcode'    => '24xxx',
117             'adr_one_region'        => 'Hamburg',
118             'adr_one_street'        => 'Pickhuben 4',
119             'adr_one_street2'       => 'no second street',
120             'adr_two_countryname'   => 'DE',
121             'adr_two_locality'      => 'Hamburg',
122             'adr_two_postalcode'    => '24xxx',
123             'adr_two_region'        => 'Hamburg',
124             'adr_two_street'        => 'Pickhuben 4',
125             'adr_two_street2'       => 'no second street2',
126             'assistent'             => 'Cornelius Weiß',
127             'bday'                  => '1975-01-02 03:04:05', // new Tinebase_DateTime???
128             'email'                 => 'unittests@tine20.org',
129             'email_home'            => 'unittests@tine20.org',
130             'jpegphoto'             => '',
131             'note'                  => 'Bla Bla Bla',
132             'container_id'          => $container->id,
133             'role'                  => 'Role',
134             'title'                 => 'Title',
135             'url'                   => 'http://www.tine20.org',
136             'url_home'              => 'http://www.tine20.com',
137             'n_family'              => 'Kneschke',
138             'n_fileas'              => 'Kneschke, Lars',
139             'n_given'               => 'Lars',
140             'n_middle'              => 'no middle name',
141             'n_prefix'              => 'no prefix',
142             'n_suffix'              => 'no suffix',
143             'org_name'              => 'Metaways Infosystems GmbH',
144             'org_unit'              => 'Tine 2.0',
145             'tel_assistent'         => '+49TELASSISTENT',
146             'tel_car'               => '+49TELCAR',
147             'tel_cell'              => '+49TELCELL',
148             'tel_cell_private'      => '+49TELCELLPRIVATE',
149             'tel_fax'               => '+49TELFAX',
150             'tel_fax_home'          => '+49TELFAXHOME',
151             'tel_home'              => '+49TELHOME',
152             'tel_pager'             => '+49TELPAGER',
153             'tel_work'              => '+49TELWORK',
154         ));
155                 
156         $this->objects['note'] = new Tinebase_Model_Note(array(
157             'note_type_id'      => 1,
158             'note'              => 'phpunit test note',    
159         ));
160         
161         $this->_instance = Addressbook_Controller_Contact::getInstance();
162     }
163
164     /**
165      * Tears down the fixture
166      * This method is called after a test is executed.
167      *
168      * @access protected
169      */
170     protected function tearDown()
171     {
172         Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts($this->_geodata);
173         
174         if ((isset($this->objects['contact']) || array_key_exists('contact', $this->objects))) {
175             $this->_instance->delete($this->objects['contact']);
176         }
177     }
178     
179     /**
180      * adds a contact
181      *
182      * @return Addressbook_Model_Contact
183      */
184     protected function _addContact()
185     {
186         $contact = $this->objects['initialContact'];
187         $contact->notes = new Tinebase_Record_RecordSet('Tinebase_Model_Note', array($this->objects['note']));
188         $contact = $this->_instance->create($contact);
189         $this->objects['contact'] = $contact;
190         
191         $this->assertEquals($this->objects['initialContact']->adr_one_locality, $contact->adr_one_locality);
192         
193         return $contact;
194     }
195     
196     /**
197      * try to get a contact
198      */
199     public function testGetContact()
200     {
201         $contact = $this->_addContact();
202         
203         $this->assertEquals($this->objects['initialContact']->adr_one_locality, $contact->adr_one_locality);
204     }
205     
206     /**
207      * test getImage function
208      *
209      */
210     public function testGetImage()
211     {
212         $contact = $this->_addContact();
213         
214         $image = Addressbook_Controller::getInstance()->getImage($contact->getId());
215         $this->assertEquals('Tinebase_Model_Image', get_class($image));
216         $this->assertEquals($image->width, 94);
217     }
218     
219     /**
220      * try to get count of contacts
221      *
222      */
223     public function testGetCountByOwner()
224     {
225         $contact = $this->_addContact();
226         
227         $filter = new Addressbook_Model_ContactFilter(array(
228             array('field' => 'query',         'operator' => 'contains', 'value' => $contact->n_family),
229             array('field' => 'containerType', 'operator' => 'equals',   'value' => 'personal'),
230             array('field' => 'owner',         'operator' => 'equals',   'value' => Zend_Registry::get('currentAccount')->getId()),
231         ));
232         $count = $this->_instance->searchCount($filter);
233         
234         $this->assertEquals(1, $count);
235     }
236     
237     /**
238      * try to get count of contacts
239      *
240      */
241     public function testGetCountByAddressbookId()
242     {
243         $contact = $this->_addContact();
244         
245         $personalContainer = Tinebase_Container::getInstance()->getPersonalContainer(
246             Zend_Registry::get('currentAccount'), 
247             'Addressbook', 
248             Zend_Registry::get('currentAccount'), 
249             Tinebase_Model_Grants::GRANT_EDIT
250         );
251         $container = $personalContainer[0];
252         
253         $filter = new Addressbook_Model_ContactFilter(array(
254             array('field' => 'containerType', 'operator' => 'equals',   'value' => 'all'),
255         ));
256         $filter->container = array($container->getId());
257         $count = $this->_instance->searchCount($filter);
258         
259         $this->assertGreaterThan(0, $count);
260     }
261     
262     /**
263      * try to get count of contacts
264      *
265      */
266     public function testGetCountOfAllContacts()
267     {
268         $contact = $this->_addContact();
269         
270         $filter = new Addressbook_Model_ContactFilter(array(
271             array('field' => 'query',         'operator' => 'contains', 'value' => $contact->n_family),
272             array('field' => 'containerType', 'operator' => 'equals',   'value' => 'all'),
273         ));
274         $count = $this->_instance->searchCount($filter);
275         
276         $this->assertEquals(1, $count);
277     }
278     
279     /**
280      * try to update a contact
281      */
282     public function testUpdateContact()
283     {
284         $contact = $this->_addContact();
285         
286         $this->objects['updatedContact']->setId($contact->getId());
287         $contact = $this->_instance->update($this->objects['updatedContact']);
288
289         $this->assertEquals($this->objects['updatedContact']->adr_one_locality, $contact->adr_one_locality);
290         $this->assertEquals($this->objects['updatedContact']->n_given." ".$this->objects['updatedContact']->n_family, $contact->n_fn);
291         
292         $filter = new Addressbook_Model_ContactFilter(array(
293             array('field' => 'last_modified_by', 'operator' => 'equals', 'value' => Zend_Registry::get('currentAccount')->getId())
294         ));
295         $count = $this->_instance->searchCount($filter);
296         $this->assertTrue($count > 0);
297         
298         $date = Tinebase_DateTime::now();
299         $filter = new Addressbook_Model_ContactFilter(array(
300             array('field' => 'last_modified_time', 'operator' => 'equals', 'value' => $date->toString('Y-m-d'))
301         ));
302         $count = $this->_instance->searchCount($filter);
303         $this->assertTrue($count > 0);
304     }
305
306     /**
307      * try to update a contact with missing postalcode
308      * 
309      * @see 0009424: missing postalcode prevents saving of contact
310      */
311     public function testUpdateContactWithMissingPostalcode()
312     {
313         Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts(true);
314         $contact = $this->_addContact();
315         $contact->adr_two_street = null;
316         $contact->adr_two_postalcode = null;
317         $contact->adr_two_locality = 'Münster';
318         $contact->adr_two_region = 'Nordrhein-Westfalen';
319         
320         $updatedContact = $this->_instance->update($contact);
321         $this->assertEquals('481xx', $updatedContact->adr_two_postalcode);
322     }
323     
324     /**
325      * test remove image
326      */
327     public function testRemoveContactImage()
328     {
329         $contact = $this->_addContact();
330         
331         $contact->jpegphoto = '';
332         $contact = $this->_instance->update($contact);
333         
334         $this->setExpectedException('Addressbook_Exception_NotFound');
335         $image = Addressbook_Controller::getInstance()->getImage($contact->id);
336     }
337     
338     /**
339      * try to delete a contact
340      *
341      */
342     public function testDeleteContact()
343     {
344         $contact = $this->_addContact();
345         
346         $this->_instance->delete($contact->getId());
347         unset($this->objects['contact']);
348
349         $this->setExpectedException('Tinebase_Exception_NotFound');
350         $contact = $this->_instance->get($contact->getId());
351     }
352
353     /**
354      * try to delete a contact
355      *
356      */
357     public function testDeleteUserAccountContact()
358     {
359         $this->setExpectedException('Addressbook_Exception_AccessDenied');
360         $userContact = $this->_instance->getContactByUserId(Tinebase_Core::getUser()->getId());
361         $this->_instance->delete($userContact->getId());
362     }
363     
364     /**
365      * try to create a personal folder 
366      *
367      */
368     public function testCreatePersonalFolder()
369     {
370         $account = Zend_Registry::get('currentAccount');
371         $folder = Addressbook_Controller::getInstance()->createPersonalFolder($account);
372         $this->assertEquals(1, count($folder));
373         $folder = Addressbook_Controller::getInstance()->createPersonalFolder($account->getId());
374         $this->assertEquals(1, count($folder));
375     }
376     
377     /**
378      * test in week operator of creation time filter
379      */
380     public function testCreationTimeWeekOperator()
381     {
382         $contact = $this->_addContact();
383         
384         $filter = new Addressbook_Model_ContactFilter(array(
385             array('field' => 'container_id',  'operator' => 'equals',   'value' => $contact->container_id),
386             array('field' => 'owner',         'operator' => 'equals',   'value' => Zend_Registry::get('currentAccount')->getId()),
387         ));
388         $count1 = $this->_instance->searchCount($filter);
389         
390         $filter = new Addressbook_Model_ContactFilter(array(
391             array('field' => 'creation_time', 'operator' => 'inweek',   'value' => 0),
392             array('field' => 'container_id',  'operator' => 'equals',   'value' => $contact->container_id),
393             array('field' => 'owner',         'operator' => 'equals',   'value' => Zend_Registry::get('currentAccount')->getId()),
394         ));
395         $count2 = $this->_instance->searchCount($filter);
396         $this->assertEquals($count1, $count2);
397     }
398     
399     /**
400      * test equals operator of creation time filter
401      */
402     public function testCreationTimeEqualsOperator()
403     {
404         $contact = $this->_addContact();
405         
406         $filter = new Addressbook_Model_ContactFilter(array(
407             array('field' => 'container_id',  'operator' => 'equals',   'value' => $contact->container_id),
408             array('field' => 'owner',         'operator' => 'equals',   'value' => Zend_Registry::get('currentAccount')->getId()),
409         ));
410         $count1 = $this->_instance->searchCount($filter);
411         
412         $date = Tinebase_DateTime::now();
413         $filter = new Addressbook_Model_ContactFilter(array(
414             array('field' => 'creation_time', 'operator' => 'equals',   'value' => $date->toString('Y-m-d')),
415             array('field' => 'container_id',  'operator' => 'equals',   'value' => $contact->container_id),
416             array('field' => 'owner',         'operator' => 'equals',   'value' => Zend_Registry::get('currentAccount')->getId()),
417         ));
418         $count2 = $this->_instance->searchCount($filter);
419         $this->assertEquals($count1, $count2);
420     }
421 }