summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorGo MAEDA <maeda@farend.jp>2020-12-26 01:36:19 +0000
committerGo MAEDA <maeda@farend.jp>2020-12-26 01:36:19 +0000
commit972f00eb473ffd07292bb0960428c0181046b613 (patch)
treec401a9b942d67ca555bf8391da3fa9f563c030be /test
parent46156e08a9a61a07c89045ad6e6d9ccbaebd875a (diff)
downloadredmine-972f00eb473ffd07292bb0960428c0181046b613.tar.gz
redmine-972f00eb473ffd07292bb0960428c0181046b613.zip
Create custom field by copy (#34307).
Patch by Takenori TAKAKI. git-svn-id: http://svn.redmine.org/redmine/trunk@20692 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'test')
-rw-r--r--test/functional/custom_fields_controller_test.rb73
-rw-r--r--test/unit/custom_field_test.rb49
2 files changed, 122 insertions, 0 deletions
diff --git a/test/functional/custom_fields_controller_test.rb b/test/functional/custom_fields_controller_test.rb
index fd042dea6..a2ca38083 100644
--- a/test/functional/custom_fields_controller_test.rb
+++ b/test/functional/custom_fields_controller_test.rb
@@ -325,6 +325,56 @@ class CustomFieldsControllerTest < Redmine::ControllerTest
assert_select 'input[type=radio][name=type]'
end
+ def test_new_with_copy
+ role_ids = [1, 2]
+ tracker_ids = [1, 2]
+ project_ids = [1, 2, 3]
+
+ copy_from = CustomField.find(1)
+ copy_from.role_ids = role_ids
+ copy_from.tracker_ids = tracker_ids
+ copy_from.project_ids = project_ids
+ copy_from.save
+
+ get :new, :params => {:copy => copy_from.id.to_s, :type => IssueCustomField}
+ assert_response :success
+
+ assert_select 'form' do
+ # field_format selected
+ assert_select 'select[name=?]', 'custom_field[field_format]' do
+ assert_select "option[value=\"#{copy_from.field_format}\"][selected=selected]"
+ end
+ # blank name
+ assert_select 'input[name=?][value=""]', 'custom_field[name]'
+ # description copied
+ assert_select 'textarea[name=?]', 'custom_field[description]', :text => copy_from.description
+ # role checked
+ role_ids.each do |role_id|
+ assert_select "input[type=checkbox][name=?][value=#{role_id}][checked=checked]", 'custom_field[role_ids][]'
+ end
+ # role not checked
+ (Role.givable.pluck(:id) - role_ids).each do |role_id|
+ assert_select "input[type=checkbox][name=?][value=#{role_id}]", 'custom_field[role_ids][]'
+ end
+ # tracker checked
+ tracker_ids.each do |tracker_id|
+ assert_select "input[type=checkbox][name=?][value=#{tracker_id}][checked=checked]", 'custom_field[tracker_ids][]'
+ end
+ # tracker not checked
+ (Tracker.all.pluck(:id) - tracker_ids).each do |tracker_id|
+ assert_select "input[type=checkbox][name=?][value=#{tracker_id}]", 'custom_field[tracker_ids][]'
+ end
+ # project checked
+ project_ids.each do |project_id|
+ assert_select "input[type=checkbox][name=?][value=#{project_id}][checked=checked]", 'custom_field[project_ids][]'
+ end
+ # project not checked
+ (Project.all.pluck(:id) - project_ids).each do |project_id|
+ assert_select "input[type=checkbox][name=?][value=#{project_id}]", 'custom_field[project_ids][]'
+ end
+ end
+ end
+
def test_create_list_custom_field
field = new_record(IssueCustomField) do
post(
@@ -449,6 +499,29 @@ class CustomFieldsControllerTest < Redmine::ControllerTest
assert_select 'input[type=radio][name=type]'
end
+ def test_create_with_enumerations
+ custom_field = IssueCustomField.create(:field_format => 'enumeration', :name => 'IssueCustomField')
+ custom_field.enumerations.build(:name => 'enumeration1', :position => 1)
+ custom_field.enumerations.build(:name => 'enumeration2', :position => 2)
+ assert custom_field.save
+
+ assert_difference 'CustomField.count' do
+ post(
+ :create,
+ :params => {
+ :type => 'IssueCustomField',
+ :copy => custom_field.id,
+ :custom_field => {:name => 'Copy'}
+ }
+ )
+ assert_response 302
+ end
+ field = IssueCustomField.order('id desc').first
+ assert_equal 'Copy', field.name
+ assert_equal ['enumeration1', 'enumeration2'], field.enumerations.pluck(:name).sort
+ assert_equal [1, 2], field.enumerations.pluck(:position).sort
+ end
+
def test_edit
get(
:edit,
diff --git a/test/unit/custom_field_test.rb b/test/unit/custom_field_test.rb
index f5d8e8027..3e18512d6 100644
--- a/test/unit/custom_field_test.rb
+++ b/test/unit/custom_field_test.rb
@@ -372,4 +372,53 @@ class CustomFieldTest < ActiveSupport::TestCase
field2 = IssueCustomField.create!(:name => 'Another long text', :field_format => 'text')
assert !field2.full_text_formatting?
end
+
+ def test_copy_from
+ custom_field = CustomField.find(1)
+ copy = CustomField.new.copy_from(custom_field)
+
+ assert_nil copy.id
+ assert_equal '', copy.name
+ assert_nil copy.position
+ (custom_field.attribute_names - ['id', 'name', 'position']).each do |attribute_name|
+ assert_equal custom_field.send(attribute_name).to_s, copy.send(attribute_name).to_s
+ end
+
+ copy.name = 'Copy'
+ assert copy.save
+ end
+
+ def test_copy_from_should_copy_enumerations
+ custom_field = CustomField.create(:field_format => 'enumeration', :name => 'CustomField')
+ custom_field.enumerations.build(:name => 'enumeration1', :position => 1)
+ custom_field.enumerations.build(:name => 'enumeration2', :position => 2)
+ assert custom_field.save
+
+ copy = CustomField.new.copy_from(custom_field)
+ copy.name = 'Copy'
+ assert copy.save
+ assert_equal ['enumeration1', 'enumeration2'], copy.enumerations.pluck(:name)
+ assert_equal [1, 2], copy.enumerations.pluck(:position)
+ end
+
+ def test_copy_from_should_copy_roles
+ %w(IssueCustomField TimeEntryCustomField ProjectCustomField VersionCustomField).each do |klass_name|
+ klass = klass_name.constantize
+ custom_field = klass.new(:name => klass_name, :role_ids => [1, 2, 3, 4, 5])
+ copy = klass.new.copy_from(custom_field)
+ assert_equal [1, 2, 3, 4, 5], copy.role_ids.sort
+ end
+ end
+
+ def test_copy_from_should_copy_trackers
+ issue_custom_field = IssueCustomField.new(:name => 'IssueCustomField', :tracker_ids => [1, 2, 3])
+ copy = IssueCustomField.new.copy_from(issue_custom_field)
+ assert_equal [1, 2, 3], copy.tracker_ids
+ end
+
+ def test_copy_from_should_copy_projects
+ issue_custom_field = IssueCustomField.new(:name => 'IssueCustomField', :project_ids => [1, 2, 3, 4, 5, 6])
+ copy = IssueCustomField.new.copy_from(issue_custom_field)
+ assert_equal [1, 2, 3, 4, 5, 6], copy.project_ids
+ end
end