Project

General

Profile

Feature #1767 » 0003-Modify-tests-for-custom-fields-configurable-switchab.patch

Yuichi HARADA, 2021-09-08 03:44

View differences:

test/functional/context_menus_controller_test.rb
419 419
  end
420 420

  
421 421
  def test_time_entries_context_menu_should_include_custom_fields
422
    field = TimeEntryCustomField.generate!(:name => "Field", :field_format => "list", :possible_values => ["foo", "bar"])
422
    field = TimeEntryCustomField.generate!(:name => "Field", :field_format => "list", :possible_values => ["foo", "bar"], :is_for_all => true)
423 423

  
424 424
    @request.session[:user_id] = 2
425 425
    get(
test/functional/custom_fields_controller_test.rb
134 134
      assert_select 'input[type=radio][name=?]', 'custom_field[visible]', 2
135 135
      assert_select 'input[type=checkbox][name=?]', 'custom_field[role_ids][]', 3
136 136

  
137
      assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count
138
      assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1
137 139
      assert_select 'input[type=hidden][name=type][value=TimeEntryCustomField]'
138 140
    end
139 141
  end
......
157 159
      assert_select 'input[type=radio][name=?]', 'custom_field[visible]', 2
158 160
      assert_select 'input[type=checkbox][name=?]', 'custom_field[role_ids][]', 3
159 161

  
162
      assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count
163
      assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1
160 164
      assert_select 'input[type=hidden][name=type][value=ProjectCustomField]'
161 165
    end
162 166
  end
......
184 188
    end
185 189
  end
186 190

  
187
  def test_new_time_entry_custom_field_should_not_show_trackers_and_projects
191
  def test_new_time_entry_custom_field_should_not_show_trackers
188 192
    get(
189 193
      :new,
190 194
      :params => {
......
195 199

  
196 200
    assert_select 'form#custom_field_form' do
197 201
      assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0
198
      assert_select 'input[name=?]', 'custom_field[project_ids][]', 0
199 202
    end
200 203
  end
201 204

  
......
257 260
    assert_select '[name=?]', 'custom_field[default_value]', 0
258 261
  end
259 262

  
260
  def test_setting_full_width_layout_shoul_be_present_only_for_long_text_issue_custom_field
263
  def test_setting_full_width_layout_should_be_present_only_for_long_text_issue_custom_field
261 264
    get(
262 265
      :new,
263 266
      :params => {
......
330 333
    tracker_ids = [1, 2]
331 334
    project_ids = [1, 2, 3]
332 335

  
333
    copy_from = CustomField.find(1)
334
    copy_from.role_ids = role_ids
335
    copy_from.tracker_ids = tracker_ids
336
    copy_from.project_ids = project_ids
337
    copy_from.save
338

  
339
    get :new, :params => {:copy => copy_from.id.to_s, :type => IssueCustomField}
340
    assert_response :success
341

  
342
    assert_select 'form' do
343
      # field_format selected
344
      assert_select 'select[name=?]', 'custom_field[field_format]' do
345
        assert_select "option[value=\"#{copy_from.field_format}\"][selected=selected]"
346
      end
347
      # blank name
348
      assert_select 'input[name=?][value=""]', 'custom_field[name]'
349
      # description copied
350
      assert_select 'textarea[name=?]', 'custom_field[description]', :text => copy_from.description
351
      # role checked
352
      role_ids.each do |role_id|
353
        assert_select "input[type=checkbox][name=?][value=#{role_id}][checked=checked]", 'custom_field[role_ids][]'
354
      end
355
      # role not checked
356
      (Role.givable.pluck(:id) - role_ids).each do |role_id|
357
        assert_select "input[type=checkbox][name=?][value=#{role_id}]", 'custom_field[role_ids][]'
358
      end
359
      # tracker checked
360
      tracker_ids.each do |tracker_id|
361
        assert_select "input[type=checkbox][name=?][value=#{tracker_id}][checked=checked]", 'custom_field[tracker_ids][]'
362
      end
363
      # tracker not checked
364
      (Tracker.all.pluck(:id) - tracker_ids).each do |tracker_id|
365
        assert_select "input[type=checkbox][name=?][value=#{tracker_id}]", 'custom_field[tracker_ids][]'
366
      end
367
      # project checked
368
      project_ids.each do |project_id|
369
        assert_select "input[type=checkbox][name=?][value=#{project_id}][checked=checked]", 'custom_field[project_ids][]'
370
      end
371
      # project not checked
372
      (Project.all.pluck(:id) - project_ids).each do |project_id|
373
        assert_select "input[type=checkbox][name=?][value=#{project_id}]", 'custom_field[project_ids][]'
336
    [
337
      CustomField.find(1),  # IssueCustomField
338
      CustomField.find(3),  # ProjectCustomField
339
      CustomField.find(10), # TimeEntryCustomField
340
    ].each do |copy_from|
341
      copy_from.role_ids = role_ids
342
      copy_from.tracker_ids = tracker_ids if copy_from.is_a?(IssueCustomField)
343
      copy_from.project_ids = project_ids
344
      copy_from.save
345

  
346
      get :new, :params => {:copy => copy_from.id.to_s, :type => copy_from.class}
347
      assert_response :success
348

  
349
      assert_select 'form' do
350
        # field_format selected
351
        assert_select 'select[name=?]', 'custom_field[field_format]' do
352
          assert_select "option[value=\"#{copy_from.field_format}\"][selected=selected]"
353
        end
354
        # blank name
355
        assert_select 'input[name=?][value=""]', 'custom_field[name]'
356
        # description copied
357
        assert_select 'textarea[name=?]', 'custom_field[description]', :text => copy_from.description
358
        # role checked
359
        role_ids.each do |role_id|
360
          assert_select "input[type=checkbox][name=?][value=#{role_id}][checked=checked]", 'custom_field[role_ids][]'
361
        end
362
        # role not checked
363
        (Role.givable.pluck(:id) - role_ids).each do |role_id|
364
          assert_select "input[type=checkbox][name=?][value=#{role_id}]", 'custom_field[role_ids][]'
365
        end
366
        if copy_from.is_a?(IssueCustomField)
367
          # tracker checked
368
          tracker_ids.each do |tracker_id|
369
            assert_select "input[type=checkbox][name=?][value=#{tracker_id}][checked=checked]", 'custom_field[tracker_ids][]'
370
          end
371
          # tracker not checked
372
          (Tracker.all.pluck(:id) - tracker_ids).each do |tracker_id|
373
            assert_select "input[type=checkbox][name=?][value=#{tracker_id}]", 'custom_field[tracker_ids][]'
374
          end
375
        end
376
        # project checked
377
        project_ids.each do |project_id|
378
          assert_select "input[type=checkbox][name=?][value=#{project_id}][checked=checked]", 'custom_field[project_ids][]'
379
        end
380
        # project not checked
381
        (Project.all.pluck(:id) - project_ids).each do |project_id|
382
          assert_select "input[type=checkbox][name=?][value=#{project_id}]", 'custom_field[project_ids][]'
383
        end
374 384
      end
375 385
    end
376 386
  end
test/functional/issues_controller_test.rb
298 298
  def test_index_with_project_custom_field_filter
299 299
    field =
300 300
      ProjectCustomField.
301
        create!(:name => 'Client', :is_filter => true, :field_format => 'string')
301
        create!(:name => 'Client', :is_filter => true, :field_format => 'string', :is_for_all => true)
302 302
    CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
303 303
    CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
304 304
    filter_name = "project.cf_#{field.id}"
test/functional/project_enumerations_controller_test.rb
213 213
                 "Time Entries are not assigned to system activities"
214 214
  end
215 215

  
216
  def test_update_could_be_switch_availability_of_time_entry_custom_field
217
    custom_field = TimeEntryCustomField.generate!(:is_for_all => false)
218
    project = Project.find(1)
219
    refute_includes project.time_entry_custom_fields, custom_field
220

  
221
    @request.session[:user_id] = 2 # manager
222
    put(
223
      :update,
224
      :params => {
225
        :project_id => 1,
226
        :enumerations => {
227
          "11"=>{"parent_id"=>"11", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"} # QA, no changes
228
        },
229
        :project => {
230
          'time_entry_custom_field_ids' => [custom_field.id.to_s]
231
        }
232
      }
233
    )
234
    assert_response :redirect
235
    assert_redirected_to '/projects/ecookbook/settings/activities'
236

  
237
    project.reload
238
    assert_includes project.time_entry_custom_fields, custom_field
239
  end
240

  
216 241
  def test_destroy
217 242
    @request.session[:user_id] = 2 # manager
218 243
    project_activity = TimeEntryActivity.new({
test/functional/projects_controller_test.rb
688 688
  end
689 689

  
690 690
  def test_show_should_not_display_blank_custom_fields_with_multiple_values
691
    f1 = ProjectCustomField.generate! :field_format => 'list', :possible_values => %w(Foo Bar), :multiple => true
692
    f2 = ProjectCustomField.generate! :field_format => 'list', :possible_values => %w(Baz Qux), :multiple => true
691
    f1 = ProjectCustomField.generate! :field_format => 'list', :possible_values => %w(Foo Bar), :multiple => true, :is_for_all => true
692
    f2 = ProjectCustomField.generate! :field_format => 'list', :possible_values => %w(Baz Qux), :multiple => true, :is_for_all => true
693 693
    project = Project.generate!(:custom_field_values => {f2.id.to_s => %w(Qux)})
694 694
    get(:show, :params => {:id => project.id})
695 695
    assert_response :success
test/functional/projects_custom_fields_visibility_test.rb
1
# frozen_string_literal: true
2

  
3
# Redmine - project management software
4
# Copyright (C) 2006-2021  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 ProjectsCustomFieldsVisibilityTest < Redmine::ControllerTest
23
  tests ProjectsController
24
  fixtures :projects, :members, :roles, :member_roles,
25
           :users,
26
           :custom_fields, :custom_values, :custom_fields_projects
27

  
28
  def test_index_should_show_visible_custom_fields_only
29
    prepare_test_data
30

  
31
    with_settings project_list_display_type: 'list' do
32
      @users_to_test.each do |user, fields|
33
        @request.session[:user_id] = user.id
34
        get :index, params: {
35
          c: (['identifier'] + @fields.map{|f| "cf_#{f.id}"})
36
        }
37
        @fields.each_with_index do |field, i|
38
          if fields.include?(field)
39
            assert_select 'td', {text: "Value#{i}", count: 1}, "User #{user.id} was not able to view #{field.name}"
40
          else
41
            assert_select 'td', {text: "Value#{i}", count: 0}, "User #{user.id} was able to view #{field.name}"
42
          end
43
        end
44
      end
45
    end
46
  end
47

  
48
  def test_index_as_csv_should_show_visible_custom_fields_only
49
    prepare_test_data
50

  
51
    with_settings project_list_display_type: 'list' do
52
      @users_to_test.each do |user, fields|
53
        @request.session[:user_id] = user.id
54
        get :index, params: {
55
          c: (['identifier'] + @fields.map{|f| "cf_#{f.id}"}),
56
          format: 'csv'
57
        }
58
        @fields.each_with_index do |field, i|
59
          if fields.include?(field)
60
            assert_include "Value#{i}", response.body, "User #{user.id} was not able to view #{field.name} in CSV"
61
          else
62
            assert_not_include "Value#{i}", response.body, "User #{user.id} was able to view #{field.name} in CSV"
63
          end
64
        end
65
      end
66
    end
67
  end
68

  
69
  def test_settings_should_not_show_custom_fields_not_visible_for_user
70
    project_cf = ProjectCustomField.find(3)
71
    project_cf.visible = false
72
    project_cf.role_ids = [2]       # Developer
73
    project_cf.save!
74

  
75
    @request.session[:user_id] = 2  # Manager
76
    get :settings, params: {
77
      id: 1
78
    }
79

  
80
    assert_response :success
81
    assert_select 'select#project_custom_field_values_3', 0
82
    assert_select 'input[type=checkbox][name="project[project_custom_field_ids][]"][value="3"]'
83
  end
84

  
85
  def test_show_should_not_show_custom_fields_not_visible_for_user
86
    project_cf = ProjectCustomField.find(3)
87
    project_cf.visible = false
88
    project_cf.role_ids = [2]       # Developer
89
    project_cf.save!
90

  
91
    @request.session[:user_id] = 2  # Manager
92
    get :show, params: {
93
      id: 1
94
    }
95

  
96
    assert_response :success
97
    assert_select 'li.list_cf.cf_3', 0
98
  end
99

  
100
  def test_show_should_not_show_custom_fields_not_available_for_project
101
    project_cf = ProjectCustomField.find(3)
102
    project_cf.visible = true
103
    project_cf.is_for_all = false
104
    project_cf.project_ids = [2]
105
    project_cf.save!
106

  
107
    @request.session[:user_id] = 2
108
    get :show, params: {
109
      id: 1
110
    }
111

  
112
    assert_response :success
113
    assert_select 'li.list_cf.cf_3', 0
114
  end
115

  
116
  private
117

  
118
  def prepare_test_data
119
    field_attributes = {field_format: 'string', is_for_all: true, is_filter: true}
120
    @fields = []
121
    @fields << (field1 = ProjectCustomField.create!(field_attributes.merge(name: 'Field 1', visible: true)))
122
    @fields << (field2 = ProjectCustomField.create!(field_attributes.merge(name: 'Field 2', visible: false, role_ids: [1, 2])))
123
    @fields << (field3 = ProjectCustomField.create!(field_attributes.merge(name: 'Field 3', visible: false, role_ids: [1, 3])))
124
    project = Project.find(1)
125
    project.safe_attributes = {
126
      custom_field_values: {field1.id => 'Value0', field2.id => 'Value1', field3.id => 'Value2'}
127
    };
128
    project.save!
129

  
130
    user_with_role_on_other_project = User.generate!
131
    User.add_to_project(user_with_role_on_other_project, Project.find(2), Role.find(3))
132

  
133
    @users_to_test = {
134
      User.find(1) => [field1, field2, field3],
135
      User.find(3) => [field1, field2],
136
      user_with_role_on_other_project => [field1], # should see field1 only on Project 1
137
      User.generate! => [field1],
138
      User.anonymous => [field1],
139
    }
140

  
141
    Member.where(project_id: 1).each do |member|
142
      member.destroy unless @users_to_test.key?(member.principal)
143
    end
144
  end
145
end
test/functional/timelog_controller_test.rb
859 859
  end
860 860

  
861 861
  def test_bulk_update_clear_custom_field
862
    field = TimeEntryCustomField.generate!(:field_format => 'string')
862
    field = TimeEntryCustomField.generate!(:field_format => 'string', :is_for_all => true)
863 863
    @request.session[:user_id] = 2
864 864
    post(
865 865
      :bulk_update,
......
1467 1467
  end
1468 1468

  
1469 1469
  def test_index_with_time_entry_custom_field_column
1470
    field = TimeEntryCustomField.generate!(:field_format => 'string')
1470
    field = TimeEntryCustomField.generate!(:field_format => 'string', :is_for_all => true)
1471 1471
    entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'})
1472 1472
    field_name = "cf_#{field.id}"
1473 1473

  
......
1479 1479
  end
1480 1480

  
1481 1481
  def test_index_with_time_entry_custom_field_sorting
1482
    field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
1482
    field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field', :is_for_all => true)
1483 1483
    TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
1484 1484
    TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
1485 1485
    TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
test/functional/timelog_custom_fields_visibility_test.rb
42 42
      get :index, :params => {
43 43
        :project_id => 1,
44 44
        :issue_id => @issue.id,
45
        :c => (['hours'] + @fields.map{|f| "issue.cf_#{f.id}"})
45
        :c => (['hours'] +
46
          @fields.map{|f|
47
            f.is_a?(TimeEntryCustomField) ?
48
              "cf_#{f.id}" : "#{(/(.+)CustomField/ =~ f.class.name;$1).underscore}.cf_#{f.id}"
49
          }
50
        )
46 51
      }
47 52
      @fields.each_with_index do |field, i|
48 53
        if fields.include?(field)
......
62 67
      get :index, :params => {
63 68
        :project_id => 1,
64 69
        :issue_id => @issue.id,
65
        :c => (['hours'] + @fields.map{|f| "issue.cf_#{f.id}"}),
70
        :c => (['hours'] +
71
          @fields.map{|f|
72
            f.is_a?(TimeEntryCustomField) ?
73
              "cf_#{f.id}" : "#{(/(.+)CustomField/ =~ f.class.name;$1).underscore}.cf_#{f.id}"
74
          }
75
        ),
66 76
        :format => 'csv'
67 77
      }
68 78
      @fields.each_with_index do |field, i|
......
125 135
    assert_select 'select#time_entry_custom_field_values_10', 0
126 136
  end
127 137

  
138
  def test_edit_should_not_show_custom_fields_not_available_for_project
139
    time_entry_cf = TimeEntryCustomField.find(10)
140
    time_entry_cf.visible = true
141
    time_entry_cf.is_for_all = false
142
    time_entry_cf.project_ids = [2]
143
    time_entry_cf.save!
144

  
145
    @request.session[:user_id] = 2
146
    get :edit, :params => {
147
      :id => 3,
148
      :project_id => 1
149
    }
150
    assert_response :success
151
    assert_select 'select#time_entry_custom_field_values_10', 0
152
  end
153

  
128 154
  private
129 155

  
130 156
  def prepare_test_data
131 157
    field_attributes = {:field_format => 'string', :is_for_all => true, :is_filter => true, :trackers => Tracker.all}
158
    field_attributes_exclude_trackers = field_attributes.reject{|k,v|k == :trackers}
132 159
    @fields = []
133 160
    @fields << (@field1 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 1', :visible => true)))
134 161
    @fields << (@field2 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 2', :visible => false, :role_ids => [1, 2])))
135 162
    @fields << (@field3 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 3', :visible => false, :role_ids => [1, 3])))
163
    @fields << (@field4 = TimeEntryCustomField.create!(field_attributes_exclude_trackers.merge(:name => 'Field 4', :visible => true)))
164
    @fields << (@field5 = TimeEntryCustomField.create!(field_attributes_exclude_trackers.merge(:name => 'Field 5', :visible => false, :role_ids => [1, 2])))
165
    @fields << (@field6 = TimeEntryCustomField.create!(field_attributes_exclude_trackers.merge(:name => 'Field 6', :visible => false, :role_ids => [1, 3])))
166
    @fields << (@field7 = ProjectCustomField.create!(field_attributes_exclude_trackers.merge(:name => 'Field 7', :visible => true)))
167
    @fields << (@field8 = ProjectCustomField.create!(field_attributes_exclude_trackers.merge(:name => 'Field 8', :visible => false, :role_ids => [1, 2])))
168
    @fields << (@field9 = ProjectCustomField.create!(field_attributes_exclude_trackers.merge(:name => 'Field 9', :visible => false, :role_ids => [1, 3])))
136 169
    @issue = Issue.generate!(
137 170
      :author_id => 1,
138 171
      :project_id => 1,
139 172
      :tracker_id => 1,
140 173
      :custom_field_values => {@field1.id => 'Value0', @field2.id => 'Value1', @field3.id => 'Value2'}
141 174
    )
142
    TimeEntry.generate!(:issue => @issue)
175
    TimeEntry.generate!(
176
      :issue => @issue,
177
      :custom_field_values => {@field4.id => 'Value3', @field5.id => 'Value4', @field6.id => 'Value5'}
178
    )
179
    project = Project.find(1)
180
    project.safe_attributes = {
181
      :custom_field_values => {@field7.id => 'Value6', @field8.id => 'Value7', @field9.id => 'Value8'}
182
    };
183
    project.save!
143 184

  
144 185
    @user_with_role_on_other_project = User.generate!
145 186
    User.add_to_project(@user_with_role_on_other_project, Project.find(2), Role.find(3))
146 187

  
147 188
    @users_to_test = {
148
      User.find(1) => [@field1, @field2, @field3],
149
      User.find(3) => [@field1, @field2],
150
      @user_with_role_on_other_project => [@field1], # should see field1 only on Project 1
151
      User.generate! => [@field1],
152
      User.anonymous => [@field1]
189
      User.find(1) => [@field1, @field2, @field3, @field4, @field5, @field6, @field7, @field8, @field9],
190
      User.find(3) => [@field1, @field2, @field4, @field5, @field7, @field8],
191
      @user_with_role_on_other_project => [@field1, @field4, @field7], # should see field1,field4,field7 only on Project 1
192
      User.generate! => [@field1, @field4, @field7],
193
      User.anonymous => [@field1, @field4, @field7],
153 194
    }
154 195

  
155 196
    Member.where(:project_id => 1).each do |member|
test/helpers/application_helper_test.rb
1815 1815
  end
1816 1816

  
1817 1817
  def test_link_to_attachment_container
1818
    field = ProjectCustomField.generate!(:name => "File", :field_format => 'attachment')
1818
    field = ProjectCustomField.generate!(:name => "File", :field_format => 'attachment', :is_for_all => true)
1819 1819
    project = projects(:projects_001)
1820 1820
    project_custom_value_attachment = new_record(Attachment) do
1821 1821
      project.custom_field_values = {field.id => {:file => mock_file}}
test/integration/api_test/time_entries_test.rb
89 89
  end
90 90

  
91 91
  test "POST /time_entries.xml with issue_id should accept custom fields" do
92
    field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
92
    field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string', :is_for_all => true)
93 93

  
94 94
    assert_difference 'TimeEntry.count' do
95 95
      post(
test/unit/custom_field_test.rb
22 22
class CustomFieldTest < ActiveSupport::TestCase
23 23
  fixtures :custom_fields, :roles, :projects,
24 24
           :trackers, :issue_statuses,
25
           :issues, :users
25
           :issues, :users,
26
           :time_entries, :members, :member_roles, :custom_fields_projects
26 27

  
27 28
  def setup
28 29
    User.current = nil
......
260 261
  def test_changing_multiple_to_false_should_delete_multiple_values
261 262
    field = ProjectCustomField.create!(:name => 'field', :field_format => 'list',
262 263
                                       :multiple => 'true',
263
                                       :possible_values => ['field1', 'field2'])
264
                                       :possible_values => ['field1', 'field2'], :is_for_all => true)
264 265
    other = ProjectCustomField.create!(:name => 'other', :field_format => 'list',
265 266
                                       :multiple => 'true',
266
                                       :possible_values => ['other1', 'other2'])
267
                                       :possible_values => ['other1', 'other2'], :is_for_all => true)
267 268
    item_with_multiple_values = Project.generate!(:custom_field_values =>
268 269
                                                   {field.id => ['field1', 'field2'],
269 270
                                                    other.id => ['other1', 'other2']})
......
349 350
    assert_equal -12.5, field.cast_value('-12.5')
350 351
  end
351 352

  
352
  def test_project_custom_field_visibility
353
    project_field = ProjectCustomField.generate!(:visible => false, :field_format => 'list', :possible_values => %w[a b c])
354
    project = Project.find(3)
355
    project.custom_field_values = {project_field.id => 'a'}
356

  
357
    # Admins can find projects with the field
358
    with_current_user(User.find(1)) do
359
      assert_includes Project.where(project_field.visibility_by_project_condition), project
360
    end
361

  
362
    # The field is not visible to normal users
363
    with_current_user(User.find(2)) do
364
      refute_includes Project.where(project_field.visibility_by_project_condition), project
353
  def test_custom_field_visibility_by_project_condition
354
    Member.create!(:principal => User.find(2), :project_id => 3, :role_ids => [1])
355
    [Issue.find(5), TimeEntry.find(4), Project.find(3)].each do |customized_obj|
356
      customized_klass = customized_obj.class
357
      custom_field_klass = "#{customized_klass.name}CustomField".constantize
358
      custom_field = custom_field_klass.generate!(
359
        :field_format => 'list', :possible_values => %w[a b c],
360
        :visible => false, :role_ids => [2],
361
        :is_for_all => false, :project_ids => [3]
362
      )
363
      customized_obj.custom_field_values = {custom_field.id => 'a'}
364

  
365
      # Admins can find customized objects with the field
366
      with_current_user(User.find(1)) do
367
        assert_includes customized_klass.where(custom_field.visibility_by_project_condition), customized_obj
368
      end
369

  
370
      # The field is not visible to normal users
371
      with_current_user(User.find(2)) do
372
        refute_includes customized_klass.where(custom_field.visibility_by_project_condition), customized_obj
373
      end
365 374
    end
366 375
  end
367 376

  
......
417 426
  end
418 427

  
419 428
  def test_copy_from_should_copy_projects
420
    issue_custom_field = IssueCustomField.new(:name => 'IssueCustomField', :project_ids => [1, 2, 3, 4, 5, 6])
421
    copy = IssueCustomField.new.copy_from(issue_custom_field)
422
    assert_equal [1, 2, 3, 4, 5, 6], copy.project_ids
429
    %w(IssueCustomField TimeEntryCustomField ProjectCustomField).each do |klass_name|
430
      klass = klass_name.constantize
431
      custom_field = klass.new(:name => klass_name, :project_ids => [1, 2, 3, 4, 5, 6])
432
      copy = klass.new.copy_from(custom_field)
433
      assert_equal [1, 2, 3, 4, 5, 6], copy.project_ids
434
    end
423 435
  end
424 436
end
test/unit/project_test.rb
1094 1094
  def test_safe_attributes_should_include_only_custom_fields_visible_to_user
1095 1095
    cf1 = ProjectCustomField.create!(:name => 'Visible field',
1096 1096
                                   :field_format => 'string',
1097
                                   :visible => false, :role_ids => [1])
1097
                                   :visible => false, :role_ids => [1], :is_for_all => true)
1098 1098
    cf2 = ProjectCustomField.create!(:name => 'Non visible field',
1099 1099
                                   :field_format => 'string',
1100
                                   :visible => false, :role_ids => [3])
1101
    user = User.find(2)
1100
                                   :visible => false, :role_ids => [3], :is_for_all => true)
1101
    User.current = User.find(2)
1102 1102
    project = Project.find(1)
1103 1103

  
1104 1104
    project.send(
......
1107 1107
        'custom_field_values' => {
1108 1108
          cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'
1109 1109
        }
1110
      },
1111
      user
1110
      }
1112 1111
    )
1113 1112
    assert_equal 'value1', project.custom_field_value(cf1)
1114 1113
    assert_nil project.custom_field_value(cf2)
......
1121 1120
            {'id' => cf1.id.to_s, 'value' => 'valuea'},
1122 1121
            {'id' => cf2.id.to_s, 'value' => 'valueb'}
1123 1122
          ]
1124
      },
1125
      user
1123
      }
1126 1124
    )
1127 1125
    assert_equal 'valuea', project.custom_field_value(cf1)
1128 1126
    assert_nil project.custom_field_value(cf2)
test/unit/query_test.rb
1161 1161
  end
1162 1162

  
1163 1163
  def test_filter_on_project_custom_field
1164
    field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
1164
    field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string', :is_for_all => true)
1165 1165
    CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
1166 1166
    CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
1167 1167

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

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

  
64 64
    assert_equal 'value1', time_entry.custom_field_value(cf1)
65 65
    assert_nil time_entry.custom_field_value(cf2)
......
67 67
    time_entry.send :safe_attributes=, {'custom_fields' => [
68 68
      {'id' => cf1.id.to_s, 'value' => 'valuea'},
69 69
      {'id' => cf2.id.to_s, 'value' => 'valueb'}
70
    ]}, user
70
    ]}
71 71

  
72 72
    assert_equal 'valuea', time_entry.custom_field_value(cf1)
73 73
    assert_nil time_entry.custom_field_value(cf2)
(3-3/11)