Skip to content

Commit

Permalink
change to use other key for input value / add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Soichiro725 committed Nov 6, 2019
1 parent e5cb577 commit a31f707
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/wovnrb/services/html_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def strip_form(node, marker)
return if original_text.nil?
return if original_text.include?(HtmlReplaceMarker::KEY_PREFIX)

node.set_attribute('value', marker.add_comment_value(original_text))
node.set_attribute('value', marker.add_value(original_text))
end
end

Expand Down
8 changes: 7 additions & 1 deletion lib/wovnrb/services/html_replace_marker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ def add_comment_value(value)
key
end

def add_value(value)
key = generate_key
@mapped_values << [key, value]

key
end

def revert(marked_html)
i = @mapped_values.size
marked_html = CGI.unescapeHTML(marked_html)
while i > 0
i -= 1
key, value = @mapped_values[i]
Expand Down
2 changes: 1 addition & 1 deletion test/lib/services/html_converter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_build_api_compatible_html_hidden_input_should_not_be_sent
converter = prepare_html_converter(html, ignore_class: [])
converted_html, = converter.build_api_compatible_html

expected_convert_html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=query&amp;langCodeAliases={}&amp;langParamName=wovn&amp;version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"http://my-site.com/?wovn=fr\"><link rel=\"alternate\" hreflang=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><input id=\"user-id\" type=\"hidden\" value=\"<!-- __wovn-backend-ignored-key-0 -->\"><input id=\"name\" type=\"text\" value=\"wovn.io\"></body></html>"
expected_convert_html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=query&amp;langCodeAliases={}&amp;langParamName=wovn&amp;version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"http://my-site.com/?wovn=fr\"><link rel=\"alternate\" hreflang=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><input id=\"user-id\" type=\"hidden\" value=\"__wovn-backend-ignored-key-0\"><input id=\"name\" type=\"text\" value=\"wovn.io\"></body></html>"
assert_equal(expected_convert_html, converted_html)
end

Expand Down
72 changes: 65 additions & 7 deletions test/lib/services/html_replace_marker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def test_add_comment_value
end

def test_add_comment_value_multiple_times
maker = HtmlReplaceMarker.new
assert_equal('<!-- __wovn-backend-ignored-key-0 -->', maker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-1 -->', maker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-2 -->', maker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-3 -->', maker.add_comment_value('hello'))
marker = HtmlReplaceMarker.new
assert_equal('<!-- __wovn-backend-ignored-key-0 -->', marker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-1 -->', marker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-2 -->', marker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-3 -->', marker.add_comment_value('hello'))
end

def test_add_same_comment_value_multiple_times
Expand All @@ -23,6 +23,27 @@ def test_add_same_comment_value_multiple_times
end
end

def test_add_same_value_multiple_times
marker = HtmlReplaceMarker.new

25.times do |i|
assert_equal("__wovn-backend-ignored-key-#{i}", marker.add_value('hello'))
end
end

def test_mixed_add_comment_value_and_add_value
marker = HtmlReplaceMarker.new

assert_equal('<!-- __wovn-backend-ignored-key-0 -->', marker.add_comment_value('hello'))
assert_equal('__wovn-backend-ignored-key-1', marker.add_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-2 -->', marker.add_comment_value('hello'))
assert_equal('__wovn-backend-ignored-key-3', marker.add_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-4 -->', marker.add_comment_value('hello'))
assert_equal('__wovn-backend-ignored-key-5', marker.add_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-6 -->', marker.add_comment_value('hello'))
assert_equal('__wovn-backend-ignored-key-7', marker.add_value('hello'))
end

def test_revert
marker = HtmlReplaceMarker.new
original_html = '<html><body>hello<a> replacement </a>world </body></html>'
Expand All @@ -35,7 +56,7 @@ def test_revert
def test_revert_input_value
marker = HtmlReplaceMarker.new
original_html = '<html><body><input type="hidden" value="please-revert"></body></html>'
key = marker.add_comment_value('please-revert')
key = marker.add_value('please-revert')
new_html = original_html.sub('please-revert', key)
assert_equal("<html><body><input type=\"hidden\" value=\"#{key}\"></body></html>", new_html)
assert_equal(original_html, marker.revert(new_html))
Expand All @@ -44,12 +65,30 @@ def test_revert_input_value
def test_revert_input_empty_value
marker = HtmlReplaceMarker.new
original_html = '<html><body><input type="hidden" value=""></body></html>'
key = marker.add_comment_value('')
key = marker.add_value('')
new_html = original_html.sub('value=""', "value=\"#{key}\"")
assert_equal("<html><body><input type=\"hidden\" value=\"#{key}\"></body></html>", new_html)
assert_equal(original_html, marker.revert(new_html))
end

def test_revert_multiple_input
marker = HtmlReplaceMarker.new
original_html = [
'<html><body>',
'<input type="hidden" value="please_revert1"></body></html>',
'<input type="hidden" value="please_revert2"></body></html>',
'<input type="hidden" value=""></body></html>'
].join
new_html = [
'<html><body>',
"<input type=\"hidden\" value=\"#{marker.add_value('please_revert1')}\"></body></html>",
"<input type=\"hidden\" value=\"#{marker.add_value('please_revert2')}\"></body></html>",
"<input type=\"hidden\" value=\"#{marker.add_value('')}\">",
'</body></html>'
].join
assert_equal(original_html, marker.revert(new_html))
end

def test_revert_multiple_values
marker = HtmlReplaceMarker.new
original_html = '<html><body>hello<a> replacement </a>world </body></html>'
Expand Down Expand Up @@ -89,5 +128,24 @@ def test_revert_same_value
new_html = "<html><body>#{key1}<a>#{key2}</a>#{key3}</body></html>"
assert_equal(original_html, marker.revert(new_html))
end

def test_revert_mixed_values
marker = HtmlReplaceMarker.new
original_html = [
'<html><body>',
'<span>hello</span>',
'<input type="hidden" value="please_revert">',
'</body></html>'
].join
key1 = marker.add_comment_value('hello')
key2 = marker.add_value('please_revert')
new_html = [
'<html><body>',
"<span>#{key1}</span>",
"<input type=\"hidden\" value=\"#{key2}\">",
'</body></html>'
].join
assert_equal(original_html, marker.revert(new_html))
end
end
end
11 changes: 6 additions & 5 deletions test/lib/wovnrb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_switch_lang

expected_body = [
'<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
"<script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script>",
"<script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script>",
'<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
'<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
'<body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>',
Expand All @@ -29,12 +29,13 @@ def test_switch_lang

def test_switch_lang_with_input_tags
body = [
'<html lang="ja"><body>',
'<html lang="ja">',
'<body>',
'<input type="hidden" value="test1">',
'<input type="hidden" value="test2">',
'<input type="hidden" value="">',
'<input value="test3">',
'<p>Hello</p></body></html>'
'</body></html>'
].join

expected_body = [
Expand Down Expand Up @@ -65,7 +66,7 @@ def test_switch_lang_splitted_body
bodies = ['<html><body><h1>Mr. Belvedere Fan Club</h1>',
'<div><p>Hello</p></div>',
'</body></html>'].join
expected_bodies = ["<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=ja&defaultLang=en&urlPattern=subdomain&langCodeAliases={}&version=WOVN.rb_#{Wovnrb::VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>"].join
expected_bodies = ["<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=subdomain&amp;langCodeAliases={}&amp;version=WOVN.rb_#{Wovnrb::VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>"].join

assert_switch_lang('en', 'ja', bodies, expected_bodies, true)
end
Expand All @@ -82,7 +83,7 @@ def test_switch_lang_missing_values
body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
<div><p>Hello</p></div>
</body></html>"
expected_body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script></head><body><h1>Mr. Belvedere Fan Club</h1>
expected_body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script></head><body><h1>Mr. Belvedere Fan Club</h1>
<div><p>Hello</p></div>
</body></html>
"
Expand Down

0 comments on commit a31f707

Please sign in to comment.