fix: Replace Prawn with Grover for PDF ticket generation
- Replace Prawn PDF generation with Grover (Chrome headless) for better compatibility - Add HTML-based ticket template with embedded CSS styling - Implement robust Grover loading with fallback to HTML download - Add QR code generation methods to Ticket model - Remove legacy TicketPdfGenerator service and tests - Update PDF generation in TicketsController with proper error handling The new implementation provides: - Better HTML/CSS rendering for ticket layouts - More reliable PDF generation using Chrome engine - Fallback mechanism for better user experience - Cleaner separation of template rendering and PDF conversion 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,283 +0,0 @@
|
||||
require "test_helper"
|
||||
|
||||
class TicketPdfGeneratorTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
# Stub QR code generation to avoid dependency issues
|
||||
mock_qrcode = mock("qrcode")
|
||||
mock_qrcode.stubs(:modules).returns([])
|
||||
RQRCode::QRCode.stubs(:new).returns(mock_qrcode)
|
||||
|
||||
@user = User.create!(
|
||||
email: "test@example.com",
|
||||
password: "password123",
|
||||
password_confirmation: "password123"
|
||||
)
|
||||
|
||||
@event = Event.create!(
|
||||
name: "Test Event",
|
||||
slug: "test-event",
|
||||
description: "A valid description for the test event that is long enough",
|
||||
latitude: 48.8566,
|
||||
longitude: 2.3522,
|
||||
venue_name: "Test Venue",
|
||||
venue_address: "123 Test Street",
|
||||
user: @user,
|
||||
start_time: 1.week.from_now,
|
||||
end_time: 1.week.from_now + 3.hours,
|
||||
state: :published
|
||||
)
|
||||
|
||||
@ticket_type = TicketType.create!(
|
||||
name: "General Admission",
|
||||
description: "General admission tickets with full access to the event",
|
||||
price_cents: 2500,
|
||||
quantity: 100,
|
||||
sale_start_at: Time.current,
|
||||
sale_end_at: @event.start_time - 1.hour,
|
||||
requires_id: false,
|
||||
event: @event
|
||||
)
|
||||
|
||||
@order = Order.create!(
|
||||
user: @user,
|
||||
event: @event,
|
||||
status: "paid",
|
||||
total_amount_cents: 2500
|
||||
)
|
||||
|
||||
@ticket = Ticket.create!(
|
||||
order: @order,
|
||||
ticket_type: @ticket_type,
|
||||
status: "active",
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
qr_code: "test-qr-code-123"
|
||||
)
|
||||
end
|
||||
|
||||
# === Initialization Tests ===
|
||||
|
||||
test "should initialize with ticket" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
assert_equal @ticket, generator.ticket
|
||||
end
|
||||
|
||||
# === PDF Generation Tests ===
|
||||
|
||||
test "should generate PDF for valid ticket" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
assert_not_nil pdf_string
|
||||
assert_kind_of String, pdf_string
|
||||
assert pdf_string.length > 0
|
||||
|
||||
# Check if it starts with PDF header
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
end
|
||||
|
||||
test "should include event name in PDF" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
|
||||
# Test that PDF generates successfully
|
||||
pdf_string = generator.generate
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
assert pdf_string.length > 1000, "PDF should be substantial in size"
|
||||
end
|
||||
|
||||
test "should include ticket type information in PDF" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
# Basic check that PDF was generated - actual content validation
|
||||
# would require parsing the PDF which is complex
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.length > 0
|
||||
end
|
||||
|
||||
test "should include price information in PDF" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.length > 0
|
||||
end
|
||||
|
||||
test "should include venue information in PDF" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.length > 0
|
||||
end
|
||||
|
||||
test "should include QR code in PDF" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
|
||||
# Just test that PDF generates successfully
|
||||
pdf_string = generator.generate
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.length > 0
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
end
|
||||
|
||||
# === Error Handling Tests ===
|
||||
|
||||
test "should raise error when QR code is blank" do
|
||||
# Create ticket with blank QR code (skip validations)
|
||||
ticket_with_blank_qr = Ticket.new(
|
||||
order: @order,
|
||||
ticket_type: @ticket_type,
|
||||
status: "active",
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
price_cents: 2500,
|
||||
qr_code: ""
|
||||
)
|
||||
ticket_with_blank_qr.save(validate: false)
|
||||
|
||||
generator = TicketPdfGenerator.new(ticket_with_blank_qr)
|
||||
|
||||
error = assert_raises(RuntimeError) do
|
||||
generator.generate
|
||||
end
|
||||
|
||||
assert_equal "Ticket QR code is missing", error.message
|
||||
end
|
||||
|
||||
test "should raise error when QR code is nil" do
|
||||
# Create ticket with nil QR code (skip validations)
|
||||
ticket_with_nil_qr = Ticket.new(
|
||||
order: @order,
|
||||
ticket_type: @ticket_type,
|
||||
status: "active",
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
price_cents: 2500,
|
||||
qr_code: nil
|
||||
)
|
||||
ticket_with_nil_qr.save(validate: false)
|
||||
|
||||
generator = TicketPdfGenerator.new(ticket_with_nil_qr)
|
||||
|
||||
error = assert_raises(RuntimeError) do
|
||||
generator.generate
|
||||
end
|
||||
|
||||
assert_equal "Ticket QR code is missing", error.message
|
||||
end
|
||||
|
||||
test "should handle missing event gracefully in QR data" do
|
||||
# Create ticket with minimal data but valid QR code
|
||||
orphaned_ticket = Ticket.new(
|
||||
order: @order,
|
||||
ticket_type: @ticket_type,
|
||||
status: "active",
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
price_cents: 2500,
|
||||
qr_code: "test-qr-code-orphaned"
|
||||
)
|
||||
orphaned_ticket.save(validate: false)
|
||||
|
||||
generator = TicketPdfGenerator.new(orphaned_ticket)
|
||||
|
||||
# Should still generate PDF
|
||||
pdf_string = generator.generate
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.length > 0
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
end
|
||||
|
||||
# === QR Code Data Tests ===
|
||||
|
||||
test "should generate correct QR code data" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
|
||||
# Just test that PDF generates successfully with QR data
|
||||
pdf_string = generator.generate
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
end
|
||||
|
||||
test "should compact QR code data removing nils" do
|
||||
# Test with a ticket that has unique QR code
|
||||
ticket_with_minimal_data = Ticket.new(
|
||||
order: @order,
|
||||
ticket_type: @ticket_type,
|
||||
status: "active",
|
||||
first_name: "Jane",
|
||||
last_name: "Smith",
|
||||
price_cents: 2500,
|
||||
qr_code: "test-qr-minimal-data"
|
||||
)
|
||||
ticket_with_minimal_data.save(validate: false)
|
||||
|
||||
generator = TicketPdfGenerator.new(ticket_with_minimal_data)
|
||||
|
||||
# Should generate PDF successfully
|
||||
pdf_string = generator.generate
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
end
|
||||
|
||||
# === Price Display Tests ===
|
||||
|
||||
test "should format price correctly in euros" do
|
||||
# Test different price formats
|
||||
@ticket.update!(price_cents: 1050) # €10.50
|
||||
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
assert_not_nil pdf_string
|
||||
assert_equal 10.5, @ticket.price_euros
|
||||
end
|
||||
|
||||
test "should handle low price" do
|
||||
@ticket_type.update!(price_cents: 1)
|
||||
@ticket.update!(price_cents: 1)
|
||||
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
assert_not_nil pdf_string
|
||||
assert_equal 0.01, @ticket.price_euros
|
||||
end
|
||||
|
||||
# === Date Formatting Tests ===
|
||||
|
||||
test "should format event date correctly" do
|
||||
specific_time = Time.parse("2024-12-25 19:30:00")
|
||||
@event.update!(start_time: specific_time)
|
||||
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
# Just verify PDF generates - date formatting is handled by strftime
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.length > 0
|
||||
end
|
||||
|
||||
# === Integration Tests ===
|
||||
|
||||
test "should generate valid PDF with all required elements" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
# Basic PDF structure validation
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
assert pdf_string.end_with?("%%EOF\n")
|
||||
assert pdf_string.length > 1000, "PDF should be substantial in size"
|
||||
end
|
||||
|
||||
test "should be callable from ticket model" do
|
||||
# Test the integration with the Ticket model's to_pdf method
|
||||
pdf_string = @ticket.to_pdf
|
||||
|
||||
assert_not_nil pdf_string
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user