Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: require File.expand_path('../../test_helper', __FILE__) Chris@1296: Chris@1296: class TimeEntryActivityTest < ActiveSupport::TestCase Chris@1296: fixtures :enumerations, :time_entries, :custom_fields Chris@1296: Chris@1296: include Redmine::I18n Chris@1296: Chris@1296: def test_should_be_an_enumeration Chris@1296: assert TimeEntryActivity.ancestors.include?(Enumeration) Chris@1296: end Chris@1296: Chris@1296: def test_objects_count Chris@1296: assert_equal 3, TimeEntryActivity.find_by_name("Design").objects_count Chris@1296: assert_equal 2, TimeEntryActivity.find_by_name("Development").objects_count Chris@1296: end Chris@1296: Chris@1296: def test_option_name Chris@1296: assert_equal :enumeration_activities, TimeEntryActivity.new.option_name Chris@1296: end Chris@1296: Chris@1296: def test_create_with_custom_field Chris@1296: field = TimeEntryActivityCustomField.find_by_name('Billable') Chris@1296: e = TimeEntryActivity.new(:name => 'Custom Data') Chris@1296: e.custom_field_values = {field.id => "1"} Chris@1296: assert e.save Chris@1296: Chris@1296: e.reload Chris@1296: assert_equal "1", e.custom_value_for(field).value Chris@1296: end Chris@1296: Chris@1296: def test_create_without_required_custom_field_should_fail Chris@1296: set_language_if_valid 'en' Chris@1296: field = TimeEntryActivityCustomField.find_by_name('Billable') Chris@1296: field.update_attribute(:is_required, true) Chris@1296: Chris@1296: e = TimeEntryActivity.new(:name => 'Custom Data') Chris@1296: assert !e.save Chris@1296: assert_equal ["Billable can't be blank"], e.errors.full_messages Chris@1296: end Chris@1296: Chris@1296: def test_create_with_required_custom_field_should_succeed Chris@1296: field = TimeEntryActivityCustomField.find_by_name('Billable') Chris@1296: field.update_attribute(:is_required, true) Chris@1296: Chris@1296: e = TimeEntryActivity.new(:name => 'Custom Data') Chris@1296: e.custom_field_values = {field.id => "1"} Chris@1296: assert e.save Chris@1296: end Chris@1296: Chris@1296: def test_update_with_required_custom_field_change Chris@1296: set_language_if_valid 'en' Chris@1296: field = TimeEntryActivityCustomField.find_by_name('Billable') Chris@1296: field.update_attribute(:is_required, true) Chris@1296: Chris@1296: e = TimeEntryActivity.find(10) Chris@1296: assert e.available_custom_fields.include?(field) Chris@1296: # No change to custom field, record can be saved Chris@1296: assert e.save Chris@1296: # Blanking custom field, save should fail Chris@1296: e.custom_field_values = {field.id => ""} Chris@1296: assert !e.save Chris@1296: assert_equal ["Billable can't be blank"], e.errors.full_messages Chris@1296: Chris@1296: # Update custom field to valid value, save should succeed Chris@1296: e.custom_field_values = {field.id => "0"} Chris@1296: assert e.save Chris@1296: e.reload Chris@1296: assert_equal "0", e.custom_value_for(field).value Chris@1296: end Chris@1296: end Chris@1296: