Project

General

Profile

Defect #31778 » 0001-Limit-total_estimated_hours-to-visible-issues.patch

Felix Schäfer, 2019-07-23 17:04

View differences:

app/models/issue.rb
1073 1073
    if leaf?
1074 1074
      estimated_hours
1075 1075
    else
1076
      @total_estimated_hours ||= self_and_descendants.sum(:estimated_hours)
1076
      @total_estimated_hours ||= self_and_descendants.visible.sum(:estimated_hours)
1077 1077
    end
1078 1078
  end
1079 1079

  
app/models/issue_query.rb
37 37
    QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
38 38
    QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours", :totalable => true),
39 39
    QueryColumn.new(:total_estimated_hours,
40
      :sortable => "COALESCE((SELECT SUM(estimated_hours) FROM #{Issue.table_name} subtasks" +
41
        " WHERE subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt), 0)",
40
      :sortable => -> { "COALESCE((SELECT SUM(estimated_hours) FROM #{Issue.table_name} subtasks" +
41
        " WHERE #{Issue.visible_condition(User.current).gsub(/\bissues\b/, 'subtasks')} AND subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt), 0)" },
42 42
      :default_order => 'desc'),
43 43
    QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
44 44
    QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
test/unit/issue_subtasking_test.rb
332 332
    end
333 333
  end
334 334

  
335
  def test_parent_total_estimated_hours_should_be_sum_of_descendants
335
  def test_parent_total_estimated_hours_should_be_sum_of_visible_descendants
336 336
    parent = Issue.generate!
337 337
    parent.generate_child!(:estimated_hours => nil)
338 338
    assert_equal 0, parent.reload.total_estimated_hours
......
340 340
    assert_equal 5, parent.reload.total_estimated_hours
341 341
    parent.generate_child!(:estimated_hours => 7)
342 342
    assert_equal 12, parent.reload.total_estimated_hours
343

  
344
    parent.generate_child!(:estimated_hours => 9, :is_private => true)
345
    assert_equal 12, parent.reload.total_estimated_hours
343 346
  end
344 347

  
345 348
  def test_open_issue_with_closed_parent_should_not_validate
test/unit/query_test.rb
1558 1558
    assert_equal values.sort, values
1559 1559
  end
1560 1560

  
1561
  def test_sort_by_total_for_estimated_hours
1562
    # Prepare issues
1563
    parent = issues(:issues_001)
1564
    child = issues(:issues_002)
1565
    private_child = issues(:issues_003)
1566
    other = issues(:issues_007)
1567

  
1568
    User.current = users(:users_001)
1569

  
1570
    parent.safe_attributes         = {:estimated_hours => 1}
1571
    child.safe_attributes          = {:estimated_hours => 2, :parent_issue_id => 1}
1572
    private_child.safe_attributes  = {:estimated_hours => 4, :parent_issue_id => 1, :is_private => true}
1573
    other.safe_attributes          = {:estimated_hours => 5}
1574

  
1575
    [parent, child, private_child, other].each(&:save!)
1576

  
1577

  
1578
    q = IssueQuery.new(
1579
      :name => '_',
1580
      :filters => { 'issue_id' => {:operator => '=', :values => ['1,7']} },
1581
      :sort_criteria => [['total_estimated_hours', 'asc']]
1582
    )
1583

  
1584
    # With private_child, `parent' is "bigger" than `other'
1585
    ids = q.issue_ids
1586
    assert_equal [7, 1], ids, "Private issue was not used to calculate sort order"
1587

  
1588
    # Without the invisible private_child, `other' is "bigger" than `parent'
1589
    User.current = User.anonymous
1590
    ids = q.issue_ids
1591
    assert_equal [1, 7], ids, "Private issue was used to calculate sort order"
1592
  end
1593

  
1561 1594
  def test_set_totalable_names
1562 1595
    q = IssueQuery.new
1563 1596
    q.totalable_names = ['estimated_hours', :spent_hours, '']
    (1-1/1)