##// END OF EJS Templates
Revert r13736, test fails with mysql....
Jean-Philippe Lang -
r13355:0b1b46ae2c18
parent child
Show More
@@ -1,297 +1,288
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 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 SearchControllerTest < ActionController::TestCase
20 class SearchControllerTest < ActionController::TestCase
21 fixtures :projects, :enabled_modules, :roles, :users, :members, :member_roles,
21 fixtures :projects, :enabled_modules, :roles, :users, :members, :member_roles,
22 :issues, :trackers, :issue_statuses, :enumerations,
22 :issues, :trackers, :issue_statuses, :enumerations,
23 :custom_fields, :custom_values,
23 :custom_fields, :custom_values,
24 :custom_fields_projects, :custom_fields_trackers,
24 :custom_fields_projects, :custom_fields_trackers,
25 :repositories, :changesets
25 :repositories, :changesets
26
26
27 def setup
27 def setup
28 User.current = nil
28 User.current = nil
29 end
29 end
30
30
31 def test_search_for_projects
31 def test_search_for_projects
32 get :index
32 get :index
33 assert_response :success
33 assert_response :success
34 assert_template 'index'
34 assert_template 'index'
35
35
36 get :index, :q => "cook"
36 get :index, :q => "cook"
37 assert_response :success
37 assert_response :success
38 assert_template 'index'
38 assert_template 'index'
39 assert assigns(:results).include?(Project.find(1))
39 assert assigns(:results).include?(Project.find(1))
40 end
40 end
41
41
42 def test_search_on_archived_project_should_return_404
42 def test_search_on_archived_project_should_return_404
43 Project.find(3).archive
43 Project.find(3).archive
44 get :index, :id => 3
44 get :index, :id => 3
45 assert_response 404
45 assert_response 404
46 end
46 end
47
47
48 def test_search_on_invisible_project_by_user_should_be_denied
48 def test_search_on_invisible_project_by_user_should_be_denied
49 @request.session[:user_id] = 7
49 @request.session[:user_id] = 7
50 get :index, :id => 2
50 get :index, :id => 2
51 assert_response 403
51 assert_response 403
52 end
52 end
53
53
54 def test_search_on_invisible_project_by_anonymous_user_should_redirect
54 def test_search_on_invisible_project_by_anonymous_user_should_redirect
55 get :index, :id => 2
55 get :index, :id => 2
56 assert_response 302
56 assert_response 302
57 end
57 end
58
58
59 def test_search_on_private_project_by_member_should_succeed
59 def test_search_on_private_project_by_member_should_succeed
60 @request.session[:user_id] = 2
60 @request.session[:user_id] = 2
61 get :index, :id => 2
61 get :index, :id => 2
62 assert_response :success
62 assert_response :success
63 end
63 end
64
64
65 def test_search_all_projects
65 def test_search_all_projects
66 with_settings :default_language => 'en' do
66 with_settings :default_language => 'en' do
67 get :index, :q => 'recipe subproject commit', :all_words => ''
67 get :index, :q => 'recipe subproject commit', :all_words => ''
68 end
68 end
69 assert_response :success
69 assert_response :success
70 assert_template 'index'
70 assert_template 'index'
71
71
72 assert assigns(:results).include?(Issue.find(2))
72 assert assigns(:results).include?(Issue.find(2))
73 assert assigns(:results).include?(Issue.find(5))
73 assert assigns(:results).include?(Issue.find(5))
74 assert assigns(:results).include?(Changeset.find(101))
74 assert assigns(:results).include?(Changeset.find(101))
75 assert_select 'dt.issue a', :text => /Add ingredients categories/
75 assert_select 'dt.issue a', :text => /Add ingredients categories/
76 assert_select 'dd', :text => /should be classified by categories/
76 assert_select 'dd', :text => /should be classified by categories/
77
77
78 assert assigns(:results_by_type).is_a?(Hash)
78 assert assigns(:results_by_type).is_a?(Hash)
79 assert_equal 5, assigns(:results_by_type)['changesets']
79 assert_equal 5, assigns(:results_by_type)['changesets']
80 assert_select 'a', :text => 'Changesets (5)'
80 assert_select 'a', :text => 'Changesets (5)'
81 end
81 end
82
82
83 def test_search_issues
83 def test_search_issues
84 get :index, :q => 'issue', :issues => 1
84 get :index, :q => 'issue', :issues => 1
85 assert_response :success
85 assert_response :success
86 assert_template 'index'
86 assert_template 'index'
87
87
88 assert_equal true, assigns(:all_words)
88 assert_equal true, assigns(:all_words)
89 assert_equal false, assigns(:titles_only)
89 assert_equal false, assigns(:titles_only)
90 assert assigns(:results).include?(Issue.find(8))
90 assert assigns(:results).include?(Issue.find(8))
91 assert assigns(:results).include?(Issue.find(5))
91 assert assigns(:results).include?(Issue.find(5))
92 assert_select 'dt.issue.closed a', :text => /Closed/
92 assert_select 'dt.issue.closed a', :text => /Closed/
93 end
93 end
94
94
95 def test_search_issues_should_search_notes
95 def test_search_issues_should_search_notes
96 Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
96 Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
97
97
98 get :index, :q => 'searchkeyword', :issues => 1
98 get :index, :q => 'searchkeyword', :issues => 1
99 assert_response :success
99 assert_response :success
100 assert_include Issue.find(2), assigns(:results)
100 assert_include Issue.find(2), assigns(:results)
101 end
101 end
102
102
103 def test_search_issues_with_multiple_matches_in_journals_should_return_issue_once
103 def test_search_issues_with_multiple_matches_in_journals_should_return_issue_once
104 Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
104 Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
105 Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
105 Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
106
106
107 get :index, :q => 'searchkeyword', :issues => 1
107 get :index, :q => 'searchkeyword', :issues => 1
108 assert_response :success
108 assert_response :success
109 assert_include Issue.find(2), assigns(:results)
109 assert_include Issue.find(2), assigns(:results)
110 assert_equal 1, assigns(:results).size
110 assert_equal 1, assigns(:results).size
111 end
111 end
112
112
113 def test_search_issues_should_search_private_notes_with_permission_only
113 def test_search_issues_should_search_private_notes_with_permission_only
114 Journal.create!(:journalized => Issue.find(2), :notes => 'Private notes with searchkeyword', :private_notes => true)
114 Journal.create!(:journalized => Issue.find(2), :notes => 'Private notes with searchkeyword', :private_notes => true)
115 @request.session[:user_id] = 2
115 @request.session[:user_id] = 2
116
116
117 Role.find(1).add_permission! :view_private_notes
117 Role.find(1).add_permission! :view_private_notes
118 get :index, :q => 'searchkeyword', :issues => 1
118 get :index, :q => 'searchkeyword', :issues => 1
119 assert_response :success
119 assert_response :success
120 assert_include Issue.find(2), assigns(:results)
120 assert_include Issue.find(2), assigns(:results)
121
121
122 Role.find(1).remove_permission! :view_private_notes
122 Role.find(1).remove_permission! :view_private_notes
123 get :index, :q => 'searchkeyword', :issues => 1
123 get :index, :q => 'searchkeyword', :issues => 1
124 assert_response :success
124 assert_response :success
125 assert_not_include Issue.find(2), assigns(:results)
125 assert_not_include Issue.find(2), assigns(:results)
126 end
126 end
127
127
128 def test_search_all_projects_with_scope_param
128 def test_search_all_projects_with_scope_param
129 get :index, :q => 'issue', :scope => 'all'
129 get :index, :q => 'issue', :scope => 'all'
130 assert_response :success
130 assert_response :success
131 assert_template 'index'
131 assert_template 'index'
132 assert assigns(:results).present?
132 assert assigns(:results).present?
133 end
133 end
134
134
135 def test_search_my_projects
135 def test_search_my_projects
136 @request.session[:user_id] = 2
136 @request.session[:user_id] = 2
137 get :index, :id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => ''
137 get :index, :id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => ''
138 assert_response :success
138 assert_response :success
139 assert_template 'index'
139 assert_template 'index'
140 assert assigns(:results).include?(Issue.find(1))
140 assert assigns(:results).include?(Issue.find(1))
141 assert !assigns(:results).include?(Issue.find(5))
141 assert !assigns(:results).include?(Issue.find(5))
142 end
142 end
143
143
144 def test_search_my_projects_without_memberships
144 def test_search_my_projects_without_memberships
145 # anonymous user has no memberships
145 # anonymous user has no memberships
146 get :index, :id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => ''
146 get :index, :id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => ''
147 assert_response :success
147 assert_response :success
148 assert_template 'index'
148 assert_template 'index'
149 assert assigns(:results).empty?
149 assert assigns(:results).empty?
150 end
150 end
151
151
152 def test_search_project_and_subprojects
152 def test_search_project_and_subprojects
153 get :index, :id => 1, :q => 'recipe subproject', :scope => 'subprojects', :all_words => ''
153 get :index, :id => 1, :q => 'recipe subproject', :scope => 'subprojects', :all_words => ''
154 assert_response :success
154 assert_response :success
155 assert_template 'index'
155 assert_template 'index'
156 assert assigns(:results).include?(Issue.find(1))
156 assert assigns(:results).include?(Issue.find(1))
157 assert assigns(:results).include?(Issue.find(5))
157 assert assigns(:results).include?(Issue.find(5))
158 end
158 end
159
159
160 def test_search_without_searchable_custom_fields
160 def test_search_without_searchable_custom_fields
161 CustomField.update_all "searchable = #{ActiveRecord::Base.connection.quoted_false}"
161 CustomField.update_all "searchable = #{ActiveRecord::Base.connection.quoted_false}"
162
162
163 get :index, :id => 1
163 get :index, :id => 1
164 assert_response :success
164 assert_response :success
165 assert_template 'index'
165 assert_template 'index'
166 assert_not_nil assigns(:project)
166 assert_not_nil assigns(:project)
167
167
168 get :index, :id => 1, :q => "can"
168 get :index, :id => 1, :q => "can"
169 assert_response :success
169 assert_response :success
170 assert_template 'index'
170 assert_template 'index'
171 end
171 end
172
172
173 def test_search_with_searchable_custom_fields
173 def test_search_with_searchable_custom_fields
174 get :index, :id => 1, :q => "stringforcustomfield"
174 get :index, :id => 1, :q => "stringforcustomfield"
175 assert_response :success
175 assert_response :success
176 results = assigns(:results)
176 results = assigns(:results)
177 assert_not_nil results
177 assert_not_nil results
178 assert_equal 1, results.size
178 assert_equal 1, results.size
179 assert results.include?(Issue.find(7))
179 assert results.include?(Issue.find(7))
180 end
180 end
181
181
182 def test_search_all_words
182 def test_search_all_words
183 # 'all words' is on by default
183 # 'all words' is on by default
184 get :index, :id => 1, :q => 'recipe updating saving', :all_words => '1'
184 get :index, :id => 1, :q => 'recipe updating saving', :all_words => '1'
185 assert_equal true, assigns(:all_words)
185 assert_equal true, assigns(:all_words)
186 results = assigns(:results)
186 results = assigns(:results)
187 assert_not_nil results
187 assert_not_nil results
188 assert_equal 1, results.size
188 assert_equal 1, results.size
189 assert results.include?(Issue.find(3))
189 assert results.include?(Issue.find(3))
190 end
190 end
191
191
192 def test_search_one_of_the_words
192 def test_search_one_of_the_words
193 get :index, :id => 1, :q => 'recipe updating saving', :all_words => ''
193 get :index, :id => 1, :q => 'recipe updating saving', :all_words => ''
194 assert_equal false, assigns(:all_words)
194 assert_equal false, assigns(:all_words)
195 results = assigns(:results)
195 results = assigns(:results)
196 assert_not_nil results
196 assert_not_nil results
197 assert_equal 3, results.size
197 assert_equal 3, results.size
198 assert results.include?(Issue.find(3))
198 assert results.include?(Issue.find(3))
199 end
199 end
200
200
201 def test_search_titles_only_without_result
201 def test_search_titles_only_without_result
202 get :index, :id => 1, :q => 'recipe updating saving', :titles_only => '1'
202 get :index, :id => 1, :q => 'recipe updating saving', :titles_only => '1'
203 results = assigns(:results)
203 results = assigns(:results)
204 assert_not_nil results
204 assert_not_nil results
205 assert_equal 0, results.size
205 assert_equal 0, results.size
206 end
206 end
207
207
208 def test_search_titles_only
208 def test_search_titles_only
209 get :index, :id => 1, :q => 'recipe', :titles_only => '1'
209 get :index, :id => 1, :q => 'recipe', :titles_only => '1'
210 assert_equal true, assigns(:titles_only)
210 assert_equal true, assigns(:titles_only)
211 results = assigns(:results)
211 results = assigns(:results)
212 assert_not_nil results
212 assert_not_nil results
213 assert_equal 2, results.size
213 assert_equal 2, results.size
214 end
214 end
215
215
216 def test_search_content
216 def test_search_content
217 Issue.where(:id => 1).update_all("description = 'This is a searchkeywordinthecontent'")
217 Issue.where(:id => 1).update_all("description = 'This is a searchkeywordinthecontent'")
218 get :index, :id => 1, :q => 'searchkeywordinthecontent', :titles_only => ''
218 get :index, :id => 1, :q => 'searchkeywordinthecontent', :titles_only => ''
219 assert_equal false, assigns(:titles_only)
219 assert_equal false, assigns(:titles_only)
220 results = assigns(:results)
220 results = assigns(:results)
221 assert_not_nil results
221 assert_not_nil results
222 assert_equal 1, results.size
222 assert_equal 1, results.size
223 end
223 end
224
224
225 def test_search_with_offset
225 def test_search_with_offset
226 get :index, :q => 'coo', :offset => '20080806073000'
226 get :index, :q => 'coo', :offset => '20080806073000'
227 assert_response :success
227 assert_response :success
228 results = assigns(:results)
228 results = assigns(:results)
229 assert results.any?
229 assert results.any?
230 assert results.map(&:event_datetime).max < '20080806T073000'.to_time
230 assert results.map(&:event_datetime).max < '20080806T073000'.to_time
231 end
231 end
232
232
233 def test_search_previous_with_offset
233 def test_search_previous_with_offset
234 get :index, :q => 'coo', :offset => '20080806073000', :previous => '1'
234 get :index, :q => 'coo', :offset => '20080806073000', :previous => '1'
235 assert_response :success
235 assert_response :success
236 results = assigns(:results)
236 results = assigns(:results)
237 assert results.any?
237 assert results.any?
238 assert results.map(&:event_datetime).min >= '20080806T073000'.to_time
238 assert results.map(&:event_datetime).min >= '20080806T073000'.to_time
239 end
239 end
240
240
241 def test_search_with_limited_results
242 issues = (0..24).map {|i| Issue.generate!(:subject => 'search_with_limited_results')}.reverse
243
244 get :index, :q => 'limited_results'
245 assert_response :success
246 assert_equal 10, assigns(:results).size
247 assert_equal issues[0..9], assigns(:results)
248 end
249
250 def test_search_with_invalid_project_id
241 def test_search_with_invalid_project_id
251 get :index, :id => 195, :q => 'recipe'
242 get :index, :id => 195, :q => 'recipe'
252 assert_response 404
243 assert_response 404
253 assert_nil assigns(:results)
244 assert_nil assigns(:results)
254 end
245 end
255
246
256 def test_quick_jump_to_issue
247 def test_quick_jump_to_issue
257 # issue of a public project
248 # issue of a public project
258 get :index, :q => "3"
249 get :index, :q => "3"
259 assert_redirected_to '/issues/3'
250 assert_redirected_to '/issues/3'
260
251
261 # issue of a private project
252 # issue of a private project
262 get :index, :q => "4"
253 get :index, :q => "4"
263 assert_response :success
254 assert_response :success
264 assert_template 'index'
255 assert_template 'index'
265 end
256 end
266
257
267 def test_large_integer
258 def test_large_integer
268 get :index, :q => '4615713488'
259 get :index, :q => '4615713488'
269 assert_response :success
260 assert_response :success
270 assert_template 'index'
261 assert_template 'index'
271 end
262 end
272
263
273 def test_tokens_with_quotes
264 def test_tokens_with_quotes
274 get :index, :id => 1, :q => '"good bye" hello "bye bye"'
265 get :index, :id => 1, :q => '"good bye" hello "bye bye"'
275 assert_equal ["good bye", "hello", "bye bye"], assigns(:tokens)
266 assert_equal ["good bye", "hello", "bye bye"], assigns(:tokens)
276 end
267 end
277
268
278 def test_results_should_be_escaped_once
269 def test_results_should_be_escaped_once
279 assert Issue.find(1).update_attributes(:subject => '<subject> escaped_once', :description => '<description> escaped_once')
270 assert Issue.find(1).update_attributes(:subject => '<subject> escaped_once', :description => '<description> escaped_once')
280 get :index, :q => 'escaped_once'
271 get :index, :q => 'escaped_once'
281 assert_response :success
272 assert_response :success
282 assert_select '#search-results' do
273 assert_select '#search-results' do
283 assert_select 'dt.issue a', :text => /&lt;subject&gt;/
274 assert_select 'dt.issue a', :text => /&lt;subject&gt;/
284 assert_select 'dd', :text => /&lt;description&gt;/
275 assert_select 'dd', :text => /&lt;description&gt;/
285 end
276 end
286 end
277 end
287
278
288 def test_keywords_should_be_highlighted
279 def test_keywords_should_be_highlighted
289 assert Issue.find(1).update_attributes(:subject => 'subject highlighted', :description => 'description highlighted')
280 assert Issue.find(1).update_attributes(:subject => 'subject highlighted', :description => 'description highlighted')
290 get :index, :q => 'highlighted'
281 get :index, :q => 'highlighted'
291 assert_response :success
282 assert_response :success
292 assert_select '#search-results' do
283 assert_select '#search-results' do
293 assert_select 'dt.issue a span.highlight', :text => 'highlighted'
284 assert_select 'dt.issue a span.highlight', :text => 'highlighted'
294 assert_select 'dd span.highlight', :text => 'highlighted'
285 assert_select 'dd span.highlight', :text => 'highlighted'
295 end
286 end
296 end
287 end
297 end
288 end
General Comments 0
You need to be logged in to leave comments. Login now