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.

issues_test.rb 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 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 IssuesTest < Redmine::IntegrationTest
  20. fixtures :projects,
  21. :users, :email_addresses,
  22. :roles,
  23. :members,
  24. :member_roles,
  25. :trackers,
  26. :projects_trackers,
  27. :enabled_modules,
  28. :issue_statuses,
  29. :issues,
  30. :enumerations,
  31. :custom_fields,
  32. :custom_values,
  33. :custom_fields_trackers,
  34. :attachments
  35. # create an issue
  36. def test_add_issue
  37. log_user('jsmith', 'jsmith')
  38. get '/projects/ecookbook/issues/new'
  39. assert_response :success
  40. issue = new_record(Issue) do
  41. post(
  42. '/projects/ecookbook/issues',
  43. :params => {
  44. :issue => {
  45. :tracker_id => "1",
  46. :start_date => "2006-12-26",
  47. :priority_id => "4",
  48. :subject => "new test issue",
  49. :category_id => "",
  50. :description => "new issue",
  51. :done_ratio => "0",
  52. :due_date => "",
  53. :assigned_to_id => "",
  54. :custom_field_values => {'2' => 'Value for field 2'}
  55. }
  56. }
  57. )
  58. end
  59. # check redirection
  60. assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
  61. follow_redirect!
  62. # check issue attributes
  63. assert_equal 'jsmith', issue.author.login
  64. assert_equal 1, issue.project.id
  65. assert_equal 1, issue.status.id
  66. end
  67. def test_create_issue_by_anonymous_without_permission_should_fail
  68. Role.anonymous.remove_permission! :add_issues
  69. assert_no_difference 'Issue.count' do
  70. post(
  71. '/projects/1/issues',
  72. :params => {
  73. :issue => {
  74. :tracker_id => "1",
  75. :subject => "new test issue"
  76. }
  77. }
  78. )
  79. end
  80. assert_response 302
  81. end
  82. def test_create_issue_by_anonymous_with_custom_permission_should_succeed
  83. Role.anonymous.remove_permission! :add_issues
  84. Member.create!(:project_id => 1, :principal => Group.anonymous, :role_ids => [3])
  85. issue = new_record(Issue) do
  86. post(
  87. '/projects/1/issues',
  88. :params => {
  89. :issue => {
  90. :tracker_id => "1",
  91. :subject => "new test issue"
  92. }
  93. }
  94. )
  95. assert_response 302
  96. end
  97. assert_equal User.anonymous, issue.author
  98. end
  99. # add then remove 2 attachments to an issue
  100. def test_issue_attachments
  101. log_user('jsmith', 'jsmith')
  102. set_tmp_attachments_directory
  103. attachment = new_record(Attachment) do
  104. put(
  105. '/issues/1',
  106. :params => {
  107. :issue => {:notes => 'Some notes'},
  108. :attachments => {
  109. '1' => {
  110. 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
  111. 'description' => 'This is an attachment'
  112. }
  113. }
  114. }
  115. )
  116. assert_redirected_to "/issues/1"
  117. end
  118. assert_equal Issue.find(1), attachment.container
  119. assert_equal 'testfile.txt', attachment.filename
  120. assert_equal 'This is an attachment', attachment.description
  121. # verify the size of the attachment stored in db
  122. assert_equal 59, attachment.filesize
  123. # verify that the attachment was written to disk
  124. assert File.exist?(attachment.diskfile)
  125. # remove the attachments
  126. Issue.find(1).attachments.each(&:destroy)
  127. assert_equal 0, Issue.find(1).attachments.length
  128. end
  129. def test_next_and_previous_links_should_be_displayed_after_query_grouped_and_sorted_by_version
  130. with_settings :default_language => 'en' do
  131. get '/projects/ecookbook/issues?set_filter=1&group_by=fixed_version&sort=priority:desc,fixed_version,id'
  132. assert_response :success
  133. assert_select 'td.id', :text => '5'
  134. get '/issues/5'
  135. assert_response :success
  136. assert_select '.next-prev-links .position', :text => '5 of 6'
  137. end
  138. end
  139. def test_next_and_previous_links_should_be_displayed_after_filter
  140. with_settings :default_language => 'en' do
  141. get '/projects/ecookbook/issues?set_filter=1&tracker_id=1'
  142. assert_response :success
  143. assert_select 'td.id', :text => '5'
  144. get '/issues/5'
  145. assert_response :success
  146. assert_select '.next-prev-links .position', :text => '3 of 5'
  147. assert_select '.next-prev-links .position a[href^=?]', '/projects/ecookbook/issues?'
  148. end
  149. end
  150. def test_next_and_previous_links_should_be_displayed_after_saved_query
  151. query =
  152. IssueQuery.create!(
  153. :name => 'Calendar Query',
  154. :visibility => IssueQuery::VISIBILITY_PUBLIC,
  155. :filters => {'tracker_id' => {:operator => '=', :values => ['1']}}
  156. )
  157. with_settings :default_language => 'en' do
  158. get "/projects/ecookbook/issues?set_filter=1&query_id=#{query.id}"
  159. assert_response :success
  160. assert_select 'td.id', :text => '5'
  161. get '/issues/5'
  162. assert_response :success
  163. assert_select '.next-prev-links .position', :text => '6 of 8'
  164. end
  165. end
  166. def test_other_formats_links_on_index
  167. get '/projects/ecookbook/issues'
  168. %w(Atom PDF CSV).each do |format|
  169. assert_select 'a[rel=nofollow][href=?]', "/projects/ecookbook/issues.#{format.downcase}", :text => format
  170. end
  171. end
  172. def test_other_formats_links_on_index_without_project_id_in_url
  173. get('/issues', :params => {:project_id => 'ecookbook'})
  174. %w(Atom PDF CSV).each do |format|
  175. assert_select 'a[rel=nofollow][href=?]', "/issues.#{format.downcase}?project_id=ecookbook", :text => format
  176. end
  177. end
  178. def test_pagination_links_on_index
  179. with_settings :per_page_options => '2' do
  180. get '/projects/ecookbook/issues'
  181. assert_select 'a[href=?]', '/projects/ecookbook/issues?page=2', :text => '2'
  182. end
  183. end
  184. def test_pagination_links_should_preserve_query_parameters
  185. with_settings :per_page_options => '2' do
  186. get '/projects/ecookbook/issues?foo=bar'
  187. assert_select 'a[href=?]', '/projects/ecookbook/issues?foo=bar&page=2', :text => '2'
  188. end
  189. end
  190. def test_pagination_links_should_not_use_params_as_url_options
  191. with_settings :per_page_options => '2' do
  192. get '/projects/ecookbook/issues?host=foo'
  193. assert_select 'a[href=?]', '/projects/ecookbook/issues?host=foo&page=2', :text => '2'
  194. end
  195. end
  196. def test_sort_links_on_index
  197. get '/projects/ecookbook/issues'
  198. assert_select 'a[href=?]', '/projects/ecookbook/issues?sort=subject%2Cid%3Adesc', :text => 'Subject'
  199. end
  200. def test_sort_links_should_preserve_query_parameters
  201. get '/projects/ecookbook/issues?foo=bar'
  202. assert_select 'a[href=?]', '/projects/ecookbook/issues?foo=bar&sort=subject%2Cid%3Adesc', :text => 'Subject'
  203. end
  204. def test_sort_links_should_not_use_params_as_url_options
  205. get '/projects/ecookbook/issues?host=foo'
  206. assert_select 'a[href=?]', '/projects/ecookbook/issues?host=foo&sort=subject%2Cid%3Adesc', :text => 'Subject'
  207. end
  208. def test_issue_with_user_custom_field
  209. @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :trackers => Tracker.all)
  210. Role.anonymous.add_permission! :add_issues, :edit_issues
  211. users = Project.find(1).users.sort
  212. tester = users.first
  213. # Issue form
  214. get '/projects/ecookbook/issues/new'
  215. assert_response :success
  216. assert_select 'select[name=?]', "issue[custom_field_values][#{@field.id}]" do
  217. assert_select 'option', users.size + 1 # +1 for blank value
  218. assert_select 'option[value=?]', tester.id.to_s, :text => tester.name
  219. end
  220. # Create issue
  221. issue = new_record(Issue) do
  222. post(
  223. '/projects/ecookbook/issues',
  224. :params => {
  225. :issue => {
  226. :tracker_id => '1',
  227. :priority_id => '4',
  228. :subject => 'Issue with user custom field',
  229. :custom_field_values => {@field.id.to_s => users.first.id.to_s}
  230. }
  231. }
  232. )
  233. assert_response 302
  234. end
  235. # Issue view
  236. follow_redirect!
  237. assert_select ".cf_#{@field.id}" do
  238. assert_select '.label', :text => 'Tester:'
  239. assert_select '.value', :text => tester.name
  240. end
  241. assert_select 'select[name=?]', "issue[custom_field_values][#{@field.id}]" do
  242. assert_select 'option', users.size + 1 # +1 for blank value
  243. assert_select 'option[value=?][selected=selected]', tester.id.to_s, :text => tester.name
  244. end
  245. new_tester = users[1]
  246. with_settings :default_language => 'en' do
  247. # Update issue
  248. assert_difference 'Journal.count' do
  249. put(
  250. "/issues/#{issue.id}",
  251. :params => {
  252. :issue => {
  253. :notes => 'Updating custom field',
  254. :custom_field_values => {@field.id.to_s => new_tester.id.to_s}
  255. }
  256. }
  257. )
  258. assert_redirected_to "/issues/#{issue.id}"
  259. end
  260. # Issue view
  261. follow_redirect!
  262. assert_select 'ul.details li', :text => "Tester changed from #{tester} to #{new_tester}"
  263. end
  264. end
  265. def test_update_using_invalid_http_verbs
  266. log_user('jsmith', 'jsmith')
  267. subject = 'Updated by an invalid http verb'
  268. get '/issues/update/1', :params => {:issue => {:subject => subject}}
  269. assert_response 404
  270. assert_not_equal subject, Issue.find(1).subject
  271. post '/issues/1', :params => {:issue => {:subject => subject}}
  272. assert_response 404
  273. assert_not_equal subject, Issue.find(1).subject
  274. end
  275. def test_get_watch_should_be_invalid
  276. log_user('jsmith', 'jsmith')
  277. assert_no_difference 'Watcher.count' do
  278. get '/watchers/watch?object_type=issue&object_id=1'
  279. assert_response 404
  280. end
  281. end
  282. end