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.

versions_controller_test.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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 VersionsControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :enabled_modules,
  21. :trackers, :projects_trackers,
  22. :versions, :issue_statuses, :issue_categories, :enumerations,
  23. :issues,
  24. :users, :email_addresses,
  25. :roles, :members, :member_roles
  26. def setup
  27. User.current = nil
  28. end
  29. def test_index
  30. get :index, :params => {:project_id => 1}
  31. assert_response :success
  32. # Version with no date set appears
  33. assert_select 'h3', :text => "#{Version.find(3).name}"
  34. assert_select 'span[class=?]', 'badge badge-status-open', :text => 'open'
  35. # Completed version doesn't appear
  36. assert_select 'h3', :text => Version.find(1).name, :count => 0
  37. # Context menu on issues
  38. assert_select "form[data-cm-url=?]", '/issues/context_menu'
  39. assert_select "div#sidebar" do
  40. # Links to versions anchors
  41. assert_select 'a[href=?]', '#2.0'
  42. # Links to completed versions in the sidebar
  43. assert_select 'a[href=?]', '/versions/1'
  44. end
  45. end
  46. def test_index_with_completed_versions
  47. get :index, :params => {:project_id => 1, :completed => 1}
  48. assert_response :success
  49. # Version with no date set appears
  50. assert_select 'h3', :text => Version.find(3).name
  51. # Completed version appears
  52. assert_select 'h3', :text => Version.find(1).name
  53. end
  54. def test_index_with_tracker_ids
  55. (1..3).each do |tracker_id|
  56. Issue.generate! :project_id => 1, :fixed_version_id => 3, :tracker_id => tracker_id
  57. end
  58. get :index, :params => {:project_id => 1, :tracker_ids => [1, 3]}
  59. assert_response :success
  60. assert_select 'a.issue.tracker-1'
  61. assert_select 'a.issue.tracker-2', 0
  62. assert_select 'a.issue.tracker-3'
  63. end
  64. def test_index_showing_subprojects_versions
  65. version_name = "Subproject version"
  66. Version.create!(:project => Project.find(3), :name => version_name)
  67. get :index, :params => {:project_id => 1, :with_subprojects => 1}
  68. assert_response :success
  69. # Shared version
  70. assert_select 'h3', :text => Version.find(4).name
  71. # Subproject version
  72. assert_select 'h3', :text => /#{version_name}/
  73. end
  74. def test_index_should_prepend_shared_versions
  75. get :index, :params => {:project_id => 1}
  76. assert_response :success
  77. assert_select '#sidebar' do
  78. assert_select 'a[href=?]', '#2.0', :text => '2.0'
  79. assert_select 'a[href=?]', '#subproject1-2.0', :text => 'eCookbook Subproject 1 - 2.0'
  80. end
  81. assert_select '#content' do
  82. assert_select 'a[name=?]', '2.0', :text => '2.0'
  83. assert_select 'a[name=?]', 'subproject1-2.0', :text => 'eCookbook Subproject 1 - 2.0'
  84. end
  85. end
  86. def test_index_should_show_issue_assignee
  87. with_settings :gravatar_enabled => '1' do
  88. Issue.generate!(:project_id => 3, :fixed_version_id => 4, :assigned_to => User.find_by_login('jsmith'))
  89. Issue.generate!(:project_id => 3, :fixed_version_id => 4)
  90. get :index, :params => {:project_id => 3}
  91. assert_response :success
  92. assert_select 'table.related-issues' do
  93. assert_select 'tr.issue', :count => 2 do
  94. assert_select 'img.gravatar[title=?]', 'Assignee: John Smith', :count => 1
  95. end
  96. end
  97. end
  98. end
  99. def test_show
  100. with_settings :gravatar_enabled => '0' do
  101. get :show, :params => {:id => 2}
  102. assert_response :success
  103. assert_select 'h2', :text => /1.0/
  104. assert_select 'span[class=?]', 'badge badge-status-locked', :text => 'locked'
  105. # no issue avatar when gravatar is disabled
  106. assert_select 'img.gravatar', :count => 0
  107. end
  108. end
  109. def test_show_should_show_issue_assignee
  110. with_settings :gravatar_enabled => '1' do
  111. get :show, :params => {:id => 2}
  112. assert_response :success
  113. assert_select 'table.related-issues' do
  114. assert_select 'tr.issue td.assigned_to', :count => 2 do
  115. assert_select 'img.gravatar[title=?]', 'Assignee: Dave Lopper', :count => 1
  116. end
  117. end
  118. end
  119. end
  120. def test_show_issue_calculations_should_take_into_account_only_visible_issues
  121. issue_9 = Issue.find(9)
  122. issue_9.fixed_version_id = 4
  123. issue_9.estimated_hours = 3
  124. issue_9.save!
  125. issue_13 = Issue.find(13)
  126. issue_13.fixed_version_id = 4
  127. issue_13.estimated_hours = 2
  128. issue_13.save!
  129. @request.session[:user_id] = 7
  130. get :show, :params => {:id => 4}
  131. assert_response :success
  132. assert_select 'p.progress-info' do
  133. assert_select 'a', :text => '1 issue'
  134. assert_select 'a', :text => '1 open'
  135. end
  136. assert_select '.time-tracking td.total-hours a:first-child', :text => '2:00 hours'
  137. end
  138. def test_show_should_link_to_spent_time_on_version
  139. version = Version.generate!
  140. issue = Issue.generate(:fixed_version => version)
  141. TimeEntry.generate!(:issue => issue, :hours => 7.2)
  142. get :show, :params => {:id => version.id}
  143. assert_response :success
  144. assert_select '.total-hours', :text => '7:12 hours'
  145. assert_select '.total-hours a[href=?]', "/projects/ecookbook/time_entries?issue.fixed_version_id=#{version.id}&set_filter=1"
  146. end
  147. def test_show_should_display_nil_counts
  148. with_settings :default_language => 'en' do
  149. get :show, :params => {:id => 2, :status_by => 'category'}
  150. assert_response :success
  151. assert_select 'div#status_by' do
  152. assert_select 'select[name=status_by]' do
  153. assert_select 'option[value=category][selected=selected]'
  154. end
  155. assert_select 'a', :text => 'none'
  156. end
  157. end
  158. end
  159. def test_show_should_round_down_progress_percentages
  160. issue = Issue.find(12)
  161. issue.estimated_hours = 40
  162. issue.save!
  163. with_settings :default_language => 'en' do
  164. get :show, :params => {:id => 2}
  165. assert_response :success
  166. assert_select 'div.version-overview' do
  167. assert_select 'table.progress-98' do
  168. assert_select 'td[class=closed][title=?]', 'closed: 98%'
  169. assert_select 'td[class=done][title=?]', '% Done: 99%'
  170. end
  171. assert_select 'p[class=percent]', :text => '99%'
  172. end
  173. end
  174. end
  175. def test_show_should_display_link_to_new_issue
  176. @request.session[:user_id] = 1
  177. get :show, :params => {:id => 3}
  178. assert_response :success
  179. assert_select 'a.icon.icon-add', :text => 'New issue'
  180. end
  181. def test_new
  182. @request.session[:user_id] = 2
  183. get :new, :params => {:project_id => '1'}
  184. assert_response :success
  185. assert_select 'input[name=?]', 'version[name]'
  186. assert_select 'select[name=?]', 'version[status]', false
  187. end
  188. def test_new_from_issue_form
  189. @request.session[:user_id] = 2
  190. get :new, :params => {:project_id => '1'}, :xhr => true
  191. assert_response :success
  192. assert_equal 'text/javascript', response.media_type
  193. end
  194. def test_create
  195. @request.session[:user_id] = 2 # manager
  196. assert_difference 'Version.count' do
  197. post :create, :params => {:project_id => '1', :version => {:name => 'test_add_version'}}
  198. end
  199. assert_redirected_to '/projects/ecookbook/settings/versions'
  200. version = Version.find_by_name('test_add_version')
  201. assert_not_nil version
  202. assert_equal 1, version.project_id
  203. end
  204. def test_create_from_issue_form
  205. @request.session[:user_id] = 2
  206. assert_difference 'Version.count' do
  207. post :create, :params => {:project_id => '1', :version => {:name => 'test_add_version_from_issue_form'}}, :xhr => true
  208. end
  209. version = Version.find_by_name('test_add_version_from_issue_form')
  210. assert_not_nil version
  211. assert_equal 1, version.project_id
  212. assert_response :success
  213. assert_equal 'text/javascript', response.media_type
  214. assert_include 'test_add_version_from_issue_form', response.body
  215. end
  216. def test_create_from_issue_form_with_failure
  217. @request.session[:user_id] = 2
  218. assert_no_difference 'Version.count' do
  219. post :create, :params => {:project_id => '1', :version => {:name => ''}}, :xhr => true
  220. end
  221. assert_response :success
  222. assert_equal 'text/javascript', response.media_type
  223. end
  224. def test_get_edit
  225. @request.session[:user_id] = 2
  226. get :edit, :params => {:id => 2}
  227. assert_response :success
  228. version = Version.find(2)
  229. assert_select 'select[name=?]', 'version[status]' do
  230. assert_select 'option[value=?][selected="selected"]', version.status
  231. end
  232. assert_select 'input[name=?][value=?]', 'version[name]', version.name
  233. end
  234. def test_close_completed
  235. Version.update_all("status = 'open'")
  236. @request.session[:user_id] = 2
  237. put :close_completed, :params => {:project_id => 'ecookbook'}
  238. assert_redirected_to :controller => 'projects', :action => 'settings',
  239. :tab => 'versions', :id => 'ecookbook'
  240. assert_not_nil Version.find_by_status('closed')
  241. end
  242. def test_post_update
  243. @request.session[:user_id] = 2
  244. put :update, :params => {
  245. :id => 2,
  246. :version => {
  247. :name => 'New version name',
  248. :effective_date => Date.today.strftime("%Y-%m-%d")
  249. }
  250. }
  251. assert_redirected_to :controller => 'projects', :action => 'settings',
  252. :tab => 'versions', :id => 'ecookbook'
  253. version = Version.find(2)
  254. assert_equal 'New version name', version.name
  255. assert_equal Date.today, version.effective_date
  256. end
  257. def test_post_update_with_validation_failure
  258. @request.session[:user_id] = 2
  259. put :update, :params => {
  260. :id => 2,
  261. :version => {
  262. :name => '',
  263. :effective_date => Date.today.strftime("%Y-%m-%d")
  264. }
  265. }
  266. assert_response :success
  267. assert_select_error /Name cannot be blank/
  268. end
  269. def test_destroy
  270. @request.session[:user_id] = 2
  271. assert_difference 'Version.count', -1 do
  272. delete :destroy, :params => {:id => 3}
  273. end
  274. assert_redirected_to :controller => 'projects', :action => 'settings',
  275. :tab => 'versions', :id => 'ecookbook'
  276. assert_nil Version.find_by_id(3)
  277. end
  278. def test_destroy_version_in_use_should_fail
  279. @request.session[:user_id] = 2
  280. assert_no_difference 'Version.count' do
  281. delete :destroy, :params => {:id => 2}
  282. end
  283. assert_redirected_to :controller => 'projects', :action => 'settings',
  284. :tab => 'versions', :id => 'ecookbook'
  285. assert flash[:error].match(/Unable to delete version/)
  286. assert Version.find_by_id(2)
  287. end
  288. def test_issue_status_by
  289. get :status_by, :params => {:id => 2}, :xhr => true
  290. assert_response :success
  291. end
  292. def test_issue_status_by_status
  293. get :status_by, :params => {:id => 2, :status_by => 'status'}, :xhr => true
  294. assert_response :success
  295. assert_include 'Assigned', response.body
  296. assert_include 'Closed', response.body
  297. end
  298. end