Skip to content

Commit

Permalink
Fix some strange text filter work
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Francois committed Oct 1, 2013
1 parent 0c38a45 commit ce9c764
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 146 deletions.
10 changes: 5 additions & 5 deletions app/models/content_base.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand Down
7 changes: 5 additions & 2 deletions app/models/ping.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/factories.rb
Expand Up @@ -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|
Expand Down Expand Up @@ -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 <a href="http://fakeurl.co.uk">body</a>'
Expand Down Expand Up @@ -261,7 +262,7 @@
user
published true
state 'published'
text_filter {FactoryGirl.create(:markdown)}
association :text_filter, factory: :markdown
guid
end

Expand Down
263 changes: 126 additions & 137 deletions 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(%{<link rel="pingback" href="http://anotherblog.org/xml-rpc" />})
@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 => '<a href="http://anotherblog.org/a-post">',
: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(%{<link rel="pingback" href="http://anotherblog.org/xml-rpc" />})
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 => '<a href="http://anotherblog.org/a-post">', :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 <a href="http://blog.rubyonrails.org/">blog.rubyonrails.org</a>

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 <a href="http://blog.rubyonrails.org/">blog.rubyonrails.org</a>

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
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
Expand All @@ -141,19 +129,20 @@ def referenced_body
dc:creator="pdcawley"
dc:date="2006-03-01T04:31:00-05:00" />
</rdf:RDF>
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

0 comments on commit ce9c764

Please sign in to comment.