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.

version_test.rb 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  19. class VersionTest < ActiveSupport::TestCase
  20. fixtures :projects, :users, :issues, :issue_statuses, :trackers,
  21. :enumerations, :versions, :projects_trackers,
  22. :custom_fields, :custom_fields_trackers, :custom_fields_projects,
  23. :members, :member_roles, :roles, :issue_categories
  24. def setup
  25. User.current = nil
  26. end
  27. def test_create
  28. v = Version.new(:project => Project.find(1), :name => '1.1',
  29. :effective_date => '2011-03-25')
  30. assert v.save
  31. assert_equal 'open', v.status
  32. assert_equal 'none', v.sharing
  33. end
  34. def test_create_as_default_project_version
  35. project = Project.find(1)
  36. v = Version.new(:project => project, :name => '1.1',
  37. :default_project_version => '1')
  38. assert v.save
  39. assert_equal v, project.reload.default_version
  40. end
  41. def test_create_not_as_default_project_version
  42. project = Project.find(1)
  43. v = Version.new(:project => project, :name => '1.1',
  44. :default_project_version => '0')
  45. assert v.save
  46. assert_nil project.reload.default_version
  47. end
  48. def test_invalid_effective_date_validation
  49. v = Version.new(:project => Project.find(1), :name => '1.1',
  50. :effective_date => '99999-01-01')
  51. assert v.invalid?
  52. v.effective_date = '2012-11-33'
  53. assert v.invalid?
  54. v.effective_date = '2012-31-11'
  55. assert v.invalid?
  56. v.effective_date = '-2012-31-11'
  57. assert v.invalid?
  58. v.effective_date = 'ABC'
  59. assert v.invalid?
  60. assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
  61. v.errors[:effective_date]
  62. end
  63. def test_progress_should_be_0_with_no_assigned_issues
  64. project = Project.find(1)
  65. v = Version.create!(:project => project, :name => 'Progress')
  66. assert_equal 0, v.completed_percent
  67. assert_equal 0, v.closed_percent
  68. end
  69. def test_progress_should_be_0_with_unbegun_assigned_issues
  70. project = Project.find(1)
  71. v = Version.create!(:project => project, :name => 'Progress')
  72. add_issue(v)
  73. add_issue(v, :done_ratio => 0)
  74. assert_progress_equal 0, v.completed_percent
  75. assert_progress_equal 0, v.closed_percent
  76. end
  77. def test_progress_should_be_100_with_closed_assigned_issues
  78. project = Project.find(1)
  79. status = IssueStatus.where(:is_closed => true).first
  80. v = Version.create!(:project => project, :name => 'Progress')
  81. add_issue(v, :status => status)
  82. add_issue(v, :status => status, :done_ratio => 20)
  83. add_issue(v, :status => status, :done_ratio => 70, :estimated_hours => 25)
  84. add_issue(v, :status => status, :estimated_hours => 15)
  85. assert_progress_equal 100.0, v.completed_percent
  86. assert_progress_equal 100.0, v.closed_percent
  87. end
  88. def test_progress_should_consider_done_ratio_of_open_assigned_issues
  89. project = Project.find(1)
  90. v = Version.create!(:project => project, :name => 'Progress')
  91. add_issue(v)
  92. add_issue(v, :done_ratio => 20)
  93. add_issue(v, :done_ratio => 70)
  94. assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_percent
  95. assert_progress_equal 0, v.closed_percent
  96. end
  97. def test_progress_should_consider_closed_issues_as_completed
  98. project = Project.find(1)
  99. v = Version.create!(:project => project, :name => 'Progress')
  100. add_issue(v)
  101. add_issue(v, :done_ratio => 20)
  102. add_issue(v, :status => IssueStatus.where(:is_closed => true).first)
  103. assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_percent
  104. assert_progress_equal (100.0)/3, v.closed_percent
  105. end
  106. def test_progress_should_consider_closed_issues_with_0h_estimated_as_completed
  107. project = Project.find(1)
  108. closed = IssueStatus.where(:is_closed => true).first
  109. v = Version.create!(:project => project, :name => 'Progress')
  110. add_issue(v, :done_ratio => 100, :estimated_hours => 0)
  111. add_issue(v, :done_ratio => 100, :estimated_hours => 0, :status => closed)
  112. assert_progress_equal 100, v.completed_percent
  113. assert_progress_equal 50, v.closed_percent
  114. end
  115. def test_progress_should_consider_estimated_hours_to_weight_issues
  116. project = Project.find(1)
  117. v = Version.create!(:project => project, :name => 'Progress')
  118. add_issue(v, :estimated_hours => 10)
  119. add_issue(v, :estimated_hours => 20, :done_ratio => 30)
  120. add_issue(v, :estimated_hours => 40, :done_ratio => 10)
  121. add_issue(v, :estimated_hours => 25, :status => IssueStatus.where(:is_closed => true).first)
  122. assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_percent
  123. assert_progress_equal 25.0/95.0*100, v.closed_percent
  124. end
  125. def test_progress_should_consider_average_estimated_hours_to_weight_unestimated_issues
  126. project = Project.find(1)
  127. v = Version.create!(:project => project, :name => 'Progress')
  128. add_issue(v, :done_ratio => 20)
  129. add_issue(v, :status => IssueStatus.where(:is_closed => true).first)
  130. add_issue(v, :estimated_hours => 10, :done_ratio => 30)
  131. add_issue(v, :estimated_hours => 40, :done_ratio => 10)
  132. assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_percent
  133. assert_progress_equal 25.0/100.0*100, v.closed_percent
  134. end
  135. def test_progress_should_be_weighted_by_estimated_times_if_any_with_grandchildren
  136. project = Project.find(1)
  137. v = Version.create!(:project => project, :name => 'Progress')
  138. with_settings :parent_issue_done_ratio => 'derived' do
  139. parent = Issue.generate!
  140. parent.generate_child!(:estimated_hours => 2, :done_ratio => 0, :fixed_version => v)
  141. child = parent.generate_child!(:fixed_version => v)
  142. child.generate_child!(:estimated_hours => 2, :done_ratio => 50)
  143. child.generate_child!(:estimated_hours => 2, :done_ratio => 50)
  144. assert_progress_equal 200.0 / (3.0 * 2), v.completed_percent
  145. end
  146. end
  147. def test_should_sort_scheduled_then_unscheduled_versions
  148. Version.delete_all
  149. v4 = Version.create!(:project_id => 1, :name => 'v4')
  150. v3 = Version.create!(:project_id => 1, :name => 'v2', :effective_date => '2012-07-14')
  151. v2 = Version.create!(:project_id => 1, :name => 'v1')
  152. v1 = Version.create!(:project_id => 1, :name => 'v3', :effective_date => '2012-08-02')
  153. v5 = Version.create!(:project_id => 1, :name => 'v5', :effective_date => '2012-07-02')
  154. assert_equal [v5, v3, v1, v2, v4], [v1, v2, v3, v4, v5].sort
  155. assert_equal [v5, v3, v1, v2, v4], Version.sorted.to_a
  156. end
  157. def test_should_sort_versions_with_same_date_by_name
  158. v1 = Version.new(:effective_date => '2014-12-03', :name => 'v2')
  159. v2 = Version.new(:effective_date => '2014-12-03', :name => 'v1')
  160. assert_equal [v2, v1], [v1, v2].sort
  161. end
  162. def test_completed_should_be_false_when_due_today
  163. version = Version.create!(:project_id => 1, :effective_date => Date.today, :name => 'Due today')
  164. assert_equal false, version.completed?
  165. end
  166. def test_completed_should_be_true_when_closed
  167. version = Version.create!(:project_id => 1, :status => 'closed', :name => 'Closed')
  168. assert_equal true, version.completed?
  169. end
  170. test "#behind_schedule? should be false if there are no issues assigned" do
  171. version = Version.generate!(:effective_date => Date.yesterday)
  172. assert_equal false, version.behind_schedule?
  173. end
  174. test "#behind_schedule? should be false if there is no effective_date" do
  175. version = Version.generate!(:effective_date => nil)
  176. assert_equal false, version.behind_schedule?
  177. end
  178. test "#behind_schedule? should be false if all of the issues are ahead of schedule" do
  179. version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
  180. add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  181. add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  182. assert_equal 60, version.completed_percent
  183. assert_equal false, version.behind_schedule?
  184. end
  185. test "#behind_schedule? should be true if any of the issues are behind schedule" do
  186. version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
  187. add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  188. add_issue(version, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left
  189. assert_equal 40, version.completed_percent
  190. assert_equal true, version.behind_schedule?
  191. end
  192. test "#behind_schedule? should be false if all of the issues are complete" do
  193. version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
  194. add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
  195. add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
  196. assert_equal 100, version.completed_percent
  197. assert_equal false, version.behind_schedule?
  198. end
  199. test "#estimated_hours should return 0 with no assigned issues" do
  200. version = Version.generate!
  201. assert_equal 0, version.estimated_hours
  202. end
  203. test "#estimated_hours should return 0 with no estimated hours" do
  204. version = Version.create!(:project_id => 1, :name => 'test')
  205. add_issue(version)
  206. assert_equal 0, version.estimated_hours
  207. end
  208. test "#estimated_hours should return return the sum of estimated hours" do
  209. version = Version.create!(:project_id => 1, :name => 'test')
  210. add_issue(version, :estimated_hours => 2.5)
  211. add_issue(version, :estimated_hours => 5)
  212. assert_equal 7.5, version.estimated_hours
  213. end
  214. test "#estimated_hours should return the sum of leaves estimated hours" do
  215. version = Version.create!(:project_id => 1, :name => 'test')
  216. parent = add_issue(version)
  217. add_issue(version, :estimated_hours => 2.5, :parent_issue_id => parent.id)
  218. add_issue(version, :estimated_hours => 5, :parent_issue_id => parent.id)
  219. assert_equal 7.5, version.estimated_hours
  220. end
  221. test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do
  222. User.current = User.find(1) # Need the admin's permissions
  223. @version = Version.find(7)
  224. # Separate hierarchy
  225. project_1_issue = Issue.find(1)
  226. project_1_issue.fixed_version = @version
  227. assert project_1_issue.save, project_1_issue.errors.full_messages.to_s
  228. project_5_issue = Issue.find(6)
  229. project_5_issue.fixed_version = @version
  230. assert project_5_issue.save
  231. # Project
  232. project_2_issue = Issue.find(4)
  233. project_2_issue.fixed_version = @version
  234. assert project_2_issue.save
  235. # Update the sharing
  236. @version.sharing = 'none'
  237. assert @version.save
  238. # Project 1 now out of the shared scope
  239. project_1_issue.reload
  240. assert_nil project_1_issue.fixed_version,
  241. "Fixed version is still set after changing the Version's sharing"
  242. # Project 5 now out of the shared scope
  243. project_5_issue.reload
  244. assert_nil project_5_issue.fixed_version,
  245. "Fixed version is still set after changing the Version's sharing"
  246. # Project 2 issue remains
  247. project_2_issue.reload
  248. assert_equal @version, project_2_issue.fixed_version
  249. end
  250. def test_deletable_should_return_true_when_not_referenced
  251. version = Version.generate!
  252. assert_equal true, version.deletable?
  253. end
  254. def test_deletable_should_return_false_when_referenced_by_an_issue
  255. version = Version.generate!
  256. Issue.generate!(:fixed_version => version)
  257. assert_equal false, version.deletable?
  258. end
  259. def test_deletable_should_return_false_when_referenced_by_a_custom_field
  260. version = Version.generate!
  261. field = IssueCustomField.generate!(:field_format => 'version')
  262. value = CustomValue.create!(:custom_field => field, :customized => Issue.first, :value => version.id)
  263. assert_equal false, version.deletable?
  264. end
  265. def test_deletable_should_return_false_when_referenced_by_an_attachment
  266. version = Version.generate!
  267. Attachment.generate!(:container => version, :filename => 'test.txt')
  268. assert_equal false, version.deletable?
  269. end
  270. def test_like_scope
  271. version = Version.create!(:project => Project.find(1), :name => 'Version for like scope test')
  272. assert_includes Version.like('VERSION FOR LIKE SCOPE TEST'), version
  273. assert_includes Version.like('version for like scope test'), version
  274. assert_includes Version.like('like scope'), version
  275. end
  276. def test_like_scope_should_escape_query
  277. version = Version.create!(:project => Project.find(1), :name => 'Version for like scope test')
  278. r = Version.like('Ver_ion')
  279. assert_not_include version, r
  280. r = Version.like('Ver%ion')
  281. assert_not_include version, r
  282. version.update_column :name, 'Ver%ion'
  283. r = Version.like('ver%i')
  284. assert_include version, r
  285. version.update_column :name, 'Ver_ion'
  286. r = Version.like('ver_i')
  287. assert_include version, r
  288. end
  289. def test_safe_attributes_should_include_only_custom_fields_visible_to_user
  290. cf1 = VersionCustomField.create!(:name => 'Visible field',
  291. :field_format => 'string',
  292. :visible => false, :role_ids => [1])
  293. cf2 = VersionCustomField.create!(:name => 'Non visible field',
  294. :field_format => 'string',
  295. :visible => false, :role_ids => [3])
  296. user = User.find(2)
  297. version = Version.new(:project_id => 1, :name => 'v4')
  298. version.send(
  299. :safe_attributes=,
  300. {
  301. 'custom_field_values' =>
  302. {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
  303. },
  304. user
  305. )
  306. assert_equal 'value1', version.custom_field_value(cf1)
  307. assert_nil version.custom_field_value(cf2)
  308. version.send(
  309. :safe_attributes=,
  310. {
  311. 'custom_fields' =>
  312. [
  313. {'id' => cf1.id.to_s, 'value' => 'valuea'},
  314. {'id' => cf2.id.to_s, 'value' => 'valueb'}
  315. ]
  316. },
  317. user
  318. )
  319. assert_equal 'valuea', version.custom_field_value(cf1)
  320. assert_nil version.custom_field_value(cf2)
  321. end
  322. private
  323. def add_issue(version, attributes={})
  324. Issue.create!({:project => version.project,
  325. :fixed_version => version,
  326. :subject => 'Test',
  327. :author => User.first,
  328. :tracker => version.project.trackers.first}.merge(attributes))
  329. end
  330. def assert_progress_equal(expected_float, actual_float, message="")
  331. assert_in_delta(expected_float, actual_float, 0.000001, message="")
  332. end
  333. end