2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-22 12:37:01 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'stylesheet/importer'
|
|
|
|
|
|
|
|
describe Stylesheet::Importer do
|
|
|
|
|
|
|
|
def compile_css(name)
|
|
|
|
Stylesheet::Compiler.compile_asset(name)[0]
|
|
|
|
end
|
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
context "#category_backgrounds" do
|
|
|
|
it "applies CDN to background category images" do
|
2021-07-06 13:11:10 -04:00
|
|
|
expect(compile_css("color_definitions")).to_not include("body.category-")
|
2017-05-22 12:37:01 -04:00
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
background = Fabricate(:upload)
|
|
|
|
parent_category = Fabricate(:category)
|
|
|
|
category = Fabricate(:category, parent_category_id: parent_category.id, uploaded_background: background)
|
2017-05-22 12:37:01 -04:00
|
|
|
|
2021-07-06 13:11:10 -04:00
|
|
|
expect(compile_css("color_definitions")).to include("body.category-#{parent_category.slug}-#{category.slug}{background-image:url(#{background.url})}")
|
2017-05-22 12:37:01 -04:00
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
GlobalSetting.stubs(:cdn_url).returns("//awesome.cdn")
|
2021-07-06 13:11:10 -04:00
|
|
|
expect(compile_css("color_definitions")).to include("body.category-#{parent_category.slug}-#{category.slug}{background-image:url(//awesome.cdn#{background.url})}")
|
2021-03-10 11:05:56 -05:00
|
|
|
end
|
2017-05-22 12:37:01 -04:00
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
it "applies S3 CDN to background category images" do
|
|
|
|
setup_s3
|
|
|
|
SiteSetting.s3_use_iam_profile = true
|
|
|
|
SiteSetting.s3_upload_bucket = 'test'
|
|
|
|
SiteSetting.s3_region = 'ap-southeast-2'
|
|
|
|
SiteSetting.s3_cdn_url = "https://s3.cdn"
|
2017-05-22 12:37:01 -04:00
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
background = Fabricate(:upload_s3)
|
|
|
|
category = Fabricate(:category, uploaded_background: background)
|
2017-05-22 12:37:01 -04:00
|
|
|
|
2021-07-06 13:11:10 -04:00
|
|
|
expect(compile_css("color_definitions")).to include("body.category-#{category.slug}{background-image:url(https://s3.cdn/original")
|
2021-03-10 11:05:56 -05:00
|
|
|
end
|
2017-05-22 12:37:01 -04:00
|
|
|
|
2020-09-04 09:25:50 -04:00
|
|
|
end
|
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
context "#font" do
|
|
|
|
it "includes font variable" do
|
|
|
|
default_font = ":root{--font-family: Arial, sans-serif}"
|
2021-07-06 13:11:10 -04:00
|
|
|
expect(compile_css("color_definitions")).to include(default_font)
|
2021-03-10 11:05:56 -05:00
|
|
|
expect(compile_css("embed")).to include(default_font)
|
|
|
|
expect(compile_css("publish")).to include(default_font)
|
|
|
|
end
|
2020-10-05 13:40:41 -04:00
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
it "includes separate body and heading font declarations" do
|
|
|
|
base_font = DiscourseFonts.fonts[2]
|
|
|
|
heading_font = DiscourseFonts.fonts[3]
|
2020-10-05 13:40:41 -04:00
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
SiteSetting.base_font = base_font[:key]
|
|
|
|
SiteSetting.heading_font = heading_font[:key]
|
|
|
|
|
2021-07-06 13:11:10 -04:00
|
|
|
expect(compile_css("color_definitions"))
|
2021-03-10 11:05:56 -05:00
|
|
|
.to include(":root{--font-family: #{base_font[:stack]}}")
|
|
|
|
.and include(":root{--heading-font-family: #{heading_font[:stack]}}")
|
2021-06-21 09:33:12 -04:00
|
|
|
|
|
|
|
set_cdn_url("http://cdn.localhost")
|
|
|
|
|
|
|
|
# uses CDN and includes cache-breaking param
|
2021-07-06 13:11:10 -04:00
|
|
|
expect(compile_css("color_definitions"))
|
2021-06-21 09:33:12 -04:00
|
|
|
.to include("http://cdn.localhost/fonts/#{base_font[:variants][0][:filename]}?v=#{DiscourseFonts::VERSION}")
|
|
|
|
.and include("http://cdn.localhost/fonts/#{heading_font[:variants][0][:filename]}?v=#{DiscourseFonts::VERSION}")
|
2021-03-10 11:05:56 -05:00
|
|
|
end
|
2020-10-05 13:40:41 -04:00
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
it "includes all fonts in wizard" do
|
|
|
|
expect(compile_css("wizard").scan(/\.body-font-/).count)
|
|
|
|
.to eq(DiscourseFonts.fonts.count)
|
2020-10-05 13:40:41 -04:00
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
expect(compile_css("wizard").scan(/\.heading-font-/).count)
|
|
|
|
.to eq(DiscourseFonts.fonts.count)
|
2020-09-04 09:25:50 -04:00
|
|
|
|
2021-03-10 11:05:56 -05:00
|
|
|
expect(compile_css("wizard").scan(/@font-face/).count)
|
|
|
|
.to eq(DiscourseFonts.fonts.map { |f| f[:variants]&.count || 0 }.sum)
|
|
|
|
end
|
2020-09-04 09:25:50 -04:00
|
|
|
end
|
|
|
|
|
2020-09-09 11:43:34 -04:00
|
|
|
context "#import_color_definitions" do
|
2021-02-04 08:51:18 -05:00
|
|
|
let(:scss) { ":root{--custom-color: green}" }
|
2021-03-24 18:35:52 -04:00
|
|
|
let(:scss_child) { "$navy: #000080; :root{--custom-color: red; --custom-color-rgb: \#{hexToRGB($navy)}}" }
|
2020-09-09 11:43:34 -04:00
|
|
|
|
|
|
|
let(:theme) do
|
|
|
|
Fabricate(:theme).tap do |t|
|
|
|
|
t.set_field(target: :common, name: "color_definitions", value: scss)
|
|
|
|
t.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-04 08:51:18 -05:00
|
|
|
let(:child) { Fabricate(:theme, component: true, name: "Child Theme").tap { |t|
|
2020-09-09 11:43:34 -04:00
|
|
|
t.set_field(target: :common, name: "color_definitions", value: scss_child)
|
|
|
|
t.save!
|
|
|
|
}}
|
|
|
|
|
|
|
|
it "should include color definitions in the theme" do
|
2021-02-19 11:22:24 -05:00
|
|
|
styles = Stylesheet::Importer.new({ theme_id: theme.id }).import_color_definitions
|
2020-09-09 11:43:34 -04:00
|
|
|
expect(styles).to include(scss)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include color definitions from components" do
|
|
|
|
theme.add_relative_theme!(:child, child)
|
|
|
|
theme.save!
|
|
|
|
|
2021-02-19 11:22:24 -05:00
|
|
|
styles = Stylesheet::Importer.new({ theme_id: theme.id }).import_color_definitions
|
2021-02-04 08:51:18 -05:00
|
|
|
expect(styles).to include("Color definitions from Child Theme")
|
2021-03-24 18:35:52 -04:00
|
|
|
expect(styles).to include("--custom-color: red")
|
|
|
|
expect(styles).to include("--custom-color-rgb: 0,0,128")
|
|
|
|
|
2020-09-09 11:43:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should include default theme color definitions" do
|
|
|
|
SiteSetting.default_theme_id = theme.id
|
2021-02-19 11:22:24 -05:00
|
|
|
styles = Stylesheet::Importer.new({}).import_color_definitions
|
2020-09-09 11:43:34 -04:00
|
|
|
expect(styles).to include(scss)
|
|
|
|
end
|
2020-10-15 14:05:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "#import_wcag_overrides" do
|
|
|
|
it "should do nothing on a regular scheme" do
|
|
|
|
scheme = ColorScheme.create_from_base(name: 'Regular')
|
2021-02-19 11:22:24 -05:00
|
|
|
expect(Stylesheet::Importer.new({ color_scheme_id: scheme.id }).import_wcag_overrides).to eq("")
|
2020-10-15 14:05:48 -04:00
|
|
|
end
|
2020-09-09 11:43:34 -04:00
|
|
|
|
2020-10-15 14:05:48 -04:00
|
|
|
it "should include WCAG overrides for WCAG based scheme" do
|
|
|
|
scheme = ColorScheme.create_from_base(name: 'WCAG New', base_scheme_id: "WCAG Dark")
|
2021-02-19 11:22:24 -05:00
|
|
|
expect(Stylesheet::Importer.new({ color_scheme_id: scheme.id }).import_wcag_overrides).to eq("@import \"wcag\";")
|
2020-10-15 14:05:48 -04:00
|
|
|
end
|
2020-09-09 11:43:34 -04:00
|
|
|
end
|
2017-05-22 12:37:01 -04:00
|
|
|
end
|