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.

auto_completes_controller_test.rb 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class AutoCompletesControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :issues, :issue_statuses,
  21. :enumerations, :users, :issue_categories,
  22. :trackers,
  23. :projects_trackers,
  24. :roles,
  25. :member_roles,
  26. :members,
  27. :enabled_modules,
  28. :journals, :journal_details,
  29. :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
  30. def test_issues_should_not_be_case_sensitive
  31. get(
  32. :issues,
  33. :params => {
  34. :project_id => 'ecookbook',
  35. :q => 'ReCiPe'
  36. }
  37. )
  38. assert_response :success
  39. assert_include "recipe", response.body
  40. end
  41. def test_issues_should_accept_term_param
  42. get(
  43. :issues,
  44. :params => {
  45. :project_id => 'ecookbook',
  46. :term => 'ReCiPe'
  47. }
  48. )
  49. assert_response :success
  50. assert_include "recipe", response.body
  51. end
  52. def test_issues_should_return_issue_with_given_id
  53. get(
  54. :issues,
  55. :params => {
  56. :project_id => 'subproject1',
  57. :q => '13'
  58. }
  59. )
  60. assert_response :success
  61. assert_include "Bug #13", response.body
  62. end
  63. def test_issues_should_return_issue_with_given_id_preceded_with_hash
  64. get(
  65. :issues,
  66. :params => {
  67. :project_id => 'subproject1',
  68. :q => '#13'
  69. }
  70. )
  71. assert_response :success
  72. assert_include "Bug #13", response.body
  73. end
  74. def test_auto_complete_with_scope_all_should_search_other_projects
  75. get(
  76. :issues,
  77. :params => {
  78. :project_id => 'ecookbook',
  79. :q => '13',
  80. :scope => 'all'
  81. }
  82. )
  83. assert_response :success
  84. assert_include "Bug #13", response.body
  85. end
  86. def test_auto_complete_without_project_should_search_all_projects
  87. get(:issues, :params => {:q => '13'})
  88. assert_response :success
  89. assert_include "Bug #13", response.body
  90. end
  91. def test_auto_complete_without_scope_all_should_not_search_other_projects
  92. get(
  93. :issues,
  94. :params => {
  95. :project_id => 'ecookbook',
  96. :q => '13'
  97. }
  98. )
  99. assert_response :success
  100. assert_not_include "Bug #13", response.body
  101. end
  102. def test_issues_should_return_json
  103. get(
  104. :issues,
  105. :params => {
  106. :project_id => 'subproject1',
  107. :q => '13'
  108. }
  109. )
  110. assert_response :success
  111. json = ActiveSupport::JSON.decode(response.body)
  112. assert_kind_of Array, json
  113. issue = json.first
  114. assert_kind_of Hash, issue
  115. assert_equal 13, issue['id']
  116. assert_equal 13, issue['value']
  117. assert_equal 'Bug #13: Subproject issue two', issue['label']
  118. end
  119. def test_auto_complete_with_status_o_should_return_open_issues_only
  120. get(
  121. :issues,
  122. :params => {
  123. :project_id => 'ecookbook',
  124. :q => 'issue',
  125. :status => 'o'
  126. }
  127. )
  128. assert_response :success
  129. assert_include "Issue due today", response.body
  130. assert_not_include "closed", response.body
  131. end
  132. def test_auto_complete_with_status_c_should_return_closed_issues_only
  133. get(
  134. :issues,
  135. :params => {
  136. :project_id => 'ecookbook',
  137. :q => 'issue',
  138. :status => 'c'
  139. }
  140. )
  141. assert_response :success
  142. assert_include "closed", response.body
  143. assert_not_include "Issue due today", response.body
  144. end
  145. def test_auto_complete_with_issue_id_should_not_return_that_issue
  146. get(
  147. :issues,
  148. :params => {
  149. :project_id => 'ecookbook',
  150. :q => 'issue',
  151. :issue_id => '12'
  152. }
  153. )
  154. assert_response :success
  155. assert_include "issue", response.body
  156. assert_not_include "Bug #12: Closed issue on a locked version", response.body
  157. end
  158. def test_auto_complete_should_return_json_content_type_response
  159. get(
  160. :issues,
  161. :params => {
  162. :project_id => 'subproject1',
  163. :q => '#13'
  164. }
  165. )
  166. assert_response :success
  167. assert_include 'application/json', response.headers['Content-Type']
  168. end
  169. def test_auto_complete_without_term_should_return_last_10_issues
  170. # There are 9 issues generated by fixtures
  171. # and we need two more to test the 10 limit
  172. %w(1..2).each do
  173. Issue.generate!
  174. end
  175. get :issues
  176. assert_response :success
  177. json = ActiveSupport::JSON.decode(response.body)
  178. assert_equal 10, json.count
  179. assert_equal Issue.last.id, json.first['id'].to_i
  180. end
  181. def test_wiki_pages_should_not_be_case_sensitive
  182. get(
  183. :wiki_pages,
  184. params: {
  185. project_id: 'ecookbook',
  186. q: 'pAgE'
  187. }
  188. )
  189. assert_response :success
  190. assert_include 'Page_with_an_inline_image', response.body
  191. end
  192. def test_wiki_pages_should_return_json
  193. get(
  194. :wiki_pages,
  195. params: {
  196. project_id: 'ecookbook',
  197. q: 'Page_with_an_inline_image'
  198. }
  199. )
  200. assert_response :success
  201. json = ActiveSupport::JSON.decode(response.body)
  202. assert_kind_of Array, json
  203. issue = json.first
  204. assert_kind_of Hash, issue
  205. assert_equal 4, issue['id']
  206. assert_equal 'Page_with_an_inline_image', issue['value']
  207. assert_equal 'Page_with_an_inline_image', issue['label']
  208. end
  209. def test_wiki_pages_without_view_wiki_pages_permission_should_not_return_pages
  210. Role.anonymous.remove_permission! :view_wiki_pages
  211. get :wiki_pages, params: { project_id: 'ecookbook', q: 'Page_with_an_inline_image' }
  212. assert_response :success
  213. json = ActiveSupport::JSON.decode(response.body)
  214. assert_equal 0, json.count
  215. end
  216. def test_wiki_pages_without_project_id_params_should_not_return_pages
  217. get :wiki_pages, params: { project_id: '' }
  218. assert_response :success
  219. json = ActiveSupport::JSON.decode(response.body)
  220. assert_equal 0, json.count
  221. end
  222. def test_wiki_pages_should_return_json_content_type_response
  223. get(
  224. :wiki_pages,
  225. params: {
  226. project_id: 'ecookbook',
  227. q: 'Page_with_an_inline_image'
  228. }
  229. )
  230. assert_response :success
  231. assert_include 'application/json', response.headers['Content-Type']
  232. end
  233. def test_wiki_pages_without_q_params_should_return_last_10_pages
  234. # There are 8 pages generated by fixtures
  235. # and we need three more to test the 10 limit
  236. wiki = Wiki.find_by(project: Project.first)
  237. 3.times do |n|
  238. WikiPage.create(wiki: wiki, title: "test#{n}")
  239. end
  240. get :wiki_pages, params: { project_id: 'ecookbook' }
  241. assert_response :success
  242. json = ActiveSupport::JSON.decode(response.body)
  243. assert_equal 10, json.count
  244. assert_equal wiki.pages[-2].id, json.first['id'].to_i
  245. end
  246. end