2014-09-24 14:45:35 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe SiteText do
|
|
|
|
|
2015-01-05 11:04:23 -05:00
|
|
|
it { is_expected.to validate_presence_of :value }
|
2014-09-24 14:45:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
describe "#text_for" do
|
|
|
|
|
|
|
|
it "returns an empty string for a missing text_type" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for('something_random')).to eq("")
|
2014-09-24 14:45:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the default value for a text` type with a default" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for("usage_tips")).to be_present
|
2014-09-24 14:45:35 -04:00
|
|
|
end
|
|
|
|
|
2014-11-11 00:28:59 -05:00
|
|
|
it "correctly expires and bypasses cache" do
|
|
|
|
SiteSetting.enable_sso = false
|
|
|
|
text = SiteText.create!(text_type: "got.sso", value: "got sso: %{enable_sso}")
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for("got.sso")).to eq("got sso: false")
|
2014-11-11 00:28:59 -05:00
|
|
|
SiteText.text_for("got.sso").frozen? == true
|
|
|
|
|
|
|
|
SiteSetting.enable_sso = true
|
2014-11-11 18:27:34 -05:00
|
|
|
wait_for do
|
|
|
|
SiteText.text_for("got.sso") == "got sso: true"
|
|
|
|
end
|
2014-11-11 00:28:59 -05:00
|
|
|
|
|
|
|
text.value = "I gots sso: %{enable_sso}"
|
|
|
|
text.save!
|
|
|
|
|
2014-11-11 18:27:34 -05:00
|
|
|
wait_for do
|
|
|
|
SiteText.text_for("got.sso") == "I gots sso: true"
|
|
|
|
end
|
|
|
|
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for("got.sso", enable_sso: "frog")).to eq("I gots sso: frog")
|
2014-11-11 00:28:59 -05:00
|
|
|
end
|
|
|
|
|
2014-09-24 14:45:35 -04:00
|
|
|
context "without replacements" do
|
|
|
|
let!(:site_text) { Fabricate(:site_text_basic) }
|
|
|
|
|
|
|
|
it "returns the simple string" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for('breaking.bad')).to eq("best show ever")
|
2014-09-24 14:45:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with replacements" do
|
|
|
|
let!(:site_text) { Fabricate(:site_text) }
|
|
|
|
let(:replacements) { {flower: 'roses', food: 'grapes'} }
|
|
|
|
|
|
|
|
it "returns the correct string with replacements" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for('great.poem', replacements)).to eq("roses are red. grapes are blue.")
|
2014-09-24 14:45:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't mind extra keys in the replacements" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for('great.poem', replacements.merge(extra: 'key'))).to eq("roses are red. grapes are blue.")
|
2014-09-24 14:45:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores missing keys" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for('great.poem', flower: 'roses')).to eq("roses are red. %{food} are blue.")
|
2014-09-24 14:45:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
context "replacing site_settings" do
|
|
|
|
let!(:site_text) { Fabricate(:site_text_site_setting) }
|
|
|
|
|
|
|
|
it "replaces site_settings by default" do
|
2014-11-11 00:28:59 -05:00
|
|
|
SiteSetting.title = "Evil Trout"
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for('site.replacement')).to eq("Evil Trout is evil.")
|
2014-09-24 14:45:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows us to override the default site settings" do
|
2014-11-11 00:28:59 -05:00
|
|
|
SiteSetting.title = "Evil Trout"
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(SiteText.text_for('site.replacement', title: 'Good Tuna')).to eq("Good Tuna is evil.")
|
2014-09-24 14:45:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|