2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-11 12:53:17 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "database/setup"
|
2019-10-01 22:17:05 -04:00
|
|
|
require "minitest/mock"
|
2017-07-11 12:53:17 -04:00
|
|
|
|
2017-07-21 16:52:09 -04:00
|
|
|
class ActiveStorage::VariantTest < ActiveSupport::TestCase
|
2019-12-05 22:21:24 -05:00
|
|
|
setup do
|
|
|
|
@was_tracking, ActiveStorage.track_variants = ActiveStorage.track_variants, false
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActiveStorage.track_variants = @was_tracking
|
|
|
|
end
|
|
|
|
|
2019-04-20 06:05:58 -04:00
|
|
|
test "variations have the same key for different types of the same transformation" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant_a = blob.variant(resize: "100x100")
|
|
|
|
variant_b = blob.variant("resize" => "100x100")
|
|
|
|
|
|
|
|
assert_equal variant_a.key, variant_b.key
|
|
|
|
end
|
|
|
|
|
2017-12-18 07:47:42 -05:00
|
|
|
test "resized variation of JPEG blob" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
2018-04-23 06:21:42 -04:00
|
|
|
variant = blob.variant(resize: "100x100").processed
|
2019-12-02 11:34:01 -05:00
|
|
|
assert_match(/racecar\.jpg/, variant.url)
|
2017-07-26 14:58:59 -04:00
|
|
|
|
2017-09-28 16:43:37 -04:00
|
|
|
image = read_image(variant)
|
2017-07-26 14:58:59 -04:00
|
|
|
assert_equal 100, image.width
|
|
|
|
assert_equal 67, image.height
|
2017-07-21 16:51:31 -04:00
|
|
|
end
|
2017-07-11 12:53:17 -04:00
|
|
|
|
2017-12-18 07:47:42 -05:00
|
|
|
test "resized and monochrome variation of JPEG blob" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
2018-04-23 06:21:42 -04:00
|
|
|
variant = blob.variant(resize: "100x100", monochrome: true).processed
|
2019-12-02 11:34:01 -05:00
|
|
|
assert_match(/racecar\.jpg/, variant.url)
|
2017-07-26 14:58:59 -04:00
|
|
|
|
2017-09-28 16:43:37 -04:00
|
|
|
image = read_image(variant)
|
2017-07-26 14:58:59 -04:00
|
|
|
assert_equal 100, image.width
|
|
|
|
assert_equal 67, image.height
|
2017-08-04 23:21:21 -04:00
|
|
|
assert_match(/Gray/, image.colorspace)
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|
2017-10-04 15:26:04 -04:00
|
|
|
|
Disable variant options when false or nil present
In response to https://github.com/rails/rails/issues/32917
In the current implementation, ActiveStorage passes all options to the underlying processor,
including when a key has a value of false.
For example, passing:
```
avatar.variant(resize: "100x100", monochrome: false, flip: "-90")
```
will return a monochrome image (or an error, pending on ImageMagick configuration) because
it passes `-monochrome false` to the command (but the command line does not allow disabling
flags this way, as usually a user would omit the flag entirely to disable that feature).
This fix only passes those keys forward to the underlying processor if the value responds to
`present?`. In practice, this means that `false` or `nil` will be filtered out before going
to the processor.
One possible use case would be for a user to be able to apply different filters to an avatar.
The code might look something like:
```
variant_options = {
monochrome: params[:monochrome],
resize: params[:resize]
}
avatar.variant(*variant_options)
```
Obviously some sanitization may be beneficial in a real-world scenario, but this type of
configuration object could be used in many other places as well.
- Add removing falsy values from varaints to changelog
- The entirety of #image_processing_transformation inject block was wrapped in `list.tap`
to guard against the default `nil` being returned if no conditional was called.
- add test for explicitly true variant options
2018-05-19 22:14:57 -04:00
|
|
|
test "monochrome with default variant_processor" do
|
2018-12-20 12:44:01 -05:00
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(monochrome: true).processed
|
|
|
|
image = read_image(variant)
|
|
|
|
assert_match(/Gray/, image.colorspace)
|
Disable variant options when false or nil present
In response to https://github.com/rails/rails/issues/32917
In the current implementation, ActiveStorage passes all options to the underlying processor,
including when a key has a value of false.
For example, passing:
```
avatar.variant(resize: "100x100", monochrome: false, flip: "-90")
```
will return a monochrome image (or an error, pending on ImageMagick configuration) because
it passes `-monochrome false` to the command (but the command line does not allow disabling
flags this way, as usually a user would omit the flag entirely to disable that feature).
This fix only passes those keys forward to the underlying processor if the value responds to
`present?`. In practice, this means that `false` or `nil` will be filtered out before going
to the processor.
One possible use case would be for a user to be able to apply different filters to an avatar.
The code might look something like:
```
variant_options = {
monochrome: params[:monochrome],
resize: params[:resize]
}
avatar.variant(*variant_options)
```
Obviously some sanitization may be beneficial in a real-world scenario, but this type of
configuration object could be used in many other places as well.
- Add removing falsy values from varaints to changelog
- The entirety of #image_processing_transformation inject block was wrapped in `list.tap`
to guard against the default `nil` being returned if no conditional was called.
- add test for explicitly true variant options
2018-05-19 22:14:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "disabled variation of JPEG blob" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(resize: "100x100", monochrome: false).processed
|
2019-12-02 11:34:01 -05:00
|
|
|
assert_match(/racecar\.jpg/, variant.url)
|
Disable variant options when false or nil present
In response to https://github.com/rails/rails/issues/32917
In the current implementation, ActiveStorage passes all options to the underlying processor,
including when a key has a value of false.
For example, passing:
```
avatar.variant(resize: "100x100", monochrome: false, flip: "-90")
```
will return a monochrome image (or an error, pending on ImageMagick configuration) because
it passes `-monochrome false` to the command (but the command line does not allow disabling
flags this way, as usually a user would omit the flag entirely to disable that feature).
This fix only passes those keys forward to the underlying processor if the value responds to
`present?`. In practice, this means that `false` or `nil` will be filtered out before going
to the processor.
One possible use case would be for a user to be able to apply different filters to an avatar.
The code might look something like:
```
variant_options = {
monochrome: params[:monochrome],
resize: params[:resize]
}
avatar.variant(*variant_options)
```
Obviously some sanitization may be beneficial in a real-world scenario, but this type of
configuration object could be used in many other places as well.
- Add removing falsy values from varaints to changelog
- The entirety of #image_processing_transformation inject block was wrapped in `list.tap`
to guard against the default `nil` being returned if no conditional was called.
- add test for explicitly true variant options
2018-05-19 22:14:57 -04:00
|
|
|
|
|
|
|
image = read_image(variant)
|
|
|
|
assert_equal 100, image.width
|
|
|
|
assert_equal 67, image.height
|
|
|
|
assert_match(/RGB/, image.colorspace)
|
|
|
|
end
|
|
|
|
|
2018-04-23 06:21:42 -04:00
|
|
|
test "center-weighted crop of JPEG blob using :resize_to_fill" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(resize_to_fill: [100, 100]).processed
|
2019-12-02 11:34:01 -05:00
|
|
|
assert_match(/racecar\.jpg/, variant.url)
|
2018-04-23 06:21:42 -04:00
|
|
|
|
|
|
|
image = read_image(variant)
|
|
|
|
assert_equal 100, image.width
|
|
|
|
assert_equal 100, image.height
|
|
|
|
end
|
|
|
|
|
2017-12-18 07:47:42 -05:00
|
|
|
test "resized variation of PSD blob" do
|
|
|
|
blob = create_file_blob(filename: "icon.psd", content_type: "image/vnd.adobe.photoshop")
|
|
|
|
variant = blob.variant(resize: "20x20").processed
|
2019-12-02 11:34:01 -05:00
|
|
|
assert_match(/icon\.png/, variant.url)
|
2017-12-18 07:47:42 -05:00
|
|
|
|
|
|
|
image = read_image(variant)
|
|
|
|
assert_equal "PNG", image.type
|
|
|
|
assert_equal 20, image.width
|
|
|
|
assert_equal 20, image.height
|
|
|
|
end
|
|
|
|
|
2018-02-24 15:27:53 -05:00
|
|
|
test "resized variation of ICO blob" do
|
|
|
|
blob = create_file_blob(filename: "favicon.ico", content_type: "image/vnd.microsoft.icon")
|
|
|
|
variant = blob.variant(resize: "20x20").processed
|
2019-12-02 11:34:01 -05:00
|
|
|
assert_match(/icon\.png/, variant.url)
|
2018-02-24 15:27:53 -05:00
|
|
|
|
|
|
|
image = read_image(variant)
|
|
|
|
assert_equal "PNG", image.type
|
|
|
|
assert_equal 20, image.width
|
|
|
|
assert_equal 20, image.height
|
|
|
|
end
|
|
|
|
|
2018-12-30 18:14:49 -05:00
|
|
|
test "resized variation of TIFF blob" do
|
|
|
|
blob = create_file_blob(filename: "racecar.tif")
|
|
|
|
variant = blob.variant(resize: "50x50").processed
|
2019-12-02 11:34:01 -05:00
|
|
|
assert_match(/racecar\.png/, variant.url)
|
2018-12-30 18:14:49 -05:00
|
|
|
|
|
|
|
image = read_image(variant)
|
|
|
|
assert_equal "PNG", image.type
|
|
|
|
assert_equal 50, image.width
|
|
|
|
assert_equal 33, image.height
|
|
|
|
end
|
|
|
|
|
2019-04-21 13:07:22 -04:00
|
|
|
test "resized variation of BMP blob" do
|
2020-07-29 17:02:32 -04:00
|
|
|
blob = create_file_blob(filename: "colors.bmp", content_type: "image/bmp")
|
2019-04-21 13:07:22 -04:00
|
|
|
variant = blob.variant(resize: "15x15").processed
|
2020-07-29 17:02:32 -04:00
|
|
|
assert_match(/colors\.png/, variant.url)
|
2019-04-21 13:07:22 -04:00
|
|
|
|
|
|
|
image = read_image(variant)
|
2020-07-29 17:02:32 -04:00
|
|
|
assert_equal "PNG", image.type
|
2019-04-21 13:07:22 -04:00
|
|
|
assert_equal 15, image.width
|
|
|
|
assert_equal 8, image.height
|
|
|
|
end
|
|
|
|
|
2018-01-02 07:19:39 -05:00
|
|
|
test "optimized variation of GIF blob" do
|
|
|
|
blob = create_file_blob(filename: "image.gif", content_type: "image/gif")
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
blob.variant(layers: "Optimize").processed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-04 22:44:02 -04:00
|
|
|
test "PNG variation of JPEG blob with lowercase format" do
|
2020-09-22 13:03:53 -04:00
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(format: :png).processed
|
|
|
|
assert_equal "racecar.png", variant.filename.to_s
|
|
|
|
assert_equal "image/png", variant.content_type
|
|
|
|
assert_equal "PNG", read_image(variant).type
|
|
|
|
end
|
|
|
|
|
2021-04-04 22:44:02 -04:00
|
|
|
test "PNG variation of JPEG blob with uppercase format" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(format: "PNG").processed
|
|
|
|
assert_equal "racecar.png", variant.filename.to_s
|
|
|
|
assert_equal "image/png", variant.content_type
|
|
|
|
assert_equal "PNG", read_image(variant).type
|
|
|
|
end
|
|
|
|
|
2017-12-15 10:45:00 -05:00
|
|
|
test "variation of invariable blob" do
|
2018-01-10 12:12:04 -05:00
|
|
|
assert_raises ActiveStorage::InvariableError do
|
2017-12-15 10:45:00 -05:00
|
|
|
create_file_blob(filename: "report.pdf", content_type: "application/pdf").variant(resize: "100x100")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-02 11:34:01 -05:00
|
|
|
test "url doesn't grow in length despite long variant options" do
|
2017-12-18 07:47:42 -05:00
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(font: "a" * 10_000).processed
|
2019-12-05 22:31:23 -05:00
|
|
|
assert_operator variant.url.length, :<, 785
|
2017-10-04 15:26:04 -04:00
|
|
|
end
|
2018-04-05 19:48:29 -04:00
|
|
|
|
2020-09-22 13:03:53 -04:00
|
|
|
test "thumbnail variation of JPEG blob processed with VIPS" do
|
|
|
|
process_variants_with :vips do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(thumbnail_image: 100).processed
|
2018-12-20 12:44:01 -05:00
|
|
|
|
2020-09-22 13:03:53 -04:00
|
|
|
image = read_image(variant)
|
|
|
|
assert_equal 100, image.width
|
|
|
|
assert_equal 67, image.height
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "thumbnail variation of extensionless GIF blob processed with VIPS" do
|
|
|
|
process_variants_with :vips do
|
|
|
|
blob = ActiveStorage::Blob.create_and_upload!(io: file_fixture("image.gif").open, filename: "image", content_type: "image/gif")
|
|
|
|
variant = blob.variant(resize_to_fit: [100, 100]).processed
|
|
|
|
|
|
|
|
image = read_image(variant)
|
|
|
|
assert_equal "image.gif", variant.filename.to_s
|
|
|
|
assert_equal "image/gif", variant.content_type
|
|
|
|
assert_equal "GIF", image.type
|
|
|
|
assert_equal 100, image.width
|
|
|
|
assert_equal 100, image.height
|
|
|
|
end
|
2018-04-05 19:48:29 -04:00
|
|
|
end
|
2019-09-17 00:08:22 -04:00
|
|
|
|
|
|
|
test "passes content_type on upload" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")
|
|
|
|
|
|
|
|
mock_upload = lambda do |_, _, options = {}|
|
2019-10-01 22:20:50 -04:00
|
|
|
assert_equal "image/jpeg", options[:content_type]
|
2019-09-17 00:08:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
blob.service.stub(:upload, mock_upload) do
|
|
|
|
blob.variant(resize: "100x100").processed
|
|
|
|
end
|
|
|
|
end
|
2020-09-22 13:03:53 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def process_variants_with(processor)
|
|
|
|
previous_processor, ActiveStorage.variant_processor = ActiveStorage.variant_processor, processor
|
|
|
|
yield
|
|
|
|
rescue LoadError
|
|
|
|
skip "Variant processor #{processor.inspect} is not installed"
|
|
|
|
ensure
|
|
|
|
ActiveStorage.variant_processor = previous_processor
|
|
|
|
end
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|