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