17 Commits

Author SHA1 Message Date
Marc Remolt
32bb8a528a Typo 2011-06-13 19:03:52 +02:00
Marc Remolt
309ec660b9 Preparing for release 0.3.0 2011-06-13 19:02:25 +02:00
Marc Remolt
90665484c7 Resource import working
* added rake tasks
2011-06-13 18:48:17 +02:00
Marc Remolt
a24ea686fa Image import working
* bundle update
* replacement of image URLs now considers wordpress thumbnails (like -150x150)
* added Guard for autotesting
2011-06-13 15:02:40 +02:00
Marc Remolt
7265d31f62 Some specs for file Attachment 2011-06-06 19:45:34 +02:00
Marc Remolt
2581895c7c Updated test dump and adapted specs
* We now have a second attachment, a simple text file
2011-06-06 19:39:27 +02:00
Marc Remolt
7550dfa164 First shot at attachment import
* only images for now
* only blog posts for now
* thumbnailed images are not yet replaced in blog posts body
2011-06-05 19:14:52 +02:00
Marc Remolt
9a2b5acef6 Spec improvements
* replaced it with specify on {...} blocks
2011-06-05 17:55:00 +02:00
Marc Remolt
772cfdc25b Started on attachment import
* created Attachment
* created Dump#attachments
* specs
* replaced require with autoload
2011-06-05 17:43:30 +02:00
Marc Remolt
814c7b4ca8 Preparing the 0.2.0 release 2011-06-05 14:04:16 +02:00
Marc Remolt
c7c76a709b More documentation 2011-06-05 14:01:47 +02:00
Marc Remolt
76830ec51c More refactorings and specs 2011-06-05 13:38:39 +02:00
Marc Remolt
ddb65040a7 Refactoring of specs
* split the one large spec file in multiple small, one for each model
* helper for getting dump object in tests
2011-06-05 13:09:06 +02:00
Marc Remolt
e1c60163e4 Started on cms page import
* created rake tasks
* still needed: cleanups, more tests and docs
2011-06-03 18:50:27 +02:00
Marc Remolt
dacd503d8f Formatting fixes
* less <br /> tags by replacing simple_format
* improved regex for SyntaxHighlighter (less false positives)
2011-06-03 17:34:40 +02:00
Marc Remolt
96a81d7b01 Added zsh hint to doc 2011-06-03 16:20:59 +02:00
Marc Remolt
a97c2968ab Added info on installing the first release via rubygems 2011-06-03 12:01:10 +02:00
26 changed files with 1020 additions and 291 deletions

3
.gitignore vendored
View File

@@ -4,3 +4,6 @@ pkg/
spec/dummy/db/*.sqlite3
spec/dummy/log/*.log
spec/dummy/tmp/
*.un~
refinerycms-wordpress-import-*.gem
*.swp

12
Gemfile
View File

@@ -3,9 +3,17 @@ source "http://rubygems.org"
gem "rails", "3.0.7"
#gem "capybara", ">= 1.0.0.beta1"
gem "sqlite3"
gem "rmagick"
gem "rspec-rails", ">= 2.6.0"
gem "database_cleaner"
group :development, :test do
gem "rspec-rails", ">= 2.6.0"
gem "database_cleaner"
gem 'guard-rspec'
gem 'ffi'
gem 'guard-bundler'
gem 'libnotify' if RUBY_PLATFORM =~ /linux/i
gem 'fakeweb'
end
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'

View File

@@ -1,7 +1,7 @@
PATH
remote: ./
specs:
refinerycms-wordpress-import (0.1.0)
refinerycms-wordpress-import (0.3.0)
bundler (~> 1.0)
nokogiri (~> 1.4.4)
refinerycms (~> 1.0.0)
@@ -42,7 +42,7 @@ GEM
arel (2.0.10)
awesome_nested_set (2.0.0)
activerecord (>= 3.0.0)
babosa (0.3.4)
babosa (0.3.5)
bcrypt-ruby (2.1.4)
builder (2.1.2)
database_cleaner (0.6.7)
@@ -51,17 +51,27 @@ GEM
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
diff-lcs (1.1.2)
dragonfly (0.9.2)
dragonfly (0.9.4)
rack
erubis (2.6.6)
abstract (>= 1.0.0)
fakeweb (1.3.0)
ffi (1.0.9)
filters_spam (0.3)
friendly_id_globalize3 (3.2.1.3)
babosa (~> 0.3.0)
globalize3 (0.1.0)
activemodel (>= 3.0.0)
activerecord (>= 3.0.0)
guard (0.4.2)
thor (~> 0.14.6)
guard-bundler (0.1.3)
bundler (>= 1.0.0)
guard (>= 0.2.2)
guard-rspec (0.4.0)
guard (>= 0.4.0)
i18n (0.5.0)
libnotify (0.5.5)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -91,7 +101,7 @@ GEM
activesupport (= 3.0.7)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.9.1)
rake (0.9.2)
refinerycms (1.0.0)
bundler (~> 1.0)
refinerycms-authentication (= 1.0.0)
@@ -125,7 +135,7 @@ GEM
will_paginate (~> 3.0.pre)
refinerycms-dashboard (1.0.0)
refinerycms-core (= 1.0.0)
refinerycms-generators (1.0.1)
refinerycms-generators (1.0.2)
refinerycms-images (1.0.0)
dragonfly (~> 0.9.0)
rack-cache (>= 0.5.3)
@@ -142,11 +152,12 @@ GEM
refinerycms-core (= 1.0.0)
refinerycms-settings (1.0.0)
refinerycms-base (= 1.0.0)
rmagick (2.13.1)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.3)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
@@ -172,9 +183,15 @@ PLATFORMS
DEPENDENCIES
database_cleaner
fakeweb
ffi
guard-bundler
guard-rspec
libnotify
rails (= 3.0.7)
refinerycms
refinerycms-blog
refinerycms-wordpress-import!
rmagick
rspec-rails (>= 2.6.0)
sqlite3

26
Guardfile Normal file
View File

@@ -0,0 +1,26 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('spec/spec_helper.rb') { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
end
guard 'bundler' do
watch('Gemfile')
# Uncomment next line if Gemfile contain `gemspec' command
watch(/^.+\.gemspec/)
end

View File

@@ -2,19 +2,28 @@
This litte project is an importer for WordPress XML dumps into refinerycms(-blog).
So far, only blog-relevant data gets imported, I'm working on the cms pages part.
You can find the source code on github: https://github.com/mremolt/refinerycms-wordpress-import
Keep in mind that links to other pages of your blog are just copied, as WordPress exports them as <a>-Tags.
If your site (blog) structure uses new urls, the links WILL break! For example, if you used
the popular WP blog url structure "YYYY-MM/slug", be warned that Refinery just uses "blog/slug".
So your inner site links will point to the old WP url.
== Prerequisites
As refinerycms-wordpress-import is an addon for RefineryCMS, is shares the prerequisites with it.
So you'll first need a running installation of refinerycms and refinerycms-blog. Make sure
the site is running, all migrations are run and you created the first refinery user.
== Installation
As there is no official release out yet, just add this repos to your projects Gemfile:
Just add the gem to your projects Gemfile:
gem 'refinerycms-wordpress-import'
Or if you want to stay on the bleeding edge:
gem 'refinerycms-wordpress-import', :git => 'git://github.com/mremolt/refinerycms-wordpress-import.git'
@@ -22,9 +31,10 @@ and run
bundle
== Usage
Importing the XML dump is done via 3 rake tasks:
Importing the XML dump is done via rake tasks:
rake wordpress:reset_blog
@@ -49,6 +59,74 @@ The task will then skip all posts that are not published.
This one combines the two previous tasks.
If you also want to import the cms part of WordPress, three more rake tasks manage
the import into RefineryCMS Pages:
rake wordpress:reset_pages
This task deletes all data from the cms tables, ensuring a clean import. Otherwise existing
pages could break the import because of duplicate IDs.
rake wordpress:import_pages[file_name]
This task imports all the WordPress pages into Refinery. The page structure (parent - child)
is preserved.
If you want to skip the draft pages, add the ONLY_PUBLISHED parameter to this task,
just like with wordpress:import_blog.
rake wordpress:import_pages[file_name] ONLY_PUBLISHED=true
If you want to clean the tables and import in one task:
rake wordpress:reset_and_import_pages[file_name]
Finally, if you want to reset and import all data including media (see below):
rake wordpress:full_import[file_name]
== Importing media files
The WP XML dump contains absolute links to media files linked inside posts, like:
www.mysite.com/wordpress/wp-content/uploads/2011/05/cv.txt
The dump does NOT contain the files itself! To get them imported, this gem downloads the files
from the given URL and imports them to refinery. So for a working media import the old site with
the media URLs must still be online.
After importing the files, this gem replaces the old links in pages and blog posts with the
new generated ones. It parses all existing records searching for the right pattern. That
means, you have to import pages and posts FIRST to get the URLs replaced.
Now to the rake tasks for media import:
rake wordpress:reset_media
This task deletes all data from the media tables (images and resources), ensuring a clean import.
rake wordpress:import_and_replace_media[file_name]
This task imports all the WordPress media into Refinery. After the import it parses all
pages and blog posts, replacing the legacy links with the current refinery ones.
If you want to clean the tables and import in one task:
rake wordpress:reset_import_and_replace_media[file_name]
== Usage on ZSH
One more hint for users of zsh (like myself):
The square brackets following the rake task need to be escaped on zsh, as they have a
special meaning there. So the syntax is:
rake wordpress:reset_and_import_blog\[file_name\]
Ugly, but it works. This is the case for all rake tasks by the way, not just mine.
== Feedback
This is still a very new gem. It manages to import my own blog and a standard WordPress 3.1 dump with some sample posts.

View File

@@ -5,7 +5,8 @@ namespace :wordpress do
task :reset_blog do
Rake::Task["environment"].invoke
%w(taggings tags blog_comments blog_categories blog_categories_blog_posts blog_posts).each do |table_name|
%w(taggings tags blog_comments blog_categories blog_categories_blog_posts
blog_posts).each do |table_name|
p "Truncating #{table_name} ..."
ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}"
end
@@ -22,12 +23,13 @@ namespace :wordpress do
only_published = ENV['ONLY_PUBLISHED'] == 'true' ? true : false
dump.posts(only_published).each(&:to_refinery)
Refinery::WordPress::Post.create_blog_page_if_necessary
ENV["MODEL"] = 'BlogPost'
Rake::Task["friendly_id:redo_slugs"].invoke
ENV.delete("MODEL")
end
desc "reset blog tables and then import blog data from a Refinery::WordPress XML dump"
task :reset_and_import_blog, :file_name do |task, params|
Rake::Task["environment"].invoke
@@ -35,4 +37,85 @@ namespace :wordpress do
Rake::Task["wordpress:import_blog"].invoke(params[:file_name])
end
desc "Reset the cms relevant tables for a clean import"
task :reset_pages do
Rake::Task["environment"].invoke
%w(page_part_translations page_translations page_parts pages).each do |table_name|
p "Truncating #{table_name} ..."
ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}"
end
end
desc "import cms data from a WordPress XML dump"
task :import_pages, :file_name do |task, params|
Rake::Task["environment"].invoke
dump = Refinery::WordPress::Dump.new(params[:file_name])
only_published = ENV['ONLY_PUBLISHED'] == 'true' ? true : false
dump.pages(only_published).each(&:to_refinery)
# After all pages are persisted we can now create the parent - child
# relationships. This is necessary, as WordPress doesn't dump the pages in
# a correct order.
dump.pages(only_published).each do |dump_page|
page = ::Page.find(dump_page.post_id)
page.parent_id = dump_page.parent_id
page.save!
end
Refinery::WordPress::Post.create_blog_page_if_necessary
ENV["MODEL"] = 'Page'
Rake::Task["friendly_id:redo_slugs"].invoke
ENV.delete("MODEL")
end
desc "reset cms tables and then import cms data from a WordPress XML dump"
task :reset_and_import_pages, :file_name do |task, params|
Rake::Task["environment"].invoke
Rake::Task["wordpress:reset_pages"].invoke
Rake::Task["wordpress:import_pages"].invoke(params[:file_name])
end
desc "Reset the media relevant tables for a clean import"
task :reset_media do
Rake::Task["environment"].invoke
%w(images resources).each do |table_name|
p "Truncating #{table_name} ..."
ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}"
end
end
desc "import media data (images and files) from a WordPress XML dump and replace target URLs in pages and posts"
task :import_and_replace_media, :file_name do |task, params|
Rake::Task["environment"].invoke
dump = Refinery::WordPress::Dump.new(params[:file_name])
attachments = dump.attachments.each(&:to_refinery)
# parse all created BlogPost and Page bodys and replace the old wordpress media uls
# with the newly created ones
attachments.each do |attachment|
attachment.replace_url
end
end
desc "reset media tables and then import media data from a WordPress XML dump"
task :reset_import_and_replace_media, :file_name do |task, params|
Rake::Task["environment"].invoke
Rake::Task["wordpress:reset_media"].invoke
Rake::Task["wordpress:import_and_replace_media"].invoke(params[:file_name])
end
desc "reset and import all data (see the other tasks)"
task :full_import, :file_name do |task, params|
Rake::Task["environment"].invoke
Rake::Task["wordpress:reset_and_import_blog"].invoke(params[:file_name])
Rake::Task["wordpress:reset_and_import_pages"].invoke(params[:file_name])
Rake::Task["wordpress:reset_import_and_replace_media"].invoke(params[:file_name])
end
end

View File

@@ -1,15 +1,16 @@
require 'nokogiri'
require "wordpress/railtie"
module Refinery
module WordPress
autoload :Author, 'wordpress/author'
autoload :Tag, 'wordpress/tag'
autoload :Category, 'wordpress/category'
autoload :Page, 'wordpress/page'
autoload :Post, 'wordpress/post'
autoload :Comment, 'wordpress/comment'
autoload :Dump, 'wordpress/dump'
autoload :Attachment, 'wordpress/attachment'
end
end
require 'nokogiri'
require 'wordpress/author'
require 'wordpress/tag'
require 'wordpress/category'
require 'wordpress/page'
require 'wordpress/post'
require 'wordpress/comment'
require 'wordpress/dump'
require "wordpress/railtie"

130
lib/wordpress/attachment.rb Normal file
View File

@@ -0,0 +1,130 @@
module Refinery
module WordPress
class Attachment
attr_reader :node
attr_reader :refinery_image
attr_reader :refinery_resource
def initialize(node)
@node = node
end
def title
node.xpath("title").text
end
def description
node.xpath("description").text
end
def file_name
url.split('/').last
end
def post_date
DateTime.parse node.xpath("wp:post_date").text
end
def url
node.xpath("wp:attachment_url").text
end
def url_pattern
url_parts = url.split('.')
extension = url_parts.pop
url_without_extension = url_parts.join('.')
/#{url_without_extension}(-\d+x\d+)?\.#{extension}/
end
def image?
url.match /\.(png|jpg|jpeg|gif)$/
end
def to_refinery
if image?
to_image
else
to_resource
end
end
def replace_url
if image?
replace_image_url
else
replace_resource_url
end
end
private
def to_image
image = ::Image.new
image.created_at = post_date
image.image_url = url
image.save!
@refinery_image = image
image
end
def to_resource
resource = ::Resource.new
resource.created_at = post_date
resource.file_url = url
resource.save!
@refinery_resource = resource
resource
end
def replace_image_url
replace_image_url_in_blog_posts
replace_image_url_in_pages
end
def replace_resource_url
replace_resource_url_in_blog_posts
replace_resource_url_in_pages
end
def replace_image_url_in_blog_posts
replace_url_in_blog_posts(refinery_image.image.url)
end
def replace_image_url_in_pages
replace_url_in_pages(refinery_image.image.url)
end
def replace_resource_url_in_blog_posts
replace_url_in_blog_posts(refinery_resource.file.url)
end
def replace_resource_url_in_pages
replace_url_in_pages(refinery_resource.file.url)
end
def replace_url_in_blog_posts(new_url)
::BlogPost.all.each do |post|
if (! post.body.empty?) && post.body.include?(url)
post.body = post.body.gsub(url_pattern, new_url)
post.save!
end
end
end
def replace_url_in_pages(new_url)
::Page.all.each do |page|
page.parts.each do |part|
if (! part.body.to_s.blank?) && part.body.include?(url)
part.body = part.body.gsub(url_pattern, new_url)
part.save!
end
end
end
end
end
end
end

View File

@@ -19,10 +19,13 @@ module Refinery
end
end
def pages
doc.xpath("//item[wp:post_type = 'page']").collect do |page|
def pages(only_published=false)
pages = doc.xpath("//item[wp:post_type = 'page']").collect do |page|
Page.new(page)
end
pages = pages.select(&:published?) if only_published
pages
end
def posts(only_published=false)
@@ -44,6 +47,12 @@ module Refinery
Category.new(category.text)
end
end
def attachments
doc.xpath("//item[wp:post_type = 'attachment']").collect do |attachment|
Attachment.new(attachment)
end
end
end
end
end

View File

@@ -23,23 +23,10 @@ module Refinery
end
def content_formatted
# WordPress doesn't export <p>-Tags, so let's run a simple_format over
# the content. As we trust ourselves, no sanatize.
formatted = simple_format(content, {}, { :sanitize => false })
# Support for SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/):
# In WordPress you can (via a plugin) enclose code in [lang][/lang]
# blocks, which are converted to a <pre>-tag with a class corresponding
# to the language.
#
# Example:
# [ruby]p "Hello World"[/ruby]
# -> <pre class="brush: ruby">p "Hello world"</pre>
formatted.gsub!(/\[(\w+)\]/, '<pre class="brush: \1">')
formatted.gsub!(/\[\/\w+\]/, '</pre>')
formatted = format_syntax_highlighter(format_paragraphs(content))
# remove all tags inside <pre> that simple_format created
# TODO: replace simple_format with a method, that ignores pre-tags
# TODO: replace format_paragraphs with a method, that ignores pre-tags
formatted.gsub!(/(<pre.*?>)(.+?)(<\/pre>)/m) do |match|
"#{$1}#{strip_tags($2)}#{$3}"
end
@@ -60,7 +47,8 @@ module Refinery
end
def parent_id
node.xpath("wp:post_parent").text.to_i
dump_id = node.xpath("wp:post_parent").text.to_i
dump_id == 0 ? nil : dump_id
end
def status
@@ -80,12 +68,40 @@ module Refinery
end
def to_refinery
page = ::Page.create!(:title => title, :created_at => post_date,
:draft => draft?, :parent_id => parent_id)
page = ::Page.create!(:id => post_id, :title => title,
:created_at => post_date, :draft => draft?)
page.parts.create(:title => 'Body', :body => content_formatted)
page
end
private
def format_paragraphs(text, html_options={})
# WordPress doesn't export <p>-Tags, so let's run a simple_format over
# the content. As we trust ourselves, no sanatize. This code is heavily
# inspired by the simple_format rails helper
text = ''.html_safe if text.nil?
start_tag = tag('p', html_options, true)
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
text.insert 0, start_tag
text.html_safe.safe_concat("</p>")
end
def format_syntax_highlighter(text)
# Support for SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/):
# In WordPress you can (via a plugin) enclose code in [lang][/lang]
# blocks, which are converted to a <pre>-tag with a class corresponding
# to the language.
#
# Example:
# [ruby]p "Hello World"[/ruby]
# -> <pre class="brush: ruby">p "Hello world"</pre>
text.gsub(/\[(\w+)\](.+?)\[\/\1\]/m, '<pre class="brush: \1">\2</pre>')
end
end
end
end

View File

@@ -61,6 +61,25 @@ module Refinery
post
end
def self.create_blog_page_if_necessary
# refinerycms wants a page at /blog, so let's make sure there is one
# taken from the original db seeds from refinery-blog
unless ::Page.where("link_url = ?", '/blog').exists?
page = ::Page.create(
:title => "Blog",
:link_url => "/blog",
:deletable => false,
:position => ((::Page.maximum(:position, :conditions => {:parent_id => nil}) || -1)+1),
:menu_match => "^/blogs?(\/|\/.+?|)$"
)
::Page.default_parts.each do |default_page_part|
page.parts.create(:title => default_page_part, :body => nil)
end
end
end
end
end
end

View File

@@ -3,9 +3,9 @@
Gem::Specification.new do |s|
s.name = "refinerycms-wordpress-import"
s.summary = "Import WordPress XML dumps into refinerycms(-blog)."
s.description = "This gem imports a WordPress XML dump into refinerycms (Page [soon], User) and refinerycms-blog (BlogPost, BlogCategory, Tag, BlogComment)"
s.version = "0.1.0"
s.date = "2011-06-03"
s.description = "This gem imports a WordPress XML dump into refinerycms (Page, User) and refinerycms-blog (BlogPost, BlogCategory, Tag, BlogComment)"
s.version = "0.3.0"
s.date = "2011-06-13"
s.authors = ['Marc Remolt']
s.email = 'marc.remolt@googlemail.com'

View File

@@ -1,3 +1,6 @@
require 'yaml'
YAML::ENGINE.yamler= 'syck'
require 'rubygems'
gemfile = File.expand_path('../../../../Gemfile', __FILE__)

View File

@@ -0,0 +1,25 @@
class CreateSeoMetaForBlog < ActiveRecord::Migration
def self.up
unless ::SeoMetum.table_exists?
create_table ::SeoMetum.table_name do |t|
t.integer :seo_meta_id
t.string :seo_meta_type
t.string :browser_title
t.string :meta_keywords
t.text :meta_description
t.timestamps
end
add_index ::SeoMetum.table_name, :id
add_index ::SeoMetum.table_name, [:seo_meta_id, :seo_meta_type]
end
end
def self.down
# can't drop the table because someone else might be using it.
end
end

View File

@@ -1,16 +1,20 @@
User.find(:all).each do |user|
user.plugins.create(:name => "refinerycms_blog",
:position => (user.plugins.maximum(:position) || -1) +1)
end
::User.find(:all).each do |user|
if user.plugins.where(:name => 'refinerycms_blog').blank?
user.plugins.create(:name => "refinerycms_blog",
:position => (user.plugins.maximum(:position) || -1) +1)
end
end if defined?(::User)
page = Page.create(
:title => "Blog",
:link_url => "/blog",
:deletable => false,
:position => ((Page.maximum(:position, :conditions => {:parent_id => nil}) || -1)+1),
:menu_match => "^/blogs?(\/|\/.+?|)$"
)
if defined?(::Page)
page = ::Page.create(
:title => "Blog",
:link_url => "/blog",
:deletable => false,
:position => ((Page.maximum(:position, :conditions => {:parent_id => nil}) || -1)+1),
:menu_match => "^/blogs?(\/|\/.+?|)$"
)
Page.default_parts.each do |default_page_part|
page.parts.create(:title => default_page_part, :body => nil)
end
::Page.default_parts.each do |default_page_part|
page.parts.create(:title => default_page_part, :body => nil)
end
end

BIN
spec/fixtures/200px-Tux.svg_.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@@ -16,7 +16,7 @@
<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
<!-- contained in this file into your site. -->
<!-- generator="WordPress/3.1.2" created="2011-05-21 12:27" -->
<!-- generator="WordPress/3.1.2" created="2011-06-06 18:42" -->
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.1/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
@@ -29,7 +29,7 @@
<title>My test blog</title>
<link>http://localhost/wordpress</link>
<description>Just another WordPress site</description>
<pubDate>Sat, 21 May 2011 12:27:19 +0000</pubDate>
<pubDate>Mon, 06 Jun 2011 18:42:09 +0000</pubDate>
<language>en</language>
<wp:wxr_version>1.1</wp:wxr_version>
<wp:base_site_url>http://localhost/wordpress</wp:base_site_url>
@@ -62,13 +62,17 @@
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>hello-world</wp:post_name>
<wp:status>publish</wp:status>
<wp:status>draft</wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>post</wp:post_type>
<wp:post_password></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<category domain="category" nicename="uncategorized"><![CDATA[Uncategorized]]></category>
<wp:postmeta>
<wp:meta_key>_edit_last</wp:meta_key>
<wp:meta_value><![CDATA[1]]></wp:meta_value>
</wp:postmeta>
<wp:comment>
<wp:comment_id>1</wp:comment_id>
<wp:comment_author><![CDATA[Mr WordPress]]></wp:comment_author>
@@ -91,14 +95,10 @@
<dc:creator>admin</dc:creator>
<guid isPermaLink="false">http://localhost/wordpress/?page_id=2</guid>
<description></description>
<content:encoded><![CDATA[This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
<blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin' caught in the rain.)</blockquote>
...or something like this:
<blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickies to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</blockquote>
<content:encoded><![CDATA[This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
<blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin' caught in the rain.)</blockquote>
...or something like this:
<blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickies to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</blockquote>
As a new WordPress user, you should go to <a href="http://localhost/wordpress/wp-admin/">your dashboard</a> to delete this page and create new pages for your content. Have fun!]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>2</wp:post_id>
@@ -107,7 +107,7 @@ As a new WordPress user, you should go to <a href="http://localhost/wordpress/wp
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>sample-page</wp:post_name>
<wp:status>publish</wp:status>
<wp:status>draft</wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>page</wp:post_type>
@@ -117,6 +117,10 @@ As a new WordPress user, you should go to <a href="http://localhost/wordpress/wp
<wp:meta_key>_wp_page_template</wp:meta_key>
<wp:meta_value><![CDATA[default]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
<wp:meta_key>_edit_last</wp:meta_key>
<wp:meta_value><![CDATA[1]]></wp:meta_value>
</wp:postmeta>
</item>
<item>
<title>Second blog post </title>
@@ -159,8 +163,12 @@ In hac habitasse platea dictumst. Nunc quis tortor sed libero hendrerit dapibu
<description></description>
<content:encoded><![CDATA[This is just a standard text page example. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin metus dolor, hendrerit sit amet, aliquet nec, posuere sed, purus. Nullam et velit iaculis odio sagittis placerat. Duis metus tellus, pellentesque ut, luctus id, egestas a, lorem. Praesent vitae mauris. Aliquam sed nulla. Sed id nunc vitae leo suscipit viverra. Proin at leo ut lacus consequat rhoncus. In hac habitasse platea dictumst. Nunc quis tortor sed libero hendrerit dapibus.
<a href="http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png"><img class="alignnone size-thumbnail wp-image-13" title="200px-Tux.svg" src="http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_-150x150.png" alt="Tux, the Linux mascot" width="150" height="150" /></a>
Integer interdum purus id erat. Duis nec velit vitae dolor mattis euismod. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse pellentesque dignissim lacus.
<a href="http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png"><img class="alignnone size-full wp-image-13" title="200px-Tux.svg" src="http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png" alt="" width="200" height="235" /></a>
Nulla semper euismod arcu. Suspendisse egestas, erat a consectetur dapibus, felis orci cursus eros, et sollicitudin purus urna et metus. Integer eget est sed nunc euismod vestibulum. Integer nulla dui, tristique in, euismod et, interdum imperdiet, enim. Mauris at lectus. Sed egestas tortor nec mi.]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>6</wp:post_id>
@@ -182,12 +190,12 @@ Nulla semper euismod arcu. Suspendisse egestas, erat a consectetur dapibus, fel
<wp:postmeta>
<wp:meta_key>_edit_last</wp:meta_key>
<wp:meta_value><![CDATA[1]]></wp:meta_value>
</wp:postmeta>
</wp:postmeta>
<wp:comment>
<wp:comment_id>2</wp:comment_id>
<wp:comment_author><![CDATA[admin]]></wp:comment_author>
<wp:comment_author_email>admin@example.com</wp:comment_author_email>
<wp:comment_author_url></wp:comment_author_url>
<wp:comment_author_url>http://www.example.com/</wp:comment_author_url>
<wp:comment_author_IP>127.0.0.1</wp:comment_author_IP>
<wp:comment_date>2011-05-21 12:26:24</wp:comment_date>
<wp:comment_date_gmt>2011-05-21 12:26:24</wp:comment_date_gmt>
@@ -201,7 +209,7 @@ Nulla semper euismod arcu. Suspendisse egestas, erat a consectetur dapibus, fel
<wp:comment_id>3</wp:comment_id>
<wp:comment_author><![CDATA[admin]]></wp:comment_author>
<wp:comment_author_email>admin@example.com</wp:comment_author_email>
<wp:comment_author_url>http://www.example.com/</wp:comment_author_url>
<wp:comment_author_url>http://www.example.com/</wp:comment_author_url>
<wp:comment_author_IP>127.0.0.1</wp:comment_author_IP>
<wp:comment_date>2011-05-21 12:26:30</wp:comment_date>
<wp:comment_date_gmt>2011-05-21 12:26:30</wp:comment_date_gmt>
@@ -253,6 +261,8 @@ In hac habitasse platea dictumst. Nunc quis tortor sed libero hendrerit dapibu
<description></description>
<content:encoded><![CDATA[This is just a standard text page example. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin metus dolor, hendrerit sit amet, aliquet nec, posuere sed, purus. Nullam et velit iaculis odio sagittis placerat.
<a href="http://localhost/wordpress/wp-content/uploads/2011/05/cv.txt">This is my CV</a>, please download!
Duis metus tellus, pellentesque ut, luctus id, egestas a, lorem. Praesent vitae mauris. Aliquam sed nulla. Sed id nunc vitae leo suscipit <strong>viverra</strong>. Proin at leo ut lacus consequat rhoncus. In hac habitasse platea dictumst. Nunc quis tortor sed libero hendrerit dapibus. Integer interdum purus id erat. Duis nec velit vitae dolor mattis euismod.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse pellentesque dignissim lacus. Nulla semper euismod arcu. Suspendisse egestas, erat a consectetur dapibus, felis orci cursus eros, et sollicitudin purus urna et metus. Integer eget est sed nunc euismod vestibulum. Integer nulla dui, tristique in, euismod et, interdum imperdiet, enim. Mauris at lectus. Sed egestas tortor nec mi.]]></content:encoded>
@@ -278,5 +288,71 @@ Class aptent taciti sociosqu ad litora torquent per conubia nostra, per incepto
<wp:meta_value><![CDATA[default]]></wp:meta_value>
</wp:postmeta>
</item>
<item>
<title>200px-Tux.svg</title>
<link>http://localhost/wordpress/?attachment_id=13</link>
<pubDate>Sun, 05 Jun 2011 15:26:51 +0000</pubDate>
<dc:creator>admin</dc:creator>
<guid isPermaLink="false">http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png</guid>
<description></description>
<content:encoded><![CDATA[]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>13</wp:post_id>
<wp:post_date>2011-06-05 15:26:51</wp:post_date>
<wp:post_date_gmt>2011-06-05 15:26:51</wp:post_date_gmt>
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>200px-tux-svg</wp:post_name>
<wp:status>inherit</wp:status>
<wp:post_parent>6</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>attachment</wp:post_type>
<wp:post_password></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<wp:attachment_url>http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png</wp:attachment_url>
<wp:postmeta>
<wp:meta_key>_wp_attached_file</wp:meta_key>
<wp:meta_value><![CDATA[2011/05/200px-Tux.svg_.png]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
<wp:meta_key>_wp_attachment_metadata</wp:meta_key>
<wp:meta_value><![CDATA[a:6:{s:5:"width";s:3:"200";s:6:"height";s:3:"235";s:14:"hwstring_small";s:22:"height='96' width='81'";s:4:"file";s:26:"2011/05/200px-Tux.svg_.png";s:5:"sizes";a:2:{s:9:"thumbnail";a:3:{s:4:"file";s:26:"200px-Tux.svg_-150x150.png";s:5:"width";s:3:"150";s:6:"height";s:3:"150";}s:14:"post-thumbnail";a:3:{s:4:"file";s:26:"200px-Tux.svg_-200x198.png";s:5:"width";s:3:"200";s:6:"height";s:3:"198";}}s:10:"image_meta";a:10:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";}}]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
<wp:meta_key>_wp_attachment_image_alt</wp:meta_key>
<wp:meta_value><![CDATA[Tux, the Linux mascot]]></wp:meta_value>
</wp:postmeta>
</item>
<item>
<title>cv</title>
<link>http://localhost/wordpress/?attachment_id=18</link>
<pubDate>Mon, 06 Jun 2011 17:27:50 +0000</pubDate>
<dc:creator>admin</dc:creator>
<guid isPermaLink="false">http://localhost/wordpress/wp-content/uploads/2011/05/cv.txt</guid>
<description></description>
<content:encoded><![CDATA[]]></content:encoded>
<excerpt:encoded><![CDATA[My CV]]></excerpt:encoded>
<wp:post_id>18</wp:post_id>
<wp:post_date>2011-06-06 17:27:50</wp:post_date>
<wp:post_date_gmt>2011-06-06 17:27:50</wp:post_date_gmt>
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>cv</wp:post_name>
<wp:status>inherit</wp:status>
<wp:post_parent>10</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>attachment</wp:post_type>
<wp:post_password></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<wp:attachment_url>http://localhost/wordpress/wp-content/uploads/2011/05/cv.txt</wp:attachment_url>
<wp:postmeta>
<wp:meta_key>_wp_attached_file</wp:meta_key>
<wp:meta_value><![CDATA[2011/05/cv.txt]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
<wp:meta_key>_wp_attachment_metadata</wp:meta_key>
<wp:meta_value><![CDATA[a:0:{}]]></wp:meta_value>
</wp:postmeta>
</item>
</channel>
</rss>

View File

@@ -0,0 +1,93 @@
require 'spec_helper'
describe Refinery::WordPress::Attachment, :type => :model do
context "an image attchment" do
let(:attachment) { test_dump.attachments.first }
specify { attachment.title.should == '200px-Tux.svg' }
# doesn't get exported atm. for some reason
specify { attachment.description.should == '' }
specify { attachment.url.should == 'http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png' }
specify { attachment.file_name.should == '200px-Tux.svg_.png' }
specify { attachment.post_date.should == DateTime.new(2011, 6, 5, 15, 26, 51) }
specify { attachment.should be_an_image }
describe "#to_refinery" do
before do
@image = attachment.to_refinery
end
it "should create an Image from the Attachment" do
@image.should be_a(Image)
end
it "should copy the attributes from Attachment" do
@image.created_at.should == attachment.post_date
@image.image.url.end_with?(attachment.file_name).should be_true
end
end
describe "#replace_url" do
let(:post) { BlogPost.first }
before do
test_dump.authors.each(&:to_refinery)
test_dump.posts.each(&:to_refinery)
@image = attachment.to_refinery
attachment.replace_url
end
specify { post.body.should_not include attachment.url }
specify { post.body.should_not include '200px-Tux.svg_-150x150.png' }
specify { post.body.should_not include 'wp-content' }
it "should replace attachment urls in the generated BlogPosts" do
post.body.should include(@image.image.url)
end
end
end
context "a file attachment" do
let(:attachment) { test_dump.attachments.last }
specify { attachment.title.should == 'cv' }
specify { attachment.url.should == 'http://localhost/wordpress/wp-content/uploads/2011/05/cv.txt' }
specify { attachment.file_name.should == 'cv.txt' }
specify { attachment.post_date.should == DateTime.new(2011, 6, 6, 17, 27, 50) }
specify { attachment.should_not be_an_image }
describe '#to_refinery' do
before do
@resource = attachment.to_refinery
end
specify { Resource.should have(1).record }
specify { @resource.should be_a(Resource) }
it "should copy the attributes from Attachment" do
@resource.created_at.should == attachment.post_date
@resource.file.url.end_with?(attachment.file_name).should be_true
end
end
describe '#replace_resource_url' do
let(:page_part) { Page.last.parts.first }
before do
test_dump.pages.each(&:to_refinery)
@resource = attachment.to_refinery
attachment.replace_url
end
specify { page_part.body.should_not include attachment.url }
specify { page_part.body.should_not include 'wp-content' }
it "should replace attachment urls in the generated BlogPosts" do
page_part.body.should include(@resource.file.url)
end
end
end
end

View File

@@ -0,0 +1,28 @@
require 'spec_helper'
describe Refinery::WordPress::Author, :type => :model do
let(:author) { test_dump.authors.first }
specify { author.login.should == 'admin' }
specify { author.email.should == 'admin@example.com' }
describe "#to_refinery" do
before do
@user = author.to_refinery
end
it "should create a User object" do
User.should have(1).record
@user.should be_a(User)
end
it "the @user should be persisted" do
@user.should be_persisted
end
it "should have copied the attributes from Refinery::WordPress::Author" do
author.login.should == @user.username
author.email.should == @user.email
end
end
end

View File

@@ -0,0 +1,29 @@
require 'spec_helper'
describe Refinery::WordPress::Category, :type => :model do
let(:category) { Refinery::WordPress::Category.new('Rant') }
describe "#name" do
specify { category.name.should == 'Rant' }
end
describe "#==" do
specify { category.should == Refinery::WordPress::Category.new('Rant') }
specify { category.should_not == Refinery::WordPress::Category.new('Tutorials') }
end
describe "#to_refinery" do
before do
@category = category.to_refinery
end
it "should create a BlogCategory" do
BlogCategory.should have(1).record
end
it "should copy the name over to the BlogCategory object" do
@category.title.should == category.name
end
end
end

View File

@@ -1,8 +1,7 @@
require 'spec_helper'
describe Refinery::WordPress::Dump, :type => :model do
let(:file_name) { File.realpath(File.join(File.dirname(__FILE__), '../../fixtures/wordpress_dump.xml')) }
let(:dump) { Refinery::WordPress::Dump.new(file_name) }
let(:dump) { test_dump }
it "should create a Dump object given a xml file" do
dump.should be_a Refinery::WordPress::Dump
@@ -18,29 +17,14 @@ describe Refinery::WordPress::Dump, :type => :model do
Refinery::WordPress::Tag.new('php'), Refinery::WordPress::Tag.new('ruby')]
end
specify { dump.tags.count == 4 }
specify { dump.tags.first.should be_a(Refinery::WordPress::Tag) }
it "should return all included tags" do
tags.each do |tag|
dump.tags.should include(tag)
end
end
context "the last tag" do
let(:tag) { dump.tags.last }
describe "#to_refinery" do
before do
@tag = tag.to_refinery
end
it "should create a ActsAsTaggableOn::Tag" do
::ActsAsTaggableOn::Tag.should have(1).record
end
it "should copy the name over to the Tag object" do
@tag.name.should == tag.name
end
end
end
end
describe "#categories" do
@@ -49,30 +33,14 @@ describe Refinery::WordPress::Dump, :type => :model do
Refinery::WordPress::Category.new('Uncategorized') ]
end
specify { dump.categories.count == 4 }
specify { dump.categories.first.should be_a(Refinery::WordPress::Category) }
it "should return all included categories" do
categories.each do |cat|
dump.categories.should include(cat)
end
end
context "the last category" do
let(:category) { dump.categories.last }
describe "#to_refinery" do
before do
@category = category.to_refinery
end
it "should create a BlogCategory" do
BlogCategory.should have(1).record
end
it "should copy the name over to the BlogCategory object" do
@category.title.should == category.name
end
end
end
end
describe "#pages" do
@@ -80,43 +48,10 @@ describe Refinery::WordPress::Dump, :type => :model do
dump.pages.should have(3).pages
end
context "the About me page" do
let(:page) { dump.pages.last }
specify { dump.pages.first.should be_a(Refinery::WordPress::Page) }
it { page.title.should == 'About me' }
it { page.content.should include('Lorem ipsum dolor sit') }
it { page.creator.should == 'admin' }
it { page.post_date.should == DateTime.new(2011, 5, 21, 12, 25, 42) }
it { page.post_id.should == 10 }
it { page.parent_id.should == 8 }
it { page.should == dump.pages.last }
it { page.should_not == dump.pages.first }
describe "#to_refinery" do
include ::ActionView::Helpers::TagHelper
include ::ActionView::Helpers::TextHelper
before do
# "About me" has a parent page with id 8 in the XML dump,
# would otherwise fails creation
Page.create! :id => 8, :title => 'About'
@count = Page.count
@page = page.to_refinery
end
it "should create a Page object" do
Page.should have(@count + 1).record
end
it "should copy the attributes from Refinery::WordPress::Page" do
@page.title.should == page.title
@page.draft.should == page.draft?
@page.created_at.should == page.post_date
@page.parts.first.body.should == "#{simple_format(page.content)}"
end
end
it "should return only published pages with only_published=true" do
dump.pages(true).should have(2).pages
end
end
@@ -125,32 +60,7 @@ describe Refinery::WordPress::Dump, :type => :model do
dump.authors.should have(1).author
end
context "the first author" do
let(:author) { dump.authors.first }
it { author.login.should == 'admin' }
it { author.email.should == 'admin@example.com' }
describe "#to_refinery" do
before do
@user = author.to_refinery
end
it "should create a User object" do
User.should have(1).record
@user.should be_a(User)
end
it "the @user should be persisted" do
@user.should be_persisted
end
it "should have copied the attributes from Refinery::WordPress::Author" do
author.login.should == @user.username
author.email.should == @user.email
end
end
end
specify { dump.authors.first.should be_a(Refinery::WordPress::Author) }
end
describe "#posts" do
@@ -158,121 +68,18 @@ describe Refinery::WordPress::Dump, :type => :model do
dump.posts.should have(3).posts
end
context "the last post" do
let(:post) { dump.posts.last }
specify { dump.posts.first.should be_a(Refinery::WordPress::Post) }
it { post.title.should == 'Third blog post' }
it { post.content.should include('Lorem ipsum dolor sit') }
it { post.creator.should == 'admin' }
it { post.post_date.should == DateTime.new(2011, 5, 21, 12, 24, 45) }
it { post.post_id.should == 6 }
it { post.parent_id.should == 0 }
it { post.status.should == 'publish' }
it { post.should == dump.posts.last }
it { post.should_not == dump.posts.first }
describe "#categories" do
it { post.categories.should have(1).category }
it { post.categories.first.should == Refinery::WordPress::Category.new('Rant') }
end
describe "#tags" do
it { post.tags.should have(3).tags }
it { post.tags.should include(Refinery::WordPress::Tag.new('css')) }
it { post.tags.should include(Refinery::WordPress::Tag.new('html')) }
it { post.tags.should include(Refinery::WordPress::Tag.new('php')) }
end
it { post.tag_list.should == 'css,html,php' }
describe "#comments" do
it "should return all attached comments" do
post.comments.should have(2).comments
end
context "the last comment" do
let(:comment) { post.comments.last }
it { comment.author.should == 'admin' }
it { comment.email.should == 'admin@example.com' }
it { comment.url.should == 'http://www.example.com/' }
it { comment.date.should == DateTime.new(2011, 5, 21, 12, 26, 30) }
it { comment.content.should include('Another one!') }
it { comment.should be_approved }
it { comment.should == post.comments.last }
describe "#to_refinery" do
before do
@comment = comment.to_refinery
end
it "should not save the comment, only initialize it" do
BlogComment.should have(0).records
@comment.should be_new_record
end
it "should copy the attributes from Refinery::WordPress::Comment" do
@comment.name.should == comment.author
@comment.email.should == comment.email
@comment.body.should == comment.content
@comment.state.should == 'approved'
@comment.created_at.should == comment.date
end
end
end
end
describe "#to_refinery" do
before do
@user = User.create! :username => 'admin', :email => 'admin@example.com',
:password => 'password', :password_confirmation => 'password'
end
context "with a unique title" do
before do
@post = post.to_refinery
end
it { BlogPost.should have(1).record }
it "should copy the attributes from Refinery::WordPress::Post" do
@post.title.should == post.title
@post.body.should == post.content_formatted
@post.draft.should == post.draft?
@post.published_at.should == post.post_date
@post.created_at.should == post.post_date
@post.author.username.should == post.creator
end
it "should assign a category for each Refinery::WordPress::Category" do
@post.categories.should have(post.categories.count).records
end
it "should assign a comment for each Refinery::WordPress::Comment" do
@post.comments.should have(post.comments.count).records
end
end
context "with a duplicate title" do
before do
BlogPost.create! :title => post.title, :body => 'Lorem', :author => @user
@post = post.to_refinery
end
it { BlogPost.should have(2).records }
it "should create the BlogPost with #post_id attached" do
@post.title.should == "#{post.title}-#{post.post_id}"
end
end
end
it "should return only published posts with only_published=true" do
dump.posts(true).should have(2).posts
end
end
describe "#attachments" do
it "should return all attachments" do
dump.attachments.should have(2).attachments
end
specify { dump.attachments.first.should be_a(Refinery::WordPress::Attachment) }
end
end

View File

@@ -0,0 +1,105 @@
require 'spec_helper'
describe Refinery::WordPress::Page, :type => :model do
let(:dump) { test_dump }
let(:page) { test_dump.pages.last }
specify { page.title.should == 'About me' }
specify { page.content.should include('Lorem ipsum dolor sit') }
specify { page.creator.should == 'admin' }
specify { page.post_date.should == DateTime.new(2011, 5, 21, 12, 25, 42) }
specify { page.post_id.should == 10 }
specify { page.parent_id.should == 8 }
specify { page.should == dump.pages.last }
specify { page.should_not == dump.pages.first }
describe "#to_refinery" do
include ::ActionView::Helpers::TagHelper
include ::ActionView::Helpers::TextHelper
before do
# "About me" has a parent page with id 8 in the XML dump,
# would otherwise fails creation
Page.create! :id => 8, :title => 'About'
@count = Page.count
@page = page.to_refinery
end
it "should create a Page object" do
Page.should have(@count + 1).record
end
it "should copy the attributes from Refinery::WordPress::Page" do
@page.title.should == page.title
@page.draft.should == page.draft?
@page.created_at.should == page.post_date
@page.parts.first.body.should == "#{simple_format(page.content)}"
end
end
describe "#format_paragraphs" do
let(:sample_text) do
text = <<-EOT
This is sample text.
Even more text.
But this time no paragraph.
EOT
end
before do
@result = page.send(:format_paragraphs, sample_text)
end
it "should add paragraphs to the sample text" do
@result.should include('<p>')
@result.should include('</p>')
end
end
describe "#format_syntax_highlighter" do
let(:sample_text) do
text = <<-EOT
This is sample text.
[ruby]
p "this is ruby code"
[/ruby]
This is more sample text.
EOT
end
before do
@result = page.send(:format_syntax_highlighter, sample_text)
end
it "should reformat the [ruby] tag to a pre with correct class" do
@result.should match(/<pre class="brush: ruby">/)
@result.should include('</pre>')
end
context "without correct code tags" do
let(:sample_text) do
text = <<-EOT
This is sample text.
[ruby]
p "this is ruby code"
[/php]
This is more sample text.
EOT
end
it "should not reformat the [ruby] tag" do
@result.should == sample_text
end
end
end
end

View File

@@ -0,0 +1,116 @@
require 'spec_helper'
describe Refinery::WordPress::Post, :type => :model do
let(:post) { test_dump.posts.last }
specify { post.title.should == 'Third blog post' }
specify { post.content.should include('Lorem ipsum dolor sit') }
specify { post.content_formatted.should include('Lorem ipsum dolor sit') }
specify { post.creator.should == 'admin' }
specify { post.post_date.should == DateTime.new(2011, 5, 21, 12, 24, 45) }
specify { post.post_id.should == 6 }
specify { post.parent_id.should == nil }
specify { post.status.should == 'publish' }
specify { post.should == test_dump.posts.last }
specify { post.should_not == test_dump.posts.first }
describe "#categories" do
specify { post.categories.should have(1).category }
specify { post.categories.first.should == Refinery::WordPress::Category.new('Rant') }
end
describe "#tags" do
specify { post.tags.should have(3).tags }
specify { post.tags.should include(Refinery::WordPress::Tag.new('css')) }
specify { post.tags.should include(Refinery::WordPress::Tag.new('html')) }
specify { post.tags.should include(Refinery::WordPress::Tag.new('php')) }
end
specify { post.tag_list.should == 'css,html,php' }
describe "#comments" do
it "should return all attached comments" do
post.comments.should have(2).comments
end
context "the last comment" do
let(:comment) { post.comments.last }
specify { comment.author.should == 'admin' }
specify { comment.email.should == 'admin@example.com' }
specify { comment.url.should == 'http://www.example.com/' }
specify { comment.date.should == DateTime.new(2011, 5, 21, 12, 26, 30) }
specify { comment.content.should include('Another one!') }
specify { comment.should be_approved }
specify { comment.should == post.comments.last }
describe "#to_refinery" do
before do
@comment = comment.to_refinery
end
it "should not save the comment, only initialize it" do
BlogComment.should have(0).records
@comment.should be_new_record
end
it "should copy the attributes from Refinery::WordPress::Comment" do
@comment.name.should == comment.author
@comment.email.should == comment.email
@comment.body.should == comment.content
@comment.state.should == 'approved'
@comment.created_at.should == comment.date
end
end
end
end
describe "#to_refinery" do
before do
@user = User.create! :username => 'admin', :email => 'admin@example.com',
:password => 'password', :password_confirmation => 'password'
end
context "with a unique title" do
before do
@post = post.to_refinery
end
specify { BlogPost.should have(1).record }
it "should copy the attributes from Refinery::WordPress::Post" do
@post.title.should == post.title
@post.body.should == post.content_formatted
@post.draft.should == post.draft?
@post.published_at.should == post.post_date
@post.created_at.should == post.post_date
@post.author.username.should == post.creator
end
it "should assign a category for each Refinery::WordPress::Category" do
@post.categories.should have(post.categories.count).records
end
it "should assign a comment for each Refinery::WordPress::Comment" do
@post.comments.should have(post.comments.count).records
end
end
context "with a duplicate title" do
before do
BlogPost.create! :title => post.title, :body => 'Lorem', :author => @user
@post = post.to_refinery
end
specify { BlogPost.should have(2).records }
it "should create the BlogPost with #post_id attached" do
@post.title.should == "#{post.title}-#{post.post_id}"
end
end
end
end

View File

@@ -0,0 +1,30 @@
require 'spec_helper'
describe Refinery::WordPress::Tag, :type => :model do
let(:tag) { Refinery::WordPress::Tag.new('ruby') }
describe "#name" do
specify { tag.name.should == 'ruby' }
end
describe "#==" do
specify { tag.should == Refinery::WordPress::Tag.new('ruby') }
specify { tag.should_not == Refinery::WordPress::Tag.new('php') }
end
describe "#to_refinery" do
before do
@tag = tag.to_refinery
end
it "should create a ActsAsTaggableOn::Tag" do
::ActsAsTaggableOn::Tag.should have(1).record
end
it "should copy the name over to the Tag object" do
@tag.name.should == tag.name
end
end
end

12
spec/support/fakeweb.rb Normal file
View File

@@ -0,0 +1,12 @@
require "fakeweb"
FakeWeb.allow_net_connect = false
# Simulating download of wordpress file attachments. The dump expects the files
# to be at the given URLs
FakeWeb.register_uri(:get,
"http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png",
:body => File.new('spec/fixtures/200px-Tux.svg_.png').read,
:content_type => "image/png")
FakeWeb.register_uri(:get, "http://localhost/wordpress/wp-content/uploads/2011/05/cv.txt", :body => "Hello World!", :content_type => "text/plain")

11
spec/support/helpers.rb Normal file
View File

@@ -0,0 +1,11 @@
module Refinery::WordPress::SpecHelpers
def test_dump
file_name = File.realpath(File.join(File.dirname(__FILE__), '../fixtures/wordpress_dump.xml'))
Refinery::WordPress::Dump.new(file_name)
end
end
RSpec.configure do |config|
config.include Refinery::WordPress::SpecHelpers
end