Skip to content

Commit

Permalink
Merge pull request #9048 from alphagov/add-ww-org-translation-validation
Browse files Browse the repository at this point in the history
Add translation validations for Editionable Worldwide Organisation parts
  • Loading branch information
brucebolt committed May 16, 2024
2 parents 4101390 + 9192c10 commit 554e731
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Contact < ApplicationRecord
validates :street_address, :country_id, presence: true, if: ->(r) { r.has_postal_address? }
accepts_nested_attributes_for :contact_numbers, allow_destroy: true, reject_if: :all_blank

validate :parent_edition_has_locales, if: :attached_to_editionable_worldwide_organisation?

after_update :republish_dependent_editions
after_update :republish_dependent_policy_groups
after_update :republish_organisation_to_publishing_api
Expand Down Expand Up @@ -102,4 +104,16 @@ def republish_embassies_index_page_to_publishing_api
def publishing_api_presenter
PublishingApi::ContactPresenter
end

def attached_to_editionable_worldwide_organisation?
contactable.is_a?(WorldwideOffice) && contactable.edition
end

def parent_edition_has_locales
non_existent_translations = non_english_translated_locales - contactable.edition.non_english_translated_locales

unless non_existent_translations.empty?
errors.add(:base, "Translations '#{non_existent_translations.map(&:code).join(', ')}' do not exist for this worldwide organisation")
end
end
end
9 changes: 9 additions & 0 deletions app/models/worldwide_organisation_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class WorldwideOrganisationPage < ApplicationRecord
presence: true,
exclusion: { in: [CorporateInformationPageType::AboutUs.id], message: "Type cannot be `About us`" }
validate :unique_worldwide_organisation_and_page_type, on: :create, if: :edition
validate :parent_edition_has_locales, if: :edition

after_commit :republish_worldwide_organisation_draft
after_destroy :discard_draft
Expand Down Expand Up @@ -77,4 +78,12 @@ def unique_worldwide_organisation_and_page_type
errors.add(:base, "Another '#{display_type_key.humanize}' page already exists for this worldwide organisation")
end
end

def parent_edition_has_locales
non_existent_translations = non_english_translated_locales - edition.non_english_translated_locales

unless non_existent_translations.empty?
errors.add(:base, "Translations '#{non_existent_translations.map(&:code).join(', ')}' do not exist for this worldwide organisation")
end
end
end
17 changes: 17 additions & 0 deletions test/unit/app/models/contact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,21 @@ class ContactTest < ActiveSupport::TestCase
office.contact.destroy!
end
end

test "when associated with a WorldwideOffice should be valid when translated into a language that the worldwide office's associated organisation has" do
worldwide_organisation = create(:editionable_worldwide_organisation, translated_into: %i[de es fr])
office = create(:worldwide_office, edition: worldwide_organisation, worldwide_organisation: nil)
contact = create(:contact, contactable: office, translated_into: %i[fr])

assert contact.valid?
end

test "when associated with a WorldwideOffice should not be valid when translated into a language that the worldwide office's associated organisation does not have" do
worldwide_organisation = create(:editionable_worldwide_organisation, translated_into: %i[de es fr])
office = create(:worldwide_office, edition: worldwide_organisation, worldwide_organisation: nil)
contact = create(:contact, contactable: office, translated_into: %i[cy es-419])

assert_not contact.valid?
assert contact.errors[:base].include?("Translations 'cy, es-419' do not exist for this worldwide organisation")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class EditionableWorldwideOrganisationTest < ActiveSupport::TestCase

test "should clone office and contact associations when new draft of published edition is created" do
contact = create(:contact, translated_into: [:es])
published_worldwide_organisation = create(:editionable_worldwide_organisation, :published)
published_worldwide_organisation = create(:editionable_worldwide_organisation, :published, translated_into: [:es])
create(:worldwide_office, worldwide_organisation: nil, edition: published_worldwide_organisation, contact:)

draft_worldwide_organisation = published_worldwide_organisation.create_draft(create(:writer))
Expand Down
15 changes: 15 additions & 0 deletions test/unit/app/models/worldwide_organisation_page_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,19 @@ class WorldwideOrganisationPageTest < ActiveSupport::TestCase
expected_locales = %i[de fr].map { |l| Locale.new(l) }
assert_equal expected_locales, page.missing_translations
end

test "should be valid when translated into a language that the worldwide organisation has" do
worldwide_organisation = create(:editionable_worldwide_organisation, translated_into: %i[de es fr])
page = create(:worldwide_organisation_page, edition: worldwide_organisation, translated_into: %i[fr])

assert page.valid?
end

test "should not be valid when translated into a language that the worldwide organisation does not have" do
worldwide_organisation = create(:editionable_worldwide_organisation, translated_into: %i[de es fr])
page = create(:worldwide_organisation_page, edition: worldwide_organisation, translated_into: %i[cy es-419])

assert_not page.valid?
assert page.errors[:base].include?("Translations 'cy, es-419' do not exist for this worldwide organisation")
end
end

0 comments on commit 554e731

Please sign in to comment.