@@ -1,91 +1,99 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class SearchController < ApplicationController |
|
18 | class SearchController < ApplicationController | |
19 | before_filter :find_optional_project |
|
19 | before_filter :find_optional_project | |
20 | accept_api_auth :index |
|
20 | accept_api_auth :index | |
21 |
|
21 | |||
22 | def index |
|
22 | def index | |
23 | @question = params[:q] || "" |
|
23 | @question = params[:q] || "" | |
24 | @question.strip! |
|
24 | @question.strip! | |
25 | @all_words = params[:all_words] ? params[:all_words].present? : true |
|
25 | @all_words = params[:all_words] ? params[:all_words].present? : true | |
26 | @titles_only = params[:titles_only] ? params[:titles_only].present? : false |
|
26 | @titles_only = params[:titles_only] ? params[:titles_only].present? : false | |
27 | @search_attachments = params[:attachments].presence || '0' |
|
27 | @search_attachments = params[:attachments].presence || '0' | |
28 | @open_issues = params[:open_issues] ? params[:open_issues].present? : false |
|
28 | @open_issues = params[:open_issues] ? params[:open_issues].present? : false | |
29 |
|
29 | |||
|
30 | case params[:format] | |||
|
31 | when 'xml', 'json' | |||
|
32 | @offset, @limit = api_offset_and_limit | |||
|
33 | else | |||
|
34 | @offset = nil | |||
|
35 | @limit = Setting.search_results_per_page.to_i | |||
|
36 | @limit = 10 if @limit == 0 | |||
|
37 | end | |||
|
38 | ||||
30 | # quick jump to an issue |
|
39 | # quick jump to an issue | |
31 | if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i)) |
|
40 | if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i)) | |
32 | redirect_to issue_path(issue) |
|
41 | redirect_to issue_path(issue) | |
33 | return |
|
42 | return | |
34 | end |
|
43 | end | |
35 |
|
44 | |||
36 | projects_to_search = |
|
45 | projects_to_search = | |
37 | case params[:scope] |
|
46 | case params[:scope] | |
38 | when 'all' |
|
47 | when 'all' | |
39 | nil |
|
48 | nil | |
40 | when 'my_projects' |
|
49 | when 'my_projects' | |
41 | User.current.projects |
|
50 | User.current.projects | |
42 | when 'subprojects' |
|
51 | when 'subprojects' | |
43 | @project ? (@project.self_and_descendants.active.to_a) : nil |
|
52 | @project ? (@project.self_and_descendants.active.to_a) : nil | |
44 | else |
|
53 | else | |
45 | @project |
|
54 | @project | |
46 | end |
|
55 | end | |
47 |
|
56 | |||
48 | @object_types = Redmine::Search.available_search_types.dup |
|
57 | @object_types = Redmine::Search.available_search_types.dup | |
49 | if projects_to_search.is_a? Project |
|
58 | if projects_to_search.is_a? Project | |
50 | # don't search projects |
|
59 | # don't search projects | |
51 | @object_types.delete('projects') |
|
60 | @object_types.delete('projects') | |
52 | # only show what the user is allowed to view |
|
61 | # only show what the user is allowed to view | |
53 | @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} |
|
62 | @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} | |
54 | end |
|
63 | end | |
55 |
|
64 | |||
56 | @scope = @object_types.select {|t| params[t]} |
|
65 | @scope = @object_types.select {|t| params[t]} | |
57 | @scope = @object_types if @scope.empty? |
|
66 | @scope = @object_types if @scope.empty? | |
58 |
|
67 | |||
59 | fetcher = Redmine::Search::Fetcher.new( |
|
68 | fetcher = Redmine::Search::Fetcher.new( | |
60 | @question, User.current, @scope, projects_to_search, |
|
69 | @question, User.current, @scope, projects_to_search, | |
61 | :all_words => @all_words, :titles_only => @titles_only, :attachments => @search_attachments, :open_issues => @open_issues, |
|
70 | :all_words => @all_words, :titles_only => @titles_only, :attachments => @search_attachments, :open_issues => @open_issues, | |
62 | :cache => params[:page].present? |
|
71 | :cache => params[:page].present? | |
63 | ) |
|
72 | ) | |
64 |
|
73 | |||
65 | if fetcher.tokens.present? |
|
74 | if fetcher.tokens.present? | |
66 | @result_count = fetcher.result_count |
|
75 | @result_count = fetcher.result_count | |
67 | @result_count_by_type = fetcher.result_count_by_type |
|
76 | @result_count_by_type = fetcher.result_count_by_type | |
68 | @tokens = fetcher.tokens |
|
77 | @tokens = fetcher.tokens | |
69 |
|
78 | |||
70 | per_page = Setting.search_results_per_page.to_i |
|
79 | @result_pages = Paginator.new @result_count, @limit, params['page'] | |
71 | per_page = 10 if per_page == 0 |
|
80 | @offset ||= @result_pages.offset | |
72 | @result_pages = Paginator.new @result_count, per_page, params['page'] |
|
81 | @results = fetcher.results(@offset, @result_pages.per_page) | |
73 | @results = fetcher.results(@result_pages.offset, @result_pages.per_page) |
|
|||
74 | else |
|
82 | else | |
75 | @question = "" |
|
83 | @question = "" | |
76 | end |
|
84 | end | |
77 | respond_to do |format| |
|
85 | respond_to do |format| | |
78 | format.html { render :layout => false if request.xhr? } |
|
86 | format.html { render :layout => false if request.xhr? } | |
79 | format.api { @results ||= []; render :layout => false } |
|
87 | format.api { @results ||= []; render :layout => false } | |
80 | end |
|
88 | end | |
81 | end |
|
89 | end | |
82 |
|
90 | |||
83 | private |
|
91 | private | |
84 | def find_optional_project |
|
92 | def find_optional_project | |
85 | return true unless params[:id] |
|
93 | return true unless params[:id] | |
86 | @project = Project.find(params[:id]) |
|
94 | @project = Project.find(params[:id]) | |
87 | check_project_privacy |
|
95 | check_project_privacy | |
88 | rescue ActiveRecord::RecordNotFound |
|
96 | rescue ActiveRecord::RecordNotFound | |
89 | render_404 |
|
97 | render_404 | |
90 | end |
|
98 | end | |
91 | end |
|
99 | end |
@@ -1,12 +1,12 | |||||
1 | api.array :results do |
|
1 | api.array :results, api_meta(:total_count => @result_count, :offset => @offset, :limit => @limit) do | |
2 | @results.each do |result| |
|
2 | @results.each do |result| | |
3 | api.result do |
|
3 | api.result do | |
4 | api.id result.id |
|
4 | api.id result.id | |
5 | api.title result.event_title |
|
5 | api.title result.event_title | |
6 | api.type result.event_type |
|
6 | api.type result.event_type | |
7 | api.url url_for(result.event_url(:only_path => false)) |
|
7 | api.url url_for(result.event_url(:only_path => false)) | |
8 | api.description result.event_description |
|
8 | api.description result.event_description | |
9 | api.datetime result.event_datetime |
|
9 | api.datetime result.event_datetime | |
10 | end |
|
10 | end | |
11 | end |
|
11 | end | |
12 | end |
|
12 | end |
@@ -1,74 +1,92 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.expand_path('../../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class Redmine::ApiTest::SearchTest < Redmine::ApiTest::Base |
|
20 | class Redmine::ApiTest::SearchTest < Redmine::ApiTest::Base | |
21 | fixtures :projects, :projects_trackers, |
|
21 | fixtures :projects, :projects_trackers, | |
22 | :enabled_modules, :roles, :users, :members, :member_roles, |
|
22 | :enabled_modules, :roles, :users, :members, :member_roles, | |
23 | :issues, :trackers, :issue_statuses, :enumerations, |
|
23 | :issues, :trackers, :issue_statuses, :enumerations, | |
24 | :workflows, |
|
24 | :workflows, | |
25 | :custom_fields, :custom_values, |
|
25 | :custom_fields, :custom_values, | |
26 | :custom_fields_projects, :custom_fields_trackers, |
|
26 | :custom_fields_projects, :custom_fields_trackers, | |
27 | :repositories, :changesets |
|
27 | :repositories, :changesets | |
28 |
|
28 | |||
29 | test "GET /search.xml should return xml content" do |
|
29 | test "GET /search.xml should return xml content" do | |
30 | get '/search.xml' |
|
30 | get '/search.xml' | |
31 |
|
31 | |||
32 | assert_response :success |
|
32 | assert_response :success | |
33 | assert_equal 'application/xml', @response.content_type |
|
33 | assert_equal 'application/xml', @response.content_type | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 | test "GET /search.json should return json content" do |
|
36 | test "GET /search.json should return json content" do | |
37 | get '/search.json' |
|
37 | get '/search.json' | |
38 |
|
38 | |||
39 | assert_response :success |
|
39 | assert_response :success | |
40 | assert_equal 'application/json', @response.content_type |
|
40 | assert_equal 'application/json', @response.content_type | |
41 |
|
41 | |||
42 | json = ActiveSupport::JSON.decode(response.body) |
|
42 | json = ActiveSupport::JSON.decode(response.body) | |
43 | assert_kind_of Hash, json |
|
43 | assert_kind_of Hash, json | |
44 | assert_kind_of Array, json['results'] |
|
44 | assert_kind_of Array, json['results'] | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | test "GET /search.xml without query strings should return empty results" do |
|
47 | test "GET /search.xml without query strings should return empty results" do | |
48 | get '/search.xml', :q => '', :all_words => '' |
|
48 | get '/search.xml', :q => '', :all_words => '' | |
49 |
|
49 | |||
50 | assert_response :success |
|
50 | assert_response :success | |
51 | assert_equal 0, assigns(:results).size |
|
51 | assert_equal 0, assigns(:results).size | |
52 | end |
|
52 | end | |
53 |
|
53 | |||
54 | test "GET /search.xml with query strings should return results" do |
|
54 | test "GET /search.xml with query strings should return results" do | |
55 | get '/search.xml', :q => 'recipe subproject commit', :all_words => '' |
|
55 | get '/search.xml', :q => 'recipe subproject commit', :all_words => '' | |
56 |
|
56 | |||
57 | assert_response :success |
|
57 | assert_response :success | |
58 | assert_not_empty(assigns(:results)) |
|
58 | assert_not_empty(assigns(:results)) | |
59 |
|
59 | |||
60 | assert_select 'results[type=array]' do |
|
60 | assert_select 'results[type=array]' do | |
61 | assert_select 'result', :count => assigns(:results).count |
|
61 | assert_select 'result', :count => assigns(:results).count | |
62 | assigns(:results).size.times.each do |i| |
|
62 | assigns(:results).size.times.each do |i| | |
63 | assert_select 'result' do |
|
63 | assert_select 'result' do | |
64 | assert_select 'id', :text => assigns(:results)[i].id.to_s |
|
64 | assert_select 'id', :text => assigns(:results)[i].id.to_s | |
65 | assert_select 'title', :text => assigns(:results)[i].event_title |
|
65 | assert_select 'title', :text => assigns(:results)[i].event_title | |
66 | assert_select 'type', :text => assigns(:results)[i].event_type |
|
66 | assert_select 'type', :text => assigns(:results)[i].event_type | |
67 | assert_select 'url', :text => url_for(assigns(:results)[i].event_url(:only_path => false)) |
|
67 | assert_select 'url', :text => url_for(assigns(:results)[i].event_url(:only_path => false)) | |
68 | assert_select 'description', :text => assigns(:results)[i].event_description |
|
68 | assert_select 'description', :text => assigns(:results)[i].event_description | |
69 | assert_select 'datetime' |
|
69 | assert_select 'datetime' | |
70 | end |
|
70 | end | |
71 | end |
|
71 | end | |
72 | end |
|
72 | end | |
73 | end |
|
73 | end | |
|
74 | ||||
|
75 | test "GET /search.xml should paginate" do | |||
|
76 | issue = (0..10).map {Issue.generate! :subject => 'search_with_limited_results'}.reverse.map(&:id) | |||
|
77 | ||||
|
78 | get '/search.json', :q => 'search_with_limited_results', :limit => 4 | |||
|
79 | json = ActiveSupport::JSON.decode(response.body) | |||
|
80 | assert_equal 11, json['total_count'] | |||
|
81 | assert_equal 0, json['offset'] | |||
|
82 | assert_equal 4, json['limit'] | |||
|
83 | assert_equal issue[0..3], json['results'].map {|r| r['id']} | |||
|
84 | ||||
|
85 | get '/search.json', :q => 'search_with_limited_results', :offset => 8, :limit => 4 | |||
|
86 | json = ActiveSupport::JSON.decode(response.body) | |||
|
87 | assert_equal 11, json['total_count'] | |||
|
88 | assert_equal 8, json['offset'] | |||
|
89 | assert_equal 4, json['limit'] | |||
|
90 | assert_equal issue[8..10], json['results'].map {|r| r['id']} | |||
|
91 | end | |||
74 | end |
|
92 | end |
General Comments 0
You need to be logged in to leave comments.
Login now