rails/activestorage/test/models/preview_test.rb
Vincent Robert 3803671a81 Generate a preview without print margins
When a PDF is used for both printing and displaying. It will most likely
contain a crop box in order to hide print margins when displaying the PDF.

Use Poppler's parameter to automatically use the crop box (visible box)
rather than the media box (printable box) in order to remove those margins
when drawing the PDF.

See https://manpages.debian.org/testing/poppler-utils/pdftoppm.1.en.html
2020-09-17 11:59:45 -04:00

79 lines
2.6 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
require "database/setup"
class ActiveStorage::PreviewTest < ActiveSupport::TestCase
test "previewing a PDF" do
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
preview = blob.preview(resize: "640x280").processed
assert_predicate preview.image, :attached?
assert_equal "report.png", preview.image.filename.to_s
assert_equal "image/png", preview.image.content_type
image = read_image(preview.image)
assert_equal 612, image.width
assert_equal 792, image.height
end
test "previewing a cropped PDF" do
blob = create_file_blob(filename: "cropped.pdf", content_type: "application/pdf")
preview = blob.preview(resize: "640x280").processed
assert_predicate preview.image, :attached?
assert_equal "cropped.png", preview.image.filename.to_s
assert_equal "image/png", preview.image.content_type
image = read_image(preview.image)
assert_equal 430, image.width
assert_equal 145, image.height
end
test "previewing an MP4 video" do
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4")
preview = blob.preview(resize: "640x280").processed
assert_predicate preview.image, :attached?
assert_equal "video.jpg", preview.image.filename.to_s
assert_equal "image/jpeg", preview.image.content_type
image = read_image(preview.image)
assert_equal 640, image.width
assert_equal 480, image.height
end
test "previewing an unpreviewable blob" do
blob = create_file_blob
assert_raises ActiveStorage::UnpreviewableError do
blob.preview resize: "640x280"
end
end
test "previewing on the writer DB" do
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
# Simulate a selector middleware switching to a read-only replica.
ActiveRecord::Base.connection_handler.while_preventing_writes do
blob.preview(resize: "640x280").processed
end
assert blob.reload.preview_image.attached?
end
test "preview of PDF is created on the same service" do
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf", service_name: "local_public")
preview = blob.preview(resize: "640x280").processed
assert_equal "local_public", preview.image.blob.service_name
end
test "preview of MP4 video is created on the same service" do
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4", service_name: "local_public")
preview = blob.preview(resize: "640x280").processed
assert_equal "local_public", preview.image.blob.service_name
end
end