From 739753ee8edeb36225de26acee202fb1c595d876 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 31 Jan 2010 18:19:35 -0500 Subject: [PATCH] tests for save post --- classes/ComicPressAdmin.inc | 12 +++-- test/ComicPressAdminTest.php | 94 +++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 5 deletions(-) diff --git a/classes/ComicPressAdmin.inc b/classes/ComicPressAdmin.inc index 71ce58f..23d0d86 100644 --- a/classes/ComicPressAdmin.inc +++ b/classes/ComicPressAdmin.inc @@ -451,10 +451,15 @@ class ComicPressAdmin { } } + function _new_comicpresscomicpost($post) { + return new ComicPressComicPost($post); + } + // @codeCoverageIgnoreEnd + function save_post($post_id) { - if (ComicPressAdmin::verify_nonces() === 'handle_update_edit_form_advanced') { - if ($post = get_post($psot_id)) { - $comic_post = new ComicPressComicPost($post); + if ($this->verify_nonces() === 'handle_update_edit_form_advanced') { + if ($post = get_post($post_id)) { + $comic_post = $this->_new_comicpresscomicpost($post); $data = $this->_json_decode(stripslashes($_POST['cp']['comic_order'])); if (!empty($data)) { if (is_array($data)) { @@ -465,6 +470,7 @@ class ComicPressAdmin { } } + // @codeCoverageIgnoreStart function handle_update_get_new_image_type_editor($info) { $type = substr(md5(rand()), 0, 6); $info = array( diff --git a/test/ComicPressAdminTest.php b/test/ComicPressAdminTest.php index 485295c..a953164 100644 --- a/test/ComicPressAdminTest.php +++ b/test/ComicPressAdminTest.php @@ -391,6 +391,96 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase { $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); } } -} -?> + function providerTestSavePost() { + return array( + array( + array( + 'verify_nonces' => false, + 'post_id' => 1 + ), + false + ), + array( + array( + 'verify_nonces' => 'handle_update_edit_form_advanced', + 'post_id' => 2 + ), + false + ), + array( + array( + 'verify_nonces' => 'handle_update_edit_form_advanced', + 'post_id' => 1, + 'comic_order' => 'order', + 'json_decode_return' => '' + ), + false + ), + array( + array( + 'verify_nonces' => 'handle_update_edit_form_advanced', + 'post_id' => 1, + 'comic_order' => 'order', + 'json_decode_return' => array() + ), + false + ), + array( + array( + 'verify_nonces' => 'handle_update_edit_form_advanced', + 'post_id' => 1, + 'comic_order' => 'order', + 'json_decode_return' => array('test') + ), + true + ), + ); + } + + /** + * @dataProvider providerTestSavePost + */ + function testSavePost($info, $expects_update_media_data) { + extract($info); + + $post = (object)array( + 'ID' => 1 + ); + + wp_insert_post($post); + + $admin = $this->getMock('ComicPressAdmin', array( + 'verify_nonces', + '_json_decode', + '_new_comicpresscomicpost' + )); + + $admin->expects($this->once())->method('verify_nonces')->will($this->returnValue($verify_nonces)); + + if (isset($info['comic_order'])) { + $comic_post = $this->getMock('ComicPressComicPost', array('update_post_media_data')); + + $expectation = $comic_post->expects($this->{$expects_update_media_data ? 'once' : 'never'}()) + ->method('update_post_media_data'); + + if ($expects_update_media_data) { + $expectation->with($json_decode_return); + } + + $admin->expects($this->once()) + ->method('_new_comicpresscomicpost') + ->with($post) + ->will($this->returnValue($comic_post)); + + $admin->expects($this->once()) + ->method('_json_decode') + ->with($comic_order) + ->will($this->returnValue($json_decode_return)); + + $_POST = array('cp' => array('comic_order' => $comic_order)); + } + + $admin->save_post($post_id); + } +}