Skip to content

Commit

Permalink
Merge pull request #9836 from alphagov/content-modelling/837-bug-sche…
Browse files Browse the repository at this point in the history
…duled-publishing-form-does-not-populate-data-from-existing-block

(837) Populate scheduled_at date/time using model values
  • Loading branch information
pezholio authored Jan 23, 2025
2 parents c77436c + 6fb29f8 commit c4f1209
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
<% end %>

<%
year_param = params.dig("scheduled_at", "scheduled_publication(1i)")
month_param = params.dig("scheduled_at", "scheduled_publication(2i)")
day_param = params.dig("scheduled_at", "scheduled_publication(3i)")
hour_param = params.dig("scheduled_at", "scheduled_publication(4i)")
minute_param = params.dig("scheduled_at", "scheduled_publication(5i)")
is_scheduled_param = params["schedule_publishing"]
%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,24 @@ def initialize(content_block_edition:, params:, context:, back_link:, form_url:,
private

attr_reader :is_rescheduling, :content_block_edition, :params, :context, :back_link, :form_url

def year_param
content_block_edition.scheduled_publication&.year || params.dig("scheduled_at", "scheduled_publication(1i)")
end

def month_param
content_block_edition.scheduled_publication&.month || params.dig("scheduled_at", "scheduled_publication(2i)")
end

def day_param
content_block_edition.scheduled_publication&.day || params.dig("scheduled_at", "scheduled_publication(3i)")
end

def hour_param
content_block_edition.scheduled_publication&.hour || params.dig("scheduled_at", "scheduled_publication(4i)")
end

def minute_param
content_block_edition.scheduled_publication&.min || params.dig("scheduled_at", "scheduled_publication(5i)")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,45 @@ class ContentBlockManager::Shared::SchedulePublishingComponentTest < ViewCompone
)}']"
end
end

describe "when the content_block_edition already has a publish date set" do
let(:params) do
{
"scheduled_at" => {
"scheduled_publication(1i)" => "2022",
"scheduled_publication(2i)" => "2",
"scheduled_publication(3i)" => "3",
"scheduled_publication(4i)" => "1",
"scheduled_publication(5i)" => "2",
},
}
end

it "prepopulates the date fields" do
render_inline(component)

assert_selector "input[name='scheduled_at[scheduled_publication(1i)]'][value='#{params['scheduled_at']['scheduled_publication(1i)']}']"
assert_selector "input[name='scheduled_at[scheduled_publication(2i)]'][value='#{params['scheduled_at']['scheduled_publication(2i)']}']"
assert_selector "input[name='scheduled_at[scheduled_publication(3i)]'][value='#{params['scheduled_at']['scheduled_publication(3i)']}']"

assert_selector "select[name='scheduled_at[scheduled_publication(4i)]'] option[value='0#{params['scheduled_at']['scheduled_publication(4i)']}'][selected='selected']"
assert_selector "select[name='scheduled_at[scheduled_publication(5i)]'] option[value='0#{params['scheduled_at']['scheduled_publication(5i)']}'][selected='selected']"
end
end

describe "when the params have date attributes set" do
let(:scheduled_publication) { Time.zone.now + 1.month }
let(:content_block_edition) { create(:content_block_edition, :email_address, document: content_block_document, scheduled_publication:) }

it "prepopulates the date fields" do
render_inline(component)

assert_selector "input[name='scheduled_at[scheduled_publication(1i)]'][value='#{scheduled_publication.year}']"
assert_selector "input[name='scheduled_at[scheduled_publication(2i)]'][value='#{scheduled_publication.month}']"
assert_selector "input[name='scheduled_at[scheduled_publication(3i)]'][value='#{scheduled_publication.day}']"

assert_selector "select[name='scheduled_at[scheduled_publication(4i)]'] option[value='#{scheduled_publication.hour}'][selected='selected']"
assert_selector "select[name='scheduled_at[scheduled_publication(5i)]'] option[value='#{scheduled_publication.min}'][selected='selected']"
end
end
end

0 comments on commit c4f1209

Please sign in to comment.