DEV: Apply syntax_tree formatting to spec/*

This commit is contained in:
David Taylor
2023-01-09 11:18:21 +00:00
parent 0cf6421716
commit cb932d6ee1
907 changed files with 58693 additions and 45909 deletions

View File

@ -1,11 +1,9 @@
# frozen_string_literal: true
require_relative '../../../script/import_scripts/base'
require_relative "../../../script/import_scripts/base"
RSpec.describe ImportScripts::Base do
before do
STDOUT.stubs(:write)
end
before { STDOUT.stubs(:write) }
class MockSpecImporter < ImportScripts::Base
def initialize(data)
@ -21,9 +19,7 @@ RSpec.describe ImportScripts::Base do
def import_users
users = @import_data[:users]
create_users(users) do |row|
{ email: row[:email], id: row[:id] }
end
create_users(users) { |row| { email: row[:email], id: row[:id] } }
end
def import_posts
@ -36,9 +32,7 @@ RSpec.describe ImportScripts::Base do
def import_bookmarks
bookmarks = @import_data[:bookmarks]
create_bookmarks(bookmarks) do |row|
{ post_id: row[:post_id], user_id: row[:user_id] }
end
create_bookmarks(bookmarks) { |row| { post_id: row[:post_id], user_id: row[:user_id] } }
end
end
@ -51,7 +45,7 @@ RSpec.describe ImportScripts::Base do
MockSpecImporter.new(import_data).perform
expect(Bookmark.where(bookmarkable_type: "Post").count).to eq(5)
expect(Post.count).to eq(5)
expect(User.where('id > 0').count).to eq(1)
expect(User.where("id > 0").count).to eq(1)
expect(SiteSetting.purge_unactivated_users_grace_period_days).to eq(60)
end

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
require Rails.root.join('script/import_scripts/phpbb3/support/bbcode/xml_to_markdown')
require Rails.root.join("script/import_scripts/phpbb3/support/bbcode/xml_to_markdown")
RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
def convert(xml, opts = {})
@ -8,19 +8,20 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
end
it "converts unformatted text" do
xml = '<t>unformatted text</t>'
expect(convert(xml)).to eq('unformatted text')
xml = "<t>unformatted text</t>"
expect(convert(xml)).to eq("unformatted text")
end
it "converts nested formatting" do
xml = '<r><I><s>[i]</s>this is italic<B><s>[b]</s> and bold<e>[/b]</e></B> text<e>[/i]</e></I></r>'
expect(convert(xml)).to eq('_this is italic **and bold** text_')
xml =
"<r><I><s>[i]</s>this is italic<B><s>[b]</s> and bold<e>[/b]</e></B> text<e>[/i]</e></I></r>"
expect(convert(xml)).to eq("_this is italic **and bold** text_")
end
context "with bold text" do
it "converts bold text" do
xml = '<r><B><s>[b]</s>this is bold text<e>[/b]</e></B></r>'
expect(convert(xml)).to eq('**this is bold text**')
xml = "<r><B><s>[b]</s>this is bold text<e>[/b]</e></B></r>"
expect(convert(xml)).to eq("**this is bold text**")
end
it "converts multi-line bold text" do
@ -46,15 +47,15 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
end
it "ignores duplicate bold text" do
xml = '<r><B><s>[b]</s><B><s>[b]</s>this is bold text<e>[/b]</e></B><e>[/b]</e></B></r>'
expect(convert(xml)).to eq('**this is bold text**')
xml = "<r><B><s>[b]</s><B><s>[b]</s>this is bold text<e>[/b]</e></B><e>[/b]</e></B></r>"
expect(convert(xml)).to eq("**this is bold text**")
end
end
context "with italic text" do
it "converts italic text" do
xml = '<r><I><s>[i]</s>this is italic text<e>[/i]</e></I></r>'
expect(convert(xml)).to eq('_this is italic text_')
xml = "<r><I><s>[i]</s>this is italic text<e>[/i]</e></I></r>"
expect(convert(xml)).to eq("_this is italic text_")
end
it "converts multi-line italic text" do
@ -80,15 +81,15 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
end
it "ignores duplicate italic text" do
xml = '<r><I><s>[i]</s><I><s>[i]</s>this is italic text<e>[/i]</e></I><e>[/i]</e></I></r>'
expect(convert(xml)).to eq('_this is italic text_')
xml = "<r><I><s>[i]</s><I><s>[i]</s>this is italic text<e>[/i]</e></I><e>[/i]</e></I></r>"
expect(convert(xml)).to eq("_this is italic text_")
end
end
context "with underlined text" do
it "converts underlined text" do
xml = '<r><U><s>[u]</s>this is underlined text<e>[/u]</e></U></r>'
expect(convert(xml)).to eq('[u]this is underlined text[/u]')
xml = "<r><U><s>[u]</s>this is underlined text<e>[/u]</e></U></r>"
expect(convert(xml)).to eq("[u]this is underlined text[/u]")
end
it "converts multi-line underlined text" do
@ -114,8 +115,8 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
end
it "ignores duplicate underlined text" do
xml = '<r><U><s>[u]</s><U><s>[u]</s>this is underlined text<e>[/u]</e></U><e>[/u]</e></U></r>'
expect(convert(xml)).to eq('[u]this is underlined text[/u]')
xml = "<r><U><s>[u]</s><U><s>[u]</s>this is underlined text<e>[/u]</e></U><e>[/u]</e></U></r>"
expect(convert(xml)).to eq("[u]this is underlined text[/u]")
end
end
@ -124,14 +125,14 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
let(:opts) { { allow_inline_code: true } }
it "converts single line code blocks" do
xml = '<r><CODE><s>[code]</s>one line of code<e>[/code]</e></CODE></r>'
expect(convert(xml, opts)).to eq('`one line of code`')
xml = "<r><CODE><s>[code]</s>one line of code<e>[/code]</e></CODE></r>"
expect(convert(xml, opts)).to eq("`one line of code`")
end
end
context "with inline code blocks disabled" do
it "converts single line code blocks" do
xml = '<r>foo <CODE><s>[code]</s>some code<e>[/code]</e></CODE> bar</r>'
xml = "<r>foo <CODE><s>[code]</s>some code<e>[/code]</e></CODE> bar</r>"
expect(convert(xml)).to eq(<<~MD.chomp)
foo
@ -315,7 +316,7 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
</URL><e>[/img]</e></IMG></r>
XML
expect(convert(xml)).to eq('![](https://example.com/foo.png)')
expect(convert(xml)).to eq("![](https://example.com/foo.png)")
end
it "converts image with link" do
@ -326,47 +327,53 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
<e>[/img]</e></IMG><e>[/url]</e></URL></r>
XML
expect(convert(xml)).to eq('[![](https://example.com/foo.png)](https://example.com/)')
expect(convert(xml)).to eq("[![](https://example.com/foo.png)](https://example.com/)")
end
end
context "with links" do
it "converts links created without BBCode" do
xml = '<r><URL url="https://en.wikipedia.org/wiki/Capybara">https://en.wikipedia.org/wiki/Capybara</URL></r>'
expect(convert(xml)).to eq('https://en.wikipedia.org/wiki/Capybara')
xml =
'<r><URL url="https://en.wikipedia.org/wiki/Capybara">https://en.wikipedia.org/wiki/Capybara</URL></r>'
expect(convert(xml)).to eq("https://en.wikipedia.org/wiki/Capybara")
end
it "converts links created with BBCode" do
xml = '<r><URL url="https://en.wikipedia.org/wiki/Capybara"><s>[url]</s>https://en.wikipedia.org/wiki/Capybara<e>[/url]</e></URL></r>'
expect(convert(xml)).to eq('https://en.wikipedia.org/wiki/Capybara')
xml =
'<r><URL url="https://en.wikipedia.org/wiki/Capybara"><s>[url]</s>https://en.wikipedia.org/wiki/Capybara<e>[/url]</e></URL></r>'
expect(convert(xml)).to eq("https://en.wikipedia.org/wiki/Capybara")
end
it "converts links with link text" do
xml = '<r><URL url="https://en.wikipedia.org/wiki/Capybara"><s>[url=https://en.wikipedia.org/wiki/Capybara]</s>Capybara<e>[/url]</e></URL></r>'
expect(convert(xml)).to eq('[Capybara](https://en.wikipedia.org/wiki/Capybara)')
xml =
'<r><URL url="https://en.wikipedia.org/wiki/Capybara"><s>[url=https://en.wikipedia.org/wiki/Capybara]</s>Capybara<e>[/url]</e></URL></r>'
expect(convert(xml)).to eq("[Capybara](https://en.wikipedia.org/wiki/Capybara)")
end
it "converts internal links" do
opts = {
url_replacement: lambda do |url|
if url == 'http://forum.example.com/viewtopic.php?f=2&t=2'
'https://discuss.example.com/t/welcome-topic/18'
end
end
url_replacement:
lambda do |url|
if url == "http://forum.example.com/viewtopic.php?f=2&t=2"
"https://discuss.example.com/t/welcome-topic/18"
end
end,
}
xml = '<r><URL url="http://forum.example.com/viewtopic.php?f=2&amp;t=2"><LINK_TEXT text="viewtopic.php?f=2&amp;t=2">http://forum.example.com/viewtopic.php?f=2&amp;t=2</LINK_TEXT></URL></r>'
expect(convert(xml, opts)).to eq('https://discuss.example.com/t/welcome-topic/18')
xml =
'<r><URL url="http://forum.example.com/viewtopic.php?f=2&amp;t=2"><LINK_TEXT text="viewtopic.php?f=2&amp;t=2">http://forum.example.com/viewtopic.php?f=2&amp;t=2</LINK_TEXT></URL></r>'
expect(convert(xml, opts)).to eq("https://discuss.example.com/t/welcome-topic/18")
end
it "converts email links created without BBCode" do
xml = '<r><EMAIL email="foo.bar@example.com">foo.bar@example.com</EMAIL></r>'
expect(convert(xml)).to eq('<foo.bar@example.com>')
expect(convert(xml)).to eq("<foo.bar@example.com>")
end
it "converts email links created with BBCode" do
xml = '<r><EMAIL email="foo.bar@example.com"><s>[email]</s>foo.bar@example.com<e>[/email]</e></EMAIL></r>'
expect(convert(xml)).to eq('<foo.bar@example.com>')
xml =
'<r><EMAIL email="foo.bar@example.com"><s>[email]</s>foo.bar@example.com<e>[/email]</e></EMAIL></r>'
expect(convert(xml)).to eq("<foo.bar@example.com>")
end
it "converts truncated, long links" do
@ -377,7 +384,9 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
<e>[/url]</e></URL></r>
XML
expect(convert(xml)).to eq('http://answers.yahoo.com/question/index?qid=20070920134223AAkkPli')
expect(convert(xml)).to eq(
"http://answers.yahoo.com/question/index?qid=20070920134223AAkkPli",
)
end
it "converts BBCodes inside link text" do
@ -387,7 +396,7 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
<e>[/url]</e></URL></r>
XML
expect(convert(xml)).to eq('[**Hello _world_!**](http://example.com)')
expect(convert(xml)).to eq("[**Hello _world_!**](http://example.com)")
end
end
@ -449,7 +458,8 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
end
it "converts quote with author attribute" do
xml = '<r><QUOTE author="Mr. Blobby"><s>[quote="Mr. Blobby"]</s>Lorem ipsum<e>[/quote]</e></QUOTE></r>'
xml =
'<r><QUOTE author="Mr. Blobby"><s>[quote="Mr. Blobby"]</s>Lorem ipsum<e>[/quote]</e></QUOTE></r>'
expect(convert(xml)).to eq(<<~MD.chomp)
[quote="Mr. Blobby"]
@ -479,10 +489,13 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
end
context "with user_id attribute" do
let(:opts) { { username_from_user_id: lambda { |user_id| user_id == 48 ? "mr_blobby" : nil } } }
let(:opts) do
{ username_from_user_id: lambda { |user_id| user_id == 48 ? "mr_blobby" : nil } }
end
it "uses the correct username when the user exists" do
xml = '<r><QUOTE author="Mr. Blobby" user_id="48"><s>[quote="Mr. Blobby" user_id=48]</s>Lorem ipsum<e>[/quote]</e></QUOTE></r>'
xml =
'<r><QUOTE author="Mr. Blobby" user_id="48"><s>[quote="Mr. Blobby" user_id=48]</s>Lorem ipsum<e>[/quote]</e></QUOTE></r>'
expect(convert(xml, opts)).to eq(<<~MD.chomp)
[quote="mr_blobby"]
@ -492,7 +505,8 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
end
it "uses the author name when the user does not exist" do
xml = '<r><QUOTE author="Mr. Blobby" user_id="49"><s>[quote="Mr. Blobby" user_id=48]</s>Lorem ipsum<e>[/quote]</e></QUOTE></r>'
xml =
'<r><QUOTE author="Mr. Blobby" user_id="49"><s>[quote="Mr. Blobby" user_id=48]</s>Lorem ipsum<e>[/quote]</e></QUOTE></r>'
expect(convert(xml, opts)).to eq(<<~MD.chomp)
[quote="Mr. Blobby"]
@ -502,14 +516,20 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
end
it "creates a blockquote when the user does not exist and the author is missing" do
xml = '<r><QUOTE user_id="49"><s>[quote=user_id=48]</s>Lorem ipsum<e>[/quote]</e></QUOTE></r>'
xml =
'<r><QUOTE user_id="49"><s>[quote=user_id=48]</s>Lorem ipsum<e>[/quote]</e></QUOTE></r>'
expect(convert(xml, opts)).to eq("> Lorem ipsum")
end
end
context "with post_id attribute" do
let(:opts) do
{ quoted_post_from_post_id: lambda { |post_id| { username: 'mr_blobby', post_number: 3, topic_id: 951 } if post_id == 43 } }
{
quoted_post_from_post_id:
lambda do |post_id|
{ username: "mr_blobby", post_number: 3, topic_id: 951 } if post_id == 43
end,
}
end
it "uses information from the quoted post if the post exists" do
@ -589,34 +609,36 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
it "converts smilies" do
opts = {
smilie_to_emoji: lambda do |smilie|
case smilie
when ':D'
':smiley:'
when ':eek:'
':astonished:'
end
end
smilie_to_emoji:
lambda do |smilie|
case smilie
when ":D"
":smiley:"
when ":eek:"
":astonished:"
end
end,
}
xml = '<r><E>:D</E> <E>:eek:</E></r>'
xml = "<r><E>:D</E> <E>:eek:</E></r>"
expect(convert(xml, opts)).to eq(":smiley: :astonished:")
end
context "with attachments" do
it "converts attachments" do
opts = {
upload_md_from_file: lambda do |filename, index|
url = \
case index
when 0 then
"upload://hash2.png"
when 1 then
"upload://hash1.png"
end
upload_md_from_file:
lambda do |filename, index|
url =
case index
when 0
"upload://hash2.png"
when 1
"upload://hash1.png"
end
"![#{filename}|231x231](#{url})"
end
"![#{filename}|231x231](#{url})"
end,
}
xml = <<~XML
@ -761,20 +783,22 @@ RSpec.describe ImportScripts::PhpBB3::BBCode::XmlToMarkdown do
end
it "preserves whitespace between tags" do
xml = "<r>foo <B><s>[b]</s>bold<e>[/b]</e></B> <I><s>[i]</s>italic<e>[/i]</e></I> <U><s>[u]</s>underlined<e>[/u]</e></U> bar</r>"
xml =
"<r>foo <B><s>[b]</s>bold<e>[/b]</e></B> <I><s>[i]</s>italic<e>[/i]</e></I> <U><s>[u]</s>underlined<e>[/u]</e></U> bar</r>"
expect(convert(xml)).to eq("foo **bold** _italic_ [u]underlined[/u] bar")
end
end
context "with unknown element" do
it "converts an unknown element right below the root element" do
xml = '<r><UNKNOWN><s>[unknown]</s>foo<e>[/unknown]</e></UNKNOWN></r>'
expect(convert(xml)).to eq('foo')
xml = "<r><UNKNOWN><s>[unknown]</s>foo<e>[/unknown]</e></UNKNOWN></r>"
expect(convert(xml)).to eq("foo")
end
it "converts an unknown element inside a known element" do
xml = '<r><B><s>[b]</s><UNKNOWN><s>[unknown]</s>bar<e>[/unknown]</e></UNKNOWN><e>[/b]</e></B></r>'
expect(convert(xml)).to eq('**bar**')
xml =
"<r><B><s>[b]</s><UNKNOWN><s>[unknown]</s>bar<e>[/unknown]</e></UNKNOWN><e>[/b]</e></B></r>"
expect(convert(xml)).to eq("**bar**")
end
end

View File

@ -1,125 +1,213 @@
# frozen_string_literal: true
require_relative '../../../script/import_scripts/vanilla_body_parser'
require_relative '../../../script/import_scripts/base/lookup_container'
require_relative '../../../script/import_scripts/base/uploader'
require_relative "../../../script/import_scripts/vanilla_body_parser"
require_relative "../../../script/import_scripts/base/lookup_container"
require_relative "../../../script/import_scripts/base/uploader"
RSpec.describe VanillaBodyParser do
let(:lookup) { ImportScripts::LookupContainer.new }
let(:uploader) { ImportScripts::Uploader.new }
let(:uploads_path) { 'spec/fixtures/images/vanilla_import' }
let(:user) { Fabricate(:user, email: 'saruman@maiar.org', name: 'Saruman, Multicolor', username: 'saruman_multicolor') }
let(:uploads_path) { "spec/fixtures/images/vanilla_import" }
let(:user) do
Fabricate(
:user,
email: "saruman@maiar.org",
name: "Saruman, Multicolor",
username: "saruman_multicolor",
)
end
let(:user_id) { lookup.add_user(user.id.to_s, user) }
before do
STDOUT.stubs(:write)
STDERR.stubs(:write)
VanillaBodyParser.configure(lookup: lookup, uploader: uploader, host: 'vanilla.sampleforum.org', uploads_path: uploads_path)
VanillaBodyParser.configure(
lookup: lookup,
uploader: uploader,
host: "vanilla.sampleforum.org",
uploads_path: uploads_path,
)
end
it 'keeps regular text intact' do
parsed = VanillaBodyParser.new({ 'Format' => 'Html', 'Body' => 'Hello everyone!' }, user_id).parse
expect(parsed).to eq 'Hello everyone!'
it "keeps regular text intact" do
parsed =
VanillaBodyParser.new({ "Format" => "Html", "Body" => "Hello everyone!" }, user_id).parse
expect(parsed).to eq "Hello everyone!"
end
it 'keeps html tags' do
parsed = VanillaBodyParser.new({ 'Format' => 'Html', 'Body' => 'H<br>E<br>L<br>L<br>O' }, user_id).parse
it "keeps html tags" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Html", "Body" => "H<br>E<br>L<br>L<br>O" },
user_id,
).parse
expect(parsed).to eq "H<br>E<br>L<br>L<br>O"
end
it 'parses invalid html, removes font tags and leading spaces' do
complex_html = '''<b><font color=green>this was bold and green:</b></font color=green>
this starts with spaces but IS NOT a quote'''
parsed = VanillaBodyParser.new({ 'Format' => 'Html', 'Body' => complex_html }, user_id).parse
expect(parsed).to eq '''<b>this was bold and green:</b>
this starts with spaces but IS NOT a quote'''
it "parses invalid html, removes font tags and leading spaces" do
complex_html =
"" \
"<b><font color=green>this was bold and green:</b></font color=green>
this starts with spaces but IS NOT a quote" \
""
parsed = VanillaBodyParser.new({ "Format" => "Html", "Body" => complex_html }, user_id).parse
expect(parsed).to eq "" \
"<b>this was bold and green:</b>
this starts with spaces but IS NOT a quote" \
""
end
it 'replaces pre tags with code backticks' do
it "replaces pre tags with code backticks" do
complex_html = '<pre class="CodeBlock">foobar</pre>'
parsed = VanillaBodyParser.new({ 'Format' => 'Html', 'Body' => complex_html }, user_id).parse
parsed = VanillaBodyParser.new({ "Format" => "Html", "Body" => complex_html }, user_id).parse
expect(parsed).to eq "\n```\nfoobar\n```\n"
end
it 'strips code tags' do
complex_html = '<code>foobar</code>'
parsed = VanillaBodyParser.new({ 'Format' => 'Html', 'Body' => complex_html }, user_id).parse
it "strips code tags" do
complex_html = "<code>foobar</code>"
parsed = VanillaBodyParser.new({ "Format" => "Html", "Body" => complex_html }, user_id).parse
expect(parsed).to eq "foobar"
end
it 'replaces div with quote class to bbcode quotes' do
it "replaces div with quote class to bbcode quotes" do
complex_html = '<div class="Quote">foobar</div>'
parsed = VanillaBodyParser.new({ 'Format' => 'Html', 'Body' => complex_html }, user_id).parse
parsed = VanillaBodyParser.new({ "Format" => "Html", "Body" => complex_html }, user_id).parse
expect(parsed).to eq "\n\n[quote]\n\nfoobar\n\n[/quote]\n\n"
end
describe 'rich format' do
let(:rich_bodies) { JSON.parse(File.read('spec/fixtures/json/vanilla-rich-posts.json')).deep_symbolize_keys }
describe "rich format" do
let(:rich_bodies) do
JSON.parse(File.read("spec/fixtures/json/vanilla-rich-posts.json")).deep_symbolize_keys
end
it 'extracts text-only bodies' do
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => rich_bodies[:text].to_json }, user_id).parse
it "extracts text-only bodies" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Rich", "Body" => rich_bodies[:text].to_json },
user_id,
).parse
expect(parsed).to eq "This is a message.\n\nAnd a second line."
end
it 'supports mentions of non-imported users' do
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => rich_bodies[:mention].to_json }, user_id).parse
it "supports mentions of non-imported users" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Rich", "Body" => rich_bodies[:mention].to_json },
user_id,
).parse
expect(parsed).to eq "@Gandalf The Grey, what do you think?"
end
it 'supports mentions imported users' do
mentioned = Fabricate(:user, email: 'gandalf@maiar.com', name: 'Gandalf The Grey', username: 'gandalf_the_grey')
it "supports mentions imported users" do
mentioned =
Fabricate(
:user,
email: "gandalf@maiar.com",
name: "Gandalf The Grey",
username: "gandalf_the_grey",
)
lookup.add_user(mentioned.id.to_s, mentioned)
body = rich_bodies[:mention].to_json.gsub("666", mentioned.id.to_s)
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => body }, user_id).parse
parsed = VanillaBodyParser.new({ "Format" => "Rich", "Body" => body }, user_id).parse
expect(parsed).to eq "@gandalf_the_grey, what do you think?"
end
it 'supports links' do
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => rich_bodies[:links].to_json }, user_id).parse
expect(parsed).to eq "We can link to the <a href=\"https:\/\/www.discourse.org\/\">Discourse home page</a> and it works."
it "supports links" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Rich", "Body" => rich_bodies[:links].to_json },
user_id,
).parse
expect(
parsed,
).to eq "We can link to the <a href=\"https:\/\/www.discourse.org\/\">Discourse home page</a> and it works."
end
it 'supports quotes without topic info when it cannot be found' do
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => rich_bodies[:quote].to_json }, user_id).parse
expect(parsed).to eq "[quote]\n\nThis is the full<br \/>body<br \/>of the quoted discussion.<br \/>\n\n[/quote]\n\nWhen did this happen?"
it "supports quotes without topic info when it cannot be found" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Rich", "Body" => rich_bodies[:quote].to_json },
user_id,
).parse
expect(
parsed,
).to eq "[quote]\n\nThis is the full<br \/>body<br \/>of the quoted discussion.<br \/>\n\n[/quote]\n\nWhen did this happen?"
end
it 'supports quotes with user and topic info' do
post = Fabricate(:post, user: user, id: 'discussion#12345', raw: "This is the full\r\nbody\r\nof the quoted discussion.\r\n")
it "supports quotes with user and topic info" do
post =
Fabricate(
:post,
user: user,
id: "discussion#12345",
raw: "This is the full\r\nbody\r\nof the quoted discussion.\r\n",
)
topic_id = lookup.add_topic(post)
lookup.add_post('discussion#12345', post)
lookup.add_post("discussion#12345", post)
body = rich_bodies[:quote].to_json.gsub("34567", user.id.to_s)
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => body }, user_id).parse
expect(parsed).to eq "[quote=\"#{user.username}, post: #{post.post_number}, topic: #{post.topic.id}\"]\n\nThis is the full<br \/>body<br \/>of the quoted discussion.<br \/>\n\n[/quote]\n\nWhen did this happen?"
parsed = VanillaBodyParser.new({ "Format" => "Rich", "Body" => body }, user_id).parse
expect(
parsed,
).to eq "[quote=\"#{user.username}, post: #{post.post_number}, topic: #{post.topic.id}\"]\n\nThis is the full<br \/>body<br \/>of the quoted discussion.<br \/>\n\n[/quote]\n\nWhen did this happen?"
end
it 'supports uploaded images' do
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => rich_bodies[:image].to_json }, user_id).parse
expect(parsed).to match(/Here's the screenshot\:\n\n\!\[Screen Shot 2020\-05\-26 at 7\.09\.06 AM\.png\|\d+x\d+\]\(upload\:\/\/\w+\.png\)$/)
it "supports uploaded images" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Rich", "Body" => rich_bodies[:image].to_json },
user_id,
).parse
expect(parsed).to match(
%r{Here's the screenshot\:\n\n\!\[Screen Shot 2020\-05\-26 at 7\.09\.06 AM\.png\|\d+x\d+\]\(upload\://\w+\.png\)$},
)
end
it 'supports embedded links' do
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => rich_bodies[:embed_link].to_json }, user_id).parse
expect(parsed).to eq "Does anyone know this website?\n\n[Title of the page being linked](https:\/\/someurl.com\/long\/path\/here_and_there\/?fdkmlgm)"
it "supports embedded links" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Rich", "Body" => rich_bodies[:embed_link].to_json },
user_id,
).parse
expect(
parsed,
).to eq "Does anyone know this website?\n\n[Title of the page being linked](https:\/\/someurl.com\/long\/path\/here_and_there\/?fdkmlgm)"
end
it 'keeps uploaded files as links' do
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => rich_bodies[:upload_file].to_json }, user_id).parse
expect(parsed).to eq "This is a PDF I've uploaded:\n\n<a href=\"https://vanilla.sampleforum.org/uploads/393/5QR3BX57K7HM.pdf\">original_name_of_file.pdf</a>"
it "keeps uploaded files as links" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Rich", "Body" => rich_bodies[:upload_file].to_json },
user_id,
).parse
expect(
parsed,
).to eq "This is a PDF I've uploaded:\n\n<a href=\"https://vanilla.sampleforum.org/uploads/393/5QR3BX57K7HM.pdf\">original_name_of_file.pdf</a>"
end
it 'supports complex formatting' do
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => rich_bodies[:complex_formatting].to_json }, user_id).parse
expect(parsed).to eq "<b>Name</b>: Jon Snow\n\n<b><i>* not their real name</i></b>\n\n<ol>\n\n<li>first item</li>\n\n<li>second</li>\n\n<li>third and last</li>\n\n</ol>\n\nThat's all folks!"
it "supports complex formatting" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Rich", "Body" => rich_bodies[:complex_formatting].to_json },
user_id,
).parse
expect(
parsed,
).to eq "<b>Name</b>: Jon Snow\n\n<b><i>* not their real name</i></b>\n\n<ol>\n\n<li>first item</li>\n\n<li>second</li>\n\n<li>third and last</li>\n\n</ol>\n\nThat's all folks!"
end
it 'support code blocks' do
parsed = VanillaBodyParser.new({ 'Format' => 'Rich', 'Body' => rich_bodies[:code_block].to_json }, user_id).parse
expect(parsed).to eq "Here's a monospaced block:\n\n```\nthis line should be monospaced\nthis one too, with extra spaces#{' ' * 4}\n```\n\nbut not this one"
it "support code blocks" do
parsed =
VanillaBodyParser.new(
{ "Format" => "Rich", "Body" => rich_bodies[:code_block].to_json },
user_id,
).parse
expect(
parsed,
).to eq "Here's a monospaced block:\n\n```\nthis line should be monospaced\nthis one too, with extra spaces#{" " * 4}\n```\n\nbut not this one"
end
end
end