diff --git a/admin/inc/class.admin_cmd_edit_preferences.inc.php b/admin/inc/class.admin_cmd_edit_preferences.inc.php index 8dff565d11..2014d0e098 100644 --- a/admin/inc/class.admin_cmd_edit_preferences.inc.php +++ b/admin/inc/class.admin_cmd_edit_preferences.inc.php @@ -80,11 +80,11 @@ class admin_cmd_edit_preferences extends admin_cmd { if (!isset($value) || $value === '') { - $prefs->delete($this->app, $name, in_array($this->pref, 'default', 'forced') ? $this->pref : 'user'); + $prefs->delete($this->app, $name, in_array($this->pref, array('default', 'forced')) ? $this->pref : 'user'); } else { - $prefs->add($this->app, $name, $value, in_array($this->pref, 'default', 'forced') ? $this->pref : 'user'); + $prefs->add($this->app, $name, $value, in_array($this->pref, array('default', 'forced')) ? $this->pref : 'user'); } } $prefs->save_repository(true, $this->pref); diff --git a/admin/tests/PreferencesCommandTest.php b/admin/tests/PreferencesCommandTest.php new file mode 100644 index 0000000000..a862e8eb82 --- /dev/null +++ b/admin/tests/PreferencesCommandTest.php @@ -0,0 +1,300 @@ +preference_name) + { + Api\Preferences::delete_preference(static::APP, $this->preference_name, 'user'); + Api\Preferences::delete_preference(static::APP, $this->preference_name, 'default'); + Api\Preferences::delete_preference(static::APP, $this->preference_name, 'forced'); + } + + Api\Cache::unsetInstance(Api\Preferences::class, 'forced'); + Api\Cache::unsetInstance(Api\Preferences::class, 'default'); + Api\Cache::unsetInstance(Api\Preferences::class, $GLOBALS['egw_info']['user']['account_id']); + parent::tearDown(); + } + + /** + * Test that adding a preference works + * + * @dataProvider typeDataProvider + */ + public function testAddPreference($type) + { + // Set up + $log_count = $this->get_log_count(); + $account = $type == 'group' ? $GLOBALS['egw']->accounts->name2id('Default') : $GLOBALS['egw_info']['user']['account_id']; + $pre_pref = new Api\Preferences($GLOBALS['egw_info']['user']['account_id']); + $pre = $pre_pref->read(static::APP); + $this->assertArrayNotHasKey($this->preference_name, $pre); + + $set = array($this->preference_name => 'Yes'); + + // Execute + $command = new admin_cmd_edit_preferences($account, $type, static::APP, $set); + $command->comment = 'Needed for unit test ' . $this->getName() . " with type $type"; + $command->run(); + + // Check + $post_pref = new Api\Preferences($GLOBALS['egw_info']['user']['account_id']); + $post = $post_pref->read_repository(false); + // At user level + $this->assertArrayHasKey($this->preference_name, $post[static::APP]); + $this->assertEquals($set[$this->preference_name], $post[static::APP][$this->preference_name]); + // At type level + $this->assertArrayHasKey($this->preference_name, $post_pref->$type[static::APP]); + $this->assertEquals($set[$this->preference_name], $post_pref->$type[static::APP][$this->preference_name]); + + $this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log"); + } + + /** + * Try to change an existing preference + * + * We check changing the various combinations with a default set. + * All should give a change. + * + * @dataProvider typeDataProvider + */ + public function testChangeWithDefault($type) + { + // Set up + $log_count = $this->get_log_count(); + $account = $GLOBALS['egw_info']['user']['account_id']; + + $set = array($this->preference_name => 'Changed'); + $old = array($this->preference_name => $type . ' original value'); + + $prefs = new Api\Preferences('default'); + $prefs->read_repository(false); + $prefs->add(static::APP, $this->preference_name, 'default original value','default'); + $prefs->save_repository('default'); + + $prefs = new Api\Preferences(in_array($type, array('default', 'forced')) ? $type : $account); + $prefs->read_repository(false); + $prefs->add(static::APP, $this->preference_name, $old[$this->preference_name], $type); + $prefs->save_repository($type); + + // Execute + $command = new admin_cmd_edit_preferences($account, $type, static::APP, $set, $old); + $command->comment = 'Needed for unit test ' . $this->getName(); + $command->run(); + + // Check + $post_pref = new Api\Preferences($account); + $post = $post_pref->read_repository(false); + + // At user level + $this->assertArrayHasKey($this->preference_name, $post[static::APP]); + $this->assertEquals($set[$this->preference_name], $post[static::APP][$this->preference_name]); + + // At type level + $this->assertArrayHasKey($this->preference_name, $post_pref->$type[static::APP]); + $this->assertEquals($set[$this->preference_name], $post_pref->$type[static::APP][$this->preference_name]); + + $this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log"); + } + + /** + * Try to change an existing preference + * + * We check changing the various combinations with a default set. + * Only forced should give a change. + * + * @dataProvider typeDataProvider + */ + public function testChangeWithForced($type) + { + // Set up + $log_count = $this->get_log_count(); + $account = $GLOBALS['egw_info']['user']['account_id']; + + $set = array($this->preference_name => 'Changed '. $type); + $old = array($this->preference_name => $type . ' original value'); + + $prefs = new Api\Preferences('forced'); + $prefs->read_repository(false); + $prefs->add(static::APP, $this->preference_name, 'forced original value','forced'); + $prefs->save_repository(false, 'forced'); + + $prefs = new Api\Preferences(in_array($type, array('default', 'forced')) ? $type : $account); + $prefs->read_repository(false); + $prefs->add(static::APP, $this->preference_name, $old[$this->preference_name], $type); + $prefs->save_repository(false, $type); + + // Execute + $command = new admin_cmd_edit_preferences($account, $type, static::APP, $set, $old); + $command->comment = 'Needed for unit test ' . $this->getName(); + $command->run(); + + // Check + $post_pref = new Api\Preferences($account); + $post = $post_pref->read_repository(false); + + // At user level + $this->assertArrayHasKey($this->preference_name, $post[static::APP]); + $this->assertEquals($type != 'forced' ? 'forced original value' : $set[$this->preference_name], $post[static::APP][$this->preference_name], + "$type preference overrode forced preference" + ); + + // At type level + $this->assertArrayHasKey($this->preference_name, $post_pref->$type[static::APP]); + $this->assertEquals($set[$this->preference_name], $post_pref->$type[static::APP][$this->preference_name]); + + $this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log"); + } + + /** + * Try to change an existing preference + * + * We check changing the various combinations, some of which should change + * + * @dataProvider typeChangeDataProvider + */ + public function testChange($type, $check, $change) + { + // Set up + $log_count = $this->get_log_count(); + $account = $GLOBALS['egw_info']['user']['account_id']; + + + $set = array($this->preference_name => $type . ' changed'); + $old = array($this->preference_name => $type . ' original value'); + $check_original = $check . ' original value'; + //echo "\n".__METHOD__ . "($type, $check, $change) Change $type but $check should " . ($change ? '' : 'not ') . "change ( " . ($change ? $set[$this->preference_name] : $check_original).")\n"; + + $prefs = new Api\Preferences(in_array($type, array('default', 'forced')) ? $type : $account); + $prefs->read_repository(false); + $prefs->add(static::APP, $this->preference_name, $old[$this->preference_name], $type); + $prefs->save_repository(False, $type); + + $prefs = new Api\Preferences(in_array($check, array('default', 'forced')) ? $check : $account); + $prefs->read_repository(false); + $prefs->add(static::APP, $this->preference_name, $check_original, $check); + $prefs->save_repository(False, $check); + + // Execute + $command = new admin_cmd_edit_preferences($account, $type, static::APP, $set, $old); + $command->comment = 'Needed for unit test ' . $this->getName(); + $command->run(); + + // Check + $post_pref = new Api\Preferences($account); + $post = $post_pref->read_repository(false); + + // At type level - should always be what we set + $this->assertArrayHasKey($this->preference_name, $post_pref->$type[static::APP]); + $this->assertEquals($set[$this->preference_name], $post_pref->$type[static::APP][$this->preference_name]); + + // At user level - depends on type priority + $this->assertArrayHasKey($this->preference_name, $post[static::APP]); + $this->assertEquals($change ? $set[$this->preference_name] : $check_original, $post[static::APP][$this->preference_name]); + + + $this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log"); + } + + /** + * Try to delete an existing preference + * + * We check the various combinations, such as deleting a default when the user + * has a value. + * + * @dataProvider typeChangeDataProvider + */ + public function testDeletePreference($type, $check, $override) + { + // Set up + $log_count = $this->get_log_count(); + $account = $type == 'group' ? $GLOBALS['egw']->accounts->name2id('Default') : $GLOBALS['egw_info']['user']['account_id']; + + $set = array($this->config_name => null); + $old = array($this->config_name => 'It will log whatever'); + + $prefs = new Api\Preferences(in_array($type, 'default', 'forced') ? $type : $account); + $prefs->add(static::APP, $this->config_name, $old[$this->config_name], in_array($type, array('default', 'forced')) ? $type : 'user'); + $prefs->save_repository(false, $type); + + + // Execute + $command = new admin_cmd_edit_preferences($account, $type, static::APP, $set, $old); + $command->comment = 'Needed for unit test ' . $this->getName(); + $command->run(); + + // Check + $account = $check == 'group' ? $GLOBALS['egw']->accounts->name2id('Default') : $GLOBALS['egw_info']['user']['account_id']; + $post_pref = new Api\Preferences($account); + $post = $post_pref->read_repository(); + + // At user level + $this->assertEquals($override ? $set[$this->preference_name] : $old[$this->preference_name], $post[static::APP][$this->preference_name]); + + // At type level + $this->assertNull($post_pref->$check[static::APP][$this->preference_name]); + + $this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log"); + } + + + /** + * Give a list of preference levels (types) so we can check them all + * They are in priority order. + */ + public function typeDataProvider() { + return array( + ['default'], + ['user'], + //['group'], Not really supported yet + ['forced'] + ); + } + + /** + * Get a list of preference levels and if they should be allowed to change + * each other + */ + public function typeChangeDataProvider() { + $levels = array( + // Change and this should/should not change + ['default', 'user', false], + ['default', 'forced', false], + ['user', 'default', true], + ['user', 'forced', false], + ['forced', 'user', true], + ['forced', 'default', true], + ); + + + return $levels; + } +} \ No newline at end of file