diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 154 | ||||
-rw-r--r-- | spec/integration/admin_public_body_edit_spec.rb | 71 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 98 |
3 files changed, 311 insertions, 12 deletions
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index 7de292303..3ab58317a 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -44,6 +44,15 @@ describe AdminPublicBodyController, 'when showing the form for a new public body assigns[:public_body].should be_a(PublicBody) end + it "builds new translations for all locales" do + get :new + + translations = assigns[:public_body].translations.map{ |t| t.locale.to_s }.sort + available = I18n.available_locales.map{ |l| l.to_s }.sort + + expect(translations).to eq(available) + end + context 'when passed a change request id as a param' do render_views @@ -88,11 +97,13 @@ describe AdminPublicBodyController, "when creating a public body" do :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code', - :translated_versions => [{ :locale => "es", - :name => "Mi Nuevo Quango", - :short_name => "", - :request_email => 'newquango@localhost' }] - } + :translations_attributes => { + 'es' => { :locale => "es", + :name => "Mi Nuevo Quango", + :short_name => "", + :request_email => 'newquango@localhost' } + } + } } PublicBody.count.should == n + 1 @@ -159,6 +170,12 @@ describe AdminPublicBodyController, "when editing a public body" do response.should render_template('edit') end + it "builds new translations if the body does not already have a translation in the specified locale" do + public_body = FactoryGirl.create(:public_body) + get :edit, :id => public_body.id + expect(assigns[:public_body].translations.map(&:locale)).to include(:fr) + end + context 'when passed a change request id as a param' do render_views @@ -196,6 +213,116 @@ describe AdminPublicBodyController, "when updating a public body" do pb.name.should == "Renamed" end + it 'adds a new translation' do + pb = public_bodies(:humpadink_public_body) + pb.translation_for(:es).destroy + pb.reload + + post :update, { + :id => pb.id, + :public_body => { + :name => "Department for Humpadinking", + :short_name => "", + :tag_string => "some tags", + :request_email => 'edited@localhost', + :last_edit_comment => 'From test code', + :translations_attributes => { + 'es' => { :locale => "es", + :name => "El Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' } + } + } + } + + request.flash[:notice].should include('successful') + + pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + + I18n.with_locale(:es) do + expect(pb.name).to eq('El Department for Humpadinking') + end + end + + it 'adds new translations' do + pb = public_bodies(:humpadink_public_body) + pb.translation_for(:es).destroy + pb.reload + + post :update, { + :id => pb.id, + :public_body => { + :name => "Department for Humpadinking", + :short_name => "", + :tag_string => "some tags", + :request_email => 'edited@localhost', + :last_edit_comment => 'From test code', + :translations_attributes => { + 'es' => { :locale => "es", + :name => "El Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' }, + 'fr' => { :locale => "fr", + :name => "Le Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' } + } + } + } + + request.flash[:notice].should include('successful') + + pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + + I18n.with_locale(:es) do + expect(pb.name).to eq('El Department for Humpadinking') + end + + I18n.with_locale(:fr) do + expect(pb.name).to eq('Le Department for Humpadinking') + end + end + + it 'updates an existing translation and adds a third translation' do + pb = public_bodies(:humpadink_public_body) + + put :update, { + :id => pb.id, + :public_body => { + :name => "Department for Humpadinking", + :short_name => "", + :tag_string => "some tags", + :request_email => 'edited@localhost', + :last_edit_comment => 'From test code', + :translations_attributes => { + # Update existing translation + 'es' => { :locale => "es", + :name => "Renamed Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' }, + # Add new translation + 'fr' => { :locale => "fr", + :name => "Le Department for Humpadinking", + :short_name => "", + :request_email => 'edited@localhost' } + } + } + } + + request.flash[:notice].should include('successful') + + pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + + I18n.with_locale(:es) do + expect(pb.name).to eq('Renamed Department for Humpadinking') + end + + I18n.with_locale(:fr) do + expect(pb.name).to eq('Le Department for Humpadinking') + end + + end + it "saves edits to a public body in another locale" do I18n.with_locale(:es) do pb = PublicBody.find(id=3) @@ -208,11 +335,11 @@ describe AdminPublicBodyController, "when updating a public body" do :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code', - :translated_versions => { - 3 => {:locale => "es", - :name => "Renamed", - :short_name => "", - :request_email => 'edited@localhost'} + :translations_attributes => { + 'es' => { :locale => "es", + :name => "Renamed", + :short_name => "", + :request_email => 'edited@localhost' } } } } @@ -220,12 +347,15 @@ describe AdminPublicBodyController, "when updating a public body" do end pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + I18n.with_locale(:es) do - pb.name.should == "Renamed" + expect(pb.name).to eq('Renamed') end + I18n.with_locale(:en) do - pb.name.should == "Department for Humpadinking" + expect(pb.name).to eq('Department for Humpadinking') end + end context 'when the body is being updated as a result of a change request' do diff --git a/spec/integration/admin_public_body_edit_spec.rb b/spec/integration/admin_public_body_edit_spec.rb new file mode 100644 index 000000000..613793dd4 --- /dev/null +++ b/spec/integration/admin_public_body_edit_spec.rb @@ -0,0 +1,71 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require File.expand_path(File.dirname(__FILE__) + '/alaveteli_dsl') + +describe 'Editing a Public Body' do + before do + AlaveteliConfiguration.stub!(:skip_admin_auth).and_return(false) + + confirm(:admin_user) + @admin = login(:admin_user) + + PublicBody.create(:name => 'New Quango', + :short_name => '', + :request_email => 'newquango@localhost', + :last_edit_editor => 'test', + :last_edit_comment => 'testing') + + @body = PublicBody.find_by_name('New Quango') + end + + it 'can edit the default locale' do + @admin.visit admin_body_edit_path(@body) + @admin.fill_in 'public_body_name__en', :with => 'New Quango EN' + @admin.click_button 'Save' + + pb = @body.reload + expect(pb.name).to eq('New Quango EN') + end + + it 'can add a translation for a single locale' do + expect(@body.find_translation_by_locale('fr')).to be_nil + + @admin.visit admin_body_edit_path(@body) + @admin.fill_in 'public_body_translations_attributes_fr_name__fr', :with => 'New Quango FR' + @admin.click_button 'Save' + + pb = @body.reload + I18n.with_locale(:fr) do + expect(pb.name).to eq('New Quango FR') + end + end + + it 'can add a translation for multiple locales', :focus => true do + @admin.visit admin_body_edit_path(@body) + @admin.fill_in 'public_body_name__en', :with => 'New Quango EN' + @admin.click_button 'Save' + + # Add FR translation + expect(@body.find_translation_by_locale('fr')).to be_nil + @admin.visit admin_body_edit_path(@body) + @admin.fill_in 'public_body_translations_attributes_fr_name__fr', :with => 'New Quango FR' + @admin.click_button 'Save' + + # Add ES translation + expect(@body.find_translation_by_locale('es')).to be_nil + @admin.visit admin_body_edit_path(@body) + @admin.fill_in 'public_body_translations_attributes_es_name__es', :with => 'New Quango ES' + @admin.click_button 'Save' + + pb = @body.reload + + expect(pb.name).to eq('New Quango EN') + + I18n.with_locale(:fr) do + expect(pb.name).to eq('New Quango FR') + end + + I18n.with_locale(:es) do + expect(pb.name).to eq('New Quango ES') + end + end +end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 510ad59fe..a9e801bfd 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -136,6 +136,104 @@ describe PublicBody do end + describe :translations_attributes= do + + context 'translation_attrs is a Hash' do + + it 'takes the correct code path for a Hash' do + attrs = {} + attrs.should_receive(:each_value) + PublicBody.new().translations_attributes = attrs + end + + it 'updates an existing translation' do + body = public_bodies(:geraldine_public_body) + translation = body.translation_for(:es) + params = { 'es' => { :locale => 'es', + :name => 'Renamed' } } + + body.translations_attributes = params + I18n.with_locale(:es) { expect(body.name).to eq('Renamed') } + end + + it 'updates an existing translation and creates a new translation' do + body = public_bodies(:geraldine_public_body) + translation = body.translation_for(:es) + + expect(body.translations.size).to eq(2) + + body.translations_attributes = { + 'es' => { :locale => 'es', + :name => 'Renamed' }, + 'fr' => { :locale => 'fr', + :name => 'Le Geraldine Quango' } + } + + expect(body.translations.size).to eq(3) + I18n.with_locale(:es) { expect(body.name).to eq('Renamed') } + I18n.with_locale(:fr) { expect(body.name).to eq('Le Geraldine Quango') } + end + + it 'skips empty translations' do + body = public_bodies(:geraldine_public_body) + translation = body.translation_for(:es) + + expect(body.translations.size).to eq(2) + + body.translations_attributes = { + 'es' => { :locale => 'es', + :name => 'Renamed' }, + 'fr' => { :locale => 'fr' } + } + + expect(body.translations.size).to eq(2) + end + + end + + context 'translation_attrs is an Array' do + + it 'takes the correct code path for an Array' do + attrs = [] + attrs.should_receive(:each) + PublicBody.new().translations_attributes = attrs + end + + it 'creates a new translation' do + body = public_bodies(:geraldine_public_body) + body.translation_for(:es).destroy + body.reload + + expect(body.translations.size).to eq(1) + + body.translations_attributes = [ { + :locale => 'es', + :name => 'Renamed' + } + ] + + expect(body.translations.size).to eq(2) + I18n.with_locale(:es) { expect(body.name).to eq('Renamed') } + end + + it 'skips empty translations' do + body = public_bodies(:geraldine_public_body) + body.translation_for(:es).destroy + body.reload + + expect(body.translations.size).to eq(1) + + body.translations_attributes = [ + { :locale => 'empty' } + ] + + expect(body.translations.size).to eq(1) + end + + end + + end + end describe PublicBody, " using tags" do |