diff --git a/app/services/court_report_format_contact_date.rb b/app/services/court_report_format_contact_date.rb index db20bfbd66..9d0b120f8a 100644 --- a/app/services/court_report_format_contact_date.rb +++ b/app/services/court_report_format_contact_date.rb @@ -1,15 +1,26 @@ +# frozen_string_literal: true + class CourtReportFormatContactDate - CONTACT_UNSUCCESSFUL_PREFIX = "*" + CONTACT_SUCCESSFUL_SUFFIX = "" + CONTACT_UNSUCCESSFUL_SUFFIX = "*" def initialize(case_contact) @case_contact = case_contact end def format - I18n.l(@case_contact.occurred_at, format: :short_date, default: nil).concat(@case_contact.contact_made ? "" : CONTACT_UNSUCCESSFUL_PREFIX) + I18n.l(@case_contact.occurred_at, format: :short_date, default: nil).concat(contact_made_suffix) end def format_long I18n.l(@case_contact.occurred_at, format: :long_date, default: nil) end + + private + + attr_reader :case_contact + + def contact_made_suffix + @case_contact.contact_made ? CONTACT_SUCCESSFUL_SUFFIX : CONTACT_UNSUCCESSFUL_SUFFIX + end end diff --git a/app/services/fdf_inputs_service.rb b/app/services/fdf_inputs_service.rb index 46c8f48899..0bff01744c 100644 --- a/app/services/fdf_inputs_service.rb +++ b/app/services/fdf_inputs_service.rb @@ -1,8 +1,11 @@ +# frozen_string_literal: true + class FdfInputsService FDF_ERB_TEMPLATE_PATH = ["data", "inputs_fdf.erb"].freeze def self.clean(str) return unless str.present? + str .to_s .gsub(/[)(\\]/, '\\\\\0') diff --git a/spec/services/court_report_format_contact_date_spec.rb b/spec/services/court_report_format_contact_date_spec.rb index 6cad895915..077ca86ae2 100644 --- a/spec/services/court_report_format_contact_date_spec.rb +++ b/spec/services/court_report_format_contact_date_spec.rb @@ -1,7 +1,41 @@ +# frozen_string_literal: true + require "rails_helper" RSpec.describe CourtReportFormatContactDate, type: :service do - # TODO: Add tests for CourtReportFormatContactDate + describe "#format" do + context "when there has been a successful contact" do + it "returns the day and month of when the Case Contact's occcurred" do + case_contact = build( + :case_contact, + occurred_at: Date.new(2026, 4, 16), + contact_made: true + ) + + contact_date = CourtReportFormatContactDate.new(case_contact).format + + expect(contact_date).to eq("4/16") + end + end + + context "when there has been no contact made" do + it "returns the day and month of when the Case Contact's occcurred with a suffix" do + case_contact = build(:case_contact, occurred_at: Date.new(2026, 3, 16)) + + contact_date = CourtReportFormatContactDate.new(case_contact).format + + expect(contact_date).to eq("3/16*") + end + end + end + + describe "#format_long" do + it "returns the day, month and year of when the Case Contact's in the long format" do + case_contact = build(:case_contact, occurred_at: Date.new(2026, 2, 16)) + + contact_date = CourtReportFormatContactDate.new(case_contact).format_long - pending "add some tests for CourtReportFormatContactDate" + expect(contact_date).to eq("02/16/26") + end + end end diff --git a/spec/services/fdf_inputs_service_spec.rb b/spec/services/fdf_inputs_service_spec.rb index 439107be01..cc17b05ef4 100644 --- a/spec/services/fdf_inputs_service_spec.rb +++ b/spec/services/fdf_inputs_service_spec.rb @@ -1,7 +1,56 @@ +# frozen_string_literal: true + require "rails_helper" RSpec.describe FdfInputsService, type: :service do - # TODO: Add tests for FdfInputsService + describe ".clean" do + context "when the string is nil" do + it "returns nil" do + expect(FdfInputsService.clean("")).to be_nil + expect(FdfInputsService.clean(" ")).to be_nil + expect(FdfInputsService.clean(nil)).to be_nil + end + end + + it "returns the escaped string" do + expect(FdfInputsService.clean("hello world")).to eq("hello world") + expect(FdfInputsService.clean("(test)")).to eq("\\(test\\)") + expect(FdfInputsService.clean("path\\to\\file")).to eq("path\\\\to\\\\file") + expect(FdfInputsService.clean("(a\\b)")).to eq("\\(a\\\\b\\)") + expect(FdfInputsService.clean("((a))")).to eq("\\(\\(a\\)\\)") + expect(FdfInputsService.clean("\\(test\\)")).to eq("\\\\\\(test\\\\\\)") + end + end + + describe "#write_to_file" do + it "calls PdfForms with the given arguments and returns a file" do + inputs = {name: "Bob Cat"} + pdf_template_path = "/path/to/template.pdf" + basename = "test_file" + + fake_pdf_forms = double("PdfForms") + allow(PdfForms).to receive(:new).and_return(fake_pdf_forms) + allow(fake_pdf_forms).to receive(:fill_form_with_fdf) + + service = FdfInputsService.new( + inputs: inputs, + pdf_template_path: pdf_template_path, + basename: basename + ) + + result = service.write_to_file + + expect(fake_pdf_forms).to have_received(:fill_form_with_fdf) do |template, output_path, fdf_path, flatten| + expect(template).to eq(pdf_template_path) + expect(output_path).to be_a(String) + expect(File.exist?(output_path)).to be(true) + expect(fdf_path).to be_a(String) + expect(File.exist?(fdf_path)).to be(true) + expect(flatten).to eq(flatten: true) + end - pending "add some tests for FdfInputsService" + expect(result).to be_a(Tempfile) + expect(File.exist?(result.path)).to be(true) + end + end end diff --git a/spec/support/pdf_helpers.rb b/spec/support/pdf_helpers.rb deleted file mode 100644 index 59fa012be9..0000000000 --- a/spec/support/pdf_helpers.rb +++ /dev/null @@ -1,35 +0,0 @@ -# module PdfHelpers -# def pdf_escape(string) -# CGI.escapeHTML(string) -# end -# -# def to_pdf_string(string) -# tempfile = Rails.root.join("tmp", "#{Time.zone.now.strftime('%Y%m%d%H%M%S%L')}-tmp.pdf") -# -# Prawn::Document.generate(tempfile) { text(string) } -# -# pdf_string = File.read(tempfile) -# File.delete(tempfile) -# pdf_string -# end -# -# def from_pdf_string(pdf_string) -# PDF::Reader.new(StringIO.new(pdf_string)) -# end -# -# def all_text_from_pdf_string(pdf_string) -# from_pdf_string(pdf_string).pages.map(&:text).map(&:squish).join("\n") -# end -# -# def stub_pdf_generation -# allow(PdfRenderer).to receive(:pdf_from_html).and_return(File.read("spec/fixtures/files/dummy.pdf")) -# end -# -# def html_pdf_generation -# allow(PdfRenderer).to receive(:pdf_from_html) { |args| "#{args[:header]}\n#{args[:body]}\n#{args[:footer]}" } -# end -# end -# -# RSpec.configure do |config| -# config.include PdfHelpers -# end