aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2013-07-18 15:33:05 +0100
committerLouise Crow <louise.crow@gmail.com>2013-07-18 17:43:11 +0100
commit9fe48c5b9db1c0ba6c1a59f0d092c88e5cae1aa0 (patch)
tree6a244ebe1422fa9fe627a7b4eebf2055adc6016e
parent6d67f4df65f8b56c9d54d71d449d46b1c6c92be2 (diff)
Switch routing-filter (which takes locale out of the params and puts it in the URL) off by default in model, controller, and helper tests. This means we can supply the locale as a param. Turn it on specifically for a couple of controller tests that test routing, and change other url localization tests into integration tests.
-rw-r--r--spec/controllers/general_controller_spec.rb88
-rw-r--r--spec/controllers/help_controller_spec.rb6
-rw-r--r--spec/controllers/public_body_controller_spec.rb24
-rw-r--r--spec/controllers/request_controller_spec.rb4
-rw-r--r--spec/controllers/track_controller_spec.rb5
-rw-r--r--spec/controllers/user_controller_spec.rb19
-rw-r--r--spec/helpers/link_to_helper_spec.rb5
-rw-r--r--spec/integration/localisation_spec.rb96
-rw-r--r--spec/spec_helper.rb10
9 files changed, 113 insertions, 144 deletions
diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb
index 0eda73c51..8c86ad0be 100644
--- a/spec/controllers/general_controller_spec.rb
+++ b/spec/controllers/general_controller_spec.rb
@@ -108,97 +108,15 @@ describe GeneralController, "when showing the frontpage" do
response.should be_success
end
- describe 'when there is more than one locale' do
-
- describe 'when using the default locale' do
-
- before do
- @default_lang_home_link = /href=".*\/en\//
- @other_lang_home_link = /href=".*\/es\//
- @old_include_default_locale_in_urls = AlaveteliConfiguration::include_default_locale_in_urls
- end
-
- def set_default_locale_in_urls(value)
- AlaveteliConfiguration.stub!(:include_default_locale_in_urls).and_return(value)
- load Rails.root.join("config/initializers/fast_gettext.rb")
- end
-
- describe 'when the config value INCLUDE_DEFAULT_LOCALE_IN_URLS is false' do
-
- before do
- set_default_locale_in_urls(false)
- end
-
- it 'should generate URLs without a locale prepended' do
- get :frontpage
- response.should_not contain @default_lang_home_link
- end
-
- it 'should render the front page in the default language when no locale param
- is present and the session locale is not the default' do
- get(:frontpage, {}, {:locale => 'es'})
- response.should_not contain @other_lang_home_link
- end
- end
-
- it 'should generate URLs with a locale prepended when the config value
- INCLUDE_DEFAULT_LOCALE_IN_URLS is true' do
- set_default_locale_in_urls(true)
- get :frontpage
- response.body.should match /#{@default_lang_home_link}/
- end
-
- after do
- set_default_locale_in_urls(@old_include_default_locale_in_urls)
- end
-
- end
- end
-
-
- describe "when using different locale settings" do
- home_link_regex = /href=".*\/en\//
-
- it "should generate URLs with a locale prepended when there's more than one locale set" do
- get :frontpage
- response.body.should match home_link_regex
- end
+ describe 'when using locales' do
it "should use our test PO files rather than the application one" do
- I18n.default_locale = :es
- get :frontpage
+ get :frontpage, :locale => 'es'
response.body.should match /XOXO/
- I18n.default_locale = :en
- end
-
- it "should generate URLs that include the locale when using one that includes an underscore" do
- I18n.default_locale = :"en_GB"
- get :frontpage
- response.body.should match /href="\/en_GB\//
- I18n.default_locale = :en
- end
-
- it "should fall back to the language if the territory is unknown" do
- I18n.default_locale = :"en_US"
- get :frontpage
- response.body.should match /href="\/en\//
- response.body.should_not match /href="\/en_US\//
- I18n.default_locale = :en
- end
-
- it "should generate URLs without a locale prepended when there's only one locale set" do
- old_fgt_available_locales = FastGettext.default_available_locales
- old_i18n_available_locales = I18n.available_locales
- FastGettext.default_available_locales = I18n.available_locales = ['en']
-
- get :frontpage
- response.should_not contain home_link_regex
-
- FastGettext.default_available_locales = old_fgt_available_locales
- I18n.available_locales = old_i18n_available_locales
end
end
+
end
describe GeneralController, "when showing the front page with fixture data" do
diff --git a/spec/controllers/help_controller_spec.rb b/spec/controllers/help_controller_spec.rb
index c47e1abd3..cc024f840 100644
--- a/spec/controllers/help_controller_spec.rb
+++ b/spec/controllers/help_controller_spec.rb
@@ -31,17 +31,11 @@ describe HelpController, "when using help" do
describe 'when requesting a page in a supported locale ' do
before do
- # Allow us to supply the locale manually
- RoutingFilter.active = false
# Prepend our fixture templates
fixture_theme_path = File.join(Rails.root, 'spec', 'fixtures', 'theme_views', 'theme_one')
controller.prepend_view_path fixture_theme_path
end
- after do
- RoutingFilter.active = true
- end
-
it 'should render the locale-specific template if available' do
get :contact, {:locale => 'es'}
response.body.should match('contáctenos theme one')
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb
index e01bcb0a6..68e02e000 100644
--- a/spec/controllers/public_body_controller_spec.rb
+++ b/spec/controllers/public_body_controller_spec.rb
@@ -43,28 +43,14 @@ describe PublicBodyController, "when showing a body" do
:conditions => ["public_body_id = ?", public_bodies(:humpadink_public_body).id])
end
- it "should redirect to the canonical name in the chosen locale" do
- get :show, {:url_name => "dfh", :view => 'all', :show_locale => "es"}
- response.should redirect_to "http://test.host/es/body/edfh"
- end
-
- it "should assign the body using same locale as that used in url_name" do
- get :show, {:url_name => "edfh", :view => 'all', :show_locale => "es"}
+ it "should display the body using same locale as that used in url_name" do
+ get :show, {:url_name => "edfh", :view => 'all', :locale => "es"}
response.should contain("Baguette")
end
it "should redirect use to the relevant locale even when url_name is for a different locale" do
- RoutingFilter.active = false
-
get :show, {:url_name => "edfh", :view => 'all'}
response.should redirect_to "http://test.host/body/dfh"
-
- RoutingFilter.active = true
- end
-
- it "should remember the filter (view) setting on redirecting" do
- get :show, :show_locale => "es", :url_name => "tgq", :view => 'successful'
- response.should redirect_to 'http://test.host/es/body/etgq/successful'
end
it "should redirect to newest name if you use historic name of public body in URL" do
@@ -95,10 +81,8 @@ describe PublicBodyController, "when listing bodies" do
:last_edit_comment => '')
@english_only.save
end
- I18n.with_locale(:es) do
- get :list
- assigns[:public_bodies].include?(@english_only).should == true
- end
+ get :list, {:locale => 'es'}
+ assigns[:public_bodies].include?(@english_only).should == true
end
it "should list bodies in alphabetical order" do
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb
index c73576c50..73d1849ea 100644
--- a/spec/controllers/request_controller_spec.rb
+++ b/spec/controllers/request_controller_spec.rb
@@ -1645,10 +1645,6 @@ describe RequestController, "when classifying an information request" do
@dog_request = info_requests(:fancy_dog_request)
@dog_request.stub!(:each).and_return([@dog_request])
InfoRequest.stub!(:find).and_return(@dog_request)
- RoutingFilter.active = false
- end
- after do
- RoutingFilter.active = true
end
def request_url
diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb
index 1575bc84e..a16024828 100644
--- a/spec/controllers/track_controller_spec.rb
+++ b/spec/controllers/track_controller_spec.rb
@@ -64,8 +64,6 @@ describe TrackController, "when sending alerts for a track" do
end
it "should send alerts" do
- # Don't do clever locale-insertion-unto-URL stuff
- RoutingFilter.active = false
# set the time the comment event happened at to within the last week
ire = info_request_events(:silly_comment_event)
@@ -111,9 +109,6 @@ describe TrackController, "when sending alerts for a track" do
TrackMailer.alert_tracks
deliveries = ActionMailer::Base.deliveries
deliveries.size.should == 0
-
- # Restore the routing filters
- RoutingFilter.active = true
end
it "should send localised alerts" do
diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb
index b09594b9c..fe95aa31c 100644
--- a/spec/controllers/user_controller_spec.rb
+++ b/spec/controllers/user_controller_spec.rb
@@ -105,8 +105,6 @@ describe UserController, "when signing in" do
end
it "should log in when you give right email/password, and redirect to where you were" do
- RoutingFilter.active = false
-
get :signin, :r => "/list"
response.should render_template('sign')
post_redirect = get_last_postredirect
@@ -117,13 +115,9 @@ describe UserController, "when signing in" do
# response doesn't contain /en/ but redirect_to does...
response.should redirect_to(:controller => 'request', :action => 'list', :post_redirect => 1)
ActionMailer::Base.deliveries.should be_empty
-
- RoutingFilter.active = true
end
it "should not log you in if you use an invalid PostRedirect token, and shouldn't give 500 error either" do
- RoutingFilter.active = false
-
post_redirect = "something invalid"
lambda {
post :signin, { :user_signin => { :email => 'bob@localhost', :password => 'jonespassword' },
@@ -134,8 +128,6 @@ describe UserController, "when signing in" do
:token => post_redirect }
response.should render_template('sign')
assigns[:post_redirect].should == nil
-
- RoutingFilter.active = true
end
# No idea how to test this in the test framework :(
@@ -159,8 +151,6 @@ describe UserController, "when signing in" do
end
it "should confirm your email, log you in and redirect you to where you were after you click an email link" do
- RoutingFilter.active = false
-
get :signin, :r => "/list"
post_redirect = get_last_postredirect
@@ -186,13 +176,9 @@ describe UserController, "when signing in" do
get :confirm, :email_token => post_redirect.email_token
session[:user_id].should == users(:unconfirmed_user).id
response.should redirect_to(:controller => 'request', :action => 'list', :post_redirect => 1)
-
- RoutingFilter.active = true
end
it "should keep you logged in if you click a confirmation link and are already logged in as an admin" do
- RoutingFilter.active = false
-
get :signin, :r => "/list"
post_redirect = get_last_postredirect
@@ -223,7 +209,6 @@ describe UserController, "when signing in" do
# And the redirect should still work, of course
response.should redirect_to(:controller => 'request', :action => 'list', :post_redirect => 1)
- RoutingFilter.active = true
end
end
@@ -301,14 +286,10 @@ describe UserController, "when signing out" do
end
it "should log you out and redirect you to where you were" do
- RoutingFilter.active = false
-
session[:user_id] = users(:bob_smith_user).id
get :signout, :r => '/list'
session[:user_id].should be_nil
response.should redirect_to(:controller => 'request', :action => 'list')
-
- RoutingFilter.active = true
end
end
diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb
index 4cc1d415b..b29419ef3 100644
--- a/spec/helpers/link_to_helper_spec.rb
+++ b/spec/helpers/link_to_helper_spec.rb
@@ -8,12 +8,7 @@ describe LinkToHelper do
before do
@mock_request = mock_model(InfoRequest, :url_title => 'test_title')
- RoutingFilter.active = false
end
- after do
- RoutingFilter.active = true
- end
-
it 'should return a path like /request/test_title' do
request_path(@mock_request).should == '/request/test_title'
diff --git a/spec/integration/localisation_spec.rb b/spec/integration/localisation_spec.rb
new file mode 100644
index 000000000..140f9f785
--- /dev/null
+++ b/spec/integration/localisation_spec.rb
@@ -0,0 +1,96 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe "when generating urls" do
+
+ before do
+ @home_link_regex = /href=".*\/en\//
+ end
+
+ it "should generate URLs that include the locale when using one that includes an underscore" do
+ AlaveteliConfiguration.stub!(:available_locales).and_return('en_GB')
+ get('/en_GB')
+ response.body.should match /href="\/en_GB\//
+ end
+
+ it "should fall back to the language if the territory is unknown" do
+ AlaveteliConfiguration.stub!(:available_locales).and_return('es en')
+ AlaveteliConfiguration.stub!(:default_locale).and_return('es')
+ get('/', {}, {'HTTP_ACCEPT_LANGUAGE' => 'en_US'})
+ response.body.should match /href="\/en\//
+ response.body.should_not match /href="\/en_US\//
+ end
+
+ it "should generate URLs without a locale prepended when there's only one locale set" do
+ AlaveteliConfiguration.stub!(:available_locales).and_return('en')
+ get('/')
+ response.should_not contain @home_link_regex
+ end
+
+ it 'should redirect requests for a public body in a locale to the canonical name in that locale' do
+ get('/es/body/dfh')
+ response.should redirect_to "/es/body/edfh"
+ end
+
+ it 'should remember a filter view when redirecting a public body request to the canonical name' do
+ get('/es/body/tgq/successful')
+ response.should redirect_to "/es/body/etgq/successful"
+ end
+
+ describe 'when there is more than one locale' do
+
+ before do
+ AlaveteliConfiguration.stub!(:available_locales).and_return('en es')
+ AlaveteliConfiguration.stub!(:default_locale).and_return('en')
+ end
+
+ it "should generate URLs with a locale prepended when there's more than one locale set" do
+ get('/')
+ response.body.should match @home_link_regex
+ end
+
+ describe 'when using the default locale' do
+
+ before do
+ @default_lang_home_link = /href=".*\/en\//
+ @other_lang_home_link = /href=".*\/es\//
+ @old_include_default_locale_in_urls = AlaveteliConfiguration::include_default_locale_in_urls
+ end
+
+ def set_default_locale_in_urls(value)
+ AlaveteliConfiguration.stub!(:include_default_locale_in_urls).and_return(value)
+ load Rails.root.join("config/initializers/fast_gettext.rb")
+ end
+
+ describe 'when the config value INCLUDE_DEFAULT_LOCALE_IN_URLS is false' do
+
+ before do
+ set_default_locale_in_urls(false)
+ end
+
+ it 'should generate URLs without a locale prepended' do
+ get '/'
+ response.should_not contain @default_lang_home_link
+ end
+
+ it 'should render the front page in the default language when no locale param
+ is present and the session locale is not the default' do
+ get('/', {:locale => 'es'})
+ response.should_not contain @other_lang_home_link
+ end
+ end
+
+ it 'should generate URLs with a locale prepended when the config value
+ INCLUDE_DEFAULT_LOCALE_IN_URLS is true' do
+ set_default_locale_in_urls(true)
+ get '/'
+ response.body.should match /#{@default_lang_home_link}/
+ end
+
+ after do
+ set_default_locale_in_urls(@old_include_default_locale_in_urls)
+ end
+
+ end
+ end
+
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index a3b06cea8..c7fb0af75 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -96,6 +96,16 @@ Spork.prefork do
I18n.locale = @save_i18n_locale
end
+ # Turn routing-filter off in functional and unit tests as per
+ # https://github.com/svenfuchs/routing-filter/blob/master/README.markdown#testing
+ config.before(:each) do
+ RoutingFilter.active = false if [:controller, :helper, :model].include? example.metadata[:type]
+ end
+
+ config.after(:each) do
+ RoutingFilter.active = true if [:controller, :helper, :model].include? example.metadata[:type]
+ end
+
# This section makes the garbage collector run less often to speed up tests
last_gc_run = Time.now