filter-by-notes-with-tests-v3.patch

Yuichi HARADA, 2021-03-15 05:42

Download (3.4 KB)

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.sub(/^!/, ''), value, Journal.table_name, 'notes'})" +
455
      " AND (#{Journal.visible_notes_condition(User.current, :skip_pre_condition => true)})"
456
    "#{/^!/.match?(operator) ? "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
    {
820
      '~' => [1, 2, 3],
821
      '!~' => Issue.ids.sort - [1, 2, 3],
822
      '^' => [2, 3],
823
      '$' => [1],
824
    }.each do |operator, expected|
825
      query.filters = {filter_name => {:operator => operator, :values => ['Notes']}}
826
      assert_equal expected, find_issues_with_query(query).map(&:id).sort
827
    end
828
  end
829

  
830
  def test_filter_notes_should_ignore_private_notes_that_are_not_visible
831
    user = User.generate!
832
    Journal.create!(:user_id => user.id, :journalized => Issue.find(2), :notes => 'Notes.', :private_notes => true)
833
    Journal.create!(:user_id => user.id, :journalized => Issue.find(3), :notes => 'Notes.')
834

  
835
    issue_journals = Issue.find(1).journals.sort
836
    assert_equal ['Journal notes', 'Some notes with Redmine links: #2, r2.'], issue_journals.map(&:notes)
837
    assert_equal [false, false], issue_journals.map(&:private_notes)
838

  
839
    query = IssueQuery.new(:name => '_')
840
    filter_name = 'notes'
841
    assert_include filter_name, query.available_filters.keys
842

  
843
    query.filters = {filter_name => {:operator => '~', :values => ['Notes']}}
844
    assert_equal [1, 3], find_issues_with_query(query).map(&:id).sort
845
  end
846

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