2020-08-24 16:19:57 -04:00
# 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 "
2022-07-27 22:27:38 -04:00
RSpec . describe VanillaBodyParser do
2020-08-24 16:19:57 -04:00
let ( :lookup ) { ImportScripts :: LookupContainer . new }
let ( :uploader ) { ImportScripts :: Uploader . new }
let ( :uploads_path ) { " spec/fixtures/images/vanilla_import " }
2022-10-25 03:29:09 -04:00
let ( :user ) do
Fabricate (
:user ,
email : " saruman@maiar.org " ,
name : " Saruman, Multicolor " ,
username : " saruman_multicolor " ,
)
2023-01-09 06:18:21 -05:00
end
2022-10-25 03:29:09 -04:00
let ( :user_id ) { lookup . add_user ( user . id . to_s , user ) }
2020-08-24 16:19:57 -04:00
before do
STDOUT . stubs ( :write )
STDERR . stubs ( :write )
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! "
end
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 =
2023-01-09 06:18:21 -05:00
" " \
2020-08-24 16:19:57 -04:00
" <b><font color=green>this was bold and green:</b></font color=green>
this starts with spaces but IS NOT a quote " \
2023-01-09 06:18:21 -05:00
" "
2020-08-24 16:19:57 -04:00
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 " \
2023-01-09 06:18:21 -05:00
" "
2020-08-24 16:19:57 -04:00
end
2021-01-16 09:05:19 -05:00
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
expect ( parsed ) . to eq " \n ``` \n foobar \n ``` \n "
end
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
complex_html = '<div class="Quote">foobar</div>'
parsed = VanillaBodyParser . new ( { " Format " = > " Html " , " Body " = > complex_html } , user_id ) . parse
expect ( parsed ) . to eq " \n \n [quote] \n \n foobar \n \n [/quote] \n \n "
end
2020-08-24 16:19:57 -04:00
describe " rich format " do
let ( :rich_bodies ) do
JSON . parse ( File . read ( " spec/fixtures/json/vanilla-rich-posts.json " ) ) . deep_symbolize_keys
2023-01-09 06:18:21 -05:00
end
2020-08-24 16:19:57 -04:00
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 \n And 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
expect ( parsed ) . to eq " @Gandalf The Grey, what do you think? "
end
it " supports mentions imported users " do
2022-10-25 03:29:09 -04:00
mentioned =
Fabricate (
:user ,
email : " gandalf@maiar.com " ,
name : " Gandalf The Grey " ,
username : " gandalf_the_grey " ,
)
lookup . add_user ( mentioned . id . to_s , mentioned )
2020-08-24 16:19:57 -04:00
2022-10-25 03:29:09 -04:00
body = rich_bodies [ :mention ] . to_json . gsub ( " 666 " , mentioned . id . to_s )
parsed = VanillaBodyParser . new ( { " Format " = > " Rich " , " Body " = > body } , user_id ) . parse
2020-08-24 16:19:57 -04:00
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. "
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 \n This is the full<br \ />body<br \ />of the quoted discussion.<br \ /> \n \n [/quote] \n \n When 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 \n body \r \n of the quoted discussion. \r \n " ,
)
topic_id = lookup . add_topic ( post )
lookup . add_post ( " discussion # 12345 " , post )
2022-10-25 03:29:09 -04:00
body = rich_bodies [ :quote ] . to_json . gsub ( " 34567 " , user . id . to_s )
parsed = VanillaBodyParser . new ( { " Format " = > " Rich " , " Body " = > body } , user_id ) . parse
2020-08-24 16:19:57 -04:00
expect (
parsed ,
) . to eq " [quote= \" #{ user . username } , post: #{ post . post_number } , topic: #{ post . topic . id } \" ] \n \n This is the full<br \ />body<br \ />of the quoted discussion.<br \ /> \n \n [/quote] \n \n When 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 (
%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) "
end
it " keeps uploaded files as links " do
parsed =
VanillaBodyParser . new (
{ " Format " = > " Rich " , " Body " = > rich_bodies [ :upload_file ] . to_json } ,
user_id ,
) . parse
2021-01-13 12:40:00 -05:00
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> "
2020-08-24 16:19:57 -04:00
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 \n That's all folks! "
end
it " support code blocks " do
parsed =
VanillaBodyParser . new (
{ " Format " = > " Rich " , " Body " = > rich_bodies [ :code_block ] . to_json } ,
user_id ,
) . parse
2021-01-13 12:40:00 -05:00
expect (
parsed ,
) . to eq " Here's a monospaced block: \n \n ``` \n this line should be monospaced \n this one too, with extra spaces #{ " " * 4 } \n ``` \n \n but not this one "
2020-08-24 16:19:57 -04:00
end
end
end