tags support

This commit is contained in:
John Bintz 2010-01-17 23:56:41 -05:00
parent 9258e1f4e7
commit 0c13247018
4 changed files with 90 additions and 19 deletions

View File

@ -59,6 +59,13 @@ class FixtureBuilder {
return $this; return $this;
} }
function tags($tags) {
$this->check_correct_type('post');
$this->current_object['post']['tags'] = explode(',', $tags);
return $this;
}
function defer() { function defer() {
if (!empty($this->current_object)) { if (!empty($this->current_object)) {
$this->ensure_type($this->current_object['type']); $this->ensure_type($this->current_object['type']);
@ -102,7 +109,7 @@ class FixtureBuilder {
function build_post($obj) { function build_post($obj) {
$post_obj = $obj['post']; $post_obj = $obj['post'];
foreach (array('categories', 'metadata') as $field) { foreach (array('categories', 'metadata', 'tags') as $field) {
unset($post_obj[$field]); unset($post_obj[$field]);
} }
$post_id = PostFixtures::create_post($post_obj); $post_id = PostFixtures::create_post($post_obj);
@ -124,6 +131,10 @@ class FixtureBuilder {
} }
} }
if (isset($obj['post']['tags'])) {
PostFixtures::create_post_tags($post_id, $obj['post']['tags']);
}
return get_post($post_id); return get_post($post_id);
} }
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart

View File

@ -293,6 +293,10 @@ class PostFixtures {
$this->create_post_metadata($id, $key, $value); $this->create_post_metadata($id, $key, $value);
} }
} }
if (isset($post['tags'])) {
$this->create_post_tags($id, $post['tags']);
}
} }
} }
} }
@ -311,6 +315,10 @@ class PostFixtures {
update_post_meta($post_id, $key, $value); update_post_meta($post_id, $key, $value);
} }
function create_post_tags($post_id, $tags) {
wp_set_post_tags($post_id, $tags);
}
function set_post_categories($post_id, $categories) { function set_post_categories($post_id, $categories) {
wp_set_post_categories($post_id, $categories); wp_set_post_categories($post_id, $categories);
} }

View File

@ -133,6 +133,10 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
array( array(
'metadata', 'metadata',
array('test', 'test2') array('test', 'test2')
),
array(
'tags',
array('tag1,tag2')
) )
), ),
(object)array( (object)array(
@ -155,6 +159,9 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
'categories' => array( 'categories' => array(
1 => 'test' 1 => 'test'
), ),
'tags' => array(
1 => array('tag1', 'tag2')
),
'post_meta' => array( 'post_meta' => array(
1 => array( 1 => array(
'test' => 'test2' 'test' => 'test2'

View File

@ -163,7 +163,11 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
) )
), ),
array(1), array(1),
array(1 => array(1)) array(
'categories' => array(
1 => array(1)
)
)
), ),
array( array(
array( array(
@ -173,7 +177,11 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
) )
), ),
array(1), array(1),
array(1 => array(2)) array(
'categories' => array(
1 => array(2)
)
)
), ),
array( array(
array( array(
@ -182,14 +190,24 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
'categories' => array('test2'), 'categories' => array('test2'),
'metadata' => array( 'metadata' => array(
'test' => 'test2' 'test' => 'test2'
),
'tags' => array(
'tag1', 'tag2'
) )
) )
), ),
array(1), array(1),
array(1 => array(2)), array(
array(1 => array( 'categories' => array(
'test' => 'test2' 1 => array(2)
)) ),
'metadata' => array(
1 => array('test' => 'test2')
),
'tags' => array(
1 => array('tag1', 'tag2')
)
)
), ),
array( array(
array( array(
@ -199,7 +217,11 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
) )
), ),
array(1), array(1),
array(1 => array(1,3)) array(
'categories' => array(
1 => array(1,3)
)
)
), ),
); );
} }
@ -207,20 +229,28 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
/** /**
* @dataProvider providerTestCreatePosts * @dataProvider providerTestCreatePosts
*/ */
function testCreatePosts($posts, $expected_post_ids, $expected_post_categories = false, $expected_metadata = false) { function testCreatePosts($posts, $expected_post_ids, $expected_additional_info = false) {
global $wp_test_expectations;
update_option('default_category', 1); update_option('default_category', 1);
wp_insert_category(array('slug' => 'test')); wp_insert_category(array('slug' => 'test'));
$this->assertEquals($expected_post_ids, $this->pf->create_posts($posts, array('test' => 1, 'test2' => 2, 'test2/test3' => 3))); $this->assertEquals($expected_post_ids, $this->pf->create_posts($posts, array('test' => 1, 'test2' => 2, 'test2/test3' => 3)));
if (is_array($expected_post_categories)) { if (is_array($expected_additional_info)) {
foreach ($expected_post_categories as $post_id => $categories) { extract($expected_additional_info);
$this->assertEquals($categories, wp_get_post_categories($post_id));
if (isset($categories)) {
if (is_array($categories)) {
foreach ($categories as $post_id => $cats) {
$this->assertEquals($cats, wp_get_post_categories($post_id));
}
} }
} }
if (is_array($expected_metadata)) { if (isset($metadata)) {
foreach ($expected_metadata as $post_id => $metadata_info) { if (is_array($metadata)) {
foreach ($metadata as $post_id => $metadata_info) {
foreach ($metadata_info as $key => $value) { foreach ($metadata_info as $key => $value) {
$this->assertEquals($value, get_post_meta($post_id, $key, true)); $this->assertEquals($value, get_post_meta($post_id, $key, true));
} }
@ -228,6 +258,21 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
} }
} }
if (isset($tags)) {
if (is_array($tags)) {
foreach ($tags as $post_id => $expected_tags) {
$post_tags = wp_get_post_tags($post_id);
foreach ($post_tags as &$tag) {
$tag = $tag->slug;
}
$this->assertEquals($post_tags, $expected_tags);
}
}
}
}
}
function testCreate() { function testCreate() {
$pf = $this->getMock('PostFixtures', array('create_posts', 'create_categories', 'process_blog_options')); $pf = $this->getMock('PostFixtures', array('create_posts', 'create_categories', 'process_blog_options'));
$pf->expects($this->once())->method('create_posts')->with('posts', 'processed'); $pf->expects($this->once())->method('create_posts')->with('posts', 'processed');