summaryrefslogtreecommitdiffstats
path: root/test/functional/search_controller_test.rb
blob: 926593647bf6fbb11282a2425c9f825db0dc06df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-2017  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require File.expand_path('../../test_helper', __FILE__)

class SearchControllerTest < Redmine::ControllerTest
  fixtures :projects, :projects_trackers,
           :enabled_modules, :roles, :users, :members, :member_roles,
           :issues, :trackers, :issue_statuses, :enumerations,
           :workflows,
           :custom_fields, :custom_values,
           :custom_fields_projects, :custom_fields_trackers,
           :repositories, :changesets

  def setup
    User.current = nil
  end

  def test_search_without_q_should_display_search_form
    get :index
    assert_response :success
    assert_select '#content input[name=q]'
  end

  def test_search_for_projects
    get :index, :params => {:q => "cook"}
    assert_response :success
    assert_select '#search-results dt.project a', :text => /eCookbook/
  end

  def test_search_on_archived_project_should_return_403
    Project.find(3).archive
    get :index, :params => {:id => 3}
    assert_response 403
  end

  def test_search_on_invisible_project_by_user_should_be_denied
    @request.session[:user_id] = 7
    get :index, :params => {:id => 2}
    assert_response 403
  end

  def test_search_on_invisible_project_by_anonymous_user_should_redirect
    get :index, :params => {:id => 2}
    assert_response 302
  end

  def test_search_on_private_project_by_member_should_succeed
    @request.session[:user_id] = 2
    get :index, :params => {:id => 2}
    assert_response :success
  end

  def test_search_all_projects
    with_settings :default_language => 'en' do
      get :index, :params => {:q => 'recipe subproject commit', :all_words => ''}
    end
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue a', :text => /Feature request #2/
      assert_select 'dt.issue a', :text => /Bug #5/
      assert_select 'dt.changeset a', :text => /Revision 1/

      assert_select 'dt.issue a', :text => /Add ingredients categories/
      assert_select 'dd', :text => /should be classified by categories/
    end

    assert_select '#search-results-counts' do
      assert_select 'a', :text => 'Changesets (5)'
    end
  end

  def test_search_issues
    get :index, :params => {:q => 'issue', :issues => 1}
    assert_response :success

    assert_select 'input[name=all_words][checked=checked]'
    assert_select 'input[name=titles_only]:not([checked])'

    assert_select '#search-results' do
      assert_select 'dt.issue a', :text => /Bug #5/
      assert_select 'dt.issue-closed a', :text => /Bug #8 \(Closed\)/
    end
  end

  def test_search_issues_should_search_notes
    Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')

    get :index, :params => {:q => 'searchkeyword', :issues => 1}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue a', :text => /Feature request #2/
    end
  end

  def test_search_issues_with_multiple_matches_in_journals_should_return_issue_once
    Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
    Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')

    get :index, :params => {:q => 'searchkeyword', :issues => 1}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue a', :text => /Feature request #2/
      assert_select 'dt', 1
    end
  end

  def test_search_issues_should_search_private_notes_with_permission_only
    Journal.create!(:journalized => Issue.find(2), :notes => 'Private notes with searchkeyword', :private_notes => true)
    @request.session[:user_id] = 2

    Role.find(1).add_permission! :view_private_notes
    get :index, :params => {:q => 'searchkeyword', :issues => 1}
    assert_response :success
    assert_select '#search-results' do
      assert_select 'dt.issue a', :text => /Feature request #2/
    end

    Role.find(1).remove_permission! :view_private_notes
    get :index, :params => {:q => 'searchkeyword', :issues => 1}
    assert_response :success
    assert_select '#search-results' do
      assert_select 'dt', :text => /Feature request #2/, :count => 0
    end
  end

  def test_search_all_projects_with_scope_param
    get :index, :params => {:q => 'issue', :scope => 'all'}
    assert_response :success

    assert_select '#search-results dt'
  end

  def test_search_my_projects
    @request.session[:user_id] = 2
    get :index, :params => {:id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => ''}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => /Bug #1/
      assert_select 'dt', :text => /Bug #5/, :count => 0
    end
  end

  def test_search_my_projects_without_memberships
    # anonymous user has no memberships
    get :index, :params => {:id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => ''}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt', 0
    end
  end

  def test_search_project_and_subprojects
    get :index, :params => {:id => 1, :q => 'recipe subproject', :scope => 'subprojects', :all_words => ''}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => /Bug #1/
      assert_select 'dt.issue', :text => /Bug #5/
    end
  end

  def test_search_without_searchable_custom_fields
    CustomField.update_all :searchable => false

    get :index, :params => {:id => 1}
    assert_response :success

    get :index, :params => {:id => 1, :q => "can"}
    assert_response :success
  end

  def test_search_with_searchable_custom_fields
    get :index, :params => {:id => 1, :q => "stringforcustomfield"}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => /#7/
      assert_select 'dt', 1
    end
  end

  def test_search_without_attachments
    issue = Issue.generate! :subject => 'search_attachments'
    attachment = Attachment.generate! :container => Issue.find(1), :filename => 'search_attachments.patch'

    get :index, :params => {:id => 1, :q => 'search_attachments', :attachments => '0'}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => /##{issue.id}/
      assert_select 'dt', 1
    end
  end

  def test_search_attachments_only
    issue = Issue.generate! :subject => 'search_attachments'
    attachment = Attachment.generate! :container => Issue.find(1), :filename => 'search_attachments.patch'

    get :index, :params => {:id => 1, :q => 'search_attachments', :attachments => 'only'}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => / #1 /
      assert_select 'dt', 1
    end
  end

  def test_search_with_attachments
    issue = Issue.generate! :subject => 'search_attachments'
    Attachment.generate! :container => Issue.find(1), :filename => 'search_attachments.patch'

    get :index, :params => {:id => 1, :q => 'search_attachments', :attachments => '1'}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => / #1 /
      assert_select 'dt.issue', :text => / ##{issue.id} /
      assert_select 'dt', 2
    end
  end

  def test_search_open_issues
    Issue.generate! :subject => 'search_open'
    Issue.generate! :subject => 'search_open', :status_id => 5

    get :index, :params => {:id => 1, :q => 'search_open', :open_issues => '1'}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt', 1
    end
  end

  def test_search_all_words
    # 'all words' is on by default
    get :index, :params => {:id => 1, :q => 'recipe updating saving', :all_words => '1'}
    assert_response :success

    assert_select 'input[name=all_words][checked=checked]'
    assert_select '#search-results' do
      assert_select 'dt.issue', :text => / #3 /
      assert_select 'dt', 1
    end
  end

  def test_search_one_of_the_words
    get :index, :params => {:id => 1, :q => 'recipe updating saving', :all_words => ''}
    assert_response :success

    assert_select 'input[name=all_words]:not([checked])'
    assert_select '#search-results' do
      assert_select 'dt.issue', :text => / #3 /
      assert_select 'dt', 3
    end
  end

  def test_search_titles_only_without_result
    get :index, :params => {:id => 1, :q => 'recipe updating saving', :titles_only => '1'}
    assert_response :success

    assert_select 'input[name=titles_only][checked=checked]'
    assert_select '#search-results' do
      assert_select 'dt', 0
    end
  end

  def test_search_titles_only
    get :index, :params => {:id => 1, :q => 'recipe', :titles_only => '1'}
    assert_response :success

    assert_select 'input[name=titles_only][checked=checked]'
    assert_select '#search-results' do
      assert_select 'dt', 2
    end
  end

  def test_search_content
    Issue.where(:id => 1).update_all("description = 'This is a searchkeywordinthecontent'")

    get :index, :params => {:id => 1, :q => 'searchkeywordinthecontent', :titles_only => ''}
    assert_response :success

    assert_select 'input[name=titles_only]:not([checked])'
    assert_select '#search-results' do
      assert_select 'dt.issue', :text => / #1 /
      assert_select 'dt', 1
    end
  end

  def test_search_with_pagination
    issues = (0..24).map {Issue.generate! :subject => 'search_with_limited_results'}.reverse

    get :index, :params => {:q => 'search_with_limited_results'}
    assert_response :success
    issues[0..9].each do |issue|
      assert_select '#search-results dt.issue', :text => / ##{issue.id} /
    end

    get :index, :params => {:q => 'search_with_limited_results', :page => 2}
    assert_response :success
    issues[10..19].each do |issue|
      assert_select '#search-results dt.issue', :text => / ##{issue.id} /
    end

    get :index, :params => {:q => 'search_with_limited_results', :page => 3}
    assert_response :success
    issues[20..24].each do |issue|
      assert_select '#search-results dt.issue', :text => / ##{issue.id} /
    end

    get :index, :params => {:q => 'search_with_limited_results', :page => 4}
    assert_response :success
    assert_select '#search-results dt', 0
  end

  def test_search_with_invalid_project_id
    get :index, :params => {:id => 195, :q => 'recipe'}
    assert_response 404
  end

  def test_search_should_include_closed_projects
    @request.session[:user_id] = 1

    project = Project.find(5)
    project.close
    project.save

    # scope all
    get :index, :params => {:q => 'Issue of a private subproject', :scope => 'all'}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => /Bug #6/
    end

    # scope my_projects
    get :index, :params => {:q => 'Issue of a private subproject', :scope => 'my_projects'}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => /Bug #6/
    end

    # scope subprojects
    get :index, :params => {:id => 1, :q => 'Issue of a private subproject', :scope => 'subprojects'}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => /Bug #6/
    end

    # scope project
    get :index, :params => {:id => 5, :q => 'Issue of a private subproject'}
    assert_response :success

    assert_select '#search-results' do
      assert_select 'dt.issue', :text => /Bug #6/
    end
  end

  def test_quick_jump_to_issue
    # issue of a public project
    get :index, :params => {:q => "3"}
    assert_redirected_to '/issues/3'

    # issue of a private project
    get :index, :params => {:q => "4"}
    assert_response :success
  end

  def test_large_integer
    get :index, :params => {:q => '4615713488'}
    assert_response :success
  end

  def test_tokens_with_quotes
    issue1 = Issue.generate! :subject => 'say hello'
    issue2 = Issue.generate! :subject => 'say good bye'
    issue3 = Issue.generate! :subject => 'say goodbye'

    get :index, :params => {:q => '"good bye" hello "bye bye"', :all_words => ''}
    assert_response :success
    assert_select '#search-results' do
      assert_select 'dt.issue a', :text => / ##{issue1.id} /
      assert_select 'dt.issue a', :text => / ##{issue2.id} /
      assert_select 'dt.issue a', :text => / ##{issue3.id} /, :count => 0
    end
  end

  def test_results_should_be_escaped_once
    assert Issue.find(1).update_attributes(:subject => '<subject> escaped_once', :description => '<description> escaped_once')
    get :index, :params => {:q => 'escaped_once'}
    assert_response :success
    assert_select '#search-results' do
      assert_select 'dt.issue a', :text => /<subject>/
      assert_select 'dd', :text => /<description>/
    end
  end

  def test_keywords_should_be_highlighted
    assert Issue.find(1).update_attributes(:subject => 'subject highlighted', :description => 'description highlighted')
    get :index, :params => {:q => 'highlighted'}
    assert_response :success
    assert_select '#search-results' do
      assert_select 'dt.issue a span.highlight', :text => 'highlighted'
      assert_select 'dd span.highlight', :text => 'highlighted'
    end
  end
end