Project

General

Profile

Defect #33550 » 0002-Delete-spent-time-custom-field-values-not-visible-by.patch

Marius BĂLTEANU, 2021-03-15 22:27

View differences:

app/models/time_entry.rb
128 128
      else
129 129
        @invalid_user_id = nil
130 130
      end
131

  
132
      # Delete assigned custom fields not visible by the user
133
      editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
134
      self.custom_field_values.delete_if do |c|
135
        !editable_custom_field_ids.include?(c.custom_field.id.to_s)
136
      end
131 137
    end
138

  
132 139
    attrs
133 140
  end
134 141

  
......
199 206

  
200 207
  # Returns the custom_field_values that can be edited by the given user
201 208
  def editable_custom_field_values(user=nil)
202
    visible_custom_field_values
209
    visible_custom_field_values(user)
203 210
  end
204 211

  
205 212
  # Returns the custom fields that can be edited by the given user
test/unit/time_entry_custom_field_test.rb
1
# frozen_string_literal: true
2

  
3
# Redmine - project management software
4
# Copyright (C) 2006-2020 Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19

  
20
require File.expand_path('../../test_helper', __FILE__)
21

  
22
class TimeEntryCustomFieldTest < ActiveSupport::TestCase
23
  include Redmine::I18n
24

  
25
  fixtures :roles
26

  
27
  def setup
28
    User.current = nil
29
  end
30

  
31
  def test_custom_field_with_visible_set_to_false_should_validate_roles
32
    set_language_if_valid 'en'
33
    field = TimeEntryCustomField.new(:name => 'Field', :field_format => 'string', :visible => false)
34
    assert !field.save
35
    assert_include "Roles cannot be blank", field.errors.full_messages
36
    field.role_ids = [1, 2]
37
    assert field.save
38
  end
39

  
40
  def test_changing_visible_to_true_should_clear_roles
41
    field = TimeEntryCustomField.create!(:name => 'Field', :field_format => 'string', :visible => false, :role_ids => [1, 2])
42
    assert_equal 2, field.roles.count
43

  
44
    field.visible = true
45
    field.save!
46
    assert_equal 0, field.roles.count
47
  end
48

  
49
  def test_safe_attributes_should_include_only_custom_fields_visible_to_user
50
    cf1 = TimeEntryCustomField.create!(:name => 'Visible field',
51
                                       :field_format => 'string',
52
                                       :visible => false, :role_ids => [1])
53
    cf2 = TimeEntryCustomField.create!(:name => 'Non visible field',
54
                                       :field_format => 'string',
55
                                       :visible => false, :role_ids => [3])
56
    user = User.find(2)
57
    time_entry = TimeEntry.new(:issue_id => 1)
58

  
59
    time_entry.send :safe_attributes=, {'custom_field_values' => {
60
      cf1.id.to_s => 'value1',
61
      cf2.id.to_s => 'value2'
62
    }}, user
63

  
64
    assert_equal 'value1', time_entry.custom_field_value(cf1)
65
    assert_nil time_entry.custom_field_value(cf2)
66

  
67
    time_entry.send :safe_attributes=, {'custom_fields' => [
68
      {'id' => cf1.id.to_s, 'value' => 'valuea'},
69
      {'id' => cf2.id.to_s, 'value' => 'valueb'}
70
    ]}, user
71

  
72
    assert_equal 'valuea', time_entry.custom_field_value(cf1)
73
    assert_nil time_entry.custom_field_value(cf2)
74
  end
75
end
test/unit/time_entry_test.rb
29 29
           :journals, :journal_details,
30 30
           :issue_categories, :enumerations,
31 31
           :groups_users,
32
           :enabled_modules
32
           :enabled_modules,
33
           :custom_fields, :custom_fields_projects, :custom_values
33 34

  
34 35
  def setup
35 36
    User.current = nil
(8-8/9)