From 43b073f67fd14a0d6734e3cb250fbbc36b9159bc Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 23 Jan 2010 11:32:34 -0500 Subject: [PATCH] fix category generation --- classes/FixtureBuilder.inc | 1 + classes/PostFixtures.inc | 11 ++++++++++- test/FixtureBuilderTest.php | 22 ++++++++++++++++------ test/PostFixturesTest.php | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/classes/FixtureBuilder.inc b/classes/FixtureBuilder.inc index 1bb219a..4db705a 100644 --- a/classes/FixtureBuilder.inc +++ b/classes/FixtureBuilder.inc @@ -131,6 +131,7 @@ class FixtureBuilder { $category_ids_by_slug = array(); foreach ($obj['post']['categories'] as $category) { $category_ids_by_slug = array_merge($category_ids_by_slug, PostFixtures::create_category($category)); + var_dump($category_ids_by_slug); } $category_ids = array_values($category_ids_by_slug); PostFixtures::set_post_categories($post_id, $category_ids); diff --git a/classes/PostFixtures.inc b/classes/PostFixtures.inc index 0513189..93c3d13 100644 --- a/classes/PostFixtures.inc +++ b/classes/PostFixtures.inc @@ -5,6 +5,7 @@ */ class PostFixtures { var $messages; + var $created_categories = array(); /** * Initialize the plugin. @@ -262,7 +263,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; } diff --git a/test/FixtureBuilderTest.php b/test/FixtureBuilderTest.php index e2287dc..fd94000 100644 --- a/test/FixtureBuilderTest.php +++ b/test/FixtureBuilderTest.php @@ -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( @@ -235,10 +235,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)); } } diff --git a/test/PostFixturesTest.php b/test/PostFixturesTest.php index 90d0e7c..088ff8c 100644 --- a/test/PostFixturesTest.php +++ b/test/PostFixturesTest.php @@ -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)), ); }