2019-06-17 14:51:17 -04:00
# coding: utf-8
2019-04-29 20:27:42 -04:00
# frozen_string_literal: true
2022-07-27 22:27:38 -04:00
RSpec . describe ApplicationHelper do
2017-10-06 01:20:01 -04:00
describe " preload_script " do
2024-02-16 06:16:54 -05:00
def script_tag ( url , entrypoint , nonce )
2019-11-04 20:15:44 -05:00
<< ~ HTML
2024-02-16 06:16:54 -05:00
< script defer src = " #{ url } " data - discourse - entrypoint = " #{ entrypoint } " nonce = " #{ nonce } " > < / script>
2019-11-04 20:15:44 -05:00
HTML
end
2024-03-14 00:48:29 -04:00
it " does not send crawler content to logged on users " do
controller . stubs ( :use_crawler_layout? ) . returns ( false )
helper . stubs ( :current_user ) . returns ( Fabricate ( :user ) )
helper . request . user_agent = " Firefox "
expect ( helper . include_crawler_content? ) . to eq ( false )
end
2024-04-01 17:27:16 -04:00
it " sends crawler content to logged on users who wants to print " do
helper . stubs ( :current_user ) . returns ( Fabricate ( :user ) )
2024-04-25 17:41:57 -04:00
controller . stubs ( :use_crawler_layout? ) . returns ( false )
2024-04-01 17:27:16 -04:00
helper . stubs ( :params ) . returns ( print : true )
expect ( helper . include_crawler_content? ) . to eq ( true )
end
2024-04-25 17:41:57 -04:00
it " sends crawler content to logged on users with a crawler user agent " do
helper . stubs ( :current_user ) . returns ( Fabricate ( :user ) )
controller . stubs ( :use_crawler_layout? ) . returns ( true )
expect ( helper . include_crawler_content? ) . to eq ( true )
end
2022-04-06 06:09:12 -04:00
it " sends crawler content to old mobiles " do
controller . stubs ( :use_crawler_layout? ) . returns ( false )
helper . request . user_agent =
" Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 "
expect ( helper . include_crawler_content? ) . to eq ( true )
end
it " does not send crawler content to new mobiles " do
controller . stubs ( :use_crawler_layout? ) . returns ( false )
helper . request . user_agent =
" Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Mobile Safari/537.36 (compatible "
expect ( helper . include_crawler_content? ) . to eq ( false )
end
2017-10-06 01:20:01 -04:00
context " with s3 CDN " do
before do
global_setting :s3_bucket , " test_bucket "
global_setting :s3_region , " ap-australia "
global_setting :s3_access_key_id , " 123 "
global_setting :s3_secret_access_key , " 123 "
global_setting :s3_cdn_url , " https://s3cdn.com "
end
2018-08-21 22:31:13 -04:00
it " deals correctly with subfolder " do
2019-11-15 00:48:24 -05:00
set_subfolder " /community "
2023-09-05 15:56:03 -04:00
expect ( helper . preload_script ( " start-discourse " ) ) . to include (
" https://s3cdn.com/assets/start-discourse.js " ,
2022-06-20 10:33:05 -04:00
)
2018-08-21 22:31:13 -04:00
end
2019-06-17 14:51:17 -04:00
it " replaces cdn URLs with s3 cdn subfolder paths " do
global_setting :s3_cdn_url , " https://s3cdn.com/s3_subpath "
set_cdn_url " https://awesome.com "
2019-11-15 00:48:24 -05:00
set_subfolder " /community "
2023-09-05 15:56:03 -04:00
expect ( helper . preload_script ( " start-discourse " ) ) . to include (
" https://s3cdn.com/s3_subpath/assets/start-discourse.js " ,
2022-06-20 10:33:05 -04:00
)
2019-06-17 14:51:17 -04:00
end
2017-10-06 01:20:01 -04:00
it " returns magic brotli mangling for brotli requests " do
helper . request . env [ " HTTP_ACCEPT_ENCODING " ] = " br "
2023-09-05 15:56:03 -04:00
link = helper . preload_script ( " start-discourse " )
2017-10-06 01:20:01 -04:00
2023-11-10 06:16:06 -05:00
expect ( link ) . to eq (
2024-02-16 06:16:54 -05:00
script_tag (
" https://s3cdn.com/assets/start-discourse.br.js " ,
" start-discourse " ,
helper . csp_nonce_placeholder ,
) ,
2023-11-10 06:16:06 -05:00
)
2017-10-06 01:20:01 -04:00
end
it " gives s3 cdn if asset host is not set " do
2023-09-05 15:56:03 -04:00
link = helper . preload_script ( " start-discourse " )
2017-10-06 01:20:01 -04:00
2023-11-10 06:16:06 -05:00
expect ( link ) . to eq (
2024-02-16 06:16:54 -05:00
script_tag (
" https://s3cdn.com/assets/start-discourse.js " ,
" start-discourse " ,
helper . csp_nonce_placeholder ,
) ,
2023-11-10 06:16:06 -05:00
)
2017-10-06 01:20:01 -04:00
end
2019-07-16 10:05:37 -04:00
it " can fall back to gzip compression " do
helper . request . env [ " HTTP_ACCEPT_ENCODING " ] = " gzip "
2023-09-05 15:56:03 -04:00
link = helper . preload_script ( " start-discourse " )
2023-11-10 06:16:06 -05:00
expect ( link ) . to eq (
2024-02-16 06:16:54 -05:00
script_tag (
" https://s3cdn.com/assets/start-discourse.gz.js " ,
" start-discourse " ,
helper . csp_nonce_placeholder ,
) ,
2023-11-10 06:16:06 -05:00
)
2019-07-16 10:05:37 -04:00
end
2017-10-06 01:20:01 -04:00
it " gives s3 cdn even if asset host is set " do
set_cdn_url " https://awesome.com "
2023-09-05 15:56:03 -04:00
link = helper . preload_script ( " start-discourse " )
2017-10-06 01:20:01 -04:00
2023-11-10 06:16:06 -05:00
expect ( link ) . to eq (
2024-02-16 06:16:54 -05:00
script_tag (
" https://s3cdn.com/assets/start-discourse.js " ,
" start-discourse " ,
helper . csp_nonce_placeholder ,
) ,
2023-11-10 06:16:06 -05:00
)
2017-10-06 01:20:01 -04:00
end
2021-07-14 15:52:35 -04:00
it " gives s3 cdn but without brotli/gzip extensions for theme tests assets " do
helper . request . env [ " HTTP_ACCEPT_ENCODING " ] = " gzip, br "
link = helper . preload_script ( " discourse/tests/theme_qunit_ember_jquery " )
2022-10-11 19:11:44 -04:00
expect ( link ) . to eq (
2023-11-10 06:16:06 -05:00
script_tag (
" https://s3cdn.com/assets/discourse/tests/theme_qunit_ember_jquery.js " ,
" discourse/tests/theme_qunit_ember_jquery " ,
2024-02-16 06:16:54 -05:00
helper . csp_nonce_placeholder ,
2023-11-10 06:16:06 -05:00
) ,
2022-10-11 19:11:44 -04:00
)
2021-07-14 15:52:35 -04:00
end
2022-12-08 05:36:20 -05:00
it " uses separate asset CDN if configured " do
global_setting :s3_asset_cdn_url , " https://s3-asset-cdn.example.com "
2023-09-05 15:56:03 -04:00
expect ( helper . preload_script ( " start-discourse " ) ) . to include (
" https://s3-asset-cdn.example.com/assets/start-discourse.js " ,
2022-12-08 05:36:20 -05:00
)
end
2017-10-06 01:20:01 -04:00
end
end
2022-10-11 19:11:44 -04:00
describe " add_resource_preload_list " do
2024-04-04 09:37:44 -04:00
it " adds resources to the preload list " do
2023-09-05 15:56:03 -04:00
add_resource_preload_list ( " /assets/start-discourse.js " , " script " )
2022-10-11 19:11:44 -04:00
add_resource_preload_list ( " /assets/discourse.css " , " style " )
2024-04-04 09:37:44 -04:00
expect ( controller . instance_variable_get ( :@asset_preload_links ) . size ) . to eq ( 2 )
2022-10-11 19:11:44 -04:00
end
it " adds resources to the preload list when preload_script is called " do
2023-09-05 15:56:03 -04:00
helper . preload_script ( " start-discourse " )
2022-10-11 19:11:44 -04:00
2024-04-04 09:37:44 -04:00
expect ( controller . instance_variable_get ( :@asset_preload_links ) . size ) . to eq ( 1 )
2022-10-11 19:11:44 -04:00
end
it " adds resources to the preload list when discourse_stylesheet_link_tag is called " do
helper . discourse_stylesheet_link_tag ( :desktop )
2024-04-04 09:37:44 -04:00
expect ( controller . instance_variable_get ( :@asset_preload_links ) . size ) . to eq ( 1 )
2022-10-11 19:11:44 -04:00
end
it " adds resources as the correct type " do
helper . discourse_stylesheet_link_tag ( :desktop )
2023-09-05 15:56:03 -04:00
helper . preload_script ( " start-discourse " )
2022-10-11 19:11:44 -04:00
2024-04-04 09:37:44 -04:00
expect ( controller . instance_variable_get ( :@asset_preload_links ) [ 0 ] ) . to match ( / as="style" / )
expect ( controller . instance_variable_get ( :@asset_preload_links ) [ 1 ] ) . to match ( / as="script" / )
2022-10-11 19:11:44 -04:00
end
end
2013-11-04 21:40:39 -05:00
describe " escape_unicode " do
it " encodes tags " do
2014-12-31 09:55:03 -05:00
expect ( helper . escape_unicode ( " <tag> " ) ) . to eq ( " \ u003ctag> " )
2013-11-04 21:40:39 -05:00
end
it " survives junk text " do
2014-12-31 09:55:03 -05:00
expect ( helper . escape_unicode ( " hello \xc3 \x28 world " ) ) . to match ( / hello.*world / )
2013-11-04 21:40:39 -05:00
end
end
2022-09-23 03:05:53 -04:00
describe " render_sitelinks_search_tag " do
context " for non-subfolder install " do
context " when on homepage " do
it " will return sitelinks search tag " do
helper . stubs ( :current_page? ) . returns ( false )
helper . stubs ( :current_page? ) . with ( " / " ) . returns ( true )
2023-08-24 10:07:45 -04:00
sitelinks_search_tag =
JSON . parse (
helper
. render_sitelinks_search_tag
. gsub ( '<script type="application/ld+json">' , " " )
. gsub ( " </script> " , " " ) ,
)
expect ( sitelinks_search_tag [ " @type " ] ) . to eq ( " WebSite " )
expect ( sitelinks_search_tag [ " potentialAction " ] [ " @type " ] ) . to eq ( " SearchAction " )
expect ( sitelinks_search_tag [ " name " ] ) . to eq ( SiteSetting . title )
expect ( sitelinks_search_tag [ " url " ] ) . to eq ( Discourse . base_url )
2022-09-23 03:05:53 -04:00
end
end
context " when not on homepage " do
it " will not return sitelinks search tag " do
helper . stubs ( :current_page? ) . returns ( true )
helper . stubs ( :current_page? ) . with ( " / " ) . returns ( false )
helper . stubs ( :current_page? ) . with ( Discourse . base_path ) . returns ( false )
expect ( helper . render_sitelinks_search_tag ) . to be_nil
end
end
end
context " for subfolder install " do
context " when on homepage " do
it " will return sitelinks search tag " do
Discourse . stubs ( :base_path ) . returns ( " /subfolder-base-path/ " )
helper . stubs ( :current_page? ) . returns ( false )
helper . stubs ( :current_page? ) . with ( Discourse . base_path ) . returns ( true )
expect ( helper . render_sitelinks_search_tag ) . to include ( '"@type":"SearchAction"' )
2023-08-24 10:07:45 -04:00
expect ( helper . render_sitelinks_search_tag ) . to include ( " subfolder-base-path " )
2022-09-23 03:05:53 -04:00
end
end
context " when not on homepage " do
it " will not return sitelinks search tag " do
Discourse . stubs ( :base_path ) . returns ( " /subfolder-base-path/ " )
helper . stubs ( :current_page? ) . returns ( true )
helper . stubs ( :current_page? ) . with ( " / " ) . returns ( false )
helper . stubs ( :current_page? ) . with ( Discourse . base_path ) . returns ( false )
expect ( helper . render_sitelinks_search_tag ) . to be_nil
end
end
end
end
2020-11-12 13:50:55 -05:00
describe " application_logo_url " do
context " when a dark color scheme is active " do
before do
dark_theme =
Theme . create (
name : " Dark " ,
2023-12-06 18:04:45 -05:00
user_id : Discourse :: SYSTEM_USER_ID ,
2020-11-12 13:50:55 -05:00
color_scheme_id : ColorScheme . find_by ( base_scheme_id : " Dark " ) . id ,
)
2021-06-15 02:57:17 -04:00
helper . request . env [ :resolved_theme_id ] = dark_theme . id
2020-11-12 13:50:55 -05:00
end
2022-07-27 12:14:14 -04:00
context " when on desktop " do
2020-11-12 13:50:55 -05:00
before { session [ :mobile_view ] = " 0 " }
2022-07-27 12:14:14 -04:00
2020-11-12 13:50:55 -05:00
context " when logo_dark is not set " do
it " will return site_logo_url instead " do
expect ( helper . application_logo_url ) . to eq ( SiteSetting . site_logo_url )
end
end
2022-07-27 12:14:14 -04:00
2020-11-12 13:50:55 -05:00
context " when logo_dark is set " do
before { SiteSetting . logo_dark = Fabricate ( :upload , url : " /images/logo-dark.png " ) }
2022-07-27 12:14:14 -04:00
2020-11-12 13:50:55 -05:00
it " will return site_logo_dark_url " do
expect ( helper . application_logo_url ) . to eq ( SiteSetting . site_logo_dark_url )
end
end
end
2022-07-27 12:14:14 -04:00
context " when on mobile " do
2020-11-12 13:50:55 -05:00
before { session [ :mobile_view ] = " 1 " }
2022-07-27 12:14:14 -04:00
2020-11-12 13:50:55 -05:00
context " when mobile_logo_dark is not set " do
it " will return site_mobile_logo_url instead " do
expect ( helper . application_logo_url ) . to eq ( SiteSetting . site_mobile_logo_url )
end
end
2022-07-27 12:14:14 -04:00
2020-11-12 13:50:55 -05:00
context " when mobile_logo_dark is set " do
before do
SiteSetting . mobile_logo_dark = Fabricate ( :upload , url : " /images/mobile-logo-dark.png " )
end
2022-07-27 12:14:14 -04:00
2020-11-12 13:50:55 -05:00
it " will return site_mobile_logo_dark_url " do
expect ( helper . application_logo_url ) . to eq ( SiteSetting . site_mobile_logo_dark_url )
end
end
end
end
end
2021-09-16 17:47:51 -04:00
describe " application_logo_dark_url " do
context " when dark theme is not present " do
context " when dark logo is not present " do
it " should return nothing " do
expect ( helper . application_logo_dark_url . present? ) . to eq ( false )
end
end
end
context " when dark theme is present " do
before do
2022-07-29 02:27:52 -04:00
_dark_theme =
Theme . create (
2021-09-16 17:47:51 -04:00
name : " Dark " ,
2023-12-06 18:04:45 -05:00
user_id : Discourse :: SYSTEM_USER_ID ,
2021-09-16 17:47:51 -04:00
color_scheme_id : ColorScheme . find_by ( base_scheme_id : " Dark " ) . id ,
)
end
context " when dark logo is not present " do
it " should return nothing " do
expect ( helper . application_logo_dark_url . present? ) . to eq ( false )
end
end
context " when dark logo is present " do
before { SiteSetting . logo_dark = Fabricate ( :upload , url : " /images/logo-dark.png " ) }
it " should return correct url " do
expect ( helper . application_logo_dark_url ) . to eq ( SiteSetting . site_logo_dark_url )
end
end
end
context " when dark theme is present and selected " do
before do
dark_theme =
Theme . create (
name : " Dark " ,
2023-12-06 18:04:45 -05:00
user_id : Discourse :: SYSTEM_USER_ID ,
2021-09-16 17:47:51 -04:00
color_scheme_id : ColorScheme . find_by ( base_scheme_id : " Dark " ) . id ,
)
helper . request . env [ :resolved_theme_id ] = dark_theme . id
SiteSetting . logo_dark = Fabricate ( :upload , url : " /images/logo-dark.png " )
end
it " should return nothing " do
expect ( helper . application_logo_url ) . to eq ( SiteSetting . site_logo_dark_url )
expect ( helper . application_logo_dark_url . present? ) . to eq ( false )
end
end
end
2022-07-27 12:14:14 -04:00
describe " # mobile_view? " do
context " when enable_mobile_theme is true " do
2017-07-07 02:09:14 -04:00
before { SiteSetting . enable_mobile_theme = true }
2013-12-18 14:47:22 -05:00
it " is true if mobile_view is '1' in the session " do
session [ :mobile_view ] = " 1 "
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to eq ( true )
2013-12-18 14:47:22 -05:00
end
it " is false if mobile_view is '0' in the session " do
session [ :mobile_view ] = " 0 "
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-18 14:47:22 -05:00
end
2022-07-27 12:14:14 -04:00
context " when mobile_view session is cleared " do
2019-02-22 13:43:40 -05:00
before { params [ :mobile_view ] = " auto " }
it " is false if user agent is not mobile " do
session [ :mobile_view ] = " 1 "
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 " ,
2023-01-09 06:18:21 -05:00
)
2019-02-22 13:43:40 -05:00
expect ( helper . mobile_view? ) . to be_falsey
end
it " is true for iPhone " do
session [ :mobile_view ] = " 0 "
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (iPhone; CPU iPhone OS 9_2_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13D15 Safari/601.1 " ,
2023-01-09 06:18:21 -05:00
)
2019-02-22 13:43:40 -05:00
expect ( helper . mobile_view? ) . to eq ( true )
end
end
2022-07-27 12:14:14 -04:00
context " when mobile_view is not set " do
2013-12-18 14:47:22 -05:00
it " is false if user agent is not mobile " do
2016-03-27 13:28:18 -04:00
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 " ,
2023-01-09 06:18:21 -05:00
)
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to be_falsey
2013-12-18 14:47:22 -05:00
end
it " is true for iPhone " do
2016-03-27 13:28:18 -04:00
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (iPhone; CPU iPhone OS 9_2_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13D15 Safari/601.1 " ,
2023-01-09 06:18:21 -05:00
)
2016-03-27 13:28:18 -04:00
expect ( helper . mobile_view? ) . to eq ( true )
end
it " is true for Android Samsung Galaxy " do
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-G925F Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/44.0.2403.133 Mobile Safari/537.36 " ,
2023-01-09 06:18:21 -05:00
)
2016-03-27 13:28:18 -04:00
expect ( helper . mobile_view? ) . to eq ( true )
end
it " is true for Android Google Nexus 5X " do
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (Linux; Android 6.0; Nexus 5X Build/MDB08I) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.43 Mobile Safari/537.36 " ,
2023-01-09 06:18:21 -05:00
)
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to eq ( true )
2013-12-18 14:47:22 -05:00
end
it " is false for iPad " do
2016-03-27 13:28:18 -04:00
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B14 3 Safari/601.1 " ,
2023-01-09 06:18:21 -05:00
)
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-18 14:47:22 -05:00
end
it " is false for Nexus 10 tablet " do
2016-03-27 13:28:18 -04:00
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (Linux; Android 5.1.1; Nexus 10 Build/LMY49G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.91 Safari/537.36 " ,
2023-01-09 06:18:21 -05:00
)
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to be_falsey
2013-12-18 14:47:22 -05:00
end
2016-03-27 13:28:18 -04:00
it " is false for Nexus 7 tablet " do
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MMB29Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.91 Safari/537.36 " ,
2023-01-09 06:18:21 -05:00
)
2016-03-27 13:28:18 -04:00
expect ( helper . mobile_view? ) . to be_falsey
2013-12-18 14:47:22 -05:00
end
end
end
2022-07-27 12:14:14 -04:00
context " when enable_mobile_theme is false " do
2017-07-07 02:09:14 -04:00
before { SiteSetting . enable_mobile_theme = false }
2013-12-18 14:47:22 -05:00
it " is false if mobile_view is '1' in the session " do
session [ :mobile_view ] = " 1 "
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-18 14:47:22 -05:00
end
it " is false if mobile_view is '0' in the session " do
session [ :mobile_view ] = " 0 "
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-18 14:47:22 -05:00
end
2022-07-27 12:14:14 -04:00
context " when mobile_view is not set " do
2013-12-18 14:47:22 -05:00
it " is false if user agent is not mobile " do
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36 " ,
2023-01-09 06:18:21 -05:00
)
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-18 14:47:22 -05:00
end
it " is false for iPhone " do
controller
. request
. stubs ( :user_agent )
. returns (
" Mozilla/5.0 (iPhone; U; ru; CPU iPhone OS 4_2_1 like Mac OS X; ru) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5 " ,
2023-01-09 06:18:21 -05:00
)
2014-12-31 09:55:03 -05:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-18 14:47:22 -05:00
end
end
end
end
2019-01-14 08:21:46 -05:00
describe " # html_classes " do
2023-11-09 17:47:59 -05:00
fab! ( :user )
2019-01-22 23:43:36 -05:00
2019-01-14 08:21:46 -05:00
it " includes 'rtl' when the I18n.locale is rtl " do
2016-02-06 14:49:39 -05:00
I18n . stubs ( :locale ) . returns ( :he )
2019-01-14 08:21:46 -05:00
expect ( helper . html_classes . split ( " " ) ) . to include ( " rtl " )
2016-02-06 14:49:39 -05:00
end
it " returns an empty string when the I18n.locale is not rtl " do
I18n . stubs ( :locale ) . returns ( :zh_TW )
2019-01-14 08:21:46 -05:00
expect ( helper . html_classes . split ( " " ) ) . not_to include ( " rtl " )
end
2019-01-25 10:06:06 -05:00
describe " text size " do
context " with a user option " do
before do
user . user_option . text_size = " larger "
user . user_option . save!
helper . request . env [ Auth :: DefaultCurrentUserProvider :: CURRENT_USER_KEY ] = user
end
it " ignores invalid text sizes " do
helper . request . cookies [ " text_size " ] = " invalid "
expect ( helper . html_classes . split ( " " ) ) . to include ( " text-size-larger " )
end
it " ignores missing text size " do
helper . request . cookies [ " text_size " ] = nil
expect ( helper . html_classes . split ( " " ) ) . to include ( " text-size-larger " )
end
2019-01-14 08:21:46 -05:00
2019-01-28 06:19:50 -05:00
it " ignores cookies with lower sequence " do
user . user_option . update! ( text_size_seq : 2 )
helper . request . cookies [ " text_size " ] = " normal|1 "
expect ( helper . html_classes . split ( " " ) ) . to include ( " text-size-larger " )
end
2019-01-25 10:06:06 -05:00
it " prioritises the cookie specified text size " do
2019-01-28 06:19:50 -05:00
user . user_option . update! ( text_size_seq : 2 )
helper . request . cookies [ " text_size " ] = " largest|4 "
expect ( helper . html_classes . split ( " " ) ) . to include ( " text-size-largest " )
2019-01-25 10:06:06 -05:00
end
it " includes the user specified text size " do
helper . request . env [ Auth :: DefaultCurrentUserProvider :: CURRENT_USER_KEY ] = user
expect ( helper . html_classes . split ( " " ) ) . to include ( " text-size-larger " )
end
end
it " falls back to the default text size for anon " do
expect ( helper . html_classes . split ( " " ) ) . to include ( " text-size-normal " )
SiteSetting . default_text_size = " largest "
expect ( helper . html_classes . split ( " " ) ) . to include ( " text-size-largest " )
end
2016-02-06 14:49:39 -05:00
end
2019-01-22 23:43:36 -05:00
it " includes 'anon' for anonymous users and excludes when logged in " do
expect ( helper . html_classes . split ( " " ) ) . to include ( " anon " )
helper . request . env [ Auth :: DefaultCurrentUserProvider :: CURRENT_USER_KEY ] = user
expect ( helper . html_classes . split ( " " ) ) . not_to include ( " anon " )
end
2016-02-06 14:49:39 -05:00
end
2013-12-18 14:47:22 -05:00
2017-02-22 16:24:05 -05:00
describe " gsub_emoji_to_unicode " do
it " converts all emoji to unicode " do
expect (
helper . gsub_emoji_to_unicode ( " Boat Talk: my :sailboat: boat: why is it so slow? :snail: " ) ,
) . to eq ( " Boat Talk: my ⛵ boat: why is it so slow? 🐌 " )
end
end
2018-09-17 04:31:46 -04:00
describe " preloaded_json " do
it " returns empty JSON if preloaded is empty " do
@preloaded = nil
expect ( helper . preloaded_json ) . to eq ( " {} " )
end
it " escapes and strips invalid unicode and strips in json body " do
@preloaded = { test : %{ ["< \x80 "] } }
expect ( helper . preloaded_json ) . to eq ( %{ { "test":"[ \\ " \\ u003c \ uFFFD \\ "]" } } )
end
end
2019-01-30 23:49:26 -05:00
2020-02-05 22:14:33 -05:00
describe " client_side_setup_data " do
context " when Rails.env.development? is true " do
before { Rails . env . stubs ( :development? ) . returns ( true ) }
it " returns the correct service worker url " do
expect ( helper . client_side_setup_data [ :service_worker_url ] ) . to eq ( " service-worker.js " )
end
it " returns the svg_icon_list in the setup data " do
expect ( helper . client_side_setup_data [ :svg_icon_list ] ) . not_to eq ( nil )
end
it " does not return debug_preloaded_app_data without the env var " do
expect ( helper . client_side_setup_data . key? ( :debug_preloaded_app_data ) ) . to eq ( false )
end
context " if the DEBUG_PRELOADED_APP_DATA env var is provided " do
before { ENV [ " DEBUG_PRELOADED_APP_DATA " ] = " true " }
it " returns that key as true " do
expect ( helper . client_side_setup_data [ :debug_preloaded_app_data ] ) . to eq ( true )
end
end
end
end
2019-01-30 23:49:26 -05:00
describe " crawlable_meta_data " do
2022-07-29 02:27:52 -04:00
it " Supports ASCII URLs with odd chars " do
result =
helper . crawlable_meta_data (
url : ( + " http://localhost/ión " ) . force_encoding ( " ASCII-8BIT " ) . freeze ,
)
expect ( result ) . to include ( " ión " )
end
2022-07-27 12:14:14 -04:00
context " with opengraph image " do
2019-01-31 14:44:20 -05:00
it " returns the correct image " do
2019-01-31 23:30:48 -05:00
SiteSetting . opengraph_image = Fabricate ( :upload , url : " /images/og-image.png " )
2019-01-31 14:44:20 -05:00
2019-01-31 23:30:48 -05:00
SiteSetting . twitter_summary_large_image = Fabricate ( :upload , url : " /images/twitter.png " )
2019-01-30 23:49:26 -05:00
2019-01-31 23:30:48 -05:00
SiteSetting . large_icon = Fabricate ( :upload , url : " /images/large_icon.png " )
2019-01-31 14:44:20 -05:00
2019-01-31 23:30:48 -05:00
SiteSetting . apple_touch_icon =
Fabricate ( :upload , url : " /images/default-apple-touch-icon.png " )
2019-01-31 14:44:20 -05:00
2019-01-31 23:30:48 -05:00
SiteSetting . logo = Fabricate ( :upload , url : " /images/d-logo-sketch.png " )
2019-01-30 23:49:26 -05:00
2019-01-31 23:30:48 -05:00
expect ( helper . crawlable_meta_data ( image : " some-image.png " ) ) . to include ( " some-image.png " )
expect ( helper . crawlable_meta_data ) . to include ( SiteSetting . site_opengraph_image_url )
SiteSetting . opengraph_image = nil
expect ( helper . crawlable_meta_data ) . to include (
SiteSetting . site_twitter_summary_large_image_url ,
)
SiteSetting . twitter_summary_large_image = nil
expect ( helper . crawlable_meta_data ) . to include ( SiteSetting . site_large_icon_url )
SiteSetting . large_icon = nil
2019-05-01 09:44:45 -04:00
SiteSetting . logo_small = nil
2019-01-31 23:30:48 -05:00
expect ( helper . crawlable_meta_data ) . to include ( SiteSetting . site_logo_url )
SiteSetting . logo = nil
2019-01-31 14:44:20 -05:00
2019-05-01 09:44:45 -04:00
expect ( helper . crawlable_meta_data ) . to include (
Upload . find ( SiteIconManager :: SKETCH_LOGO_ID ) . url ,
)
2019-01-30 23:49:26 -05:00
end
2022-06-02 19:02:57 -04:00
it " does not allow SVG images for twitter:image, falls back to site logo or nothing if site logo is SVG too " do
SiteSetting . logo = Fabricate ( :upload , url : " /images/d-logo-sketch.png " )
SiteSetting . opengraph_image = Fabricate ( :upload , url : " /images/og-image.png " )
expect ( helper . crawlable_meta_data ) . to include ( << ~ HTML )
< meta name = \ " twitter:image \" content= \" #{ SiteSetting . site_opengraph_image_url } \" />
HTML
SiteSetting . opengraph_image = Fabricate ( :upload , url : " /images/og-image.svg " )
expect ( helper . crawlable_meta_data ) . to include ( << ~ HTML )
< meta name = \ " twitter:image \" content= \" #{ SiteSetting . site_logo_url } \" />
HTML
SiteSetting . twitter_summary_large_image = Fabricate ( :upload , url : " /images/twitter.png " )
expect ( helper . crawlable_meta_data ) . to include ( << ~ HTML )
< meta name = \ " twitter:image \" content= \" #{ SiteSetting . site_twitter_summary_large_image_url } \" />
HTML
SiteSetting . twitter_summary_large_image = Fabricate ( :upload , url : " /images/twitter.svg " )
expect ( helper . crawlable_meta_data ) . to include ( << ~ HTML )
< meta name = \ " twitter:image \" content= \" #{ SiteSetting . site_logo_url } \" />
HTML
SiteSetting . logo = Fabricate ( :upload , url : " /images/d-logo-sketch.svg " )
expect ( helper . crawlable_meta_data ) . not_to include ( " twitter:image " )
end
2019-01-30 23:49:26 -05:00
end
2022-11-24 10:28:21 -05:00
context " with breadcrumbs " do
subject ( :metadata ) { helper . crawlable_meta_data ( breadcrumbs : breadcrumbs ) }
let ( :breadcrumbs ) do
[ { name : " section1 " , color : " ff0000 " } , { name : " section2 " , color : " 0000ff " } ]
end
let ( :tags ) { << ~ HTML . strip }
< meta property = " og:article:section " content = " section1 " / >
< meta property = " og:article:section:color " content = " ff0000 " / >
< meta property = " og:article:section " content = " section2 " / >
< meta property = " og:article:section:color " content = " 0000ff " / >
HTML
it " generates section and color tags " do
expect ( metadata ) . to include tags
end
end
context " with tags " do
subject ( :metadata ) { helper . crawlable_meta_data ( tags : tags ) }
let ( :tags ) { %w[ tag1 tag2 ] }
let ( :output_tags ) { << ~ HTML . strip }
< meta property = " og:article:tag " content = " tag1 " / >
< meta property = " og:article:tag " content = " tag2 " / >
HTML
it " generates tag tags " do
expect ( metadata ) . to include output_tags
end
end
2024-01-22 12:57:52 -05:00
context " with custom site name " do
before { SiteSetting . title = " Default Site Title " }
it " uses the provided site name in og:site_name " do
custom_site_name = " Custom Site Name "
result = helper . crawlable_meta_data ( site_name : custom_site_name )
expect ( result ) . to include (
" <meta property= \" og:site_name \" content= \" #{ custom_site_name } \" /> " ,
)
end
it " falls back to the default site title if no custom site name is provided " do
result = helper . crawlable_meta_data
expect ( result ) . to include (
" <meta property= \" og:site_name \" content= \" #{ SiteSetting . title } \" /> " ,
)
end
end
2019-01-30 23:49:26 -05:00
end
2020-08-03 22:57:10 -04:00
describe " discourse_color_scheme_stylesheets " do
2023-11-09 17:47:59 -05:00
fab! ( :user )
2020-08-06 09:45:37 -04:00
2020-08-03 22:57:10 -04:00
it " returns a stylesheet link tag by default " do
cs_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( cs_stylesheets ) . to include ( " stylesheets/color_definitions " )
end
it " returns two color scheme link tags when dark mode is enabled " do
2023-02-12 23:39:45 -05:00
SiteSetting . default_dark_mode_color_scheme_id = ColorScheme . where ( name : " Dark " ) . pick ( :id )
2020-08-03 22:57:10 -04:00
cs_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( cs_stylesheets ) . to include ( " (prefers-color-scheme: dark) " )
expect ( cs_stylesheets . scan ( " stylesheets/color_definitions " ) . size ) . to eq ( 2 )
end
2020-08-06 09:45:37 -04:00
2020-08-07 08:52:47 -04:00
it " handles a missing dark color scheme gracefully " do
scheme = ColorScheme . create! ( name : " pyramid " )
SiteSetting . default_dark_mode_color_scheme_id = scheme . id
scheme . destroy!
2020-08-06 09:45:37 -04:00
cs_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( cs_stylesheets ) . to include ( " stylesheets/color_definitions " )
expect ( cs_stylesheets ) . not_to include ( " (prefers-color-scheme: dark) " )
end
2022-07-27 12:14:14 -04:00
context " with custom light scheme " do
2020-08-28 10:36:52 -04:00
before do
@new_cs = Fabricate ( :color_scheme , name : " Flamboyant " )
user . user_option . color_scheme_id = @new_cs . id
user . user_option . save!
helper . request . env [ Auth :: DefaultCurrentUserProvider :: CURRENT_USER_KEY ] = user
end
it " returns color scheme from user option value " do
color_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( color_stylesheets ) . to include ( " color_definitions_flamboyant " )
end
it " returns color scheme from cookie value " do
cs = ColorScheme . where ( name : " Dark " ) . first
helper . request . cookies [ " color_scheme_id " ] = cs . id
color_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( color_stylesheets ) . to include ( " color_definitions_dark " )
expect ( color_stylesheets ) . not_to include ( " color_definitions_flamboyant " )
end
it " falls back to base scheme with invalid cookie value " do
helper . request . cookies [ " color_scheme_id " ] = - 50
color_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( color_stylesheets ) . not_to include ( " color_definitions_flamboyant " )
expect ( color_stylesheets ) . to include ( " color_definitions_base " )
end
end
2022-07-27 12:14:14 -04:00
context " with dark scheme with user option and/or cookies " do
2020-08-06 09:45:37 -04:00
before do
user . user_option . dark_scheme_id = - 1
user . user_option . save!
helper . request . env [ Auth :: DefaultCurrentUserProvider :: CURRENT_USER_KEY ] = user
2020-08-28 10:36:52 -04:00
@new_cs = Fabricate ( :color_scheme , name : " Custom Color Scheme " )
2020-08-06 09:45:37 -04:00
2023-02-12 23:39:45 -05:00
SiteSetting . default_dark_mode_color_scheme_id = ColorScheme . where ( name : " Dark " ) . pick ( :id )
2020-08-06 09:45:37 -04:00
end
it " returns no dark scheme stylesheet when user has disabled that option " do
color_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( color_stylesheets ) . to include ( " stylesheets/color_definitions " )
expect ( color_stylesheets ) . not_to include ( " (prefers-color-scheme: dark) " )
end
it " returns user-selected dark color scheme stylesheet " do
2020-08-28 10:36:52 -04:00
user . user_option . update! ( dark_scheme_id : @new_cs . id )
color_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( color_stylesheets ) . to include ( " (prefers-color-scheme: dark) " )
expect ( color_stylesheets ) . to include ( " custom-color-scheme " )
end
it " respects cookie value over user option for dark color scheme " do
helper . request . cookies [ " dark_scheme_id " ] = @new_cs . id
2020-08-06 09:45:37 -04:00
color_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( color_stylesheets ) . to include ( " (prefers-color-scheme: dark) " )
expect ( color_stylesheets ) . to include ( " custom-color-scheme " )
end
2020-08-28 10:36:52 -04:00
it " returns no dark scheme with invalid cookie value " do
helper . request . cookies [ " dark_scheme_id " ] = - 10
color_stylesheets = helper . discourse_color_scheme_stylesheets
expect ( color_stylesheets ) . not_to include ( " (prefers-color-scheme: dark) " )
end
2020-08-06 09:45:37 -04:00
end
2020-08-03 22:57:10 -04:00
end
2020-08-20 14:23:18 -04:00
describe " dark_color_scheme? " do
2020-10-14 10:18:02 -04:00
it " returns false for the base color scheme " do
2020-09-09 03:43:38 -04:00
expect ( helper . dark_color_scheme? ) . to eq ( false )
2020-08-20 14:23:18 -04:00
end
it " works correctly for a dark scheme " do
2020-10-14 10:18:02 -04:00
dark_theme =
Theme . create (
name : " Dark " ,
2023-12-06 18:04:45 -05:00
user_id : Discourse :: SYSTEM_USER_ID ,
2020-10-14 10:18:02 -04:00
color_scheme_id : ColorScheme . find_by ( base_scheme_id : " Dark " ) . id ,
)
2021-06-15 02:57:17 -04:00
helper . request . env [ :resolved_theme_id ] = dark_theme . id
2020-08-20 14:23:18 -04:00
expect ( helper . dark_color_scheme? ) . to eq ( true )
end
end
2021-02-10 10:12:09 -05:00
describe " html_lang " do
2023-11-09 17:47:59 -05:00
fab! ( :user )
2021-02-10 10:12:09 -05:00
before do
I18n . locale = :de
SiteSetting . default_locale = :fr
end
it " returns default locale if no request " do
helper . request = nil
expect ( helper . html_lang ) . to eq ( SiteSetting . default_locale )
end
it " returns current user locale if request " do
helper . request . env [ Auth :: DefaultCurrentUserProvider :: CURRENT_USER_KEY ] = user
expect ( helper . html_lang ) . to eq ( I18n . locale . to_s )
end
end
2022-10-26 00:18:05 -04:00
describe " # discourse_theme_color_meta_tags " do
before do
light = Fabricate ( :color_scheme )
light . color_scheme_colors << ColorSchemeColor . new ( name : " header_background " , hex : " abcdef " )
light . save!
helper . request . cookies [ " color_scheme_id " ] = light . id
dark = Fabricate ( :color_scheme )
dark . color_scheme_colors << ColorSchemeColor . new ( name : " header_background " , hex : " defabc " )
dark . save!
helper . request . cookies [ " dark_scheme_id " ] = dark . id
end
2022-11-07 02:06:26 -05:00
it " renders theme-color meta for the light scheme with media=(prefers-color-scheme: light) and another one for the dark scheme with media=(prefers-color-scheme: dark) " do
2022-10-26 00:18:05 -04:00
expect ( helper . discourse_theme_color_meta_tags ) . to eq ( << ~ HTML )
2022-11-07 02:06:26 -05:00
< meta name = " theme-color " media = " (prefers-color-scheme: light) " content = " # abcdef " >
2022-10-26 00:18:05 -04:00
< meta name = " theme-color " media = " (prefers-color-scheme: dark) " content = " # defabc " >
HTML
end
it " doesn't render theme-color meta tag for the dark scheme if none is set " do
SiteSetting . default_dark_mode_color_scheme_id = - 1
helper . request . cookies . delete ( " dark_scheme_id " )
expect ( helper . discourse_theme_color_meta_tags ) . to eq ( << ~ HTML )
< meta name = " theme-color " media = " all " content = " # abcdef " >
HTML
end
end
2013-08-27 14:57:42 -04:00
end