Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/tasks/forms.rake
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ namespace :forms do
Rails.logger.info "Form #{form.id} (\"#{form.name}\") created by #{creator&.name || 'No creator'} with organisation #{creator&.organisation&.name || 'N/A'}"
end
end

desc "Show a form's form_document as JSON"
task :show_form_document, %i[form_id tag language] => :environment do |_, args|
usage_message = "usage: rake forms:show_form_document[<form_id>, <tag>, <language>]".freeze

abort usage_message if args[:form_id].blank? || args[:tag].blank?
language = args[:language].presence || "en"

abort "tag must be one of draft, live or archived" unless %w[draft live archived].include?(args[:tag])
abort "language must be en or cy" unless %w[en cy].include?(language)

form = Form.find(args[:form_id])

form_document = form.form_documents.find_by(tag: args[:tag], language:)
abort "#{fmt_form(form)} does not have a #{args[:tag]} #{language} form document" if form_document.blank?

puts JSON.pretty_generate(form_document.as_json)
end
end

def move_forms(form_ids, group_id)
Expand Down
55 changes: 55 additions & 0 deletions spec/lib/tasks/forms.rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -807,4 +807,59 @@
end
end
end

describe "forms:show_form_document" do
subject(:task) do
Rake::Task["forms:show_form_document"]
end

let(:form) { create(:form) }

it "prints the requested form document as JSON" do
expect { task.invoke(form.id, "draft", "en") }
.to output(/"id": #{form.draft_form_document.id}/).to_stdout
end

it "prints the requested English form document when no language is given" do
expect { task.invoke(form.id, "draft") }
.to output(/"id": #{form.draft_form_document.id}/).to_stdout
end

it "aborts with a usage message when arguments are missing" do
expect {
task.invoke(form.id)
}.to raise_error(SystemExit)
.and output(/usage: rake forms:show_form_document\[<form_id>, <tag>, <language>\]/).to_stderr
end

it "aborts when the tag is invalid" do
expect {
task.invoke(form.id, "invalid", "en")
}.to raise_error(SystemExit)
.and output(/tag must be one of draft, live or archived/).to_stderr
end

it "aborts when the language is invalid" do
expect {
task.invoke(form.id, "draft", "invalid")
}.to raise_error(SystemExit)
.and output(/language must be en or cy/).to_stderr
end

it "aborts when the requested form document is missing" do
expect {
task.invoke(form.id, "draft", "cy")
}.to raise_error(SystemExit)
.and output(/form #{form.id} \("#{form.name}"\) does not have a draft cy form document/).to_stderr
end

context "when a form has a Welsh translation" do
let(:form) { create(:form, :with_welsh_translation) }

it "prints the requested form document as JSON" do
expect { task.invoke(form.id, "draft", "cy") }
.to output(/"id": #{form.draft_welsh_form_document.id}/).to_stdout
end
end
end
end