2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe SiteCustomization do
|
|
|
|
|
|
|
|
let :user do
|
|
|
|
Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
2013-09-16 12:21:49 -04:00
|
|
|
let :customization_params do
|
2013-09-16 12:55:44 -04:00
|
|
|
{name: 'my name', user_id: user.id, header: "my awesome header", stylesheet: "my awesome css", mobile_stylesheet: nil, mobile_header: nil}
|
2013-09-16 12:21:49 -04:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
let :customization do
|
2013-09-16 12:21:49 -04:00
|
|
|
SiteCustomization.create!(customization_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
let :customization_with_mobile do
|
|
|
|
SiteCustomization.create!(customization_params.merge(mobile_stylesheet: ".mobile {better: true;}", mobile_header: "fancy mobile stuff"))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should set default key when creating a new customization' do
|
2013-02-05 14:16:51 -05:00
|
|
|
s = SiteCustomization.create!(name: 'my name', user_id: user.id)
|
|
|
|
s.key.should_not == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'caching' do
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
context 'enabled style' do
|
2013-02-07 08:55:04 -05:00
|
|
|
before do
|
|
|
|
@customization = customization
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'finds no style when none enabled' do
|
2013-02-07 08:55:04 -05:00
|
|
|
SiteCustomization.enabled_style_key.should be_nil
|
|
|
|
end
|
|
|
|
|
2013-02-10 07:05:11 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'finds the enabled style' do
|
|
|
|
@customization.enabled = true
|
2013-02-07 08:55:04 -05:00
|
|
|
@customization.save
|
|
|
|
SiteCustomization.enabled_style_key.should == @customization.key
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'finds no enabled style on other sites' do
|
|
|
|
@customization.enabled = true
|
2013-02-07 08:55:04 -05:00
|
|
|
@customization.save
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-07 08:55:04 -05:00
|
|
|
RailsMultisite::ConnectionManagement.expects(:current_db).returns("foo").twice
|
|
|
|
# the mocking is tricky, lets remove the record so we can properly pretend we are on another db
|
|
|
|
# this bypasses the before / after stuff
|
|
|
|
SiteCustomization.exec_sql('delete from site_customizations')
|
|
|
|
|
|
|
|
SiteCustomization.enabled_style_key.should be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'ensure stylesheet is on disk on first fetch' do
|
2013-02-10 07:05:11 -05:00
|
|
|
c = customization
|
|
|
|
c.remove_from_cache!
|
|
|
|
File.delete(c.stylesheet_fullpath)
|
2013-09-16 12:21:49 -04:00
|
|
|
File.delete(c.stylesheet_fullpath(:mobile))
|
2013-02-10 07:05:11 -05:00
|
|
|
|
|
|
|
SiteCustomization.custom_stylesheet(c.key)
|
|
|
|
File.exists?(c.stylesheet_fullpath).should == true
|
2013-09-16 12:21:49 -04:00
|
|
|
File.exists?(c.stylesheet_fullpath(:mobile)).should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
context '#custom_stylesheet' do
|
|
|
|
it 'should allow me to lookup a filename containing my preview stylesheet' do
|
|
|
|
SiteCustomization.custom_stylesheet(customization.key).should ==
|
2014-07-24 12:21:22 -04:00
|
|
|
"<link class=\"custom-css\" rel=\"stylesheet\" href=\"/uploads/stylesheet-cache/#{customization.key}.css?#{customization.stylesheet_hash}\" type=\"text/css\" media=\"all\">"
|
2013-09-16 12:21:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return blank link tag for mobile if mobile_stylesheet is blank" do
|
|
|
|
SiteCustomization.custom_stylesheet(customization.key, :mobile).should == ""
|
|
|
|
end
|
2013-02-10 07:05:11 -05:00
|
|
|
|
2013-09-16 12:21:49 -04:00
|
|
|
it "should return link tag for mobile custom stylesheet" do
|
|
|
|
SiteCustomization.custom_stylesheet(customization_with_mobile.key, :mobile).should ==
|
2014-07-24 12:21:22 -04:00
|
|
|
"<link class=\"custom-css\" rel=\"stylesheet\" href=\"/uploads/stylesheet-cache/mobile_#{customization_with_mobile.key}.css?#{customization_with_mobile.stylesheet_hash(:mobile)}\" type=\"text/css\" media=\"all\">"
|
2013-09-16 12:21:49 -04:00
|
|
|
end
|
2013-02-10 07:05:11 -05:00
|
|
|
end
|
|
|
|
|
2013-09-16 12:21:49 -04:00
|
|
|
context '#custom_header' do
|
|
|
|
it "returns empty string when there is no custom header" do
|
|
|
|
c = SiteCustomization.create!(customization_params.merge(header: ''))
|
|
|
|
SiteCustomization.custom_header(c.key).should == ''
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can return the custom header html" do
|
|
|
|
SiteCustomization.custom_header(customization.key).should == customization_params[:header]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns empty string for mobile header when there's no custom mobile header" do
|
|
|
|
SiteCustomization.custom_header(customization.key, :mobile).should == ''
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can return the custom mobile header html" do
|
|
|
|
SiteCustomization.custom_header(customization_with_mobile.key, :mobile).should == customization_with_mobile.mobile_header
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should fix stylesheet files after changing the stylesheet' do
|
|
|
|
old_file = customization.stylesheet_fullpath
|
2013-02-05 14:16:51 -05:00
|
|
|
original = SiteCustomization.custom_stylesheet(customization.key)
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
File.exists?(old_file).should == true
|
|
|
|
customization.stylesheet = "div { clear:both; }"
|
|
|
|
customization.save
|
|
|
|
|
|
|
|
SiteCustomization.custom_stylesheet(customization.key).should_not == original
|
|
|
|
end
|
|
|
|
|
2013-09-16 12:21:49 -04:00
|
|
|
it 'should fix mobile stylesheet files after changing the mobile_stylesheet' do
|
|
|
|
old_file = customization_with_mobile.stylesheet_fullpath(:mobile)
|
|
|
|
original = SiteCustomization.custom_stylesheet(customization_with_mobile.key, :mobile)
|
|
|
|
|
|
|
|
File.exists?(old_file).should == true
|
|
|
|
customization_with_mobile.mobile_stylesheet = "div { clear:both; }"
|
|
|
|
customization_with_mobile.save
|
|
|
|
|
|
|
|
SiteCustomization.custom_stylesheet(customization_with_mobile.key).should_not == original
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'should delete old stylesheet files after deleting' do
|
2013-02-25 11:42:20 -05:00
|
|
|
old_file = customization.stylesheet_fullpath
|
2013-09-16 12:21:49 -04:00
|
|
|
customization.ensure_stylesheets_on_disk!
|
2013-02-05 14:16:51 -05:00
|
|
|
customization.destroy
|
|
|
|
File.exists?(old_file).should == false
|
|
|
|
end
|
|
|
|
|
2013-09-16 12:21:49 -04:00
|
|
|
it 'should delete old mobile stylesheet files after deleting' do
|
|
|
|
old_file = customization_with_mobile.stylesheet_fullpath(:mobile)
|
|
|
|
customization_with_mobile.ensure_stylesheets_on_disk!
|
|
|
|
customization_with_mobile.destroy
|
|
|
|
File.exists?(old_file).should == false
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should nuke old revs out of the cache' do
|
2013-02-05 14:16:51 -05:00
|
|
|
old_style = SiteCustomization.custom_stylesheet(customization.key)
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
customization.stylesheet = "hello worldz"
|
|
|
|
customization.save
|
|
|
|
SiteCustomization.custom_stylesheet(customization.key).should_not == old_style
|
|
|
|
end
|
|
|
|
|
2013-09-16 12:21:49 -04:00
|
|
|
it 'should nuke old revs out of the cache for mobile too' do
|
|
|
|
old_style = SiteCustomization.custom_stylesheet(customization_with_mobile.key)
|
|
|
|
|
|
|
|
customization_with_mobile.mobile_stylesheet = "hello worldz"
|
|
|
|
customization_with_mobile.save
|
|
|
|
SiteCustomization.custom_stylesheet(customization.key, :mobile).should_not == old_style
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should compile scss' do
|
2013-02-05 14:16:51 -05:00
|
|
|
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: '$black: #000; #a { color: $black; }', header: '')
|
2014-05-20 15:32:35 -04:00
|
|
|
s = c.stylesheet_baked.gsub(' ', '').gsub("\n", '')
|
|
|
|
(s.include?("#a{color:#000;}") || s.include?("#a{color:black;}")).should be_true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-09-16 12:21:49 -04:00
|
|
|
it 'should compile mobile scss' do
|
|
|
|
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: '', header: '', mobile_stylesheet: '$black: #000; #a { color: $black; }', mobile_header: '')
|
2014-05-20 15:32:35 -04:00
|
|
|
s = c.mobile_stylesheet_baked.gsub(' ', '').gsub("\n", '')
|
|
|
|
(s.include?("#a{color:#000;}") || s.include?("#a{color:black;}")).should be_true
|
2014-04-01 07:42:14 -04:00
|
|
|
end
|
|
|
|
|
2014-04-08 10:39:11 -04:00
|
|
|
it 'should allow including discourse styles' do
|
2014-04-01 07:42:14 -04:00
|
|
|
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: '@import "desktop";', mobile_stylesheet: '@import "mobile";')
|
|
|
|
c.stylesheet_baked.should_not =~ /Syntax error/
|
|
|
|
c.stylesheet_baked.length.should > 1000
|
|
|
|
c.mobile_stylesheet_baked.should_not =~ /Syntax error/
|
|
|
|
c.mobile_stylesheet_baked.length.should > 1000
|
2013-09-16 12:21:49 -04:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should provide an awesome error on failure' do
|
2013-02-05 14:16:51 -05:00
|
|
|
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: "$black: #000; #a { color: $black; }\n\n\nboom", header: '')
|
|
|
|
c.stylesheet_baked.should =~ /Syntax error/
|
2013-09-16 12:21:49 -04:00
|
|
|
c.mobile_stylesheet_baked.should_not be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should provide an awesome error on failure for mobile too' do
|
|
|
|
c = SiteCustomization.create!(user_id: user.id, name: "test", stylesheet: '', header: '', mobile_stylesheet: "$black: #000; #a { color: $black; }\n\n\nboom", mobile_header: '')
|
|
|
|
c.mobile_stylesheet_baked.should =~ /Syntax error/
|
|
|
|
c.stylesheet_baked.should_not be_present
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|