2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-08-23 02:21:52 -04:00
|
|
|
require_dependency 'plugin/instance'
|
|
|
|
|
|
|
|
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")
|
2018-01-25 06:09:18 -05:00
|
|
|
expect(plugins.count).to eq(2)
|
|
|
|
plugin = plugins[1]
|
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
|
|
|
|
|
|
|
|
class Trout; end
|
|
|
|
class TroutSerializer < ApplicationSerializer; end
|
|
|
|
|
|
|
|
class TroutPlugin < Plugin::Instance
|
|
|
|
attr_accessor :enabled
|
|
|
|
def enabled?; @enabled; end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
@plugin = TroutPlugin.new
|
|
|
|
@trout = Trout.new
|
|
|
|
|
|
|
|
# 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 }
|
|
|
|
@serializer = TroutSerializer.new(@trout)
|
|
|
|
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)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
end
|
|
|
|
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!
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(DiscoursePluginRegistry.mobile_stylesheets.count).to eq(0)
|
|
|
|
expect(DiscoursePluginRegistry.stylesheets.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!
|
|
|
|
|
|
|
|
expect(DiscoursePluginRegistry.vendored_core_pretty_text.first).to eq("lib/javascripts/moment.js")
|
|
|
|
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
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
context "activate!" do
|
|
|
|
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)
|
2017-07-27 21:20:09 -04:00
|
|
|
File.open("#{plugin.auto_generated_path}/junk", "w") { |f| f.write("junk") }
|
2013-08-23 02:21:52 -04:00
|
|
|
plugin.activate!
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(plugin.auth_providers.count).to eq(1)
|
2013-08-23 02:21:52 -04:00
|
|
|
auth_provider = plugin.auth_providers[0]
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(auth_provider.authenticator.name).to eq('ubuntu')
|
2013-08-23 02:21:52 -04:00
|
|
|
|
|
|
|
# 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|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(File.exists?(a)).to eq(true)
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# ensure it cleans up all crap in autogenerated directory
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(File.exists?(junk_file)).to eq(false)
|
2013-08-23 02:21:52 -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-25 04:26:37 -04:00
|
|
|
plugin.register_asset("variables1.scss", :variables)
|
|
|
|
plugin.register_asset("variables2.scss", :variables)
|
|
|
|
|
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)
|
|
|
|
expect(DiscoursePluginRegistry.desktop_stylesheets.count).to eq(2)
|
|
|
|
expect(DiscoursePluginRegistry.sass_variables.count).to eq(2)
|
|
|
|
expect(DiscoursePluginRegistry.stylesheets.count).to eq(2)
|
|
|
|
expect(DiscoursePluginRegistry.mobile_stylesheets.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 {
|
2017-07-27 21:20:09 -04:00
|
|
|
plugin.register_color_scheme("Purple", primary: 'EEE0E5')
|
2014-06-03 12:36:34 -04:00
|
|
|
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 {
|
2017-07-27 21:20:09 -04:00
|
|
|
plugin.register_color_scheme("Halloween", primary: 'EEE0E5')
|
2014-06-03 12:36:34 -04:00
|
|
|
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)
|
|
|
|
|
|
|
|
user.update_attributes!(username: 'some_username')
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
user.update_attributes!(username: 'some_username')
|
|
|
|
|
|
|
|
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
|
|
|
|
plugin.register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
|
|
|
plugin.register_locale("es_MX", name: "Spanish (Mexico)", nativeName: "Español (México)", fallbackLocale: "es")
|
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(0)
|
|
|
|
|
|
|
|
plugin.activate!
|
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows finding the locale by string and symbol" do
|
|
|
|
register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
|
|
|
|
|
|
|
expect(DiscoursePluginRegistry.locales).to have_key(:foo)
|
|
|
|
expect(DiscoursePluginRegistry.locales).to have_key('foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly registers a new locale" do
|
|
|
|
locale = register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
|
|
|
|
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(1)
|
|
|
|
expect(DiscoursePluginRegistry.locales).to have_key(:foo)
|
|
|
|
|
|
|
|
expect(locale[:fallbackLocale]).to be_nil
|
|
|
|
expect(locale[:message_format]).to eq(["foo", "#{plugin_path}/lib/javascripts/locale/message_format/foo.js"])
|
|
|
|
expect(locale[:moment_js]).to eq(["foo", "#{plugin_path}/lib/javascripts/locale/moment_js/foo.js"])
|
|
|
|
expect(locale[:plural]).to eq(plural.with_indifferent_access)
|
|
|
|
|
|
|
|
expect(Rails.configuration.assets.precompile).to include("locales/foo.js")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly registers a new locale using a fallback locale" do
|
|
|
|
locale = register_locale("es_MX", name: "Spanish (Mexico)", nativeName: "Español (México)", fallbackLocale: "es")
|
|
|
|
|
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(1)
|
|
|
|
expect(DiscoursePluginRegistry.locales).to have_key(:es_MX)
|
|
|
|
|
|
|
|
expect(locale[:fallbackLocale]).to eq("es")
|
|
|
|
expect(locale[:message_format]).to eq(["es", "#{Rails.root}/lib/javascripts/locale/es.js"])
|
|
|
|
expect(locale[:moment_js]).to eq(["es", "#{Rails.root}/lib/javascripts/moment_locale/es.js"])
|
|
|
|
expect(locale[:plural]).to be_nil
|
|
|
|
|
|
|
|
expect(Rails.configuration.assets.precompile).to include("locales/es_MX.js")
|
|
|
|
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"])
|
|
|
|
expect(locale[:moment_js]).to eq(["tlh", "#{Rails.root}/lib/javascripts/moment_locale/tlh.js"])
|
|
|
|
expect(locale[:plural]).to eq(plural.with_indifferent_access)
|
|
|
|
|
|
|
|
expect(Rails.configuration.assets.precompile).to include("locales/tlh.js")
|
|
|
|
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
|
|
|
|
|
|
|
|
[
|
|
|
|
"config/locales/client.foo.yml",
|
|
|
|
"config/locales/server.foo.yml",
|
|
|
|
"lib/javascripts/locale/message_format/foo.js",
|
|
|
|
"lib/javascripts/locale/moment_js/foo.js",
|
|
|
|
"assets/locales/foo.js.erb"
|
|
|
|
].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)
|
|
|
|
|
|
|
|
register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
|
|
|
expect(DiscoursePluginRegistry.locales.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:21:52 -04:00
|
|
|
end
|