From: Jean-Philippe Lang Date: Fri, 30 Oct 2015 12:59:48 +0000 (+0000) Subject: Adds tests for default custom field value (#21074). X-Git-Tag: 3.2.0~105 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fac0f7f38b1d32a82c2e6789819e349c9d313d44;p=redmine.git Adds tests for default custom field value (#21074). git-svn-id: http://svn.redmine.org/redmine/trunk@14768 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/test/integration/api_test/issues_test.rb b/test/integration/api_test/issues_test.rb index 08543ab15..18dbfaad9 100644 --- a/test/integration/api_test/issues_test.rb +++ b/test/integration/api_test/issues_test.rb @@ -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') diff --git a/test/object_helpers.rb b/test/object_helpers.rb index 905ab0998..66334f63a 100644 --- a/test/object_helpers.rb +++ b/test/object_helpers.rb @@ -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!