Skip to content

Commit d210d07

Browse files
authored
Merge pull request #10516 from alphagov/edition-validity-issue
Stop 'invalid' organisations from blocking edits to publications (WHIT-2396)
2 parents 2c16718 + 15133bf commit d210d07

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

app/models/concerns/edition/organisations.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ def limits_access_via_organisations?
9090
true
9191
end
9292

93+
# Rails automatically defines methods called
94+
# `validate_associated_records_for_<association>` for any association that is
95+
# autosaved or validated. These methods run validations on each associated
96+
# EditionOrganisation and Organisation, which causes an otherwise-valid edition
97+
# to become invalid if any linked organisation is invalid (for example, because
98+
# of an overlong custom_jobs_url). We don’t want an invalid organisation to
99+
# block publication of an edition, so we override these methods to no-op.
100+
#
101+
# Presence and uniqueness of associated organisations are already enforced
102+
# by our own custom validators, so skipping these auto-generated validations
103+
# has no negative side-effects.
104+
def validate_associated_records_for_edition_organisations
105+
# no-op: prevent associated EditionOrganisation validations from running
106+
end
107+
108+
def validate_associated_records_for_organisations
109+
# no-op: prevent associated Organisation validations from running
110+
end
111+
93112
private
94113

95114
def at_least_one_lead_organisation

test/unit/app/models/edition/organisations_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ class Edition::OrganisationsTest < ActiveSupport::TestCase
4343
end
4444
end
4545

46+
test "should remain publishable when linked organisations are invalid" do
47+
# Build and persist an organisation that will be invalid.
48+
invalid_org = create(:organisation)
49+
invalid_org.update_column(:homepage_type, "invalid")
50+
assert_not invalid_org.valid?, "Homepage type is not included in the list"
51+
52+
# Create an edition using the invalid organisation as the lead organisation and
53+
# disable default organisation creation. Persisting the edition mirrors real usage.
54+
edition = create(
55+
:publication,
56+
create_default_organisation: false,
57+
lead_organisations: [invalid_org],
58+
supporting_organisations: [],
59+
)
60+
61+
# The edition should be valid for publishing before any side-effects.
62+
assert edition.valid?(:publish), "edition should be valid in publish context before touching organisations"
63+
64+
# Reading organisation names used to build unsaved translations and make the edition
65+
# invalid via autosave validations:contentReference[oaicite:0]{index=0}. After overriding those validations,
66+
# the edition should remain valid even after accessing organisation names.
67+
edition.organisations.map(&:name)
68+
assert edition.valid?(:publish), "edition should still be valid in publish context after reading organisation names"
69+
end
70+
4671
test "#sorted_organisations returns organisations in alphabetical order" do
4772
organisation1 = create(:organisation, name: "Ministry of Jazz")
4873
organisation2 = create(:organisation, name: "Free Jazz Foundation")

0 commit comments

Comments
 (0)