From f4c73763e5b3d65f3377fb1238a94122a1c272a4 Mon Sep 17 00:00:00 2001 From: hukl Date: Wed, 25 Feb 2009 22:37:22 +0100 Subject: updated globalize2 --- .../test/model/active_record/migration_test.rb | 2 +- .../model/active_record/sti_translated_test.rb | 75 +++++++++++++++++++ .../test/model/active_record/translated_test.rb | 85 +++++++++++++++++++++- 3 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb (limited to 'vendor/plugins/globalize2/test/model') diff --git a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb index 09804c7d..c539fb6e 100644 --- a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb @@ -10,7 +10,7 @@ require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) class MigrationTest < ActiveSupport::TestCase def setup - reset_db! 'no_globalize_schema' + reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb')) end test 'globalize table added' do 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 new file mode 100644 index 00000000..14d7d0f9 --- /dev/null +++ b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb @@ -0,0 +1,75 @@ +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', 'post' ) + +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 index ec507e03..6bb7b26a 100644 --- a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb @@ -12,7 +12,8 @@ class TranslatedTest < ActiveSupport::TestCase def setup I18n.locale = :'en-US' I18n.fallbacks.clear - reset_db! + reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb')) + ActiveRecord::Base.locale = nil end def teardown @@ -92,6 +93,13 @@ class TranslatedTest < ActiveSupport::TestCase 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 @@ -255,6 +263,81 @@ class TranslatedTest < ActiveSupport::TestCase 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 end # TODO should validate_presence_of take fallbacks into account? maybe we need -- cgit v1.3