require "test_helper" class Pressa::Views::LayoutTest < Minitest::Test def content_view Class.new(Phlex::HTML) do def view_template article do h1 { "Hello" } end end end.new end def site @site ||= Pressa::Site.new( author: "Sami Samhuri", email: "sami@samhuri.net", title: "samhuri.net", description: "blog", url: "https://samhuri.net" ) end def site_with_copyright_start_year(year) Pressa::Site.new( author: "Sami Samhuri", email: "sami@samhuri.net", title: "samhuri.net", description: "blog", url: "https://samhuri.net", copyright_start_year: year ) end def test_rendering_child_components_as_html_instead_of_escaped_text html = Pressa::Views::Layout.new( site:, canonical_url: "https://samhuri.net/posts/", content: content_view ).call assert_includes(html, "
") assert_includes(html, "

Hello

") refute_includes(html, "<article>") end def test_keeps_escaping_enabled_for_untrusted_string_fields subtitle = "" html = Pressa::Views::Layout.new( site:, canonical_url: "https://samhuri.net/posts/", page_subtitle: subtitle, content: content_view ).call assert_includes(html, "samhuri.net: <img src=x onerror=alert(1)>") end def test_preserves_absolute_stylesheet_urls cdn_site = Pressa::Site.new( author: "Sami Samhuri", email: "sami@samhuri.net", title: "samhuri.net", description: "blog", url: "https://samhuri.net", styles: [Pressa::Stylesheet.new(href: "https://cdn.example.com/site.css")] ) html = Pressa::Views::Layout.new( site: cdn_site, canonical_url: "https://samhuri.net/posts/", content: content_view ).call assert_includes(html, %()) end def test_footer_renders_year_range_using_copyright_start_year html = Pressa::Views::Layout.new( site: site_with_copyright_start_year(2006), canonical_url: "https://samhuri.net/posts/", content: content_view ).call assert_includes(html, "") end def test_footer_renders_single_year_when_start_year_matches_current_year current_year = Time.now.year html = Pressa::Views::Layout.new( site: site_with_copyright_start_year(current_year), canonical_url: "https://samhuri.net/posts/", content: content_view ).call assert_includes(html, "") refute_includes(html, "