2015-10-11 17:41:23 +08:00
require 'rails_helper'
2013-08-28 02:57:42 +08:00
describe ApplicationHelper do
2017-10-06 13:20:01 +08:00
describe " preload_script " do
it " provides brotli links to brotli cdn " do
set_cdn_url " https://awesome.com "
helper . request . env [ " HTTP_ACCEPT_ENCODING " ] = 'br'
link = helper . preload_script ( 'application' )
expect ( link ) . to eq ( " <link rel='preload' href='https://awesome.com/brotli_asset/application.js' as='script'/> \n <script src='https://awesome.com/brotli_asset/application.js'></script> " )
end
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-22 10:31:13 +08:00
after do
ActionController :: Base . config . relative_url_root = nil
end
it " deals correctly with subfolder " do
ActionController :: Base . config . relative_url_root = " /community "
expect ( helper . preload_script ( " application " ) ) . to include ( 'https://s3cdn.com/assets/application.js' )
end
2017-10-06 13:20:01 +08:00
it " returns magic brotli mangling for brotli requests " do
helper . request . env [ " HTTP_ACCEPT_ENCODING " ] = 'br'
link = helper . preload_script ( 'application' )
expect ( link ) . to eq ( " <link rel='preload' href='https://s3cdn.com/assets/application.br.js' as='script'/> \n <script src='https://s3cdn.com/assets/application.br.js'></script> " )
end
it " gives s3 cdn if asset host is not set " do
link = helper . preload_script ( 'application' )
expect ( link ) . to eq ( " <link rel='preload' href='https://s3cdn.com/assets/application.js' as='script'/> \n <script src='https://s3cdn.com/assets/application.js'></script> " )
end
it " gives s3 cdn even if asset host is set " do
set_cdn_url " https://awesome.com "
link = helper . preload_script ( 'application' )
expect ( link ) . to eq ( " <link rel='preload' href='https://s3cdn.com/assets/application.js' as='script'/> \n <script src='https://s3cdn.com/assets/application.js'></script> " )
end
end
end
2013-11-05 10:40:39 +08:00
describe " escape_unicode " do
it " encodes tags " do
2014-12-31 22:55:03 +08:00
expect ( helper . escape_unicode ( " <tag> " ) ) . to eq ( " \ u003ctag> " )
2013-11-05 10:40:39 +08:00
end
it " survives junk text " do
2014-12-31 22:55:03 +08:00
expect ( helper . escape_unicode ( " hello \xc3 \x28 world " ) ) . to match ( / hello.*world / )
2013-11-05 10:40:39 +08:00
end
end
2013-12-19 03:47:22 +08:00
describe " mobile_view? " do
context " enable_mobile_theme is true " do
before do
2017-07-07 14:09:14 +08:00
SiteSetting . enable_mobile_theme = true
2013-12-19 03:47:22 +08:00
end
it " is true if mobile_view is '1' in the session " do
session [ :mobile_view ] = '1'
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to eq ( true )
2013-12-19 03:47:22 +08:00
end
it " is false if mobile_view is '0' in the session " do
session [ :mobile_view ] = '0'
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-19 03:47:22 +08:00
end
2019-02-23 02:43:40 +08:00
context " mobile_view session is cleared " do
before do
params [ :mobile_view ] = 'auto'
end
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' )
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' )
expect ( helper . mobile_view? ) . to eq ( true )
end
end
2013-12-19 03:47:22 +08:00
context " mobile_view is not set " do
it " is false if user agent is not mobile " do
2016-03-28 01:28:18 +08: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' )
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to be_falsey
2013-12-19 03:47:22 +08:00
end
it " is true for iPhone " do
2016-03-28 01:28:18 +08: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' )
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' )
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' )
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to eq ( true )
2013-12-19 03:47:22 +08:00
end
it " is false for iPad " do
2016-03-28 01:28:18 +08: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 " )
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-19 03:47:22 +08:00
end
it " is false for Nexus 10 tablet " do
2016-03-28 01:28:18 +08: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 " )
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to be_falsey
2013-12-19 03:47:22 +08:00
end
2016-03-28 01:28:18 +08: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 " )
expect ( helper . mobile_view? ) . to be_falsey
2013-12-19 03:47:22 +08:00
end
end
end
context " enable_mobile_theme is false " do
before do
2017-07-07 14:09:14 +08:00
SiteSetting . enable_mobile_theme = false
2013-12-19 03:47:22 +08:00
end
it " is false if mobile_view is '1' in the session " do
session [ :mobile_view ] = '1'
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-19 03:47:22 +08:00
end
it " is false if mobile_view is '0' in the session " do
session [ :mobile_view ] = '0'
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-19 03:47:22 +08:00
end
context " mobile_view is not set " do
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' )
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-19 03:47:22 +08: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' )
2014-12-31 22:55:03 +08:00
expect ( helper . mobile_view? ) . to eq ( false )
2013-12-19 03:47:22 +08:00
end
end
end
end
2019-01-14 21:21:46 +08:00
describe '#html_classes' do
2019-01-23 12:43:36 +08:00
let ( :user ) { Fabricate ( :user ) }
2019-01-14 21:21:46 +08:00
it " includes 'rtl' when the I18n.locale is rtl " do
2016-02-07 03:49:39 +08:00
I18n . stubs ( :locale ) . returns ( :he )
2019-01-14 21:21:46 +08:00
expect ( helper . html_classes . split ( " " ) ) . to include ( 'rtl' )
2016-02-07 03:49:39 +08:00
end
it 'returns an empty string when the I18n.locale is not rtl' do
I18n . stubs ( :locale ) . returns ( :zh_TW )
2019-01-14 21:21:46 +08:00
expect ( helper . html_classes . split ( " " ) ) . not_to include ( 'rtl' )
end
2019-01-25 23:06:06 +08: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 21:21:46 +08:00
2019-01-28 19:19:50 +08: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 23:06:06 +08:00
it 'prioritises the cookie specified text size' do
2019-01-28 19:19:50 +08: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 23:06:06 +08: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-07 03:49:39 +08:00
end
2019-01-23 12:43:36 +08: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-07 03:49:39 +08:00
end
2013-12-19 03:47:22 +08:00
2017-02-23 05:24:05 +08: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 16:31:46 +08: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-31 12:49:26 +08:00
describe 'crawlable_meta_data' do
context " opengraph image " do
2019-02-01 03:44:20 +08:00
it 'returns the correct image' do
2019-02-01 12:30:48 +08:00
SiteSetting . opengraph_image = Fabricate ( :upload ,
url : '/images/og-image.png'
)
2019-02-01 03:44:20 +08:00
2019-02-01 12:30:48 +08:00
SiteSetting . twitter_summary_large_image = Fabricate ( :upload ,
url : '/images/twitter.png'
)
2019-01-31 12:49:26 +08:00
2019-02-01 12:30:48 +08:00
SiteSetting . large_icon = Fabricate ( :upload ,
url : '/images/large_icon.png'
)
2019-02-01 03:44:20 +08:00
2019-02-01 12:30:48 +08:00
SiteSetting . apple_touch_icon = Fabricate ( :upload ,
url : '/images/default-apple-touch-icon.png'
)
2019-02-01 03:44:20 +08:00
2019-02-01 12:30:48 +08:00
SiteSetting . logo = Fabricate ( :upload , url : '/images/d-logo-sketch.png' )
2019-01-31 12:49:26 +08:00
2019-02-01 12:30:48 +08:00
expect (
helper . crawlable_meta_data ( image : " some-image.png " )
) . to include ( " some-image.png " )
expect ( helper . crawlable_meta_data ) . to include (
2019-03-13 17:39:07 +08:00
SiteSetting . site_opengraph_image_url
2019-02-01 12:30:48 +08:00
)
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
expect ( helper . crawlable_meta_data ) . to include (
SiteSetting . site_apple_touch_icon_url
)
SiteSetting . apple_touch_icon = nil
SiteSetting . apple_touch_icon_url = nil
expect ( helper . crawlable_meta_data ) . to include ( SiteSetting . site_logo_url )
SiteSetting . logo = nil
SiteSetting . logo_url = nil
2019-02-01 03:44:20 +08:00
expect ( helper . crawlable_meta_data ) . to_not include ( " /images " )
2019-01-31 12:49:26 +08:00
end
end
end
2013-08-28 02:57:42 +08:00
end