PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/globalize/test/db_translation_test.rb

http://pmsaas.googlecode.com/
Ruby | 374 lines | 314 code | 52 blank | 8 comment | 12 complexity | d554475a6e811c91446ae19705f60344 MD5 | raw file
  1. require File.dirname(__FILE__) + '/test_helper'
  2. class TranslationTest < Test::Unit::TestCase
  3. self.use_instantiated_fixtures = true
  4. fixtures :globalize_languages, :globalize_translations, :globalize_countries,
  5. :globalize_products, :globalize_manufacturers, :globalize_categories,
  6. :globalize_categories_products, :globalize_simples
  7. class Product < ActiveRecord::Base
  8. set_table_name "globalize_products"
  9. has_and_belongs_to_many :categories, :join_table => "globalize_categories_products"
  10. belongs_to :manufacturer, :foreign_key => 'manufacturer_id'
  11. translates :name, :description, :specs, {
  12. :name => { :bidi_embed => false }, :specs => { :bidi_embed => false } }
  13. end
  14. class Category < ActiveRecord::Base
  15. set_table_name "globalize_categories"
  16. has_and_belongs_to_many :products, :join_table => "globalize_categories_products"
  17. translates :name
  18. end
  19. class Manufacturer < ActiveRecord::Base
  20. set_table_name "globalize_manufacturers"
  21. has_many :products
  22. translates :name
  23. end
  24. class Simple < ActiveRecord::Base
  25. set_table_name "globalize_simples"
  26. translates :name, :description
  27. end
  28. def setup
  29. Globalize::Locale.set_base_language("en-US")
  30. Globalize::Locale.set("en-US")
  31. end
  32. def test_simple
  33. simp = Simple.find(1)
  34. assert_equal "first", simp.name
  35. assert_equal "This is a description of the first simple", simp.description
  36. Globalize::Locale.set 'he-IL'
  37. simp = Simple.find(1)
  38. assert_equal "??? ??? ??????", simp.name
  39. assert_equal "??? ?????? ??????", simp.description
  40. end
  41. def test_simple_save
  42. simp = Simple.find(1)
  43. simp.name = '1st'
  44. simp.save!
  45. Globalize::Locale.set 'he-IL'
  46. simp = Simple.find(1)
  47. simp.name = '?-1'
  48. simp.save!
  49. end
  50. def test_simple_create
  51. simp = Simple.new
  52. simp.name = '1st'
  53. simp.save!
  54. Globalize::Locale.set 'he-IL'
  55. simp = Simple.new
  56. simp.name = '?-1'
  57. simp.save!
  58. end
  59. def test_native_language
  60. heb = Globalize::Language.pick("he")
  61. assert_equal "?????", heb.native_name
  62. end
  63. def test_nil
  64. Globalize::Locale.set(nil)
  65. prod = Product.find(1)
  66. assert_equal "first-product", prod.code
  67. assert_equal "these are the specs for the first product",
  68. prod.specs
  69. end
  70. def test_nil_include_translated
  71. Globalize::Locale.set(nil)
  72. prods = Product.find(:all, :order => "globalize_products.code", :include_translated => :manufacturer)
  73. assert_equal "first-product", prods[1].code
  74. assert_equal "these are the specs for the first product",
  75. prods[1].specs
  76. assert_equal "first", prods[1].name
  77. assert_equal "Reverend", prods.first.manufacturer_name
  78. assert_equal "Reverend", prods.last.manufacturer_name
  79. end
  80. def test_prod_tr_all
  81. prods = Product.find(:all, :order => "code" )
  82. assert_equal 5, prods.length
  83. assert_equal "first-product", prods[1].code
  84. assert_equal "second-product", prods[3].code
  85. assert_equal "these are the specs for the first product",
  86. prods[1].specs
  87. assert_equal "This is a description of the first product",
  88. prods[1].description
  89. assert_equal "these are the specs for the second product",
  90. prods[3].specs
  91. end
  92. def test_prod_tr_first
  93. prod = Product.find(1)
  94. assert_equal "first-product", prod.code
  95. assert_equal "these are the specs for the first product",
  96. prod.specs
  97. assert_equal "This is a description of the first product",
  98. prod.description
  99. end
  100. def test_prod_tr_id
  101. prod = Product.find(1)
  102. assert_equal "first-product", prod.code
  103. assert_equal "these are the specs for the first product",
  104. prod.specs
  105. assert_equal "This is a description of the first product",
  106. prod.description
  107. end
  108. # Ordering of records returned is database-dependent although MySQL is explicit about ordering
  109. # its result sets. This means this test is only guaranteed to pass on MySQL.
  110. def pending_test_prod_tr_ids
  111. prods = Product.find(1, 2)
  112. assert_equal 2, prods.length
  113. assert_equal "first-product", prods[0].code
  114. assert_equal "second-product", prods[1].code
  115. assert_equal "these are the specs for the first product",
  116. prods[0].specs
  117. assert_equal "This is a description of the first product",
  118. prods[0].description
  119. assert_equal "these are the specs for the second product",
  120. prods[1].specs
  121. end
  122. def test_base
  123. Globalize::Locale.set("he-IL")
  124. prod = Product.find(1)
  125. assert_equal "first-product", prod.code
  126. assert_equal "these are the specs for the first product",
  127. prod.specs
  128. assert_equal "??? ????? ????? ??????",
  129. prod.description
  130. end
  131. def test_habtm_translation
  132. Globalize::Locale.set("he-IL")
  133. cat = Category.find(1)
  134. prods = cat.products
  135. assert_equal 1, prods.length
  136. prod = prods.first
  137. assert_equal "first-product", prod.code
  138. assert_equal "these are the specs for the first product",
  139. prod.specs
  140. assert_equal "??? ????? ????? ??????",
  141. prod.description
  142. end
  143. # test has_many translation
  144. def test_has_many_translation
  145. Globalize::Locale.set("he-IL")
  146. mfr = Manufacturer.find(1)
  147. assert_equal 5, mfr.products.length
  148. prod = mfr.products.find(1)
  149. assert_equal "first-product", prod.code
  150. assert_equal "these are the specs for the first product",
  151. prod.specs
  152. assert_equal "??? ????? ????? ??????",
  153. prod.description
  154. end
  155. def test_belongs_to_translation
  156. Globalize::Locale.set("he-IL")
  157. prod = Product.find(1)
  158. mfr = prod.manufacturer
  159. assert_equal "first-mfr", mfr.code
  160. assert_equal "?????",
  161. mfr.name
  162. end
  163. def test_new
  164. prod = Product.new(:code => "new-product", :specs => "These are the product specs")
  165. assert_equal "These are the product specs", prod.specs
  166. assert_nil prod.description
  167. end
  168. # test creating updating
  169. def test_create_update
  170. prod = Product.create(:code => "new-product",
  171. :specs => "These are the product specs")
  172. assert prod.errors.empty?, prod.errors.full_messages.first
  173. prod = nil
  174. prod = Product.find_by_code("new-product")
  175. assert_not_nil prod
  176. assert_equal "These are the product specs", prod.specs
  177. prod.specs = "Dummy"
  178. prod.save
  179. prod = nil
  180. prod = Product.find_by_code("new-product")
  181. assert_not_nil prod
  182. assert_equal "Dummy", prod.specs
  183. end
  184. def test_include_translated
  185. Globalize::Locale.set("he-IL")
  186. prods = Product.find(:all, :include_translated => :manufacturer)
  187. assert_equal 5, prods.size
  188. assert_equal "?????", prods.first.manufacturer_name
  189. assert_equal "?????", prods.last.manufacturer_name
  190. Globalize::Locale.set("en-US")
  191. prods = Product.find(:all, :include_translated => :manufacturer)
  192. assert_equal 5, prods.size
  193. assert_equal "Reverend", prods.first.manufacturer_name
  194. assert_equal "Reverend", prods.last.manufacturer_name
  195. end
  196. # Doesn't pull in translations
  197. def test_include
  198. prods = Product.find(:all, :include => :manufacturer)
  199. assert_equal 5, prods.size
  200. assert_equal "first-mfr", prods.first.manufacturer.code
  201. end
  202. def test_order_en
  203. prods = Product.find(:all, :order => "name").select {|rec| rec.name}
  204. assert_equal 5, prods[0].id
  205. assert_equal 3, prods[1].id
  206. assert_equal 4, prods[2].id
  207. end
  208. def test_order_he
  209. Globalize::Locale.set("he-IL")
  210. prods = Product.find(:all, :order => "name").select {|rec| rec.name}
  211. assert_equal 4, prods[1].id
  212. assert_equal 5, prods[2].id
  213. assert_equal 3, prods[3].id
  214. end
  215. def test_base_translation_create
  216. prod = Product.create!(:code => 'test-base', :name => 'english test')
  217. prod.reload
  218. assert_equal 'english test', prod.name
  219. Globalize::Locale.set("he-IL")
  220. prod = Product.find_by_code('test-base')
  221. assert_equal 'english test', prod.name
  222. prod.name = "hebrew test"
  223. prod.save!
  224. prod.reload
  225. assert_equal 'hebrew test', prod.name
  226. # delete hebrew version and test if it reverts to english base
  227. prod.name = nil
  228. assert_nil prod.name
  229. prod.save!
  230. prod.reload
  231. assert_equal 'english test', prod.name
  232. # change base and see if hebrew gets updated
  233. Globalize::Locale.set("en-US")
  234. prod.reload
  235. prod.name = "english test two"
  236. prod.save!
  237. prod.reload
  238. assert_equal "english test two", prod.name
  239. Globalize::Locale.set("he-IL")
  240. prod.reload
  241. assert_equal "english test two", prod.name
  242. end
  243. def test_wrong_language
  244. prod = Product.find(1)
  245. Globalize::Locale.set("he-IL")
  246. assert_raise(Globalize::WrongLanguageError) { prod.description }
  247. assert_raise(Globalize::WrongLanguageError) { prod.description = "??? ????? ????? ????" }
  248. assert_raise(Globalize::WrongLanguageError) { prod.save! }
  249. prod = Product.find(1)
  250. assert_equal "??? ????? ????? ??????", prod.description
  251. Globalize::Locale.set("en-US")
  252. assert_raise(Globalize::WrongLanguageError) { prod.description }
  253. assert_raise(Globalize::WrongLanguageError) { prod.save! }
  254. end
  255. def test_destroy
  256. prod = Product.find(1)
  257. tr = Globalize::ModelTranslation.find(:first, :conditions => [
  258. "table_name = ? AND item_id = ? AND facet = ? AND language_id = ?",
  259. "globalize_products", 1, "description", 2 ])
  260. assert_not_nil tr
  261. prod.destroy
  262. tr = Globalize::ModelTranslation.find(:first, :conditions => [
  263. "table_name = ? AND item_id = ? AND facet = ? AND language_id = ?",
  264. "globalize_products", 1, "description", 2 ])
  265. assert_nil tr
  266. end
  267. def test_destroy_class_method
  268. tr = Globalize::ModelTranslation.find(:first, :conditions => [
  269. "table_name = ? AND item_id = ? AND facet = ? AND language_id = ?",
  270. "globalize_products", 1, "description", 2 ])
  271. assert_not_nil tr
  272. Product.destroy(1)
  273. tr = Globalize::ModelTranslation.find(:first, :conditions => [
  274. "table_name = ? AND item_id = ? AND facet = ? AND language_id = ?",
  275. "globalize_products", 1, "description", 2 ])
  276. assert_nil tr
  277. end
  278. def test_fix_conditions
  279. assert_equal 'globalize_products.name="test"',
  280. Product.class_eval { fix_conditions('name="test"') }
  281. assert_equal '(globalize_products.name="test" OR globalize_products.name = "test2")',
  282. Product.class_eval { fix_conditions('(name="test" OR name = "test2")') }
  283. assert_equal 'globalize_products.name = globalize_translations.name',
  284. Product.class_eval { fix_conditions('globalize_products.name = globalize_translations.name') }
  285. assert_equal ' globalize_products.name = globalize_translations.name',
  286. Product.class_eval { fix_conditions(' name = globalize_translations.name') }
  287. assert_equal ' globalize_products."name" = globalize_translations.name',
  288. Product.class_eval { fix_conditions(' "name" = globalize_translations.name') }
  289. assert_equal ' globalize_products.\'name\' = globalize_translations.name',
  290. Product.class_eval { fix_conditions(' \'name\' = globalize_translations.name') }
  291. assert_equal ' globalize_products.`name` = globalize_translations.name',
  292. Product.class_eval { fix_conditions(' `name` = globalize_translations.name') }
  293. end
  294. def test_native_name
  295. heb = Globalize::Language.pick('he')
  296. assert_equal 'Hebrew', heb.english_name
  297. assert_equal '?????', heb.native_name
  298. urdu = Globalize::Language.pick('ur')
  299. assert_equal 'Urdu', urdu.english_name
  300. assert_equal 'Urdu', urdu.native_name
  301. end
  302. def test_returned_base
  303. Globalize::Locale.set("he-IL")
  304. prod = Product.find(1)
  305. assert_equal "first-product", prod.code
  306. assert_equal "these are the specs for the first product",
  307. prod.specs
  308. assert_equal "??? ????? ????? ??????",
  309. prod.description
  310. assert prod.specs_is_base?
  311. assert !prod.description_is_base?
  312. assert_equal 'ltr', prod.specs.direction
  313. assert_equal 'rtl', prod.description.direction
  314. end
  315. def test_bidi_embed
  316. Globalize::Locale.set("he-IL")
  317. prod = Product.find(2)
  318. assert_equal "\xe2\x80\xaaThis is a description of the second product\xe2\x80\xac",
  319. prod.description
  320. end
  321. # association building/creating?
  322. end