Class: WordPress

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cucumber-wordpress.rb,
lib/cucumber-wordpress/path_to.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ABSPATHObject

Returns the value of attribute ABSPATH.



18
19
20
# File 'lib/cucumber-wordpress.rb', line 18

def ABSPATH
  @ABSPATH
end

#configObject

Returns the value of attribute config.



17
18
19
# File 'lib/cucumber-wordpress.rb', line 17

def config
  @config
end

#DB_CHARSETObject

Returns the value of attribute DB_CHARSET.



18
19
20
# File 'lib/cucumber-wordpress.rb', line 18

def DB_CHARSET
  @DB_CHARSET
end

#DB_COLLATEObject

Returns the value of attribute DB_COLLATE.



18
19
20
# File 'lib/cucumber-wordpress.rb', line 18

def DB_COLLATE
  @DB_COLLATE
end

#DB_HOSTObject

Returns the value of attribute DB_HOST.



18
19
20
# File 'lib/cucumber-wordpress.rb', line 18

def DB_HOST
  @DB_HOST
end

#DB_NAMEObject

Returns the value of attribute DB_NAME.



18
19
20
# File 'lib/cucumber-wordpress.rb', line 18

def DB_NAME
  @DB_NAME
end

#DB_PASSWORDObject

Returns the value of attribute DB_PASSWORD.



18
19
20
# File 'lib/cucumber-wordpress.rb', line 18

def DB_PASSWORD
  @DB_PASSWORD
end

#DB_USERObject

Returns the value of attribute DB_USER.



18
19
20
# File 'lib/cucumber-wordpress.rb', line 18

def DB_USER
  @DB_USER
end

#original_contentsObject

Returns the value of attribute original_contents.



17
18
19
# File 'lib/cucumber-wordpress.rb', line 17

def original_contents
  @original_contents
end

#passwordsObject

Returns the value of attribute passwords.



17
18
19
# File 'lib/cucumber-wordpress.rb', line 17

def passwords
  @passwords
end

#TABLE_PREFIXObject

Returns the value of attribute TABLE_PREFIX.



18
19
20
# File 'lib/cucumber-wordpress.rb', line 18

def TABLE_PREFIX
  @TABLE_PREFIX
end

#tablesObject

Returns the value of attribute tables.



17
18
19
# File 'lib/cucumber-wordpress.rb', line 17

def tables
  @tables
end

#WEBHOSTObject

Returns the value of attribute WEBHOST.



18
19
20
# File 'lib/cucumber-wordpress.rb', line 18

def WEBHOST
  @WEBHOST
end

Class Method Details

.method_missing(method, *args, &block) ⇒ Object



13
14
15
# File 'lib/cucumber-wordpress.rb', line 13

def self.method_missing(method, *args, &block)
  self.instance.send(method, *args, &block)
end

Instance Method Details

#configure(data) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cucumber-wordpress.rb', line 20

def configure(data)
  @config = data
  @ABSPATH = data['ABSPATH'].to_s
  @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
  @tables = %w[comments
               links
               options
               postmeta
               posts
               term_relationships
               term_taxonomy
               terms
               usermeta
               users].map{|t|@TABLE_PREFIX+t}
end

#create_dbObject



48
49
50
51
# File 'lib/cucumber-wordpress.rb', line 48

def create_db
  mysql.query("create database #{@DB_NAME} character set = #{@DB_CHARSET}#{@DB_COLLATE.empty? ? '' : " collate = #{@DB_COLLATE}"}")
  mysql.query("use #{@DB_NAME}")
end

#drop_dbObject



53
54
55
# File 'lib/cucumber-wordpress.rb', line 53

def drop_db
  mysql.query("drop database if exists #{@DB_NAME}")
end

#get_post_id(title) ⇒ Object



99
100
101
# File 'lib/cucumber-wordpress.rb', line 99

def get_post_id(title)
  WordPress.mysql.query(%Q'SELECT ID FROM #{WordPress.TABLE_PREFIX}posts WHERE post_title="#{title}"').fetch_row.first.to_i
end

#majorObject



117
118
119
120
# File 'lib/cucumber-wordpress.rb', line 117

def major
  version
  @major
end

#minorObject



121
122
123
124
# File 'lib/cucumber-wordpress.rb', line 121

def minor
  version
  @minor
end

#mysqlObject



43
44
45
46
# File 'lib/cucumber-wordpress.rb', line 43

def mysql
  @mysql ||= Mysql::new(@DB_HOST, @DB_USER, @DB_PASSWORD)
  @mysql
end

#patchObject



125
126
127
128
# File 'lib/cucumber-wordpress.rb', line 125

def patch
  version
  @patch
end

#path_to(page_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cucumber-wordpress/path_to.rb', line 4

def path_to(page_name)
  partial = case page_name
  when /^homepage$/
    '/'
  when /^login page$/
    '/wp-login.php'
  when /^admin dashboard$/
    '/wp-admin/'
  when /^new post$/
    '/wp-admin/post-new.php'
  when /^media library$/
    "/wp-admin/upload.php"
  when /^manage themes$/
    '/wp-admin/themes.php'
  when /^plugins$/
    '/wp-admin/plugins.php'
  when /^new user$/
    '/wp-admin/user-new.php'
  when /^new page$/
    case major
    when 2
      '/wp-admin/page-new.php'
    when 3
      '/wp-admin/post-new.php?post_type=page'
    else
      raise
    end
  when /^(post|page) "(.+?)"$/
    WordPress.php("echo get_permalink(#{get_post_id($2)})")
  when /^edit (post|page) "(.+?)"$/
    "/wp-admin/#{$1}.php?action=edit&post=#{get_post_id($2)}"
  else
    return nil
  end
  URI::join("http://#{@WEBHOST}/", partial).to_s
end

#php(code) ⇒ Object



103
104
105
# File 'lib/cucumber-wordpress.rb', line 103

def php code
  `php -r '$_SERVER["SERVER_SOFTWARE"]=""; $_SERVER["REQUEST_URI"]="/"; include "#{WordPress.ABSPATH}/wp-load.php"; #{code};' 2>/dev/null`
end

#reset_configObject



84
85
86
87
# File 'lib/cucumber-wordpress.rb', line 84

def reset_config
  FileUtils.rm File.join(@ABSPATH,'wp-config.php')
  FileUtils.mv File.join(@ABSPATH,'.wp-config.php'), File.join(@ABSPATH,'wp-config.php') if @has_config
end

#reset_dbObject



89
90
91
92
93
94
95
96
97
# File 'lib/cucumber-wordpress.rb', line 89

def reset_db
  @original_contents.nil? ? nil : @original_contents.each_pair do |table,contents|
    mysql.query("delete from #{table}")
    contents.each do |row|
      values = row.map{|v|"#{v.nil? ? 'null' : "'"+Mysql.escape_string(v)+"'"}"}.join(', ')
      mysql.query("insert into #{table} values (#{values})")
    end
  end
end

#versionObject



107
108
109
110
111
112
113
114
115
# File 'lib/cucumber-wordpress.rb', line 107

def version
  return @version if @version
  @version = php 'global $wp_version; echo $wp_version;'
  @version.match(/^(\d+)\.(\d+)(:?\.(\d+))?$/)
  @major = $1.to_i
  @minor = $2.to_i
  @patch = $3.to_i
  @version
end

#write_config {|extra_config| ... } ⇒ Object

Yields:

  • (extra_config)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cucumber-wordpress.rb', line 57

def write_config
  # Copy production DB elsewhere
  @has_config = File.exist? File.join(@ABSPATH,'wp-config.php')
  FileUtils.cp File.join(@ABSPATH,'wp-config.php'), File.join(@ABSPATH,'.wp-config.php') if @has_config

  extra_config = ''
  yield extra_config if block_given?

  # Write our own
  open(File.join(@ABSPATH,'wp-config.php'),'w+') do |f|
    f.write <<HERE
<?php
define('WP_DEBUG', true);
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}');
$table_prefix  = '#{@TABLE_PREFIX}';
if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/');
#{extra_config}
require_once(ABSPATH . 'wp-settings.php');
HERE
  end
end