2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
require "discourse_plugin_registry"
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe DiscoursePluginRegistry do
|
2023-01-09 05:18:21 -06:00
|
|
|
class TestRegistry < DiscoursePluginRegistry
|
|
|
|
end
|
2014-04-01 00:08:18 -05:00
|
|
|
|
|
|
|
let(:registry) { TestRegistry }
|
|
|
|
let(:registry_instance) { registry.new }
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".define_register" do
|
2020-05-15 08:04:38 -05:00
|
|
|
let(:fresh_registry) { Class.new(TestRegistry) }
|
|
|
|
|
|
|
|
let(:plugin_class) do
|
|
|
|
Class.new(Plugin::Instance) do
|
|
|
|
attr_accessor :enabled
|
|
|
|
def enabled?
|
|
|
|
@enabled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:plugin) { plugin_class.new }
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "works for a set" do
|
2020-05-15 08:04:38 -05:00
|
|
|
fresh_registry.define_register(:test_things, Set)
|
|
|
|
fresh_registry.test_things << "My Thing"
|
|
|
|
expect(fresh_registry.test_things).to contain_exactly("My Thing")
|
|
|
|
fresh_registry.reset!
|
|
|
|
expect(fresh_registry.test_things.length).to eq(0)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "works for a hash" do
|
2020-05-15 08:04:38 -05:00
|
|
|
fresh_registry.define_register(:test_things, Hash)
|
|
|
|
fresh_registry.test_things[:test] = "hello world"
|
|
|
|
expect(fresh_registry.test_things[:test]).to eq("hello world")
|
|
|
|
fresh_registry.reset!
|
|
|
|
expect(fresh_registry.test_things[:test]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".define_filtered_register" do
|
|
|
|
it "works" do
|
2020-05-15 08:04:38 -05:00
|
|
|
fresh_registry.define_filtered_register(:test_things)
|
|
|
|
expect(fresh_registry.test_things.length).to eq(0)
|
|
|
|
|
|
|
|
fresh_registry.register_test_thing("mything", plugin)
|
|
|
|
|
|
|
|
plugin.enabled = true
|
|
|
|
expect(fresh_registry.test_things).to contain_exactly("mything")
|
|
|
|
|
|
|
|
plugin.enabled = false
|
|
|
|
expect(fresh_registry.test_things.length).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#stylesheets" do
|
|
|
|
it "defaults to an empty Set" do
|
2020-05-13 06:25:34 -05:00
|
|
|
registry.reset!
|
2019-08-20 11:39:52 -05:00
|
|
|
expect(registry.stylesheets).to eq(Hash.new)
|
2013-02-08 01:56:12 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#mobile_stylesheets" do
|
|
|
|
it "defaults to an empty Set" do
|
2020-05-13 06:25:34 -05:00
|
|
|
registry.reset!
|
2019-08-20 11:39:52 -05:00
|
|
|
expect(registry.mobile_stylesheets).to eq(Hash.new)
|
2014-04-07 09:33:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#javascripts" do
|
|
|
|
it "defaults to an empty Set" do
|
2020-05-13 06:25:34 -05:00
|
|
|
registry.reset!
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(registry.javascripts).to eq(Set.new)
|
2013-02-08 01:56:12 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#auth_providers" do
|
|
|
|
it "defaults to an empty Set" do
|
2020-05-13 06:25:34 -05:00
|
|
|
registry.reset!
|
2018-07-23 10:51:57 -05:00
|
|
|
expect(registry.auth_providers).to eq(Set.new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#seed_data" do
|
|
|
|
it "defaults to an empty Set" do
|
2020-05-13 06:25:34 -05:00
|
|
|
registry.reset!
|
2015-06-04 14:56:17 -05:00
|
|
|
expect(registry.seed_data).to be_a(Hash)
|
|
|
|
expect(registry.seed_data.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-26 00:16:03 -05:00
|
|
|
describe "#mail_pollers" do
|
|
|
|
it "defaults to an empty Set" do
|
|
|
|
registry.reset!
|
|
|
|
expect(registry.mail_pollers).to eq(Set.new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".register_html_builder" do
|
2017-04-17 14:47:21 -05:00
|
|
|
it "can register and build html" do
|
|
|
|
DiscoursePluginRegistry.register_html_builder(:my_html) { "<b>my html</b>" }
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(DiscoursePluginRegistry.build_html(:my_html)).to eq("<b>my html</b>")
|
2017-04-17 14:47:21 -05:00
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
expect(DiscoursePluginRegistry.build_html(:my_html)).to be_blank
|
|
|
|
end
|
2017-11-03 10:32:32 -05:00
|
|
|
|
|
|
|
it "can register multiple builders" do
|
|
|
|
DiscoursePluginRegistry.register_html_builder(:my_html) { "one" }
|
|
|
|
DiscoursePluginRegistry.register_html_builder(:my_html) { "two" }
|
|
|
|
expect(DiscoursePluginRegistry.build_html(:my_html)).to eq("one\ntwo")
|
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
end
|
2017-04-17 14:47:21 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".register_css" do
|
2019-08-20 11:39:52 -05:00
|
|
|
let(:plugin_directory_name) { "hello" }
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
before { registry_instance.register_css("hello.css", plugin_directory_name) }
|
2014-04-01 00:08:18 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "is not leaking" do
|
2019-08-20 11:39:52 -05:00
|
|
|
expect(DiscoursePluginRegistry.new.stylesheets[plugin_directory_name]).to be_nil
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "is returned by DiscoursePluginRegistry.stylesheets" do
|
|
|
|
expect(registry_instance.stylesheets[plugin_directory_name].include?("hello.css")).to eq(true)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't add the same file twice" do
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { registry_instance.register_css("hello.css", plugin_directory_name) }.not_to change(
|
|
|
|
registry.stylesheets[plugin_directory_name],
|
|
|
|
:size,
|
|
|
|
)
|
2013-02-25 10:42:20 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".register_js" do
|
|
|
|
before { registry_instance.register_js("hello.js") }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "is returned by DiscoursePluginRegistry.javascripts" do
|
|
|
|
expect(registry_instance.javascripts.include?("hello.js")).to eq(true)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't add the same file twice" do
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { registry_instance.register_js("hello.js") }.not_to change(
|
|
|
|
registry.javascripts,
|
|
|
|
:size,
|
|
|
|
)
|
2013-02-25 10:42:20 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".register_auth_provider" do
|
2018-07-23 10:51:57 -05:00
|
|
|
let(:registry) { DiscoursePluginRegistry }
|
|
|
|
let(:auth_provider) do
|
2018-07-31 10:18:50 -05:00
|
|
|
provider = Auth::AuthProvider.new
|
2018-07-23 10:51:57 -05:00
|
|
|
provider.authenticator = Auth::Authenticator.new
|
|
|
|
provider
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
before { registry.register_auth_provider(auth_provider) }
|
2018-07-23 10:51:57 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
after { registry.reset! }
|
2018-07-23 10:51:57 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "is returned by DiscoursePluginRegistry.auth_providers" do
|
2018-07-23 10:51:57 -05:00
|
|
|
expect(registry.auth_providers.include?(auth_provider)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".register_service_worker" do
|
2017-11-22 19:02:01 -06:00
|
|
|
let(:registry) { DiscoursePluginRegistry }
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
before { registry.register_service_worker("hello.js") }
|
2017-11-22 19:02:01 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
after { registry.reset! }
|
2017-11-22 19:02:01 -06:00
|
|
|
|
|
|
|
it "should register the file once" do
|
2023-01-09 05:18:21 -06:00
|
|
|
2.times { registry.register_service_worker("hello.js") }
|
2017-11-22 19:02:01 -06:00
|
|
|
|
|
|
|
expect(registry.service_workers.size).to eq(1)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(registry.service_workers).to include("hello.js")
|
2017-11-22 19:02:01 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".register_archetype" do
|
2013-02-05 13:16:51 -06:00
|
|
|
it "delegates archetypes to the Archetype component" do
|
2023-01-09 05:18:21 -06:00
|
|
|
Archetype.expects(:register).with("threaded", { hello: 123 })
|
|
|
|
registry_instance.register_archetype("threaded", hello: 123)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#register_asset" do
|
2014-12-09 13:20:53 -06:00
|
|
|
let(:registry) { DiscoursePluginRegistry }
|
2019-08-22 00:32:18 -05:00
|
|
|
let(:plugin_directory_name) { "my_plugin" }
|
2014-12-09 13:20:53 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
after { registry.reset! }
|
2014-12-09 13:20:53 -06:00
|
|
|
|
|
|
|
it "does register general css properly" do
|
2019-08-20 11:39:52 -05:00
|
|
|
registry.register_asset("test.css", nil, plugin_directory_name)
|
|
|
|
registry.register_asset("test2.css", nil, plugin_directory_name)
|
2014-12-09 13:20:53 -06:00
|
|
|
|
2019-08-20 11:39:52 -05:00
|
|
|
expect(registry.mobile_stylesheets[plugin_directory_name]).to be_nil
|
|
|
|
expect(registry.stylesheets[plugin_directory_name].count).to eq(2)
|
2014-12-09 13:20:53 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers desktop css properly" do
|
2019-08-22 00:32:18 -05:00
|
|
|
registry.register_asset("test.css", :desktop, plugin_directory_name)
|
2014-12-09 13:20:53 -06:00
|
|
|
|
2019-08-22 00:32:18 -05:00
|
|
|
expect(registry.desktop_stylesheets[plugin_directory_name].count).to eq(1)
|
2019-08-22 01:22:52 -05:00
|
|
|
expect(registry.stylesheets[plugin_directory_name]).to eq(nil)
|
|
|
|
expect(registry.mobile_stylesheets[plugin_directory_name]).to eq(nil)
|
2014-12-09 13:20:53 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers mobile css properly" do
|
2019-08-22 00:32:18 -05:00
|
|
|
registry.register_asset("test.css", :mobile, plugin_directory_name)
|
|
|
|
expect(registry.mobile_stylesheets[plugin_directory_name].count).to eq(1)
|
2019-08-22 01:22:52 -05:00
|
|
|
expect(registry.stylesheets[plugin_directory_name]).to eq(nil)
|
2014-12-09 13:20:53 -06:00
|
|
|
end
|
|
|
|
|
2020-08-06 08:46:17 -05:00
|
|
|
it "registers color definitions properly" do
|
|
|
|
registry.register_asset("test.css", :color_definitions, plugin_directory_name)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(registry.color_definition_stylesheets[plugin_directory_name]).to eq("test.css")
|
2020-08-06 08:46:17 -05:00
|
|
|
expect(registry.stylesheets[plugin_directory_name]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2018-04-10 01:37:16 -05:00
|
|
|
it "registers vendored_core_pretty_text properly" do
|
|
|
|
registry.register_asset("my_lib.js", :vendored_core_pretty_text)
|
|
|
|
|
|
|
|
expect(registry.vendored_core_pretty_text.count).to eq(1)
|
|
|
|
expect(registry.javascripts.count).to eq(0)
|
|
|
|
end
|
2014-12-09 13:20:53 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#register_seed_data" do
|
2015-06-04 14:56:17 -05:00
|
|
|
let(:registry) { DiscoursePluginRegistry }
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
after { registry.reset! }
|
2015-06-04 14:56:17 -05:00
|
|
|
|
|
|
|
it "registers seed data properly" do
|
|
|
|
registry.register_seed_data("admin_quick_start_title", "Banana Hosting: Quick Start Guide")
|
2023-01-09 05:18:21 -06:00
|
|
|
registry.register_seed_data(
|
|
|
|
"admin_quick_start_filename",
|
|
|
|
File.expand_path("../docs/BANANA-QUICK-START.md", __FILE__),
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(registry.seed_data["admin_quick_start_title"]).to eq(
|
|
|
|
"Banana Hosting: Quick Start Guide",
|
|
|
|
)
|
|
|
|
expect(registry.seed_data["admin_quick_start_filename"]).to eq(
|
|
|
|
File.expand_path("../docs/BANANA-QUICK-START.md", __FILE__),
|
|
|
|
)
|
2015-06-04 14:56:17 -05:00
|
|
|
end
|
|
|
|
end
|
2023-03-29 22:39:55 -05:00
|
|
|
|
|
|
|
context "with filters" do
|
|
|
|
after { DiscoursePluginRegistry.clear_modifiers! }
|
|
|
|
|
|
|
|
class TestFilterPlugInstance < Plugin::Instance
|
|
|
|
def enabled?
|
|
|
|
!@disabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled=(value)
|
|
|
|
@disabled = !value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:plugin_instance) { TestFilterPlugInstance.new }
|
|
|
|
let(:plugin_instance2) { TestFilterPlugInstance.new }
|
|
|
|
|
|
|
|
it "handles modifiers with multiple parameters" do
|
|
|
|
DiscoursePluginRegistry.register_modifier(plugin_instance, :magic_sum_modifier) do |a, b|
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
|
|
|
|
sum = DiscoursePluginRegistry.apply_modifier(:magic_sum_modifier, 1, 2)
|
|
|
|
expect(sum).to eq(3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles modifier stacking" do
|
|
|
|
# first in, first called
|
|
|
|
DiscoursePluginRegistry.register_modifier(plugin_instance, :stacking) { |x| x == 1 ? 2 : 1 }
|
|
|
|
DiscoursePluginRegistry.register_modifier(plugin_instance2, :stacking) { |x| x + 1 }
|
|
|
|
|
|
|
|
expect(DiscoursePluginRegistry.apply_modifier(:stacking, 1)).to eq(3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles disabled plugins" do
|
|
|
|
plugin_instance.enabled = false
|
|
|
|
DiscoursePluginRegistry.register_modifier(plugin_instance, :magic_sum_modifier) do |a, b|
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
|
|
|
|
sum = DiscoursePluginRegistry.apply_modifier(:magic_sum_modifier, 1, 2)
|
|
|
|
expect(sum).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can handle arity mismatch" do
|
|
|
|
DiscoursePluginRegistry.register_modifier(plugin_instance, :magic_sum_modifier) { 42 }
|
|
|
|
|
|
|
|
sum = DiscoursePluginRegistry.apply_modifier(:magic_sum_modifier, 1, 2)
|
|
|
|
expect(sum).to eq(42)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|