filter-by-notes-with-tests.patch

Yuichi HARADA, 2021-03-09 01:43

Download (3.39 KB)

View differences:

app/models/issue_query.rb
189 189

  
190 190
    add_available_filter "done_ratio", :type => :integer
191 191

  
192
    add_available_filter "notes", :type => :text
193

  
192 194
    if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
193 195
      User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
194 196
      add_available_filter(
......
564 566
    "(#{nl} #{sql_for_field("fixed_version_id", "=", version_ids, Issue.table_name, "fixed_version_id")})"
565 567
  end
566 568

  
569
  def sql_for_notes_field(field, operator, value)
570
    subquery = "SELECT 1 FROM #{Journal.table_name}" +
571
      " WHERE #{Journal.table_name}.journalized_type='Issue' AND #{Journal.table_name}.journalized_id=#{Issue.table_name}.id" +
572
      " AND (#{sql_for_field field, operator.ends_with?("~") ? "~" : "*", value, Journal.table_name, 'notes'})" +
573
      " AND (#{Journal.visible_notes_condition(User.current, :skip_pre_condition => true)})"
574
    "#{operator.starts_with?("!") ? "NOT EXISTS" : "EXISTS"} (#{subquery})"
575
  end
576

  
567 577
  def sql_for_is_private_field(field, operator, value)
568 578
    op = (operator == "=" ? 'IN' : 'NOT IN')
569 579
    va =
test/unit/query_test.rb
803 803
    end
804 804
  end
805 805

  
806
  def test_filter_notes
807
    user = User.generate!
808
    Journal.create!(:user_id => user.id, :journalized => Issue.find(2), :notes => 'Notes')
809
    Journal.create!(:user_id => user.id, :journalized => Issue.find(3), :notes => 'Notes')
810

  
811
    issue_journals = Issue.find(1).journals.sort
812
    assert_equal ['Journal notes', 'Some notes with Redmine links: #2, r2.'], issue_journals.map(&:notes)
813
    assert_equal [false, false], issue_journals.map(&:private_notes)
814

  
815
    query = IssueQuery.new(:name => '_')
816
    filter_name = "notes"
817
    assert_include filter_name, query.available_filters.keys
818

  
819
    query.filters = {filter_name => {:operator => '~', :values => ['Notes']}}
820
    assert_equal [1, 2, 3], find_issues_with_query(query).map(&:id).sort
821

  
822
    query.filters = {filter_name => {:operator => '!~', :values => ['Notes']}}
823
    assert_equal (Issue.ids.sort - [1, 2, 3]), find_issues_with_query(query).map(&:id).sort
824
  end
825

  
826
  def test_filter_notes_should_ignore_private_notes_that_are_not_visible
827
    user = User.generate!
828
    Journal.create!(:user_id => user.id, :journalized => Issue.find(2), :notes => 'Notes', :private_notes => true)
829
    Journal.create!(:user_id => user.id, :journalized => Issue.find(3), :notes => 'Notes')
830

  
831
    issue_journals = Issue.find(1).journals.sort
832
    assert_equal ['Journal notes', 'Some notes with Redmine links: #2, r2.'], issue_journals.map(&:notes)
833
    assert_equal [false, false], issue_journals.map(&:private_notes)
834

  
835
    query = IssueQuery.new(:name => '_')
836
    filter_name = "notes"
837
    assert_include filter_name, query.available_filters.keys
838

  
839
    query.filters = {filter_name => {:operator => '~', :values => ['Notes']}}
840
    assert_equal [1, 3], find_issues_with_query(query).map(&:id).sort
841
  end
842

  
806 843
  def test_filter_updated_by
807 844
    user = User.generate!
808 845
    Journal.create!(:user_id => user.id, :journalized => Issue.find(2), :notes => 'Notes')