0009932: Parametrize references to e-mail application
[tine20] / tests / tine20 / Tinebase / Frontend / JsonTest.php
1 <?php
2 /**
3  * Tine 2.0
4  *
5  * @package     Tinebase
6  * @subpackage  Json
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2007-2015 Metaways Infosystems GmbH (http://www.metaways.de)
10  *
11  */
12
13 /**
14  * Test class for Tinebase_Frontend_Json
15  */
16 class Tinebase_Frontend_JsonTest extends TestCase
17 {
18     /**
19      * unit under test (UIT)
20      * @var Tinebase_Frontend_Json
21      */
22     protected $_instance;
23
24     /**
25      * @var array test objects
26      */
27     protected $_objects = array();
28     
29     /**
30      * set up tests
31      *
32      */
33     public function setUp()
34     {
35         parent::setUp();
36         
37         $this->_instance = new Tinebase_Frontend_Json();
38         
39         $this->_objects['record'] = array(
40             'id'        => 1,
41             'model'     => 'Addressbook_Model_Contact',
42             'backend'    => 'Sql',
43         );
44
45         $this->_objects['group'] = new Tinebase_Model_Group(array(
46             'name'            => 'phpunit test group',
47             'description'    => 'phpunit test group'
48         ));
49
50         $this->_objects['role'] = new Tinebase_Model_Role(array(
51             'name'            => 'phpunit test role',
52             'description'    => 'phpunit test role'
53         ));
54
55         $this->_objects['note'] = new Tinebase_Model_Note(array(
56             'note_type_id'      => 1,
57             'note'              => 'phpunit test note',
58             'record_model'      => $this->_objects['record']['model'],
59             'record_backend'    => $this->_objects['record']['backend'],
60         ));
61     }
62     
63     /**
64      * tear down
65      */
66     public function tearDown()
67     {
68         parent::tearDown();
69         
70         // reset tz in core
71         Tinebase_Core::set(Tinebase_Core::USERTIMEZONE, Tinebase_Core::getPreference()->getValue(Tinebase_Preference::TIMEZONE));
72     }
73     
74     /**
75      * try to add a note type
76      */
77     public function testSearchNotes()
78     {
79         $contact = Addressbook_Controller_Contact::getInstance()->create(new Addressbook_Model_Contact(array('n_family' => 'Schulz')));
80         $note = $this->_objects['note'];
81         $note->record_id = $contact->getId();
82         Tinebase_Notes::getInstance()->addNote($note);
83
84         $filter = array(array(
85             'field' => 'query',
86             'operator' => 'contains',
87             'value' => 'phpunit'
88         ), array(
89             'field' => "record_model",
90             'operator' => "equals",
91             'value' => $this->_objects['record']['model']
92         ), array(
93             'field' => 'record_id',
94             'operator' => 'equals',
95             'value' => $contact->getId()
96         ));
97         $paging = array();
98         
99         $notes = $this->_instance->searchNotes($filter, $paging);
100         
101         $this->assertGreaterThan(0, $notes['totalcount']);
102         $found = false;
103         foreach ($notes['results'] as $note) {
104             if ($this->_objects['note']->note === $note['note']) {
105                 $found = true;
106             }
107         }
108         $this->assertTrue($found, 'note not found in notes: ' . print_r($notes['results'], true));
109         
110         // delete note
111         Tinebase_Notes::getInstance()->deleteNotesOfRecord(
112             $this->_objects['record']['model'],
113             $this->_objects['record']['backend'],
114             $contact->getId()
115         );
116     }
117     
118     /**
119      * try to delete role and then search
120      */
121     public function testSearchRoles()
122     {
123         $role = Tinebase_Acl_Roles::getInstance()->createRole($this->_objects['role']);
124         
125         $filter = array(array(
126             'field'     => 'query',
127             'operator'     => 'contains',
128             'value'     => 'phpunit test role'
129         ));
130         $paging = array(
131             'start'    => 0,
132             'limit'    => 1
133         );
134         
135         $roles = $this->_instance->searchRoles($filter, $paging);
136         
137         $this->assertGreaterThan(0, $roles['totalcount']);
138         $this->assertEquals($this->_objects['role']->name, $roles['results'][0]['name']);
139         
140         // delete role
141         Tinebase_Acl_Roles::getInstance()->deleteRoles($role->id);
142     }
143     
144     /**
145      * test getCountryList
146      *
147      */
148     public function testGetCountryList()
149     {
150         $list = $this->_instance->getCountryList();
151         $this->assertTrue(count($list['results']) > 200);
152     }
153     
154     /**
155      * test get translations
156      *
157      */
158     public function testGetAvailableTranslations()
159     {
160         $list = $this->_instance->getAvailableTranslations();
161         $this->assertTrue(count($list['results']) > 3);
162     }
163     
164     /**
165      * tests locale fallback
166      */
167     public function testSetLocaleFallback()
168     {
169         // de_LU -> de
170         $this->_instance->setLocale('de_LU', FALSE, FALSE);
171         $this->assertEquals('de', (string)Zend_Registry::get('locale'), 'Fallback to generic german did not succseed');
172         
173         $this->_instance->setLocale('zh', FALSE, FALSE);
174         $this->assertEquals('zh_CN', (string)Zend_Registry::get('locale'), 'Fallback to simplified chinese did not succseed');
175         
176         $this->_instance->setLocale('foo_bar', FALSE, FALSE);
177         $this->assertEquals('en', (string)Zend_Registry::get('locale'), 'Exception fallback to english did not succseed');
178     }
179     
180     /**
181      * test set locale and save it in db
182      */
183     public function testSetLocaleAsPreference()
184     {
185         $oldPreference = Tinebase_Core::getPreference()->{Tinebase_Preference::LOCALE};
186         
187         $locale = 'de';
188         $result = $this->_instance->setLocale($locale, TRUE, FALSE);
189         
190         // get config setting from db
191         $preference = Tinebase_Core::getPreference()->{Tinebase_Preference::LOCALE};
192         $this->assertEquals($locale, $preference, "Didn't get right locale preference.");
193         
194         // restore old setting
195         Tinebase_Core::getPreference()->{Tinebase_Preference::LOCALE} = $oldPreference;
196     }
197
198     /**
199      * test set timezone and save it in db
200      */
201     public function testSetTimezoneAsPreference()
202     {
203         $oldPreference = Tinebase_Core::getPreference()->{Tinebase_Preference::TIMEZONE};
204         
205         $timezone = 'America/Vancouver';
206         $result = $this->_instance->setTimezone($timezone, true);
207         
208         // check json result
209         $this->assertEquals($timezone, $result);
210         
211         // get config setting from db
212         $preference = Tinebase_Core::getPreference()->{Tinebase_Preference::TIMEZONE};
213         $this->assertEquals($timezone, $preference, "Didn't get right timezone preference.");
214         
215         // restore old settings
216         Tinebase_Core::set(Tinebase_Core::USERTIMEZONE, $oldPreference);
217         Tinebase_Core::getPreference()->{Tinebase_Preference::TIMEZONE} = $oldPreference;
218     }
219     
220     /**
221      * get notes types
222      */
223     public function testGetNotesTypes()
224     {
225         $noteTypes = $this->_instance->getNoteTypes();
226         $this->assertTrue($noteTypes['totalcount'] >= 5);
227     }
228
229     /**
230      * toogle advanced search preference
231      */
232     public function testAdvancedSearchToogle()
233     {
234         $toogle = $this->_instance->toogleAdvancedSearch(1);
235
236         $this->assertEquals($toogle, 1);
237         $this->assertEquals(Tinebase_Core::getPreference()->getValue(Tinebase_Preference::ADVANCED_SEARCH, 0), 1);
238     }
239
240     /**
241      * search preferences by application
242      *
243      */
244     public function testSearchPreferences()
245     {
246         // search prefs
247         $result = $this->_instance->searchPreferencesForApplication('Tinebase', $this->_getPreferenceFilter());
248         
249         // check results
250         $this->assertTrue(isset($result['results']));
251         $this->assertGreaterThan(2, $result['totalcount']);
252         
253         //check locale/timezones options
254         foreach ($result['results'] as $pref) {
255             switch($pref['name']) {
256                 case Tinebase_Preference::LOCALE:
257                     $this->assertGreaterThan(10, count($pref['options']));
258                     break;
259                 case Tinebase_Preference::TIMEZONE:
260                     $this->assertGreaterThan(100, count($pref['options']));
261                     break;
262             }
263             // check label and description
264             $this->assertTrue(isset($pref['label']) && !empty($pref['label']));
265             $this->assertTrue(isset($pref['description']) && !empty($pref['description']));
266         }
267     }
268
269     /**
270      * search preferences by application
271      *
272      */
273     public function testSearchPreferencesWithOptions()
274     {
275         // add new default pref
276         $pref = $this->_getPreferenceWithOptions();
277         $pref = Tinebase_Core::getPreference()->create($pref);
278         
279         // search prefs
280         $results = $this->_instance->searchPreferencesForApplication('Tinebase', $this->_getPreferenceFilter());
281         
282         // check results
283         $this->assertTrue(isset($results['results']));
284         $this->assertGreaterThan(3, $results['totalcount']);
285         
286         foreach ($results['results'] as $result) {
287             if ($result['name'] == 'defaultapp') {
288                 $this->assertEquals(Tinebase_Model_Preference::DEFAULT_VALUE, $result['value']);
289                 $this->assertTrue(is_array($result['options']));
290                 $this->assertEquals(3, count($result['options']));
291                 $this->assertContains('option1', $result['options'][1][1]);
292             } else if ($result['name'] == Tinebase_Preference::TIMEZONE) {
293                 $this->assertTrue(is_array($result['options'][0]), 'options should be arrays');
294             }
295         }
296         
297         Tinebase_Core::getPreference()->delete($pref);
298     }
299     
300     /**
301      * search preferences of another user
302      *
303      * @todo add check for the case that searching user has no admin rights
304      */
305     public function testSearchPreferencesOfOtherUsers()
306     {
307         // add new default pref
308         $pref = $this->_getPreferenceWithOptions();
309         $pref->account_id   = '2';
310         $pref->account_type = Tinebase_Acl_Rights::ACCOUNT_TYPE_USER;
311         $pref = Tinebase_Core::getPreference()->create($pref);
312         
313         // search prefs
314         $results = $this->_instance->searchPreferencesForApplication('Tinebase', $this->_getPreferenceFilter(TRUE, FALSE, '2'));
315         
316         // check results
317         $this->assertTrue(isset($results['results']));
318         $this->assertEquals(1, $results['totalcount']);
319     }
320     
321     /**
322      * save preferences for user
323      *
324      * @todo add test for saving of other users prefs and acl check
325      */
326     public function testSavePreferences()
327     {
328         $prefData = $this->_getUserPreferenceData();
329         $this->_instance->savePreferences($prefData, false);
330
331         // search saved prefs
332         $results = $this->_instance->searchPreferencesForApplication('Tinebase', $this->_getPreferenceFilter(FALSE));
333         
334         // check results
335         $this->assertTrue(isset($results['results']));
336         $this->assertGreaterThan(2, $results['totalcount']);
337         
338         $savedPrefData = array();
339         foreach ($results['results'] as $result) {
340             if ($result['name'] == 'timezone') {
341                 $savedPrefData['Tinebase'][$result['name']] = array('value' => $result['value']);
342             
343                 $this->assertTrue(is_array($result['options']), 'options missing');
344                 $this->assertGreaterThan(100, count($result['options']));
345             }
346         }
347         $this->assertEquals($prefData, $savedPrefData);
348     }
349     
350     /**
351      * tests if 'use default' appears in options and if it can be selected and if it changes if default changes
352      */
353     public function testGetSetChangeDefaultPref()
354     {
355         $locale = $this->_getLocalePref();
356         foreach ($locale['options'] as $option) {
357             if ($option[0] == Tinebase_Model_Preference::DEFAULT_VALUE) {
358                 $result = $option;
359                 $defaultString = $option[1];
360             }
361         }
362         
363         $this->assertTrue(isset($defaultString));
364         $this->assertContains('(auto)', $defaultString);
365
366         // set user pref to en first then to 'use default'
367         Tinebase_Core::getPreference()->{Tinebase_Preference::LOCALE} = 'en';
368         Tinebase_Core::getPreference()->{Tinebase_Preference::LOCALE} = Tinebase_Model_Preference::DEFAULT_VALUE;
369         $this->assertEquals('auto', Tinebase_Core::getPreference()->{Tinebase_Preference::LOCALE});
370         
371         // set new default locale
372         $prefData['Tinebase']['default' . $locale['id']] = array('value' => 'de', 'type' => 'default', 'name' => Tinebase_Preference::LOCALE);
373         $this->_instance->savePreferences($prefData, true);
374         
375         $updatedLocale = $this->_getLocalePref();
376         foreach ($updatedLocale['options'] as $option) {
377             if ($option[0] == Tinebase_Model_Preference::DEFAULT_VALUE) {
378                 $result = $option;
379                 $defaultString = $option[1];
380             }
381         }
382         $this->assertEquals(count($locale['options']), count($updatedLocale['options']), 'option count has to be equal');
383         $this->assertContains('(Deutsch)', $defaultString);
384         $this->assertEquals('de', Tinebase_Core::getPreference()->{Tinebase_Preference::LOCALE});
385     }
386     
387     /**
388      * get locale pref
389      */
390     protected function _getLocalePref()
391     {
392         $results = $this->_instance->searchPreferencesForApplication('Tinebase', $this->_getPreferenceFilter());
393         foreach ($results['results'] as $result) {
394             if ($result['name'] == Tinebase_Preference::LOCALE) {
395                 $locale = $result;
396             }
397         }
398         
399         $this->assertTrue(isset($locale));
400         
401         return $locale;
402     }
403
404     /**
405      * get admin prefs
406      */
407     public function testGetAdminPreferences()
408     {
409         $this->testGetSetChangeDefaultPref();
410         
411         // set new default locale
412         $locale = $this->_getLocalePref();
413         $prefData['Tinebase'][$locale['id']] = array('value' => 'de', 'type' => 'default', 'name' => Tinebase_Preference::LOCALE);
414         $this->_instance->savePreferences($prefData, true);
415         
416         // check as admin
417         $results = $this->_instance->searchPreferencesForApplication('Tinebase', $this->_getPreferenceFilter(FALSE, TRUE));
418         foreach ($results['results'] as $pref) {
419             if ($pref['name'] !== Tinebase_Preference::LOCALE) {
420                 $this->assertEquals(Tinebase_Model_Preference::DEFAULT_VALUE, $pref['value']);
421             } else {
422                 $this->assertEquals(Tinebase_Model_Preference::TYPE_ADMIN, $pref['type']);
423                 $this->assertEquals('de', $pref['value'], print_r($pref, TRUE));
424             }
425         }
426
427         // check as user
428         $locale = $this->_getLocalePref();
429         $this->assertEquals(Tinebase_Model_Preference::TYPE_ADMIN, $locale['type'], 'pref should be of type admin: ' . print_r($locale, TRUE));
430         $this->assertEquals(Tinebase_Model_Preference::DEFAULT_VALUE, $locale['value'], 'pref should be default value: ' . print_r($locale, TRUE));
431     }
432     
433     /**
434      * save admin prefs
435      *
436      */
437     public function testSaveAdminPreferences()
438     {
439         // add new default pref
440         $pref = $this->_getPreferenceWithOptions();
441         $pref = Tinebase_Core::getPreference()->create($pref);
442         
443         $prefData = array();
444         $prefData['Tinebase'][$pref->getId()] = array('value' => 'test', 'type' => 'forced');
445         $this->_instance->savePreferences($prefData, true);
446
447         // search saved prefs
448         $results = $this->_instance->searchPreferencesForApplication('Tinebase', $this->_getPreferenceFilter(TRUE));
449         
450         // check results
451         $this->assertTrue(isset($results['results']));
452         $this->assertEquals(1, $results['totalcount']);
453         $this->assertEquals($prefData['Tinebase'][$pref->getId()]['value'], $results['results'][0]['value']);
454         $this->assertEquals($prefData['Tinebase'][$pref->getId()]['type'], $results['results'][0]['type']);
455     }
456     
457     /**
458      * save state and load it with registry data
459      */
460     public function testSaveAndGetState()
461     {
462         $testData = array(
463             'bla'   => 'blubb',
464             'zzing' => 'zzang'
465         );
466         
467         foreach ($testData as $key => $value) {
468             Tinebase_State::getInstance()->setState($key, $value);
469         }
470         
471         $stateInfo = Tinebase_State::getInstance()->loadStateInfo();
472         
473         $this->assertEquals($testData, $stateInfo);
474     }
475     
476     /**
477      * test get all registry data
478      *
479      * @return void
480      *
481      * @see 0007934: change pw button active even if it is not allowed
482      * @see 0008310: apps should be sorted the other way round in menu
483      * @see 0009130: Can't open login page on Ubuntu "due to a temporary overloading"
484      */
485     public function testGetAllRegistryData()
486     {
487         $registryData = $this->_instance->getAllRegistryData();
488         $currentUser = Tinebase_Core::getUser();
489         
490         $this->assertEquals($currentUser->toArray(), $registryData['Tinebase']['currentAccount']);
491         $this->assertEquals(
492             Addressbook_Controller_Contact::getInstance()->getContactByUserId($currentUser->getId())->toArray(),
493             $registryData['Tinebase']['userContact']
494         );
495         $this->assertEquals(TRUE, $registryData['Tinebase']['config']['changepw']['value'], 'changepw should be TRUE');
496         
497         Tinebase_Config::getInstance()->set('changepw', 0);
498         $registryData = $this->_instance->getAllRegistryData();
499         $changepwValue = $registryData['Tinebase']['config']['changepw']['value'];
500         $this->assertEquals(FALSE, $changepwValue, 'changepw should be (bool) false');
501         $this->assertTrue(is_bool($changepwValue), 'changepw should be (bool) false: ' . var_export($changepwValue, TRUE));
502         
503         $userApps = $registryData['Tinebase']['userApplications'];
504         $this->assertEquals('Admin', $userApps[0]['name'], 'first app should be Admin: ' . print_r($userApps, TRUE));
505         
506         $locale = Tinebase_Core::getLocale();
507         $symbols = Zend_Locale::getTranslationList('symbols', $locale);
508         $this->assertEquals($symbols['decimal'], $registryData['Tinebase']['decimalSeparator']);
509     }
510
511     /**
512      * test get all registry data with persistent filters
513      * 
514      * @return void
515      * 
516      * @see 0009610: shared favorites acl
517      */
518     public function testGetAllPersistentFilters()
519     {
520         $this->markTestSkipped('@see 0010192: fix persistent filter tests');
521         
522         $registryData = $this->_instance->getAllRegistryData();
523         
524         $filterData = $registryData['Tinebase']['persistentFilters'];
525         $this->assertTrue($filterData['totalcount'] > 10);
526         $this->assertTrue(isset($filterData['results'][0]['grants']), 'grants are missing');
527         $grants = $filterData['results'][0]['grants'];
528         $this->assertTrue($grants[0]['readGrant']);
529         
530         // check if accounts are resolved
531         $this->assertTrue(is_array($grants[0]['account_name']), 'account should be resolved: ' . print_r($grants[0], true));
532     }
533     
534     /**
535      * testGetUserProfile
536      */
537     public function testGetUserProfile()
538     {
539         $profile = $this->_instance->getUserProfile(Tinebase_Core::getUser()->getId());
540
541         $this->assertTrue(is_array($profile));
542         $this->assertTrue((isset($profile['userProfile']) || array_key_exists('userProfile', $profile)));
543         $this->assertTrue(is_array($profile['userProfile']));
544         $this->assertTrue((isset($profile['readableFields']) || array_key_exists('readableFields', $profile)));
545         $this->assertTrue(is_array($profile['readableFields']));
546         $this->assertTrue((isset($profile['updateableFields']) || array_key_exists('updateableFields', $profile)));
547         $this->assertTrue(is_array($profile['updateableFields']));
548         
549         // try to get user profile of different user
550         $this->setExpectedException('Tinebase_Exception_AccessDenied');
551         
552         $sclever = Tinebase_Helper::array_value('sclever',Zend_Registry::get('personas'));
553         $this->_instance->getUserProfile($sclever->getId());
554     }
555     
556     /**
557      * testGetUserProfileConfig
558      */
559     public function testGetUserProfileConfig()
560     {
561         $config = $this->_instance->getUserProfileConfig();
562         
563         $this->assertTrue(is_array($config));
564         $this->assertTrue((isset($config['possibleFields']) || array_key_exists('possibleFields', $config)));
565         $this->assertTrue(is_array($config['possibleFields']));
566         $this->assertTrue((isset($config['readableFields']) || array_key_exists('readableFields', $config)));
567         $this->assertTrue(is_array($config['readableFields']));
568         $this->assertTrue((isset($config['updateableFields']) || array_key_exists('updateableFields', $config)));
569         $this->assertTrue(is_array($config['updateableFields']));
570     }
571     
572     /**
573      * testSetUserProfileConfig
574      */
575     public function testSetUserProfileConfig()
576     {
577         $config = $this->_instance->getUserProfileConfig();
578         
579         $idx = array_search('n_prefix', $config['readableFields']);
580         if ($idx !== false) {
581             unset ($config['readableFields'][$idx]);
582         }
583         
584         $idx = array_search('tel_home', $config['updateableFields']);
585         if ($idx !== false) {
586             unset ($config['updateableFields'][$idx]);
587         }
588         
589         $this->_instance->setUserProfileConfig($config);
590     }
591     
592     /**
593      * testupdateUserProfile
594      */
595     public function testUpdateUserProfile()
596     {
597         $profile = $this->_instance->getUserProfile(Tinebase_Core::getUser()->getId());
598         $profileData = $profile['userProfile'];
599         
600         $this->assertFalse(array_search('n_prefix', $profileData));
601         
602         $profileData['tel_home'] = 'mustnotchange';
603         $profileData['email_home'] = 'email@userprofile.set';
604         
605         $this->_instance->updateUserProfile($profileData);
606         
607         $updatedProfile = $this->_instance->getUserProfile(Tinebase_Core::getUser()->getId());
608         $updatedProfileData = $updatedProfile['userProfile'];
609         $this->assertNotEquals('mustnotchange', $updatedProfileData['tel_home']);
610         $this->assertEquals('email@userprofile.set', $updatedProfileData['email_home']);
611     }
612     
613     /**
614      * testGetSaveApplicationConfig
615      */
616     public function testGetSaveApplicationConfig()
617     {
618         $config = $this->_instance->getConfig('Admin');
619         $this->assertGreaterThan(0, count($config));
620         
621         $data = array(
622             'id'        => 'Admin',
623             'settings'  => Admin_Controller::getInstance()->getConfigSettings(),
624         );
625         
626         $newConfig = $this->_instance->saveConfig($data);
627         
628         $this->assertEquals($config, $newConfig);
629     }
630     
631     /**
632      * testChangeUserAccount
633      * 
634      * @see 0009984: allow to change user role
635      */
636     public function testChangeUserAccount()
637     {
638         // allow test user to sign in as sclever
639         Tinebase_Config::getInstance()->set(Tinebase_Config::ROLE_CHANGE_ALLOWED, new Tinebase_Config_Struct(array(
640             Tinebase_Core::getUser()->accountLoginName => array('sclever')
641         )));
642         
643         $sclever = $this->_personas['sclever'];
644         $result = $this->_instance->changeUserAccount('sclever');
645         
646         $this->assertEquals(array('success' => true), $result);
647         
648         // make sure, we are sclever
649         $this->assertEquals('sclever', Tinebase_Core::getUser()->accountLoginName);
650         $this->assertEquals('sclever', Tinebase_Session::getSessionNamespace()->currentAccount->accountLoginName);
651         
652         // reset to original user
653         Tinebase_Controller::getInstance()->initUser($this->_originalTestUser, /* $fixCookieHeader = */ false);
654         Tinebase_Session::getSessionNamespace()->userAccountChanged = false;
655     }
656     
657     /**
658      * testOmitPersonalTagsOnSearch
659      * 
660      * @see 0010732: add "use personal tags" right to all applications
661      */
662     public function testOmitPersonalTagsOnSearch()
663     {
664         $personalTag = $this->_getTag(Tinebase_Model_Tag::TYPE_PERSONAL);
665         Tinebase_Tags::getInstance()->createTag($personalTag);
666         
667         $this->_removeRoleRight('Addressbook', Tinebase_Acl_Rights::USE_PERSONAL_TAGS);
668         $filter = array(
669             'application' => 'Addressbook',
670             'grant' => Tinebase_Model_TagRight::VIEW_RIGHT,
671             'type' => Tinebase_Model_Tag::TYPE_PERSONAL
672         );
673         $result = $this->_instance->searchTags($filter, array());
674         
675         $this->assertEquals(0, $result['totalCount']);
676     }
677     
678     /******************** protected helper funcs ************************/
679     
680     /**
681      * get preference filter
682      *
683      * @param bool $_savedPrefs
684      * @return array
685      */
686     protected function _getPreferenceFilter($_savedPrefs = FALSE, $_adminPrefs = FALSE, $_userId = NULL)
687     {
688         if ($_userId === NULL) {
689             $_userId = Tinebase_Core::getUser()->getId();
690         }
691         
692         $result = array(
693             array(
694                 'field' => 'account',
695                 'operator' => 'equals',
696                 'value' => array(
697                     'accountId'     => ($_adminPrefs) ? 0 : $_userId,
698                     'accountType'   => ($_adminPrefs)
699                         ? Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE
700                         : Tinebase_Acl_Rights::ACCOUNT_TYPE_USER
701                 )
702             )
703         );
704
705         if ($_savedPrefs) {
706             $result[] = array(
707                 'field' => 'name',
708                 'operator' => 'contains',
709                 'value' => 'defaultapp'
710             );
711         }
712         
713         return $result;
714     }
715
716     /**
717      * get preference data for testSavePreferences()
718      *
719      * @return array
720      */
721     protected function _getUserPreferenceData()
722     {
723         return array(
724             'Tinebase' => array(
725                 'timezone' => array('value' => 'Europe/Amsterdam'),
726             )
727         );
728     }
729     
730     /**
731      * get preference with options
732      *
733      * @return Tinebase_Model_Preference
734      */
735     protected function _getPreferenceWithOptions()
736     {
737         return new Tinebase_Model_Preference(array(
738             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('Tinebase')->getId(),
739             'name'              => 'defaultapp',
740             'value'             => 'value1',
741             'account_id'        => '0',
742             'account_type'      => Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE,
743             'type'              => Tinebase_Model_Preference::TYPE_ADMIN,
744             'options'           => '<?xml version="1.0" encoding="UTF-8"?>
745                 <options>
746                     <option>
747                         <label>option1</label>
748                         <value>value1</value>
749                     </option>
750                     <option>
751                         <label>option2</label>
752                         <value>value2</value>
753                     </option>
754                 </options>'
755         ));
756     }
757 }