annotate test/unit/lib/redmine/i18n_test.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents fb9a13467253
children
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1464 108 assert_equal '02/20/2011 03:45 PM', format_time(now)
Chris@1464 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@1464 112 assert_equal '2011-02-20 03:45 PM', format_time(now)
Chris@1464 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_utc_time_format
Chris@0 135 set_language_if_valid 'en'
Chris@441 136 now = Time.now
Chris@0 137 Setting.date_format = '%d %m %Y'
Chris@0 138 Setting.time_format = '%H %M'
Chris@441 139 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc)
Chris@441 140 assert_equal now.strftime('%H %M'), format_time(now.utc, false)
Chris@0 141 end
Chris@441 142
Chris@0 143 def test_number_to_human_size_for_each_language
Chris@0 144 valid_languages.each do |lang|
Chris@0 145 set_language_if_valid lang
Chris@0 146 assert_nothing_raised "#{lang} failure" do
Chris@1115 147 size = number_to_human_size(257024)
Chris@1115 148 assert_match /251/, size
Chris@0 149 end
Chris@0 150 end
Chris@0 151 end
Chris@441 152
Chris@1115 153 def test_day_name
Chris@1115 154 set_language_if_valid 'fr'
Chris@1115 155 assert_equal 'dimanche', day_name(0)
Chris@1115 156 assert_equal 'jeudi', day_name(4)
Chris@1115 157 end
Chris@1115 158
Chris@1115 159 def test_day_letter
Chris@1115 160 set_language_if_valid 'fr'
Chris@1115 161 assert_equal 'd', day_letter(0)
Chris@1115 162 assert_equal 'j', day_letter(4)
Chris@1115 163 end
Chris@1115 164
Chris@1115 165 def test_number_to_currency_for_each_language
Chris@1115 166 valid_languages.each do |lang|
Chris@1115 167 set_language_if_valid lang
Chris@1115 168 assert_nothing_raised "#{lang} failure" do
Chris@1115 169 number_to_currency(-1000.2)
Chris@1115 170 end
Chris@1115 171 end
Chris@1115 172 end
Chris@1115 173
Chris@1115 174 def test_number_to_currency_default
Chris@1115 175 set_language_if_valid 'bs'
Chris@1115 176 assert_equal "KM -1000,20", number_to_currency(-1000.2)
Chris@1115 177 set_language_if_valid 'de'
Chris@1115 178 euro_sign = "\xe2\x82\xac"
Chris@1115 179 euro_sign.force_encoding('UTF-8') if euro_sign.respond_to?(:force_encoding)
Chris@1115 180 assert_equal "-1000,20 #{euro_sign}", number_to_currency(-1000.2)
Chris@1115 181 end
Chris@1115 182
Chris@0 183 def test_valid_languages
Chris@0 184 assert valid_languages.is_a?(Array)
Chris@0 185 assert valid_languages.first.is_a?(Symbol)
Chris@0 186 end
Chris@441 187
Chris@1115 188 def test_languages_options
Chris@1115 189 options = languages_options
Chris@1115 190 assert options.is_a?(Array)
Chris@1115 191 assert_equal valid_languages.size, options.size
Chris@1115 192 assert_nil options.detect {|option| !option.is_a?(Array)}
Chris@1115 193 assert_nil options.detect {|option| option.size != 2}
Chris@1115 194 assert_nil options.detect {|option| !option.first.is_a?(String) || !option.last.is_a?(String)}
Chris@1115 195 assert_include ["English", "en"], options
Chris@1464 196 ja = "Japanese (\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e)"
Chris@1464 197 ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
Chris@1464 198 assert_include [ja, "ja"], options
Chris@1115 199 end
Chris@1115 200
Chris@1115 201 def test_locales_validness
Chris@1115 202 lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
Chris@1115 203 assert_equal lang_files_count, valid_languages.size
Chris@1115 204 valid_languages.each do |lang|
Chris@1115 205 assert set_language_if_valid(lang)
Chris@1115 206 end
Chris@1115 207 set_language_if_valid('en')
Chris@1115 208 end
Chris@1115 209
Chris@0 210 def test_valid_language
Chris@0 211 to_test = {'fr' => :fr,
Chris@0 212 'Fr' => :fr,
Chris@0 213 'zh' => :zh,
Chris@0 214 'zh-tw' => :"zh-TW",
Chris@0 215 'zh-TW' => :"zh-TW",
Chris@0 216 'zh-ZZ' => nil }
Chris@0 217 to_test.each {|lang, expected| assert_equal expected, find_language(lang)}
Chris@0 218 end
Chris@441 219
Chris@119 220 def test_fallback
Chris@119 221 ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"})
Chris@119 222 ::I18n.locale = 'en'
Chris@119 223 assert_equal "Untranslated string", l(:untranslated)
Chris@119 224 ::I18n.locale = 'fr'
Chris@119 225 assert_equal "Untranslated string", l(:untranslated)
Chris@441 226
Chris@119 227 ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"})
Chris@119 228 ::I18n.locale = 'en'
Chris@119 229 assert_equal "Untranslated string", l(:untranslated)
Chris@119 230 ::I18n.locale = 'fr'
Chris@119 231 assert_equal "Pas de traduction", l(:untranslated)
Chris@119 232 end
Chris@441 233
Chris@441 234 def test_utf8
Chris@441 235 set_language_if_valid 'ja'
Chris@441 236 str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84"
Chris@441 237 i18n_ja_yes = l(:general_text_Yes)
Chris@441 238 if str_ja_yes.respond_to?(:force_encoding)
Chris@441 239 str_ja_yes.force_encoding('UTF-8')
Chris@441 240 assert_equal "UTF-8", i18n_ja_yes.encoding.to_s
Chris@441 241 end
Chris@441 242 assert_equal str_ja_yes, i18n_ja_yes
Chris@441 243 end
Chris@1115 244
Chris@1115 245 def test_traditional_chinese_locale
Chris@1115 246 set_language_if_valid 'zh-TW'
Chris@1115 247 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
Chris@1115 248 if str_tw.respond_to?(:force_encoding)
Chris@1115 249 str_tw.force_encoding('UTF-8')
Chris@1115 250 end
Chris@1115 251 assert_equal str_tw, l(:general_lang_name)
Chris@1115 252 end
Chris@1115 253
Chris@1115 254 def test_french_locale
Chris@1115 255 set_language_if_valid 'fr'
Chris@1115 256 str_fr = "Fran\xc3\xa7ais"
Chris@1115 257 if str_fr.respond_to?(:force_encoding)
Chris@1115 258 str_fr.force_encoding('UTF-8')
Chris@1115 259 end
Chris@1115 260 assert_equal str_fr, l(:general_lang_name)
Chris@1115 261 end
Chris@0 262 end