generate from post

This commit is contained in:
John Bintz 2009-12-03 21:52:52 -05:00
parent 502ecd9696
commit d6d0e5bcb8
2 changed files with 54 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -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));
}
}