Skip to content

Commit

Permalink
Merge pull request #3183 from AlchemyCMS/backport/7.4-stable/pr-3182
Browse files Browse the repository at this point in the history
[7.4-stable] Add specs for format matchers
  • Loading branch information
tvdeyen authored Feb 5, 2025
2 parents cf8a4c5 + 4f86fcf commit 86e276f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions config/alchemy/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ link_target_options: [blank]
# validates_format_of :url, with: Alchemy::Config.get('format_matchers')['url']
#
format_matchers:
email: !ruby/regexp '/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/'
url: !ruby/regexp '/\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?\z/ix'
link_url: !ruby/regexp '/^(tel:|mailto:|\/|[a-z]+:\/\/)/'
email: !ruby/regexp /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
url: !ruby/regexp /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?\z/ix
link_url: !ruby/regexp /^(tel:|mailto:|\/|[a-z]+:\/\/)/

# The layout used for rendering the +alchemy/admin/pages#show+ action.
admin_page_preview_layout: application
Expand Down
7 changes: 7 additions & 0 deletions spec/dummy/config/alchemy/elements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,10 @@

- name: element_with_warning
warning: "Do not use this element!"

- name: element_with_url
ingredients:
- role: url
type: Text
validate:
- format: url
1 change: 1 addition & 0 deletions spec/libraries/alchemy/tasks/usage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
{"name" => "contactform", "count" => 0},
{"name" => "download", "count" => 0},
{"name" => "element_with_ingredient_groups", "count" => 0},
{"name" => "element_with_url", "count" => 0},
{"name" => "element_with_warning", "count" => 0},
{"name" => "erb_cell", "count" => 0},
{"name" => "erb_element", "count" => 0},
Expand Down
27 changes: 27 additions & 0 deletions spec/libraries/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,32 @@ module Alchemy
end
end
end

describe "format matchers" do
describe "email" do
subject { Alchemy::Config.get("format_matchers")["email"] }

it { is_expected.to match("[email protected]") }
it { is_expected.not_to match("stulli@gmx") }
end

describe "url" do
subject { Alchemy::Config.get("format_matchers")["url"] }

it { is_expected.to match("www.example.com:80/about") }
it { is_expected.not_to match('www.example.com:80\/about') }
end

describe "link_url" do
subject { Alchemy::Config.get("format_matchers")["link_url"] }

it { is_expected.to match("tel:12345") }
it { is_expected.to match("mailto:[email protected]") }
it { is_expected.to match("/home") }
it { is_expected.to match("https://example.com/home") }
it { is_expected.not_to match('\/brehmstierleben') }
it { is_expected.not_to match('https:\/\/example.com/home') }
end
end
end
end
22 changes: 22 additions & 0 deletions spec/models/alchemy/ingredient_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@
end
end

context "an element with url format validation" do
let(:element) { create(:alchemy_element, :with_ingredients, name: "element_with_url") }
let(:ingredient) { element.ingredient_by_role(:url) }

before do
expect(ingredient).to receive(:value).at_least(:once) { value }
validate
end

context "with a slash" do
let(:value) { "www.example.com:80/about" }

it { expect(ingredient.errors).to be_blank }
end

context "and the value is not matching" do
let(:value) { 'www.example.com:80\/about' }

it { expect(ingredient.errors).to be_present }
end
end

context "with an ingredient having length validation" do
let(:element) { create(:alchemy_element, :with_ingredients, name: "all_you_can_eat") }
let(:ingredient) { element.ingredient_by_role(:headline) }
Expand Down

0 comments on commit 86e276f

Please sign in to comment.