annotate .svn/pristine/15/1501976e33da8b363bb737aa1d51d7b528152400.svn-base @ 1464:261b3d9a4903 redmine-2.4

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