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 14KB

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