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