annotate .svn/pristine/3d/3d911a7995f6f6d40cec83d7a109bb146cc550bd.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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