From a118c4ccb093306073d22cbbc69e5d4d4b066131 Mon Sep 17 00:00:00 2001 From: Etienne Massip Date: Wed, 12 Oct 2011 23:45:47 +0000 Subject: [PATCH] Fixed shot filter expression parsing depending upon field operators (#8371). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7624 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/query.rb | 9 ++- test/functional/issues_controller_test.rb | 74 +++++++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/app/models/query.rb b/app/models/query.rb index c79677e2b..9eac4b81c 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -308,9 +308,12 @@ class Query < ActiveRecord::Base end def add_short_filter(field, expression) - return unless expression - parms = expression.scan(/^(o|c|!\*|!|\*)?(.*)$/).first - add_filter field, (parms[0] || "="), [parms[1] || ""] + return unless expression && available_filters.has_key?(field) + field_type = available_filters[field][:type] + @@operators_by_filter_type[field_type].sort.reverse.detect do |operator| + next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ + add_filter field, operator, $1.present? ? $1.split('|') : [''] + end || add_filter(field, '=', expression.split('|')) end # Add multiple filters using +add_filter+ diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index 7e67b8ce8..692212134 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -144,6 +144,80 @@ class IssuesControllerTest < ActionController::TestCase assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters) end + def test_index_with_short_filters + + to_test = { + 'status_id' => { + 'o' => { :op => 'o', :values => [''] }, + 'c' => { :op => 'c', :values => [''] }, + '7' => { :op => '=', :values => ['7'] }, + '7|3|4' => { :op => '=', :values => ['7', '3', '4'] }, + '=7' => { :op => '=', :values => ['7'] }, + '!3' => { :op => '!', :values => ['3'] }, + '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }}, + 'subject' => { + 'This is a subject' => { :op => '=', :values => ['This is a subject'] }, + 'o' => { :op => '=', :values => ['o'] }, + '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] }, + '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }}, + 'tracker_id' => { + '3' => { :op => '=', :values => ['3'] }, + '=3' => { :op => '=', :values => ['3'] }, + '*' => { :op => '=', :values => ['*'] }, + '!*' => { :op => '!', :values => ['*'] }}, + 'start_date' => { + '2011-10-12' => { :op => '=', :values => ['2011-10-12'] }, + '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] }, + '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] }, + '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] }, + '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] }, + ' { :op => ' ['2'] }, + '>t+2' => { :op => '>t+', :values => ['2'] }, + 't+2' => { :op => 't+', :values => ['2'] }, + 't' => { :op => 't', :values => [''] }, + 'w' => { :op => 'w', :values => [''] }, + '>t-2' => { :op => '>t-', :values => ['2'] }, + ' { :op => ' ['2'] }, + 't-2' => { :op => 't-', :values => ['2'] }}, + 'created_on' => { + '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] }, + ' { :op => '=', :values => ['t+2' => { :op => '=', :values => ['>t+2'] }, + 't+2' => { :op => 't', :values => ['+2'] }}, + 'cf_1' => { + 'c' => { :op => '=', :values => ['c'] }, + '!c' => { :op => '!', :values => ['c'] }, + '!*' => { :op => '!*', :values => [''] }, + '*' => { :op => '*', :values => [''] }}, + 'estimated_hours' => { + '=13.4' => { :op => '=', :values => ['13.4'] }, + '>=45' => { :op => '>=', :values => ['45'] }, + '<=125' => { :op => '<=', :values => ['125'] }, + '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] }, + '!*' => { :op => '!*', :values => [''] }, + '*' => { :op => '*', :values => [''] }} + } + + default_filter = { 'status_id' => {:operator => 'o', :values => [''] }} + + to_test.each do |field, expression_and_expected| + expression_and_expected.each do |filter_expression, expected| + + get :index, :set_filter => 1, field => filter_expression + + assert_response :success + assert_template 'index' + assert_not_nil assigns(:issues) + + query = assigns(:query) + assert_not_nil query + assert query.has_filter?(field) + assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters) + end + end + + end + def test_index_with_project_and_empty_filters get :index, :project_id => 1, :set_filter => 1, :fields => [''] assert_response :success -- 2.39.5