##// END OF EJS Templates
Moved test from ApplicationControllerTest....
Jean-Philippe Lang -
r8365:610a92c4d1e8
parent child
Show More
@@ -1,100 +1,90
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19 require 'application_controller'
19 require 'application_controller'
20
20
21 class ApplicationControllerTest < ActionController::TestCase
21 class ApplicationControllerTest < ActionController::TestCase
22 include Redmine::I18n
22 include Redmine::I18n
23
23
24 def setup
24 def setup
25 @controller = ApplicationController.new
25 @controller = ApplicationController.new
26 @request = ActionController::TestRequest.new
26 @request = ActionController::TestRequest.new
27 @response = ActionController::TestResponse.new
27 @response = ActionController::TestResponse.new
28 end
28 end
29
29
30 # check that all language files are valid
31 def test_localization
32 lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
33 assert_equal lang_files_count, valid_languages.size
34 valid_languages.each do |lang|
35 assert set_language_if_valid(lang)
36 end
37 set_language_if_valid('en')
38 end
39
40 def test_call_hook_mixed_in
30 def test_call_hook_mixed_in
41 assert @controller.respond_to?(:call_hook)
31 assert @controller.respond_to?(:call_hook)
42 end
32 end
43
33
44 context "test_api_offset_and_limit" do
34 context "test_api_offset_and_limit" do
45 context "without params" do
35 context "without params" do
46 should "return 0, 25" do
36 should "return 0, 25" do
47 assert_equal [0, 25], @controller.api_offset_and_limit({})
37 assert_equal [0, 25], @controller.api_offset_and_limit({})
48 end
38 end
49 end
39 end
50
40
51 context "with limit" do
41 context "with limit" do
52 should "return 0, limit" do
42 should "return 0, limit" do
53 assert_equal [0, 30], @controller.api_offset_and_limit({:limit => 30})
43 assert_equal [0, 30], @controller.api_offset_and_limit({:limit => 30})
54 end
44 end
55
45
56 should "not exceed 100" do
46 should "not exceed 100" do
57 assert_equal [0, 100], @controller.api_offset_and_limit({:limit => 120})
47 assert_equal [0, 100], @controller.api_offset_and_limit({:limit => 120})
58 end
48 end
59
49
60 should "not be negative" do
50 should "not be negative" do
61 assert_equal [0, 25], @controller.api_offset_and_limit({:limit => -10})
51 assert_equal [0, 25], @controller.api_offset_and_limit({:limit => -10})
62 end
52 end
63 end
53 end
64
54
65 context "with offset" do
55 context "with offset" do
66 should "return offset, 25" do
56 should "return offset, 25" do
67 assert_equal [10, 25], @controller.api_offset_and_limit({:offset => 10})
57 assert_equal [10, 25], @controller.api_offset_and_limit({:offset => 10})
68 end
58 end
69
59
70 should "not be negative" do
60 should "not be negative" do
71 assert_equal [0, 25], @controller.api_offset_and_limit({:offset => -10})
61 assert_equal [0, 25], @controller.api_offset_and_limit({:offset => -10})
72 end
62 end
73
63
74 context "and limit" do
64 context "and limit" do
75 should "return offset, limit" do
65 should "return offset, limit" do
76 assert_equal [10, 50], @controller.api_offset_and_limit({:offset => 10, :limit => 50})
66 assert_equal [10, 50], @controller.api_offset_and_limit({:offset => 10, :limit => 50})
77 end
67 end
78 end
68 end
79 end
69 end
80
70
81 context "with page" do
71 context "with page" do
82 should "return offset, 25" do
72 should "return offset, 25" do
83 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 1})
73 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 1})
84 assert_equal [50, 25], @controller.api_offset_and_limit({:page => 3})
74 assert_equal [50, 25], @controller.api_offset_and_limit({:page => 3})
85 end
75 end
86
76
87 should "not be negative" do
77 should "not be negative" do
88 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 0})
78 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 0})
89 assert_equal [0, 25], @controller.api_offset_and_limit({:page => -2})
79 assert_equal [0, 25], @controller.api_offset_and_limit({:page => -2})
90 end
80 end
91
81
92 context "and limit" do
82 context "and limit" do
93 should "return offset, limit" do
83 should "return offset, limit" do
94 assert_equal [0, 100], @controller.api_offset_and_limit({:page => 1, :limit => 100})
84 assert_equal [0, 100], @controller.api_offset_and_limit({:page => 1, :limit => 100})
95 assert_equal [200, 100], @controller.api_offset_and_limit({:page => 3, :limit => 100})
85 assert_equal [200, 100], @controller.api_offset_and_limit({:page => 3, :limit => 100})
96 end
86 end
97 end
87 end
98 end
88 end
99 end
89 end
100 end
90 end
@@ -1,159 +1,168
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../../../test_helper', __FILE__)
18 require File.expand_path('../../../../test_helper', __FILE__)
19
19
20 class Redmine::I18nTest < ActiveSupport::TestCase
20 class Redmine::I18nTest < ActiveSupport::TestCase
21 include Redmine::I18n
21 include Redmine::I18n
22 include ActionView::Helpers::NumberHelper
22 include ActionView::Helpers::NumberHelper
23
23
24 def setup
24 def setup
25 @hook_module = Redmine::Hook
25 @hook_module = Redmine::Hook
26 end
26 end
27
27
28 def test_date_format_default
28 def test_date_format_default
29 set_language_if_valid 'en'
29 set_language_if_valid 'en'
30 today = Date.today
30 today = Date.today
31 Setting.date_format = ''
31 Setting.date_format = ''
32 assert_equal I18n.l(today), format_date(today)
32 assert_equal I18n.l(today), format_date(today)
33 end
33 end
34
34
35 def test_date_format
35 def test_date_format
36 set_language_if_valid 'en'
36 set_language_if_valid 'en'
37 today = Date.today
37 today = Date.today
38 Setting.date_format = '%d %m %Y'
38 Setting.date_format = '%d %m %Y'
39 assert_equal today.strftime('%d %m %Y'), format_date(today)
39 assert_equal today.strftime('%d %m %Y'), format_date(today)
40 end
40 end
41
41
42 def test_date_and_time_for_each_language
42 def test_date_and_time_for_each_language
43 Setting.date_format = ''
43 Setting.date_format = ''
44 valid_languages.each do |lang|
44 valid_languages.each do |lang|
45 set_language_if_valid lang
45 set_language_if_valid lang
46 assert_nothing_raised "#{lang} failure" do
46 assert_nothing_raised "#{lang} failure" do
47 format_date(Date.today)
47 format_date(Date.today)
48 format_time(Time.now)
48 format_time(Time.now)
49 format_time(Time.now, false)
49 format_time(Time.now, false)
50 assert_not_equal 'default', ::I18n.l(Date.today, :format => :default),
50 assert_not_equal 'default', ::I18n.l(Date.today, :format => :default),
51 "date.formats.default missing in #{lang}"
51 "date.formats.default missing in #{lang}"
52 assert_not_equal 'time', ::I18n.l(Time.now, :format => :time),
52 assert_not_equal 'time', ::I18n.l(Time.now, :format => :time),
53 "time.formats.time missing in #{lang}"
53 "time.formats.time missing in #{lang}"
54 end
54 end
55 assert l('date.day_names').is_a?(Array)
55 assert l('date.day_names').is_a?(Array)
56 assert_equal 7, l('date.day_names').size
56 assert_equal 7, l('date.day_names').size
57
57
58 assert l('date.month_names').is_a?(Array)
58 assert l('date.month_names').is_a?(Array)
59 assert_equal 13, l('date.month_names').size
59 assert_equal 13, l('date.month_names').size
60 end
60 end
61 end
61 end
62
62
63 def test_time_format
63 def test_time_format
64 set_language_if_valid 'en'
64 set_language_if_valid 'en'
65 now = Time.parse('2011-02-20 15:45:22')
65 now = Time.parse('2011-02-20 15:45:22')
66 with_settings :time_format => '%H:%M' do
66 with_settings :time_format => '%H:%M' do
67 with_settings :date_format => '' do
67 with_settings :date_format => '' do
68 assert_equal '02/20/2011 15:45', format_time(now)
68 assert_equal '02/20/2011 15:45', format_time(now)
69 assert_equal '15:45', format_time(now, false)
69 assert_equal '15:45', format_time(now, false)
70 end
70 end
71 with_settings :date_format => '%Y-%m-%d' do
71 with_settings :date_format => '%Y-%m-%d' do
72 assert_equal '2011-02-20 15:45', format_time(now)
72 assert_equal '2011-02-20 15:45', format_time(now)
73 assert_equal '15:45', format_time(now, false)
73 assert_equal '15:45', format_time(now, false)
74 end
74 end
75 end
75 end
76 end
76 end
77
77
78 def test_time_format_default
78 def test_time_format_default
79 set_language_if_valid 'en'
79 set_language_if_valid 'en'
80 now = Time.parse('2011-02-20 15:45:22')
80 now = Time.parse('2011-02-20 15:45:22')
81 with_settings :time_format => '' do
81 with_settings :time_format => '' do
82 with_settings :date_format => '' do
82 with_settings :date_format => '' do
83 assert_equal '02/20/2011 03:45 pm', format_time(now)
83 assert_equal '02/20/2011 03:45 pm', format_time(now)
84 assert_equal '03:45 pm', format_time(now, false)
84 assert_equal '03:45 pm', format_time(now, false)
85 end
85 end
86 with_settings :date_format => '%Y-%m-%d' do
86 with_settings :date_format => '%Y-%m-%d' do
87 assert_equal '2011-02-20 03:45 pm', format_time(now)
87 assert_equal '2011-02-20 03:45 pm', format_time(now)
88 assert_equal '03:45 pm', format_time(now, false)
88 assert_equal '03:45 pm', format_time(now, false)
89 end
89 end
90 end
90 end
91 end
91 end
92
92
93 def test_time_format
93 def test_time_format
94 set_language_if_valid 'en'
94 set_language_if_valid 'en'
95 now = Time.now
95 now = Time.now
96 Setting.date_format = '%d %m %Y'
96 Setting.date_format = '%d %m %Y'
97 Setting.time_format = '%H %M'
97 Setting.time_format = '%H %M'
98 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now)
98 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now)
99 assert_equal now.strftime('%H %M'), format_time(now, false)
99 assert_equal now.strftime('%H %M'), format_time(now, false)
100 end
100 end
101
101
102 def test_utc_time_format
102 def test_utc_time_format
103 set_language_if_valid 'en'
103 set_language_if_valid 'en'
104 now = Time.now
104 now = Time.now
105 Setting.date_format = '%d %m %Y'
105 Setting.date_format = '%d %m %Y'
106 Setting.time_format = '%H %M'
106 Setting.time_format = '%H %M'
107 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc)
107 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc)
108 assert_equal now.strftime('%H %M'), format_time(now.utc, false)
108 assert_equal now.strftime('%H %M'), format_time(now.utc, false)
109 end
109 end
110
110
111 def test_number_to_human_size_for_each_language
111 def test_number_to_human_size_for_each_language
112 valid_languages.each do |lang|
112 valid_languages.each do |lang|
113 set_language_if_valid lang
113 set_language_if_valid lang
114 assert_nothing_raised "#{lang} failure" do
114 assert_nothing_raised "#{lang} failure" do
115 number_to_human_size(1024*1024*4)
115 number_to_human_size(1024*1024*4)
116 end
116 end
117 end
117 end
118 end
118 end
119
119
120 def test_valid_languages
120 def test_valid_languages
121 assert valid_languages.is_a?(Array)
121 assert valid_languages.is_a?(Array)
122 assert valid_languages.first.is_a?(Symbol)
122 assert valid_languages.first.is_a?(Symbol)
123 end
123 end
124
124
125 def test_locales_validness
126 lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
127 assert_equal lang_files_count, valid_languages.size
128 valid_languages.each do |lang|
129 assert set_language_if_valid(lang)
130 end
131 set_language_if_valid('en')
132 end
133
125 def test_valid_language
134 def test_valid_language
126 to_test = {'fr' => :fr,
135 to_test = {'fr' => :fr,
127 'Fr' => :fr,
136 'Fr' => :fr,
128 'zh' => :zh,
137 'zh' => :zh,
129 'zh-tw' => :"zh-TW",
138 'zh-tw' => :"zh-TW",
130 'zh-TW' => :"zh-TW",
139 'zh-TW' => :"zh-TW",
131 'zh-ZZ' => nil }
140 'zh-ZZ' => nil }
132 to_test.each {|lang, expected| assert_equal expected, find_language(lang)}
141 to_test.each {|lang, expected| assert_equal expected, find_language(lang)}
133 end
142 end
134
143
135 def test_fallback
144 def test_fallback
136 ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"})
145 ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"})
137 ::I18n.locale = 'en'
146 ::I18n.locale = 'en'
138 assert_equal "Untranslated string", l(:untranslated)
147 assert_equal "Untranslated string", l(:untranslated)
139 ::I18n.locale = 'fr'
148 ::I18n.locale = 'fr'
140 assert_equal "Untranslated string", l(:untranslated)
149 assert_equal "Untranslated string", l(:untranslated)
141
150
142 ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"})
151 ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"})
143 ::I18n.locale = 'en'
152 ::I18n.locale = 'en'
144 assert_equal "Untranslated string", l(:untranslated)
153 assert_equal "Untranslated string", l(:untranslated)
145 ::I18n.locale = 'fr'
154 ::I18n.locale = 'fr'
146 assert_equal "Pas de traduction", l(:untranslated)
155 assert_equal "Pas de traduction", l(:untranslated)
147 end
156 end
148
157
149 def test_utf8
158 def test_utf8
150 set_language_if_valid 'ja'
159 set_language_if_valid 'ja'
151 str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84"
160 str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84"
152 i18n_ja_yes = l(:general_text_Yes)
161 i18n_ja_yes = l(:general_text_Yes)
153 if str_ja_yes.respond_to?(:force_encoding)
162 if str_ja_yes.respond_to?(:force_encoding)
154 str_ja_yes.force_encoding('UTF-8')
163 str_ja_yes.force_encoding('UTF-8')
155 assert_equal "UTF-8", i18n_ja_yes.encoding.to_s
164 assert_equal "UTF-8", i18n_ja_yes.encoding.to_s
156 end
165 end
157 assert_equal str_ja_yes, i18n_ja_yes
166 assert_equal str_ja_yes, i18n_ja_yes
158 end
167 end
159 end
168 end
General Comments 0
You need to be logged in to leave comments. Login now