create posts and metadata

This commit is contained in:
John Bintz 2009-11-22 18:57:23 -05:00
parent 33eeae55c8
commit 054dd5a229
2 changed files with 99 additions and 2 deletions

View File

@ -42,4 +42,34 @@ class PostFixtures {
} }
return $category_ids_by_slug; return $category_ids_by_slug;
} }
function create_posts($posts, $categories) {
$post_ids_created = array();
if (is_array($posts)) {
foreach ($posts as $post) {
$id = wp_insert_post($post);
if ($id != 0) {
$post_ids_created[] = $id;
if (isset($post['categories'])) {
$all_categories = array();
foreach ($post['categories'] as $slug) {
if (isset($categories[$slug])) {
$all_categories[] = $categories[$slug];
}
}
wp_set_post_categories($id, $all_categories);
} else {
wp_set_post_categories($id, array(get_option('default_category')));
}
if (isset($post['metadata'])) {
foreach ($post['metadata'] as $key => $value) {
update_post_meta($id, $key, $value);
}
}
}
}
}
return $post_ids_created;
}
} }

View File

@ -119,8 +119,75 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
* @dataProvider providerTestCreateCategories * @dataProvider providerTestCreateCategories
*/ */
function testCreateCategories($input, $expected_output) { function testCreateCategories($input, $expected_output) {
global $wp_test_expectations;
$this->assertEquals($expected_output, $this->pf->create_categories($input)); $this->assertEquals($expected_output, $this->pf->create_categories($input));
} }
function providerTestCreatePosts() {
return array(
array(false, array(), false),
array(
array(),
array()
),
array(
array(
array(
'post_title' => 'test'
)
),
array(1),
array(1 => array(1))
),
array(
array(
array(
'post_title' => 'test',
'categories' => array('test2')
)
),
array(1),
array(1 => array(2))
),
array(
array(
array(
'post_title' => 'test',
'categories' => array('test2'),
'metadata' => array(
'test' => 'test2'
)
)
),
array(1),
array(1 => array(2)),
array(1 => array(
'test' => 'test2'
))
),
);
}
/**
* @dataProvider providerTestCreatePosts
*/
function testCreatePosts($posts, $expected_post_ids, $expected_post_categories = false, $expected_metadata = false) {
update_option('default_category', 1);
wp_insert_category(array('slug' => 'test'));
$this->assertEquals($expected_post_ids, $this->pf->create_posts($posts, array('test' => 1, 'test2' => 2)));
if (is_array($expected_post_categories)) {
foreach ($expected_post_categories as $post_id => $categories) {
$this->assertEquals($categories, wp_get_post_categories($post_id));
}
}
if (is_array($expected_metadata)) {
foreach ($expected_metadata as $post_id => $metadata_info) {
foreach ($metadata_info as $key => $value) {
$this->assertEquals($value, get_post_meta($post_id, $key, true));
}
}
}
}
} }