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