Project

General

Profile

Defect #36794 ยป 36794.patch

Dmitry Makurin , 2022-03-17 16:46

View differences:

app/models/issue.rb
284 284
  # Copies attributes from another issue, arg can be an id or an Issue
285 285
  def copy_from(arg, options={})
286 286
    issue = arg.is_a?(Issue) ? arg : Issue.visible.find(arg)
287
    # core fields
288
    not_copyable = %w[id root_id parent_id lft rgt created_on updated_on status_id closed_on]
289
    not_copyable += issue.disabled_core_fields
290
    not_copyable += issue.read_only_attribute_names(User.current)
291
    # For a new issue project and tracker should be copied regardless of workflow
292
    if new_record?
293
      not_copyable -= %w[project_id tracker_id]
294
    end
287 295
    self.attributes =
288
      issue.attributes.dup.except(
289
        "id", "root_id", "parent_id", "lft", "rgt",
290
        "created_on", "updated_on", "status_id", "closed_on"
291
      )
296
      issue.attributes.dup.except(*not_copyable)
292 297
    self.custom_field_values =
293
      issue.custom_field_values.inject({}) do |h, v|
298
      issue.editable_custom_field_values(User.current).inject({}) do |h, v|
294 299
        h[v.custom_field_id] = v.value
295 300
        h
296 301
      end
test/unit/issue_test.rb
3427 3427
    r = Issue.like('issue today')
3428 3428
    assert_include Issue.find(7), r
3429 3429
  end
3430

  
3431
  def test_copy_issue_should_copy_only_editable_attributes
3432
    User.current = User.find(2)
3433
    issue_orig = Issue.find(11)
3434
    cf = IssueCustomField.find(6)
3435
    cv = CustomValue.create!(:custom_field => cf, :customized => issue_orig, :value => 12.34)
3436

  
3437
    WorkflowPermission.delete_all
3438
    # status new
3439
    WorkflowPermission.create!(:role_id => 1, :tracker_id => 1,
3440
                               :old_status_id => 1,
3441
                               :author => false, :assignee => false,
3442
                               :field_name => 'fixed_version_id', :rule => 'readonly')
3443
    WorkflowPermission.create!(:role_id => 1, :tracker_id => 1,
3444
                               :old_status_id => 1,
3445
                               :author => false, :assignee => false,
3446
                               :field_name => cf.id, :rule => 'readonly')
3447
    # closed
3448
    WorkflowPermission.create!(:role_id => 1, :tracker_id => 1,
3449
                               :old_status_id => 5,
3450
                               :author => false, :assignee => false,
3451
                               :field_name => 'fixed_version_id', :rule => 'readonly')
3452
    WorkflowPermission.create!(:role_id => 1, :tracker_id => 1,
3453
                               :old_status_id => 5,
3454
                               :author => false, :assignee => false,
3455
                               :field_name => cf.id, :rule => 'readonly')
3456

  
3457
    # user cannot change field
3458
    assert_not issue_orig.safe_attribute?('fixed_version_id')
3459
    assert_not_nil issue_orig.fixed_version_id
3460
    # cf_6 is read only
3461
    assert issue_orig.read_only_attribute_names(User.current).include?(cf.id.to_s)
3462
    assert_equal cv.to_s, issue_orig.custom_field_value(cf.id)
3463

  
3464
    issue_copy = Issue.new.copy_from issue_orig
3465
    # field was not copied
3466
    assert_nil issue_copy.fixed_version_id
3467
    assert_empty issue_copy.custom_field_value(cf.id)
3468
    # still can't change field
3469
    assert_not issue_copy.safe_attribute?('fixed_version_id')
3470
    assert issue_copy.read_only_attribute_names(User.current).include?(cf.id.to_s)
3471
  end
3472

  
3473
  def test_copy_issue_from_should_ignore_disabled_rules_for_project_and_tracker
3474
    User.current = User.find(2)
3475
    source_issue = Issue.find(1)
3476
    icf = IssueCustomField.generate!(:tracker_ids => [1], :visible => true, :role_ids => [1], :is_for_all => false, :project_ids => [1])
3477

  
3478
    # Disable changing Project for role Manager
3479
    WorkflowPermission.create!(:role_id => 1, :tracker_id => 1,
3480
                               :old_status_id => 1,
3481
                               :author => false, :assignee => false,
3482
                               :field_name => 'project_id', :rule => 'readonly')
3483

  
3484
    CustomValue.create!(:custom_field => icf, :customized => source_issue, :value => 'test string')
3485

  
3486

  
3487
    new_issue = Issue.new.copy_from(source_issue)
3488
    # custom values were copied
3489
    assert_not_empty new_issue.custom_field_values
3490
    # copied values are equal
3491
    assert_equal source_issue.custom_field_value(icf.id), new_issue.custom_field_value(icf.id)
3492
  end
3430 3493
end
    (1-1/1)