mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: support uploads for themes
This allows themes to bundle various assets
This commit is contained in:
@@ -11,6 +11,26 @@ describe Admin::ThemesController do
|
||||
@user = log_in(:admin)
|
||||
end
|
||||
|
||||
context '.upload_asset' do
|
||||
render_views
|
||||
|
||||
let(:upload) do
|
||||
ActionDispatch::Http::UploadedFile.new({
|
||||
filename: 'test.woff2',
|
||||
tempfile: file_from_fixtures("fake.woff2", "woff2")
|
||||
})
|
||||
end
|
||||
|
||||
it 'can create a theme upload' do
|
||||
xhr :post, :upload_asset, file: upload, original_filename: 'wooof.woff2'
|
||||
expect(response.status).to eq(201)
|
||||
upload = Upload.find_by(original_filename: "wooof.woff2")
|
||||
expect(upload.id).not_to be_nil
|
||||
expect(JSON.parse(response.body)["upload_id"]).to eq(upload.id)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context '.import' do
|
||||
let(:theme_file) do
|
||||
ActionDispatch::Http::UploadedFile.new({
|
||||
@@ -93,30 +113,35 @@ describe Admin::ThemesController do
|
||||
end
|
||||
|
||||
it 'updates a theme' do
|
||||
#focus
|
||||
theme = Theme.new(name: 'my name', user_id: -1)
|
||||
theme.set_field(target: :common, name: :scss, value: '.body{color: black;}')
|
||||
theme.save
|
||||
|
||||
child_theme = Theme.create(name: 'my name', user_id: -1)
|
||||
|
||||
upload = Fabricate(:upload)
|
||||
|
||||
xhr :put, :update, id: theme.id,
|
||||
theme: {
|
||||
child_theme_ids: [child_theme.id],
|
||||
name: 'my test name',
|
||||
theme_fields: [
|
||||
{ name: 'scss', target: 'common', value: '' },
|
||||
{ name: 'scss', target: 'desktop', value: 'body{color: blue;}' }
|
||||
{ name: 'scss', target: 'desktop', value: 'body{color: blue;}' },
|
||||
{ name: 'bob', target: 'common', value: '', type_id: 2, upload_id: upload.id },
|
||||
]
|
||||
}
|
||||
expect(response).to be_success
|
||||
|
||||
json = ::JSON.parse(response.body)
|
||||
|
||||
fields = json["theme"]["theme_fields"]
|
||||
fields = json["theme"]["theme_fields"].sort{|a,b| a["value"] <=> b["value"]}
|
||||
|
||||
expect(fields.first["value"]).to eq('body{color: blue;}')
|
||||
expect(fields.length).to eq(1)
|
||||
expect(fields[0]["value"]).to eq('')
|
||||
expect(fields[0]["upload_id"]).to eq(upload.id)
|
||||
expect(fields[1]["value"]).to eq('body{color: blue;}')
|
||||
|
||||
expect(fields.length).to eq(2)
|
||||
|
||||
expect(json["theme"]["child_themes"].length).to eq(1)
|
||||
|
||||
|
||||
1
spec/fixtures/woff2/fake.woff2
vendored
Normal file
1
spec/fixtures/woff2/fake.woff2
vendored
Normal file
@@ -0,0 +1 @@
|
||||
not a woff
|
||||
@@ -9,7 +9,7 @@ describe RemoteTheme do
|
||||
`cd #{repo_dir} && git init . `
|
||||
`cd #{repo_dir} && git config user.email 'someone@cool.com'`
|
||||
`cd #{repo_dir} && git config user.name 'The Cool One'`
|
||||
`cd #{repo_dir} && mkdir desktop mobile common`
|
||||
`cd #{repo_dir} && mkdir desktop mobile common assets`
|
||||
files.each do |name, data|
|
||||
File.write("#{repo_dir}/#{name}", data)
|
||||
`cd #{repo_dir} && git add #{name}`
|
||||
@@ -26,6 +26,17 @@ describe RemoteTheme do
|
||||
"name": "awesome theme",
|
||||
"about_url": "https://www.site.com/about",
|
||||
"license_url": "https://www.site.com/license",
|
||||
"assets": {
|
||||
"font": "assets/awesome.woff2"
|
||||
},
|
||||
"fields": {
|
||||
"color": {
|
||||
"target": "desktop",
|
||||
"value": "#FEF",
|
||||
"type": "color"
|
||||
},
|
||||
"name": "sam"
|
||||
},
|
||||
"color_schemes": {
|
||||
"Amazing": {
|
||||
"love": "#{options[:love]}"
|
||||
@@ -35,13 +46,18 @@ describe RemoteTheme do
|
||||
JSON
|
||||
end
|
||||
|
||||
let :scss_data do
|
||||
"@font-face { font-family: magic; src: url($font)}; body {color: $color; content: $name;}"
|
||||
end
|
||||
|
||||
let :initial_repo do
|
||||
setup_git_repo(
|
||||
"about.json" => about_json,
|
||||
"desktop/desktop.scss" => "body {color: red;}",
|
||||
"desktop/desktop.scss" => scss_data,
|
||||
"common/header.html" => "I AM HEADER",
|
||||
"common/random.html" => "I AM SILLY",
|
||||
"common/embedded.scss" => "EMBED",
|
||||
"assets/awesome.woff2" => "FAKE FONT",
|
||||
)
|
||||
end
|
||||
|
||||
@@ -65,14 +81,20 @@ JSON
|
||||
expect(remote.about_url).to eq("https://www.site.com/about")
|
||||
expect(remote.license_url).to eq("https://www.site.com/license")
|
||||
|
||||
expect(@theme.theme_fields.length).to eq(3)
|
||||
expect(@theme.theme_fields.length).to eq(6)
|
||||
|
||||
mapped = Hash[*@theme.theme_fields.map{|f| ["#{f.target_id}-#{f.name}", f.value]}.flatten]
|
||||
|
||||
expect(mapped["0-header"]).to eq("I AM HEADER")
|
||||
expect(mapped["1-scss"]).to eq("body {color: red;}")
|
||||
expect(mapped["1-scss"]).to eq(scss_data)
|
||||
expect(mapped["0-embedded_scss"]).to eq("EMBED")
|
||||
|
||||
expect(mapped["1-color"]).to eq("#FEF")
|
||||
expect(mapped["0-font"]).to eq("")
|
||||
expect(mapped["0-name"]).to eq("sam")
|
||||
|
||||
expect(mapped.length).to eq(6)
|
||||
|
||||
expect(remote.remote_updated_at).to eq(time)
|
||||
|
||||
scheme = ColorScheme.find_by(theme_id: @theme.id)
|
||||
@@ -104,7 +126,7 @@ JSON
|
||||
mapped = Hash[*@theme.theme_fields.map{|f| ["#{f.target_id}-#{f.name}", f.value]}.flatten]
|
||||
|
||||
expect(mapped["0-header"]).to eq("I AM UPDATED")
|
||||
expect(mapped["1-scss"]).to eq("body {color: red;}")
|
||||
expect(mapped["1-scss"]).to eq(scss_data)
|
||||
expect(remote.remote_updated_at).to eq(time)
|
||||
|
||||
end
|
||||
|
||||
@@ -182,6 +182,9 @@ HTML
|
||||
|
||||
expect(Upload.where(id: upload.id)).to be_exist
|
||||
|
||||
# no error for theme field
|
||||
theme.reload
|
||||
expect(theme.theme_fields.find_by(name: :scss).error).to eq(nil)
|
||||
|
||||
scss,_map = Stylesheet::Compiler.compile('@import "theme_variables"; @import "desktop_theme"; ', "theme.scss", theme_id: theme.id)
|
||||
expect(scss).to include(upload.url)
|
||||
|
||||
Reference in New Issue
Block a user