summaryrefslogtreecommitdiff
path: root/vendor/plugins/globalize2/test/model
diff options
context:
space:
mode:
authorhukl <hukl@eight.local>2009-02-07 15:50:40 +0100
committerhukl <hukl@eight.local>2009-02-07 15:50:40 +0100
commit36a2f1f3c085dd81171cf7d8220770d4ff921ab3 (patch)
tree5ded15331b192bf428af4c94d46d1abb28ef0690 /vendor/plugins/globalize2/test/model
parentce9645d0092d42c7bf8781378181ffbd4ff3d088 (diff)
added globalize2 plugin as well as some modifications
which use the new translation facilities. backend mostly.
Diffstat (limited to 'vendor/plugins/globalize2/test/model')
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/migration_test.rb73
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/translated_test.rb266
2 files changed, 339 insertions, 0 deletions
diff --git a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb
new file mode 100644
index 0000000..09804c7
--- /dev/null
+++ b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb
@@ -0,0 +1,73 @@
1require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
2require 'active_record'
3require 'globalize/model/active_record'
4
5# Hook up model translation
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7
8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
10
11class MigrationTest < ActiveSupport::TestCase
12 def setup
13 reset_db! 'no_globalize_schema'
14 end
15
16 test 'globalize table added' do
17 assert !Post.connection.table_exists?( :post_translations )
18 Post.create_translation_table! :subject => :string, :content => :text
19 assert Post.connection.table_exists?( :post_translations )
20 columns = Post.connection.columns( :post_translations )
21 assert locale = columns.detect {|c| c.name == 'locale' }
22 assert_equal :string, locale.type
23 assert subject = columns.detect {|c| c.name == 'subject' }
24 assert_equal :string, subject.type
25 assert content = columns.detect {|c| c.name == 'content' }
26 assert_equal :text, content.type
27 assert post_id = columns.detect {|c| c.name == 'post_id' }
28 assert_equal :integer, post_id.type
29 assert created_at = columns.detect {|c| c.name == 'created_at' }
30 assert_equal :datetime, created_at.type
31 assert updated_at = columns.detect {|c| c.name == 'updated_at' }
32 assert_equal :datetime, updated_at.type
33 end
34
35 test 'globalize table dropped' do
36 assert !Post.connection.table_exists?( :post_translations )
37 Post.create_translation_table! :subject => :string, :content => :text
38 assert Post.connection.table_exists?( :post_translations )
39 Post.drop_translation_table!
40 assert !Post.connection.table_exists?( :post_translations )
41 end
42
43 test 'exception on untranslated field inputs' do
44 assert_raise Globalize::Model::UntranslatedMigrationField do
45 Post.create_translation_table! :subject => :string, :content => :text, :bogus => :string
46 end
47 end
48
49 test 'exception on missing field inputs' do
50 assert_raise Globalize::Model::MigrationMissingTranslatedField do
51 Post.create_translation_table! :content => :text
52 end
53 end
54
55 test 'exception on bad input type' do
56 assert_raise Globalize::Model::BadMigrationFieldType do
57 Post.create_translation_table! :subject => :string, :content => :integer
58 end
59 end
60
61 test 'create_translation_table! should not be called on non-translated models' do
62 assert_raise NoMethodError do
63 Blog.create_translation_table! :name => :string
64 end
65 end
66
67 test 'drop_translation_table! should not be called on non-translated models' do
68 assert_raise NoMethodError do
69 Blog.drop_translation_table!
70 end
71 end
72
73end \ 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
new file mode 100644
index 0000000..ec507e0
--- /dev/null
+++ b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb
@@ -0,0 +1,266 @@
1require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
2require 'active_record'
3require 'globalize/model/active_record'
4
5# Hook up model translation
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7
8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
10
11class TranslatedTest < ActiveSupport::TestCase
12 def setup
13 I18n.locale = :'en-US'
14 I18n.fallbacks.clear
15 reset_db!
16 end
17
18 def teardown
19 I18n.fallbacks.clear
20 end
21
22 test "modifiying translated fields" do
23 post = Post.create :subject => 'foo'
24 assert_equal 'foo', post.subject
25 post.subject = 'bar'
26 assert_equal 'bar', post.subject
27 end
28
29 test "modifiying translated fields while switching locales" do
30 post = Post.create :subject => 'foo'
31 assert_equal 'foo', post.subject
32 I18n.locale = :'de-DE'
33 post.subject = 'bar'
34 assert_equal 'bar', post.subject
35 I18n.locale = :'en-US'
36 assert_equal 'foo', post.subject
37 I18n.locale = :'de-DE'
38 post.subject = 'bar'
39 end
40
41 test "has post_translations" do
42 post = Post.create
43 assert_nothing_raised { post.globalize_translations }
44 end
45
46 test "has German post_translations" do
47 I18n.locale = :de
48 post = Post.create :subject => 'foo'
49 assert_equal 1, post.globalize_translations.size
50 I18n.locale = :en
51 assert_equal 1, post.globalize_translations.size
52 end
53
54 test "returns the value passed to :subject" do
55 post = Post.new
56 assert_equal 'foo', (post.subject = 'foo')
57 end
58
59 test "translates subject and content into en-US" do
60 post = Post.create :subject => 'foo', :content => 'bar'
61 assert_equal 'foo', post.subject
62 assert_equal 'bar', post.content
63 assert post.save
64 post.reload
65 assert_equal 'foo', post.subject
66 assert_equal 'bar', post.content
67 end
68
69 test "finds a German post" do
70 post = Post.create :subject => 'foo (en)', :content => 'bar'
71 I18n.locale = 'de-DE'
72 post = Post.first
73 post.subject = 'baz (de)'
74 post.save
75 assert_equal 'baz (de)', Post.first.subject
76 I18n.locale = :'en-US'
77 assert_equal 'foo (en)', Post.first.subject
78 end
79
80 test "saves an English post and loads test correctly" do
81 assert_nil Post.first
82 post = Post.create :subject => 'foo', :content => 'bar'
83 assert post.save
84 post = Post.first
85 assert_equal 'foo', post.subject
86 assert_equal 'bar', post.content
87 end
88
89 test "updates an attribute" do
90 post = Post.create :subject => 'foo', :content => 'bar'
91 post.update_attribute :subject, 'baz'
92 assert_equal 'baz', Post.first.subject
93 end
94
95 test "validates presence of :subject" do
96 post = Post.new
97 assert !post.save
98
99 post = Post.new :subject => 'foo'
100 assert post.save
101 end
102
103 test "returns the value for the correct locale, after locale switching" do
104 post = Post.create :subject => 'foo'
105 I18n.locale = 'de-DE'
106 post.subject = 'bar'
107 post.save
108 I18n.locale = 'en-US'
109 post = Post.first
110 assert_equal 'foo', post.subject
111 I18n.locale = 'de-DE'
112 assert_equal 'bar', post.subject
113 end
114
115 test "keeping one field in new locale when other field is changed" do
116 I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
117 post = Post.create :subject => 'foo'
118 I18n.locale = 'de-DE'
119 post.content = 'bar'
120 assert_equal 'foo', post.subject
121 end
122
123 test "modifying non-required field in a new locale" do
124 I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
125 post = Post.create :subject => 'foo'
126 I18n.locale = 'de-DE'
127 post.content = 'bar'
128 assert post.save
129 end
130
131 test "returns the value for the correct locale, after locale switching, without saving" do
132 post = Post.create :subject => 'foo'
133 I18n.locale = 'de-DE'
134 post.subject = 'bar'
135 I18n.locale = 'en-US'
136 assert_equal 'foo', post.subject
137 I18n.locale = 'de-DE'
138 assert_equal 'bar', post.subject
139 end
140
141 test "saves all locales, even after locale switching" do
142 post = Post.new :subject => 'foo'
143 I18n.locale = 'de-DE'
144 post.subject = 'bar'
145 I18n.locale = 'he-IL'
146 post.subject = 'baz'
147 post.save
148 I18n.locale = 'en-US'
149 post = Post.first
150 assert_equal 'foo', post.subject
151 I18n.locale = 'de-DE'
152 assert_equal 'bar', post.subject
153 I18n.locale = 'he-IL'
154 assert_equal 'baz', post.subject
155 end
156
157 test "resolves a simple fallback" do
158 I18n.locale = 'de-DE'
159 post = Post.create :subject => 'foo'
160 I18n.locale = 'de'
161 post.subject = 'baz'
162 post.content = 'bar'
163 post.save
164 I18n.locale = 'de-DE'
165 assert_equal 'foo', post.subject
166 assert_equal 'bar', post.content
167 end
168
169 test "resolves a simple fallback without reloading" do
170 I18n.locale = 'de-DE'
171 post = Post.new :subject => 'foo'
172 I18n.locale = 'de'
173 post.subject = 'baz'
174 post.content = 'bar'
175 I18n.locale = 'de-DE'
176 assert_equal 'foo', post.subject
177 assert_equal 'bar', post.content
178 end
179
180 test "resolves a complex fallback without reloading" do
181 I18n.fallbacks.map 'de' => %w(en he)
182 I18n.locale = 'de'
183 post = Post.new
184 I18n.locale = 'en'
185 post.subject = 'foo'
186 I18n.locale = 'he'
187 post.subject = 'baz'
188 post.content = 'bar'
189 I18n.locale = 'de'
190 assert_equal 'foo', post.subject
191 assert_equal 'bar', post.content
192 end
193
194 test "returns nil if no translations are found" do
195 post = Post.new :subject => 'foo'
196 assert_equal 'foo', post.subject
197 assert_nil post.content
198 end
199
200 test "returns nil if no translations are found; reloaded" do
201 post = Post.create :subject => 'foo'
202 post = Post.first
203 assert_equal 'foo', post.subject
204 assert_nil post.content
205 end
206
207 test "works with associations" do
208 blog = Blog.create
209 post1 = blog.posts.create :subject => 'foo'
210 I18n.locale = 'de-DE'
211 post2 = blog.posts.create :subject => 'bar'
212 assert_equal 2, blog.posts.size
213 I18n.locale = 'en-US'
214 assert_equal 'foo', blog.posts.first.subject
215 assert_nil blog.posts.last.subject
216 I18n.locale = 'de-DE'
217 assert_equal 'bar', blog.posts.last.subject
218 end
219
220 test "works with simple dynamic finders" do
221 foo = Post.create :subject => 'foo'
222 Post.create :subject => 'bar'
223 post = Post.find_by_subject('foo')
224 assert_equal foo, post
225 end
226
227 test 'change attribute on globalized model' do
228 post = Post.create :subject => 'foo', :content => 'bar'
229 assert_equal [], post.changed
230 post.subject = 'baz'
231 assert_equal [ 'subject' ], post.changed
232 post.content = 'quux'
233 assert_member 'subject', post.changed
234 assert_member 'content', post.changed
235 end
236
237 test 'change attribute on globalized model after locale switching' do
238 post = Post.create :subject => 'foo', :content => 'bar'
239 assert_equal [], post.changed
240 post.subject = 'baz'
241 I18n.locale = :de
242 assert_equal [ 'subject' ], post.changed
243 end
244
245 test 'fallbacks with lots of locale switching' do
246 I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
247 post = Post.create :subject => 'foo'
248
249 I18n.locale = :'de-DE'
250 assert_equal 'foo', post.subject
251
252 I18n.locale = :'en-US'
253 post.update_attribute :subject, 'bar'
254
255 I18n.locale = :'de-DE'
256 assert_equal 'bar', post.subject
257 end
258end
259
260# TODO should validate_presence_of take fallbacks into account? maybe we need
261# an extra validation call, or more options for validate_presence_of.
262# TODO error checking for fields that exist in main table, don't exist in
263# proxy table, aren't strings or text
264#
265# TODO allow finding by translated attributes in conditions?
266# TODO generate advanced dynamic finders?