diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 88 | ||||
-rw-r--r-- | spec/controllers/help_controller_spec.rb | 6 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 40 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 4 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 5 | ||||
-rw-r--r-- | spec/controllers/user_controller_spec.rb | 19 | ||||
-rw-r--r-- | spec/fixtures/public_body_translations.yml | 14 | ||||
-rw-r--r-- | spec/helpers/link_to_helper_spec.rb | 5 | ||||
-rw-r--r-- | spec/integration/localisation_spec.rb | 88 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 5 | ||||
-rw-r--r-- | spec/spec_helper.rb | 14 |
13 files changed, 143 insertions, 148 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..4e1841164 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -43,28 +43,20 @@ 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" + 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 assign the body using same locale as that used in url_name" do - get :show, {:url_name => "edfh", :view => 'all', :show_locale => "es"} - response.should contain("Baguette") + it 'should show public body names in the selected locale language if present for a locale with underscores' do + AlaveteliLocalization.set_locales('he_IL en', 'en') + get :show, {:url_name => 'dfh', :view => 'all', :locale => 'he_IL'} + response.should contain('Hebrew Humpadinking') 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,12 +87,22 @@ 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 show public body names in the selected locale language if present' do + get :list, {:locale => 'es'} + response.should contain('El Department for Humpadinking') end + it 'should show public body names in the selected locale language if present for a locale with underscores' do + AlaveteliLocalization.set_locales('he_IL en', 'en') + get :list, {:locale => 'he_IL'} + response.should contain('Hebrew Humpadinking') + end + + it "should list bodies in alphabetical order" do # Note that they are alphabetised by localised name get :list diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 387b040d6..2c605a139 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -1646,10 +1646,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/fixtures/public_body_translations.yml b/spec/fixtures/public_body_translations.yml index 61e07fb5b..de1bf2f18 100644 --- a/spec/fixtures/public_body_translations.yml +++ b/spec/fixtures/public_body_translations.yml @@ -101,3 +101,17 @@ other_public_body_translation: notes: More notes publication_scheme: "" disclosure_log: "" + +humpadink_he_IL_public_body_translation: + name: "Hebrew Humpadinking" + first_letter: D + request_email: humpadink-requests@localhost + id: 9 + public_body_id: 3 + short_name: DfH + url_name: dfh + locale: he_IL + notes: An albatross told me!!! + publication_scheme: "" + disclosure_log: "" + 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..4f6b61ae1 --- /dev/null +++ b/spec/integration/localisation_spec.rb @@ -0,0 +1,88 @@ +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 + get('/en_GB') + response.body.should match /href="\/en_GB\// + end + + it "should fall back to the language if the territory is unknown" do + AlaveteliLocalization.set_locales(available_locales='es en', default_locale='en') + 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 + AlaveteliLocalization.set_locales(available_locales='en', default_locale='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 + AlaveteliLocalization.set_locales(available_locales='es en', default_locale='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 + + describe 'when the config value INCLUDE_DEFAULT_LOCALE_IN_URLS is false' do + + before do + AlaveteliLocalization.set_default_locale_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 + AlaveteliLocalization.set_default_locale_urls(true) + get '/' + response.body.should match /#{@default_lang_home_link}/ + end + + after do + AlaveteliLocalization.set_default_locale_urls(@old_include_default_locale_in_urls) + end + + end + end + +end diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 0ba8abd75..c9ee57c74 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -156,6 +156,7 @@ describe InfoRequest do end it "should cope with indexing after item is deleted" do + load_raw_emails_data IncomingMessage.find(:all).each{|x| x.parse_raw_email!} rebuild_xapian_index # delete event from underneath indexing; shouldn't cause error diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index bc693b4da..34a91a2c9 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -464,7 +464,7 @@ end describe PublicBody, " when override all public body request emails set" do it "should return the overridden request email" do - MySociety::Config.should_receive(:get).with("OVERRIDE_ALL_PUBLIC_BODY_REQUEST_EMAILS", "").twice.and_return("catch_all_test_email@foo.com") + AlaveteliConfiguration.should_receive(:override_all_public_body_request_emails).twice.and_return("catch_all_test_email@foo.com") @geraldine = public_bodies(:geraldine_public_body) @geraldine.request_email.should == "catch_all_test_email@foo.com" end diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index c40334142..7aab9cdc6 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -373,6 +373,11 @@ end # I would expect ActsAsXapian to have some tests under vendor/plugins/acts_as_xapian, but # it looks like this is not the case. Putting a test here instead. describe ActsAsXapian::Search, "#words_to_highlight" do + before(:each) do + load_raw_emails_data + get_fixtures_xapian_index + end + it "should return a list of words used in the search" do s = ActsAsXapian::Search.new([PublicBody], "albatross words", :limit => 100) s.words_to_highlight.should == ["albatross", "words"] diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a3b06cea8..86ca5150a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -31,8 +31,7 @@ Spork.prefork do Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} # Use test-specific translations - FastGettext.add_text_domain 'app', :path => File.join(File.dirname(__FILE__), 'fixtures', 'locale'), :type => :po - FastGettext.default_text_domain = 'app' + AlaveteliLocalization.set_default_text_domain('app', File.join(File.dirname(__FILE__), 'fixtures', 'locale')) RSpec.configure do |config| # ## Mock Framework @@ -88,12 +87,19 @@ Spork.prefork do # ApplicationController#set_gettext_locale which sets the locale and so you may be setting # the locale in your tests and not even realising it. So, let's make things easier for # ourselves and just always restore the locale for all tests. + config.after(:each) do + AlaveteliLocalization.set_locales(AlaveteliConfiguration::available_locales, + AlaveteliConfiguration::default_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 - @save_i18n_locale = I18n.locale + RoutingFilter.active = false if [:controller, :helper, :model].include? example.metadata[:type] end config.after(:each) do - I18n.locale = @save_i18n_locale + 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 |