2019-04-30 08:27:42 +08:00
# frozen_string_literal: true
2013-02-06 03:16:51 +08:00
describe Jobs :: ProcessPost do
it " returns when the post cannot be found " do
2014-12-31 22:55:03 +08:00
expect { Jobs :: ProcessPost . new . perform ( post_id : 1 , sync_exec : true ) } . not_to raise_error
2013-02-06 03:16:51 +08:00
end
context 'with a post' do
2019-05-07 11:12:20 +08:00
fab! ( :post ) { Fabricate ( :post ) }
2013-02-06 03:16:51 +08:00
2014-12-07 15:11:38 +08:00
it 'does not erase posts when CookedPostProcessor malfunctions' do
# Look kids, an actual reason why you want to use mocks
CookedPostProcessor . any_instance . expects ( :html ) . returns ( ' ' )
cooked = post . cooked
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . cooked ) . to eq ( cooked )
2014-12-07 15:11:38 +08:00
Jobs :: ProcessPost . new . execute ( post_id : post . id , cook : true )
2013-02-06 03:16:51 +08:00
end
2014-12-07 15:11:38 +08:00
it 'recooks if needed' do
cooked = post . cooked
post . update_columns ( cooked : " frogs " )
Jobs :: ProcessPost . new . execute ( post_id : post . id , cook : true )
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . cooked ) . to eq ( cooked )
2013-02-06 03:16:51 +08:00
end
2014-12-07 15:11:38 +08:00
it 'processes posts' do
post = Fabricate ( :post , raw : " <img src=' #{ Discourse . base_url_no_prefix } /awesome/picture.png'> " )
2014-12-31 22:55:03 +08:00
expect ( post . cooked ) . to match ( / http / )
2021-10-19 19:42:29 +08:00
stub_image_size
2014-12-07 15:11:38 +08:00
Jobs :: ProcessPost . new . execute ( post_id : post . id )
post . reload
# subtle but cooked post processor strip this stuff, this ensures all the code gets a workout
2014-12-31 22:55:03 +08:00
expect ( post . cooked ) . not_to match ( / http / )
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
2017-01-30 16:42:05 +08:00
it " always re-extracts links on post process " do
post . update_columns ( raw : " sam has a blog at https://samsaffron.com " )
2017-01-31 16:40:56 +08:00
expect { Jobs :: ProcessPost . new . execute ( post_id : post . id ) } . to change { TopicLink . count } . by ( 1 )
2017-01-30 16:42:05 +08:00
end
2017-02-06 21:45:04 +08:00
it " extracts links to quoted posts " do
quoted_post = Fabricate ( :post , raw : " This is a post with a link to https://www.discourse.org " , post_number : 42 )
2017-07-14 20:27:28 +08:00
post . update_columns ( raw : " This quote is the best \n \n [quote= \" #{ quoted_post . user . username } , topic: #{ quoted_post . topic_id } , post: #{ quoted_post . post_number } \" ] \n #{ quoted_post . excerpt } \n [/quote] " )
2021-10-19 19:42:29 +08:00
stub_image_size
2017-02-06 21:45:04 +08:00
# when creating a quote, we also create the reflexion link
expect { Jobs :: ProcessPost . new . execute ( post_id : post . id ) } . to change { TopicLink . count } . by ( 2 )
end
2013-02-06 03:16:51 +08:00
2017-02-06 21:45:04 +08:00
it " extracts links to oneboxed topics " do
oneboxed_post = Fabricate ( :post )
post . update_columns ( raw : " This post is the best \n \n #{ oneboxed_post . full_url } " )
2021-10-19 19:42:29 +08:00
stub_image_size
2017-02-06 21:45:04 +08:00
# when creating a quote, we also create the reflexion link
expect { Jobs :: ProcessPost . new . execute ( post_id : post . id ) } . to change { TopicLink . count } . by ( 2 )
end
2017-12-21 21:45:59 +08:00
it " works for posts that belong to no existing user " do
cooked = post . cooked
post . update_columns ( cooked : " frogs " , user_id : nil )
Jobs :: ProcessPost . new . execute ( post_id : post . id , cook : true )
post . reload
expect ( post . cooked ) . to eq ( cooked )
post . update_columns ( cooked : " frogs " , user_id : User . maximum ( " id " ) + 1 )
Jobs :: ProcessPost . new . execute ( post_id : post . id , cook : true )
post . reload
expect ( post . cooked ) . to eq ( cooked )
end
2021-03-03 16:53:38 +08:00
2021-03-11 01:07:13 +08:00
it " updates the topic excerpt when first post " do
post = Fabricate ( :post , raw : " Some OP content " , cooked : " " )
post . topic . update_excerpt ( " Incorrect " )
Jobs :: ProcessPost . new . execute ( post_id : post . id )
expect ( post . topic . reload . excerpt ) . to eq ( " Some OP content " )
post2 = Fabricate ( :post , raw : " Some reply content " , cooked : " " , topic : post . topic )
Jobs :: ProcessPost . new . execute ( post_id : post2 . id )
expect ( post . topic . reload . excerpt ) . to eq ( " Some OP content " )
end
2017-02-06 21:45:04 +08:00
end
2013-02-06 03:16:51 +08:00
end