2017-04-12 10:52:52 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Theme do
|
|
|
|
|
|
|
|
before do
|
|
|
|
Theme.clear_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
let :user do
|
|
|
|
Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
let :customization_params do
|
|
|
|
{name: 'my name', user_id: user.id, header: "my awesome header"}
|
|
|
|
end
|
|
|
|
|
|
|
|
let :customization do
|
|
|
|
Theme.create!(customization_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set default key when creating a new customization' do
|
|
|
|
s = Theme.create!(name: 'my name', user_id: user.id)
|
|
|
|
expect(s.key).not_to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can support child themes' do
|
|
|
|
child = Theme.new(name: '2', user_id: user.id)
|
|
|
|
|
2017-05-02 16:01:01 -04:00
|
|
|
child.set_field(target: :common, name: "header", value: "World")
|
|
|
|
child.set_field(target: :desktop, name: "header", value: "Desktop")
|
|
|
|
child.set_field(target: :mobile, name: "header", value: "Mobile")
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
child.save!
|
|
|
|
|
|
|
|
expect(Theme.lookup_field(child.key, :desktop, "header")).to eq("World\nDesktop")
|
|
|
|
expect(Theme.lookup_field(child.key, "mobile", :header)).to eq("World\nMobile")
|
|
|
|
|
|
|
|
|
2017-05-02 16:01:01 -04:00
|
|
|
child.set_field(target: :common, name: "header", value: "Worldie")
|
2017-04-12 10:52:52 -04:00
|
|
|
child.save!
|
|
|
|
|
|
|
|
expect(Theme.lookup_field(child.key, :mobile, :header)).to eq("Worldie\nMobile")
|
|
|
|
|
|
|
|
parent = Theme.new(name: '1', user_id: user.id)
|
|
|
|
|
2017-05-02 16:01:01 -04:00
|
|
|
parent.set_field(target: :common, name: "header", value: "Common Parent")
|
|
|
|
parent.set_field(target: :mobile, name: "header", value: "Mobile Parent")
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
parent.save!
|
|
|
|
|
|
|
|
parent.add_child_theme!(child)
|
|
|
|
|
|
|
|
expect(Theme.lookup_field(parent.key, :mobile, "header")).to eq("Common Parent\nMobile Parent\nWorldie\nMobile")
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can correctly find parent themes' do
|
|
|
|
grandchild = Theme.create!(name: 'grandchild', user_id: user.id)
|
|
|
|
child = Theme.create!(name: 'child', user_id: user.id)
|
|
|
|
theme = Theme.create!(name: 'theme', user_id: user.id)
|
|
|
|
|
|
|
|
theme.add_child_theme!(child)
|
|
|
|
child.add_child_theme!(grandchild)
|
|
|
|
|
|
|
|
expect(grandchild.dependant_themes.length).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
it 'should correct bad html in body_tag_baked and head_tag_baked' do
|
|
|
|
theme = Theme.new(user_id: -1, name: "test")
|
2017-05-02 16:01:01 -04:00
|
|
|
theme.set_field(target: :common, name: "head_tag", value: "<b>I am bold")
|
2017-04-12 10:52:52 -04:00
|
|
|
theme.save!
|
|
|
|
|
|
|
|
expect(Theme.lookup_field(theme.key, :desktop, "head_tag")).to eq("<b>I am bold</b>")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should precompile fragments in body and head tags' do
|
|
|
|
with_template = <<HTML
|
|
|
|
<script type='text/x-handlebars' name='template'>
|
|
|
|
{{hello}}
|
|
|
|
</script>
|
|
|
|
<script type='text/x-handlebars' data-template-name='raw_template.raw'>
|
|
|
|
{{hello}}
|
|
|
|
</script>
|
|
|
|
HTML
|
|
|
|
theme = Theme.new(user_id: -1, name: "test")
|
2017-05-02 16:01:01 -04:00
|
|
|
theme.set_field(target: :common, name: "header", value: with_template)
|
2017-04-12 10:52:52 -04:00
|
|
|
theme.save!
|
|
|
|
|
|
|
|
baked = Theme.lookup_field(theme.key, :mobile, "header")
|
|
|
|
|
|
|
|
expect(baked).to match(/HTMLBars/)
|
|
|
|
expect(baked).to match(/raw-handlebars/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create body_tag_baked on demand if needed' do
|
|
|
|
|
|
|
|
theme = Theme.new(user_id: -1, name: "test")
|
2017-05-02 16:01:01 -04:00
|
|
|
theme.set_field(target: :common, name: :body_tag, value: "<b>test")
|
2017-04-12 10:52:52 -04:00
|
|
|
theme.save
|
|
|
|
|
|
|
|
ThemeField.update_all(value_baked: nil)
|
|
|
|
|
|
|
|
expect(Theme.lookup_field(theme.key, :desktop, :body_tag)).to match(/<b>test<\/b>/)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "plugin api" do
|
|
|
|
def transpile(html)
|
2017-05-02 16:01:01 -04:00
|
|
|
f = ThemeField.create!(target_id: Theme.targets[:mobile], theme_id: -1, name: "after_header", value: html)
|
2017-04-12 10:52:52 -04:00
|
|
|
f.value_baked
|
|
|
|
end
|
|
|
|
|
|
|
|
it "transpiles ES6 code" do
|
|
|
|
html = <<HTML
|
|
|
|
<script type='text/discourse-plugin' version='0.1'>
|
|
|
|
const x = 1;
|
|
|
|
</script>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
transpiled = transpile(html)
|
|
|
|
expect(transpiled).to match(/\<script\>/)
|
|
|
|
expect(transpiled).to match(/var x = 1;/)
|
|
|
|
expect(transpiled).to match(/_registerPluginCode\('0.1'/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts errors to a script type that is not evaluated" do
|
|
|
|
html = <<HTML
|
|
|
|
<script type='text/discourse-plugin' version='0.1'>
|
|
|
|
const x = 1;
|
|
|
|
x = 2;
|
|
|
|
</script>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
transpiled = transpile(html)
|
|
|
|
expect(transpiled).to match(/text\/discourse-js-error/)
|
|
|
|
expect(transpiled).to match(/read-only/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-02 16:01:01 -04:00
|
|
|
context 'theme vars' do
|
|
|
|
it 'can generate scss based off theme vars' do
|
|
|
|
theme = Theme.new(name: 'theme', user_id: -1)
|
|
|
|
theme.set_field(target: :common, name: :scss, value: 'body {color: $magic; content: quote($content)}')
|
|
|
|
theme.set_field(target: :common, name: :magic, value: 'red', type: :theme_var)
|
|
|
|
theme.set_field(target: :common, name: :content, value: 'Sam\'s Test', type: :theme_var)
|
|
|
|
theme.save
|
|
|
|
|
|
|
|
scss,_map = Stylesheet::Compiler.compile('@import "theme_variables"; @import "desktop_theme"; ', "theme.scss", theme_id: theme.id)
|
|
|
|
expect(scss).to include("red")
|
|
|
|
expect(scss).to include('"Sam\'s Test"')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-14 13:35:12 -04:00
|
|
|
it 'correctly caches theme keys' do
|
2017-05-03 11:31:16 -04:00
|
|
|
Theme.destroy_all
|
|
|
|
|
2017-04-14 13:35:12 -04:00
|
|
|
theme = Theme.create!(name: "bob", user_id: -1)
|
|
|
|
|
|
|
|
expect(Theme.theme_keys).to eq(Set.new([theme.key]))
|
|
|
|
expect(Theme.user_theme_keys).to eq(Set.new([]))
|
|
|
|
|
|
|
|
theme.user_selectable = true
|
|
|
|
theme.save
|
|
|
|
|
|
|
|
expect(Theme.user_theme_keys).to eq(Set.new([theme.key]))
|
|
|
|
|
|
|
|
theme.user_selectable = false
|
|
|
|
theme.save
|
|
|
|
|
|
|
|
theme.set_default!
|
|
|
|
expect(Theme.user_theme_keys).to eq(Set.new([theme.key]))
|
|
|
|
|
|
|
|
theme.destroy
|
|
|
|
|
|
|
|
expect(Theme.theme_keys).to eq(Set.new([]))
|
|
|
|
expect(Theme.user_theme_keys).to eq(Set.new([]))
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
end
|