Skip to content

Commit 1b2dc4e

Browse files
committed
Add tests
1 parent deeed18 commit 1b2dc4e

File tree

7 files changed

+176
-14
lines changed

7 files changed

+176
-14
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ $ cd /your/path/redmine/plugins
1616
$ git clone https://github.com/ishikawa999/redmine_message_customize.git
1717
$ # redmine restart
1818
```
19+
20+
## Run test
21+
22+
```
23+
$ cd /your/path/redmine
24+
$ bundle exec rake test TEST=plugins/redmine_message_customize/test RAILS_ENV=test
25+
```

app/helpers/custom_message_settings_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def available_message_options(setting, lang)
88

99
def normal_mode_input_fields(setting, lang)
1010
return '' if setting.value[:custom_messages].is_a?(String) || setting.value[:custom_messages].blank?
11-
content = ''
11+
content = ActiveSupport::SafeBuffer.new
1212
custom_messages_hash = setting.custom_messages_to_flatten_hash(lang.to_s)
1313
custom_messages_hash.each do |k, v|
1414
content += content_tag(:p) do
@@ -17,6 +17,6 @@ def normal_mode_input_fields(setting, lang)
1717
link_to_function('', '$(this).closest("p").remove();', class: 'icon-only icon-del clear-key-link')
1818
end
1919
end
20-
content.html_safe
20+
content
2121
end
2222
end

app/models/custom_message_setting.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@ def self.find_or_default
66
end
77

88
def custom_messages(lang=nil)
9+
messages = self.value[:custom_messages] || self.value['custom_messages']
910
if lang.present?
10-
self.value.dig(:custom_messages, self.class.find_language(lang)) || {}
11-
else
12-
self.value[:custom_messages] || {}
11+
messages = messages[self.class.find_language(lang)]
1312
end
13+
14+
messages.present? ? messages : {}
1415
end
1516

1617
def custom_messages_to_flatten_hash(lang=nil)
1718
self.class.flatten_hash(custom_messages(lang))
1819
end
1920

2021
def custom_messages_to_yaml
21-
if self.custom_messages.blank?
22+
messages = self.custom_messages
23+
if messages.blank?
2224
''
23-
elsif self.custom_messages.is_a?(Hash)
24-
YAML.dump(self.custom_messages)
25+
elsif messages.is_a?(Hash)
26+
YAML.dump(messages)
2527
else
26-
self.custom_messages
28+
messages
2729
end
2830
end
2931

@@ -54,7 +56,7 @@ def update_with_custom_messages_yaml(yaml)
5456
self.save
5557
end
5658

57-
def self.available_messages(lang='en')
59+
def self.available_messages(lang)
5860
messages = I18n.backend.translations[self.find_language(lang).to_sym]
5961
if messages.nil?
6062
CustomMessageSetting.reload_translations!([lang])
@@ -112,10 +114,10 @@ def custom_message_keys_are_available
112114
custom_messages.values.compact.each do |val|
113115
custom_messages_hash = self.class.flatten_hash(custom_messages_hash.merge(val)) if val.is_a?(Hash)
114116
end
115-
available_keys = self.class.flatten_hash(self.class.available_messages).keys
116-
unavailable_keys = custom_messages_hash.keys.reject{|k|available_keys.include?(k.to_sym)}
117+
available_keys = self.class.flatten_hash(self.class.available_messages('en')).keys
118+
unavailable_keys = custom_messages_hash.keys.reject{|k| available_keys.include?(k.to_sym)}
117119
if unavailable_keys.present?
118-
self.errors.add(:base, l(:error_unavailable_keys) + "keys: [#{unavailable_keys.join(', ')}]")
120+
self.errors.add(:base, l(:error_unavailable_keys) + " keys: [#{unavailable_keys.join(', ')}]")
119121
false
120122
end
121123
end
@@ -125,10 +127,11 @@ def convertible_to_yaml
125127
end
126128

127129
def add_errors
128-
unless @errs.blank?
130+
if @errs.present?
129131
@errs.each do |key, value|
130132
self.errors.add(key, value)
131133
end
134+
@errs = nil
132135
false
133136
end
134137
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
one:
2+
id: 1
3+
name: plugin_redmine_message_customize
4+
value: { custom_messages: { en: { label_home: 'Home1' }, ja: { label_home: 'Home2' }}}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require File.dirname(__FILE__) + '/../test_helper'
2+
3+
class CustomMessageSettingsControllerTest < Redmine::ControllerTest
4+
fixtures :custom_message_settings
5+
include Redmine::I18n
6+
7+
def setup
8+
@request.session[:user_id] = 1 # admin
9+
CustomMessageSetting.reload_translations!('en')
10+
end
11+
12+
# custom_message_settings/edit
13+
def test_edit
14+
get :edit
15+
assert_response :success
16+
17+
assert_select 'h2', :text => l(:label_custom_messages)
18+
assert_select 'div.tabs' do
19+
assert_select 'a#tab-normal'
20+
assert_select 'a#tab-yaml'
21+
end
22+
end
23+
def test_edit_except_admin_user
24+
@request.session[:user_id] = 2
25+
get :edit
26+
assert_response 403
27+
assert_select 'p#errorExplanation', text: 'You are not authorized to access this page.'
28+
end
29+
30+
def test_update_with_custom_messages
31+
assert_equal 'Home1', l(:label_home)
32+
33+
get :update, params: { settings: {'custom_messages'=>{'label_home' => 'Home3'}}, lang: 'en', tab: 'normal' }
34+
35+
assert_equal 'Home3', l(:label_home)
36+
assert_redirected_to edit_custom_message_settings_path(lang: 'en', tab: 'normal')
37+
assert_equal l(:notice_successful_update), flash[:notice]
38+
end
39+
def test_update_with_custom_messages_yaml
40+
assert_equal 'Home1', l(:label_home)
41+
42+
get :update, params: { settings: {'custom_messages_yaml'=>"---\nen:\n label_home: Home3"}, tab: 'yaml' }
43+
44+
assert_equal 'Home3', l(:label_home)
45+
assert_redirected_to edit_custom_message_settings_path(lang: 'en', tab: 'yaml')
46+
assert_equal l(:notice_successful_update), flash[:notice]
47+
end
48+
def test_update_with_invalid_params
49+
get :update, params: { settings: {'custom_messages'=>{'foobar'=>'foobar'}, lang: 'en' }}
50+
51+
assert_response 200
52+
assert_select 'h2', :text => l(:label_custom_messages)
53+
assert_select 'div#errorExplanation'
54+
end
55+
def test_edit_except_admin_user
56+
@request.session[:user_id] = 2
57+
get :update, params: { settings: {'custom_messages'=>{'label_home' => 'Home3'}}, lang: 'en', tab: 'normal' }
58+
59+
assert_response 403
60+
assert_select 'p#errorExplanation', text: 'You are not authorized to access this page.'
61+
end
62+
end

test/test_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Load the normal Rails helper
2+
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
3+
ActiveRecord::FixtureSet.create_fixtures(File.dirname(__FILE__) + '/fixtures', 'custom_message_settings')
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)