2015-10-11 10:41:23 +01:00
require 'rails_helper'
2013-08-23 16:21:52 +10:00
require_dependency 'plugin/instance'
describe Plugin :: Instance do
2014-04-02 16:22:12 +11:00
after do
2014-12-09 14:20:53 -05:00
DiscoursePluginRegistry . reset!
2014-04-02 16:22:12 +11:00
end
2013-08-26 11:04:16 +10:00
context " find_all " do
it " can find plugins correctly " do
plugins = Plugin :: Instance . find_all ( " #{ Rails . root } /spec/fixtures/plugins " )
2015-01-09 13:34:37 -03:00
expect ( plugins . count ) . to eq ( 1 )
2013-10-21 11:18:24 +02:00
plugin = plugins [ 0 ]
2013-08-26 11:04:16 +10:00
2015-01-09 13:34:37 -03:00
expect ( plugin . name ) . to eq ( " plugin-name " )
expect ( plugin . path ) . to eq ( " #{ Rails . root } /spec/fixtures/plugins/my_plugin/plugin.rb " )
2013-08-26 11:04:16 +10:00
end
it " does not blow up on missing directory " do
plugins = Plugin :: Instance . find_all ( " #{ Rails . root } /frank_zappa " )
2015-01-09 13:34:37 -03:00
expect ( plugins . count ) . to eq ( 0 )
2013-08-26 11:04:16 +10: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
@plugin . on ( :hello ) { @hello_count += 1 }
# Serializer
@plugin . add_to_serializer ( :trout , :scales ) { 1024 }
@serializer = TroutSerializer . new ( @trout )
end
2015-02-04 17:33:18 -05:00
after do
DiscourseEvent . clear
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 16:33:35 +02:00
context " register asset " do
2014-12-09 14:20:53 -05:00
it " populates the DiscoursePluginRegistry " do
2014-04-07 16:33:35 +02:00
plugin = Plugin :: Instance . new nil , " /tmp/test.rb "
plugin . register_asset ( " test.css " )
plugin . register_asset ( " test2.css " )
2014-04-10 16:30:22 +10:00
plugin . send :register_assets!
2015-01-09 13:34:37 -03:00
expect ( DiscoursePluginRegistry . mobile_stylesheets . count ) . to eq ( 0 )
expect ( DiscoursePluginRegistry . stylesheets . count ) . to eq ( 2 )
2014-04-07 16:33:35 +02:00
end
end
2013-08-23 16:21:52 +10:00
context " activate! " do
it " can activate plugins correctly " do
2013-08-26 11:04:16 +10:00
plugin = Plugin :: Instance . new
2013-08-23 16:21:52 +10: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!
2015-01-09 13:34:37 -03:00
expect ( plugin . auth_providers . count ) . to eq ( 1 )
2013-08-23 16:21:52 +10:00
auth_provider = plugin . auth_providers [ 0 ]
2015-01-09 13:34:37 -03:00
expect ( auth_provider . authenticator . name ) . to eq ( 'ubuntu' )
2013-08-23 16:21:52 +10:00
# calls ensure_assets! make sure they are there
2015-01-09 13:34:37 -03:00
expect ( plugin . assets . count ) . to eq ( 1 )
2014-04-10 16:30:22 +10:00
plugin . assets . each do | a , opts |
2015-01-09 13:34:37 -03:00
expect ( File . exists? ( a ) ) . to eq ( true )
2013-08-23 16:21:52 +10:00
end
# ensure it cleans up all crap in autogenerated directory
2015-01-09 13:34:37 -03:00
expect ( File . exists? ( junk_file ) ) . to eq ( false )
2013-08-23 16:21:52 +10:00
end
2014-04-07 16:33:35 +02: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 16:30:22 +10:00
2014-04-07 16:33:35 +02:00
plugin . register_asset ( " test.css " )
plugin . register_asset ( " test2.scss " )
2014-04-10 16:30:22 +10:00
plugin . register_asset ( " mobile.css " , :mobile )
plugin . register_asset ( " desktop.css " , :desktop )
plugin . register_asset ( " desktop2.css " , :desktop )
2014-04-25 10:26:37 +02:00
plugin . register_asset ( " variables1.scss " , :variables )
plugin . register_asset ( " variables2.scss " , :variables )
2014-04-07 16:33:35 +02:00
plugin . register_asset ( " code.js " )
2014-04-10 16:30:22 +10:00
2014-04-07 16:33:35 +02:00
plugin . register_asset ( " server_side.js " , :server_side )
2014-04-10 16:30:22 +10:00
2014-04-07 16:33:35 +02:00
plugin . register_asset ( " my_admin.js " , :admin )
plugin . register_asset ( " my_admin2.js " , :admin )
plugin . activate!
2015-01-09 13:34:37 -03:00
expect ( DiscoursePluginRegistry . javascripts . count ) . to eq ( 3 )
expect ( DiscoursePluginRegistry . admin_javascripts . count ) . to eq ( 2 )
expect ( DiscoursePluginRegistry . server_side_javascripts . count ) . to eq ( 1 )
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 16:33:35 +02:00
end
2013-08-23 16:21:52 +10:00
end
2014-06-11 11:57:22 +10:00
context " serialized_current_user_fields " do
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 13:34:37 -03:00
expect ( payload [ " current_user " ] [ " custom_fields " ] [ " has_car " ] ) . to eq ( " true " )
2014-06-11 11:57:22 +10: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 13:34:37 -03: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
2013-08-23 16:21:52 +10:00
end