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_activity_test.rb 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 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 TimeEntryActivityTest < ActiveSupport::TestCase
  19. fixtures :enumerations, :time_entries, :custom_fields
  20. include Redmine::I18n
  21. def test_should_be_an_enumeration
  22. assert TimeEntryActivity.ancestors.include?(Enumeration)
  23. end
  24. def test_objects_count
  25. assert_equal 3, TimeEntryActivity.find_by_name("Design").objects_count
  26. assert_equal 2, TimeEntryActivity.find_by_name("Development").objects_count
  27. end
  28. def test_option_name
  29. assert_equal :enumeration_activities, TimeEntryActivity.new.option_name
  30. end
  31. def test_create_with_custom_field
  32. field = TimeEntryActivityCustomField.find_by_name('Billable')
  33. e = TimeEntryActivity.new(:name => 'Custom Data')
  34. e.custom_field_values = {field.id => "1"}
  35. assert e.save
  36. e.reload
  37. assert_equal "1", e.custom_value_for(field).value
  38. end
  39. def test_create_without_required_custom_field_should_fail
  40. set_language_if_valid 'en'
  41. field = TimeEntryActivityCustomField.find_by_name('Billable')
  42. field.update_attribute(:is_required, true)
  43. e = TimeEntryActivity.new(:name => 'Custom Data')
  44. assert !e.save
  45. assert_equal ["Billable can't be blank"], e.errors.full_messages
  46. end
  47. def test_create_with_required_custom_field_should_succeed
  48. field = TimeEntryActivityCustomField.find_by_name('Billable')
  49. field.update_attribute(:is_required, true)
  50. e = TimeEntryActivity.new(:name => 'Custom Data')
  51. e.custom_field_values = {field.id => "1"}
  52. assert e.save
  53. end
  54. def test_update_with_required_custom_field_change
  55. set_language_if_valid 'en'
  56. field = TimeEntryActivityCustomField.find_by_name('Billable')
  57. field.update_attribute(:is_required, true)
  58. e = TimeEntryActivity.find(10)
  59. assert e.available_custom_fields.include?(field)
  60. # No change to custom field, record can be saved
  61. assert e.save
  62. # Blanking custom field, save should fail
  63. e.custom_field_values = {field.id => ""}
  64. assert !e.save
  65. assert_equal ["Billable can't be blank"], e.errors.full_messages
  66. # Update custom field to valid value, save should succeed
  67. e.custom_field_values = {field.id => "0"}
  68. assert e.save
  69. e.reload
  70. assert_equal "0", e.custom_value_for(field).value
  71. end
  72. def test_system_activity_with_child_in_use_should_be_in_use
  73. project = Project.generate!
  74. system_activity = TimeEntryActivity.create!(:name => 'Activity')
  75. project_activity = TimeEntryActivity.create!(:name => 'Activity', :project => project, :parent_id => system_activity.id)
  76. TimeEntry.generate!(:project => project, :activity => project_activity)
  77. assert project_activity.in_use?
  78. assert system_activity.in_use?
  79. end
  80. def test_destroying_a_system_activity_should_reassign_children_activities
  81. project = Project.generate!
  82. system_activity = TimeEntryActivity.create!(:name => 'Activity')
  83. project_activity = TimeEntryActivity.create!(:name => 'Activity', :project => project, :parent_id => system_activity.id)
  84. entries = [
  85. TimeEntry.generate!(:project => project, :activity => system_activity),
  86. TimeEntry.generate!(:project => project, :activity => project_activity)
  87. ]
  88. assert_difference 'TimeEntryActivity.count', -2 do
  89. assert_nothing_raised do
  90. assert system_activity.destroy(TimeEntryActivity.find_by_name('Development'))
  91. end
  92. end
  93. assert entries.all? {|entry| entry.reload.activity.name == 'Development'}
  94. end
  95. end