Skip to content

Commit

Permalink
Add permitted_uses to image kind config
Browse files Browse the repository at this point in the history
This means we don't have to hardcode the fact that only default images are allowed to be embedded in govspeak. If we later have other kinds of images which are allowed to be embedded in govspeak, that could be represented in config too.

The permitted_uses array could be used in future for other situations where only some kinds of images are allowed to be used in a particular circumstance - for example in featured images, lead images, person images, etc.
  • Loading branch information
richardTowers committed Nov 14, 2024
1 parent 4fe17e9 commit e0ade76
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/helpers/govspeak_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def govspeak_html_attachment_to_html(html_attachment)

def prepare_images(images)
images
.select { |image| image.image_data&.image_kind == "default" }
.select { |image| image.image_data&.image_kind_config&.permits? "govspeak_embed" }
.select { |image| image.image_data&.all_asset_variants_uploaded? }
.map do |image|
{
Expand Down
2 changes: 2 additions & 0 deletions config/image_kinds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ default:
display_name: "Default (960 by 640)"
valid_width: 960
valid_height: 640
permitted_uses:
- govspeak_embed
versions:
- name: s960
width: 960
Expand Down
9 changes: 7 additions & 2 deletions lib/whitehall/image_kinds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@ def resize_to_fill
end

class ImageKind
attr_reader :name, :display_name, :valid_width, :valid_height, :versions
attr_reader :name, :display_name, :valid_width, :valid_height, :permitted_uses, :versions

def initialize(name, config)
@name = name
@display_name = config.fetch("display_name")
@valid_width = config.fetch("valid_width")
@valid_height = config.fetch("valid_height")
@permitted_uses = config.fetch("permitted_uses")
@versions = config.fetch("versions").map { |version_config| ImageVersion.new(version_config) }.freeze
end

def deconstruct_keys(_keys)
{ name:, display_name:, valid_width:, valid_height:, versions: }
{ name:, display_name:, valid_width:, valid_height:, permitted_uses:, versions: }
end

def version_names
versions.map(&:name)
end

def permits?(use_case)
permitted_uses.include?(use_case)
end
end

class ImageKinds
Expand Down
12 changes: 10 additions & 2 deletions test/unit/app/helpers/govspeak_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,19 @@ def edition_with_attachment(body:)
refute_select_within_html html, ".image.embedded"
end

test "should ignore images with non-default image kinds" do
test "should ignore images with image kinds which do not permit use in govspeak embeds" do
embed_code = "[Image: minister-of-funk.960x640.jpg]"
body = "#Heading\n\n#{embed_code}\n\n##Subheading"
image = build(:image)
image.image_data.image_kind = "some_non_default_image_kind"
image_data = image.image_data
def image_data.image_kind_config = Whitehall::ImageKind.new(
"some name",
"display_name" => "some display name",
"valid_width" => 0,
"valid_height" => 0,
"permitted_uses" => [],
"versions" => [],
)
document = build(:published_news_article, images: [image], body:)

html = govspeak_edition_to_html(document)
Expand Down
1 change: 1 addition & 0 deletions test/unit/app/validators/image_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def image_kind_config
Whitehall::ImageKind.new(
"some image kind",
"display_name" => "some image kind display name",
"permitted_uses" => [],
"valid_width" => 50,
"valid_height" => 33,
"versions" => [],
Expand Down
18 changes: 18 additions & 0 deletions test/unit/lib/image_kinds_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ImageKindsTest < ActiveSupport::TestCase
"display_name" => "default display name",
"valid_width" => 1,
"valid_height" => 2,
"permitted_uses" => [],
"versions" => [
{
"name" => "some name",
Expand Down Expand Up @@ -42,6 +43,7 @@ class ImageKindsTest < ActiveSupport::TestCase
"display_name" => "default display name",
"valid_width" => 0,
"valid_height" => 0,
"permitted_uses" => [],
"versions" => %w[a b c d e f g].map do
{
"name" => _1,
Expand All @@ -55,6 +57,22 @@ class ImageKindsTest < ActiveSupport::TestCase
assert_equal %w[a b c d e f g], result["default"].version_names
end

test "#permits? checks if use cases are permitted" do
image_kind = Whitehall::ImageKind.new(
"default",
"display_name" => "default display name",
"valid_width" => 0,
"valid_height" => 0,
"permitted_uses" => %w[use_case_1 use_case_2 use_case_3],
"versions" => [],
)
assert_equal(true, image_kind.permits?("use_case_1"))
assert_equal(true, image_kind.permits?("use_case_2"))
assert_equal(true, image_kind.permits?("use_case_3"))
assert_equal(false, image_kind.permits?("use_case_4"))
assert_equal(false, image_kind.permits?("use_case_5"))
end

test "raises errors when given invalid config" do
# Missing display_name / valid_width / valid_height / versions keys
assert_raise(KeyError, "key not found") { Whitehall::ImageKinds.build_image_kinds("default" => {}) }
Expand Down

0 comments on commit e0ade76

Please sign in to comment.