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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 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
  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.valid?
  52. v.effective_date = '2012-11-33'
  53. assert !v.valid?
  54. v.effective_date = '2012-31-11'
  55. assert !v.valid?
  56. v.effective_date = '-2012-31-11'
  57. assert !v.valid?
  58. v.effective_date = 'ABC'
  59. assert !v.valid?
  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_estimated_hours_to_weight_issues
  107. project = Project.find(1)
  108. v = Version.create!(:project => project, :name => 'Progress')
  109. add_issue(v, :estimated_hours => 10)
  110. add_issue(v, :estimated_hours => 20, :done_ratio => 30)
  111. add_issue(v, :estimated_hours => 40, :done_ratio => 10)
  112. add_issue(v, :estimated_hours => 25, :status => IssueStatus.where(:is_closed => true).first)
  113. assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_percent
  114. assert_progress_equal 25.0/95.0*100, v.closed_percent
  115. end
  116. def test_progress_should_consider_average_estimated_hours_to_weight_unestimated_issues
  117. project = Project.find(1)
  118. v = Version.create!(:project => project, :name => 'Progress')
  119. add_issue(v, :done_ratio => 20)
  120. add_issue(v, :status => IssueStatus.where(:is_closed => true).first)
  121. add_issue(v, :estimated_hours => 10, :done_ratio => 30)
  122. add_issue(v, :estimated_hours => 40, :done_ratio => 10)
  123. assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_percent
  124. assert_progress_equal 25.0/100.0*100, v.closed_percent
  125. end
  126. def test_should_sort_scheduled_then_unscheduled_versions
  127. Version.delete_all
  128. v4 = Version.create!(:project_id => 1, :name => 'v4')
  129. v3 = Version.create!(:project_id => 1, :name => 'v2', :effective_date => '2012-07-14')
  130. v2 = Version.create!(:project_id => 1, :name => 'v1')
  131. v1 = Version.create!(:project_id => 1, :name => 'v3', :effective_date => '2012-08-02')
  132. v5 = Version.create!(:project_id => 1, :name => 'v5', :effective_date => '2012-07-02')
  133. assert_equal [v5, v3, v1, v2, v4], [v1, v2, v3, v4, v5].sort
  134. assert_equal [v5, v3, v1, v2, v4], Version.sorted.to_a
  135. end
  136. def test_should_sort_versions_with_same_date_by_name
  137. v1 = Version.new(:effective_date => '2014-12-03', :name => 'v2')
  138. v2 = Version.new(:effective_date => '2014-12-03', :name => 'v1')
  139. assert_equal [v2, v1], [v1, v2].sort
  140. end
  141. def test_completed_should_be_false_when_due_today
  142. version = Version.create!(:project_id => 1, :effective_date => Date.today, :name => 'Due today')
  143. assert_equal false, version.completed?
  144. end
  145. def test_completed_should_be_true_when_closed
  146. version = Version.create!(:project_id => 1, :status => 'closed', :name => 'Closed')
  147. assert_equal true, version.completed?
  148. end
  149. test "#behind_schedule? should be false if there are no issues assigned" do
  150. version = Version.generate!(:effective_date => Date.yesterday)
  151. assert_equal false, version.behind_schedule?
  152. end
  153. test "#behind_schedule? should be false if there is no effective_date" do
  154. version = Version.generate!(:effective_date => nil)
  155. assert_equal false, version.behind_schedule?
  156. end
  157. test "#behind_schedule? should be false if all of the issues are ahead of schedule" do
  158. version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
  159. add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  160. add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  161. assert_equal 60, version.completed_percent
  162. assert_equal false, version.behind_schedule?
  163. end
  164. test "#behind_schedule? should be true if any of the issues are behind schedule" do
  165. version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
  166. add_issue(version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
  167. add_issue(version, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left
  168. assert_equal 40, version.completed_percent
  169. assert_equal true, version.behind_schedule?
  170. end
  171. test "#behind_schedule? should be false if all of the issues are complete" do
  172. version = Version.create!(:project_id => 1, :name => 'test', :effective_date => 7.days.from_now.to_date)
  173. add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
  174. add_issue(version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
  175. assert_equal 100, version.completed_percent
  176. assert_equal false, version.behind_schedule?
  177. end
  178. test "#estimated_hours should return 0 with no assigned issues" do
  179. version = Version.generate!
  180. assert_equal 0, version.estimated_hours
  181. end
  182. test "#estimated_hours should return 0 with no estimated hours" do
  183. version = Version.create!(:project_id => 1, :name => 'test')
  184. add_issue(version)
  185. assert_equal 0, version.estimated_hours
  186. end
  187. test "#estimated_hours should return return the sum of estimated hours" do
  188. version = Version.create!(:project_id => 1, :name => 'test')
  189. add_issue(version, :estimated_hours => 2.5)
  190. add_issue(version, :estimated_hours => 5)
  191. assert_equal 7.5, version.estimated_hours
  192. end
  193. test "#estimated_hours should return the sum of leaves estimated hours" do
  194. version = Version.create!(:project_id => 1, :name => 'test')
  195. parent = add_issue(version)
  196. add_issue(version, :estimated_hours => 2.5, :parent_issue_id => parent.id)
  197. add_issue(version, :estimated_hours => 5, :parent_issue_id => parent.id)
  198. assert_equal 7.5, version.estimated_hours
  199. end
  200. test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do
  201. User.current = User.find(1) # Need the admin's permissions
  202. @version = Version.find(7)
  203. # Separate hierarchy
  204. project_1_issue = Issue.find(1)
  205. project_1_issue.fixed_version = @version
  206. assert project_1_issue.save, project_1_issue.errors.full_messages.to_s
  207. project_5_issue = Issue.find(6)
  208. project_5_issue.fixed_version = @version
  209. assert project_5_issue.save
  210. # Project
  211. project_2_issue = Issue.find(4)
  212. project_2_issue.fixed_version = @version
  213. assert project_2_issue.save
  214. # Update the sharing
  215. @version.sharing = 'none'
  216. assert @version.save
  217. # Project 1 now out of the shared scope
  218. project_1_issue.reload
  219. assert_nil project_1_issue.fixed_version,
  220. "Fixed version is still set after changing the Version's sharing"
  221. # Project 5 now out of the shared scope
  222. project_5_issue.reload
  223. assert_nil project_5_issue.fixed_version,
  224. "Fixed version is still set after changing the Version's sharing"
  225. # Project 2 issue remains
  226. project_2_issue.reload
  227. assert_equal @version, project_2_issue.fixed_version
  228. end
  229. def test_deletable_should_return_true_when_not_referenced
  230. version = Version.generate!
  231. assert_equal true, version.deletable?
  232. end
  233. def test_deletable_should_return_false_when_referenced_by_an_issue
  234. version = Version.generate!
  235. Issue.generate!(:fixed_version => version)
  236. assert_equal false, version.deletable?
  237. end
  238. def test_deletable_should_return_false_when_referenced_by_a_custom_field
  239. version = Version.generate!
  240. field = IssueCustomField.generate!(:field_format => 'version')
  241. value = CustomValue.create!(:custom_field => field, :customized => Issue.first, :value => version.id)
  242. assert_equal false, version.deletable?
  243. end
  244. def test_deletable_should_return_false_when_referenced_by_an_attachment
  245. version = Version.generate!
  246. Attachment.generate!(:container => version, :filename => 'test.txt')
  247. assert_equal false, version.deletable?
  248. end
  249. def test_like_scope
  250. version = Version.create!(:project => Project.find(1), :name => 'Version for like scope test')
  251. assert_includes Version.like('VERSION FOR LIKE SCOPE TEST'), version
  252. assert_includes Version.like('version for like scope test'), version
  253. assert_includes Version.like('like scope'), version
  254. end
  255. def test_like_scope_should_escape_query
  256. version = Version.create!(:project => Project.find(1), :name => 'Version for like scope test')
  257. r = Version.like('Ver_ion')
  258. assert_not_include version, r
  259. r = Version.like('Ver%ion')
  260. assert_not_include version, r
  261. version.update_column :name, 'Ver%ion'
  262. r = Version.like('ver%i')
  263. assert_include version, r
  264. version.update_column :name, 'Ver_ion'
  265. r = Version.like('ver_i')
  266. assert_include version, r
  267. end
  268. def test_safe_attributes_should_include_only_custom_fields_visible_to_user
  269. cf1 = VersionCustomField.create!(:name => 'Visible field',
  270. :field_format => 'string',
  271. :visible => false, :role_ids => [1])
  272. cf2 = VersionCustomField.create!(:name => 'Non visible field',
  273. :field_format => 'string',
  274. :visible => false, :role_ids => [3])
  275. user = User.find(2)
  276. version = Version.new(:project_id => 1, :name => 'v4')
  277. version.send(
  278. :safe_attributes=,
  279. {
  280. 'custom_field_values' =>
  281. {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
  282. },
  283. user
  284. )
  285. assert_equal 'value1', version.custom_field_value(cf1)
  286. assert_nil version.custom_field_value(cf2)
  287. version.send(
  288. :safe_attributes=,
  289. {
  290. 'custom_fields' =>
  291. [
  292. {'id' => cf1.id.to_s, 'value' => 'valuea'},
  293. {'id' => cf2.id.to_s, 'value' => 'valueb'}
  294. ]
  295. },
  296. user
  297. )
  298. assert_equal 'valuea', version.custom_field_value(cf1)
  299. assert_nil version.custom_field_value(cf2)
  300. end
  301. private
  302. def add_issue(version, attributes={})
  303. Issue.create!({:project => version.project,
  304. :fixed_version => version,
  305. :subject => 'Test',
  306. :author => User.first,
  307. :tracker => version.project.trackers.first}.merge(attributes))
  308. end
  309. def assert_progress_equal(expected_float, actual_float, message="")
  310. assert_in_delta(expected_float, actual_float, 0.000001, message="")
  311. end
  312. end