Compare commits

..

7 Commits
dsl ... master

Author SHA1 Message Date
John Bintz
b2763bd8ce flush cache after updating fixtures 2010-02-12 19:36:22 -05:00
John Bintz
f391a933bd fix for adding categories of multiple levels 2010-02-12 18:47:48 -05:00
John Bintz
3ed7e6a475 logic bug in build() 2010-02-12 18:39:10 -05:00
John Bintz
5a01664357 update for new version 2010-01-23 11:35:36 -05:00
John Bintz
98db430c2b remove unnecessary var_dump 2010-01-23 11:32:52 -05:00
John Bintz
43b073f67f fix category generation 2010-01-23 11:32:34 -05:00
John Bintz
3ccd43983e clean up some wordpress stuff 2010-01-18 10:37:45 -05:00
7 changed files with 194 additions and 15 deletions

View File

@ -9,6 +9,11 @@ This allows you to create post, category, and options configurations to test spe
Clone the repository into your `wp-content/plugins` directory. Activate it like a normal plugin.
### From WordPress Plugins
Download the plugin from the WordPress plugins repository: http://wordpress.org/extend/plugins/post-fixtures/. Activate it like any other plugin.
**Post Fixtures** requires PHP 5 or above.
## Using
**Post Fixtures** places a new menu item under *Tools* called *Post Fixtures*.

139
Readme.txt Normal file
View File

@ -0,0 +1,139 @@
=== Post Fixtures ===
Contributors: johncoswell
Donate link: http://www.coswellproductions.com/wordpress/wordpress-plugins
Tags: admin, developer, database
Requires at least: 2.8
Tested up to: 2.9.1
Stable tag: 0.2.3
Post Fixtures let you quickly tear down and set up test environments within your development WordPress environment.
== Description ==
Post Fixtures let you quickly tear down and set up test environments within your development WordPress environment.
This allows you to create post, category, and options configurations to test specific issues and features of your themes and plugins.
**Post Fixtures** places a new menu item under *Tools* called *Post Fixtures*.
When you visit the page, you'll see a large textarea in which to copy and paste your JSON fixture data.
Submitting the form with valid JSON data will cause your posts and categories to be deleted & overwritten,
and any options provided to be overwritten or deleted.
As of the current release on GitHub, the following WordPress features are supported:
* Posts
* Most post data found in the `posts` table
* Post metadata with serialization
* Categories
* Tags
* Blog options with serialization and deletion
== Installation ==
Activate it like any other plugin. **Post Fixtures** requires PHP 5 or above.
== Frequently Asked Questions ==
= What are the data formats accepted? =
**JSON**
<pre>
{
"posts": [
{
"post_date": "2010-01-01 9:00am",
"post_title": "This is a sample post",
"post_content": "This is the post's content",
"categories": [ "Top Level 1/Sub Category 1", "Top Level 2/Sub Category 2" ],
"metadata": {
"field-1": "value 1",
"field-2": {
"a_complex": "field",
"with": [
"lots", "of", "nested", "datatypes"
]
}
},
"tags": "tag 1,tag 2"
},
{
"post_date": "2010-01-01 10:00am",
"post_title": "This is the second sample post",
"post_content": "This is the second post's content",
"categories": [ "Top Level 1/Sub Category 2", "Top Level 2/Sub Category 2" ]
}
],
"options": {
"an-option-to-set": "simple-string",
"an-option-to-serialize": {
"this": "is a hash"
},
"an-option-to-delete": false
}
}
</pre>
**PHP**
<pre>
// build an object immediately, and get the new post's ID
$post_id = $builder->post('This is a sample post')
->date('2010-01-01 9:00am')
->content("This is the post's content")
->categories("Top Level 1/Sub Category 1,Top Level 2/Sub Category 2")
->metadata('field-1', 'value-1')
->metadata('field-2', array(
'a_complex' => 'field',
'with' => array(
'lots', 'of', 'nested', 'datatypes'
)
))
->tags('tag 1,tag 2')->build();
// build and object at the end, if order doesn't matter
$builder->post('This is the second sample post')
->date('2010-01-01 10:00am')
->content("This is the second post's content")
->categories("Top Level 1/Sub Category 2,Top Level 2/Sub Category 2")->defer();
// convenience wrapper around options setting
$builder->option('an-option-to-set', 'simple-string')
->option('an-option-to-serialize', array('this' => 'is a hash'))
->option('an-option-to-delete', false);
</pre>
== Changelog ==
= 0.2.3 =
* Clear cache after running fixtures. Needed for persistent caches.
= 0.2.2 =
* Bugfix for immediate build generation and sub-category addition when adding posts.
= 0.2.1 =
* Bugfix for multiple generated nested categories.
= 0.2 =
* Initial release on WordPress Plugins site.
== Upgrade Notice ==
= 0.2.3 =
* Clear cache after running fixtures. Needed for persistent caches.
= 0.2.2 =
* Bugfix for immediate build generation and sub-category addition when adding posts.
= 0.2.1 =
* Bugfix for multiple generated nested categories.
= 0.2 =
* Initial release.

View File

@ -84,7 +84,9 @@ class FixtureBuilder {
function build() {
if (!empty($this->current_object)) {
$this->ensure_type($this->current_object['type']);
return $this->{"build_{$this->current_object['type']}"}($this->current_object);
$result = $this->{"build_{$this->current_object['type']}"}($this->current_object);
unset($this->current_object);
return $result;
}
}
@ -129,10 +131,12 @@ class FixtureBuilder {
$category_ids = array();
if (isset($obj['post']['categories'])) {
$category_ids_by_slug = array();
$category_ids = array();
foreach ($obj['post']['categories'] as $category) {
$category_ids_by_slug = array_merge($category_ids_by_slug, PostFixtures::create_category($category));
$created_categories = PostFixtures::create_category($category);
$category_ids[] = array_pop(array_values($created_categories));
$category_ids_by_slug = array_merge($category_ids_by_slug, $created_categories);
}
$category_ids = array_values($category_ids_by_slug);
PostFixtures::set_post_categories($post_id, $category_ids);
}

View File

@ -5,6 +5,7 @@
*/
class PostFixtures {
var $messages;
var $created_categories = array();
/**
* Initialize the plugin.
@ -54,6 +55,7 @@ class PostFixtures {
} else {
$this->handle_json($info['data']);
}
wp_cache_flush();
}
}
@ -262,7 +264,15 @@ class PostFixtures {
$joined_nodes = array();
foreach ($nodes as $node) {
$joined_nodes[] = $node;
$parent = $category_ids_by_slug[implode('/', $joined_nodes)] = wp_insert_category(array('cat_name' => $node, 'category_nicename' => $node, 'category_parent' => $parent));
$key = implode('/', $joined_nodes);
if (isset($this->created_categories[$key])) {
$parent = $this->created_categories[$key];
} else {
$parent = wp_insert_category(array('cat_name' => $node, 'category_nicename' => $node, 'category_parent' => $parent));
$this->created_categories[$key] = $parent;
}
$category_ids_by_slug[$key] = $parent;
}
return $category_ids_by_slug;
}

View File

@ -3,11 +3,11 @@
Plugin Name: Post Fixtures
Plugin URI: http://www.coswellproductions.com/wordpress/wordpress-plugins/
Description: Tear down and build up well crafted posts, categories, meta data, and other WordPress data in a test environment.
Version: 0.1
Version: 0.2.2
Author: John Bintz
Author URI: http://www.coswellproductions.com/wordpress/
Copyright 2009 John Bintz (email : john@coswellproductions.com)
Copyright 2009-2010 John Bintz (email : john@coswellproductions.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@ -45,7 +45,7 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
array(
array('post', array('Post title')),
array('date', array('2010-01-01')),
array('categories', array('test,test2')),
array('categories', array('test,test2/test3')),
array('metadata', array('key', array('metadata' => 'value'))),
array('post', array('Post title 2')),
array('date', array('2010-01-02')),
@ -56,7 +56,7 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
'post_title' => 'Post title',
'post_type' => 'post',
'post_date' => '2010-01-01',
'categories' => array('test', 'test2'),
'categories' => array('test', 'test2/test3'),
'metadata' => array('key' => array('metadata' => 'value'))
),
array(
@ -132,7 +132,7 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
),
array(
'categories',
array('test')
array('test/test2')
),
array(
'metadata',
@ -163,7 +163,8 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
)
),
'categories' => array(
1 => 'test'
1 => 'test',
2 => 'test2'
),
'tags' => array(
1 => array('tag1', 'tag2')
@ -172,6 +173,9 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
1 => array(
'test' => 'test2'
)
),
'post_categories' => array(
1 => array(2)
)
)
),
@ -208,8 +212,15 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
}
}
break;
case 'post_categories':
foreach ($info as $post_id => $expected_categories) {
$this->assertEquals($expected_categories, wp_get_post_categories($post_id));
}
break;
}
}
$this->assertTrue(!isset($builder->current_object));
}
function testBuildEmpty() {
@ -235,10 +246,20 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
$fb->finalize();
}
function testOption() {
$fb = new FixtureBuilder();
$fb->option('test', 'value');
function providerTestOption() {
return array(
array('test', 'value', 'value'),
array('test2', false, false)
);
}
$this->assertEquals('value', get_option('test'));
/**
* @dataProvider providerTestOption
*/
function testOption($key, $value, $expected_value) {
$fb = new FixtureBuilder();
$fb->option($key, $value);
$this->assertEquals($expected_value, get_option($key));
}
}

View File

@ -138,7 +138,7 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
array(false, array()),
array(array(), array()),
array(array('test'), array('test' => 1)),
array(array('test/test2'), array('test' => 1, 'test/test2' => 2)),
array(array('test/test2', 'test/test3'), array('test' => 1, 'test/test2' => 2, 'test/test3' => 3)),
);
}