|
| 1 | +require File.dirname(__FILE__) + '/../test_helper' |
| 2 | + |
| 3 | +class CustomMessageSettingTest < ActiveSupport::TestCase |
| 4 | + fixtures :custom_message_settings |
| 5 | + include Redmine::I18n |
| 6 | + |
| 7 | + def setup |
| 8 | + @custom_message_setting = CustomMessageSetting.find(1) |
| 9 | + end |
| 10 | + |
| 11 | + def test_validate_with_not_available_keys_should_return_false |
| 12 | + @custom_message_setting.value = { custom_messages: { 'en' => {'foobar' => 'foobar' }} } |
| 13 | + assert_not @custom_message_setting.save |
| 14 | + assert_equal "#{l(:error_unavailable_keys)} keys: [foobar]", @custom_message_setting.errors[:base].first |
| 15 | + end |
| 16 | + |
| 17 | + def test_find_or_default |
| 18 | + assert_equal @custom_message_setting, CustomMessageSetting.find_or_default |
| 19 | + end |
| 20 | + |
| 21 | + def test_custom_messages |
| 22 | + assert_equal @custom_message_setting.value['custom_messages'], @custom_message_setting.custom_messages |
| 23 | + assert_equal ({'label_home' => 'Home1'}), @custom_message_setting.custom_messages('en') |
| 24 | + assert_equal ({}), @custom_message_setting.custom_messages('foo') |
| 25 | + end |
| 26 | + |
| 27 | + def test_custom_messages_to_yaml |
| 28 | + assert_equal "---\nen:\n label_home: Home1\nja:\n label_home: Home2\n", @custom_message_setting.custom_messages_to_yaml |
| 29 | + |
| 30 | + @custom_message_setting.value = { custom_messages: {} } |
| 31 | + assert_equal '', @custom_message_setting.custom_messages_to_yaml |
| 32 | + |
| 33 | + @custom_message_setting.value = { custom_messages: 'test' } |
| 34 | + assert_equal 'test', @custom_message_setting.custom_messages_to_yaml |
| 35 | + end |
| 36 | + |
| 37 | + def test_update_with_custom_messages_if_custom_messages_is_present |
| 38 | + flatten_hash = {'label_home' => 'Home3', 'time.am' => 'foo'} |
| 39 | + |
| 40 | + assert @custom_message_setting.update_with_custom_messages(flatten_hash, 'en') |
| 41 | + assert_equal ({'label_home' => 'Home3', 'time' => { 'am' => 'foo'}}), @custom_message_setting.custom_messages('en') |
| 42 | + end |
| 43 | + def test_update_with_custom_messages_if_custom_messages_is_blank |
| 44 | + assert @custom_message_setting.update_with_custom_messages({}, 'en') |
| 45 | + assert_not @custom_message_setting.custom_messages.key('en') |
| 46 | + end |
| 47 | + |
| 48 | + def test_update_with_custom_messages_yaml_if_yaml_is_valid |
| 49 | + yaml = "---\nen:\n label_home: Home3" |
| 50 | + assert @custom_message_setting.update_with_custom_messages_yaml(yaml) |
| 51 | + assert_equal ({ 'label_home' => 'Home3' }), @custom_message_setting.custom_messages('en') |
| 52 | + end |
| 53 | + def test_update_with_custom_messages_yaml_if_yaml_is_invalid |
| 54 | + yaml = "---\nen:\n label_home: Home3\ninvalid-string" |
| 55 | + assert_not @custom_message_setting.update_with_custom_messages_yaml(yaml) |
| 56 | + assert_equal "(<unknown>): could not find expected ':' while scanning a simple key at line 4 column 1", @custom_message_setting.errors[:base].first |
| 57 | + end |
| 58 | + |
| 59 | + def test_available_messages_should_flatten_translations |
| 60 | + flatten_hash = CustomMessageSetting.available_messages('en') |
| 61 | + assert_equal 'am', flatten_hash[:'time.am'] |
| 62 | + |
| 63 | + # Language 'ar' not loaded |
| 64 | + flatten_hash = CustomMessageSetting.available_messages('ar') |
| 65 | + assert_equal "صباحا", flatten_hash[:'time.am'] |
| 66 | + end |
| 67 | + |
| 68 | + def test_flatten_hash_should_return_hash_with_flat_keys |
| 69 | + flatten_hash = CustomMessageSetting.flatten_hash({time: {am: 'foo'}}) |
| 70 | + assert_equal ({:'time.am' => 'foo'}), flatten_hash |
| 71 | + end |
| 72 | + |
| 73 | + def test_flatten_hash_should_return_nest_hash |
| 74 | + nested_hash = CustomMessageSetting.nested_hash({:'time.am' => 'foo'}) |
| 75 | + assert_equal ({'time' => {'am' => 'foo'}}), nested_hash |
| 76 | + end |
| 77 | + |
| 78 | + def test_reload_translations! |
| 79 | + assert_nil I18n.backend.translations[:fr] |
| 80 | + CustomMessageSetting.reload_translations!(['fr']) |
| 81 | + assert_not_nil I18n.backend.translations[:fr] |
| 82 | + end |
| 83 | +end |
0 commit comments