annotate test/unit/lib/redmine/i18n_test.rb @ 1465:ab8bd24eeb65 bug_635

Close obsolete branch bug_635
author Chris Cannam
date Fri, 19 Jul 2013 12:13:20 +0100
parents bb32da3bea34
children 4f746d8966dd fb9a13467253
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@441 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@441 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@119 18 require File.expand_path('../../../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class Redmine::I18nTest < ActiveSupport::TestCase
Chris@0 21 include Redmine::I18n
Chris@0 22 include ActionView::Helpers::NumberHelper
Chris@441 23
Chris@0 24 def setup
Chris@1115 25 User.current.language = nil
Chris@1115 26 end
Chris@1115 27
Chris@1115 28 def teardown
Chris@1115 29 set_language_if_valid 'en'
Chris@0 30 end
Chris@441 31
Chris@0 32 def test_date_format_default
Chris@0 33 set_language_if_valid 'en'
Chris@0 34 today = Date.today
Chris@441 35 Setting.date_format = ''
Chris@119 36 assert_equal I18n.l(today), format_date(today)
Chris@0 37 end
Chris@441 38
Chris@0 39 def test_date_format
Chris@0 40 set_language_if_valid 'en'
Chris@0 41 today = Date.today
Chris@0 42 Setting.date_format = '%d %m %Y'
Chris@0 43 assert_equal today.strftime('%d %m %Y'), format_date(today)
Chris@0 44 end
Chris@441 45
Chris@1115 46 def test_date_format_default_with_user_locale
Chris@1115 47 set_language_if_valid 'es'
Chris@1115 48 today = now = Time.parse('2011-02-20 14:00:00')
Chris@1115 49 Setting.date_format = '%d %B %Y'
Chris@1115 50 User.current.language = 'fr'
Chris@1115 51 s1 = "20 f\xc3\xa9vrier 2011"
Chris@1115 52 s1.force_encoding("UTF-8") if s1.respond_to?(:force_encoding)
Chris@1115 53 assert_equal s1, format_date(today)
Chris@1115 54 User.current.language = nil
Chris@1115 55 assert_equal '20 Febrero 2011', format_date(today)
Chris@1115 56 end
Chris@1115 57
Chris@0 58 def test_date_and_time_for_each_language
Chris@0 59 Setting.date_format = ''
Chris@0 60 valid_languages.each do |lang|
Chris@0 61 set_language_if_valid lang
Chris@0 62 assert_nothing_raised "#{lang} failure" do
Chris@0 63 format_date(Date.today)
Chris@0 64 format_time(Time.now)
Chris@0 65 format_time(Time.now, false)
Chris@441 66 assert_not_equal 'default', ::I18n.l(Date.today, :format => :default),
Chris@441 67 "date.formats.default missing in #{lang}"
Chris@441 68 assert_not_equal 'time', ::I18n.l(Time.now, :format => :time),
Chris@441 69 "time.formats.time missing in #{lang}"
Chris@0 70 end
Chris@0 71 assert l('date.day_names').is_a?(Array)
Chris@0 72 assert_equal 7, l('date.day_names').size
Chris@441 73
Chris@0 74 assert l('date.month_names').is_a?(Array)
Chris@0 75 assert_equal 13, l('date.month_names').size
Chris@0 76 end
Chris@0 77 end
Chris@441 78
Chris@1115 79 def test_time_for_each_zone
Chris@1115 80 ActiveSupport::TimeZone.all.each do |zone|
Chris@1115 81 User.current.stubs(:time_zone).returns(zone.name)
Chris@1115 82 assert_nothing_raised "#{zone} failure" do
Chris@1115 83 format_time(Time.now)
Chris@1115 84 end
Chris@1115 85 end
Chris@1115 86 end
Chris@1115 87
Chris@245 88 def test_time_format
Chris@245 89 set_language_if_valid 'en'
Chris@245 90 now = Time.parse('2011-02-20 15:45:22')
Chris@245 91 with_settings :time_format => '%H:%M' do
Chris@245 92 with_settings :date_format => '' do
Chris@245 93 assert_equal '02/20/2011 15:45', format_time(now)
Chris@245 94 assert_equal '15:45', format_time(now, false)
Chris@245 95 end
Chris@245 96 with_settings :date_format => '%Y-%m-%d' do
Chris@245 97 assert_equal '2011-02-20 15:45', format_time(now)
Chris@245 98 assert_equal '15:45', format_time(now, false)
Chris@245 99 end
Chris@245 100 end
Chris@245 101 end
Chris@441 102
Chris@0 103 def test_time_format_default
Chris@0 104 set_language_if_valid 'en'
Chris@245 105 now = Time.parse('2011-02-20 15:45:22')
Chris@245 106 with_settings :time_format => '' do
Chris@245 107 with_settings :date_format => '' do
Chris@245 108 assert_equal '02/20/2011 03:45 pm', format_time(now)
Chris@245 109 assert_equal '03:45 pm', format_time(now, false)
Chris@245 110 end
Chris@245 111 with_settings :date_format => '%Y-%m-%d' do
Chris@245 112 assert_equal '2011-02-20 03:45 pm', format_time(now)
Chris@245 113 assert_equal '03:45 pm', format_time(now, false)
Chris@245 114 end
Chris@245 115 end
Chris@0 116 end
Chris@441 117
Chris@1115 118 def test_time_format_default_with_user_locale
Chris@1115 119 set_language_if_valid 'en'
Chris@1115 120 User.current.language = 'fr'
Chris@1115 121 now = Time.parse('2011-02-20 15:45:22')
Chris@1115 122 with_settings :time_format => '' do
Chris@1115 123 with_settings :date_format => '' do
Chris@1115 124 assert_equal '20/02/2011 15:45', format_time(now)
Chris@1115 125 assert_equal '15:45', format_time(now, false)
Chris@1115 126 end
Chris@1115 127 with_settings :date_format => '%Y-%m-%d' do
Chris@1115 128 assert_equal '2011-02-20 15:45', format_time(now)
Chris@1115 129 assert_equal '15:45', format_time(now, false)
Chris@1115 130 end
Chris@1115 131 end
Chris@1115 132 end
Chris@1115 133
Chris@0 134 def test_time_format
Chris@0 135 set_language_if_valid 'en'
Chris@0 136 now = Time.now
Chris@0 137 Setting.date_format = '%d %m %Y'
Chris@0 138 Setting.time_format = '%H %M'
Chris@0 139 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now)
Chris@0 140 assert_equal now.strftime('%H %M'), format_time(now, false)
Chris@0 141 end
Chris@441 142
Chris@0 143 def test_utc_time_format
Chris@0 144 set_language_if_valid 'en'
Chris@441 145 now = Time.now
Chris@0 146 Setting.date_format = '%d %m %Y'
Chris@0 147 Setting.time_format = '%H %M'
Chris@441 148 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc)
Chris@441 149 assert_equal now.strftime('%H %M'), format_time(now.utc, false)
Chris@0 150 end
Chris@441 151
Chris@0 152 def test_number_to_human_size_for_each_language
Chris@0 153 valid_languages.each do |lang|
Chris@0 154 set_language_if_valid lang
Chris@0 155 assert_nothing_raised "#{lang} failure" do
Chris@1115 156 size = number_to_human_size(257024)
Chris@1115 157 assert_match /251/, size
Chris@0 158 end
Chris@0 159 end
Chris@0 160 end
Chris@441 161
Chris@1115 162 def test_day_name
Chris@1115 163 set_language_if_valid 'fr'
Chris@1115 164 assert_equal 'dimanche', day_name(0)
Chris@1115 165 assert_equal 'jeudi', day_name(4)
Chris@1115 166 end
Chris@1115 167
Chris@1115 168 def test_day_letter
Chris@1115 169 set_language_if_valid 'fr'
Chris@1115 170 assert_equal 'd', day_letter(0)
Chris@1115 171 assert_equal 'j', day_letter(4)
Chris@1115 172 end
Chris@1115 173
Chris@1115 174 def test_number_to_currency_for_each_language
Chris@1115 175 valid_languages.each do |lang|
Chris@1115 176 set_language_if_valid lang
Chris@1115 177 assert_nothing_raised "#{lang} failure" do
Chris@1115 178 number_to_currency(-1000.2)
Chris@1115 179 end
Chris@1115 180 end
Chris@1115 181 end
Chris@1115 182
Chris@1115 183 def test_number_to_currency_default
Chris@1115 184 set_language_if_valid 'bs'
Chris@1115 185 assert_equal "KM -1000,20", number_to_currency(-1000.2)
Chris@1115 186 set_language_if_valid 'de'
Chris@1115 187 euro_sign = "\xe2\x82\xac"
Chris@1115 188 euro_sign.force_encoding('UTF-8') if euro_sign.respond_to?(:force_encoding)
Chris@1115 189 assert_equal "-1000,20 #{euro_sign}", number_to_currency(-1000.2)
Chris@1115 190 end
Chris@1115 191
Chris@0 192 def test_valid_languages
Chris@0 193 assert valid_languages.is_a?(Array)
Chris@0 194 assert valid_languages.first.is_a?(Symbol)
Chris@0 195 end
Chris@441 196
Chris@1115 197 def test_languages_options
Chris@1115 198 options = languages_options
Chris@1115 199
Chris@1115 200 assert options.is_a?(Array)
Chris@1115 201 assert_equal valid_languages.size, options.size
Chris@1115 202 assert_nil options.detect {|option| !option.is_a?(Array)}
Chris@1115 203 assert_nil options.detect {|option| option.size != 2}
Chris@1115 204 assert_nil options.detect {|option| !option.first.is_a?(String) || !option.last.is_a?(String)}
Chris@1115 205 assert_include ["English", "en"], options
Chris@1115 206 end
Chris@1115 207
Chris@1115 208 def test_locales_validness
Chris@1115 209 lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
Chris@1115 210 assert_equal lang_files_count, valid_languages.size
Chris@1115 211 valid_languages.each do |lang|
Chris@1115 212 assert set_language_if_valid(lang)
Chris@1115 213 end
Chris@1115 214 set_language_if_valid('en')
Chris@1115 215 end
Chris@1115 216
Chris@0 217 def test_valid_language
Chris@0 218 to_test = {'fr' => :fr,
Chris@0 219 'Fr' => :fr,
Chris@0 220 'zh' => :zh,
Chris@0 221 'zh-tw' => :"zh-TW",
Chris@0 222 'zh-TW' => :"zh-TW",
Chris@0 223 'zh-ZZ' => nil }
Chris@0 224 to_test.each {|lang, expected| assert_equal expected, find_language(lang)}
Chris@0 225 end
Chris@441 226
Chris@119 227 def test_fallback
Chris@119 228 ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"})
Chris@119 229 ::I18n.locale = 'en'
Chris@119 230 assert_equal "Untranslated string", l(:untranslated)
Chris@119 231 ::I18n.locale = 'fr'
Chris@119 232 assert_equal "Untranslated string", l(:untranslated)
Chris@441 233
Chris@119 234 ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"})
Chris@119 235 ::I18n.locale = 'en'
Chris@119 236 assert_equal "Untranslated string", l(:untranslated)
Chris@119 237 ::I18n.locale = 'fr'
Chris@119 238 assert_equal "Pas de traduction", l(:untranslated)
Chris@119 239 end
Chris@441 240
Chris@441 241 def test_utf8
Chris@441 242 set_language_if_valid 'ja'
Chris@441 243 str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84"
Chris@441 244 i18n_ja_yes = l(:general_text_Yes)
Chris@441 245 if str_ja_yes.respond_to?(:force_encoding)
Chris@441 246 str_ja_yes.force_encoding('UTF-8')
Chris@441 247 assert_equal "UTF-8", i18n_ja_yes.encoding.to_s
Chris@441 248 end
Chris@441 249 assert_equal str_ja_yes, i18n_ja_yes
Chris@441 250 end
Chris@1115 251
Chris@1115 252 def test_traditional_chinese_locale
Chris@1115 253 set_language_if_valid 'zh-TW'
Chris@1115 254 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
Chris@1115 255 if str_tw.respond_to?(:force_encoding)
Chris@1115 256 str_tw.force_encoding('UTF-8')
Chris@1115 257 end
Chris@1115 258 assert_equal str_tw, l(:general_lang_name)
Chris@1115 259 end
Chris@1115 260
Chris@1115 261 def test_french_locale
Chris@1115 262 set_language_if_valid 'fr'
Chris@1115 263 str_fr = "Fran\xc3\xa7ais"
Chris@1115 264 if str_fr.respond_to?(:force_encoding)
Chris@1115 265 str_fr.force_encoding('UTF-8')
Chris@1115 266 end
Chris@1115 267 assert_equal str_fr, l(:general_lang_name)
Chris@1115 268 end
Chris@0 269 end