Project

General

Profile

Feature #5893 » filter-by-notes-with-tests-v2.patch

Original patch by Tomasz O, Alexander Achenbach, and Yuichi HARADA - Go MAEDA, 2021-03-09 10:25

View differences:

app/models/issue_query.rb
176 176
    ) if project
177 177
    add_available_filter "subject", :type => :text
178 178
    add_available_filter "description", :type => :text
179
    add_available_filter "notes", :type => :text
179 180
    add_available_filter "created_on", :type => :date_past
180 181
    add_available_filter "updated_on", :type => :date_past
181 182
    add_available_filter "closed_on", :type => :date_past
......
447 448
    raise StatementInvalid.new(e.message)
448 449
  end
449 450

  
451
  def sql_for_notes_field(field, operator, value)
452
    subquery = "SELECT 1 FROM #{Journal.table_name}" +
453
      " WHERE #{Journal.table_name}.journalized_type='Issue' AND #{Journal.table_name}.journalized_id=#{Issue.table_name}.id" +
454
      " AND (#{sql_for_field field, operator.ends_with?("~") ? "~" : "*", value, Journal.table_name, 'notes'})" +
455
      " AND (#{Journal.visible_notes_condition(User.current, :skip_pre_condition => true)})"
456
    "#{operator.starts_with?("!") ? "NOT EXISTS" : "EXISTS"} (#{subquery})"
457
  end
458

  
450 459
  def sql_for_updated_by_field(field, operator, value)
451 460
    neg = (operator == '!' ? 'NOT' : '')
452 461
    subquery = "SELECT 1 FROM #{Journal.table_name}" +
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')
(4-4/7)