Merge pull request #4521 from consul/phases_exception

Validate process dates depending on enabled phases
This commit is contained in:
Javi Martín
2021-05-29 14:28:46 +02:00
committed by GitHub
3 changed files with 143 additions and 20 deletions

View File

@@ -46,13 +46,14 @@ class Legislation::Process < ApplicationRecord
validates_translation :title, presence: true
validates :start_date, presence: true
validates :end_date, presence: true
validates :debate_start_date, presence: true, if: :debate_end_date?
validates :debate_end_date, presence: true, if: :debate_start_date?
validates :draft_start_date, presence: true, if: :draft_end_date?
validates :draft_end_date, presence: true, if: :draft_start_date?
validates :allegations_start_date, presence: true, if: :allegations_end_date?
validates :allegations_end_date, presence: true, if: :allegations_start_date?
validates :proposals_phase_end_date, presence: true, if: :proposals_phase_start_date?
%i[draft debate proposals_phase allegations].each do |phase_name|
enabled_attribute = :"#{phase_name.to_s.gsub("_phase", "")}_phase_enabled?"
validates :"#{phase_name}_start_date", presence: true, if: enabled_attribute
validates :"#{phase_name}_end_date", presence: true, if: enabled_attribute
end
validate :valid_date_ranges
validates :background_color, format: { allow_blank: true, with: CSS_HEX_COLOR }
validates :font_color, format: { allow_blank: true, with: CSS_HEX_COLOR }

View File

@@ -18,30 +18,134 @@ describe Legislation::Process do
end
describe "dates validations" do
it "is invalid if debate_start_date is present but debate_end_date is not" do
process = build(:legislation_process, debate_start_date: Date.current, debate_end_date: "")
it "is invalid if draft is enabled but draft_start_date is not present" do
process = build(:legislation_process, draft_phase_enabled: true, draft_start_date: nil)
expect(process).to be_invalid
expect(process.errors.messages[:debate_end_date]).to include("can't be blank")
expect(process.errors.messages[:draft_start_date]).to include("can't be blank")
end
it "is invalid if debate_end_date is present but debate_start_date is not" do
process = build(:legislation_process, debate_start_date: nil, debate_end_date: Date.current)
it "is invalid if draft is enabled but draft_end_date is not present" do
process = build(:legislation_process, draft_phase_enabled: true, draft_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:draft_end_date]).to include("can't be blank")
end
it "is invalid if debate phase is enabled but debate_start_date is not present" do
process = build(:legislation_process, debate_phase_enabled: true, debate_start_date: nil)
expect(process).to be_invalid
expect(process.errors.messages[:debate_start_date]).to include("can't be blank")
end
it "is invalid if allegations_start_date is present but debate_end_date is not" do
process = build(:legislation_process, allegations_start_date: Date.current,
allegations_end_date: "")
it "is invalid if debate phase is enabled but debate_end_date is not present" do
process = build(:legislation_process, debate_phase_enabled: true, debate_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:debate_end_date]).to include("can't be blank")
end
it "is invalid if proposals phase is enabled but proposals_phase_start_date is not present" do
process = build(:legislation_process, proposals_phase_enabled: true, proposals_phase_start_date: nil)
expect(process).to be_invalid
expect(process.errors.messages[:proposals_phase_start_date]).to include("can't be blank")
end
it "is invalid if proposals phase is enabled but proposals_phase_end_date is not present" do
process = build(:legislation_process, proposals_phase_enabled: true, proposals_phase_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:proposals_phase_end_date]).to include("can't be blank")
end
it "is invalid if allegations phase is enabled but allegations_start_date is not present" do
process = build(:legislation_process, allegations_phase_enabled: true,
allegations_start_date: nil,)
expect(process).to be_invalid
expect(process.errors.messages[:allegations_start_date]).to include("can't be blank")
end
it "is invalid if allegations phase is enabled but allegations_end_date is not present" do
process = build(:legislation_process, allegations_phase_enabled: true, allegations_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:allegations_end_date]).to include("can't be blank")
end
it "is invalid if debate_end_date is present but allegations_start_date is not" do
process = build(:legislation_process, allegations_start_date: nil,
allegations_end_date: Date.current)
expect(process).to be_invalid
expect(process.errors.messages[:allegations_start_date]).to include("can't be blank")
it "is valid if start dates are missing and the phase is disabled" do
draft_disabled = build(:legislation_process,
draft_phase_enabled: false,
draft_start_date: nil)
debate_disabled = build(:legislation_process,
debate_phase_enabled: false,
debate_start_date: nil)
proposals_disabled = build(:legislation_process,
proposals_phase_enabled: false,
proposals_phase_start_date: nil)
allegations_disabled = build(:legislation_process,
allegations_phase_enabled: false,
allegations_start_date: nil)
expect(draft_disabled).to be_valid
expect(debate_disabled).to be_valid
expect(proposals_disabled).to be_valid
expect(allegations_disabled).to be_valid
end
it "is valid if end dates are missing and the phase is disabled" do
draft_disabled = build(:legislation_process,
draft_phase_enabled: false,
draft_end_date: nil)
debate_disabled = build(:legislation_process,
debate_phase_enabled: false,
debate_end_date: nil)
proposals_disabled = build(:legislation_process,
proposals_phase_enabled: false,
proposals_phase_end_date: nil)
allegations_disabled = build(:legislation_process,
allegations_phase_enabled: false,
allegations_end_date: nil)
expect(draft_disabled).to be_valid
expect(debate_disabled).to be_valid
expect(proposals_disabled).to be_valid
expect(allegations_disabled).to be_valid
end
it "is valid if start and end dates are missing and the phase is disabled" do
draft_disabled = build(:legislation_process,
draft_phase_enabled: false,
draft_start_date: nil,
draft_end_date: nil)
debate_disabled = build(:legislation_process,
debate_phase_enabled: false,
debate_start_date: nil,
debate_end_date: nil)
proposals_disabled = build(:legislation_process,
proposals_phase_enabled: false,
proposals_phase_start_date: nil,
proposals_phase_end_date: nil)
allegations_disabled = build(:legislation_process,
allegations_phase_enabled: false,
allegations_start_date: nil,
allegations_end_date: nil)
expect(draft_disabled).to be_valid
expect(debate_disabled).to be_valid
expect(proposals_disabled).to be_valid
expect(allegations_disabled).to be_valid
end
end

View File

@@ -289,6 +289,24 @@ describe "Admin collaborative legislation", :admin do
expect(page).to have_field "draft_publication_date", disabled: true
end
scenario "Enabling comments phase with blank dates" do
visit edit_admin_legislation_process_path(process)
within_fieldset "Comments phase" do
check "Enabled"
fill_in "Start", with: ""
fill_in "End", with: ""
end
click_button "Save changes"
expect(page).to have_content "errors prevented this process from being saved"
within_fieldset "Comments phase" do
expect(page).to have_content "can't be blank"
end
end
scenario "Change proposal categories" do
visit edit_admin_legislation_process_path(process)
within(".admin-content") { click_link "Proposals" }