Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit decc08effa6765b86afdd388961c3c158513a133 @holizz holizz committed Nov 19, 2009
Showing with 191 additions and 0 deletions.
  1. +13 −0 LICENCE
  2. +14 −0 Rakefile
  3. +93 −0 lib/cucumber-wordpress.rb
  4. +71 −0 lib/cucumber-wordpress/steps.rb
13 LICENCE
@@ -0,0 +1,13 @@
+Copyright © 2009, The Dextrous Web
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
@@ -0,0 +1,14 @@
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.version = "1.0"
+ gem.name = "cucumber-wordpress"
+ gem.summary = %Q{Environment setup and step definitions for testing WordPress with Cucumber}
+ gem.email = "tom@thedextrousweb.com"
+ gem.homepage = "http://github.com/dxw/cucumber-wordpress"
+ gem.authors = ["Tom Adams"]
+ gem.add_dependency "cucumber"
+ end
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
+end
@@ -0,0 +1,93 @@
+require 'mysql'
+#Mysql::Result.send(:include, Enumerable)
+
+# RSpec
+require 'spec/expectations'
+require 'spec/mocks'
+require 'spec/mocks/example_methods'
+
+#require 'test/unit/assertions'
+#World(Test::Unit::Assertions)
+
+class WordPress
+ def self.configure(data)
+ $WEBHOST = data['WEBHOST'].to_s
+ $DB_NAME = data['DB_NAME'].to_s
+ $DB_USER = data['DB_USER'].to_s
+ $DB_PASSWORD = data['DB_PASSWORD'].to_s
+ $DB_HOST = data['DB_HOST'].to_s
+ $DB_CHARSET = data['DB_CHARSET'].to_s
+ $DB_COLLATE = data['DB_COLLATE'].to_s
+ $TABLE_PREFIX = data['TABLE_PREFIX'].to_s
+ end
+
+ def self.create_db
+ mysql = Mysql::new($DB_HOST, $DB_USER, $DB_PASSWORD)
+ mysql.query("create database if not exists #{$DB_NAME} character set = #{$DB_CHARSET}#{$DB_COLLATE.present? ? " collate = #{$DB_COLLATE}" : ''}")
+ $mysql = Mysql::new($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME)
+ end
+
+ def self.drop_db
+ $mysql.query("drop database if exists #{$DB_NAME}")
+ end
+
+ def self.write_config
+ # Copy production DB elsewhere
+ @@has_config = File.exist? 'wp-config.php'
+ FileUtils.cp 'wp-config.php', '.wp-config.php' if @@has_config
+
+ # Write our own
+ open('wp-config.php','w+') do |f|
+ f.write <<HERE
+<?php
+define('DB_NAME', '#{$DB_NAME}');
+define('DB_USER', '#{$DB_USER}');
+define('DB_PASSWORD', '#{$DB_PASSWORD}');
+define('DB_HOST', '#{$DB_HOST}');
+define('DB_CHARSET', '#{$DB_CHARSET}');
+define('DB_COLLATE', '#{$DB_COLLATE}');
+define('AUTH_KEY', 'put your unique phrase here');
+define('SECURE_AUTH_KEY', 'put your unique phrase here');
+define('LOGGED_IN_KEY', 'put your unique phrase here');
+define('NONCE_KEY', 'put your unique phrase here');
+$table_prefix = '#{$TABLE_PREFIX}';
+define ('WPLANG', '');
+if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/');
+require_once(ABSPATH . 'wp-settings.php');
+HERE
+ end
+ end
+
+ def self.reset_config
+ FileUtils.rm 'wp-config.php'
+ FileUtils.mv '.wp-config.php', 'wp-config.php' if @@has_config
+ end
+
+ def self.reset_db
+ $original_contents.nil? ? nil : $original_contents.each_pair do |table,contents|
+ $mysql.query("delete from #{$TABLE_PREFIX}#{table}")
+ contents.each do |row|
+ values = row.map{|v|"#{v.nil? ? 'null' : "'"+Mysql.escape_string(v)+"'"}"}.join(', ')
+ $mysql.query("insert into #{$TABLE_PREFIX}#{table} values (#{values})")
+ end
+ end
+ end
+
+ def self.path_to(page_name)
+ partial = case page_name
+ when /^homepage$/
+ '/'
+ when /^login page$/
+ '/wp-login.php'
+ when /^admin dashboard$/
+ '/wp-admin/'
+ when /^upload new consultation$/
+ '/wp-admin/consultation-new.php'
+ when /^media library$/
+ "/wp-admin/upload.php"
+ else
+ raise "Can't find mapping from \"#{page_name}\" to a path.\n"
+ end
+ URI::join("http://#{$WEBHOST}/", partial)
+ end
+end
@@ -0,0 +1,71 @@
+Given /^WordPress is installed$/ do
+ visit path_to 'homepage'
+ title = 'A Very Boring Test Title'
+ if response.body.include? '<title>WordPress &rsaquo; Installation</title>'
+ fill_in('Blog Title', :with => title)
+ fill_in('Your E-mail', :with => 'test@wordpress.org')
+ click_button('Install WordPress')
+ $passwords = {'admin' => response.body.match(%r[<td><code>(.+)</code><br />])[1]}
+ end
+ visit path_to 'homepage'
+ unless response.body.include? "<title> #{title}</title>"
+ raise Exception
+ end
+
+ # Take this so we can reset the DB before each scenario
+ $original_contents = {}
+ %w[comments
+ links
+ options
+ postmeta
+ posts
+ term_relationships
+ term_taxonomy
+ terms
+ usermeta
+ users].each do |table|
+ $original_contents[table] = $mysql.query("select * from #{$TABLE_PREFIX}#{table}").map{|row|row}
+ end
+end
+
+Given /^I am logged in as "([^\"]*)"$/ do |user|
+ visit path_to 'login page'
+ fill_in('Username', :with => user)
+ fill_in('Password', :with => $passwords[user])
+ click_button('Log In')
+end
+
+Given /^plugin "([^\"]*)" is (enabled|disabled)$/ do |able|
+ Given 'I am logged in as "admin"'
+ visit path_to 'admin dashboard'
+ click_link('Plugins')
+ link = '//a[contains(@href,"consultationxml")]'
+ if dom.xpath("#{link}/child::text()").any?{|t|t.to_s == 'Activate'}
+ if able == 'enabled'
+ click_link_within("#{link}/..",'Activate')
+ else
+ click_link_within("#{link}/..",'Deactivate')
+ end
+ end
+end
+
+Then /^there should be (\d+) posts$/ do |count|
+ $mysql.query("select count(*) from #{$TABLE_PREFIX}posts where ID != 1 and post_type = 'post'").fetch_row.first.to_i.should == count.to_i
+end
+
+Then /^there should be (\d+) categories$/ do |count|
+ # Two initial categories, which we won't count: Uncategorized and Blogroll
+ $mysql.query("select count(*) from #{$TABLE_PREFIX}terms where term_id > 2").fetch_row.first.to_i.should == count.to_i
+end
+
+Then /^there should be a category called "([^\"]*)"$/ do |category|
+ $mysql.query("select count(*) > 0 from #{$TABLE_PREFIX}terms where name = '#{Mysql.escape_string(category)}' or slug = '#{Mysql.escape_string(category)}'").fetch_row.first.to_i.should == 1
+end
+
+Then /^there should be a post called "([^\"]*)"$/ do |post|
+ $mysql.query("select count(*) > 0 from #{$TABLE_PREFIX}posts where post_title = '#{Mysql.escape_string(post)}' or post_name = '#{Mysql.escape_string(post)}'").fetch_row.first.to_i.should == 1
+end
+
+Then /^there should be a post called "([^\"]*)" in the "([^\"]*)" category$/ do |post, category|
+ $mysql.query("select count(*) > 0 from #{$TABLE_PREFIX}terms join #{$TABLE_PREFIX}term_relationships join #{$TABLE_PREFIX}posts where (post_title = '#{Mysql.escape_string(post)}' or post_name = '#{Mysql.escape_string(post)}') and (name = '#{Mysql.escape_string(category)}' or slug = '#{Mysql.escape_string(category)}')").fetch_row.first.to_i.should == 1
+end

0 comments on commit decc08e

Please sign in to comment.