Project

General

Profile

Defect #5385 » 5385_restrict_issues_statuses_in_query.patch

Marius BĂLTEANU, 2017-04-18 01:48

View differences:

app/models/issue_query.rb
82 82

  
83 83
  def initialize_available_filters
84 84
    add_available_filter "status_id",
85
      :type => :list_status, :values => lambda { IssueStatus.sorted.collect{|s| [s.name, s.id.to_s] } }
85
      :type => :list_status, :values => lambda { issue_statuses }
86 86

  
87 87
    add_available_filter("project_id",
88 88
      :type => :list, :values => lambda { project_values }
app/models/project.rb
324 324
    @shared_versions = nil
325 325
    @rolled_up_versions = nil
326 326
    @rolled_up_trackers = nil
327
    @rolled_up_statuses = nil
327 328
    @rolled_up_custom_fields = nil
328 329
    @all_issue_custom_fields = nil
329 330
    @all_time_entry_custom_fields = nil
......
452 453
      sorted
453 454
  end
454 455

  
456
  def rolled_up_statuses
457
    issue_status_ids = WorkflowTransition.
458
      where(:tracker_id => trackers).
459
      distinct.
460
      pluck(:old_status_id, :new_status_id).
461
      flatten.
462
      uniq
463

  
464
    IssueStatus.where(:id => issue_status_ids).sorted
465
  end
466

  
455 467
  # Closes open and locked project versions that are completed
456 468
  def close_completed_versions
457 469
    Version.transaction do
app/models/query.rb
551 551
    Version.sort_by_status(versions).collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s, l("version_status_#{s.status}")] }
552 552
  end
553 553

  
554
  # Returns a scope of issue statuses that are available as columns for filters
555
  def issue_statuses
556
    if project
557
      statuses = project.rolled_up_statuses
558
    else
559
      statuses = IssueStatus.all.sorted
560
    end
561
    statuses.collect{|s| [s.name, s.id.to_s]}
562
  end
563

  
554 564
  # Returns a scope of issue custom fields that are available as columns or filters
555 565
  def issue_custom_fields
556 566
    if project
test/unit/project_test.rb
483 483
    assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
484 484
  end
485 485

  
486
  def test_rolled_up_statuses
487
    project = Project.find(1)
488

  
489
    WorkflowTransition.delete_all
490
    WorkflowTransition.create(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
491
    WorkflowTransition.create(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
492
    WorkflowTransition.create(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
493
    WorkflowTransition.create(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :new_status_id => 3)
494

  
495
    assert_kind_of IssueStatus, project.rolled_up_statuses.first
496
    assert_equal IssueStatus.find(1), project.rolled_up_statuses.first
497

  
498
    assert_equal [1, 2, 3, 4], project.rolled_up_statuses.collect(&:id)
499
  end
500

  
486 501
  test "#rolled_up_trackers should ignore projects with issue_tracking module disabled" do
487 502
    parent = Project.generate!
488 503
    parent.trackers = Tracker.find([1, 2])
test/unit/query_test.rb
2093 2093
    issues = find_issues_with_query(query)
2094 2094
    assert_equal [1, 2, 5, 11, 12, 13], issues.map(&:id).sort
2095 2095
  end
2096

  
2097
  def test_issue_statuses_should_return_only_statuses_used_by_that_project
2098
    query = IssueQuery.new(:name => '_', :project => Project.find(1))
2099
    query.filters = {'status_id' => {:operator => '=', :values => []}}
2100

  
2101
    WorkflowTransition.delete_all
2102
    WorkflowTransition.create(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
2103
    WorkflowTransition.create(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
2104
    WorkflowTransition.create(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
2105
    WorkflowTransition.create(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :new_status_id => 3)
2106

  
2107
    assert_equal ['1','2','3','4'], query.available_filters['status_id'][:values].map(&:second)
2108
  end
2109

  
2110
  def test_issue_statuses_without_project_should_return_all_statuses
2111
    query = IssueQuery.new(:name => '_')
2112
    query.filters = {'status_id' => {:operator => '=', :values => []}}
2113

  
2114
    WorkflowTransition.delete_all
2115
    WorkflowTransition.create(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
2116
    WorkflowTransition.create(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
2117
    WorkflowTransition.create(:role_id => 1, :tracker_id => 1, :old_status_id => 2, :new_status_id => 3)
2118
    WorkflowTransition.create(:role_id => 1, :tracker_id => 2, :old_status_id => 1, :new_status_id => 3)
2119

  
2120
    assert_equal ['1','2','3','4','5','6'], query.available_filters['status_id'][:values].map(&:second)
2121
  end
2096 2122
end
(5-5/5)