generate from id

This commit is contained in:
John Bintz 2009-12-04 07:02:08 -05:00
parent d6d0e5bcb8
commit f9ee36f7fd
2 changed files with 48 additions and 0 deletions

View File

@ -67,4 +67,23 @@ class ComicPressBackendURL extends ComicPressBackend {
return false;
}
function generate_from_id($id) {
if (preg_match('#^url-([0-9]+)-(.+)$#', $id, $matches) > 0) {
list($all, $post_id, $key) = $matches;
$post = get_post($post_id);
if (!empty($post)) {
$meta = get_post_meta($post_id, 'backend_url_image_urls', true);
if (is_array($meta)) {
if (isset($meta[$key])) {
$backend = new ComicPressBackendURL();
$backend->id = $id;
$backend->urls_by_type = $meta[$key];
return $backend;
}
}
}
}
return false;
}
}

View File

@ -87,4 +87,33 @@ class ComicPressBackendUrlTest extends PHPUnit_Framework_TestCase {
function testGenerateID($post_id, $key, $expected_result) {
$this->assertEquals($expected_result, ComicPressBackendURL::generate_id($post_id, $key));
}
function providerTestGenerateFromID() {
$valid_backend = new ComicPressBackendURL();
$valid_backend->id = 'url-1-12345';
$valid_backend->urls_by_type = array(
'comic' => 'comic',
'rss' => 'rss'
);
return array(
array('', false),
array('url-', false),
array('url-1', false),
array('url-2-12345', false),
array('url-1-123456', false),
array('url-1-12345', $valid_backend)
);
}
/**
* @dataProvider providerTestGenerateFromID
*/
function testGenerateFromID($id, $expected_result) {
wp_insert_post((object)array('ID' => 1));
update_post_meta(1, 'backend_url_image_urls', array('12345' => array('comic' => 'comic', 'rss' => 'rss')));
$this->assertEquals($expected_result, ComicPressBackendURL::generate_from_id($id));
}
}