mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-25 01:23:23 +00:00
Finding unique permalink before validation.
This commit is contained in:
@@ -97,7 +97,6 @@ FactoryGirl.define do
|
||||
factory :enterprise, :class => Enterprise do
|
||||
owner { FactoryGirl.create :user }
|
||||
sequence(:name) { |n| "Enterprise #{n}" }
|
||||
sequence(:permalink) { |n| "enterprise#{n}" }
|
||||
sells 'any'
|
||||
description 'enterprise'
|
||||
long_description '<p>Hello, world!</p><p>This is a paragraph.</p>'
|
||||
|
||||
@@ -2,11 +2,32 @@ require 'spec_helper'
|
||||
|
||||
describe EnterpriseGroup do
|
||||
describe "validations" do
|
||||
it "pass with name, description and address" do
|
||||
e = EnterpriseGroup.new
|
||||
e.name = 'Test Group'
|
||||
e.description = 'A valid test group.'
|
||||
e.address = build(:address)
|
||||
e.should be_valid
|
||||
end
|
||||
|
||||
it "is valid when built from factory" do
|
||||
e = build(:enterprise_group)
|
||||
e.should be_valid
|
||||
end
|
||||
|
||||
it "replace empty permalink and pass" do
|
||||
e = build(:enterprise_group, permalink: '')
|
||||
e.should be_valid
|
||||
e.permalink.should == e.name.parameterize
|
||||
end
|
||||
|
||||
it "restores permalink and pass" do
|
||||
e = create(:enterprise_group, permalink: 'p')
|
||||
e.permalink = ''
|
||||
e.should be_valid
|
||||
e.permalink.should == 'p'
|
||||
end
|
||||
|
||||
it "requires a name" do
|
||||
e = build(:enterprise_group, name: '')
|
||||
e.should_not be_valid
|
||||
@@ -60,5 +81,39 @@ describe EnterpriseGroup do
|
||||
|
||||
EnterpriseGroup.managed_by(user).should == [eg1]
|
||||
end
|
||||
|
||||
describe "finding a permalink" do
|
||||
it "finds available permalink" do
|
||||
existing = []
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink"
|
||||
end
|
||||
|
||||
it "finds available permalink similar to existing" do
|
||||
existing = ["permalink1"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink"
|
||||
end
|
||||
|
||||
it "adds unique number to existing permalinks" do
|
||||
existing = ["permalink"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink1"
|
||||
existing = ["permalink", "permalink1"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "ignores permalinks with characters after the index value" do
|
||||
existing = ["permalink", "permalink1", "permalink2xxx"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "finds gaps in the indices of existing permalinks" do
|
||||
existing = ["permalink", "permalink1", "permalink3"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "finds available indexed permalink" do
|
||||
existing = ["permalink", "permalink1"]
|
||||
expect(EnterpriseGroup.find_available_value(existing, "permalink1")).to eq "permalink11"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -845,6 +845,11 @@ describe Enterprise do
|
||||
expect(Enterprise.find_available_permalink("permalink")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "finds available permalink similar to existing" do
|
||||
create(:enterprise, permalink: "permalink2xxx")
|
||||
expect(Enterprise.find_available_permalink("permalink2")).to eq "permalink2"
|
||||
end
|
||||
|
||||
it "finds gaps in the indices of existing permalinks" do
|
||||
create(:enterprise, permalink: "permalink3")
|
||||
expect(Enterprise.find_available_permalink("permalink")).to eq "permalink2"
|
||||
|
||||
Reference in New Issue
Block a user