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