From d6d0e5bcb84f4762b349c1469c504b53143c3b9a Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 3 Dec 2009 21:52:52 -0500 Subject: [PATCH] generate from post --- classes/backends/ComicPressBackendURL.inc | 33 ++++++++++++++++++++++ test/backends/ComicPressBackendURLTest.php | 23 +++++++++++++-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/classes/backends/ComicPressBackendURL.inc b/classes/backends/ComicPressBackendURL.inc index 7320b83..d0cc56e 100644 --- a/classes/backends/ComicPressBackendURL.inc +++ b/classes/backends/ComicPressBackendURL.inc @@ -34,4 +34,37 @@ class ComicPressBackendURL extends ComicPressBackend { } } } + + function generate_from_post($post) { + $backends = array(); + + if (is_numeric($post)) { + $post = get_post($post); + } + + $result = get_post_meta($post->ID, 'backend_url_image_urls', true); + if (!empty($result)) { + if (is_array($result)) { + foreach ($result as $key => $value) { + if (is_scalar($key)) { + $backend = new ComicPressBackendURL(); + $backend->id = ComicPressBackendURL::generate_id($post->ID, $key); + $backend->urls_by_type = $value; + $backends[] = $backend; + } + } + } + } + return $backends; + } + + function generate_id($post_id, $key) { + if (is_numeric($post_id)) { + if (is_scalar($key)) { + return "url-${post_id}-${key}"; + } + } + + return false; + } } diff --git a/test/backends/ComicPressBackendURLTest.php b/test/backends/ComicPressBackendURLTest.php index 4162b1e..137fcbb 100644 --- a/test/backends/ComicPressBackendURLTest.php +++ b/test/backends/ComicPressBackendURLTest.php @@ -58,7 +58,7 @@ class ComicPressBackendUrlTest extends PHPUnit_Framework_TestCase { return array( array(false, array()), array(array(), array()), - array(array('12345' => array('comic' => 'test')), array()), + array(array('12345' => array('comic' => 'test')), array($valid_backend)), ); } @@ -66,6 +66,25 @@ class ComicPressBackendUrlTest extends PHPUnit_Framework_TestCase { * @dataProvider providerTestGenerateFromPost */ function testGenerateFromPost($metadata, $expected_backends) { - update_post_meta(1, 'backend_url_images', $metadata); + update_post_meta(1, 'backend_url_image_urls', $metadata); + + $this->assertEquals($expected_backends, ComicPressBackendURL::generate_from_post((object)array('ID' => 1))); + } + + function providerTestGenerateID() { + return array( + array(null, null, false), + array(1, null, false), + array(null, 'test', false), + array('test', 'test', false), + array(1, 'test', 'url-1-test'), + ); + } + + /** + * @dataProvider providerTestGenerateID + */ + function testGenerateID($post_id, $key, $expected_result) { + $this->assertEquals($expected_result, ComicPressBackendURL::generate_id($post_id, $key)); } }