require 'test_helper'
module Wovnrb
class HtmlConverterTest < WovnMiniTest
test 'build API compatible html' do
converter = prepare_html_converter('
hello', supported_langs: %w[en vi])
converted_html, = converter.build_api_compatible_html
expected_html = "hello"
assert_equal(expected_html, converted_html)
end
test 'build API compatible html - with custom lang param name' do
settings = {
supported_langs: %w[en vi],
url_lang_pattern: 'query',
lang_param_name: 'lang'
}
converter = prepare_html_converter('hello', settings)
converted_html, = converter.build_api_compatible_html
expected_html = "hello"
assert_equal(expected_html, converted_html)
end
test 'build API compatible html - excessively large HTML' do
long_string = 'a' * 60_000
converter = prepare_html_converter("#{long_string}
", supported_langs: %w[en vi])
converted_html, = converter.build_api_compatible_html
expected_html = "#{long_string}
"
assert_equal(expected_html, converted_html)
end
test 'build API compatible html - ignored content should not be sent' do
html = [
'',
'Hello WOVN
',
'
Hello WOVN
',
'
should be ignored
',
'Have a nice day!',
''
].join
converter = prepare_html_converter(html, ignore_class: ['ignore-me'])
converted_html, = converter.build_api_compatible_html
expected_convert_html = "Hello
Hello
Have a nice day!"
assert_equal(expected_convert_html, converted_html)
end
test 'build API compatible html - do not send html form' do
html = [
'',
'',
''
].join
converter = prepare_html_converter(html, ignore_class: [])
converted_html, = converter.build_api_compatible_html
expected_convert_html = ""
assert_equal(expected_convert_html, converted_html)
end
test 'build API compatible html - do not send hidden form input' do
html = [
'',
'',
'',
'',
'',
''
].join
converter = prepare_html_converter(html, ignore_class: [])
converted_html, = converter.build_api_compatible_html
expected_convert_html = [
'',
"",
'',
'',
'',
'',
''
].join
assert_equal(expected_convert_html, converted_html)
end
test 'Transform HTML' do
converter = prepare_html_converter('hello', supported_langs: %w[en vi])
translated_html = converter.build
expected_html = "hello"
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - with empty supported langs' do
converter = prepare_html_converter('hello', supported_langs: [])
translated_html = converter.build
expected_html = "hello"
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - with head tag' do
converter = prepare_html_converter('TITLEhello', supported_langs: %w[en vi])
translated_html = converter.build
expected_html = "TITLEhello"
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - without body' do
converter = prepare_html_converter('helloworld', supported_langs: [])
translated_html = converter.build
expected_html = "helloworld"
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - default lang - with query pattern and supported langs defined' do
dom = get_dom('helloworld')
settings = {
'default_lang' => 'en',
'supported_langs' => %w[en ja vi],
'url_pattern' => 'query'
}
store, headers = store_headers_factory(settings)
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(dom, store, headers, url_lang_switcher)
translated_html = converter.build
expected_html = "helloworld"
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - canonical tag - target lang - should translate' do
dom = get_dom('')
settings = {
'default_lang' => 'en',
'supported_langs' => %w[en ja vi],
'url_pattern' => 'path',
'translate_canonical_tag' => true
}
store, headers = store_headers_factory(settings, 'http://my-site.com/vi/')
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(dom, store, headers, url_lang_switcher)
translated_html = converter.build
expected_html = ""
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - canonical tag - default lang - path pattern - no need to translate' do
dom = get_dom('')
settings = {
'default_lang' => 'en',
'supported_langs' => %w[en ja vi],
'url_pattern' => 'path',
'translate_canonical_tag' => true
}
store, headers = store_headers_factory(settings, 'http://my-site.com/')
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(dom, store, headers, url_lang_switcher)
translated_html = converter.build
expected_html = ""
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - canonical tag - default lang - query pattern - no need to translate' do
dom = get_dom('')
settings = {
'default_lang' => 'en',
'supported_langs' => %w[en ja vi],
'url_pattern' => 'query',
'translate_canonical_tag' => true
}
store, headers = store_headers_factory(settings, 'http://my-site.com/')
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(dom, store, headers, url_lang_switcher)
translated_html = converter.build
expected_html = ""
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - canonical tag - default lang - has default lang alias - should use alias' do
dom = get_dom('')
settings = {
'default_lang' => 'en',
'supported_langs' => %w[en ja vi],
'url_pattern' => 'query',
'translate_canonical_tag' => true,
'custom_lang_aliases' => { 'en' => 'english' }
}
store, headers = store_headers_factory(settings, 'http://my-site.com/')
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(dom, store, headers, url_lang_switcher)
translated_html = converter.build
expected_html = ""
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - canonical tag - disabled - do not translate' do
dom = get_dom('')
settings = {
'default_lang' => 'en',
'supported_langs' => %w[en ja vi],
'url_pattern' => 'path',
'translate_canonical_tag' => false
}
store, headers = store_headers_factory(settings, 'http://my-site.com/vi/')
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(dom, store, headers, url_lang_switcher)
translated_html = converter.build
expected_html = ""
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - default lang - with path pattern and supported langs defined' do
dom = get_dom('helloworld')
settings = {
'default_lang' => 'en',
'supported_langs' => %w[en ja vi],
'url_pattern' => 'path'
}
store, headers = store_headers_factory(settings)
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(dom, store, headers, url_lang_switcher)
translated_html = converter.build
expected_html = "helloworld"
assert_equal(expected_html, translated_html)
end
test 'Transform HTML - custom domain langs' do
dom = get_dom('hello')
settings = {
'default_lang' => 'en',
'supported_langs' => %w[en fr],
'custom_domain_langs' => { 'en' => { 'url' => 'my-site.com' }, 'fr' => { 'url' => 'french.com/fr' } },
'url_pattern' => 'custom_domain'
}
store, headers = store_headers_factory(settings)
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(dom, store, headers, url_lang_switcher)
translated_html = converter.build
expected_html = "hello"
assert_equal(expected_html, translated_html)
end
test 'replace_snippet' do
converter = prepare_html_converter('
')
translated_html = converter.build
dom = Helpers::NokogumboHelper.parse_html(translated_html)
scripts = dom.css('script')
assert_equal(2, scripts.length)
expected_wovn_script = ""
assert_equal(expected_wovn_script, scripts.first.to_html)
end
test 'replace_hreflangs' do
converter = prepare_html_converter('')
translated_html = converter.build
dom = Helpers::NokogumboHelper.parse_html(translated_html)
href_langs = dom.css('link[rel="alternate"]')
assert_equal(4, href_langs.length)
expected_href_langs = {
'en' =>
{
'href' => 'http://my-site.com/'
},
'fr' =>
{
'href' => 'http://my-site.com/?wovn=fr'
},
'ja' =>
{
'href' => 'http://my-site.com/?wovn=ja'
},
'vi' =>
{
'href' => 'http://my-site.com/?wovn=vi'
}
}
href_langs.each do |node|
assertions = expected_href_langs[node['hreflang']]
assert_not_nil(assertions)
assert_equal(assertions['href'], node['href'])
end
end
test 'inject_lang_html_tag - with no lang in HTML tag - should inject' do
settings = default_store_settings
store = Wovnrb::Store.instance
store.update_settings(settings)
headers = Wovnrb::Headers.new(
Wovnrb.get_env('url' => 'http://my-site.com/'),
Wovnrb.get_settings(settings),
UrlLanguageSwitcher.new(store)
)
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(get_dom('hello'), store, headers, url_lang_switcher)
translated_html = converter.build
dom = Helpers::NokogumboHelper.parse_html(translated_html)
assert_equal('en', dom.at_css('html')['lang'])
end
test 'inject_lang_html_tag - with lang in HTML tag - do not override' do
settings = default_store_settings
store = Wovnrb::Store.instance
store.update_settings(settings)
headers = Wovnrb::Headers.new(
Wovnrb.get_env('url' => 'http://my-site.com/'),
Wovnrb.get_settings(settings),
UrlLanguageSwitcher.new(store)
)
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(get_dom('hello'), store, headers, url_lang_switcher)
translated_html = converter.build
dom = Helpers::NokogumboHelper.parse_html(translated_html)
assert_equal('th', dom.at_css('html')['lang'])
end
test 'translate_canonical_tag' do
settings = default_store_settings
store = Wovnrb::Store.instance
store.update_settings(settings)
headers = Wovnrb::Headers.new(
Wovnrb.get_env('url' => 'http://my-site.com/?wovn=fr'),
store.settings,
UrlLanguageSwitcher.new(store)
)
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(get_dom('hello'), store, headers, url_lang_switcher)
translated_html = converter.build
dom = Helpers::NokogumboHelper.parse_html(translated_html)
canonical_tag = dom.at_css('link[rel="canonical"]')
assert_not_nil(canonical_tag)
assert_equal('http://my-site.com?wovn=fr', canonical_tag['href'])
end
test 'translate_canonical_tag - path pattern' do
settings = default_store_settings
settings['url_pattern'] = 'path'
store = Wovnrb::Store.instance
store.update_settings(settings)
headers = Wovnrb::Headers.new(
Wovnrb.get_env('url' => 'http://my-site.com/fr/'),
store.settings,
UrlLanguageSwitcher.new(store)
)
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(get_dom('hello'), store, headers, url_lang_switcher)
translated_html = converter.build
dom = Helpers::NokogumboHelper.parse_html(translated_html)
canonical_tag = dom.at_css('link[rel="canonical"]')
assert_not_nil(canonical_tag)
assert_equal('http://my-site.com/fr/', canonical_tag['href'])
end
test 'translate_canonical_tag - canonical tag is already translated' do
# NOTE: this behavior is not correct, but it is the same as html-swapper
settings = default_store_settings
settings['url_pattern'] = 'path'
store = Wovnrb::Store.instance
store.update_settings(settings)
headers = Wovnrb::Headers.new(
Wovnrb.get_env('url' => 'http://my-site.com/fr/'),
store.settings,
UrlLanguageSwitcher.new(store)
)
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
converter = HtmlConverter.new(get_dom('hello'), store, headers, url_lang_switcher)
translated_html = converter.build
dom = Helpers::NokogumboHelper.parse_html(translated_html)
canonical_tag = dom.at_css('link[rel="canonical"]')
assert_not_nil(canonical_tag)
assert_equal('http://my-site.com/fr/fr/', canonical_tag['href'])
end
test 'build_api_compatible_html - backend-wovn-ignore comment - should be removed' do
store = Wovnrb::Store.instance
url_lang_switcher = Wovnrb::UrlLanguageSwitcher.new(store)
headers = Wovnrb::Headers.new(
Wovnrb.get_env('url' => 'http://my-site.com/'),
store.settings,
url_lang_switcher
)
html = 'helloignored world'
dom = get_dom(html)
sut = HtmlConverter.new(dom, store, headers, url_lang_switcher)
translated_html, marker = sut.build_api_compatible_html
assert_equal(1, marker.keys.count)
assert(translated_html.include?("hello#{marker.keys[0]} world