diff --git a/classes/PostFixtures.inc b/classes/PostFixtures.inc index fce6eb3..5c17aec 100644 --- a/classes/PostFixtures.inc +++ b/classes/PostFixtures.inc @@ -42,4 +42,34 @@ class PostFixtures { } 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; + } } diff --git a/test/PostFixturesTest.php b/test/PostFixturesTest.php index eb6ba5d..2cd1bf1 100644 --- a/test/PostFixturesTest.php +++ b/test/PostFixturesTest.php @@ -119,8 +119,75 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { * @dataProvider providerTestCreateCategories */ function testCreateCategories($input, $expected_output) { - global $wp_test_expectations; - $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)); + } + } + } + } }