From ce9c76446aa4e00a29dc1f30c0c54ba85776b4df Mon Sep 17 00:00:00 2001 From: Yannick Francois Date: Tue, 1 Oct 2013 22:01:05 +0200 Subject: [PATCH] Fix some strange text filter work --- app/models/content_base.rb | 10 +- app/models/ping.rb | 7 +- spec/factories.rb | 5 +- spec/models/ping_spec.rb | 263 ++++++++++++++++++------------------- 4 files changed, 139 insertions(+), 146 deletions(-) diff --git a/app/models/content_base.rb b/app/models/content_base.rb index f395362124..386a8ffe01 100644 --- a/app/models/content_base.rb +++ b/app/models/content_base.rb @@ -48,7 +48,7 @@ def generate_html(field, text = nil) text ||= self[field].to_s prehtml = html_preprocess(field, text).to_s html = (text_filter || default_text_filter).filter_text_for_content(blog, prehtml, self) || prehtml - html_postprocess(field,html).to_s + html_postprocess(field, html).to_s end # Post-process the HTML. This is a noop by default, but Comment overrides it @@ -80,10 +80,10 @@ def excerpt_text(length = 160) def invalidates_cache?(on_destruction = false) @invalidates_cache ||= if on_destruction - just_changed_published_status? || published? - else - (changed? && published?) || just_changed_published_status? - end + just_changed_published_status? || published? + else + (changed? && published?) || just_changed_published_status? + end end def publish! diff --git a/app/models/ping.rb b/app/models/ping.rb index 0f6a61ca3c..b3d3c01b26 100644 --- a/app/models/ping.rb +++ b/app/models/ping.rb @@ -61,9 +61,9 @@ def trackback_url def send_pingback if pingback_url send_xml_rpc(pingback_url, "pingback.ping", origin_url, ping.url) - return true + true else - return false + false end end @@ -92,6 +92,9 @@ def do_send_trackback(trackback_url, origin_url) def initialize(origin_url, ping) @origin_url = origin_url @ping = ping + #Add this call to text filter cause of a strange thing around text_filter. Need to clean text_filter usage ! + ping.article.default_text_filter + ping.article.text_filter # Make sure these are fetched now for thread safety purposes. self.article = ping.article self.blog = article.blog diff --git a/spec/factories.rb b/spec/factories.rb index 345424bc4d..7fe64a6d5a 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -61,6 +61,7 @@ allow_comments true published true allow_pings true + association :text_filter, factory: :textile end factory :unpublished_article, :parent => :article do |a| @@ -220,7 +221,7 @@ factory :comment do published true article - text_filter {FactoryGirl.create(:textile)} + association :text_filter, factory: :textile author 'Bob Foo' url 'http://fakeurl.com' body 'Test body' @@ -261,7 +262,7 @@ user published true state 'published' - text_filter {FactoryGirl.create(:markdown)} + association :text_filter, factory: :markdown guid end diff --git a/spec/models/ping_spec.rb b/spec/models/ping_spec.rb index 78c49fedd9..cde6aac983 100644 --- a/spec/models/ping_spec.rb +++ b/spec/models/ping_spec.rb @@ -1,134 +1,122 @@ require 'spec_helper' require 'xmlrpc/client' -describe 'Given a post which references a pingback enabled article' do - def pingback_target; 'http://anotherblog.org/xml-rpc'; end - def referenced_url; 'http://anotherblog.org/a-post'; end - def referrer_url; 'http://myblog.net/referring-post'; end - - - before(:each) do - @mock_response = double('response') - @mock_xmlrpc_response = double('xmlrpc_response') - end - - it 'Pingback sent to url found in referenced header' do - FactoryGirl.create(:blog) - @mock_response.should_receive(:[]).with('X-Pingback').at_least(:once).and_return(pingback_target) - @mock_xmlrpc_response.should_receive(:call).with('pingback.ping', referrer_url, referenced_url) - make_and_send_ping - end - - it 'Pingback sent to url found in referenced body' do - FactoryGirl.create(:blog) - @mock_response.should_receive(:[]).with('X-Pingback').at_least(:once).and_return(nil) - @mock_response.should_receive(:body).at_least(:once)\ - .and_return(%{}) - @mock_xmlrpc_response.should_receive(:call).with('pingback.ping', referrer_url, referenced_url) - make_and_send_ping - end - - it 'Pingback sent when new article is saved' do - ActiveRecord::Base.observers.should include(:email_notifier) - ActiveRecord::Base.observers.should include(:web_notifier) - - FactoryGirl.create(:blog, :send_outbound_pings => 1) - - a = Article.new \ - :body => '', - :title => 'Test the pinging', - :published => true - - Net::HTTP.should_receive(:get_response).and_return(@mock_response) - XMLRPC::Client.should_receive(:new2).with(pingback_target).and_return(@mock_xmlrpc_response) - - @mock_response.should_receive(:[]).with('X-Pingback').at_least(:once).and_return(pingback_target) - @mock_xmlrpc_response.should_receive(:call)\ - .with('pingback.ping', - %r{http://myblog.net/\d{4}/\d{2}/\d{2}/test-the-pinging}, - referenced_url) - - - a.should have(1).html_urls - a.save! - a.should be_just_published - a = Article.find(a.id) - a.should_not be_just_published - # Saving again will not resend the pings - a.save +describe :ping do + describe 'Given a post which references a pingback enabled article' do + + let(:pingback_target) { 'http://anotherblog.org/xml-rpc' } + let(:referenced_url) { 'http://anotherblog.org/a-post' } + let(:referrer_url) { 'http://myblog.net/referring-post' } + let(:mock_response) { double('response') } + let(:mock_xmlrpc_response) { double('xmlrpc_response') } + + context "with a default blog" do + let!(:blog) { create(:blog) } + + it 'Pingback sent to url found in referenced header' do + mock_response.should_receive(:[]).with('X-Pingback').at_least(:once).and_return(pingback_target) + mock_xmlrpc_response.should_receive(:call).with('pingback.ping', referrer_url, referenced_url) + make_and_send_ping + end + + it 'Pingback sent to url found in referenced body' do + mock_response.should_receive(:[]).with('X-Pingback').at_least(:once).and_return(nil) + mock_response.should_receive(:body).at_least(:once)\ + .and_return(%{}) + mock_xmlrpc_response.should_receive(:call).with('pingback.ping', referrer_url, referenced_url) + make_and_send_ping + end + end + + context "with a send outboug pings blog" do + let!(:blog) { create(:blog, send_outbound_pings: 1) } + + it 'Pingback sent when new article is saved' do + ActiveRecord::Base.observers.should include(:email_notifier) + ActiveRecord::Base.observers.should include(:web_notifier) + a = Article.new :body => '', :title => 'Test the pinging', :published => true + + Net::HTTP.should_receive(:get_response).and_return(mock_response) + XMLRPC::Client.should_receive(:new2).with(pingback_target).and_return(mock_xmlrpc_response) + + mock_response.should_receive(:[]).with('X-Pingback').at_least(:once).and_return(pingback_target) + mock_xmlrpc_response.should_receive(:call).with('pingback.ping', %r{http://myblog.net/\d{4}/\d{2}/\d{2}/test-the-pinging}, referenced_url) + + a.should have(1).html_urls + a.save! + a.should be_just_published + a = Article.find(a.id) + a.should_not be_just_published + # Saving again will not resend the pings + a.save + end + + end + + def make_and_send_ping + Net::HTTP.should_receive(:get_response).and_return(mock_response) + XMLRPC::Client.should_receive(:new2).with(pingback_target).and_return(mock_xmlrpc_response) + + ping = FactoryGirl.create(:article).pings.build("url" => referenced_url) + ping.should be_instance_of(Ping) + ping.url.should == referenced_url + ping.send_pingback_or_trackback(referrer_url).join + end end - def make_and_send_ping - Net::HTTP.should_receive(:get_response).and_return(@mock_response) - XMLRPC::Client.should_receive(:new2).with(pingback_target).and_return(@mock_xmlrpc_response) - - ping = FactoryGirl.create(:article).pings.build("url" => referenced_url) - ping.should be_instance_of(Ping) - ping.url.should == referenced_url - ping.send_pingback_or_trackback(referrer_url).join - end -end - -describe "An article links to another article, which contains a trackback URL" do - def referenced_url; 'http://anotherblog.org/a-post'; end - def trackback_url; "http://anotherblog.org/a-post/trackback"; end - before(:each) do - build_stubbed(:blog) - end - - it 'Trackback URL is detected and pinged' do - referrer_url = 'http://myblog.net/referring-post' - post = "title=Article+1%21&excerpt=body&url=http://myblog.net/referring-post&blog_name=test+blog" - article = FactoryGirl.create(:article, :title => 'Article 1!', :body => 'body', :permalink => 'referring-post') - make_and_send_ping(post, article, referrer_url) - end - - it 'sends a trackback without html tag in excerpt' do - # TODO: Assert the following: - # contents(:xmltest).body = originally seen on blog.rubyonrails.org - - article = FactoryGirl.create(:article, :title => "Associations aren't :dependent => true anymore", - :excerpt => "A content with several data") - post = "title=#{CGI.escape(article.title)}" - post << "&excerpt=#{CGI.escape("A content with several data")}" # not original text see if normal ? - post << "&url=#{article.permalink_url}" - post << "&blog_name=#{CGI.escape('test blog')}" - - make_and_send_ping(post, article, article.permalink_url) - end - - it 'sends a trackback without markdown tag in excerpt' do - # TODO: Assert the following: - # contents(:markdown_article) #in markdown format\n * we\n * use\n [ok](http://blog.ok.com) to define a link - - article = FactoryGirl.create(:article, :title => "How made link with markdown", - :excerpt => "A content with several data" ) - post = "title=#{CGI.escape(article.title)}" - post << "&excerpt=#{CGI.escape("A content with several data")}" # not original text see if normal ? - post << "&url=#{article.permalink_url}" - post << "&blog_name=#{CGI.escape('test blog')}" - - make_and_send_ping(post, article, article.permalink_url) - end - - def make_and_send_ping(post, article, article_url) - @mock = double('html_response') - Net::HTTP.should_receive(:get_response).with(URI.parse(referenced_url)).and_return(@mock) - @mock.should_receive(:[]).with('X-Pingback').at_least(:once) - @mock.should_receive(:body).twice.and_return(referenced_body) - Net::HTTP.should_receive(:start).with(URI.parse(trackback_url).host, 80).and_yield(@mock) - - @mock.should_receive(:post) \ - .with('/a-post/trackback', post, - 'Content-type' => 'application/x-www-form-urlencoded; charset=utf-8') \ - .and_return(@mock) - - ping = article.pings.build(:url => referenced_url) - ping.send_pingback_or_trackback(article_url).join - end - - def referenced_body - <<-eobody + describe "An article links to another article, which contains a trackback URL" do + let(:referenced_url) { 'http://anotherblog.org/a-post' } + let(:trackback_url) { "http://anotherblog.org/a-post/trackback" } + let!(:blog) { create(:blog) } + + it 'Trackback URL is detected and pinged' do + referrer_url = 'http://myblog.net/referring-post' + post = "title=Article+1%21&excerpt=body&url=http://myblog.net/referring-post&blog_name=test+blog" + article = create(:article, title: 'Article 1!', body: 'body', permalink: 'referring-post') + make_and_send_ping(post, article, referrer_url) + end + + it 'sends a trackback without html tag in excerpt' do + # TODO: Assert the following: + # contents(:xmltest).body = originally seen on blog.rubyonrails.org + + article = create(:article, title: "Associations aren't :dependent => true anymore", excerpt: "A content with several data") + post = "title=#{CGI.escape(article.title)}" + post << "&excerpt=#{CGI.escape("A content with several data")}" # not original text see if normal ? + post << "&url=#{article.permalink_url}" + post << "&blog_name=#{CGI.escape('test blog')}" + + make_and_send_ping(post, article, article.permalink_url) + end + + it 'sends a trackback without markdown tag in excerpt' do + # TODO: Assert the following: + # contents(:markdown_article) #in markdown format\n * we\n * use\n [ok](http://blog.ok.com) to define a link + + article = create(:article, title: "How made link with markdown", excerpt: "A content with several data" ) + post = "title=#{CGI.escape(article.title)}" + post << "&excerpt=#{CGI.escape("A content with several data")}" # not original text see if normal ? + post << "&url=#{article.permalink_url}" + post << "&blog_name=#{CGI.escape('test blog')}" + + make_and_send_ping(post, article, article.permalink_url) + end + + def make_and_send_ping(post, article, article_url) + mock = double('html_response') + Net::HTTP.should_receive(:get_response).with(URI.parse(referenced_url)).and_return(mock) + mock.should_receive(:[]).with('X-Pingback').at_least(:once) + mock.should_receive(:body).twice.and_return(referenced_body) + Net::HTTP.should_receive(:start).with(URI.parse(trackback_url).host, 80).and_yield(mock) + + mock.should_receive(:post).with('/a-post/trackback', post, 'Content-type' => 'application/x-www-form-urlencoded; charset=utf-8').and_return(mock) + + ping = article.pings.create(url: referenced_url) + ping.send_pingback_or_trackback(article_url).join + end + + def referenced_body + r = <<-eobody @@ -141,19 +129,20 @@ def referenced_body dc:creator="pdcawley" dc:date="2006-03-01T04:31:00-05:00" /> - eobody + eobody + r + end end -end -describe 'Given a remote site to notify, eg technorati' do - it 'we can ping them correctly' do - FactoryGirl.create(:blog) - mock = double('response') - XMLRPC::Client.should_receive(:new2).with('http://rpc.technorati.com/rpc/ping').and_return(mock) - mock.should_receive(:call).with('weblogUpdates.ping', 'test blog', - 'http://myblog.net', 'http://myblog.net/new-post') + describe 'Given a remote site to notify, eg technorati' do + it 'we can ping them correctly' do + create(:blog) + mock = double('response') + XMLRPC::Client.should_receive(:new2).with('http://rpc.technorati.com/rpc/ping').and_return(mock) + mock.should_receive(:call).with('weblogUpdates.ping', 'test blog', 'http://myblog.net', 'http://myblog.net/new-post') - ping = FactoryGirl.create(:article).pings.build("url" => "http://rpc.technorati.com/rpc/ping") - ping.send_weblogupdatesping('http://myblog.net', 'http://myblog.net/new-post').join + ping = create(:article).pings.build("url" => "http://rpc.technorati.com/rpc/ping") + ping.send_weblogupdatesping('http://myblog.net', 'http://myblog.net/new-post').join + end end end