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.

time_entry_test.rb 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 TimeEntryTest < ActiveSupport::TestCase
  19. include Redmine::I18n
  20. fixtures :issues, :projects, :users, :time_entries,
  21. :members, :roles, :member_roles,
  22. :trackers, :issue_statuses,
  23. :projects_trackers,
  24. :journals, :journal_details,
  25. :issue_categories, :enumerations,
  26. :groups_users,
  27. :enabled_modules
  28. def test_visibility_with_permission_to_view_all_time_entries
  29. user = User.generate!
  30. role = Role.generate!(:permissions => [:view_time_entries], :time_entries_visibility => 'all')
  31. Role.non_member.remove_permission! :view_time_entries
  32. project = Project.find(1)
  33. User.add_to_project user, project, role
  34. own = TimeEntry.generate! :user => user, :project => project
  35. other = TimeEntry.generate! :user => User.find(2), :project => project
  36. assert TimeEntry.visible(user).find_by_id(own.id)
  37. assert TimeEntry.visible(user).find_by_id(other.id)
  38. assert own.visible?(user)
  39. assert other.visible?(user)
  40. end
  41. def test_visibility_with_permission_to_view_own_time_entries
  42. user = User.generate!
  43. role = Role.generate!(:permissions => [:view_time_entries], :time_entries_visibility => 'own')
  44. Role.non_member.remove_permission! :view_time_entries
  45. project = Project.find(1)
  46. User.add_to_project user, project, role
  47. own = TimeEntry.generate! :user => user, :project => project
  48. other = TimeEntry.generate! :user => User.find(2), :project => project
  49. assert TimeEntry.visible(user).find_by_id(own.id)
  50. assert_nil TimeEntry.visible(user).find_by_id(other.id)
  51. assert own.visible?(user)
  52. assert_equal false, other.visible?(user)
  53. end
  54. def test_hours_format
  55. assertions = { "2" => 2.0,
  56. "21.1" => 21.1,
  57. "2,1" => 2.1,
  58. "1,5h" => 1.5,
  59. "7:12" => 7.2,
  60. "10h" => 10.0,
  61. "10 h" => 10.0,
  62. "45m" => 0.75,
  63. "45 m" => 0.75,
  64. "3h15" => 3.25,
  65. "3h 15" => 3.25,
  66. "3 h 15" => 3.25,
  67. "3 h 15m" => 3.25,
  68. "3 h 15 m" => 3.25,
  69. "3 hours" => 3.0,
  70. "12min" => 0.2,
  71. "12 Min" => 0.2,
  72. }
  73. assertions.each do |k, v|
  74. t = TimeEntry.new(:hours => k)
  75. assert_equal v, t.hours, "Converting #{k} failed:"
  76. end
  77. end
  78. def test_hours_should_default_to_nil
  79. assert_nil TimeEntry.new.hours
  80. end
  81. def test_should_accept_0_hours
  82. entry = TimeEntry.generate
  83. entry.hours = 0
  84. assert entry.save
  85. end
  86. def test_should_not_accept_0_hours_if_disabled
  87. with_settings :timelog_accept_0_hours => '0' do
  88. entry = TimeEntry.generate
  89. entry.hours = 0
  90. assert !entry.save
  91. assert entry.errors[:hours].present?
  92. end
  93. end
  94. def test_should_not_accept_more_than_maximum_hours_per_day_and_user
  95. with_settings :timelog_max_hours_per_day => '8' do
  96. entry = TimeEntry.generate(:spent_on => '2017-07-16', :hours => 6.0, :user_id => 2)
  97. assert entry.save
  98. entry = TimeEntry.generate(:spent_on => '2017-07-16', :hours => 1.5, :user_id => 2)
  99. assert entry.save
  100. entry = TimeEntry.generate(:spent_on => '2017-07-16', :hours => 3.0, :user_id => 2)
  101. assert !entry.save
  102. end
  103. end
  104. def test_should_accept_future_dates
  105. entry = TimeEntry.generate
  106. entry.spent_on = User.current.today + 1
  107. assert entry.save
  108. end
  109. def test_should_not_accept_future_dates_if_disabled
  110. with_settings :timelog_accept_future_dates => '0' do
  111. entry = TimeEntry.generate
  112. entry.spent_on = User.current.today + 1
  113. assert !entry.save
  114. assert entry.errors[:base].present?
  115. end
  116. end
  117. def test_spent_on_with_blank
  118. c = TimeEntry.new
  119. c.spent_on = ''
  120. assert_nil c.spent_on
  121. end
  122. def test_spent_on_with_nil
  123. c = TimeEntry.new
  124. c.spent_on = nil
  125. assert_nil c.spent_on
  126. end
  127. def test_spent_on_with_string
  128. c = TimeEntry.new
  129. c.spent_on = "2011-01-14"
  130. assert_equal Date.parse("2011-01-14"), c.spent_on
  131. end
  132. def test_spent_on_with_invalid_string
  133. c = TimeEntry.new
  134. c.spent_on = "foo"
  135. assert_nil c.spent_on
  136. end
  137. def test_spent_on_with_date
  138. c = TimeEntry.new
  139. c.spent_on = Date.today
  140. assert_equal Date.today, c.spent_on
  141. end
  142. def test_spent_on_with_time
  143. c = TimeEntry.new
  144. c.spent_on = Time.now
  145. assert_kind_of Date, c.spent_on
  146. end
  147. def test_validate_time_entry
  148. anon = User.anonymous
  149. project = Project.find(1)
  150. issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
  151. :priority => IssuePriority.all.first, :subject => 'test_create',
  152. :description => 'IssueTest#test_create', :estimated_hours => '1:30')
  153. assert issue.save
  154. activity = TimeEntryActivity.find_by_name('Design')
  155. te = TimeEntry.create(:spent_on => '2010-01-01',
  156. :hours => 100000,
  157. :issue => issue,
  158. :project => project,
  159. :user => anon,
  160. :author => anon,
  161. :activity => activity)
  162. assert_equal 1, te.errors.count
  163. end
  164. def test_acitivity_should_belong_to_project_activities
  165. activity = TimeEntryActivity.create!(:name => 'Other project activity', :project_id => 2, :active => true)
  166. entry = TimeEntry.new(:spent_on => Date.today, :hours => 1.0, :user => User.find(1), :project_id => 1, :activity => activity)
  167. assert !entry.valid?
  168. assert_include I18n.translate('activerecord.errors.messages.inclusion'), entry.errors[:activity_id]
  169. end
  170. def test_spent_on_with_2_digits_year_should_not_be_valid
  171. entry = TimeEntry.new(:project => Project.find(1), :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1)
  172. entry.spent_on = "09-02-04"
  173. assert !entry.valid?
  174. assert_include I18n.translate('activerecord.errors.messages.not_a_date'), entry.errors[:spent_on]
  175. end
  176. def test_set_project_if_nil
  177. anon = User.anonymous
  178. project = Project.find(1)
  179. issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1,
  180. :priority => IssuePriority.all.first, :subject => 'test_create',
  181. :description => 'IssueTest#test_create', :estimated_hours => '1:30')
  182. assert issue.save
  183. activity = TimeEntryActivity.find_by_name('Design')
  184. te = TimeEntry.create(:spent_on => '2010-01-01',
  185. :hours => 10,
  186. :issue => issue,
  187. :user => anon,
  188. :activity => activity)
  189. assert_equal project.id, te.project.id
  190. end
  191. def test_create_with_required_issue_id_and_comment_should_be_validated
  192. set_language_if_valid 'en'
  193. with_settings :timelog_required_fields => ['issue_id' , 'comments'] do
  194. entry = TimeEntry.new(:project => Project.find(1), :spent_on => Date.today, :author => User.find(1), :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1)
  195. assert !entry.save
  196. assert_equal ["Comment cannot be blank", "Issue cannot be blank"], entry.errors.full_messages.sort
  197. end
  198. end
  199. def test_create_should_validate_user_id
  200. entry = TimeEntry.new(:spent_on => '2010-01-01',
  201. :hours => 10,
  202. :project_id => 1,
  203. :user_id => 4)
  204. assert !entry.save
  205. assert_equal ["User is invalid"], entry.errors.full_messages.sort
  206. end
  207. def test_assignable_users_should_include_active_project_members_with_log_time_permission
  208. Role.find(2).remove_permission! :log_time
  209. time_entry = TimeEntry.find(1)
  210. assert_equal [2], time_entry.assignable_users.map(&:id)
  211. end
  212. end