OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Merge pull request #12839 from opf/bug/41275-perform-language-reset-on-setting-save
Christophe Bliard8e2abcba655f
diff --git a/app/controllers/admin/settings/languages_settings_controller.rb b/app/controllers/admin/settings/languages_settings_controller.rbindex bee2dca0699..8851ee2a130 100644--- a/app/controllers/admin/settings/languages_settings_controller.rb+++ b/app/controllers/admin/settings/languages_settings_controller.rb@@ -33,5 +33,11 @@ module Admin::Settingsdef default_breadcrumbt(:label_languages)end++ protected++ def update_service+ ::Settings::LanguageUpdateService+ endendenddiff --git a/app/services/settings/language_update_service.rb b/app/services/settings/language_update_service.rbnew file mode 100644index 00000000000..f9c44b7d5a7--- /dev/null+++ b/app/services/settings/language_update_service.rb@@ -0,0 +1,45 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# +++#++class Settings::LanguageUpdateService < Settings::UpdateService+ def after_perform(call)+ force_users_to_use_only_available_languages++ call+ end++ private++ def force_users_to_use_only_available_languages+ User.where.not(language: Setting.available_languages)+ .update_all(language: Setting.default_language)+ end+enddiff --git a/app/views/admin/settings/languages_settings/show.html.erb b/app/views/admin/settings/languages_settings/show.html.erbindex e23c7ea9440..1bc39ab44a8 100644--- a/app/views/admin/settings/languages_settings/show.html.erb+++ b/app/views/admin/settings/languages_settings/show.html.erb@@ -39,9 +39,3 @@ See COPYRIGHT and LICENSE files for more details.<%= styled_button_tag t(:button_save), class: '-highlight -with-icon icon-checkmark' %><% end %>--<div style="float:right">- <%= link_to t(:label_force_user_language_to_default),- { controller: '/admin', action: 'force_user_language' },- method: :post %>-</div>diff --git a/config/routes.rb b/config/routes.rbindex a192c89f737..daf597c6440 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -325,7 +325,6 @@ OpenProject::Application.routes.draw docollection doget :pluginsget :info- post :force_user_languagepost :test_emailendenddiff --git a/spec/controllers/admin/settings/date_format_settings_controller_spec.rb b/spec/controllers/admin/settings/date_format_settings_controller_spec.rbnew file mode 100644index 00000000000..f8b6856c512--- /dev/null+++ b/spec/controllers/admin/settings/date_format_settings_controller_spec.rb@@ -0,0 +1,71 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# +++#++require 'spec_helper'++RSpec.describe Admin::Settings::DateFormatSettingsController do+ shared_let(:user) { create(:admin) }+ current_user { user }++ describe 'PATCH #update' do+ subject { patch 'update', params: }++ let(:start_of_week) { 1 }+ let(:first_week_of_year) { 1 }+ let(:date_format) { Settings::Definition[:date_format].allowed.first }+ let(:time_format) { Settings::Definition[:time_format].allowed.first }+ let(:base_settings) do+ { start_of_week:, first_week_of_year:, date_format:, time_format: }+ end+ let(:params) { { settings: } }++ shared_examples 'invalid combination of start_of_week and first_week_of_year' do |missing_param:|+ provided_param = (%i[start_of_week first_week_of_year] - [missing_param]).first++ context "when setting only #{provided_param} but not #{missing_param}" do+ let(:settings) { base_settings.except(missing_param) }++ it 'redirects and sets the flash error' do+ subject++ expect(response).to redirect_to action: :show+ expect(flash[:error])+ .to eq(I18n.t('settings.display.first_date_of_week_and_year_set',+ first_week_setting_name: I18n.t(:setting_first_week_of_year),+ day_of_week_setting_name: I18n.t(:setting_start_of_week)))+ end+ end+ end++ include_examples 'invalid combination of start_of_week and first_week_of_year', missing_param: :first_week_of_year+ include_examples 'invalid combination of start_of_week and first_week_of_year', missing_param: :start_of_week+ end+enddiff --git a/spec/controllers/admin/settings/languages_settings_controller_spec.rb b/spec/controllers/admin/settings/languages_settings_controller_spec.rbindex a0326a01bbd..7aa24005175 100644--- a/spec/controllers/admin/settings/languages_settings_controller_spec.rb+++ b/spec/controllers/admin/settings/languages_settings_controller_spec.rb@@ -56,12 +56,8 @@ RSpec.describe Admin::Settings::LanguagesSettingsController dosubject { patch 'update', params: }let(:available_languages) { %w[en fr de] }- let(:start_of_week) { 1 }- let(:first_week_of_year) { 1 }- let(:date_format) { Settings::Definition[:date_format].allowed.first }- let(:time_format) { Settings::Definition[:time_format].allowed.first }let(:base_settings) do- { available_languages:, start_of_week:, first_week_of_year:, date_format:, time_format: }+ { available_languages: }endlet(:params) { { settings: } }@@ -92,26 +88,5 @@ RSpec.describe Admin::Settings::LanguagesSettingsController doexpect(user_ja.reload.language).to eq('ja')endend-- shared_examples 'invalid combination of start_of_week and first_week_of_year' do |missing_param:|- provided_param = (%i[start_of_week first_week_of_year] - [missing_param]).first-- context "when setting only #{provided_param} but not #{missing_param}" do- let(:settings) { base_settings.except(missing_param) }-- it 'redirects and sets the flash error' do- subject-- expect(response).to redirect_to action: :show- expect(flash[:error])- .to eq(I18n.t('settings.display.first_date_of_week_and_year_set',- first_week_setting_name: I18n.t(:setting_first_week_of_year),- day_of_week_setting_name: I18n.t(:setting_start_of_week)))- end- end- end-- include_examples 'invalid combination of start_of_week and first_week_of_year', missing_param: :first_week_of_year- include_examples 'invalid combination of start_of_week and first_week_of_year', missing_param: :start_of_weekendenddiff --git a/spec/controllers/admin/settings/projects_settings_controller_spec.rb b/spec/controllers/admin/settings/projects_settings_controller_spec.rbindex f639ebf1b8c..07c82630bce 100644--- a/spec/controllers/admin/settings/projects_settings_controller_spec.rb+++ b/spec/controllers/admin/settings/projects_settings_controller_spec.rb@@ -29,26 +29,17 @@require 'spec_helper'RSpec.describe Admin::Settings::ProjectsSettingsController do- before do- allow(@controller).to receive(:set_localization)- @params = {}+ shared_let(:user) { create(:admin) }+ current_user { user }- @user = create(:admin)- allow(User).to receive(:current).and_return @user+ before do+ allow(controller).to receive(:set_localization)end- describe 'show' do+ describe 'GET #show' dorender_views- context 'default project modules' do- before do- @previous_projects_modules = Setting.default_projects_modules- end-- after do- Setting.default_projects_modules = @previous_projects_modules- end-+ describe 'default project modules' doit 'contains a check box for the activity module on the projects tab' doget 'show', params: { tab: 'projects' }@@ -57,15 +48,7 @@ RSpec.describe Admin::Settings::ProjectsSettingsController doexpect(response.body).to have_selector "input[@name='settings[default_projects_modules][]'][@value='activity']"end- it 'contains a check box for the activity module on the projects tab' do- get 'show', params: { tab: 'projects' }-- expect(response).to be_successful- expect(response).to render_template 'admin/settings/projects_settings/show'- expect(response.body).to have_selector "input[@name='settings[default_projects_modules][]'][@value='activity']"- end-- describe 'without activated activity module' do+ context 'without activated activity module' dobefore doSetting.default_projects_modules = %w[wiki]end@@ -80,9 +63,9 @@ RSpec.describe Admin::Settings::ProjectsSettingsController doendend- describe 'with activity in Setting.default_projects_modules' do+ context 'with activity in Setting.default_projects_modules' dobefore do- Setting.default_projects_modules = %w[activity wiki]+ Setting.default_projects_modules = %w[activity]endit 'contains a checked checkbox for activity' do@@ -97,18 +80,10 @@ RSpec.describe Admin::Settings::ProjectsSettingsController doendend- describe 'update' do+ describe 'PATCH #update' dorender_views- context 'default project modules' do- before do- @previous_projects_modules = Setting.default_projects_modules- end-- after do- Setting.default_projects_modules = @previous_projects_modules- end-+ describe 'default project modules' doit 'does not store the activity in the default_projects_modules if unchecked' dopatch 'update',params: {@@ -141,17 +116,6 @@ RSpec.describe Admin::Settings::ProjectsSettingsController doenddescribe 'password settings' do- let(:old_settings) do- {- password_min_length: 10,- password_active_rules: [],- password_min_adhered_rules: 0,- password_days_valid: 365,- password_count_former_banned: 2,- lost_password: true- }- end-let(:new_settings) do{password_min_length: 42,@@ -163,26 +127,22 @@ RSpec.describe Admin::Settings::ProjectsSettingsController do}end- let(:original_settings) { Hash.new }-before do- old_settings.keys.each do |key|- original_settings[key] = Setting[key]- end-- old_settings.keys.each do |key|- Setting[key] = old_settings[key]- end- end+ old_settings = {+ password_min_length: 10,+ password_active_rules: [],+ password_min_adhered_rules: 0,+ password_days_valid: 365,+ password_count_former_banned: 2,+ lost_password: true+ }- after do- # restore settings- old_settings.keys.each do |key|- Setting[key] = original_settings[key]+ old_settings.each do |key, value|+ Setting[key] = valueendend- describe 'PATCH #update with password login enabled' do+ context 'with password login enabled' dobefore doallow(OpenProject::Configuration).to receive(:disable_password_login?).and_return(false)@@ -198,7 +158,7 @@ RSpec.describe Admin::Settings::ProjectsSettingsController doendit 'sets the active character classes to lowercase and uppercase' do- expect(Setting[:password_active_rules]).to eq ['uppercase', 'lowercase']+ expect(Setting[:password_active_rules]).to eq %w[uppercase lowercase]endit 'sets the required number of classes to 7' do@@ -218,7 +178,7 @@ RSpec.describe Admin::Settings::ProjectsSettingsController doendend- describe 'PATCH #update with password login disabled' do+ describe 'with password login disabled' dobefore doallow(OpenProject::Configuration).to receive(:disable_password_login?).and_return(true)diff --git a/spec/routing/admin_spec.rb b/spec/routing/admin_spec.rbindex 39c8a054e28..0853b5ea57f 100644--- a/spec/routing/admin_spec.rb+++ b/spec/routing/admin_spec.rb@@ -49,11 +49,6 @@ RSpec.describe 'admin routes' do.to route_to('admin#info')end- it 'connects POST /admin/force_user_language to admin#force_user_language' do- expect(post('/admin/force_user_language'))- .to route_to('admin#force_user_language')- end-it 'connects POST /admin/test_email to admin#test_email' doexpect(post('/admin/test_email')).to route_to('admin#test_email')diff --git a/spec/services/settings/language_update_service_spec.rb b/spec/services/settings/language_update_service_spec.rbnew file mode 100644index 00000000000..67b8432f412--- /dev/null+++ b/spec/services/settings/language_update_service_spec.rb@@ -0,0 +1,65 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# +++#++require 'spec_helper'++RSpec.describe Settings::LanguageUpdateService do+ let(:service) do+ described_class.new(user: build_stubbed(:admin))+ end+ let(:available_languages) { %w[de fr] }++ before do+ allow(service).to receive(:force_users_to_use_only_available_languages)+ end++ it 'sets language of users having a non-available language to the default language' do+ service.call(available_languages:)++ expect(service)+ .to have_received(:force_users_to_use_only_available_languages)+ end++ context 'when the contract is not successfully validated' do+ before do+ allow(service)+ .to receive(:validate_contract)+ .and_return(ServiceResult.failure(message: 'fake error'))+ end++ it 'does not change language of any users' do+ service.call(available_languages:)++ expect(service)+ .not_to have_received(:force_users_to_use_only_available_languages)+ end+ end+enddiff --git a/spec/services/settings/shared/shared_setup_context.rb b/spec/services/settings/shared/shared_setup_context.rbnew file mode 100644index 00000000000..94f6a10c477--- /dev/null+++ b/spec/services/settings/shared/shared_setup_context.rb@@ -0,0 +1,76 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# +++#++require 'spec_helper'++RSpec.shared_context 'with update service setup' do+ let(:instance) do+ described_class.new(user:)+ end+ let(:user) { build_stubbed(:user) }+ let(:contract) do+ instance_double(Settings::UpdateContract,+ validate: contract_success,+ errors: instance_double(ActiveModel::Error))+ end+ let(:contract_success) { true }+ let(:setting_definition) do+ instance_double(Settings::Definition)+ end+ let(:setting_name) { :a_setting_name }+ let(:new_setting_value) { 'a_new_setting_value' }+ let(:previous_setting_value) { 'the_previous_setting_value' }+ let(:params) { { setting_name => new_setting_value } }++ before do+ # stub a setting definition+ allow(Settings::Definition)+ .to receive(:[])+ .and_call_original+ allow(Settings::Definition)+ .to receive(:[])+ .with(setting_name)+ .and_return(setting_definition)+ allow(Setting)+ .to receive(:[])+ .and_call_original+ allow(Setting)+ .to receive(:[]).with(setting_name)+ .and_return(previous_setting_value)+ allow(Setting)+ .to receive(:[]=)++ # stub contract+ allow(Settings::UpdateContract)+ .to receive(:new)+ .and_return(contract)+ end+enddiff --git a/spec/services/settings/update_service_spec.rb b/spec/services/settings/update_service_spec.rbindex f1d70bf19d7..abff0b6e38e 100644--- a/spec/services/settings/update_service_spec.rb+++ b/spec/services/settings/update_service_spec.rb@@ -26,53 +26,11 @@require 'spec_helper'require_relative 'shared/shared_call_examples'+require_relative 'shared/shared_setup_context'-RSpec.describe Settings::UpdateService do- let(:instance) do- described_class.new(user:)- end- let(:user) { build_stubbed(:user) }- let(:contract) do- instance_double(Settings::UpdateContract,- validate: contract_success,- errors: instance_double(ActiveModel::Error))- end- let(:contract_success) { true }- let(:setting_definition) do- instance_double(Settings::Definition)- end- let(:definition_on_change) do- instance_double(Proc,- call: nil)- end- let(:setting_name) { :a_setting_name }- let(:new_setting_value) { 'a_new_setting_value' }- let(:previous_setting_value) { 'the_previous_setting_value' }- let(:params) { { setting_name => new_setting_value } }-- before do- # stub a setting definition- allow(Settings::Definition)- .to receive(:[])- .and_call_original- allow(Settings::Definition)- .to receive(:[])- .with(setting_name)- .and_return(setting_definition)- allow(Setting)- .to receive(:[])- .and_call_original- allow(Setting)- .to receive(:[]).with(setting_name)- .and_return(previous_setting_value)- allow(Setting)- .to receive(:[]=)- # stub contract- allow(Settings::UpdateContract)- .to receive(:new)- .and_return(contract)- end+RSpec.describe Settings::UpdateService do+ include_context 'with update service setup'describe '#call' dosubject { instance.call(params) }