diff --git a/classes/FixtureBuilder.inc b/classes/FixtureBuilder.inc index 1883689..c5114b1 100644 --- a/classes/FixtureBuilder.inc +++ b/classes/FixtureBuilder.inc @@ -64,6 +64,7 @@ class FixtureBuilder { $this->ensure_type($this->current_object['type']); $this->{"defer_{$this->current_object['type']}"}($this->current_object); } + unset($this->current_object); } function build() { @@ -113,6 +114,7 @@ class FixtureBuilder { $category_ids_by_slug = array_merge($category_ids_by_slug, PostFixtures::create_category($category)); } $category_ids = array_values($category_ids_by_slug); + PostFixtures::set_post_categories($post_id, $category_ids); } $metadata = array(); @@ -128,4 +130,23 @@ class FixtureBuilder { return false; // @codeCoverageIgnoreEnd } + + function finalize() { + $this->defer(); + + foreach ($this->deferred_builds as $type => $entries) { + foreach ($entries as $entry) { + $object = null; + switch ($type) { + case 'category': + $object = array('name' => $entry); + break; + case 'post': + $object = array('post' => $entry); + break; + } + $this->{"build_${type}"}($object); + } + } + } } diff --git a/classes/PostFixtures.inc b/classes/PostFixtures.inc index 920661d..37c7af7 100644 --- a/classes/PostFixtures.inc +++ b/classes/PostFixtures.inc @@ -49,15 +49,40 @@ class PostFixtures { */ function handle_update($info) { if (isset($info['is_ok'])) { - $data = $this->parse_json(stripslashes($info['data'])); - if (!empty($data)) { - $data = $this->process_data($data); - $this->remove(); - $this->create($data); - $this->messages[] = __("New data set loaded into WordPress.", 'post-fixtures'); - } else { - $this->messages[] = __("Data is not valid JSON.", 'post-fixtures'); - } + if (strpos($info['data'], 'file:') === 0) { + $this->handle_load_file(substr($info['data'], 5)); + } else { + $this->handle_json($info['data']); + } + } + } + + function handle_load_file($file) { + $file = realpath($this->wpcontent_path() . '/' . $file); + if (file_exists($file)) { + switch (pathinfo($file, PATHINFO_EXTENSION)) { + case "json": + $this->handle_json(file_get_contents($file)); + break; + case "inc"; + $this->remove(); + $builder = new FixtureBuilder(); + include($file); + $builder->finalize(); + break; + } + } + } + + function handle_json($input) { + $data = $this->parse_json(stripslashes($input)); + if (!empty($data)) { + $data = $this->process_data($data); + $this->remove(); + $this->create($data); + $this->messages[] = __("New data set loaded into WordPress.", 'post-fixtures'); + } else { + $this->messages[] = __("Data is not valid JSON.", 'post-fixtures'); } } @@ -128,7 +153,7 @@ class PostFixtures { $dh = opendir($dir); $is_fixture_dir = (basename($dir) === 'fixtures'); while ($file = readdir($dh)) { - if ($file[0] != '.') { + if ($file[0] != '.') { $target = $dir . '/' . $file; if ($is_fixture_dir) { if (is_file($target)) { @@ -286,6 +311,10 @@ class PostFixtures { update_post_meta($post_id, $key, $value); } + function set_post_categories($post_id, $categories) { + wp_set_post_categories($post_id, $categories); + } + /** * Create everything from the provided data. * @param array $data The data to use in creation. @@ -339,9 +368,9 @@ class PostFixtures { if (count($parts) == 1) { $index = reset($parts); if (isset($this->{"_${source}"})) { - if (isset($this->{"_${source}"}[$index])) { - $value = $this->{"_${source}"}[$index]; - } + if (isset($this->{"_${source}"}[$index])) { + $value = $this->{"_${source}"}[$index]; + } } } } diff --git a/classes/partials/admin.inc b/classes/partials/admin.inc index c83bb80..0409d1b 100644 --- a/classes/partials/admin.inc +++ b/classes/partials/admin.inc @@ -9,8 +9,20 @@
-

- +
+
+

+ +
+
+

+
    + +
  • + +
+
+
@@ -27,6 +39,11 @@ $('#post-fixtures-additional-options').hide(); $('input[name*=is_ok]').get(0).checked = false; + $('#pre-built-fixture-holder a').click(function() { + $('textarea[name*=data]').val('file:' + $(this).attr('href')); + return false; + }); + $('#post-fixtures-form').submit(function() { var checkbox = $('input[name*=is_ok]', this.target).get(0); var ok = false diff --git a/post-fixtures.php b/post-fixtures.php index 192aa74..89210ed 100644 --- a/post-fixtures.php +++ b/post-fixtures.php @@ -27,7 +27,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA function __post_fixtures_plugins_loaded() { if (version_compare(PHP_VERSION, '5.0.0') === 1) { - require_once('classes/PostFixtures.inc'); + foreach (glob(dirname(__FILE__) . '/classes/*.inc') as $file) { + require_once($file); + } $post_fixtures = new PostFixtures(); $post_fixtures->init(); } else { diff --git a/test/FixtureBuilderTest.php b/test/FixtureBuilderTest.php index a4ab3a5..e6b9ea1 100644 --- a/test/FixtureBuilderTest.php +++ b/test/FixtureBuilderTest.php @@ -204,4 +204,21 @@ class FixtureBuilderTest extends PHPUnit_Framework_TestCase { $builder->build(); } + + function testFinalize() { + $fb = $this->getMock('FixtureBuilder', array('defer', 'build_category', 'build_post')); + + $fb->deferred_builds = array( + 'category' => array('test', 'test2'), + 'post' => array('post3', 'post4') + ); + + $fb->expects($this->at(0))->method('defer'); + $fb->expects($this->at(1))->method('build_category')->with(array('name' => 'test')); + $fb->expects($this->at(2))->method('build_category')->with(array('name' => 'test2')); + $fb->expects($this->at(3))->method('build_post')->with(array('post' => 'post3')); + $fb->expects($this->at(4))->method('build_post')->with(array('post' => 'post4')); + + $fb->finalize(); + } } diff --git a/test/PostFixturesTest.php b/test/PostFixturesTest.php index d6c6e64..614cabc 100644 --- a/test/PostFixturesTest.php +++ b/test/PostFixturesTest.php @@ -288,25 +288,52 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { } } - function providerTestHandleUpdate() { + function providerTestHandleJSON() { return array( - array(array(), false), - array(array('is_ok' => true, 'data' => '{]'), false), - array(array('is_ok' => true, 'data' => '{"test": "test"}'), true), + array('', false), + array('{]', false), + array('{"test": "test"}', true), ); } /** - * @dataProvider providerTestHandleUpdate + * @dataProvider providerTestHandleJSON */ - function testHandleUpdate($info, $should_succeed) { + function testHandleJSON($info, $should_succeed) { $pf = $this->getMock('PostFixtures', array('process_data', 'remove', 'create')); foreach (array('process_data', 'remove', 'create') as $method) { $pf->expects($this->{$should_succeed ? 'once' : 'never'}())->method($method); } - $pf->handle_update($info); + $pf->handle_json($info); + } + + function providerTestHandleUpdate() { + return array( + array( + array(), false + ), + array( + array('is_ok' => true, 'data' => 'json'), array('handle_json', 'json') + ), + array( + array('is_ok' => true, 'data' => 'file:test'), array('handle_load_file', 'test') + ), + ); + } + + /** + * @dataProvider providerTestHandleUpdate + */ + function testHandleUpdate($input, $expected_method) { + $pf = $this->getMock('PostFixtures', array('handle_load_file', 'handle_json')); + + if (!empty($expected_method)) { + $pf->expects($this->once())->method($expected_method[0])->with($expected_method[1]); + } + + $pf->handle_update($input); } function providerTestAdminInit() {