Skip to content

Commit

Permalink
Merge pull request #1862 from alphagov/fix-mongo-update-validation-be…
Browse files Browse the repository at this point in the history
…haviour

Fix Part validation behaviour after upgrading Mongo version
  • Loading branch information
minhngocd authored Mar 28, 2024
2 parents 4996e05 + 51ee786 commit c027351
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions app/models/travel_advice_edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TravelAdviceEdition
validates :version_number, presence: true, uniqueness: { scope: :country_slug }
validate :alert_status_contains_valid_values
validate :first_version_cant_be_minor_update
validate :parts_valid?
validates_with SafeHtml
validates_with LinkValidator

Expand Down Expand Up @@ -249,6 +250,10 @@ def first_version_cant_be_minor_update
end
end

def parts_valid?
parts.map(&:valid?).all?
end

def validate_scheduled_publication_time
errors.add(:scheduled_publication_time, "can't be in the past") if scheduled_publication_time && scheduled_publication_time <= Time.zone.now
end
Expand Down
38 changes: 27 additions & 11 deletions spec/models/travel_advice_edition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,24 +569,40 @@
end

context "parts" do
it "should merge part validation errors with parent document's errors" do
it "should merge part validation errors with parent document's errors when parts are created as nested attributes" do
edition = build(:travel_advice_edition,
parts: [
{ _id: "54c10d4d759b743528000010", order: "1", title: "", slug: "part-1" },
{ _id: "54c10d4d759b743528000011", order: "3", title: "Part 3", slug: "" },
{ _id: "54c10d4d759b743528000012", order: "2", title: "Part 2", slug: "part-2" },
])

expect(edition).not_to be_valid

expect(edition.errors[:part]).to eq(["1: Title can't be blank and 3: Slug can't be blank and Slug is invalid"])
end

it "should merge part validation errors with parent document's errors when parts are created as built parts" do
edition = create(:travel_advice_edition)
edition.parts = [
Part.new(_id: "54c10d4d759b743528000010", order: "1", title: "", slug: "overview"),
Part.new(_id: "54c10d4d759b743528000011", order: "2", title: "Prepare for your appointment", slug: ""),
Part.new(_id: "54c10d4d759b743528000012", order: "3", title: "Valid", slug: "valid"),
]
edition.parts.build(_id: "54c10d4d759b743528000010", order: "1", title: "", slug: "part-1")
edition.parts.build(_id: "54c10d4d759b743528000011", order: "3", title: "Part 3", slug: "")
edition.parts.build(_id: "54c10d4d759b743528000012", order: "2", title: "Part 2", slug: "part-2")

expect(edition).not_to be_valid

expect(edition.errors[:part]).to eq(["1: Title can't be blank and 2: Slug can't be blank and Slug is invalid"])
expect(edition.errors[:part]).to eq(["1: Title can't be blank and 3: Slug can't be blank and Slug is invalid"])
end

it "#whole_body returns ordered parts" do
edition = create(:travel_advice_edition)
edition.parts.build(_id: "54c10d4d759b743528000010", order: "1", title: "Part 1", slug: "part_1")
edition.parts.build(_id: "54c10d4d759b743528000011", order: "3", title: "Part 3", slug: "part_3")
edition.parts.build(_id: "54c10d4d759b743528000012", order: "2", title: "Part 2", slug: "part_2")
edition = create(:travel_advice_edition,
parts: [
{ _id: "54c10d4d759b743528000010", order: "1", title: "Part 1", slug: "part-1" },
{ _id: "54c10d4d759b743528000011", order: "3", title: "Part 3", slug: "part-3" },
{ _id: "54c10d4d759b743528000012", order: "2", title: "Part 2", slug: "part-2" },
])

expect(edition).to be_valid

expect(edition.whole_body).to eq("# Part 1\n\n\n\n# Part 2\n\n\n\n# Part 3\n\n")
end
end
Expand Down

0 comments on commit c027351

Please sign in to comment.