Project

General

Profile

Defect #3657 ยป copy_issue_disabled_tracker.patch

Validates issue's tracker on save - Pierre Paysant-Le Roux, 2009-07-23 09:31

View differences:

app/models/issue.rb
88 88
  def move_to(new_project, new_tracker = nil, options = {})
89 89
    options ||= {}
90 90
    issue = options[:copy] ? self.clone : self
91
    transaction do
92
      if new_project && issue.project_id != new_project.id
93
        # delete issue relations
94
        unless Setting.cross_project_issue_relations?
95
          issue.relations_from.clear
96
          issue.relations_to.clear
91
    begin
92
      transaction do
93
        if new_project && issue.project_id != new_project.id
94
          # delete issue relations
95
          unless Setting.cross_project_issue_relations?
96
            issue.relations_from.clear
97
            issue.relations_to.clear
98
          end
99
          # issue is moved to another project
100
          # reassign to the category with same name if any
101
          new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
102
          issue.category = new_category
103
          issue.fixed_version = nil
104
          issue.project = new_project
97 105
        end
98
        # issue is moved to another project
99
        # reassign to the category with same name if any
100
        new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
101
        issue.category = new_category
102
        issue.fixed_version = nil
103
        issue.project = new_project
104
      end
105
      if new_tracker
106
        issue.tracker = new_tracker
107
      end
108
      if options[:copy]
109
        issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
110
        issue.status = self.status
111
      end
112
      if issue.save
106
        if new_tracker
107
          issue.tracker = new_tracker
108
        end
109
        if options[:copy]
110
          issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
111
          issue.status = self.status
112
        end
113
        issue.save!
113 114
        unless options[:copy]
114 115
          # Manually update project_id on related time entries
115 116
          TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
116 117
        end
117
      else
118
        Issue.connection.rollback_db_transaction
119
        return false
120 118
      end
119
    rescue ActiveRecord::RecordInvalid
120
      #   #Issue.connection.rollback_db_transaction
121
      return false
121 122
    end
122 123
    return issue
123 124
  end
......
143 144
    if start_date && soonest_start && start_date < soonest_start
144 145
      errors.add :start_date, :invalid
145 146
    end
146
  end
147
  
148
  def validate_on_create
147
 
149 148
    errors.add :tracker_id, :invalid unless project.trackers.include?(tracker)
150 149
  end
151 150
  
test/unit/issue_test.rb
136 136
    assert_equal orig.tracker, issue.tracker
137 137
    assert_equal orig.custom_values.first.value, issue.custom_values.first.value
138 138
  end
139
  
139

  
140 140
  def test_should_close_duplicates
141 141
    # Create 3 issues
142 142
    issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Duplicates test', :description => 'Duplicates test')
......
228 228
    assert_nil copy.custom_value_for(2)
229 229
  end
230 230
  
231
  def test_copy_to_another_project_with_disabled_tracker
232
    assert !Project.find(3).trackers.include?(Tracker.find(1))
233
    issue = Issue.find(1)
234
    assert_equal 1, issue.tracker_id
235
    assert !issue.move_to(Project.find(3), nil, :copy => true)
236
  end
237

  
238

  
231 239
  def test_issue_destroy
232 240
    Issue.find(1).destroy
233 241
    assert_nil Issue.find_by_id(1)
    (1-1/1)