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.

issue_subtasking_test.rb 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class IssueSubtaskingTest < ActiveSupport::TestCase
  19. fixtures :projects, :users, :roles, :members, :member_roles,
  20. :trackers, :projects_trackers,
  21. :issue_statuses, :issue_categories, :enumerations,
  22. :issues,
  23. :enabled_modules,
  24. :workflows
  25. def test_leaf_planning_fields_should_be_editable
  26. issue = Issue.generate!
  27. user = User.find(1)
  28. %w(priority_id done_ratio start_date due_date estimated_hours).each do |attribute|
  29. assert issue.safe_attribute?(attribute, user)
  30. end
  31. end
  32. def test_parent_dates_should_be_read_only_with_parent_issue_dates_set_to_derived
  33. with_settings :parent_issue_dates => 'derived' do
  34. issue = Issue.generate_with_child!
  35. user = User.find(1)
  36. %w(start_date due_date).each do |attribute|
  37. assert !issue.safe_attribute?(attribute, user)
  38. end
  39. end
  40. end
  41. def test_parent_dates_should_be_lowest_start_and_highest_due_dates_with_parent_issue_dates_set_to_derived
  42. with_settings :parent_issue_dates => 'derived' do
  43. parent = Issue.generate!
  44. parent.generate_child!(:start_date => '2010-01-25', :due_date => '2010-02-15')
  45. parent.generate_child!( :due_date => '2010-02-13')
  46. parent.generate_child!(:start_date => '2010-02-01', :due_date => '2010-02-22')
  47. parent.reload
  48. assert_equal Date.parse('2010-01-25'), parent.start_date
  49. assert_equal Date.parse('2010-02-22'), parent.due_date
  50. end
  51. end
  52. def test_reschuling_a_parent_should_reschedule_subtasks_with_parent_issue_dates_set_to_derived
  53. with_settings :parent_issue_dates => 'derived' do
  54. parent = Issue.generate!
  55. c1 = parent.generate_child!(:start_date => '2010-05-12', :due_date => '2010-05-18')
  56. c2 = parent.generate_child!(:start_date => '2010-06-03', :due_date => '2010-06-10')
  57. parent.reload.reschedule_on!(Date.parse('2010-06-02'))
  58. c1.reload
  59. assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-08')], [c1.start_date, c1.due_date]
  60. c2.reload
  61. assert_equal [Date.parse('2010-06-03'), Date.parse('2010-06-10')], [c2.start_date, c2.due_date] # no change
  62. parent.reload
  63. assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-10')], [parent.start_date, parent.due_date]
  64. end
  65. end
  66. def test_parent_priority_should_be_read_only_with_parent_issue_priority_set_to_derived
  67. with_settings :parent_issue_priority => 'derived' do
  68. issue = Issue.generate_with_child!
  69. user = User.find(1)
  70. assert !issue.safe_attribute?('priority_id', user)
  71. end
  72. end
  73. def test_parent_priority_should_be_the_highest_open_child_priority
  74. with_settings :parent_issue_priority => 'derived' do
  75. parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal'))
  76. # Create children
  77. child1 = parent.generate_child!(:priority => IssuePriority.find_by_name('High'))
  78. assert_equal 'High', parent.reload.priority.name
  79. child2 = child1.generate_child!(:priority => IssuePriority.find_by_name('Immediate'))
  80. assert_equal 'Immediate', child1.reload.priority.name
  81. assert_equal 'Immediate', parent.reload.priority.name
  82. child3 = parent.generate_child!(:priority => IssuePriority.find_by_name('Low'))
  83. child4 = parent.generate_child!(:priority => IssuePriority.find_by_name('Urgent'))
  84. assert_equal 'Immediate', parent.reload.priority.name
  85. # Destroy a child
  86. child1.destroy
  87. assert_equal 'Urgent', parent.reload.priority.name
  88. # Close a child
  89. child4.status = IssueStatus.where(:is_closed => true).first
  90. child4.save!
  91. assert_equal 'Low', parent.reload.priority.name
  92. # Update a child
  93. child3.reload.priority = IssuePriority.find_by_name('Normal')
  94. child3.save!
  95. assert_equal 'Normal', parent.reload.priority.name
  96. # Reopen a child
  97. child4.status = IssueStatus.where(:is_closed => false).first
  98. child4.save!
  99. assert_equal 'Urgent', parent.reload.priority.name
  100. end
  101. end
  102. def test_parent_priority_should_be_set_to_default_when_all_children_are_closed
  103. with_settings :parent_issue_priority => 'derived' do
  104. parent = Issue.generate!
  105. child = parent.generate_child!(:priority => IssuePriority.find_by_name('High'))
  106. assert_equal 'High', parent.reload.priority.name
  107. child.status = IssueStatus.where(:is_closed => true).first
  108. child.save!
  109. assert_equal 'Normal', parent.reload.priority.name
  110. end
  111. end
  112. def test_parent_priority_should_be_left_unchanged_when_all_children_are_closed_and_no_default_priority
  113. IssuePriority.update_all :is_default => false
  114. with_settings :parent_issue_priority => 'derived' do
  115. parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal'))
  116. child = parent.generate_child!(:priority => IssuePriority.find_by_name('High'))
  117. assert_equal 'High', parent.reload.priority.name
  118. child.status = IssueStatus.where(:is_closed => true).first
  119. child.save!
  120. assert_equal 'High', parent.reload.priority.name
  121. end
  122. end
  123. def test_parent_done_ratio_should_be_read_only_with_parent_issue_done_ratio_set_to_derived
  124. with_settings :parent_issue_done_ratio => 'derived' do
  125. issue = Issue.generate_with_child!
  126. user = User.find(1)
  127. assert !issue.safe_attribute?('done_ratio', user)
  128. end
  129. end
  130. def test_parent_done_ratio_should_be_average_done_ratio_of_leaves
  131. with_settings :parent_issue_done_ratio => 'derived' do
  132. parent = Issue.generate!
  133. parent.generate_child!(:done_ratio => 20)
  134. assert_equal 20, parent.reload.done_ratio
  135. parent.generate_child!(:done_ratio => 70)
  136. assert_equal 45, parent.reload.done_ratio
  137. child = parent.generate_child!(:done_ratio => 0)
  138. assert_equal 30, parent.reload.done_ratio
  139. child.generate_child!(:done_ratio => 30)
  140. assert_equal 30, child.reload.done_ratio
  141. assert_equal 40, parent.reload.done_ratio
  142. end
  143. end
  144. def test_parent_done_ratio_should_be_rounded_down_to_the_nearest_integer
  145. with_settings :parent_issue_done_ratio => 'derived' do
  146. parent = Issue.generate!
  147. parent.generate_child!(:done_ratio => 20)
  148. parent.generate_child!(:done_ratio => 20)
  149. parent.generate_child!(:done_ratio => 10)
  150. # (20 + 20 + 10) / 3 = 16.666...
  151. assert_equal 16, parent.reload.done_ratio
  152. end
  153. end
  154. def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any
  155. with_settings :parent_issue_done_ratio => 'derived' do
  156. parent = Issue.generate!
  157. parent.generate_child!(:estimated_hours => 10, :done_ratio => 20)
  158. assert_equal 20, parent.reload.done_ratio
  159. parent.generate_child!(:estimated_hours => 20, :done_ratio => 50)
  160. assert_equal (50 * 20 + 20 * 10) / 30, parent.reload.done_ratio
  161. end
  162. end
  163. def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any_with_grandchildren
  164. # parent
  165. # child 1 (2h estd, 0% done)
  166. # child 2 (no estd)
  167. # child a (2h estd, 50% done)
  168. # child b (2h estd, 50% done)
  169. #
  170. # => parent should have a calculated progress of 33%
  171. #
  172. with_settings :parent_issue_done_ratio => 'derived' do
  173. parent = Issue.generate!
  174. parent.generate_child!(:estimated_hours => 2, :done_ratio => 0)
  175. child = parent.generate_child!
  176. child.generate_child!(:estimated_hours => 2, :done_ratio => 50)
  177. child.generate_child!(:estimated_hours => 2, :done_ratio => 50)
  178. assert_equal 50, child.reload.done_ratio
  179. assert_equal 33, parent.reload.done_ratio
  180. end
  181. end
  182. def test_parent_done_ratio_with_child_estimate_to_0_should_reach_100
  183. with_settings :parent_issue_done_ratio => 'derived' do
  184. parent = Issue.generate!
  185. issue1 = parent.generate_child!
  186. issue2 = parent.generate_child!(:estimated_hours => 0)
  187. assert_equal 0, parent.reload.done_ratio
  188. issue1.reload.close!
  189. assert_equal 50, parent.reload.done_ratio
  190. issue2.reload.close!
  191. assert_equal 100, parent.reload.done_ratio
  192. end
  193. end
  194. def test_done_ratio_of_parent_with_a_child_without_estimated_time_should_not_exceed_100
  195. with_settings :parent_issue_done_ratio => 'derived' do
  196. parent = Issue.generate!
  197. parent.generate_child!(:estimated_hours => 40)
  198. parent.generate_child!(:estimated_hours => 40)
  199. parent.generate_child!(:estimated_hours => 20)
  200. parent.generate_child!
  201. parent.reload.children.each(&:close!)
  202. assert_equal 100, parent.reload.done_ratio
  203. end
  204. end
  205. def test_done_ratio_of_parent_with_a_child_with_estimated_time_at_0_should_not_exceed_100
  206. with_settings :parent_issue_done_ratio => 'derived' do
  207. parent = Issue.generate!
  208. parent.generate_child!(:estimated_hours => 40)
  209. parent.generate_child!(:estimated_hours => 40)
  210. parent.generate_child!(:estimated_hours => 20)
  211. parent.generate_child!(:estimated_hours => 0)
  212. parent.reload.children.each(&:close!)
  213. assert_equal 100, parent.reload.done_ratio
  214. end
  215. end
  216. def test_changing_parent_should_update_previous_parent_done_ratio
  217. with_settings :parent_issue_done_ratio => 'derived' do
  218. first_parent = Issue.generate!
  219. second_parent = Issue.generate!
  220. first_parent.generate_child!(:done_ratio => 40)
  221. child = first_parent.generate_child!(:done_ratio => 20)
  222. assert_equal 30, first_parent.reload.done_ratio
  223. assert_equal 0, second_parent.reload.done_ratio
  224. child.update_attributes(:parent_issue_id => second_parent.id)
  225. assert_equal 40, first_parent.reload.done_ratio
  226. assert_equal 20, second_parent.reload.done_ratio
  227. end
  228. end
  229. def test_done_ratio_of_parent_should_reflect_children
  230. root = Issue.generate!
  231. child1 = root.generate_child!
  232. child2 = child1.generate_child!
  233. assert_equal 0, root.done_ratio
  234. assert_equal 0, child1.done_ratio
  235. assert_equal 0, child2.done_ratio
  236. with_settings :issue_done_ratio => 'issue_status' do
  237. status = IssueStatus.find(4)
  238. status.update_attribute :default_done_ratio, 50
  239. child1.update_attribute :status, status
  240. assert_equal 50, child1.done_ratio
  241. root.reload
  242. assert_equal 50, root.done_ratio
  243. end
  244. end
  245. def test_parent_dates_should_be_editable_with_parent_issue_dates_set_to_independent
  246. with_settings :parent_issue_dates => 'independent' do
  247. issue = Issue.generate_with_child!
  248. user = User.find(1)
  249. %w(start_date due_date).each do |attribute|
  250. assert issue.safe_attribute?(attribute, user)
  251. end
  252. end
  253. end
  254. def test_parent_dates_should_not_be_updated_with_parent_issue_dates_set_to_independent
  255. with_settings :parent_issue_dates => 'independent' do
  256. parent = Issue.generate!(:start_date => '2015-07-01', :due_date => '2015-08-01')
  257. parent.generate_child!(:start_date => '2015-06-01', :due_date => '2015-09-01')
  258. parent.reload
  259. assert_equal Date.parse('2015-07-01'), parent.start_date
  260. assert_equal Date.parse('2015-08-01'), parent.due_date
  261. end
  262. end
  263. def test_reschuling_a_parent_should_not_reschedule_subtasks_with_parent_issue_dates_set_to_independent
  264. with_settings :parent_issue_dates => 'independent' do
  265. parent = Issue.generate!(:start_date => '2010-05-01', :due_date => '2010-05-20')
  266. c1 = parent.generate_child!(:start_date => '2010-05-12', :due_date => '2010-05-18')
  267. parent.reload.reschedule_on!(Date.parse('2010-06-01'))
  268. assert_equal Date.parse('2010-06-01'), parent.reload.start_date
  269. c1.reload
  270. assert_equal [Date.parse('2010-05-12'), Date.parse('2010-05-18')], [c1.start_date, c1.due_date]
  271. end
  272. end
  273. def test_parent_priority_should_be_editable_with_parent_issue_priority_set_to_independent
  274. with_settings :parent_issue_priority => 'independent' do
  275. issue = Issue.generate_with_child!
  276. user = User.find(1)
  277. assert issue.safe_attribute?('priority_id', user)
  278. end
  279. end
  280. def test_parent_priority_should_not_be_updated_with_parent_issue_priority_set_to_independent
  281. with_settings :parent_issue_priority => 'independent' do
  282. parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal'))
  283. child1 = parent.generate_child!(:priority => IssuePriority.find_by_name('High'))
  284. assert_equal 'Normal', parent.reload.priority.name
  285. end
  286. end
  287. def test_parent_done_ratio_should_be_editable_with_parent_issue_done_ratio_set_to_independent
  288. with_settings :parent_issue_done_ratio => 'independent' do
  289. issue = Issue.generate_with_child!
  290. user = User.find(1)
  291. assert issue.safe_attribute?('done_ratio', user)
  292. end
  293. end
  294. def test_parent_done_ratio_should_not_be_updated_with_parent_issue_done_ratio_set_to_independent
  295. with_settings :parent_issue_done_ratio => 'independent' do
  296. parent = Issue.generate!(:done_ratio => 0)
  297. child1 = parent.generate_child!(:done_ratio => 10)
  298. assert_equal 0, parent.reload.done_ratio
  299. end
  300. end
  301. def test_parent_total_estimated_hours_should_be_sum_of_descendants
  302. parent = Issue.generate!
  303. parent.generate_child!(:estimated_hours => nil)
  304. assert_equal 0, parent.reload.total_estimated_hours
  305. parent.generate_child!(:estimated_hours => 5)
  306. assert_equal 5, parent.reload.total_estimated_hours
  307. parent.generate_child!(:estimated_hours => 7)
  308. assert_equal 12, parent.reload.total_estimated_hours
  309. end
  310. def test_open_issue_with_closed_parent_should_not_validate
  311. parent = Issue.generate!(:status_id => 5)
  312. child = Issue.generate!
  313. child.parent_issue_id = parent.id
  314. assert !child.save
  315. assert_include I18n.t("activerecord.errors.messages.open_issue_with_closed_parent"), child.errors.full_messages
  316. end
  317. end