annotate .svn/pristine/86/86b9bcb879f7b1ffb317c5c416700af50bfd5f4c.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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