2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
describe Plugin::Instance do
|
|
|
|
|
2014-04-02 01:22:12 -04:00
|
|
|
after do
|
2014-12-09 14:20:53 -05:00
|
|
|
DiscoursePluginRegistry.reset!
|
2014-04-02 01:22:12 -04:00
|
|
|
end
|
|
|
|
|
2013-08-25 21:04:16 -04:00
|
|
|
context "find_all" do
|
|
|
|
it "can find plugins correctly" do
|
|
|
|
plugins = Plugin::Instance.find_all("#{Rails.root}/spec/fixtures/plugins")
|
2021-03-12 11:17:42 -05:00
|
|
|
expect(plugins.count).to eq(5)
|
2020-08-06 09:46:17 -04:00
|
|
|
plugin = plugins[3]
|
2013-08-25 21:04:16 -04:00
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(plugin.name).to eq("plugin-name")
|
|
|
|
expect(plugin.path).to eq("#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb")
|
2013-08-25 21:04:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not blow up on missing directory" do
|
|
|
|
plugins = Plugin::Instance.find_all("#{Rails.root}/frank_zappa")
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(plugins.count).to eq(0)
|
2013-08-25 21:04:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
context "enabling/disabling" do
|
|
|
|
|
|
|
|
it "is enabled by default" do
|
|
|
|
expect(Plugin::Instance.new.enabled?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a plugin that extends things" do
|
|
|
|
|
2019-12-04 12:26:23 -05:00
|
|
|
class Trout
|
|
|
|
attr_accessor :data
|
|
|
|
end
|
|
|
|
|
2019-08-27 04:21:53 -04:00
|
|
|
class TroutSerializer < ApplicationSerializer
|
|
|
|
attribute :name
|
|
|
|
|
|
|
|
def name
|
|
|
|
"a trout"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class TroutJuniorSerializer < TroutSerializer
|
|
|
|
|
|
|
|
attribute :i_am_child
|
|
|
|
|
|
|
|
def name
|
|
|
|
"a trout jr"
|
|
|
|
end
|
|
|
|
|
|
|
|
def i_am_child
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2015-02-04 16:23:39 -05:00
|
|
|
|
|
|
|
class TroutPlugin < Plugin::Instance
|
|
|
|
attr_accessor :enabled
|
2018-12-03 22:48:13 -05:00
|
|
|
def enabled?
|
|
|
|
@enabled
|
|
|
|
end
|
2015-02-04 16:23:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
@plugin = TroutPlugin.new
|
|
|
|
@trout = Trout.new
|
|
|
|
|
2019-08-27 04:21:53 -04:00
|
|
|
poison = TroutSerializer.new(@trout)
|
|
|
|
poison.attributes
|
|
|
|
|
|
|
|
poison = TroutJuniorSerializer.new(@trout)
|
|
|
|
poison.attributes
|
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
# New method
|
|
|
|
@plugin.add_to_class(:trout, :status?) { "evil" }
|
|
|
|
|
|
|
|
# DiscourseEvent
|
|
|
|
@hello_count = 0
|
2016-09-05 03:48:59 -04:00
|
|
|
@increase_count = -> { @hello_count += 1 }
|
2016-09-05 04:10:03 -04:00
|
|
|
@set = @plugin.on(:hello, &@increase_count)
|
2015-02-04 16:23:39 -05:00
|
|
|
|
|
|
|
# Serializer
|
|
|
|
@plugin.add_to_serializer(:trout, :scales) { 1024 }
|
2019-08-27 04:21:53 -04:00
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
@serializer = TroutSerializer.new(@trout)
|
2019-08-27 04:21:53 -04:00
|
|
|
@child_serializer = TroutJuniorSerializer.new(@trout)
|
2015-02-04 16:23:39 -05:00
|
|
|
end
|
|
|
|
|
2015-02-04 17:33:18 -05:00
|
|
|
after do
|
2016-09-05 05:03:26 -04:00
|
|
|
DiscourseEvent.off(:hello, &@set.first)
|
2015-02-04 17:33:18 -05:00
|
|
|
end
|
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
it "checks enabled/disabled functionality for extensions" do
|
|
|
|
# with an enabled plugin
|
|
|
|
@plugin.enabled = true
|
|
|
|
expect(@trout.status?).to eq("evil")
|
|
|
|
DiscourseEvent.trigger(:hello)
|
|
|
|
expect(@hello_count).to eq(1)
|
|
|
|
expect(@serializer.scales).to eq(1024)
|
|
|
|
expect(@serializer.include_scales?).to eq(true)
|
|
|
|
|
2019-08-27 04:21:53 -04:00
|
|
|
expect(@child_serializer.attributes[:scales]).to eq(1024)
|
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
# When a plugin is disabled
|
|
|
|
@plugin.enabled = false
|
|
|
|
expect(@trout.status?).to eq(nil)
|
|
|
|
DiscourseEvent.trigger(:hello)
|
|
|
|
expect(@hello_count).to eq(1)
|
|
|
|
expect(@serializer.scales).to eq(1024)
|
|
|
|
expect(@serializer.include_scales?).to eq(false)
|
2019-08-27 04:21:53 -04:00
|
|
|
expect(@serializer.name).to eq("a trout")
|
2015-02-04 16:23:39 -05:00
|
|
|
|
2019-08-27 04:21:53 -04:00
|
|
|
expect(@child_serializer.scales).to eq(1024)
|
|
|
|
expect(@child_serializer.include_scales?).to eq(false)
|
|
|
|
expect(@child_serializer.name).to eq("a trout jr")
|
2015-02-04 16:23:39 -05:00
|
|
|
end
|
2019-12-04 12:26:23 -05:00
|
|
|
|
|
|
|
it "only returns HTML if enabled" do
|
|
|
|
ctx = Trout.new
|
|
|
|
ctx.data = "hello"
|
|
|
|
|
|
|
|
@plugin.register_html_builder('test:html') { |c| "<div>#{c.data}</div>" }
|
|
|
|
@plugin.enabled = false
|
|
|
|
expect(DiscoursePluginRegistry.build_html('test:html', ctx)).to eq("")
|
|
|
|
@plugin.enabled = true
|
|
|
|
expect(DiscoursePluginRegistry.build_html('test:html', ctx)).to eq("<div>hello</div>")
|
|
|
|
end
|
2015-02-04 16:23:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-07 10:33:35 -04:00
|
|
|
context "register asset" do
|
2014-12-09 14:20:53 -05:00
|
|
|
it "populates the DiscoursePluginRegistry" do
|
2014-04-07 10:33:35 -04:00
|
|
|
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
|
|
|
|
plugin.register_asset("test.css")
|
|
|
|
plugin.register_asset("test2.css")
|
|
|
|
|
2014-04-10 02:30:22 -04:00
|
|
|
plugin.send :register_assets!
|
|
|
|
|
2019-08-20 12:39:52 -04:00
|
|
|
expect(DiscoursePluginRegistry.mobile_stylesheets[plugin.directory_name]).to be_nil
|
|
|
|
expect(DiscoursePluginRegistry.stylesheets[plugin.directory_name].count).to eq(2)
|
2014-04-07 10:33:35 -04:00
|
|
|
end
|
2018-04-10 02:37:16 -04:00
|
|
|
|
|
|
|
it "remaps vendored_core_pretty_text asset" do
|
|
|
|
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
|
|
|
|
plugin.register_asset("moment.js", :vendored_core_pretty_text)
|
|
|
|
|
|
|
|
plugin.send :register_assets!
|
|
|
|
|
2019-02-12 13:57:52 -05:00
|
|
|
expect(DiscoursePluginRegistry.vendored_core_pretty_text.first).to eq("vendor/assets/javascripts/moment.js")
|
2018-04-10 02:37:16 -04:00
|
|
|
end
|
2014-04-07 10:33:35 -04:00
|
|
|
end
|
|
|
|
|
2017-11-22 20:02:01 -05:00
|
|
|
context "register service worker" do
|
|
|
|
it "populates the DiscoursePluginRegistry" do
|
|
|
|
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
|
|
|
|
plugin.register_service_worker("test.js")
|
|
|
|
plugin.register_service_worker("test2.js")
|
|
|
|
|
|
|
|
plugin.send :register_service_workers!
|
|
|
|
|
|
|
|
expect(DiscoursePluginRegistry.service_workers.count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-19 09:00:11 -04:00
|
|
|
context "#add_report" do
|
|
|
|
it "adds a report" do
|
|
|
|
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
|
|
|
|
plugin.add_report("readers") {}
|
|
|
|
|
|
|
|
expect(Report.respond_to?(:report_readers)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
it 'patches the enabled? function for auth_providers if not defined' do
|
2019-10-08 07:10:43 -04:00
|
|
|
SimpleAuthenticator = Class.new(Auth::Authenticator) do
|
|
|
|
def name
|
|
|
|
"my_authenticator"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
plugin = Plugin::Instance.new
|
|
|
|
|
2019-05-06 21:00:09 -04:00
|
|
|
# lets piggy back on another boolean setting, so we don't dirty our SiteSetting object
|
|
|
|
SiteSetting.enable_badges = false
|
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
# No enabled_site_setting
|
2019-10-08 07:10:43 -04:00
|
|
|
authenticator = SimpleAuthenticator.new
|
2018-07-23 11:51:57 -04:00
|
|
|
plugin.auth_provider(authenticator: authenticator)
|
2018-11-30 11:58:18 -05:00
|
|
|
plugin.notify_before_auth
|
2018-07-23 11:51:57 -04:00
|
|
|
expect(authenticator.enabled?).to eq(true)
|
|
|
|
|
|
|
|
# With enabled site setting
|
2018-11-30 11:58:18 -05:00
|
|
|
plugin = Plugin::Instance.new
|
2019-10-08 07:10:43 -04:00
|
|
|
authenticator = SimpleAuthenticator.new
|
2019-05-06 21:00:09 -04:00
|
|
|
plugin.auth_provider(enabled_setting: 'enable_badges', authenticator: authenticator)
|
2018-11-30 11:58:18 -05:00
|
|
|
plugin.notify_before_auth
|
2018-07-23 11:51:57 -04:00
|
|
|
expect(authenticator.enabled?).to eq(false)
|
|
|
|
|
|
|
|
# Defines own method
|
2018-11-30 11:58:18 -05:00
|
|
|
plugin = Plugin::Instance.new
|
2019-05-06 21:00:09 -04:00
|
|
|
|
|
|
|
SiteSetting.enable_badges = true
|
2019-10-08 07:10:43 -04:00
|
|
|
authenticator = Class.new(SimpleAuthenticator) do
|
2018-07-23 11:51:57 -04:00
|
|
|
def enabled?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end.new
|
2019-05-06 21:00:09 -04:00
|
|
|
plugin.auth_provider(enabled_setting: 'enable_badges', authenticator: authenticator)
|
2018-11-30 11:58:18 -05:00
|
|
|
plugin.notify_before_auth
|
2018-07-23 11:51:57 -04:00
|
|
|
expect(authenticator.enabled?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
context "activate!" do
|
2018-07-23 11:51:57 -04:00
|
|
|
before do
|
2019-05-06 21:00:09 -04:00
|
|
|
# lets piggy back on another boolean setting, so we don't dirty our SiteSetting object
|
|
|
|
SiteSetting.enable_badges = false
|
2018-07-23 11:51:57 -04:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
it "can activate plugins correctly" do
|
2013-08-25 21:04:16 -04:00
|
|
|
plugin = Plugin::Instance.new
|
2013-08-23 02:21:52 -04:00
|
|
|
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
|
|
|
|
junk_file = "#{plugin.auto_generated_path}/junk"
|
|
|
|
|
|
|
|
plugin.ensure_directory(junk_file)
|
|
|
|
File.open("#{plugin.auto_generated_path}/junk", "w") { |f| f.write("junk") }
|
|
|
|
plugin.activate!
|
|
|
|
|
|
|
|
# calls ensure_assets! make sure they are there
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(plugin.assets.count).to eq(1)
|
2014-04-10 02:30:22 -04:00
|
|
|
plugin.assets.each do |a, opts|
|
2022-01-05 12:45:08 -05:00
|
|
|
expect(File.exist?(a)).to eq(true)
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# ensure it cleans up all crap in autogenerated directory
|
2022-01-05 12:45:08 -05:00
|
|
|
expect(File.exist?(junk_file)).to eq(false)
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
2014-04-07 10:33:35 -04:00
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
it "registers auth providers correctly" do
|
|
|
|
plugin = Plugin::Instance.new
|
|
|
|
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
|
|
|
|
plugin.activate!
|
2018-11-30 11:58:18 -05:00
|
|
|
expect(DiscoursePluginRegistry.auth_providers.count).to eq(0)
|
|
|
|
plugin.notify_before_auth
|
2018-07-23 11:51:57 -04:00
|
|
|
expect(DiscoursePluginRegistry.auth_providers.count).to eq(1)
|
2018-11-30 11:58:18 -05:00
|
|
|
auth_provider = DiscoursePluginRegistry.auth_providers.to_a[0]
|
2020-02-07 12:32:35 -05:00
|
|
|
expect(auth_provider.authenticator.name).to eq('facebook')
|
2018-07-23 11:51:57 -04:00
|
|
|
end
|
|
|
|
|
2014-04-07 10:33:35 -04:00
|
|
|
it "finds all the custom assets" do
|
|
|
|
plugin = Plugin::Instance.new
|
|
|
|
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
|
2014-04-10 02:30:22 -04:00
|
|
|
|
2014-04-07 10:33:35 -04:00
|
|
|
plugin.register_asset("test.css")
|
|
|
|
plugin.register_asset("test2.scss")
|
2014-04-10 02:30:22 -04:00
|
|
|
plugin.register_asset("mobile.css", :mobile)
|
|
|
|
plugin.register_asset("desktop.css", :desktop)
|
|
|
|
plugin.register_asset("desktop2.css", :desktop)
|
|
|
|
|
2014-04-07 10:33:35 -04:00
|
|
|
plugin.register_asset("code.js")
|
2014-04-10 02:30:22 -04:00
|
|
|
|
2014-04-07 10:33:35 -04:00
|
|
|
plugin.register_asset("my_admin.js", :admin)
|
|
|
|
plugin.register_asset("my_admin2.js", :admin)
|
|
|
|
|
|
|
|
plugin.activate!
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
expect(DiscoursePluginRegistry.javascripts.count).to eq(2)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(DiscoursePluginRegistry.admin_javascripts.count).to eq(2)
|
2019-08-20 12:39:52 -04:00
|
|
|
expect(DiscoursePluginRegistry.desktop_stylesheets[plugin.directory_name].count).to eq(2)
|
|
|
|
expect(DiscoursePluginRegistry.stylesheets[plugin.directory_name].count).to eq(2)
|
|
|
|
expect(DiscoursePluginRegistry.mobile_stylesheets[plugin.directory_name].count).to eq(1)
|
2014-04-07 10:33:35 -04:00
|
|
|
end
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
2014-06-10 21:57:22 -04:00
|
|
|
context "serialized_current_user_fields" do
|
2017-11-14 19:55:37 -05:00
|
|
|
before do
|
|
|
|
DiscoursePluginRegistry.serialized_current_user_fields << "has_car"
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
DiscoursePluginRegistry.serialized_current_user_fields.delete "has_car"
|
|
|
|
end
|
|
|
|
|
2014-06-10 21:57:22 -04:00
|
|
|
it "correctly serializes custom user fields" do
|
|
|
|
DiscoursePluginRegistry.serialized_current_user_fields << "has_car"
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.custom_fields["has_car"] = "true"
|
|
|
|
user.save!
|
|
|
|
|
|
|
|
payload = JSON.parse(CurrentUserSerializer.new(user, scope: Guardian.new(user)).to_json)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(payload["current_user"]["custom_fields"]["has_car"]).to eq("true")
|
2017-11-14 19:55:37 -05:00
|
|
|
|
|
|
|
payload = JSON.parse(UserSerializer.new(user, scope: Guardian.new(user)).to_json)
|
|
|
|
expect(payload["user"]["custom_fields"]["has_car"]).to eq("true")
|
|
|
|
|
|
|
|
UserCustomField.destroy_all
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
payload = JSON.parse(CurrentUserSerializer.new(user, scope: Guardian.new(user)).to_json)
|
|
|
|
expect(payload["current_user"]["custom_fields"]).to eq({})
|
|
|
|
|
|
|
|
payload = JSON.parse(UserSerializer.new(user, scope: Guardian.new(user)).to_json)
|
|
|
|
expect(payload["user"]["custom_fields"]).to eq({})
|
2014-06-10 21:57:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-03 12:36:34 -04:00
|
|
|
context "register_color_scheme" do
|
|
|
|
it "can add a color scheme for the first time" do
|
|
|
|
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
|
|
|
|
expect {
|
|
|
|
plugin.register_color_scheme("Purple", primary: 'EEE0E5')
|
|
|
|
plugin.notify_after_initialize
|
|
|
|
}.to change { ColorScheme.count }.by(1)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(ColorScheme.where(name: "Purple")).to be_present
|
2014-06-03 12:36:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add the same color scheme twice" do
|
|
|
|
Fabricate(:color_scheme, name: "Halloween")
|
|
|
|
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
|
|
|
|
expect {
|
|
|
|
plugin.register_color_scheme("Halloween", primary: 'EEE0E5')
|
|
|
|
plugin.notify_after_initialize
|
|
|
|
}.to_not change { ColorScheme.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-25 02:55:53 -04:00
|
|
|
describe '.register_seedfu_fixtures' do
|
|
|
|
it "should add the new path to SeedFu's fixtures path" do
|
|
|
|
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
|
|
|
|
plugin.register_seedfu_fixtures(['some_path'])
|
|
|
|
plugin.register_seedfu_fixtures('some_path2')
|
|
|
|
|
|
|
|
expect(SeedFu.fixture_paths).to include('some_path')
|
|
|
|
expect(SeedFu.fixture_paths).to include('some_path2')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-20 21:20:31 -05:00
|
|
|
describe '#add_model_callback' do
|
|
|
|
let(:metadata) do
|
|
|
|
metadata = Plugin::Metadata.new
|
|
|
|
metadata.name = 'test'
|
|
|
|
metadata
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:plugin_instance) do
|
|
|
|
plugin = Plugin::Instance.new(nil, "/tmp/test.rb")
|
|
|
|
plugin.metadata = metadata
|
|
|
|
plugin
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should add the right callback' do
|
|
|
|
called = 0
|
|
|
|
|
2017-01-12 15:43:09 -05:00
|
|
|
plugin_instance.add_model_callback(User, :after_create) do
|
2016-11-20 21:20:31 -05:00
|
|
|
called += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
expect(called).to eq(1)
|
|
|
|
|
2019-04-29 03:32:25 -04:00
|
|
|
user.update!(username: 'some_username')
|
2016-11-20 21:20:31 -05:00
|
|
|
|
|
|
|
expect(called).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should add the right callback with options' do
|
|
|
|
called = 0
|
|
|
|
|
2017-01-12 15:43:09 -05:00
|
|
|
plugin_instance.add_model_callback(User, :after_commit, on: :create) do
|
2016-11-20 21:20:31 -05:00
|
|
|
called += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
expect(called).to eq(1)
|
|
|
|
|
2019-04-29 03:32:25 -04:00
|
|
|
user.update!(username: 'some_username')
|
2016-11-20 21:20:31 -05:00
|
|
|
|
|
|
|
expect(called).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2018-01-25 06:09:18 -05:00
|
|
|
|
|
|
|
context "locales" do
|
|
|
|
let(:plugin_path) { "#{Rails.root}/spec/fixtures/plugins/custom_locales" }
|
|
|
|
let!(:plugin) { Plugin::Instance.new(nil, "#{plugin_path}/plugin.rb") }
|
|
|
|
let(:plural) do
|
|
|
|
{
|
|
|
|
keys: [:one, :few, :other],
|
|
|
|
rule: lambda do |n|
|
|
|
|
return :one if n == 1
|
|
|
|
return :few if n < 10
|
|
|
|
:other
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_locale(locale, opts)
|
|
|
|
plugin.register_locale(locale, opts)
|
|
|
|
plugin.activate!
|
|
|
|
|
|
|
|
DiscoursePluginRegistry.locales[locale]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "enables the registered locales only on activate" do
|
2019-02-19 09:27:30 -05:00
|
|
|
plugin.register_locale("foo_BAR", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
2020-11-10 20:34:26 -05:00
|
|
|
plugin.register_locale("tup", name: "Tupi", nativeName: "Tupi", fallbackLocale: "pt_BR")
|
2018-01-25 06:09:18 -05:00
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(0)
|
|
|
|
|
|
|
|
plugin.activate!
|
2019-02-25 14:40:02 -05:00
|
|
|
|
2018-01-25 06:09:18 -05:00
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows finding the locale by string and symbol" do
|
2019-02-19 09:27:30 -05:00
|
|
|
register_locale("foo_BAR", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
2018-01-25 06:09:18 -05:00
|
|
|
|
2019-02-19 09:27:30 -05:00
|
|
|
expect(DiscoursePluginRegistry.locales).to have_key(:foo_BAR)
|
|
|
|
expect(DiscoursePluginRegistry.locales).to have_key('foo_BAR')
|
2018-01-25 06:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly registers a new locale" do
|
2019-02-19 09:27:30 -05:00
|
|
|
locale = register_locale("foo_BAR", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
2018-01-25 06:09:18 -05:00
|
|
|
|
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(1)
|
2019-02-19 09:27:30 -05:00
|
|
|
expect(DiscoursePluginRegistry.locales).to have_key(:foo_BAR)
|
2018-01-25 06:09:18 -05:00
|
|
|
|
|
|
|
expect(locale[:fallbackLocale]).to be_nil
|
2019-02-19 09:27:30 -05:00
|
|
|
expect(locale[:message_format]).to eq(["foo_BAR", "#{plugin_path}/lib/javascripts/locale/message_format/foo_BAR.js"])
|
|
|
|
expect(locale[:moment_js]).to eq(["foo_BAR", "#{plugin_path}/lib/javascripts/locale/moment_js/foo_BAR.js"])
|
2019-02-25 14:40:02 -05:00
|
|
|
expect(locale[:moment_js_timezones]).to eq(["foo_BAR", "#{plugin_path}/lib/javascripts/locale/moment_js_timezones/foo_BAR.js"])
|
2018-01-25 06:09:18 -05:00
|
|
|
expect(locale[:plural]).to eq(plural.with_indifferent_access)
|
|
|
|
|
2019-02-19 09:27:30 -05:00
|
|
|
expect(Rails.configuration.assets.precompile).to include("locales/foo_BAR.js")
|
|
|
|
|
|
|
|
expect(JsLocaleHelper.find_message_format_locale(["foo_BAR"], fallback_to_english: true)).to eq(locale[:message_format])
|
|
|
|
expect(JsLocaleHelper.find_moment_locale(["foo_BAR"])).to eq (locale[:moment_js])
|
2019-02-25 14:40:02 -05:00
|
|
|
expect(JsLocaleHelper.find_moment_locale(["foo_BAR"], timezone_names: true)).to eq (locale[:moment_js_timezones])
|
2018-01-25 06:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly registers a new locale using a fallback locale" do
|
2020-11-10 20:34:26 -05:00
|
|
|
locale = register_locale("tup", name: "Tupi", nativeName: "Tupi", fallbackLocale: "pt_BR")
|
2018-01-25 06:09:18 -05:00
|
|
|
|
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(1)
|
2020-11-10 20:34:26 -05:00
|
|
|
expect(DiscoursePluginRegistry.locales).to have_key(:tup)
|
2018-01-25 06:09:18 -05:00
|
|
|
|
2020-11-10 20:34:26 -05:00
|
|
|
expect(locale[:fallbackLocale]).to eq("pt_BR")
|
|
|
|
expect(locale[:message_format]).to eq(["pt_BR", "#{Rails.root}/lib/javascripts/locale/pt_BR.js"])
|
|
|
|
expect(locale[:moment_js]).to eq(["pt-br", "#{Rails.root}/vendor/assets/javascripts/moment-locale/pt-br.js"])
|
|
|
|
expect(locale[:moment_js_timezones]).to eq(["pt", "#{Rails.root}/vendor/assets/javascripts/moment-timezone-names-locale/pt.js"])
|
2018-01-25 06:09:18 -05:00
|
|
|
expect(locale[:plural]).to be_nil
|
|
|
|
|
2020-11-10 20:34:26 -05:00
|
|
|
expect(Rails.configuration.assets.precompile).to include("locales/tup.js")
|
2019-02-19 09:27:30 -05:00
|
|
|
|
2020-11-10 20:34:26 -05:00
|
|
|
expect(JsLocaleHelper.find_message_format_locale(["tup"], fallback_to_english: true)).to eq(locale[:message_format])
|
|
|
|
expect(JsLocaleHelper.find_moment_locale(["tup"])).to eq (locale[:moment_js])
|
2018-01-25 06:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly registers a new locale when some files exist in core" do
|
|
|
|
locale = register_locale("tlh", name: "Klingon", nativeName: "tlhIngan Hol", plural: plural)
|
|
|
|
|
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(1)
|
|
|
|
expect(DiscoursePluginRegistry.locales).to have_key(:tlh)
|
|
|
|
|
|
|
|
expect(locale[:fallbackLocale]).to be_nil
|
|
|
|
expect(locale[:message_format]).to eq(["tlh", "#{plugin_path}/lib/javascripts/locale/message_format/tlh.js"])
|
2019-02-12 13:57:52 -05:00
|
|
|
expect(locale[:moment_js]).to eq(["tlh", "#{Rails.root}/vendor/assets/javascripts/moment-locale/tlh.js"])
|
2018-01-25 06:09:18 -05:00
|
|
|
expect(locale[:plural]).to eq(plural.with_indifferent_access)
|
|
|
|
|
|
|
|
expect(Rails.configuration.assets.precompile).to include("locales/tlh.js")
|
2019-02-19 09:27:30 -05:00
|
|
|
|
|
|
|
expect(JsLocaleHelper.find_message_format_locale(["tlh"], fallback_to_english: true)).to eq(locale[:message_format])
|
|
|
|
expect(JsLocaleHelper.find_moment_locale(["tlh"])).to eq (locale[:moment_js])
|
2018-01-25 06:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not register a new locale when the fallback locale does not exist" do
|
|
|
|
register_locale("bar", name: "Bar", nativeName: "Bar", fallbackLocale: "foo")
|
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
[
|
2019-02-19 09:27:30 -05:00
|
|
|
"config/locales/client.foo_BAR.yml",
|
|
|
|
"config/locales/server.foo_BAR.yml",
|
|
|
|
"lib/javascripts/locale/message_format/foo_BAR.js",
|
|
|
|
"lib/javascripts/locale/moment_js/foo_BAR.js",
|
|
|
|
"assets/locales/foo_BAR.js.erb"
|
2018-01-25 06:09:18 -05:00
|
|
|
].each do |path|
|
|
|
|
it "does not register a new locale when #{path} is missing" do
|
|
|
|
path = "#{plugin_path}/#{path}"
|
|
|
|
File.stubs('exist?').returns(false)
|
|
|
|
File.stubs('exist?').with(regexp_matches(/#{Regexp.quote(plugin_path)}.*/)).returns(true)
|
|
|
|
File.stubs('exist?').with(path).returns(false)
|
|
|
|
|
2019-02-19 09:27:30 -05:00
|
|
|
register_locale("foo_BAR", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
2018-01-25 06:09:18 -05:00
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
describe '#register_reviewable_types' do
|
|
|
|
it 'Overrides the existing Reviewable types adding new ones' do
|
|
|
|
current_types = Reviewable.types
|
2019-04-08 13:42:36 -04:00
|
|
|
new_type_class = Class
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-04-08 13:42:36 -04:00
|
|
|
Plugin::Instance.new.register_reviewable_type new_type_class
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-04-08 13:42:36 -04:00
|
|
|
expect(Reviewable.types).to match_array(current_types << new_type_class.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#extend_list_method' do
|
|
|
|
it 'Overrides the existing list appending new elements' do
|
|
|
|
current_list = Reviewable.types
|
|
|
|
new_element = Class.name
|
|
|
|
|
|
|
|
Plugin::Instance.new.extend_list_method Reviewable, :types, [new_element]
|
|
|
|
|
|
|
|
expect(Reviewable.types).to match_array(current_list << new_element)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
2020-03-30 14:16:10 -04:00
|
|
|
|
|
|
|
describe '#register_emoji' do
|
|
|
|
before do
|
|
|
|
Plugin::CustomEmoji.clear_cache
|
|
|
|
end
|
|
|
|
|
2020-04-06 13:41:59 -04:00
|
|
|
after do
|
|
|
|
Plugin::CustomEmoji.clear_cache
|
|
|
|
end
|
|
|
|
|
2020-03-30 14:16:10 -04:00
|
|
|
it 'allows to register an emoji' do
|
|
|
|
Plugin::Instance.new.register_emoji("foo", "/foo/bar.png")
|
|
|
|
|
|
|
|
custom_emoji = Emoji.custom.first
|
|
|
|
|
|
|
|
expect(custom_emoji.name).to eq("foo")
|
|
|
|
expect(custom_emoji.url).to eq("/foo/bar.png")
|
|
|
|
expect(custom_emoji.group).to eq(Emoji::DEFAULT_GROUP)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows to register an emoji with a group' do
|
|
|
|
Plugin::Instance.new.register_emoji("bar", "/baz/bar.png", "baz")
|
|
|
|
|
|
|
|
custom_emoji = Emoji.custom.first
|
|
|
|
|
|
|
|
expect(custom_emoji.name).to eq("bar")
|
|
|
|
expect(custom_emoji.url).to eq("/baz/bar.png")
|
|
|
|
expect(custom_emoji.group).to eq("baz")
|
|
|
|
end
|
|
|
|
end
|
2020-07-02 10:47:43 -04:00
|
|
|
|
|
|
|
describe '#replace_flags' do
|
2020-07-06 12:09:56 -04:00
|
|
|
after do
|
|
|
|
PostActionType.replace_flag_settings(nil)
|
|
|
|
ReviewableScore.reload_types
|
|
|
|
end
|
|
|
|
|
2020-07-02 10:47:43 -04:00
|
|
|
let(:original_flags) { PostActionType.flag_settings }
|
|
|
|
|
|
|
|
it 'adds a new flag' do
|
|
|
|
highest_flag_id = ReviewableScore.types.values.max
|
|
|
|
flag_name = :new_flag
|
|
|
|
|
|
|
|
subject.replace_flags(settings: original_flags) do |settings, next_flag_id|
|
|
|
|
settings.add(
|
|
|
|
next_flag_id,
|
|
|
|
flag_name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(PostActionType.flag_settings.flag_types.keys).to include(flag_name)
|
|
|
|
expect(PostActionType.flag_settings.flag_types.values.max).to eq(highest_flag_id + 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a new score type after adding a new flag' do
|
|
|
|
highest_flag_id = ReviewableScore.types.values.max
|
|
|
|
new_score_type = :new_score_type
|
|
|
|
|
|
|
|
subject.replace_flags(settings: original_flags, score_type_names: [new_score_type]) do |settings, next_flag_id|
|
|
|
|
settings.add(
|
|
|
|
next_flag_id,
|
|
|
|
:new_flag
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(PostActionType.flag_settings.flag_types.values.max).to eq(highest_flag_id + 1)
|
|
|
|
expect(ReviewableScore.types.keys).to include(new_score_type)
|
|
|
|
expect(ReviewableScore.types.values.max).to eq(highest_flag_id + 2)
|
|
|
|
end
|
|
|
|
end
|
2021-02-17 12:42:44 -05:00
|
|
|
|
|
|
|
describe '#add_api_key_scope' do
|
2021-02-18 14:05:44 -05:00
|
|
|
after { DiscoursePluginRegistry.reset! }
|
|
|
|
|
2021-02-17 12:42:44 -05:00
|
|
|
it 'adds a custom api key scope' do
|
|
|
|
actions = %w[admin/groups#create]
|
|
|
|
subject.add_api_key_scope(:groups, create: { actions: actions })
|
|
|
|
|
|
|
|
expect(ApiKeyScope.scope_mappings.dig(:groups, :create, :actions)).to contain_exactly(*actions)
|
|
|
|
end
|
|
|
|
end
|
2021-06-22 14:00:04 -04:00
|
|
|
|
|
|
|
describe '#add_directory_column' do
|
|
|
|
let!(:plugin) { Plugin::Instance.new }
|
|
|
|
|
|
|
|
before do
|
|
|
|
DirectoryItem.clear_plugin_queries
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
DirectoryColumn.clear_plugin_directory_columns
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with valid column name" do
|
|
|
|
let(:column_name) { "random_c" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
DB.exec("ALTER TABLE directory_items ADD COLUMN IF NOT EXISTS #{column_name} integer")
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
DB.exec("ALTER TABLE directory_items DROP COLUMN IF EXISTS #{column_name}")
|
|
|
|
DiscourseEvent.all_off("before_directory_refresh")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a directory column record when directory items are refreshed' do
|
|
|
|
plugin.add_directory_column(column_name, query: "SELECT COUNT(*) FROM users", icon: 'recycle')
|
|
|
|
expect(DirectoryColumn.find_by(name: column_name, icon: 'recycle', enabled: false)).not_to be_present
|
|
|
|
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
expect(DirectoryColumn.find_by(name: column_name, icon: 'recycle', enabled: false)).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'errors when the column_name contains invalid characters' do
|
|
|
|
expect {
|
|
|
|
plugin.add_directory_column('Capital', query: "SELECT COUNT(*) FROM users", icon: 'recycle')
|
|
|
|
}.to raise_error(RuntimeError)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
plugin.add_directory_column('has space', query: "SELECT COUNT(*) FROM users", icon: 'recycle')
|
|
|
|
}.to raise_error(RuntimeError)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
plugin.add_directory_column('has_number_1', query: "SELECT COUNT(*) FROM users", icon: 'recycle')
|
|
|
|
}.to raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
end
|
2021-07-19 01:54:19 -04:00
|
|
|
|
|
|
|
describe '#register_site_categories_callback' do
|
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
|
|
|
|
it 'adds a callback to the Site#categories' do
|
|
|
|
instance = Plugin::Instance.new
|
|
|
|
|
|
|
|
instance.register_site_categories_callback do |categories|
|
|
|
|
categories.each do |category|
|
|
|
|
category[:test_field] = "test"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
site = Site.new(Guardian.new)
|
|
|
|
|
|
|
|
expect(site.categories.first[:test_field]).to eq("test")
|
|
|
|
ensure
|
|
|
|
Site.clear_cache
|
|
|
|
Site.categories_callbacks.clear
|
|
|
|
end
|
|
|
|
end
|
REFACTOR: Improve support for consolidating notifications. (#14904)
* REFACTOR: Improve support for consolidating notifications.
Before this commit, we didn't have a single way of consolidating notifications. For notifications like group summaries, we manually removed old ones before creating a new one. On the other hand, we used an after_create callback for likes and group membership requests, which caused unnecessary work, as we need to delete the record we created to replace it with a consolidated one.
We now have all the consolidation rules centralized in a single place: the consolidation planner class. Other parts of the app looking to create a consolidable notification can do so by calling Notification#consolidate_or_save!, instead of the default Notification#create! method.
Finally, we added two more rules: one for re-using existing group summaries and another for deleting duplicated dashboard problems PMs notifications when the user is tracking the moderator's inbox. Setting the threshold to one forces the planner to apply this rule every time.
I plan to add plugin support for adding custom rules in another PR to keep this one relatively small.
* DEV: Introduces a plugin API for consolidating notifications.
This commit removes the `Notification#filter_by_consolidation_data` scope since plugins could have to define their criteria. The Plan class now receives two blocks, one to query for an already consolidated notification, which we'll try to update, and another to query for existing ones to consolidate.
It also receives a consolidation window, which accepts an ActiveSupport::Duration object, and filter notifications created since that value.
2021-11-30 11:36:14 -05:00
|
|
|
|
|
|
|
describe '#register_notification_consolidation_plan' do
|
|
|
|
let(:plugin) { Plugin::Instance.new }
|
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
after do
|
|
|
|
DiscoursePluginRegistry.reset_register!(:notification_consolidation_plans)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when the received object is not a consolidation plan' do
|
|
|
|
expect { plugin.register_notification_consolidation_plan(Object.new) }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers a consolidation plan and uses it' do
|
|
|
|
plan = Notifications::ConsolidateNotifications.new(
|
|
|
|
from: Notification.types[:code_review_commit_approved],
|
|
|
|
to: Notification.types[:code_review_commit_approved],
|
|
|
|
threshold: 1,
|
|
|
|
consolidation_window: 1.minute,
|
|
|
|
unconsolidated_query_blk: ->(notifications, _data) do
|
|
|
|
notifications.where("(data::json ->> 'consolidated') IS NULL")
|
|
|
|
end,
|
|
|
|
consolidated_query_blk: ->(notifications, _data) do
|
|
|
|
notifications.where("(data::json ->> 'consolidated') IS NOT NULL")
|
|
|
|
end
|
|
|
|
).set_mutations(
|
|
|
|
set_data_blk: ->(notification) do
|
|
|
|
notification.data_hash.merge(consolidated: true)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
plugin.register_notification_consolidation_plan(plan)
|
|
|
|
|
|
|
|
create_notification!
|
|
|
|
create_notification!
|
|
|
|
|
|
|
|
expect(commit_approved_notifications.count).to eq(1)
|
|
|
|
consolidated_notification = commit_approved_notifications.last
|
|
|
|
expect(consolidated_notification.data_hash[:consolidated]).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_approved_notifications
|
|
|
|
Notification.where(user: topic.user, notification_type: Notification.types[:code_review_commit_approved])
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_notification!
|
|
|
|
Notification.consolidate_or_create!(
|
|
|
|
notification_type: Notification.types[:code_review_commit_approved],
|
|
|
|
topic_id: topic.id,
|
|
|
|
user: topic.user,
|
|
|
|
data: {}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|