From 7c9f0d0823bdce9a011d3bdd2823e15ffb7c1311 Mon Sep 17 00:00:00 2001 From: hukl Date: Sun, 19 Jul 2009 19:02:31 +0200 Subject: updated globalize2 plugin --- .../globalize2/test/backends/chained_test.rb | 1 + vendor/plugins/globalize2/test/data/post.rb | 9 ++ .../test/model/active_record/translated_test.rb | 109 +++++++++++++++++++++ 3 files changed, 119 insertions(+) (limited to 'vendor/plugins/globalize2/test') diff --git a/vendor/plugins/globalize2/test/backends/chained_test.rb b/vendor/plugins/globalize2/test/backends/chained_test.rb index bc451791..bb0b3e05 100644 --- a/vendor/plugins/globalize2/test/backends/chained_test.rb +++ b/vendor/plugins/globalize2/test/backends/chained_test.rb @@ -58,6 +58,7 @@ 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 diff --git a/vendor/plugins/globalize2/test/data/post.rb b/vendor/plugins/globalize2/test/data/post.rb index fdfb7c0e..6673dc44 100644 --- a/vendor/plugins/globalize2/test/data/post.rb +++ b/vendor/plugins/globalize2/test/data/post.rb @@ -12,4 +12,13 @@ class Parent < ActiveRecord::Base end class Child < Parent +end + +class Comment < ActiveRecord::Base + validates_presence_of :content + belongs_to :post +end + +class TranslatedComment < Comment + translates :content 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 index 6bb7b26a..3e9ea00a 100644 --- a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb @@ -338,6 +338,115 @@ class TranslatedTest < ActiveSupport::TestCase 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 end # TODO should validate_presence_of take fallbacks into account? maybe we need -- cgit v1.3