From 3fb80a12652792df9da06c22b4bb944ab80bf376 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Thu, 29 Aug 2013 10:24:02 +0100 Subject: Make falling back to default locale in public body listings optional As the code stood, the list method in PublicBodyController would only return results that had translations of the public body in the default locale. This has a variety of problems if you're viewing pages in the non-default locale - for example, the "first letter" links wouldn't bring up the public bodies that began with that letter in the current locale, only those that began with it in the default locale. Ideally, every public body would be translated into every available locale for the site, but there are cases where deployers wish to have public body listings also include those from the default locale, in case there are untralsated public bodies: https://groups.google.com/d/msg/alaveteli-dev/zUY_USaAMAM/M7KTQ9RC5YUJ This commit makes the default behaviour to look for public body listings only in the current locale, but if the new configuration option PUBLIC_BODY_LIST_FALLBACK_TO_DEFAULT_LOCALE is set, then public body listings will be looked for in both the current locale and the default locale. Fixes #1000 --- spec/controllers/public_body_controller_spec.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 4e1841164..2b921b488 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -78,7 +78,8 @@ describe PublicBodyController, "when listing bodies" do response.should be_success end - it "should list all bodies from default locale, even when there are no translations for selected locale" do + it "if fallback is requested, should list all bodies from default locale, even when there are no translations for selected locale" do + AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) I18n.with_locale(:en) do @english_only = PublicBody.new(:name => 'English only', :short_name => 'EO', @@ -96,6 +97,12 @@ describe PublicBodyController, "when listing bodies" do response.should contain('El Department for Humpadinking') end + it 'should not show the internal admin authority' do + PublicBody.internal_admin_body + get :list, {:locale => 'en'} + response.should_not contain('Internal admin authority') + 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'} -- cgit v1.2.3 From 6395868beaaefb5525c19ce8b5deeaeb61925883 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Tue, 10 Sep 2013 13:54:52 +0100 Subject: Add tests to assert the new public body listing behaviour These tests will fail unless we have the new behaviour for public body listing, which depend on the setting of PUBLIC_BODY_LIST_FALLBACK_TO_DEFAULT_LOCALE. --- spec/controllers/public_body_controller_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 2b921b488..1700053fb 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -92,6 +92,20 @@ describe PublicBodyController, "when listing bodies" do assigns[:public_bodies].include?(@english_only).should == true end + it 'if fallback is requested, should still list public bodies only with translations in the current locale' do + AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) + I18n.with_locale(:es) do + @spanish_only = PublicBody.new(:name => 'Español Solamente', + :short_name => 'ES', + :request_email => 'spanish@flourish.org', + :last_edit_editor => 'test', + :last_edit_comment => '') + @spanish_only.save + end + get :list, {:locale => 'es'} + assigns[:public_bodies].include?(@spanish_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') -- cgit v1.2.3 From 8146051161e0359b621019bb25091f2aa0634594 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Tue, 10 Sep 2013 14:12:27 +0100 Subject: Factor out the code to generate example single language public bodies It's useful to be able to generate example public bodies that only have translations in particular languages. --- spec/controllers/public_body_controller_spec.rb | 39 +++++++++++++++---------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 1700053fb..d4f359a80 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -78,30 +78,37 @@ describe PublicBodyController, "when listing bodies" do response.should be_success end + def make_single_language_example(locale) + result = nil + I18n.with_locale(locale) do + case locale + when :en + result = PublicBody.new(:name => 'English only', + :short_name => 'EO') + when :es + result = PublicBody.new(:name => 'Español Solamente', + :short_name => 'ES') + else + raise StandardError.new "Unknown locale #{locale}" + end + result.request_email = "#{locale}@example.org" + result.last_edit_editor = 'test' + result.last_edit_comment = '' + result.save + end + result + end + it "if fallback is requested, should list all bodies from default locale, even when there are no translations for selected locale" do AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) - I18n.with_locale(:en) do - @english_only = PublicBody.new(:name => 'English only', - :short_name => 'EO', - :request_email => 'english@flourish.org', - :last_edit_editor => 'test', - :last_edit_comment => '') - @english_only.save - end + @english_only = make_single_language_example :en get :list, {:locale => 'es'} assigns[:public_bodies].include?(@english_only).should == true end it 'if fallback is requested, should still list public bodies only with translations in the current locale' do AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) - I18n.with_locale(:es) do - @spanish_only = PublicBody.new(:name => 'Español Solamente', - :short_name => 'ES', - :request_email => 'spanish@flourish.org', - :last_edit_editor => 'test', - :last_edit_comment => '') - @spanish_only.save - end + @spanish_only = make_single_language_example :es get :list, {:locale => 'es'} assigns[:public_bodies].include?(@spanish_only).should == true end -- cgit v1.2.3 From dff7f73bc0ce0054cd4c837357b1ad3cf7bdfe1c Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Tue, 10 Sep 2013 14:31:12 +0100 Subject: Don't display duplicate public bodies with the fallback This introduces some raw SQL statement for the fallback case, but we can't see an easy way to avoid that in this case. This commit also adds some tests that assert the sorting and non-duplication properties of the listing. Thanks to Louise Crow for working out the SQL expression for falling back to the default locale. --- spec/controllers/public_body_controller_spec.rb | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index d4f359a80..70ed482b2 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -1,6 +1,8 @@ # coding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'nokogiri' + describe PublicBodyController, "when showing a body" do render_views @@ -99,6 +101,14 @@ describe PublicBodyController, "when listing bodies" do result end + it "with no fallback, should only return bodies from the current locale" do + @english_only = make_single_language_example :en + @spanish_only = make_single_language_example :es + get :list, {:locale => 'es'} + assigns[:public_bodies].include?(@english_only).should == false + assigns[:public_bodies].include?(@spanish_only).should == true + end + it "if fallback is requested, should list all bodies from default locale, even when there are no translations for selected locale" do AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) @english_only = make_single_language_example :en @@ -113,6 +123,14 @@ describe PublicBodyController, "when listing bodies" do assigns[:public_bodies].include?(@spanish_only).should == true end + it "if fallback is requested, make sure that there are no duplicates listed" do + AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) + get :list, {:locale => 'es'} + pb_ids = assigns[:public_bodies].map { |pb| pb.id } + unique_pb_ids = pb_ids.uniq + pb_ids.sort.should === unique_pb_ids.sort + 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') @@ -124,6 +142,18 @@ describe PublicBodyController, "when listing bodies" do response.should_not contain('Internal admin authority') end + it 'should order on the translated name, even with the fallback' do + # The names of each public body is in: + # Public Body Name + # ... eo extract all of those, and check that they are ordered: + AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) + get :list, {:locale => 'es'} + parsed = Nokogiri::HTML(response.body) + public_body_names = parsed.xpath '//span[@class="head"]/a/text()' + public_body_names = public_body_names.map { |pb| pb.to_s } + public_body_names.should == public_body_names.sort + 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'} -- cgit v1.2.3