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
move tz valid values to contract
ulfertsac9bdb7d2912
diff --git a/app/cells/settings/time_zone_setting_cell.rb b/app/cells/settings/time_zone_setting_cell.rbindex 3b9696f46a0..931a0d36d08 100644--- a/app/cells/settings/time_zone_setting_cell.rb+++ b/app/cells/settings/time_zone_setting_cell.rb@@ -42,34 +42,18 @@ module Settings)end- def time_zones- ActiveSupport::TimeZone.all- end-- ##- # Returns time zone (label, value) tuples to be used for a select field.- # As we only store tzinfo compatible data we only provide options, for which the- # values can later on be retrieved unambiguously. This is not always the case- # for values in ActiveSupport::TimeZone since multiple AS zones map to single tzinfo zones.def time_zone_entries- time_zones- .group_by { |tz| tz.tzinfo.name }- .values- .map do |zones|- tz = namesake_time_zone(zones)-+ contract+ .assignable_time_zones+ .map do |tz|[tz.to_s, tz.tzinfo.canonical_identifier]endend- # If there are multiple AS::TimeZones for a single TZInfo::Timezone, we- # only return the one that is the namesake.- def namesake_time_zone(time_zones)- if time_zones.length == 1- time_zones.first- else- time_zones.detect { |tz| tz.tzinfo.name.include?(tz.name.gsub(' ', '_')) }- end+ private++ def contract+ @contract ||= UserPreferences::UpdateContract.new(form.object, User.current)endendenddiff --git a/app/contracts/user_preferences/base_contract.rb b/app/contracts/user_preferences/base_contract.rbindex c8697c9ec5e..53cbd7d1b32 100644--- a/app/contracts/user_preferences/base_contract.rb+++ b/app/contracts/user_preferences/base_contract.rb@@ -51,11 +51,31 @@ module UserPreferencesvalidate :no_duplicate_workdays,if: -> { model.workdays.present? }+ ##+ # Returns time zones supported by OpenProject. Those include only the subset of all the+ # TZInfo timezones also handled by ActiveSupport::TimeZone.+ # The reason for this is currently:+ # * the reminder mail implementation which could be amended+ # * the select in the form which only displays ActiveSupport::TimeZone as they are more+ # user friendly.+ # As we only store tzinfo compatible data we only provide options, for which the+ # values can later on be retrieved unambiguously. This is not always the case+ # for values in ActiveSupport::TimeZone since multiple AS zones map to single tzinfo zones.+ def assignable_time_zones+ ActiveSupport::TimeZone+ .all+ .group_by { |tz| tz.tzinfo.name }+ .values+ .map do |zones|+ namesake_time_zone(zones)+ end+ end+protecteddef time_zone_correctnessif model.time_zone.present? &&- ActiveSupport::TimeZone[model.time_zone]&.tzinfo&.canonical_identifier != model.time_zone+ assignable_time_zones.none? { |tz| tz.tzinfo.canonical_identifier == model.time_zone }errors.add(:time_zone, :inclusion)endend@@ -80,5 +100,15 @@ module UserPreferenceserrors.add :workdays, :no_duplicatesendend++ # If there are multiple AS::TimeZones for a single TZInfo::Timezone, we+ # only return the one that is the namesake.+ def namesake_time_zone(time_zones)+ if time_zones.length == 1+ time_zones.first+ else+ time_zones.detect { |tz| tz.tzinfo.name.include?(tz.name.gsub(' ', '_')) }+ end+ endendenddiff --git a/spec/contracts/user_preferences/update_contract_spec.rb b/spec/contracts/user_preferences/update_contract_spec.rbindex 8ea4cdb3255..00aabdcdd36 100644--- a/spec/contracts/user_preferences/update_contract_spec.rb+++ b/spec/contracts/user_preferences/update_contract_spec.rb@@ -57,215 +57,242 @@ describe UserPreferences::UpdateContract doendlet(:contract) { described_class.new(user_preference, current_user) }- context 'when current_user is admin' do- let(:current_user) { FactoryBot.build_stubbed(:admin) }- let(:preference_user) { FactoryBot.build_stubbed(:user) }+ describe 'validation' do+ context 'when current_user is admin' do+ let(:current_user) { FactoryBot.build_stubbed(:admin) }+ let(:preference_user) { FactoryBot.build_stubbed(:user) }- it_behaves_like 'contract is valid'- end-- context 'when current_user has manage_user permission' do- let(:preference_user) { FactoryBot.build_stubbed(:user) }-- before do- allow(current_user).to receive(:allowed_to_globally?).with(:manage_user).and_return true+ it_behaves_like 'contract is valid'end- it_behaves_like 'contract is valid'- end+ context 'when current_user has manage_user permission' do+ let(:preference_user) { FactoryBot.build_stubbed(:user) }- context 'when current_user is the own user' do- it_behaves_like 'contract is valid'- end+ before do+ allow(current_user).to receive(:allowed_to_globally?).with(:manage_user).and_return true+ end- context 'when current_user is the own user but not active' do- before do- allow(current_user).to receive(:active?).and_return false+ it_behaves_like 'contract is valid'end- it_behaves_like 'contract user is unauthorized'- end-- context 'when current_user is anonymous' do- let(:current_user) { User.anonymous }+ context 'when current_user is the own user' do+ it_behaves_like 'contract is valid'+ end- it_behaves_like 'contract user is unauthorized'- end+ context 'when current_user is the own user but not active' do+ before do+ allow(current_user).to receive(:active?).and_return false+ end- context 'when current_user is a regular user' do- let(:preference_user) { FactoryBot.build_stubbed(:user) }+ it_behaves_like 'contract user is unauthorized'+ end- it_behaves_like 'contract user is unauthorized'- end+ context 'when current_user is anonymous' do+ let(:current_user) { User.anonymous }- context 'with empty settings' do- let(:settings) do- {}+ it_behaves_like 'contract user is unauthorized'end- it_behaves_like 'contract is valid'- end+ context 'when current_user is a regular user' do+ let(:preference_user) { FactoryBot.build_stubbed(:user) }- context 'with a string for hide_mail' do- let(:settings) do- {- hide_mail: 'yes please'- }+ it_behaves_like 'contract user is unauthorized'end- it_behaves_like 'contract is invalid', hide_mail: :type_mismatch- end+ context 'with empty settings' do+ let(:settings) do+ {}+ end- context 'with a field within the daily_reminders having the wrong type' do- let(:settings) do- {- daily_reminders: {- enabled: 'sure',- times: %w[08:00:00+00:00 12:00:00+00:00]- }- }+ it_behaves_like 'contract is valid'end- it_behaves_like 'contract is invalid', daily_reminders: :type_mismatch_nested- end-- context 'with a field within the daily_reminders missing' do- let(:settings) do- {- daily_reminders: {- times: %w[08:00:00+00:00 12:00:00+00:00]+ context 'with a string for hide_mail' do+ let(:settings) do+ {+ hide_mail: 'yes please'}- }+ end++ it_behaves_like 'contract is invalid', hide_mail: :type_mismatchend- it_behaves_like 'contract is invalid', daily_reminders: :blank_nested- end+ context 'with a field within the daily_reminders having the wrong type' do+ let(:settings) do+ {+ daily_reminders: {+ enabled: 'sure',+ times: %w[08:00:00+00:00 12:00:00+00:00]+ }+ }+ end- context 'with an extra property' do- let(:settings) do- {- foo: true- }+ it_behaves_like 'contract is invalid', daily_reminders: :type_mismatch_nestedend- it_behaves_like 'contract is invalid', foo: :unknown_property- end+ context 'with a field within the daily_reminders missing' do+ let(:settings) do+ {+ daily_reminders: {+ times: %w[08:00:00+00:00 12:00:00+00:00]+ }+ }+ end++ it_behaves_like 'contract is invalid', daily_reminders: :blank_nested+ end- context 'with an extra property within the daily_reminders' do- let(:settings) do- {- daily_reminders: {- enabled: true,- times: %w[08:00:00+00:00 12:00:00+00:00],+ context 'with an extra property' do+ let(:settings) do+ {foo: true}- }+ end++ it_behaves_like 'contract is invalid', foo: :unknown_propertyend- it_behaves_like 'contract is invalid', daily_reminders: :unknown_property_nested- end+ context 'with an extra property within the daily_reminders' do+ let(:settings) do+ {+ daily_reminders: {+ enabled: true,+ times: %w[08:00:00+00:00 12:00:00+00:00],+ foo: true+ }+ }+ end- context 'with an invalid time for the daily_reminders' do- let(:settings) do- {- daily_reminders: {- enabled: true,- times: %w[abc 12:00:00+00:00]+ it_behaves_like 'contract is invalid', daily_reminders: :unknown_property_nested+ end++ context 'with an invalid time for the daily_reminders' do+ let(:settings) do+ {+ daily_reminders: {+ enabled: true,+ times: %w[abc 12:00:00+00:00]+ }}- }+ end++ it_behaves_like 'contract is invalid', daily_reminders: %i[format_nested full_hour]end- it_behaves_like 'contract is invalid', daily_reminders: %i[format_nested full_hour]- end+ context 'with a sub hour time for the daily_reminders' do+ let(:settings) do+ {+ daily_reminders: {+ enabled: true,+ times: %w[12:30:00+00:00]+ }+ }+ end++ it_behaves_like 'contract is invalid', daily_reminders: :full_hour+ end- context 'with a sub hour time for the daily_reminders' do- let(:settings) do- {- daily_reminders: {- enabled: true,- times: %w[12:30:00+00:00]+ context 'with an invalid order for comments_sorting' do+ let(:settings) do+ {+ comments_sorting: 'up'}- }+ end++ it_behaves_like 'contract is invalid', comments_sorting: :inclusionend- it_behaves_like 'contract is invalid', daily_reminders: :full_hour- end+ context 'without a time_zone' do+ let(:settings) do+ {+ hide_mail: true,+ auto_hide_popups: true,+ comments_sorting: 'desc',+ daily_reminders: {+ enabled: true,+ times: %w[08:00:00+00:00 12:00:00+00:00]+ },+ warn_on_leaving_unsaved: true+ }+ end- context 'with an invalid order for comments_sorting' do- let(:settings) do- {- comments_sorting: 'up'- }+ it_behaves_like 'contract is valid'end- it_behaves_like 'contract is invalid', comments_sorting: :inclusion- end+ context 'with a full time_zone' do+ let(:settings) do+ {+ time_zone: 'Europe/Paris'+ }+ end- context 'without a time_zone' do- let(:settings) do- {- hide_mail: true,- auto_hide_popups: true,- comments_sorting: 'desc',- daily_reminders: {- enabled: true,- times: %w[08:00:00+00:00 12:00:00+00:00]- },- warn_on_leaving_unsaved: true- }+ it_behaves_like 'contract is valid'end- it_behaves_like 'contract is valid'- end+ context 'with a non ActiveSupport::Timezone timezone' do+ let(:settings) do+ {+ time_zone: 'America/Adak'+ }+ end- context 'with a full time_zone' do- let(:settings) do- {- time_zone: 'Europe/Paris'- }+ it_behaves_like 'contract is invalid', time_zone: :inclusionend- it_behaves_like 'contract is valid'- end+ context 'with a malformed time_zone' do+ let(:settings) do+ {+ time_zone: '123Brasilia'+ }+ end- context 'with a malformed time_zone' do- let(:settings) do- {- time_zone: '123Brasilia'- }+ it_behaves_like 'contract is invalid', time_zone: :inclusionend- it_behaves_like 'contract is invalid', time_zone: :inclusion- end+ context 'with a non tzinfo time_zone' do+ let(:settings) do+ {+ time_zone: 'Brasilia'+ }+ end- context 'with a non tzinfo time_zone' do- let(:settings) do- {- time_zone: 'Brasilia'- }+ it_behaves_like 'contract is invalid', time_zone: :inclusionend- it_behaves_like 'contract is invalid', time_zone: :inclusion- end+ context 'with duplicate workday entries' do+ let(:settings) do+ {+ workdays: [1, 1]+ }+ end- context 'with duplicate workday entries' do- let(:settings) do- {- workdays: [1, 1]- }+ it_behaves_like 'contract is invalid', workdays: :no_duplicatesend- it_behaves_like 'contract is invalid', workdays: :no_duplicates+ context 'with non-iso workday entries' do+ let(:settings) do+ {+ workdays: [nil, 'foo', :bar, 21345, 2.0]+ }+ end++ it_behaves_like 'contract is invalid', workdays: %i[invalid type_mismatch_nested+ type_mismatch_nested type_mismatch_nested]+ endend- context 'with non-iso workday entries' do- let(:settings) do- {- workdays: [nil, 'foo', :bar, 21345, 2.0]- }+ describe '#assignable_time_zones' do+ subject(:time_zones) { contract.assignable_time_zones }++ it 'returns a list of AS::TimeZones' do+ expect(time_zones)+ .to(be_all { |tz| tz.is_a?(ActiveSupport::TimeZone) })end- it_behaves_like 'contract is invalid', workdays: %i[invalid type_mismatch_nested- type_mismatch_nested type_mismatch_nested]+ it 'includes only the namesake zone if multiple AS::Timezone map to the same TZInfo' do+ # In this case 'Edinburgh' and 'Bern' are not included+ expect(time_zones.select { |tz| %w[Europe/London Europe/Zurich].include? tz.tzinfo.canonical_zone.name })+ .to match_array([ActiveSupport::TimeZone['London'], ActiveSupport::TimeZone['Zurich']])+ endendend