]> source.dussan.org Git - redmine.git/commitdiff
Adds tests for default custom field value (#21074).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 30 Oct 2015 12:59:48 +0000 (12:59 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 30 Oct 2015 12:59:48 +0000 (12:59 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@14768 e93f8b46-1217-0410-a6f0-8f06a7374b81

test/integration/api_test/issues_test.rb
test/object_helpers.rb

index 08543ab150f91d71e9a26daf5b796d7ea5f49231..18dbfaad9bdd0b40ac5592bd885db9daa723c741 100644 (file)
@@ -435,6 +435,28 @@ JSON
     assert_equal ["V1", "V3"], issue.custom_field_value(field).sort
   end
 
+  test "POST /issues.json with omitted custom field should set default value" do
+    field = IssueCustomField.generate!(:default_value => "Default")
+
+    issue = new_record(Issue) do
+      post '/issues.json',
+        {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {}}},
+        credentials('jsmith')
+    end
+    assert_equal "Default", issue.custom_field_value(field)
+  end
+
+  test "POST /issues.json with custom field set to blank should not set default value" do
+    field = IssueCustomField.generate!(:default_value => "Default")
+
+    issue = new_record(Issue) do
+      post '/issues.json',
+        {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {field.id.to_s => ""}}},
+        credentials('jsmith')
+    end
+    assert_equal "", issue.custom_field_value(field)
+  end
+
   test "POST /issues.json with failure should return errors" do
     assert_no_difference('Issue.count') do
       post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
@@ -512,6 +534,62 @@ JSON
     assert_equal "Notes only", journal.notes
   end
 
+  test "PUT /issues/:id.json with omitted custom field should not change blank value to default value" do
+    field = IssueCustomField.generate!(:default_value => "Default")
+    issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
+    assert_equal "", issue.reload.custom_field_value(field)
+
+    assert_difference('Journal.count') do
+      put "/issues/#{issue.id}.json",
+        {:issue => {:custom_field_values => {}, :notes => 'API'}},
+        credentials('jsmith')
+    end
+
+    assert_equal "", issue.reload.custom_field_value(field)
+  end
+
+  test "PUT /issues/:id.json with custom field set to blank should not change blank value to default value" do
+    field = IssueCustomField.generate!(:default_value => "Default")
+    issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
+    assert_equal "", issue.reload.custom_field_value(field)
+
+    assert_difference('Journal.count') do
+      put "/issues/#{issue.id}.json",
+        {:issue => {:custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
+        credentials('jsmith')
+    end
+
+    assert_equal "", issue.reload.custom_field_value(field)
+  end
+
+  test "PUT /issues/:id.json with tracker change and omitted custom field specific to that tracker does not set default value" do
+    field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
+    issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
+
+    assert_difference('Journal.count') do
+      put "/issues/#{issue.id}.json",
+        {:issue => {:tracker_id => 2, :custom_field_values => {}, :notes => 'API'}},
+        credentials('jsmith')
+    end
+
+    assert_equal 2, issue.reload.tracker_id
+    assert_nil issue.reload.custom_field_value(field)
+  end
+
+  test "PUT /issues/:id.json with tracker change and custom field specific to that tracker set to blank should not set default value" do
+    field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
+    issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
+
+    assert_difference('Journal.count') do
+      put "/issues/#{issue.id}.json",
+        {:issue => {:tracker_id => 2, :custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
+        credentials('jsmith')
+    end
+
+    assert_equal 2, issue.reload.tracker_id
+    assert_equal "", issue.reload.custom_field_value(field)
+  end
+
   test "PUT /issues/:id.xml with failed update" do
     put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
 
index 905ab099835e184e6a83b35d13fe2997d2a3d64b..66334f63a3b5586ff71b1a73da5e247c7da46a00 100644 (file)
@@ -190,6 +190,13 @@ module ObjectHelpers
     field
   end
 
+  def IssueCustomField.generate!(attributes={})
+    super do |field|
+      field.is_for_all = true unless attributes.key?(:is_for_all)
+      field.tracker_ids = Tracker.all.ids unless attributes.key?(:tracker_ids) || attributes.key?(:trackers)
+    end
+  end
+
   def Changeset.generate!(attributes={})
     @generated_changeset_rev ||= '123456'
     @generated_changeset_rev.succ!