From 57d1382013c85a7b11ac8ce5e683f6006b12b328 Mon Sep 17 00:00:00 2001 From: hukl Date: Thu, 14 Jan 2010 22:21:43 +0100 Subject: Updated globalize2 to latest version. Modified existing code accoringly --- .../test/active_record/fallbacks_test.rb | 102 +++++ .../test/active_record/migration_test.rb | 118 +++++ .../test/active_record/sti_translated_test.rb | 49 +++ .../test/active_record/translates_test.rb | 87 ++++ .../test/active_record/translation_class_test.rb | 30 ++ .../test/active_record/validation_tests.rb | 75 ++++ .../plugins/globalize2/test/active_record_test.rb | 442 +++++++++++++++++++ .../globalize2/test/backends/chained_test.rb | 175 -------- .../globalize2/test/backends/pluralizing_test.rb | 63 --- .../globalize2/test/backends/static_test.rb | 143 ------ vendor/plugins/globalize2/test/data/locale/all.yml | 2 - .../plugins/globalize2/test/data/locale/de-DE.yml | 2 - .../plugins/globalize2/test/data/locale/en-US.yml | 2 - .../globalize2/test/data/locale/en-US/module.yml | 2 - .../globalize2/test/data/locale/fi-FI/module.yml | 2 - .../plugins/globalize2/test/data/locale/root.yml | 0 vendor/plugins/globalize2/test/data/models.rb | 26 +- .../test/i18n/missing_translations_test.rb | 12 +- vendor/plugins/globalize2/test/load_path_test.rb | 49 --- .../globalize2/test/locale/fallbacks_test.rb | 154 ------- .../globalize2/test/locale/language_tag_test.rb | 130 ------ .../test/model/active_record/migration_test.rb | 125 ------ .../model/active_record/sti_translated_test.rb | 75 ---- .../test/model/active_record/translated_test.rb | 478 --------------------- vendor/plugins/globalize2/test/test_helper.rb | 70 ++- vendor/plugins/globalize2/test/translation_test.rb | 54 --- 26 files changed, 988 insertions(+), 1479 deletions(-) create mode 100644 vendor/plugins/globalize2/test/active_record/fallbacks_test.rb create mode 100644 vendor/plugins/globalize2/test/active_record/migration_test.rb create mode 100644 vendor/plugins/globalize2/test/active_record/sti_translated_test.rb create mode 100644 vendor/plugins/globalize2/test/active_record/translates_test.rb create mode 100644 vendor/plugins/globalize2/test/active_record/translation_class_test.rb create mode 100644 vendor/plugins/globalize2/test/active_record/validation_tests.rb create mode 100644 vendor/plugins/globalize2/test/active_record_test.rb delete mode 100644 vendor/plugins/globalize2/test/backends/chained_test.rb delete mode 100644 vendor/plugins/globalize2/test/backends/pluralizing_test.rb delete mode 100644 vendor/plugins/globalize2/test/backends/static_test.rb delete mode 100644 vendor/plugins/globalize2/test/data/locale/all.yml delete mode 100644 vendor/plugins/globalize2/test/data/locale/de-DE.yml delete mode 100644 vendor/plugins/globalize2/test/data/locale/en-US.yml delete mode 100644 vendor/plugins/globalize2/test/data/locale/en-US/module.yml delete mode 100644 vendor/plugins/globalize2/test/data/locale/fi-FI/module.yml delete mode 100644 vendor/plugins/globalize2/test/data/locale/root.yml delete mode 100644 vendor/plugins/globalize2/test/load_path_test.rb delete mode 100644 vendor/plugins/globalize2/test/locale/fallbacks_test.rb delete mode 100644 vendor/plugins/globalize2/test/locale/language_tag_test.rb delete mode 100644 vendor/plugins/globalize2/test/model/active_record/migration_test.rb delete mode 100644 vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb delete mode 100644 vendor/plugins/globalize2/test/model/active_record/translated_test.rb delete mode 100644 vendor/plugins/globalize2/test/translation_test.rb (limited to 'vendor/plugins/globalize2/test') diff --git a/vendor/plugins/globalize2/test/active_record/fallbacks_test.rb b/vendor/plugins/globalize2/test/active_record/fallbacks_test.rb new file mode 100644 index 00000000..449ec8b2 --- /dev/null +++ b/vendor/plugins/globalize2/test/active_record/fallbacks_test.rb @@ -0,0 +1,102 @@ +require File.expand_path(File.dirname(__FILE__) + '/../test_helper') +require File.expand_path(File.dirname(__FILE__) + '/../data/models') + +if I18n.respond_to?(:fallbacks) + class TranslatedTest < ActiveSupport::TestCase + def setup + I18n.locale = :'en-US' + I18n.fallbacks.clear + reset_db! + ActiveRecord::Base.locale = nil + end + + def teardown + I18n.fallbacks.clear + end + + test "keeping one field in new locale when other field is changed" do + I18n.fallbacks.map 'de-DE' => [ 'en-US' ] + post = Post.create :subject => 'foo' + I18n.locale = 'de-DE' + post.content = 'bar' + assert_equal 'foo', post.subject + end + + test "modifying non-required field in a new locale" do + I18n.fallbacks.map 'de-DE' => [ 'en-US' ] + post = Post.create :subject => 'foo' + I18n.locale = 'de-DE' + post.content = 'bar' + assert post.save + end + + test "resolves a simple fallback" do + I18n.locale = 'de-DE' + post = Post.create :subject => 'foo' + I18n.locale = 'de' + post.subject = 'baz' + post.content = 'bar' + post.save + I18n.locale = 'de-DE' + assert_equal 'foo', post.subject + assert_equal 'bar', post.content + end + + test "resolves a simple fallback without reloading" do + I18n.locale = 'de-DE' + post = Post.new :subject => 'foo' + I18n.locale = 'de' + post.subject = 'baz' + post.content = 'bar' + I18n.locale = 'de-DE' + assert_equal 'foo', post.subject + assert_equal 'bar', post.content + end + + test "resolves a complex fallback without reloading" do + I18n.fallbacks.map 'de' => %w(en he) + I18n.locale = 'de' + post = Post.new + I18n.locale = 'en' + post.subject = 'foo' + I18n.locale = 'he' + post.subject = 'baz' + post.content = 'bar' + I18n.locale = 'de' + assert_equal 'foo', post.subject + assert_equal 'bar', post.content + end + + test 'fallbacks with lots of locale switching' do + I18n.fallbacks.map :'de-DE' => [ :'en-US' ] + post = Post.create :subject => 'foo' + + I18n.locale = :'de-DE' + assert_equal 'foo', post.subject + + I18n.locale = :'en-US' + post.update_attribute :subject, 'bar' + + I18n.locale = :'de-DE' + assert_equal 'bar', post.subject + end + + test 'fallbacks with lots of locale switching' do + I18n.fallbacks.map :'de-DE' => [ :'en-US' ] + child = Child.create :content => 'foo' + + I18n.locale = :'de-DE' + assert_equal 'foo', child.content + + I18n.locale = :'en-US' + child.update_attribute :content, 'bar' + + I18n.locale = :'de-DE' + assert_equal 'bar', child.content + end + end +end + +# TODO should validate_presence_of take fallbacks into account? maybe we need +# an extra validation call, or more options for validate_presence_of. + diff --git a/vendor/plugins/globalize2/test/active_record/migration_test.rb b/vendor/plugins/globalize2/test/active_record/migration_test.rb new file mode 100644 index 00000000..359d811f --- /dev/null +++ b/vendor/plugins/globalize2/test/active_record/migration_test.rb @@ -0,0 +1,118 @@ +require File.expand_path(File.dirname(__FILE__) + '/../test_helper') +require File.expand_path(File.dirname(__FILE__) + '/../data/models') + +class MigrationTest < ActiveSupport::TestCase + def setup + reset_db! + Post.drop_translation_table! + end + + test 'globalize table added' do + assert !Post.connection.table_exists?(:post_translations) + assert !Post.connection.index_exists?(:post_translations, :post_id) + + Post.create_translation_table!(:subject => :string, :content => :text) + assert Post.connection.table_exists?(:post_translations) + assert Post.connection.index_exists?(:post_translations, :post_id) + + columns = Post.connection.columns(:post_translations) + assert locale = columns.detect { |c| c.name == 'locale' } + assert_equal :string, locale.type + assert subject = columns.detect { |c| c.name == 'subject' } + assert_equal :string, subject.type + assert content = columns.detect { |c| c.name == 'content' } + assert_equal :text, content.type + assert post_id = columns.detect { |c| c.name == 'post_id' } + assert_equal :integer, post_id.type + assert created_at = columns.detect { |c| c.name == 'created_at' } + assert_equal :datetime, created_at.type + assert updated_at = columns.detect { |c| c.name == 'updated_at' } + assert_equal :datetime, updated_at.type + end + + test 'globalize table dropped' do + assert !Post.connection.table_exists?( :post_translations ) + assert !Post.connection.index_exists?( :post_translations, :post_id ) + Post.create_translation_table! :subject => :string, :content => :text + assert Post.connection.table_exists?( :post_translations ) + assert Post.connection.index_exists?( :post_translations, :post_id ) + Post.drop_translation_table! + assert !Post.connection.table_exists?( :post_translations ) + assert !Post.connection.index_exists?( :post_translations, :post_id ) + end + + test 'exception on missing field inputs' do + assert_raise Globalize::MigrationMissingTranslatedField do + Post.create_translation_table! :content => :text + end + end + + test 'exception on bad input type' do + assert_raise Globalize::BadMigrationFieldType do + Post.create_translation_table! :subject => :string, :content => :integer + end + end + + test "exception on bad input type isn't raised for untranslated fields" do + assert_nothing_raised do + Post.create_translation_table! :subject => :string, :content => :string, :views_count => :integer + end + end + + test 'create_translation_table! should not be called on non-translated models' do + assert_raise NoMethodError do + Blog.create_translation_table! :name => :string + end + end + + test 'drop_translation_table! should not be called on non-translated models' do + assert_raise NoMethodError do + Blog.drop_translation_table! + end + end + + test "translation_index_name returns a readable index name when it's not longer than 50 characters" do + assert_equal 'index_post_translations_on_post_id', Post.send(:translation_index_name) + end + + test "translation_index_name returns a hashed index name when it's longer than 50 characters" do + class UltraLongModelNameWithoutProper < ActiveRecord::Base + translates :foo + end + name = UltraLongModelNameWithoutProper.send(:translation_index_name) + assert_match /^index_[a-z0-9]{40}$/, name + end + + test 'globalize table added when table has long name' do + UltraLongModelNameWithoutProper.create_translation_table!( + :subject => :string, :content => :text + ) + + assert UltraLongModelNameWithoutProper.connection.table_exists?( + :ultra_long_model_name_without_proper_translations + ) + assert UltraLongModelNameWithoutProper.connection.index_exists?( + :ultra_long_model_name_without_proper_translations, + :name => UltraLongModelNameWithoutProper.send( + :translation_index_name + ) + ) + end + + test 'globalize table dropped when table has long name' do + UltraLongModelNameWithoutProper.drop_translation_table! + UltraLongModelNameWithoutProper.create_translation_table!( + :subject => :string, :content => :text + ) + UltraLongModelNameWithoutProper.drop_translation_table! + + assert !UltraLongModelNameWithoutProper.connection.table_exists?( + :ultra_long_model_name_without_proper_translations + ) + assert !UltraLongModelNameWithoutProper.connection.index_exists?( + :ultra_long_model_name_without_proper_translations, + :ultra_long_model_name_without_proper_id + ) + end + +end diff --git a/vendor/plugins/globalize2/test/active_record/sti_translated_test.rb b/vendor/plugins/globalize2/test/active_record/sti_translated_test.rb new file mode 100644 index 00000000..f529b8d6 --- /dev/null +++ b/vendor/plugins/globalize2/test/active_record/sti_translated_test.rb @@ -0,0 +1,49 @@ +require File.expand_path(File.dirname(__FILE__) + '/../test_helper') +require File.expand_path(File.dirname(__FILE__) + '/../data/models') + +class StiTranslatedTest < ActiveSupport::TestCase + def setup + I18n.locale = :'en-US' + reset_db! + end + + test "works with simple dynamic finders" do + foo = Child.create :content => 'foo' + Child.create :content => 'bar' + child = Child.find_by_content('foo') + assert_equal foo, child + end + + test 'change attribute on globalized model' do + child = Child.create :content => 'foo' + assert_equal [], child.changed + child.content = 'bar' + assert_equal [ 'content' ], child.changed + child.content = 'baz' + assert_member 'content', child.changed + end + + test 'change attribute on globalized model after locale switching' do + child = Child.create :content => 'foo' + assert_equal [], child.changed + child.content = 'bar' + I18n.locale = :de + assert_equal [ 'content' ], child.changed + end + + test "saves all locales, even after locale switching" do + child = Child.new :content => 'foo' + I18n.locale = 'de-DE' + child.content = 'bar' + I18n.locale = 'he-IL' + child.content = 'baz' + child.save + I18n.locale = 'en-US' + child = Child.first + assert_equal 'foo', child.content + I18n.locale = 'de-DE' + assert_equal 'bar', child.content + I18n.locale = 'he-IL' + assert_equal 'baz', child.content + end +end diff --git a/vendor/plugins/globalize2/test/active_record/translates_test.rb b/vendor/plugins/globalize2/test/active_record/translates_test.rb new file mode 100644 index 00000000..4207bc45 --- /dev/null +++ b/vendor/plugins/globalize2/test/active_record/translates_test.rb @@ -0,0 +1,87 @@ +require File.expand_path(File.dirname(__FILE__) + '/../test_helper') +require File.expand_path(File.dirname(__FILE__) + '/../data/models') + +class TranslatesTest < ActiveSupport::TestCase + def setup + I18n.locale = nil + ActiveRecord::Base.locale = nil + reset_db! + end + + test 'defines a :locale accessors on ActiveRecord::Base' do + ActiveRecord::Base.locale = :de + assert_equal :de, ActiveRecord::Base.locale + end + + test 'the :locale reader on ActiveRecord::Base does not default to I18n.locale (anymore)' do + I18n.locale = :en + assert_nil ActiveRecord::Base.locale + end + + test 'ActiveRecord::Base.with_locale temporarily sets the given locale and yields the block' do + I18n.locale = :en + post = Post.with_locale(:de) do + Post.create!(:subject => 'Titel', :content => 'Inhalt') + end + assert_nil Post.locale + assert_equal :en, I18n.locale + + I18n.locale = :de + assert_equal 'Titel', post.subject + end + + test 'translation_class returns the Translation class' do + assert_equal Post::Translation, Post.translation_class + end + + test 'defines a has_many association on the model class' do + assert_has_many Post, :translations + end + + test 'defines a scope for retrieving locales that have complete translations' do + post = Post.create!(:subject => 'subject', :content => 'content') + assert_equal [:en], post.translated_locales + end + + test 'sets the given attributes to translated_attribute_names' do + assert_equal [:subject, :content], Post.translated_attribute_names + end + + test 'defines accessors for the translated attributes' do + post = Post.new + assert post.respond_to?(:subject) + assert post.respond_to?(:subject=) + end + + test 'attribute reader without arguments will use the current locale on ActiveRecord::Base or I18n' do + post = Post.with_locale(:de) do + Post.create!(:subject => 'Titel', :content => 'Inhalt') + end + I18n.locale = :de + assert_equal 'Titel', post.subject + + I18n.locale = :en + ActiveRecord::Base.locale = :de + assert_equal 'Titel', post.subject + end + + test 'attribute reader when passed a locale will use the given locale' do + post = Post.with_locale(:de) do + Post.create!(:subject => 'Titel', :content => 'Inhalt') + end + assert_equal 'Titel', post.subject(:de) + end + + test 'attribute reader will use the current locale on ActiveRecord::Base or I18n' do + post = Post.with_locale(:en) do + Post.create!(:subject => 'title', :content => 'content') + end + I18n.locale = :de + post.subject = 'Titel' + assert_equal 'Titel', post.subject + + ActiveRecord::Base.locale = :en + post.subject = 'title' + assert_equal 'title', post.subject + end +end diff --git a/vendor/plugins/globalize2/test/active_record/translation_class_test.rb b/vendor/plugins/globalize2/test/active_record/translation_class_test.rb new file mode 100644 index 00000000..1628416d --- /dev/null +++ b/vendor/plugins/globalize2/test/active_record/translation_class_test.rb @@ -0,0 +1,30 @@ +require File.expand_path(File.dirname(__FILE__) + '/../test_helper') +require File.expand_path(File.dirname(__FILE__) + '/../data/models') + +class TranlationClassTest < ActiveSupport::TestCase + def setup + reset_db! + end + + test 'defines a Translation class nested in the model class' do + assert Post.const_defined?(:Translation) + end + + test 'defines a belongs_to association' do + assert_belongs_to Post::Translation, :post + end + + test 'defines a reader for :locale that always returns a symbol' do + post = Post::Translation.new + post.write_attribute('locale', 'de') + assert_equal :de, post.locale + end + + test 'defines a write for :locale that always writes a string' do + post = Post::Translation.new + post.locale = :de + assert_equal 'de', post.read_attribute('locale') + end +end + + diff --git a/vendor/plugins/globalize2/test/active_record/validation_tests.rb b/vendor/plugins/globalize2/test/active_record/validation_tests.rb new file mode 100644 index 00000000..0148fa38 --- /dev/null +++ b/vendor/plugins/globalize2/test/active_record/validation_tests.rb @@ -0,0 +1,75 @@ +require File.expand_path(File.dirname(__FILE__) + '/../test_helper') +require File.expand_path(File.dirname(__FILE__) + '/../data/models') + +class ValidationTest < ActiveSupport::TestCase + def setup + reset_db! + end + + def teardown + Validatee.instance_variable_set(:@validate_callbacks, CallbackChain.new) + end + + test "validates_presence_of" do + Validatee.class_eval { validates_presence_of :string } + assert !Validatee.new.valid? + assert Validatee.new(:string => 'foo').valid? + end + + test "validates_confirmation_of" do + Validatee.class_eval { validates_confirmation_of :string } + assert !Validatee.new(:string => 'foo', :string_confirmation => 'bar').valid? + assert Validatee.new(:string => 'foo', :string_confirmation => 'foo').valid? + end + + test "validates_acceptance_of" do + Validatee.class_eval { validates_acceptance_of :string, :accept => '1' } + assert !Validatee.new(:string => '0').valid? + assert Validatee.new(:string => '1').valid? + end + + test "validates_length_of (:is)" do + Validatee.class_eval { validates_length_of :string, :is => 1 } + assert !Validatee.new(:string => 'aa').valid? + assert Validatee.new(:string => 'a').valid? + end + + test "validates_format_of" do + Validatee.class_eval { validates_format_of :string, :with => /^\d+$/ } + assert !Validatee.new(:string => 'a').valid? + assert Validatee.new(:string => '1').valid? + end + + test "validates_inclusion_of" do + Validatee.class_eval { validates_inclusion_of :string, :in => %(a) } + assert !Validatee.new(:string => 'b').valid? + assert Validatee.new(:string => 'a').valid? + end + + test "validates_exclusion_of" do + Validatee.class_eval { validates_exclusion_of :string, :in => %(b) } + assert !Validatee.new(:string => 'b').valid? + assert Validatee.new(:string => 'a').valid? + end + + test "validates_numericality_of" do + Validatee.class_eval { validates_numericality_of :string } + assert !Validatee.new(:string => 'a').valid? + assert Validatee.new(:string => '1').valid? + end + + # This doesn't pass and Rails' validates_uniqueness_of implementation doesn't + # seem to be extensible easily. One can work around that by either defining + # a custom validation on the Validatee model itself, or by using validates_uniqueness_of + # on Validatee::Translation. + # + # test "validates_uniqueness_of" do + # Validatee.class_eval { validates_uniqueness_of :string } + # Validatee.create!(:string => 'a') + # assert !Validatee.new(:string => 'a').valid? + # assert Validatee.new(:string => 'b').valid? + # end + + # test "validates_associated" do + # end +end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/active_record_test.rb b/vendor/plugins/globalize2/test/active_record_test.rb new file mode 100644 index 00000000..e6b54f01 --- /dev/null +++ b/vendor/plugins/globalize2/test/active_record_test.rb @@ -0,0 +1,442 @@ +require File.expand_path(File.dirname(__FILE__) + '/test_helper') +require File.expand_path(File.dirname(__FILE__) + '/data/models') + +# Higher level tests. + +class ActiveRecordTest < ActiveSupport::TestCase + def setup + I18n.locale = :en + reset_db! + ActiveRecord::Base.locale = nil + end + + def assert_translated(locale, record, names, expected) + I18n.locale = locale + assert_equal Array(expected), Array(names).map { |name| record.send(name) } + end + + test "a translated record has translations" do + assert_equal [], Post.new.translations + end + + test "saves a translated version of the record for each locale" do + post = Post.create(:subject => 'title') + I18n.locale = :de + post.update_attributes(:subject => 'Titel') + + assert_equal 2, post.translations.size + assert_equal %w(de en), post.translations.map(&:locale).map(&:to_s).sort + assert_equal %w(Titel title), post.translations.map(&:subject).sort + end + + test "a translated record has German translations" do + I18n.locale = :de + post = Post.create(:subject => 'foo') + assert_equal 1, post.translations.size + assert_equal [:de], post.translations.map { |t| t.locale } + end + + test "modifiying translated fields while switching locales" do + post = Post.create(:subject => 'title', :content => 'content') + assert_equal %w(title content), [post.subject, post.content] + + I18n.locale = :de + post.subject, post.content = 'Titel', 'Inhalt' + + assert_translated(:de, post, [:subject, :content], %w(Titel Inhalt)) + assert_translated(:en, post, [:subject, :content], %w(title content)) + assert_translated(:de, post, [:subject, :content], %w(Titel Inhalt)) + + post.save + post.reload + + assert_translated(:en, post, [:subject, :content], %w(title content)) + assert_translated(:de, post, [:subject, :content], %w(Titel Inhalt)) + end + + test "attribute writers do return their argument" do + value = Post.new.subject = 'foo' + assert_equal 'foo', value + end + + test "update_attribute succeeds with valid values" do + post = Post.create(:subject => 'foo', :content => 'bar') + post.update_attribute(:subject, 'baz') + assert_equal 'baz', Post.first.subject + end + + test "update_attributes fails with invalid values" do + post = Post.create(:subject => 'foo', :content => 'bar') + assert !post.update_attributes(:subject => '') + assert_nil post.reload.attributes['subject'] + assert_equal 'foo', post.subject + end + + test "passing the locale to create uses the given locale" do + post = Post.create(:subject => 'Titel', :content => 'Inhalt', :locale => :de) + assert_equal :en, I18n.locale + assert_nil ActiveRecord::Base.locale + + I18n.locale = :de + assert_equal 'Titel', post.subject + end + + test "passing the locale to attributes= uses the given locale" do + post = Post.create(:subject => 'title', :content => 'content') + post.update_attributes(:subject => 'Titel', :content => 'Inhalt', :locale => :de) + post.reload + + assert_equal :en, I18n.locale + assert_nil ActiveRecord::Base.locale + + assert_equal 'title', post.subject + I18n.locale = :de + assert_equal 'Titel', post.subject + end + + test 'reload works' do + post = Post.create(:subject => 'foo', :content => 'bar') + post.subject = 'baz' + post.reload + assert_equal 'foo', post.subject + end + + test "returns nil if no translations are found (unsaved record)" do + post = Post.new(:subject => 'foo') + assert_equal 'foo', post.subject + assert_nil post.content + end + + test "returns nil if no translations are found (saved record)" do + post = Post.create(:subject => 'foo') + post.reload + assert_equal 'foo', post.subject + assert_nil post.content + end + + test "finds a German post" do + post = Post.create(:subject => 'foo (en)', :content => 'bar') + I18n.locale = :de + post = Post.first + post.subject = 'baz (de)' + post.save + assert_equal 'baz (de)', Post.first.subject + I18n.locale = :en + assert_equal 'foo (en)', Post.first.subject + end + + test "saves an English post and loads correctly" do + post = Post.create(:subject => 'foo', :content => 'bar') + assert post.save + post = Post.first + assert_equal 'foo', post.subject + assert_equal 'bar', post.content + end + + test "returns the value for the correct locale, after locale switching" do + post = Post.create(:subject => 'foo') + I18n.locale = :de + post.subject = 'bar' + post.save + I18n.locale = :en + post = Post.first + assert_equal 'foo', post.subject + I18n.locale = :de + assert_equal 'bar', post.subject + end + + test "returns the value for the correct locale, after locale switching, without saving" do + post = Post.create :subject => 'foo' + I18n.locale = :de + post.subject = 'bar' + I18n.locale = :en + assert_equal 'foo', post.subject + I18n.locale = :de + assert_equal 'bar', post.subject + end + + test "saves all locales, even after locale switching" do + post = Post.new :subject => 'foo' + I18n.locale = :de + post.subject = 'bar' + I18n.locale = :he + post.subject = 'baz' + post.save + I18n.locale = :en + post = Post.first + assert_equal 'foo', post.subject + I18n.locale = :de + assert_equal 'bar', post.subject + I18n.locale = :he + assert_equal 'baz', post.subject + end + + test "works with associations" do + blog = Blog.create + post1 = blog.posts.create(:subject => 'foo') + + I18n.locale = :de + post2 = blog.posts.create(:subject => 'bar') + assert_equal 2, blog.posts.size + + I18n.locale = :en + assert_equal 'foo', blog.posts.first.subject + assert_nil blog.posts.last.subject + + I18n.locale = :de + assert_equal 'bar', blog.posts.last.subject + end + + test "works with simple dynamic finders" do + foo = Post.create(:subject => 'foo') + Post.create(:subject => 'bar') + post = Post.find_by_subject('foo') + assert_equal foo, post + end + + test 'change attribute on globalized model' do + post = Post.create(:subject => 'foo', :content => 'bar') + assert_equal [], post.changed + post.subject = 'baz' + assert_equal ['subject'], post.changed + post.content = 'quux' + assert_member 'subject', post.changed + assert_member 'content', post.changed + end + + test 'change attribute on globalized model after locale switching' do + post = Post.create(:subject => 'foo', :content => 'bar') + assert_equal [], post.changed + post.subject = 'baz' + I18n.locale = :de + assert_equal ['subject'], post.changed + end + + test 'complex writing and stashing' do + post = Post.create(:subject => 'foo', :content => 'bar') + post.subject = nil + assert_nil post.subject + assert !post.valid? + post.subject = 'stashed_foo' + assert_equal 'stashed_foo', post.subject + end + + test 'translated class locale setting' do + assert ActiveRecord::Base.respond_to?(:locale) + assert_equal :en, I18n.locale + assert_nil ActiveRecord::Base.locale + + I18n.locale = :de + assert_equal :de, I18n.locale + assert_nil ActiveRecord::Base.locale + + ActiveRecord::Base.locale = :es + assert_equal :de, I18n.locale + assert_equal :es, ActiveRecord::Base.locale + + I18n.locale = :fr + assert_equal :fr, I18n.locale + assert_equal :es, ActiveRecord::Base.locale + end + + test "untranslated class responds to locale" do + assert Blog.respond_to?(:locale) + end + + test "to ensure locales in different classes are the same" do + ActiveRecord::Base.locale = :de + assert_equal :de, ActiveRecord::Base.locale + assert_equal :de, Parent.locale + + Parent.locale = :es + assert_equal :es, ActiveRecord::Base.locale + assert_equal :es, Parent.locale + end + + test "attribute saving goes by content locale and not global locale" do + ActiveRecord::Base.locale = :de + assert_equal :en, I18n.locale + Post.create :subject => 'foo' + assert_equal :de, Post.first.translations.first.locale + end + + test "attribute loading goes by content locale and not global locale" do + post = Post.create(:subject => 'foo') + assert_nil ActiveRecord::Base.locale + + ActiveRecord::Base.locale = :de + assert_equal :en, I18n.locale + post.update_attribute(:subject, 'foo [de]') + assert_equal 'foo [de]', Post.first.subject + + ActiveRecord::Base.locale = :en + assert_equal 'foo', Post.first.subject + end + + test "access content locale before setting" do + Globalize::ActiveRecord::ActMacro.class_eval "remove_class_variable(:@@locale)" + assert_nothing_raised { ActiveRecord::Base.locale } + end + + test "available_locales" do + Post.locale = :de + post = Post.create(:subject => 'foo') + Post.locale = :es + post.update_attribute(:subject, 'bar') + Post.locale = :fr + post.update_attribute(:subject, 'baz') + assert_equal [:de, :es, :fr], post.available_locales + assert_equal [:de, :es, :fr], Post.first.available_locales + end + + test "saving record correctly after post-save reload" do + reloader = Reloader.create(:content => 'foo') + assert_equal 'foo', reloader.content + end + + test "including translations" do + I18n.locale = :de + Post.create(:subject => "Foo1", :content => "Bar1") + Post.create(:subject => "Foo2", :content => "Bar2") + + class << Post + def translations_included + self.all(:include => :translations) + end + end + + default = Post.all.map { |x| [x.subject, x.content] } + with_include = Post.translations_included.map { |x| [x.subject, x.content] } + assert_equal default, with_include + end + + test "setting multiple translations at once with options hash" do + Post.locale = :de + post = Post.create(:subject => "foo1", :content => "foo1") + Post.locale = :en + post.update_attributes(:subject => "bar1", :content => "bar1") + + options = { :de => {:subject => "foo2", :content => "foo2"}, + :en => {:subject => "bar2", :content => "bar2"} } + post.set_translations options + post.reload + + assert ["bar2", "bar2"], [post.subject, post.content] + Post.locale = :de + assert ["foo2", "foo2"], [post.subject, post.content] + end + + test "setting only one translation with set_translations" do + Post.locale = :de + post = Post.create(:subject => "foo1", :content => "foo1") + Post.locale = :en + post.update_attributes(:subject => "bar1", :content => "bar1") + + options = { :en => { :subject => "bar2", :content => "bar2" } } + post.set_translations options + post.reload + + assert ["bar2", "bar2"], [post.subject, post.content] + Post.locale = :de + assert ["foo1", "foo1"], [post.subject, post.content] + end + + test "setting only selected attributes with set_translations" do + Post.locale = :de + post = Post.create(:subject => "foo1", :content => "foo1") + Post.locale = :en + post.update_attributes(:subject => "bar1", :content => "bar1") + + options = { :de => { :content => "foo2" }, :en => { :subject => "bar2" } } + post.set_translations options + post.reload + + assert ["bar2", "bar1"], [post.subject, post.content] + Post.locale = :de + assert ["foo1", "foo2"], [post.subject, post.content] + end + + test "setting invalid attributes raises ArgumentError" do + Post.locale = :de + post = Post.create(:subject => "foo1", :content => "foo1") + Post.locale = :en + post.update_attributes(:subject => "bar1", :content => "bar1") + + options = { :de => {:fake => "foo2"} } + exception = assert_raise(ActiveRecord::UnknownAttributeError) do + post.set_translations options + end + assert_equal "unknown attribute: fake", exception.message + end + + test "reload accepting find options" do + p = Post.create(:subject => "Foo", :content => "Bar") + assert p.reload(:readonly => true, :lock => true) + assert_raise(ArgumentError) { p.reload(:foo => :bar) } + end + + test "dependent destroy of translation" do + p = Post.create(:subject => "Foo", :content => "Bar") + assert_equal 1, PostTranslation.count + p.destroy + assert_equal 0, PostTranslation.count + end + + test "translating subclass of untranslated comment model" do + translated_comment = TranslatedComment.create(:post => @post) + assert_nothing_raised { translated_comment.translations } + end + + test "modifiying translated comments works as expected" do + I18n.locale = :en + translated_comment = TranslatedComment.create(:post => @post, :content => 'foo') + assert_equal 'foo', translated_comment.content + + I18n.locale = :de + translated_comment.content = 'bar' + assert translated_comment.save + assert_equal 'bar', translated_comment.content + + I18n.locale = :en + assert_equal 'foo', translated_comment.content + + assert_equal 2, translated_comment.translations.size + end + + test "can create a proxy class for a namespaced model" do + assert_nothing_raised do + module Foo + module Bar + class Baz < ActiveRecord::Base + translates :bumm + end + end + end + end + end + + test "attribute translated before type cast" do + Post.locale = :en + post = Post.create(:subject => 'foo', :content => 'bar') + Post.locale = :de + post.update_attribute(:subject, "German foo") + assert_equal 'German foo', post.subject_before_type_cast + Post.locale = :en + assert_equal 'foo', post.subject_before_type_cast + end + + test "don't override existing translation class" do + assert PostTranslation.new.respond_to?(:existing_method) + end + + test "has_many and named scopes work with globalize" do + blog = Blog.create + assert_nothing_raised { blog.posts.foobar } + end +end + +# TODO error checking for fields that exist in main table, don't exist in +# proxy table, aren't strings or text +# +# TODO allow finding by translated attributes in conditions? +# TODO generate advanced dynamic finders? diff --git a/vendor/plugins/globalize2/test/backends/chained_test.rb b/vendor/plugins/globalize2/test/backends/chained_test.rb deleted file mode 100644 index bb0b3e05..00000000 --- a/vendor/plugins/globalize2/test/backends/chained_test.rb +++ /dev/null @@ -1,175 +0,0 @@ -require File.join( File.dirname(__FILE__), '..', 'test_helper' ) -require 'globalize/backend/chain' - -module Globalize - module Backend - class Dummy - def translate(locale, key, options = {}) - end - end - end -end - -class ChainedTest < ActiveSupport::TestCase - - test "instantiates a chained backend and sets test as backend" do - assert_nothing_raised { I18n.chain_backends } - assert_instance_of Globalize::Backend::Chain, I18n.backend - end - - test "passes all given arguments to the chained backends #initialize method" do - Globalize::Backend::Chain.expects(:new).with(:spec, :simple) - I18n.chain_backends :spec, :simple - end - - test "passes all given arguments to #add assuming that they are backends" do - # no idea how to spec that - end -end - -class AddChainedTest < ActiveSupport::TestCase - def setup - I18n.backend = Globalize::Backend::Chain.new - end - - test "accepts an instance of a backend" do - assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy.new } - assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first - end - - test "accepts a class and instantiates the backend" do - assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy } - assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first - end - - test "accepts a symbol, constantizes test as a backend class and instantiates the backend" do - assert_nothing_raised { I18n.backend.add :dummy } - assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first - end - - test "accepts any number of backend instances, classes or symbols" do - assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy.new, Globalize::Backend::Dummy, :dummy } - assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first - assert_equal [ Globalize::Backend::Dummy, Globalize::Backend::Dummy, Globalize::Backend::Dummy ], - I18n.backend.send(:backends).map{|backend| backend.class } - end - -end - -class TranslateChainedTest < ActiveSupport::TestCase - def setup - I18n.locale = :en - I18n.backend = Globalize::Backend::Chain.new - @first_backend = I18n::Backend::Simple.new - @last_backend = I18n::Backend::Simple.new - I18n.backend.add @first_backend - I18n.backend.add @last_backend - end - - test "delegates #translate to all backends in the order they were added" do - @first_backend.expects(:translate).with(:en, :foo, {}) - @last_backend.expects(:translate).with(:en, :foo, {}) - I18n.translate :foo - end - - test "returns the result from #translate from the first backend if test's not nil" do - @first_backend.store_translations :en, {:foo => 'foo from first backend'} - @last_backend.store_translations :en, {:foo => 'foo from last backend'} - result = I18n.translate :foo - assert_equal 'foo from first backend', result - end - - test "returns the result from #translate from the second backend if the first one returned nil" do - @first_backend.store_translations :en, {} - @last_backend.store_translations :en, {:foo => 'foo from last backend'} - result = I18n.translate :foo - assert_equal 'foo from last backend', result - end - - test "looks up a namespace from all backends and merges them (if a result is a hash and no count option is present)" do - @first_backend.store_translations :en, {:foo => {:bar => 'bar from first backend'}} - @last_backend.store_translations :en, {:foo => {:baz => 'baz from last backend'}} - result = I18n.translate :foo - assert_equal( {:bar => 'bar from first backend', :baz => 'baz from last backend'}, result ) - end - - test "raises a MissingTranslationData exception if no translation was found" do - assert_raise( I18n::MissingTranslationData ) { I18n.translate :not_here, :raise => true } - end - - test "raises an InvalidLocale exception if the locale is nil" do - assert_raise( I18n::InvalidLocale ) { Globalize::Backend::Chain.new.translate nil, :foo } - end - - test "bulk translates a number of keys from different backends" do - @first_backend.store_translations :en, {:foo => 'foo from first backend'} - @last_backend.store_translations :en, {:bar => 'bar from last backend'} - result = I18n.translate [:foo, :bar] - assert_equal( ['foo from first backend', 'bar from last backend'], result ) - end - - test "still calls #translate on all the backends" do - @last_backend.expects :translate - I18n.translate :not_here, :default => 'default' - end - - test "returns a given default string when no backend returns a translation" do - result = I18n.translate :not_here, :default => 'default' - assert_equal 'default', result - end - -end - -class CustomLocalizeBackend < I18n::Backend::Simple - def localize(locale, object, format = :default) - "result from custom localize backend" if locale == 'custom' - end -end - -class LocalizeChainedTest < ActiveSupport::TestCase - def setup - I18n.locale = :en - I18n.backend = Globalize::Backend::Chain.new - @first_backend = CustomLocalizeBackend.new - @last_backend = I18n::Backend::Simple.new - I18n.backend.add @first_backend - I18n.backend.add @last_backend - @time = Time.now - end - - test "delegates #localize to all backends in the order they were added" do - @first_backend.expects(:localize).with(:en, @time, :default) - @last_backend.expects(:localize).with(:en, @time, :default) - I18n.localize @time - end - - test "returns the result from #localize from the first backend if test's not nil" do - @last_backend.expects(:localize).never - result = I18n.localize @time, :locale => 'custom' - assert_equal 'result from custom localize backend', result - end - - test "returns the result from #localize from the second backend if the first one returned nil" do - @last_backend.expects(:localize).returns "value from last backend" - result = I18n.localize @time - assert_equal 'value from last backend', result - end -end - -class NamespaceChainedTest < ActiveSupport::TestCase - def setup - @backend = Globalize::Backend::Chain.new - end - - test "returns false if the given result is not a Hash" do - assert !@backend.send(:namespace_lookup?, 'foo', {}) - end - - test "returns false if a count option is present" do - assert !@backend.send(:namespace_lookup?, {:foo => 'foo'}, {:count => 1}) - end - - test "returns true if the given result is a Hash AND no count option is present" do - assert @backend.send(:namespace_lookup?, {:foo => 'foo'}, {}) - end -end diff --git a/vendor/plugins/globalize2/test/backends/pluralizing_test.rb b/vendor/plugins/globalize2/test/backends/pluralizing_test.rb deleted file mode 100644 index 7445e3ff..00000000 --- a/vendor/plugins/globalize2/test/backends/pluralizing_test.rb +++ /dev/null @@ -1,63 +0,0 @@ -require File.join( File.dirname(__FILE__), '..', 'test_helper' ) -require 'globalize/backend/pluralizing' - -class PluralizingTest < ActiveSupport::TestCase - def setup - @backend = Globalize::Backend::Pluralizing.new - @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other } - end - - test "#pluralizer returns the pluralizer for a given locale if defined" do - assert_instance_of Proc, @backend.pluralizer(:en) - end - - test "#pluralizer returns the default pluralizer if no pluralizer is defined for the given locale" do - assert_equal @backend.pluralizer(:en), @backend.pluralizer(:de) - end - - test "#add_pluralizer allows to store a pluralizer per locale" do - assert_nothing_raised { @backend.add_pluralizer(:cz, @cz_pluralizer) } - assert_equal @cz_pluralizer, @backend.pluralizer(:cz) - end - -end - -class PluralizePluralizingTest < ActiveSupport::TestCase - def setup - @backend = Globalize::Backend::Pluralizing.new - @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other } - @backend.store_translations :en, :foo => {:one => 'one en foo', :other => 'many en foos'} - @backend.store_translations :cz, :foo => {:one => 'one cz foo', :few => 'few cz foos', :other => 'many cz foos'} - end - - test "looks up the :one translation when count is 1" do - assert_equal 'one en foo', @backend.translate(:en, :foo, :count => 1) - end - - test "looks up the :other translation when count is 2" do - assert_equal 'many en foos', @backend.translate(:en, :foo, :count => 2) - end -end - -class CzPluralizingTest < ActiveSupport::TestCase - def setup - @backend = Globalize::Backend::Pluralizing.new - @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other } - @backend.store_translations :en, :foo => {:one => 'one en foo', :other => 'many en foos'} - @backend.store_translations :cz, :foo => {:one => 'one cz foo', :few => 'few cz foos', :other => 'many cz foos'} - @backend.add_pluralizer(:cz, @cz_pluralizer) - end - - test "looks up the :one translation when count is 1 (:cz)" do - assert_equal 'one cz foo', @backend.translate(:cz, :foo, :count => 1) - end - - test "looks up the :few translation when count is 2 (:cz)" do - assert_equal 'few cz foos', @backend.translate(:cz, :foo, :count => 2) - end - - test "looks up the :other translation when count is 5 (:cz)" do - assert_equal 'many cz foos', @backend.translate(:cz, :foo, :count => 5) - end - -end diff --git a/vendor/plugins/globalize2/test/backends/static_test.rb b/vendor/plugins/globalize2/test/backends/static_test.rb deleted file mode 100644 index 133e5477..00000000 --- a/vendor/plugins/globalize2/test/backends/static_test.rb +++ /dev/null @@ -1,143 +0,0 @@ -require File.join( File.dirname(__FILE__), '..', 'test_helper' ) -require 'globalize/backend/static' -require 'globalize/translation' -require 'action_view' -include ActionView::Helpers::NumberHelper - -I18n.locale = :'en-US' # Need to set this, since I18n defaults to 'en' - -class StaticTest < ActiveSupport::TestCase - def setup - I18n.backend = Globalize::Backend::Static.new - translations = {:"en-US" => {:foo => "foo in en-US", :boz => 'boz', :buz => {:bum => 'bum'}}, - :"en" => {:bar => "bar in en"}, - :"de-DE" => {:baz => "baz in de-DE"}, - :"de" => {:boo => "boo in de", :number => { :currency => { :format => { :unit => '€', :format => '%n %u'}}}}} - translations.each do |locale, data| - I18n.backend.store_translations locale, data - end - I18n.fallbacks.map :"de-DE" => :"en-US", :he => :en - end - - test "returns an instance of Translation:Static" do - translation = I18n.translate :foo - assert_instance_of Globalize::Translation::Static, translation - end - - test "returns the translation in en-US if present" do - assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"en-US") - end - - test "returns the translation in en if en-US is not present" do - assert_equal "bar in en", I18n.translate(:bar, :locale => :"en-US") - end - - test "returns the translation in de-DE if present" do - assert_equal "baz in de-DE", I18n.translate(:baz, :locale => :"de-DE") - end - - test "returns the translation in de if de-DE is not present" do - assert_equal "boo in de", I18n.translate(:boo, :locale => :"de-DE") - end - - test "returns the translation in en-US if none of de-DE and de are present" do - assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"de-DE") - end - - test "returns the translation in en if none of de-DE, de and en-US are present" do - assert_equal "bar in en", I18n.translate(:bar, :locale => :"de-DE") - end - - test "returns the translation in en if none in he is present" do - assert_equal "bar in en", I18n.translate(:bar, :locale => :he) - end - - test "returns the given default String when the key is not present for any locale" do - assert_equal "default", I18n.translate(:missing, :default => "default") - end - - test "returns the fallback translation for the key if present for a fallback locale" do - I18n.backend.store_translations :de, :non_default => "non_default in de" - assert_equal "non_default in de", I18n.translate(:non_default, :default => "default", :locale => :"de-DE") - end - - test "returns an array of translations" do - assert_instance_of Array, I18n.translate([:foo, :boz]) - end - - test "returns an array of instances of Translation::Static" do - assert_equal [Globalize::Translation::Static], I18n.translate([:foo, :boz]).map(&:class).uniq - end - - test "returns a hash of translations" do - assert_instance_of Hash, I18n.translate(:"buz") - end - - test "returns an array of translations 2" do - assert_equal [Globalize::Translation::Static], I18n.translate(:"buz").values.map(&:class) - end - - test "returns currency properly formated" do - currency = number_to_currency(10) - assert_equal "$10.00", currency - end - - test "returns currency properly formated for locale" do - currency = number_to_currency(10, :locale => :'de') - assert_equal "10.000 €", currency - end - - test "returns currency properly formated from parameters" do - currency = number_to_currency(10, :format => "%n %u", :unit => '€') - assert_equal "10.00 €", currency - end - - test "makes sure interpolation does not break even with False as string" do - assert_equal "translation missing: en, support, array, skip_last_comma", I18n.translate(:"support.array.skip_last_comma") - end -end - -class TranslationStaticTest < ActiveSupport::TestCase - def setup - I18n.backend = Globalize::Backend::Static.new - translations = { - :greeting => "Hi {{name}}", - :messages => { :one => "You have one message.", :other => "You have {{count}} messages."} - } - I18n.backend.store_translations :"en", translations - end - - def greeting - I18n.translate :greeting, :locale => :"en-US", :name => "Joshua" - end - - test "stores the actual locale" do - assert_equal :en, greeting.locale - end - - test "stores the requested locale" do - assert_equal :'en-US', greeting.requested_locale - end - - test "stores the requested key" do - assert_equal :greeting, greeting.key - end - - test "stores the options given to #translate" do - assert_equal( {:name => "Joshua"}, greeting.options ) - end - - test "stores the original translation before test was interpolated" do - assert_equal "Hi {{name}}", greeting.original - end - - test "stores the plural_key :one if pluralized as such" do - message = I18n.translate :messages, :locale => :"en-US", :count => 1 - assert_equal :one, message.plural_key - end - - test "stores the plural_key :other if pluralized as such" do - messages = I18n.translate :messages, :locale => :"en-US", :count => 2 - assert_equal :other, messages.plural_key - end -end diff --git a/vendor/plugins/globalize2/test/data/locale/all.yml b/vendor/plugins/globalize2/test/data/locale/all.yml deleted file mode 100644 index ee4fb4a4..00000000 --- a/vendor/plugins/globalize2/test/data/locale/all.yml +++ /dev/null @@ -1,2 +0,0 @@ -en-US: - from-all-file: From the "all" file. diff --git a/vendor/plugins/globalize2/test/data/locale/de-DE.yml b/vendor/plugins/globalize2/test/data/locale/de-DE.yml deleted file mode 100644 index d4c468ed..00000000 --- a/vendor/plugins/globalize2/test/data/locale/de-DE.yml +++ /dev/null @@ -1,2 +0,0 @@ -de-DE: - from-locale-file: Aus der Locale Datei. \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/data/locale/en-US.yml b/vendor/plugins/globalize2/test/data/locale/en-US.yml deleted file mode 100644 index 866b171a..00000000 --- a/vendor/plugins/globalize2/test/data/locale/en-US.yml +++ /dev/null @@ -1,2 +0,0 @@ -en-US: - from-locale-file: From the locale file. diff --git a/vendor/plugins/globalize2/test/data/locale/en-US/module.yml b/vendor/plugins/globalize2/test/data/locale/en-US/module.yml deleted file mode 100644 index 6655e9bf..00000000 --- a/vendor/plugins/globalize2/test/data/locale/en-US/module.yml +++ /dev/null @@ -1,2 +0,0 @@ -en-US: - from-locale-dir: From the locale directory. diff --git a/vendor/plugins/globalize2/test/data/locale/fi-FI/module.yml b/vendor/plugins/globalize2/test/data/locale/fi-FI/module.yml deleted file mode 100644 index 62d00b1f..00000000 --- a/vendor/plugins/globalize2/test/data/locale/fi-FI/module.yml +++ /dev/null @@ -1,2 +0,0 @@ -fi-FI: - from-locale-dir: Locale hakemistosta. \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/data/locale/root.yml b/vendor/plugins/globalize2/test/data/locale/root.yml deleted file mode 100644 index e69de29b..00000000 diff --git a/vendor/plugins/globalize2/test/data/models.rb b/vendor/plugins/globalize2/test/data/models.rb index f6dab903..553fca35 100644 --- a/vendor/plugins/globalize2/test/data/models.rb +++ b/vendor/plugins/globalize2/test/data/models.rb @@ -1,6 +1,16 @@ +#require 'ruby2ruby' +#require 'parse_tree' +#require 'parse_tree_extensions' +#require 'pp' + +class PostTranslation < ActiveRecord::Base + def existing_method ; end +end + class Post < ActiveRecord::Base translates :subject, :content validates_presence_of :subject + named_scope :foobar, :conditions => { :title => "foobar" } end class Blog < ActiveRecord::Base @@ -18,7 +28,7 @@ class Comment < ActiveRecord::Base validates_presence_of :content belongs_to :post end - + class TranslatedComment < Comment translates :content end @@ -26,4 +36,16 @@ end class UltraLongModelNameWithoutProper < ActiveRecord::Base translates :subject, :content validates_presence_of :subject -end \ No newline at end of file +end + +class Reloader < Parent + after_create :do_reload + + def do_reload + reload + end +end + +class Validatee < ActiveRecord::Base + translates :string +end diff --git a/vendor/plugins/globalize2/test/i18n/missing_translations_test.rb b/vendor/plugins/globalize2/test/i18n/missing_translations_test.rb index 8641d57f..5d0ecd6f 100644 --- a/vendor/plugins/globalize2/test/i18n/missing_translations_test.rb +++ b/vendor/plugins/globalize2/test/i18n/missing_translations_test.rb @@ -1,5 +1,5 @@ -require File.join( File.dirname(__FILE__), '..', 'test_helper' ) -require 'globalize/i18n/missing_translations_log_handler' +require File.dirname(__FILE__) + '/../test_helper' +require 'i18n/missing_translations_log_handler' class MissingTranslationsTest < ActiveSupport::TestCase test "defines I18n.missing_translations_logger accessor" do @@ -19,18 +19,18 @@ class LogMissingTranslationsTest < ActiveSupport::TestCase def setup @locale, @key, @options = :en, :foo, {} @exception = I18n::MissingTranslationData.new(@locale, @key, @options) - + @logger = TestLogger.new I18n.missing_translations_logger = @logger end test "still returns the exception message for MissingTranslationData exceptions" do result = I18n.send(:missing_translations_log_handler, @exception, @locale, @key, @options) - assert_equal 'translation missing: en, foo', result + assert_equal 'translation missing: en, foo', result end - + test "logs the missing translation to I18n.missing_translations_logger" do I18n.send(:missing_translations_log_handler, @exception, @locale, @key, @options) - assert_equal 'translation missing: en, foo', @logger + assert_equal 'translation missing: en, foo', @logger end end diff --git a/vendor/plugins/globalize2/test/load_path_test.rb b/vendor/plugins/globalize2/test/load_path_test.rb deleted file mode 100644 index ff009b38..00000000 --- a/vendor/plugins/globalize2/test/load_path_test.rb +++ /dev/null @@ -1,49 +0,0 @@ -require File.join( File.dirname(__FILE__), 'test_helper' ) -require 'globalize/load_path' - -class LoadPathTest < ActiveSupport::TestCase - def setup - @plugin_dir = "#{File.dirname(__FILE__)}/.." - @locale_dir = "#{File.dirname(__FILE__)}/data/locale" - @load_path = Globalize::LoadPath.new - end - - test "returns glob patterns for all locales and ruby + yaml files by default" do - patterns = %w(locales/all.rb - locales/*.rb - locales/*/**/*.rb - locales/all.yml - locales/*.yml - locales/*/**/*.yml) - assert_equal patterns, @load_path.send(:patterns, 'locales') - end - - test "returns the glob patterns for registered locales and extensions" do - @load_path.locales = [:en, :de] - @load_path.extensions = [:sql] - patterns = %w(locales/all.sql - locales/en.sql - locales/en/**/*.sql - locales/de.sql - locales/de/**/*.sql) - assert_equal patterns, @load_path.send(:patterns, 'locales') - end - - test "expands paths using yml as a default file extension" do - @load_path << @locale_dir - expected = %w(all.yml de-DE.yml en-US.yml en-US/module.yml fi-FI/module.yml root.yml) - assert_equal expected, @load_path.map{|path| path.sub("#{@locale_dir}\/", '')} - end - - test "appends new paths to the collection so earlier collected paths preceed later collected ones" do - @load_path.locales = [:root] - @load_path << "#{@plugin_dir}/lib/locale" - @load_path << @locale_dir - - expected = %W(#{@plugin_dir}/lib/locale/root.yml - #{@locale_dir}/all.yml - #{@locale_dir}/root.yml) - assert_equal expected, @load_path - end - -end diff --git a/vendor/plugins/globalize2/test/locale/fallbacks_test.rb b/vendor/plugins/globalize2/test/locale/fallbacks_test.rb deleted file mode 100644 index 304d3da0..00000000 --- a/vendor/plugins/globalize2/test/locale/fallbacks_test.rb +++ /dev/null @@ -1,154 +0,0 @@ -require File.join( File.dirname(__FILE__), '..', 'test_helper' ) -require 'globalize/locale/fallbacks' - -include Globalize::Locale -I18n.default_locale = :'en-US' # This has to be set explicitly, no longer default for I18n - -class FallbacksTest < ActiveSupport::TestCase - def setup - I18n.fallbacks = Fallbacks.new - end - - def teardown - I18n.default_locale = :'en-US' - end - - test "#[] caches computed results" do - I18n.fallbacks['en'] - assert_equal( { :en => [:en, :"en-US", :root] }, I18n.fallbacks ) - end - - test "#defaults always reflect the I18n.default_locale if no default has been set manually" do - I18n.default_locale = :'en-US' - assert_equal( [:'en-US', :en, :root], I18n.fallbacks.defaults ) - end - - test "#defaults always reflect a manually passed default locale if any" do - I18n.fallbacks = Fallbacks.new(:'fi-FI') - assert_equal( [:'fi-FI', :fi, :root], I18n.fallbacks.defaults ) - I18n.default_locale = :'de-DE' - assert_equal( [:'fi-FI', :fi, :root], I18n.fallbacks.defaults ) - end - - test "#defaults allows to set multiple defaults" do - I18n.fallbacks = Fallbacks.new(:'fi-FI', :'se-FI') - assert_equal( [:'fi-FI', :fi, :'se-FI', :se, :root], I18n.fallbacks.defaults ) - end -end - -class NoMappingFallbacksTest < ActiveSupport::TestCase - def setup - @fallbacks = Fallbacks.new(:'en-US') - end - - test "returns [:es, :en-US, :root] for :es" do - assert_equal [:es, :"en-US", :en, :root], @fallbacks[:es] - end - - test "returns [:es-ES, :es, :en-US, :root] for :es-ES" do - assert_equal [:"es-ES", :es, :"en-US", :en, :root], @fallbacks[:"es-ES"] - end - - test "returns [:es-MX, :es, :en-US, :root] for :es-MX" do - assert_equal [:"es-MX", :es, :"en-US", :en, :root], @fallbacks[:"es-MX"] - end - - test "returns [:es-Latn-ES, :es-Latn, :es, :en-US, :root] for :es-Latn-ES" do - assert_equal [:"es-Latn-ES", :"es-Latn", :es, :"en-US", :en, :root], @fallbacks[:'es-Latn-ES'] - end - - test "returns [:en, :en-US, :root] for :en" do - assert_equal [:en, :"en-US", :root], @fallbacks[:en] - end - - test "returns [:en-US, :en, :root] for :en-US (special case: locale == default)" do - assert_equal [:"en-US", :en, :root], @fallbacks[:"en-US"] - end -end - -class CaMappingFallbacksTest < ActiveSupport::TestCase - # Most people who speak Catalan also live in Spain, so test is safe to assume - # that they also speak Spanish as spoken in Spain. - def setup - @fallbacks = Fallbacks.new(:'en-US') - @fallbacks.map :ca => :"es-ES" - end - - test "returns [:ca, :es-ES, :es, :en-US, :root] for :ca" do - assert_equal [:ca, :"es-ES", :es, :"en-US", :en, :root], @fallbacks[:ca] - end - - test "returns [:ca-ES, :ca, :es-ES, :es, :en-US, :root] for :ca-ES" do - assert_equal [:"ca-ES", :ca, :"es-ES", :es, :"en-US", :en, :root], @fallbacks[:"ca-ES"] - end -end - -class ArMappingFallbacksTest < ActiveSupport::TestCase - # People who speak Arabic as spoken in Palestine often times also speak - # Hebrew as spoken in Israel. However test is in no way safe to assume that - # everybody who speaks Arabic also speaks Hebrew. - def setup - @fallbacks = Fallbacks.new(:'en-US') - @fallbacks.map :"ar-PS" => :"he-IL" - end - - test "returns [:ar, :en-US, :root] for :ar" do - assert_equal [:ar, :"en-US", :en, :root], @fallbacks[:ar] - end - - test "returns [:ar-EG, :ar, :en-US, :root] for :ar-EG" do - assert_equal [:"ar-EG", :ar, :"en-US", :en, :root], @fallbacks[:"ar-EG"] - end - - test "returns [:ar-PS, :ar, :he-IL, :he, :en-US, :root] for :ar-PS" do - assert_equal [:"ar-PS", :ar, :"he-IL", :he, :"en-US", :en, :root], @fallbacks[:"ar-PS"] - end -end - -class SmsMappingFallbacksTest < ActiveSupport::TestCase - # Sami people live in several scandinavian countries. In Finnland many people - # know Swedish and Finnish. Thus, test can be assumed that Sami living in - # Finnland also speak Swedish and Finnish. - def setup - @fallbacks = Fallbacks.new(:'en-US') - @fallbacks.map :sms => [:"se-FI", :"fi-FI"] - end - - test "returns [:sms-FI, :sms, :se-FI, :se, :fi-FI, :fi, :en-US, :root] for :sms-FI" do - assert_equal [:"sms-FI", :sms, :"se-FI", :se, :"fi-FI", :fi, :"en-US", :en, :root], @fallbacks[:"sms-FI"] - end -end - -class DeAtMappingFallbacksTest < ActiveSupport::TestCase - def setup - @fallbacks = Fallbacks.new(:'en-US') - @fallbacks.map :"de-AT" => :"de-DE" - end - - test "returns [:de, :en-US, :root] for de" do - assert_equal [:de, :"en-US", :en, :root], @fallbacks[:"de"] - end - - test "returns [:de-DE, :de, :en-US, :root] for de-DE" do - assert_equal [:"de-DE", :de, :"en-US", :en, :root], @fallbacks[:"de-DE"] - end - - test "returns [:de-AT, :de, :de-DE, :en-US, :root] for de-AT" do - assert_equal [:"de-AT", :de, :"de-DE", :"en-US", :en, :root], @fallbacks[:"de-AT"] - end -end - -class DeMappingFallbacksTest < ActiveSupport::TestCase - def setup - @fallbacks = Fallbacks.new(:'en-US') - @fallbacks.map :de => :en, :he => :en - end - - test "returns [:de, :en, :root] for :de" do - assert_equal [:de, :en, :"en-US", :root], @fallbacks[:de] - end - - test "returns [:he, :en, :root] for :de" do - assert_equal [:he, :en, :"en-US", :root], @fallbacks[:he] - end -end diff --git a/vendor/plugins/globalize2/test/locale/language_tag_test.rb b/vendor/plugins/globalize2/test/locale/language_tag_test.rb deleted file mode 100644 index 2448af12..00000000 --- a/vendor/plugins/globalize2/test/locale/language_tag_test.rb +++ /dev/null @@ -1,130 +0,0 @@ -require File.join( File.dirname(__FILE__), '..', 'test_helper' ) -require 'globalize/locale/language_tag' - -include Globalize::Locale - -class LanguageTagTest < ActiveSupport::TestCase - test "given a valid tag 'de' returns an LanguageTag from #tag" do - assert_instance_of LanguageTag, LanguageTag.tag('de') - end - - test "given a valid tag 'de' returns an array of subtags" do - assert_equal ['de', nil, nil, nil, nil, nil, nil], LanguageTag::SimpleParser.match('de') - end - - test "given a valid tag 'de-DE' returns an array of subtags" do - assert_equal ['de', nil, 'DE', nil, nil, nil, nil], LanguageTag::SimpleParser.match('de-DE') - end - - test "given a valid lowercase tag 'de-latn-de-variant-x-phonebk' returns an array of subtags" do - assert_equal ['de', 'latn', 'de', 'variant', nil, 'x-phonebk', nil], - LanguageTag::SimpleParser.match('de-latn-de-variant-x-phonebk') - end - - test "given a valid uppercase tag 'DE-LATN-DE-VARIANT-X-PHONEBK' returns an array of subtags" do - assert_equal ['DE', 'LATN', 'DE', 'VARIANT', nil, 'X-PHONEBK', nil], - LanguageTag::SimpleParser.match('DE-LATN-DE-VARIANT-X-PHONEBK') - end - - test "given an invalid tag 'a-DE' test returns false" do - assert !LanguageTag::SimpleParser.match('a-DE') - end - - test "given an invalid tag 'de-419-DE' test returns false" do - assert !LanguageTag::SimpleParser.match('de-419-DE') - end -end - -class DeLatnLanguageTagTest < ActiveSupport::TestCase - def setup - subtags = %w(de Latn DE variant a-ext x-phonebk i-klingon) - @tag = LanguageTag.new *subtags - end - - test "returns 'de' as the language subtag in lowercase" do - assert_equal 'de', @tag.language - end - - test "returns 'Latn' as the script subtag in titlecase" do - assert_equal 'Latn', @tag.script - end - - test "returns 'DE' as the region subtag in uppercase" do - assert_equal 'DE', @tag.region - end - - test "returns 'variant' as the variant subtag in lowercase" do - assert_equal 'variant', @tag.variant - end - - test "returns 'a-ext' as the extension subtag" do - assert_equal 'a-ext', @tag.extension - end - - test "returns 'x-phonebk' as the privateuse subtag" do - assert_equal 'x-phonebk', @tag.privateuse - end - - test "returns 'i-klingon' as the grandfathered subtag" do - assert_equal 'i-klingon', @tag.grandfathered - end - - test "returns a formatted tag string from #to_s" do - assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon', @tag.to_s - end - - test "returns an array containing the formatted subtags from #to_a" do - assert_equal %w(de Latn DE variant a-ext x-phonebk i-klingon), @tag.to_a - end -end - -class InheritanceLanguageTagTest < ActiveSupport::TestCase - test "returns 'de-Latn-DE-variant-a-ext-x-phonebk' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do - tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk i-klingon) - assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk', tag.parent.to_s - end - - test "returns 'de-Latn-DE-variant-a-ext' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk'" do - tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk) - assert_equal 'de-Latn-DE-variant-a-ext', tag.parent.to_s - end - - test "returns 'de-Latn-DE-variant' as the parent of 'de-Latn-DE-variant-a-ext'" do - tag = LanguageTag.new *%w(de Latn DE variant a-ext) - assert_equal 'de-Latn-DE-variant', tag.parent.to_s - end - - test "returns 'de-Latn-DE' as the parent of 'de-Latn-DE-variant'" do - tag = LanguageTag.new *%w(de Latn DE variant) - assert_equal 'de-Latn-DE', tag.parent.to_s - end - - test "returns 'de-Latn' as the parent of 'de-Latn-DE'" do - tag = LanguageTag.new *%w(de Latn DE) - assert_equal 'de-Latn', tag.parent.to_s - end - - test "returns 'de' as the parent of 'de-Latn'" do - tag = LanguageTag.new *%w(de Latn) - assert_equal 'de', tag.parent.to_s - end - - # TODO RFC4647 says: "If no language tag matches the request, the "default" value is returned." - # where should we set the default language? - # test "returns '' as the parent of 'de'" do - # tag = LanguageTag.new *%w(de) - # assert_equal '', tag.parent.to_s - # end - - test "returns an array of 5 parents for 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do - parents = %w(de-Latn-DE-variant-a-ext-x-phonebk-i-klingon - de-Latn-DE-variant-a-ext-x-phonebk - de-Latn-DE-variant-a-ext - de-Latn-DE-variant - de-Latn-DE - de-Latn - de) - tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk i-klingon) - assert_equal parents, tag.parents.map{|tag| tag.to_s} - end -end diff --git a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb deleted file mode 100644 index eb6533d3..00000000 --- a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb +++ /dev/null @@ -1,125 +0,0 @@ -require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' ) -require 'active_record' -require 'globalize/model/active_record' - -# Hook up model translation -ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) - -# Load Post model -require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' ) - -class MigrationTest < ActiveSupport::TestCase - def setup - reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb')) - end - - test 'globalize table added' do - assert !Post.connection.table_exists?( :post_translations ) - assert !Post.connection.index_exists?( :post_translations, :post_id ) - Post.create_translation_table! :subject => :string, :content => :text - assert Post.connection.table_exists?( :post_translations ) - assert Post.connection.index_exists?( :post_translations, :post_id ) - columns = Post.connection.columns( :post_translations ) - assert locale = columns.detect {|c| c.name == 'locale' } - assert_equal :string, locale.type - assert subject = columns.detect {|c| c.name == 'subject' } - assert_equal :string, subject.type - assert content = columns.detect {|c| c.name == 'content' } - assert_equal :text, content.type - assert post_id = columns.detect {|c| c.name == 'post_id' } - assert_equal :integer, post_id.type - assert created_at = columns.detect {|c| c.name == 'created_at' } - assert_equal :datetime, created_at.type - assert updated_at = columns.detect {|c| c.name == 'updated_at' } - assert_equal :datetime, updated_at.type - end - - test 'globalize table dropped' do - assert !Post.connection.table_exists?( :post_translations ) - assert !Post.connection.index_exists?( :post_translations, :post_id ) - Post.create_translation_table! :subject => :string, :content => :text - assert Post.connection.table_exists?( :post_translations ) - assert Post.connection.index_exists?( :post_translations, :post_id ) - Post.drop_translation_table! - assert !Post.connection.table_exists?( :post_translations ) - assert !Post.connection.index_exists?( :post_translations, :post_id ) - end - - test 'exception on missing field inputs' do - assert_raise Globalize::Model::MigrationMissingTranslatedField do - Post.create_translation_table! :content => :text - end - end - - test 'exception on bad input type' do - assert_raise Globalize::Model::BadMigrationFieldType do - Post.create_translation_table! :subject => :string, :content => :integer - end - end - - test "exception on bad input type isn't raised for untranslated fields" do - assert_nothing_raised do - Post.create_translation_table! :subject => :string, :content => :string, :views_count => :integer - end - end - - test 'create_translation_table! should not be called on non-translated models' do - assert_raise NoMethodError do - Blog.create_translation_table! :name => :string - end - end - - test 'drop_translation_table! should not be called on non-translated models' do - assert_raise NoMethodError do - Blog.drop_translation_table! - end - end - - test "translation_index_name returns a readable index name when it's not longer than 50 characters" do - assert_equal 'index_post_translations_on_post_id', Post.send(:translation_index_name) - end - - test "translation_index_name returns a hashed index name when it's longer than 50 characters" do - class UltraLongModelNameWithoutProper < ActiveRecord::Base - translates :foo - end - expected = 'index_44eba0f057e01a590ffccd0b8a3b5c78979539cd' - actual = UltraLongModelNameWithoutProper.send(:translation_index_name) - - assert_equal expected, actual - end - - test 'globalize table added when table has long name' do - UltraLongModelNameWithoutProper.create_translation_table!( - :subject => :string, :content => :text - ) - - assert UltraLongModelNameWithoutProper.connection.table_exists?( - :ultra_long_model_name_without_proper_translations - ) - assert UltraLongModelNameWithoutProper.connection.index_exists?( - :ultra_long_model_name_without_proper_translations, - :name => UltraLongModelNameWithoutProper.send( - :translation_index_name - ) - ) - end - - test 'globalize table dropped when table has long name' do - UltraLongModelNameWithoutProper.create_translation_table!( - :subject => :string, :content => :text - ) - - UltraLongModelNameWithoutProper.drop_translation_table! - - assert !UltraLongModelNameWithoutProper.connection.table_exists?( - :ultra_long_model_name_without_proper_translations - ) - - assert !UltraLongModelNameWithoutProper.connection.index_exists?( - :ultra_long_model_name_without_proper_translations, - :ultra_long_model_name_without_proper_id - ) - end - -end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb deleted file mode 100644 index 6980ba05..00000000 --- a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb +++ /dev/null @@ -1,75 +0,0 @@ -require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' ) -require 'active_record' -require 'globalize/model/active_record' - -# Hook up model translation -ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) - -# Load Post model -require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' ) - -class StiTranslatedTest < ActiveSupport::TestCase - def setup - I18n.locale = :'en-US' - I18n.fallbacks.clear - reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb')) - end - - def teardown - I18n.fallbacks.clear - end - - test "works with simple dynamic finders" do - foo = Child.create :content => 'foo' - Child.create :content => 'bar' - child = Child.find_by_content('foo') - assert_equal foo, child - end - - test 'change attribute on globalized model' do - child = Child.create :content => 'foo' - assert_equal [], child.changed - child.content = 'bar' - assert_equal [ 'content' ], child.changed - child.content = 'baz' - assert_member 'content', child.changed - end - - test 'change attribute on globalized model after locale switching' do - child = Child.create :content => 'foo' - assert_equal [], child.changed - child.content = 'bar' - I18n.locale = :de - assert_equal [ 'content' ], child.changed - end - - test 'fallbacks with lots of locale switching' do - I18n.fallbacks.map :'de-DE' => [ :'en-US' ] - child = Child.create :content => 'foo' - - I18n.locale = :'de-DE' - assert_equal 'foo', child.content - - I18n.locale = :'en-US' - child.update_attribute :content, 'bar' - - I18n.locale = :'de-DE' - assert_equal 'bar', child.content - end - - test "saves all locales, even after locale switching" do - child = Child.new :content => 'foo' - I18n.locale = 'de-DE' - child.content = 'bar' - I18n.locale = 'he-IL' - child.content = 'baz' - child.save - I18n.locale = 'en-US' - child = Child.first - assert_equal 'foo', child.content - I18n.locale = 'de-DE' - assert_equal 'bar', child.content - I18n.locale = 'he-IL' - assert_equal 'baz', child.content - end -end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb deleted file mode 100644 index f54894e3..00000000 --- a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb +++ /dev/null @@ -1,478 +0,0 @@ -require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' ) -require 'active_record' -require 'globalize/model/active_record' - -# Hook up model translation -ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) - -# Load Post model -require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' ) - -class TranslatedTest < ActiveSupport::TestCase - def setup - I18n.locale = :'en-US' - I18n.fallbacks.clear - reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb')) - ActiveRecord::Base.locale = nil - end - - def teardown - I18n.fallbacks.clear - end - - test "modifiying translated fields" do - post = Post.create :subject => 'foo' - assert_equal 'foo', post.subject - post.subject = 'bar' - assert_equal 'bar', post.subject - end - - test "modifiying translated fields while switching locales" do - post = Post.create :subject => 'foo' - assert_equal 'foo', post.subject - I18n.locale = :'de-DE' - post.subject = 'bar' - assert_equal 'bar', post.subject - I18n.locale = :'en-US' - assert_equal 'foo', post.subject - I18n.locale = :'de-DE' - post.subject = 'bar' - end - - test "has post_translations" do - post = Post.create - assert_nothing_raised { post.globalize_translations } - end - - test "has German post_translations" do - I18n.locale = :de - post = Post.create :subject => 'foo' - assert_equal 1, post.globalize_translations.size - I18n.locale = :en - assert_equal 1, post.globalize_translations.size - end - - test "returns the value passed to :subject" do - post = Post.new - assert_equal 'foo', (post.subject = 'foo') - end - - test "translates subject and content into en-US" do - post = Post.create :subject => 'foo', :content => 'bar' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content - assert post.save - post.reload - assert_equal 'foo', post.subject - assert_equal 'bar', post.content - end - - test "finds a German post" do - post = Post.create :subject => 'foo (en)', :content => 'bar' - I18n.locale = 'de-DE' - post = Post.first - post.subject = 'baz (de)' - post.save - assert_equal 'baz (de)', Post.first.subject - I18n.locale = :'en-US' - assert_equal 'foo (en)', Post.first.subject - end - - test "saves an English post and loads test correctly" do - assert_nil Post.first - post = Post.create :subject => 'foo', :content => 'bar' - assert post.save - post = Post.first - assert_equal 'foo', post.subject - assert_equal 'bar', post.content - end - - test "updates an attribute" do - post = Post.create :subject => 'foo', :content => 'bar' - post.update_attribute :subject, 'baz' - assert_equal 'baz', Post.first.subject - end - - test "update_attributes failure" do - post = Post.create :subject => 'foo', :content => 'bar' - assert !post.update_attributes( { :subject => '' } ) - assert_nil post.reload.attributes['subject'] - assert_equal 'foo', post.subject - end - - test "validates presence of :subject" do - post = Post.new - assert !post.save - - post = Post.new :subject => 'foo' - assert post.save - end - - test "returns the value for the correct locale, after locale switching" do - post = Post.create :subject => 'foo' - I18n.locale = 'de-DE' - post.subject = 'bar' - post.save - I18n.locale = 'en-US' - post = Post.first - assert_equal 'foo', post.subject - I18n.locale = 'de-DE' - assert_equal 'bar', post.subject - end - - test "keeping one field in new locale when other field is changed" do - I18n.fallbacks.map 'de-DE' => [ 'en-US' ] - post = Post.create :subject => 'foo' - I18n.locale = 'de-DE' - post.content = 'bar' - assert_equal 'foo', post.subject - end - - test "modifying non-required field in a new locale" do - I18n.fallbacks.map 'de-DE' => [ 'en-US' ] - post = Post.create :subject => 'foo' - I18n.locale = 'de-DE' - post.content = 'bar' - assert post.save - end - - test "returns the value for the correct locale, after locale switching, without saving" do - post = Post.create :subject => 'foo' - I18n.locale = 'de-DE' - post.subject = 'bar' - I18n.locale = 'en-US' - assert_equal 'foo', post.subject - I18n.locale = 'de-DE' - assert_equal 'bar', post.subject - end - - test "saves all locales, even after locale switching" do - post = Post.new :subject => 'foo' - I18n.locale = 'de-DE' - post.subject = 'bar' - I18n.locale = 'he-IL' - post.subject = 'baz' - post.save - I18n.locale = 'en-US' - post = Post.first - assert_equal 'foo', post.subject - I18n.locale = 'de-DE' - assert_equal 'bar', post.subject - I18n.locale = 'he-IL' - assert_equal 'baz', post.subject - end - - test "resolves a simple fallback" do - I18n.locale = 'de-DE' - post = Post.create :subject => 'foo' - I18n.locale = 'de' - post.subject = 'baz' - post.content = 'bar' - post.save - I18n.locale = 'de-DE' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content - end - - test "resolves a simple fallback without reloading" do - I18n.locale = 'de-DE' - post = Post.new :subject => 'foo' - I18n.locale = 'de' - post.subject = 'baz' - post.content = 'bar' - I18n.locale = 'de-DE' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content - end - - test "resolves a complex fallback without reloading" do - I18n.fallbacks.map 'de' => %w(en he) - I18n.locale = 'de' - post = Post.new - I18n.locale = 'en' - post.subject = 'foo' - I18n.locale = 'he' - post.subject = 'baz' - post.content = 'bar' - I18n.locale = 'de' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content - end - - test "returns nil if no translations are found" do - post = Post.new :subject => 'foo' - assert_equal 'foo', post.subject - assert_nil post.content - end - - test "returns nil if no translations are found; reloaded" do - post = Post.create :subject => 'foo' - post = Post.first - assert_equal 'foo', post.subject - assert_nil post.content - end - - test "works with associations" do - blog = Blog.create - post1 = blog.posts.create :subject => 'foo' - I18n.locale = 'de-DE' - post2 = blog.posts.create :subject => 'bar' - assert_equal 2, blog.posts.size - I18n.locale = 'en-US' - assert_equal 'foo', blog.posts.first.subject - assert_nil blog.posts.last.subject - I18n.locale = 'de-DE' - assert_equal 'bar', blog.posts.last.subject - end - - test "works with simple dynamic finders" do - foo = Post.create :subject => 'foo' - Post.create :subject => 'bar' - post = Post.find_by_subject('foo') - assert_equal foo, post - end - - test 'change attribute on globalized model' do - post = Post.create :subject => 'foo', :content => 'bar' - assert_equal [], post.changed - post.subject = 'baz' - assert_equal [ 'subject' ], post.changed - post.content = 'quux' - assert_member 'subject', post.changed - assert_member 'content', post.changed - end - - test 'change attribute on globalized model after locale switching' do - post = Post.create :subject => 'foo', :content => 'bar' - assert_equal [], post.changed - post.subject = 'baz' - I18n.locale = :de - assert_equal [ 'subject' ], post.changed - end - - test 'fallbacks with lots of locale switching' do - I18n.fallbacks.map :'de-DE' => [ :'en-US' ] - post = Post.create :subject => 'foo' - - I18n.locale = :'de-DE' - assert_equal 'foo', post.subject - - I18n.locale = :'en-US' - post.update_attribute :subject, 'bar' - - I18n.locale = :'de-DE' - assert_equal 'bar', post.subject - end - - test 'reload' do - post = Post.create :subject => 'foo', :content => 'bar' - post.subject = 'baz' - assert_equal 'foo', post.reload.subject - end - - test 'complex writing and stashing' do - post = Post.create :subject => 'foo', :content => 'bar' - post.subject = nil - assert_nil post.subject - assert !post.valid? - end - - test 'translated class locale setting' do - assert ActiveRecord::Base.respond_to?(:locale) - assert_equal :'en-US', I18n.locale - assert_equal :'en-US', ActiveRecord::Base.locale - I18n.locale = :de - assert_equal :de, I18n.locale - assert_equal :de, ActiveRecord::Base.locale - ActiveRecord::Base.locale = :es - assert_equal :de, I18n.locale - assert_equal :es, ActiveRecord::Base.locale - I18n.locale = :fr - assert_equal :fr, I18n.locale - assert_equal :es, ActiveRecord::Base.locale - end - - test "untranslated class responds to locale" do - assert Blog.respond_to?(:locale) - end - - test "to ensure locales in different classes are the same" do - ActiveRecord::Base.locale = :de - assert_equal :de, ActiveRecord::Base.locale - assert_equal :de, Parent.locale - Parent.locale = :es - assert_equal :es, ActiveRecord::Base.locale - assert_equal :es, Parent.locale - end - - test "attribute saving goes by content locale and not global locale" do - ActiveRecord::Base.locale = :de - assert_equal :'en-US', I18n.locale - Post.create :subject => 'foo' - assert_equal :de, Post.first.globalize_translations.first.locale - end - - test "attribute loading goes by content locale and not global locale" do - post = Post.create :subject => 'foo' - assert_equal :'en-US', ActiveRecord::Base.locale - ActiveRecord::Base.locale = :de - assert_equal :'en-US', I18n.locale - post.update_attribute :subject, 'foo [de]' - assert_equal 'foo [de]', Post.first.subject - ActiveRecord::Base.locale = :'en-US' - assert_equal 'foo', Post.first.subject - end - - test "access content locale before setting" do - Globalize::Model::ActiveRecord::Translated::ActMethods.class_eval "remove_class_variable(:@@locale)" - assert_nothing_raised { ActiveRecord::Base.locale } - end - - test "translated_locales" do - Post.locale = :de - post = Post.create :subject => 'foo' - Post.locale = :es - post.update_attribute :subject, 'bar' - Post.locale = :fr - post.update_attribute :subject, 'baz' - assert_equal [ :de, :es, :fr ], post.translated_locales - assert_equal [ :de, :es, :fr ], Post.first.translated_locales - end - - test "including globalize_translations" do - I18n.locale = :de - Post.create :subject => "Foo1", :content => "Bar1" - Post.create :subject => "Foo2", :content => "Bar2" - - class << Post - def tranlsations_included - self.all(:include => :globalize_translations) - end - end - - default = Post.all.map {|x| [x.subject, x.content]} - with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]} - assert_equal default, with_include - end - - test "setting multiple translations at once with options hash" do - Post.locale = :de - post = Post.create :subject => "foo1", :content => "foo1" - Post.locale = :en - post.update_attributes( :subject => "bar1", :content => "bar1" ) - - options = { :de => {:subject => "foo2", :content => "foo2"}, - :en => {:subject => "bar2", :content => "bar2"} } - post.set_translations options - post.reload - - assert ["bar2", "bar2"], [post.subject, post.content] - Post.locale = :de - assert ["foo2", "foo2"], [post.subject, post.content] - end - - test "setting only one translation with set_translations" do - Post.locale = :de - post = Post.create :subject => "foo1", :content => "foo1" - Post.locale = :en - post.update_attributes( :subject => "bar1", :content => "bar1" ) - - options = { :en => {:subject => "bar2", :content => "bar2"} } - post.set_translations options - post.reload - - assert ["bar2", "bar2"], [post.subject, post.content] - Post.locale = :de - assert ["foo1", "foo1"], [post.subject, post.content] - end - - test "setting only selected attributes with set_translations" do - Post.locale = :de - post = Post.create :subject => "foo1", :content => "foo1" - Post.locale = :en - post.update_attributes( :subject => "bar1", :content => "bar1" ) - - options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} } - post.set_translations options - post.reload - - assert ["bar2", "bar1"], [post.subject, post.content] - Post.locale = :de - assert ["foo1", "foo2"], [post.subject, post.content] - end - - test "setting invalid attributes raises ArgumentError" do - Post.locale = :de - post = Post.create :subject => "foo1", :content => "foo1" - Post.locale = :en - post.update_attributes( :subject => "bar1", :content => "bar1" ) - - options = { :de => {:fake => "foo2"} } - exception = assert_raise(ActiveRecord::UnknownAttributeError) do - post.set_translations options - end - assert_equal "unknown attribute: fake", exception.message - end - - test "reload accepting find options" do - p = Post.create :subject => "Foo", :content => "Bar" - assert p.reload(:readonly => true, :lock => true) - assert_raise(ArgumentError) { p.reload(:foo => :bar) } - end - - test "dependent destroy of translation" do - p = Post.create :subject => "Foo", :content => "Bar" - assert_equal 1, PostTranslation.count - p.destroy - assert_equal 0, PostTranslation.count - end - - test "translating subclass of untranslated comment model" do - translated_comment = TranslatedComment.create(:post => @post) - assert_nothing_raised { translated_comment.globalize_translations } - end - - test "modifiying translated comments works as expected" do - I18n.locale = :en - translated_comment = TranslatedComment.create(:post => @post, :content => 'foo') - assert_equal 'foo', translated_comment.content - - I18n.locale = :de - translated_comment.content = 'bar' - assert translated_comment.save - assert_equal 'bar', translated_comment.content - - I18n.locale = :en - assert_equal 'foo', translated_comment.content - - assert_equal 2, translated_comment.globalize_translations.size - end - - test "can create a proxy class for a namespaced model" do - module Foo - module Bar - class Baz < ActiveRecord::Base - translates :bumm - end - end - end - end - - test "attribute translated before type cast" do - Post.locale = :en - post = Post.create :subject => 'foo', :content => 'bar' - Post.locale = :de - post.update_attribute :subject, "German foo" - assert_equal 'German foo', post.subject_before_type_cast - Post.locale = :en - assert_equal 'foo', post.subject_before_type_cast - end -end - -# TODO should validate_presence_of take fallbacks into account? maybe we need -# an extra validation call, or more options for validate_presence_of. -# TODO error checking for fields that exist in main table, don't exist in -# proxy table, aren't strings or text -# -# TODO allow finding by translated attributes in conditions? -# TODO generate advanced dynamic finders? diff --git a/vendor/plugins/globalize2/test/test_helper.rb b/vendor/plugins/globalize2/test/test_helper.rb index 3a1c8c46..195907df 100644 --- a/vendor/plugins/globalize2/test/test_helper.rb +++ b/vendor/plugins/globalize2/test/test_helper.rb @@ -1,28 +1,41 @@ +$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' ) + require 'rubygems' require 'test/unit' +require 'active_record' require 'active_support' require 'active_support/test_case' require 'mocha' +require 'globalize' +require 'validation_reflection' -$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' ) +config = { :adapter => 'sqlite3', :database => ':memory:' } +ActiveRecord::Base.establish_connection(config) class ActiveSupport::TestCase - def reset_db!( schema_path ) - ::ActiveRecord::Migration.verbose = false # Quiet down the migration engine - ::ActiveRecord::Base.establish_connection({ - :adapter => 'sqlite3', - :database => ':memory:' - }) - ::ActiveRecord::Base.silence do - load schema_path - end + def reset_db!(schema_path = nil) + schema_path ||= File.expand_path(File.dirname(__FILE__) + '/data/schema.rb') + ActiveRecord::Migration.verbose = false + ActiveRecord::Base.silence { load(schema_path) } end - - def assert_member(item, arr) - assert_block "Item #{item} is not in array #{arr}" do - arr.member? item + + def assert_member(item, array) + assert_block "Item #{item} is not in array #{array}" do + array.member?(item) end end + + def assert_belongs_to(model, associated) + assert model.reflect_on_all_associations(:belongs_to).detect { |association| + association.name.to_s == associated.to_s + } + end + + def assert_has_many(model, associated) + assert model.reflect_on_all_associations(:has_many).detect { |association| + association.name.to_s == associated.to_s + } + end end module ActiveRecord @@ -33,4 +46,31 @@ module ActiveRecord end end end -end \ No newline at end of file +end + +# module ActiveRecord +# class BaseWithoutTable < Base +# self.abstract_class = true +# +# def create_or_update +# errors.empty? +# end +# +# class << self +# def columns() +# @columns ||= [] +# end +# +# def column(name, sql_type = nil, default = nil, null = true) +# columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) +# reset_column_information +# end +# +# # Do not reset @columns +# def reset_column_information +# read_methods.each { |name| undef_method(name) } +# @column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = nil +# end +# end +# end +# end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/translation_test.rb b/vendor/plugins/globalize2/test/translation_test.rb deleted file mode 100644 index 4b52bb10..00000000 --- a/vendor/plugins/globalize2/test/translation_test.rb +++ /dev/null @@ -1,54 +0,0 @@ -require File.join( File.dirname(__FILE__), 'test_helper' ) -require 'globalize/translation' - -class TranslationTest < ActiveSupport::TestCase - include Globalize - - def setup - @translation = Translation::Static.new 'foo', :locale => :'en-US' - end - - test "responds to fallback?" do - assert @translation.respond_to?( :fallback? ) - end - - test "returns true when :locale and :requested_locale are not equal" do - @translation.requested_locale = :'de-DE' - assert @translation.fallback? - end - - test "returns false when :locale and :requested_locale are equal" do - @translation.requested_locale = :'en-US' - assert !@translation.fallback? - end - - test "has the attribute :locale" do - assert @translation.respond_to?( :locale ) - end - - test "has the attribute :requested_locale" do - assert @translation.respond_to?( :requested_locale ) - end - - test "has the attribute :options" do - assert @translation.respond_to?( :options ) - end - - test "has the attribute :plural_key" do - assert @translation.respond_to?( :plural_key ) - end - - test "has the attribute :original" do - assert @translation.respond_to?( :original ) - end - - test "Translation::Attribute has the attribute :locale" do - translation = Translation::Attribute.new 'foo' - assert translation.respond_to?( :locale ) - end - - test "Translation::Attribute has the attribute :requested_locale" do - translation = Translation::Attribute.new 'foo' - assert translation.respond_to?( :requested_locale ) - end -end -- cgit v1.3