You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_controller_test.rb 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class SearchControllerTest < Redmine::ControllerTest
  19. fixtures :projects, :projects_trackers,
  20. :enabled_modules, :roles, :users, :members, :member_roles,
  21. :issues, :trackers, :issue_statuses, :enumerations,
  22. :workflows,
  23. :custom_fields, :custom_values,
  24. :custom_fields_projects, :custom_fields_trackers,
  25. :repositories, :changesets
  26. def setup
  27. User.current = nil
  28. end
  29. def test_search_without_q_should_display_search_form
  30. get :index
  31. assert_response :success
  32. assert_select '#content input[name=q]'
  33. end
  34. def test_search_for_projects
  35. get :index, :params => {:q => "cook"}
  36. assert_response :success
  37. assert_select '#search-results dt.project a', :text => /eCookbook/
  38. end
  39. def test_search_on_archived_project_should_return_403
  40. Project.find(3).archive
  41. get :index, :params => {:id => 3}
  42. assert_response 403
  43. end
  44. def test_search_on_invisible_project_by_user_should_be_denied
  45. @request.session[:user_id] = 7
  46. get :index, :params => {:id => 2}
  47. assert_response 403
  48. end
  49. def test_search_on_invisible_project_by_anonymous_user_should_redirect
  50. get :index, :params => {:id => 2}
  51. assert_response 302
  52. end
  53. def test_search_on_private_project_by_member_should_succeed
  54. @request.session[:user_id] = 2
  55. get :index, :params => {:id => 2}
  56. assert_response :success
  57. end
  58. def test_search_all_projects
  59. with_settings :default_language => 'en' do
  60. get :index, :params => {:q => 'recipe subproject commit', :all_words => ''}
  61. end
  62. assert_response :success
  63. assert_select '#search-results' do
  64. assert_select 'dt.issue a', :text => /Feature request #2/
  65. assert_select 'dt.issue a', :text => /Bug #5/
  66. assert_select 'dt.changeset a', :text => /Revision 1/
  67. assert_select 'dt.issue a', :text => /Add ingredients categories/
  68. assert_select 'dd', :text => /should be classified by categories/
  69. end
  70. assert_select '#search-results-counts' do
  71. assert_select 'a', :text => 'Changesets (5)'
  72. end
  73. end
  74. def test_search_issues
  75. get :index, :params => {:q => 'issue', :issues => 1}
  76. assert_response :success
  77. assert_select 'input[name=all_words][checked=checked]'
  78. assert_select 'input[name=titles_only]:not([checked])'
  79. assert_select '#search-results' do
  80. assert_select 'dt.issue a', :text => /Bug #5/
  81. assert_select 'dt.issue-closed a', :text => /Bug #8 \(Closed\)/
  82. end
  83. end
  84. def test_search_issues_should_search_notes
  85. Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
  86. get :index, :params => {:q => 'searchkeyword', :issues => 1}
  87. assert_response :success
  88. assert_select '#search-results' do
  89. assert_select 'dt.issue a', :text => /Feature request #2/
  90. end
  91. end
  92. def test_search_issues_with_multiple_matches_in_journals_should_return_issue_once
  93. Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
  94. Journal.create!(:journalized => Issue.find(2), :notes => 'Issue notes with searchkeyword')
  95. get :index, :params => {:q => 'searchkeyword', :issues => 1}
  96. assert_response :success
  97. assert_select '#search-results' do
  98. assert_select 'dt.issue a', :text => /Feature request #2/
  99. assert_select 'dt', 1
  100. end
  101. end
  102. def test_search_issues_should_search_private_notes_with_permission_only
  103. Journal.create!(:journalized => Issue.find(2), :notes => 'Private notes with searchkeyword', :private_notes => true)
  104. @request.session[:user_id] = 2
  105. Role.find(1).add_permission! :view_private_notes
  106. get :index, :params => {:q => 'searchkeyword', :issues => 1}
  107. assert_response :success
  108. assert_select '#search-results' do
  109. assert_select 'dt.issue a', :text => /Feature request #2/
  110. end
  111. Role.find(1).remove_permission! :view_private_notes
  112. get :index, :params => {:q => 'searchkeyword', :issues => 1}
  113. assert_response :success
  114. assert_select '#search-results' do
  115. assert_select 'dt', :text => /Feature request #2/, :count => 0
  116. end
  117. end
  118. def test_search_all_projects_with_scope_param
  119. get :index, :params => {:q => 'issue', :scope => 'all'}
  120. assert_response :success
  121. assert_select '#search-results dt'
  122. end
  123. def test_search_my_projects
  124. @request.session[:user_id] = 2
  125. get :index, :params => {:id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => ''}
  126. assert_response :success
  127. assert_select '#search-results' do
  128. assert_select 'dt.issue', :text => /Bug #1/
  129. assert_select 'dt', :text => /Bug #5/, :count => 0
  130. end
  131. end
  132. def test_search_my_projects_without_memberships
  133. # anonymous user has no memberships
  134. get :index, :params => {:id => 1, :q => 'recipe subproject', :scope => 'my_projects', :all_words => ''}
  135. assert_response :success
  136. assert_select '#search-results' do
  137. assert_select 'dt', 0
  138. end
  139. end
  140. def test_search_project_and_subprojects
  141. get :index, :params => {:id => 1, :q => 'recipe subproject', :scope => 'subprojects', :all_words => ''}
  142. assert_response :success
  143. assert_select '#search-results' do
  144. assert_select 'dt.issue', :text => /Bug #1/
  145. assert_select 'dt.issue', :text => /Bug #5/
  146. end
  147. end
  148. def test_search_without_searchable_custom_fields
  149. CustomField.update_all :searchable => false
  150. get :index, :params => {:id => 1}
  151. assert_response :success
  152. get :index, :params => {:id => 1, :q => "can"}
  153. assert_response :success
  154. end
  155. def test_search_with_searchable_custom_fields
  156. get :index, :params => {:id => 1, :q => "stringforcustomfield"}
  157. assert_response :success
  158. assert_select '#search-results' do
  159. assert_select 'dt.issue', :text => /#7/
  160. assert_select 'dt', 1
  161. end
  162. end
  163. def test_search_without_attachments
  164. issue = Issue.generate! :subject => 'search_attachments'
  165. attachment = Attachment.generate! :container => Issue.find(1), :filename => 'search_attachments.patch'
  166. get :index, :params => {:id => 1, :q => 'search_attachments', :attachments => '0'}
  167. assert_response :success
  168. assert_select '#search-results' do
  169. assert_select 'dt.issue', :text => /##{issue.id}/
  170. assert_select 'dt', 1
  171. end
  172. end
  173. def test_search_attachments_only
  174. issue = Issue.generate! :subject => 'search_attachments'
  175. attachment = Attachment.generate! :container => Issue.find(1), :filename => 'search_attachments.patch'
  176. get :index, :params => {:id => 1, :q => 'search_attachments', :attachments => 'only'}
  177. assert_response :success
  178. assert_select '#search-results' do
  179. assert_select 'dt.issue', :text => / #1 /
  180. assert_select 'dt', 1
  181. end
  182. end
  183. def test_search_with_attachments
  184. issue = Issue.generate! :subject => 'search_attachments'
  185. Attachment.generate! :container => Issue.find(1), :filename => 'search_attachments.patch'
  186. get :index, :params => {:id => 1, :q => 'search_attachments', :attachments => '1'}
  187. assert_response :success
  188. assert_select '#search-results' do
  189. assert_select 'dt.issue', :text => / #1 /
  190. assert_select 'dt.issue', :text => / ##{issue.id} /
  191. assert_select 'dt', 2
  192. end
  193. end
  194. def test_search_open_issues
  195. Issue.generate! :subject => 'search_open'
  196. Issue.generate! :subject => 'search_open', :status_id => 5
  197. get :index, :params => {:id => 1, :q => 'search_open', :open_issues => '1'}
  198. assert_response :success
  199. assert_select '#search-results' do
  200. assert_select 'dt', 1
  201. end
  202. end
  203. def test_search_all_words
  204. # 'all words' is on by default
  205. get :index, :params => {:id => 1, :q => 'recipe updating saving', :all_words => '1'}
  206. assert_response :success
  207. assert_select 'input[name=all_words][checked=checked]'
  208. assert_select '#search-results' do
  209. assert_select 'dt.issue', :text => / #3 /
  210. assert_select 'dt', 1
  211. end
  212. end
  213. def test_search_one_of_the_words
  214. get :index, :params => {:id => 1, :q => 'recipe updating saving', :all_words => ''}
  215. assert_response :success
  216. assert_select 'input[name=all_words]:not([checked])'
  217. assert_select '#search-results' do
  218. assert_select 'dt.issue', :text => / #3 /
  219. assert_select 'dt', 3
  220. end
  221. end
  222. def test_search_titles_only_without_result
  223. get :index, :params => {:id => 1, :q => 'recipe updating saving', :titles_only => '1'}
  224. assert_response :success
  225. assert_select 'input[name=titles_only][checked=checked]'
  226. assert_select '#search-results' do
  227. assert_select 'dt', 0
  228. end
  229. end
  230. def test_search_titles_only
  231. get :index, :params => {:id => 1, :q => 'recipe', :titles_only => '1'}
  232. assert_response :success
  233. assert_select 'input[name=titles_only][checked=checked]'
  234. assert_select '#search-results' do
  235. assert_select 'dt', 2
  236. end
  237. end
  238. def test_search_content
  239. Issue.where(:id => 1).update_all("description = 'This is a searchkeywordinthecontent'")
  240. get :index, :params => {:id => 1, :q => 'searchkeywordinthecontent', :titles_only => ''}
  241. assert_response :success
  242. assert_select 'input[name=titles_only]:not([checked])'
  243. assert_select '#search-results' do
  244. assert_select 'dt.issue', :text => / #1 /
  245. assert_select 'dt', 1
  246. end
  247. end
  248. def test_search_with_pagination
  249. issues = (0..24).map {Issue.generate! :subject => 'search_with_limited_results'}.reverse
  250. get :index, :params => {:q => 'search_with_limited_results'}
  251. assert_response :success
  252. issues[0..9].each do |issue|
  253. assert_select '#search-results dt.issue', :text => / ##{issue.id} /
  254. end
  255. get :index, :params => {:q => 'search_with_limited_results', :page => 2}
  256. assert_response :success
  257. issues[10..19].each do |issue|
  258. assert_select '#search-results dt.issue', :text => / ##{issue.id} /
  259. end
  260. get :index, :params => {:q => 'search_with_limited_results', :page => 3}
  261. assert_response :success
  262. issues[20..24].each do |issue|
  263. assert_select '#search-results dt.issue', :text => / ##{issue.id} /
  264. end
  265. get :index, :params => {:q => 'search_with_limited_results', :page => 4}
  266. assert_response :success
  267. assert_select '#search-results dt', 0
  268. end
  269. def test_search_with_invalid_project_id
  270. get :index, :params => {:id => 195, :q => 'recipe'}
  271. assert_response 404
  272. end
  273. def test_search_should_include_closed_projects
  274. @request.session[:user_id] = 1
  275. project = Project.find(5)
  276. project.close
  277. project.save
  278. # scope all
  279. get :index, :params => {:q => 'Issue of a private subproject', :scope => 'all'}
  280. assert_response :success
  281. assert_select '#search-results' do
  282. assert_select 'dt.issue', :text => /Bug #6/
  283. end
  284. # scope my_projects
  285. get :index, :params => {:q => 'Issue of a private subproject', :scope => 'my_projects'}
  286. assert_response :success
  287. assert_select '#search-results' do
  288. assert_select 'dt.issue', :text => /Bug #6/
  289. end
  290. # scope subprojects
  291. get :index, :params => {:id => 1, :q => 'Issue of a private subproject', :scope => 'subprojects'}
  292. assert_response :success
  293. assert_select '#search-results' do
  294. assert_select 'dt.issue', :text => /Bug #6/
  295. end
  296. # scope project
  297. get :index, :params => {:id => 5, :q => 'Issue of a private subproject'}
  298. assert_response :success
  299. assert_select '#search-results' do
  300. assert_select 'dt.issue', :text => /Bug #6/
  301. end
  302. end
  303. def test_quick_jump_to_issue
  304. # issue of a public project
  305. get :index, :params => {:q => "3"}
  306. assert_redirected_to '/issues/3'
  307. # issue of a private project
  308. get :index, :params => {:q => "4"}
  309. assert_response :success
  310. end
  311. def test_large_integer
  312. get :index, :params => {:q => '4615713488'}
  313. assert_response :success
  314. end
  315. def test_tokens_with_quotes
  316. issue1 = Issue.generate! :subject => 'say hello'
  317. issue2 = Issue.generate! :subject => 'say good bye'
  318. issue3 = Issue.generate! :subject => 'say goodbye'
  319. get :index, :params => {:q => '"good bye" hello "bye bye"', :all_words => ''}
  320. assert_response :success
  321. assert_select '#search-results' do
  322. assert_select 'dt.issue a', :text => / ##{issue1.id} /
  323. assert_select 'dt.issue a', :text => / ##{issue2.id} /
  324. assert_select 'dt.issue a', :text => / ##{issue3.id} /, :count => 0
  325. end
  326. end
  327. def test_results_should_be_escaped_once
  328. assert Issue.find(1).update_attributes(:subject => '<subject> escaped_once', :description => '<description> escaped_once')
  329. get :index, :params => {:q => 'escaped_once'}
  330. assert_response :success
  331. assert_select '#search-results' do
  332. assert_select 'dt.issue a', :text => /<subject>/
  333. assert_select 'dd', :text => /<description>/
  334. end
  335. end
  336. def test_keywords_should_be_highlighted
  337. assert Issue.find(1).update_attributes(:subject => 'subject highlighted', :description => 'description highlighted')
  338. get :index, :params => {:q => 'highlighted'}
  339. assert_response :success
  340. assert_select '#search-results' do
  341. assert_select 'dt.issue a span.highlight', :text => 'highlighted'
  342. assert_select 'dd span.highlight', :text => 'highlighted'
  343. end
  344. end
  345. end