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

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