Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

time_entry_activity_test.rb 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 TimeEntryActivityTest < ActiveSupport::TestCase
  20. fixtures :enumerations, :time_entries,
  21. :custom_fields, :custom_values,
  22. :issues, :projects, :users,
  23. :members, :roles, :member_roles,
  24. :trackers, :issue_statuses,
  25. :projects_trackers,
  26. :issue_categories,
  27. :groups_users,
  28. :enabled_modules
  29. include Redmine::I18n
  30. def setup
  31. User.current = nil
  32. end
  33. def test_should_be_an_enumeration
  34. assert TimeEntryActivity.ancestors.include?(Enumeration)
  35. end
  36. def test_objects_count
  37. assert_equal 3, TimeEntryActivity.find_by_name("Design").objects_count
  38. assert_equal 2, TimeEntryActivity.find_by_name("Development").objects_count
  39. end
  40. def test_option_name
  41. assert_equal :enumeration_activities, TimeEntryActivity.new.option_name
  42. end
  43. def test_create_with_custom_field
  44. field = TimeEntryActivityCustomField.find_by_name('Billable')
  45. e = TimeEntryActivity.new(:name => 'Custom Data')
  46. e.custom_field_values = {field.id => "1"}
  47. assert e.save
  48. e.reload
  49. assert_equal "1", e.custom_value_for(field).value
  50. end
  51. def test_create_without_required_custom_field_should_fail
  52. set_language_if_valid 'en'
  53. field = TimeEntryActivityCustomField.find_by_name('Billable')
  54. field.update_attribute(:is_required, true)
  55. e = TimeEntryActivity.new(:name => 'Custom Data')
  56. assert !e.save
  57. assert_equal ["Billable cannot be blank"], e.errors.full_messages
  58. end
  59. def test_create_with_required_custom_field_should_succeed
  60. field = TimeEntryActivityCustomField.find_by_name('Billable')
  61. field.update_attribute(:is_required, true)
  62. e = TimeEntryActivity.new(:name => 'Custom Data')
  63. e.custom_field_values = {field.id => "1"}
  64. assert e.save
  65. end
  66. def test_update_with_required_custom_field_change
  67. set_language_if_valid 'en'
  68. field = TimeEntryActivityCustomField.find_by_name('Billable')
  69. field.update_attribute(:is_required, true)
  70. e = TimeEntryActivity.find(10)
  71. assert e.available_custom_fields.include?(field)
  72. # No change to custom field, record can be saved
  73. assert e.save
  74. # Blanking custom field, save should fail
  75. e.custom_field_values = {field.id => ""}
  76. assert !e.save
  77. assert_equal ["Billable cannot be blank"], e.errors.full_messages
  78. # Update custom field to valid value, save should succeed
  79. e.custom_field_values = {field.id => "0"}
  80. assert e.save
  81. e.reload
  82. assert_equal "0", e.custom_value_for(field).value
  83. end
  84. def test_system_activity_with_child_in_use_should_be_in_use
  85. project = Project.generate!
  86. system_activity = TimeEntryActivity.create!(:name => 'Activity')
  87. project_activity =
  88. TimeEntryActivity.create!(:name => 'Activity', :project => project,
  89. :parent_id => system_activity.id)
  90. TimeEntry.generate!(:project => project, :activity => project_activity)
  91. assert project_activity.in_use?
  92. assert system_activity.in_use?
  93. end
  94. def test_destroying_a_system_activity_should_reassign_children_activities
  95. project = Project.generate!
  96. entries = []
  97. system_activity = TimeEntryActivity.create!(:name => 'Activity')
  98. entries << TimeEntry.generate!(:project => project, :activity => system_activity)
  99. project_activity =
  100. TimeEntryActivity.create!(:name => 'Activity', :project => project,
  101. :parent_id => system_activity.id)
  102. entries << TimeEntry.generate!(:project => project.reload, :activity => project_activity)
  103. assert_difference 'TimeEntryActivity.count', -2 do
  104. assert_nothing_raised do
  105. assert system_activity.destroy(TimeEntryActivity.find_by_name('Development'))
  106. end
  107. end
  108. assert entries.all? {|entry| entry.reload.activity.name == 'Development'}
  109. end
  110. def test_project_activity_without_parent_should_not_disable_system_activities
  111. project = Project.find(1)
  112. activity = TimeEntryActivity.create!(:name => 'Csutom', :project => project)
  113. assert_include activity, project.activities
  114. assert_include TimeEntryActivity.find(9), project.activities
  115. end
  116. def test_project_activity_should_have_the_same_position_as_parent_activity
  117. project = Project.find(1)
  118. parent_activity = TimeEntryActivity.find_by(position: 3, parent_id: nil)
  119. project.update_or_create_time_entry_activities(
  120. {
  121. parent_activity.id.to_s => {
  122. 'parent_id' => parent_activity.id.to_s,
  123. 'active' => '0',
  124. 'custom_field_values' => {'7' => ''}
  125. }
  126. }
  127. )
  128. project_activity = TimeEntryActivity.find_by(position: 3, parent_id: parent_activity.id, project_id: 1)
  129. assert_equal parent_activity.position, project_activity.position
  130. # Changing the position of the parent activity also changes the position of the activity in each project.
  131. other_parent_activity = TimeEntryActivity.find_by(position: 4, parent_id: nil)
  132. project.update_or_create_time_entry_activities(
  133. {
  134. other_parent_activity.id.to_s => {
  135. 'parent_id' => other_parent_activity.id.to_s,
  136. 'active' => '0',
  137. 'custom_field_values' => {'7' => ''}
  138. }
  139. }
  140. )
  141. other_project_activity = TimeEntryActivity.find_by(position: 4, parent_id: other_parent_activity.id, project_id: 1)
  142. parent_activity.update(position: 4)
  143. assert_equal 4, parent_activity.reload.position
  144. assert_equal parent_activity.position, project_activity.reload.position
  145. assert_equal 3, other_parent_activity.reload.position
  146. assert_equal other_parent_activity.position, other_project_activity.reload.position
  147. end
  148. def test_project_activity_should_have_the_same_name_as_parent_activity
  149. parent_activity = TimeEntryActivity.find_by(name: 'Design', parent_id: nil)
  150. project = Project.find(1)
  151. project.update_or_create_time_entry_activities(
  152. {
  153. parent_activity.id.to_s => {
  154. 'parent_id' => parent_activity.id.to_s,
  155. 'active' => '0',
  156. 'custom_field_values' => {'7' => ''}
  157. }
  158. }
  159. )
  160. project_activity = TimeEntryActivity.find_by(name: 'Design', parent_id: parent_activity.id, project_id: project.id)
  161. assert_equal parent_activity.name, project_activity.name
  162. parent_activity.update(name: 'Design1')
  163. assert_equal parent_activity.reload.name, project_activity.reload.name
  164. # When changing the name of parent_activity,
  165. # if the name of parent_activity before the change and the name of project_activity do not match, the name of project_activity is not changed.
  166. project_activity.update(name: 'Design2')
  167. parent_activity.update(name: 'Design3')
  168. assert_equal 'Design2', project_activity.reload.name
  169. assert_equal 'Design3', parent_activity.reload.name
  170. end
  171. end