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