route_translator 3.2.4 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ #encoding: utf-8
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ class RoutingTest < integration_test_suite_parent_class
5
+
6
+ include RouteTranslator::ConfigurationHelper
7
+
8
+ def test_set_locale_from_params
9
+ config_default_locale_settings 'en'
10
+
11
+ get '/es/dummy'
12
+ assert_equal 'es', @response.body
13
+ assert_response :success
14
+ end
15
+
16
+ def test_mounted_apps_work_with_correct_path
17
+ get '/dummy_mounted_app'
18
+ assert_equal "Good", @response.body
19
+ assert_response :success
20
+ end
21
+
22
+ end
@@ -0,0 +1,16 @@
1
+ #encoding: utf-8
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ class ThreadSafetyTest < integration_test_suite_parent_class
5
+
6
+ include RouteTranslator::ConfigurationHelper
7
+
8
+ def test_i18n_locale_thread_safe
9
+ config_default_locale_settings 'en'
10
+ get '/es/dummy'
11
+ assert_equal 'es', @response.body
12
+
13
+ assert_equal :en, I18n.locale
14
+ end
15
+
16
+ end
data/test/routing_test.rb CHANGED
@@ -1,31 +1,29 @@
1
1
  #encoding: utf-8
2
- require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+ require File.expand_path('../test_helper', __FILE__)
3
3
 
4
4
  class PeopleController < ActionController::Base; end
5
5
  class ProductsController < ActionController::Base; end
6
6
 
7
7
  class TranslateRoutesTest < ActionController::TestCase
8
+
8
9
  include ActionDispatch::Assertions::RoutingAssertions
9
- include RouteTranslator::TestHelper
10
+ include RouteTranslator::AssertionHelper
11
+ include RouteTranslator::ConfigurationHelper
12
+ include RouteTranslator::I18nHelper
13
+ include RouteTranslator::RoutesHelper
10
14
 
11
15
  def setup
16
+ setup_config
17
+ setup_i18n
12
18
  @routes = ActionDispatch::Routing::RouteSet.new
13
- I18n.backend = I18n::Backend::Simple.new
14
- I18n.load_path = [ File.expand_path('../locales/routes.yml', __FILE__) ]
15
- I18n.reload!
16
19
  end
17
20
 
18
21
  def teardown
19
- config_force_locale false
20
- config_hide_locale false
21
- config_generate_unlocalized_routes false
22
- config_default_locale_settings("en")
23
- config_generate_unnamed_unlocalized_routes false
24
- config_host_locales({})
22
+ teardown_i18n
23
+ teardown_config
25
24
  end
26
25
 
27
26
  def test_unnamed_root_route
28
- config_default_locale_settings 'en'
29
27
  draw_routes do
30
28
  localized do
31
29
  root :to => 'people#index'
@@ -141,8 +139,6 @@ class TranslateRoutesTest < ActionController::TestCase
141
139
  end
142
140
 
143
141
  def test_unnamed_untranslated_route
144
- config_default_locale_settings 'en'
145
-
146
142
  draw_routes do
147
143
  localized do
148
144
  get 'foo', :to => 'people#index'
@@ -169,7 +165,6 @@ class TranslateRoutesTest < ActionController::TestCase
169
165
  end
170
166
 
171
167
  def test_unnamed_translated_route_on_non_default_locale
172
- config_default_locale_settings 'en'
173
168
  draw_routes do
174
169
  localized do
175
170
  get 'people', :to => 'people#index'
@@ -181,8 +176,6 @@ class TranslateRoutesTest < ActionController::TestCase
181
176
  end
182
177
 
183
178
  def test_named_translated_route_with_prefix_must_have_locale_as_static_segment
184
- config_default_locale_settings 'en'
185
-
186
179
  draw_routes do
187
180
  localized do
188
181
  get 'people', :to => 'people#index'
@@ -259,12 +252,9 @@ class TranslateRoutesTest < ActionController::TestCase
259
252
  end
260
253
  end
261
254
 
262
- config_default_locale_settings 'en'
263
-
264
255
  assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
265
256
  assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
266
257
 
267
-
268
258
  assert_helpers_include :people_en, :people_es, :people
269
259
  end
270
260
 
@@ -309,8 +299,6 @@ class TranslateRoutesTest < ActionController::TestCase
309
299
  end
310
300
 
311
301
  def test_i18n_based_translations_setting_locales
312
- config_default_locale_settings 'en'
313
-
314
302
  draw_routes do
315
303
  localized do
316
304
  get 'people', :to => 'people#index', :as => 'people'
@@ -325,21 +313,24 @@ class TranslateRoutesTest < ActionController::TestCase
325
313
  end
326
314
 
327
315
  def test_translations_depend_on_available_locales
328
- config_default_locale_settings 'en'
316
+ available_locales = I18n.available_locales
317
+ begin
318
+ I18n.available_locales = [:es, :en, :fr]
329
319
 
330
- I18n.stub(:available_locales, [:es, :en, :fr]) do
331
320
  draw_routes do
332
321
  localized do
333
322
  get 'people', :to => 'people#index', :as => 'people'
334
323
  end
335
324
  end
336
- end
337
325
 
338
- assert_routing '/fr/people', :controller => 'people', :action => 'index', :locale => 'fr'
339
- assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
340
- assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
326
+ assert_routing '/fr/people', :controller => 'people', :action => 'index', :locale => 'fr'
327
+ assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
328
+ assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
341
329
 
342
- assert_helpers_include :people_fr, :people_en, :people_es, :people
330
+ assert_helpers_include :people_fr, :people_en, :people_es, :people
331
+ ensure
332
+ I18n.available_locales = available_locales
333
+ end
343
334
  end
344
335
 
345
336
  def test_2_localized_blocks
@@ -371,8 +362,6 @@ class TranslateRoutesTest < ActionController::TestCase
371
362
  end
372
363
 
373
364
  def test_force_locale
374
- I18n.locale = 'en'
375
- config_default_locale_settings 'en'
376
365
  config_force_locale true
377
366
 
378
367
  draw_routes do
@@ -385,14 +374,13 @@ class TranslateRoutesTest < ActionController::TestCase
385
374
  assert_unrecognized_route '/people', :controller => 'people', :action => 'index'
386
375
  assert_equal '/en/people', @routes.url_helpers.people_en_path
387
376
  assert_equal '/en/people', @routes.url_helpers.people_path
388
- I18n.locale = 'es'
389
- # The dynamic route maps to the current locale
390
- assert_equal '/es/gente', @routes.url_helpers.people_path
377
+ I18n.with_locale :es do
378
+ # The dynamic route maps to the current locale
379
+ assert_equal '/es/gente', @routes.url_helpers.people_path
380
+ end
391
381
  end
392
382
 
393
383
  def test_generate_unlocalized_routes
394
- I18n.locale = 'en'
395
- config_default_locale_settings 'en'
396
384
  config_generate_unlocalized_routes true
397
385
 
398
386
  draw_routes do
@@ -405,14 +393,14 @@ class TranslateRoutesTest < ActionController::TestCase
405
393
  assert_routing '/people', :controller => 'people', :action => 'index'
406
394
  assert_equal '/en/people', @routes.url_helpers.people_en_path
407
395
  assert_equal '/people', @routes.url_helpers.people_path
408
- I18n.locale = 'es'
409
- # The dynamic route maps to the default locale, not the current
410
- assert_equal '/people', @routes.url_helpers.people_path
396
+
397
+ I18n.with_locale :es do
398
+ # The dynamic route maps to the default locale, not the current
399
+ assert_equal '/people', @routes.url_helpers.people_path
400
+ end
411
401
  end
412
402
 
413
403
  def test_generate_unnamed_unlocalized_routes
414
- I18n.locale = 'en'
415
- config_default_locale_settings 'en'
416
404
  config_generate_unnamed_unlocalized_routes true
417
405
 
418
406
  draw_routes do
@@ -426,28 +414,48 @@ class TranslateRoutesTest < ActionController::TestCase
426
414
  assert_equal '/en/people', @routes.url_helpers.people_en_path
427
415
  assert_equal '/en/people', @routes.url_helpers.people_path
428
416
 
429
- I18n.locale = 'es'
430
- # The dynamic route maps to the current locale
431
- assert_equal '/es/gente', @routes.url_helpers.people_path
417
+ I18n.with_locale :es do
418
+ # The dynamic route maps to the current locale
419
+ assert_equal '/es/gente', @routes.url_helpers.people_path
420
+ end
432
421
  end
433
422
 
434
423
  def test_blank_localized_routes
435
- I18n.locale = 'en'
436
- config_default_locale_settings 'en'
437
-
438
424
  draw_routes do
439
425
  localized do
440
426
  get 'people/blank', :to => 'people#index', :as => 'people'
441
427
  end
442
428
  end
443
429
 
444
- I18n.locale = 'en'
445
- assert_routing '/people/blank', :controller => 'people', :action => 'index', :locale => 'en'
446
- assert_equal '/people/blank', @routes.url_helpers.people_en_path
430
+ I18n.with_locale :en do
431
+ assert_routing '/people/blank', :controller => 'people', :action => 'index', :locale => 'en'
432
+ assert_equal '/people/blank', @routes.url_helpers.people_en_path
433
+ end
447
434
 
448
- I18n.locale = 'es'
449
- assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
450
- assert_equal '/es/gente', @routes.url_helpers.people_es_path
435
+ I18n.with_locale :es do
436
+ assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
437
+ assert_equal '/es/gente', @routes.url_helpers.people_es_path
438
+ end
439
+ end
440
+
441
+ def test_path_helper_arguments
442
+ config_default_locale_settings 'es'
443
+ I18n.with_locale :es do
444
+ config_host_locales({ '*.es' => 'es', '*.com' => 'en' })
445
+
446
+ draw_routes do
447
+ localized do
448
+ resources :products
449
+ end
450
+ end
451
+
452
+ assert_equal '/productos', @routes.url_helpers.products_path
453
+ assert_equal '/productos/some_product', @routes.url_helpers.product_path('some_product')
454
+ assert_equal '/productos/some_product?some=param', @routes.url_helpers.product_path('some_product', :some => 'param')
455
+ assert_equal '/en/products', @routes.url_helpers.products_path(:locale => 'en')
456
+ assert_equal '/en/products/some_product', @routes.url_helpers.product_path('some_product', :locale => 'en')
457
+ assert_equal '/en/products/some_product?some=param', @routes.url_helpers.product_path('some_product', :locale => 'en', :some => 'param')
458
+ end
451
459
  end
452
460
 
453
461
  def test_dont_add_locale_to_routes_if_local_param_present
@@ -467,7 +475,6 @@ class TranslateRoutesTest < ActionController::TestCase
467
475
  end
468
476
 
469
477
  def test_config_hide_locale
470
- config_default_locale_settings 'en'
471
478
  config_hide_locale true
472
479
 
473
480
  draw_routes do
@@ -510,17 +517,40 @@ class TranslateRoutesTest < ActionController::TestCase
510
517
  def test_action_view_gets_locale_suffix_helper
511
518
  ActionView::Base.instance_methods.include?('locale_suffix')
512
519
  end
520
+
521
+ # See https://github.com/enriclluelles/route_translator/issues/69
522
+ def test_no_side_effects
523
+ draw_routes do
524
+ localized do
525
+ resources :people
526
+ end
527
+
528
+ scope "(:locale)", :locale => /(en|es)/ do
529
+ get '*id' => 'products#show', :as => 'product'
530
+ end
531
+ end
532
+
533
+ assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
534
+ assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
535
+
536
+ assert_routing '/es/path/to/a/product', :controller => 'products', :action => 'show', :locale => 'es', :id => 'path/to/a/product'
537
+ assert_routing '/path/to/another/product', :controller => 'products', :action => 'show', :id => 'path/to/another/product'
538
+ end
539
+
513
540
  end
514
541
 
515
542
  class ProductsControllerTest < ActionController::TestCase
543
+
544
+ include RouteTranslator::ConfigurationHelper
545
+ include RouteTranslator::I18nHelper
516
546
  include ActionDispatch::Assertions::RoutingAssertions
517
- include RouteTranslator::TestHelper
547
+ include RouteTranslator::RoutesHelper
518
548
 
519
549
  def setup
550
+ setup_config
551
+ setup_i18n
552
+
520
553
  @routes = ActionDispatch::Routing::RouteSet.new
521
- I18n.backend = I18n::Backend::Simple.new
522
- I18n.load_path = [ File.expand_path('../locales/routes.yml', __FILE__) ]
523
- I18n.reload!
524
554
 
525
555
  config_default_locale_settings 'es'
526
556
  config_host_locales({:es => 'es'})
@@ -533,19 +563,17 @@ class ProductsControllerTest < ActionController::TestCase
533
563
  end
534
564
 
535
565
  def teardown
536
- config_force_locale false
537
- config_hide_locale false
538
- config_generate_unlocalized_routes false
539
- config_default_locale_settings("en")
540
- config_generate_unnamed_unlocalized_routes false
541
- config_host_locales({})
566
+ teardown_i18n
567
+ teardown_config
542
568
  end
543
569
 
544
570
  def test_url_helpers_are_included
545
571
  #doing it this way because assert_nothing_raised doesn't work on all rails versions
572
+ controller = ProductsController.new
573
+ controller.request = OpenStruct.new(:host => 'example.com') # mocking request
546
574
  %w(product_path product_url product_es_path product_es_url product_native_es_path product_native_es_url).each do |method|
547
575
  begin
548
- send(method)
576
+ controller.send(method)
549
577
  rescue Exception => e
550
578
  raise e if e.is_a?(NameError) #swallow anything that isn't a NameError
551
579
  end
@@ -0,0 +1,27 @@
1
+
2
+ module RouteTranslator
3
+ module AssertionHelper
4
+
5
+ def assert_helpers_include(*helpers)
6
+ controller = ActionController::Base.new
7
+ view = ActionView::Base.new
8
+ helpers.each do |helper|
9
+ ['url', 'path'].each do |suffix|
10
+ [controller, view].each { |obj| assert_respond_to obj, "#{helper}_#{suffix}".to_sym }
11
+ end
12
+ end
13
+ end
14
+
15
+ # Hack for compatibility between Rails 4 and Rails 3
16
+ def assert_unrecognized_route(route_path, options)
17
+ assert_raise ActionController::RoutingError do
18
+ begin
19
+ assert_routing route_path, options
20
+ rescue Minitest::Assertion => m
21
+ raise ActionController::RoutingError.new("")
22
+ end
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,51 @@
1
+
2
+ module RouteTranslator
3
+ module ConfigurationHelper
4
+
5
+ def config_reset
6
+ config_force_locale false
7
+ config_hide_locale false
8
+ config_generate_unlocalized_routes false
9
+ config_generate_unnamed_unlocalized_routes false
10
+ config_host_locales(host_locales_config_hash)
11
+
12
+ config_default_locale_settings(:en)
13
+ end
14
+
15
+ alias :setup_config :config_reset
16
+ alias :teardown_config :config_reset
17
+
18
+ def config_default_locale_settings(locale)
19
+ I18n.default_locale = locale
20
+ end
21
+
22
+ def config_force_locale(boolean)
23
+ RouteTranslator.config.force_locale = boolean
24
+ end
25
+
26
+ def config_hide_locale(boolean)
27
+ RouteTranslator.config.hide_locale = boolean
28
+ end
29
+
30
+ def config_generate_unlocalized_routes(boolean)
31
+ RouteTranslator.config.generate_unlocalized_routes = boolean
32
+ end
33
+
34
+ def config_generate_unnamed_unlocalized_routes(boolean)
35
+ RouteTranslator.config.generate_unnamed_unlocalized_routes = boolean
36
+ end
37
+
38
+ def config_host_locales(hash)
39
+ RouteTranslator.config.host_locales = hash
40
+ end
41
+
42
+ def host_locales_config_hash
43
+ if RUBY_VERSION < '1.9'
44
+ ::ActiveSupport::OrderedHash.new
45
+ else
46
+ ::Hash.new
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,24 @@
1
+
2
+ module RouteTranslator
3
+ module I18nHelper
4
+
5
+ def setup_i18n
6
+ @i18n_backend = I18n.backend
7
+ @i18n_load_path = I18n.load_path
8
+
9
+ I18n.locale = I18n.default_locale
10
+ I18n.backend = I18n::Backend::Simple.new
11
+ I18n.load_path = [ File.expand_path('../../locales/routes.yml', __FILE__) ]
12
+
13
+ I18n.reload!
14
+ end
15
+
16
+ def teardown_i18n
17
+ I18n.backend = @i18n_backend
18
+ I18n.load_path = @i18n_load_path
19
+
20
+ I18n.reload!
21
+ end
22
+
23
+ end
24
+ end