|
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
|