working on new backend code

This commit is contained in:
John Bintz 2009-11-08 22:11:26 -05:00
parent ead478fcec
commit 4cfacec35f
10 changed files with 309 additions and 212 deletions

View File

@ -13,9 +13,16 @@ class ComicPress {
'mini_dimensions' => '100x', 'mini_dimensions' => '100x',
'helpers' => array(), 'helpers' => array(),
'addons' => array(), 'addons' => array(),
'storyline_order' => '' 'storyline_order' => '',
'subattachment_types' => array(
'rss' => 'RSS',
'archive' => 'Archive',
'mini' => 'Mini Thumb'
)
); );
var $backends = array('ComicPressBackendAttachment');
function &get_instance() { function &get_instance() {
static $instance; static $instance;
@ -58,6 +65,7 @@ class ComicPress {
} }
add_filter('intermediate_image_sizes', array(&$this, 'intermediate_image_sizes')); add_filter('intermediate_image_sizes', array(&$this, 'intermediate_image_sizes'));
add_filter('editor_max_image_size', array(&$this, 'editor_max_image_size'), 10, 2);
foreach (array('comic', 'rss', 'archive', 'mini') as $size) { foreach (array('comic', 'rss', 'archive', 'mini') as $size) {
list($w, $h) = explode("x", $this->comicpress_options["${size}_dimensions"]); list($w, $h) = explode("x", $this->comicpress_options["${size}_dimensions"]);
@ -71,6 +79,14 @@ class ComicPress {
return array_merge($sizes, array('comic', 'rss', 'archive', 'mini')); return array_merge($sizes, array('comic', 'rss', 'archive', 'mini'));
} }
function editor_max_image_size($current_max, $size) {
if (isset($this->comicpress_options["${size}_dimensions"])) {
list($width, $height) = explode('x', $this->comicpress_options["${size}_dimensions"]);
$current_max = array(intval($width), intval($height));
}
return $current_max;
}
function announce_activated_helpers() { function announce_activated_helpers() {
echo "<center>[ <strong>Activated ComicPress helpers:</strong> " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]</center>"; echo "<center>[ <strong>Activated ComicPress helpers:</strong> " . implode(", ", array_keys($this->comicpress_options['helpers'])) . " ]</center>";
} }

View File

@ -80,7 +80,7 @@ class ComicPressAdmin {
wp_enqueue_script('cp-admin', get_template_directory_uri() . '/js/Storyline.js', array('prototype', 'scriptaculous')); wp_enqueue_script('cp-admin', get_template_directory_uri() . '/js/Storyline.js', array('prototype', 'scriptaculous'));
} }
if (strpos($pagenow, "media-upload") === 0) { if (strpos($pagenow, "-upload") !== false) {
wp_enqueue_script('cp-media', get_template_directory_uri() . '/js/MediaUpload.js', array('prototype')); wp_enqueue_script('cp-media', get_template_directory_uri() . '/js/MediaUpload.js', array('prototype'));
} }
} }
@ -176,28 +176,26 @@ class ComicPressAdmin {
$comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID)); $comic_post = new ComicPressComicPost(get_post($uploading_iframe_ID));
$ordering = $comic_post->normalize_comic_image_ordering(); $ordering = $comic_post->normalize_comic_image_ordering();
if (is_array($ordering)) { $nonce = wp_create_nonce('comicpress');
$nonce = wp_create_nonce('comicpress'); $zoom_level = 40;
$zoom_level = 40; $current_user = wp_get_current_user();
$current_user = wp_get_current_user(); if (!empty($current_user)) {
if (!empty($current_user)) { $comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings');
$comicpress_meta = get_usermeta($current_user->ID, 'comicpress-settings'); if (is_array($comicpress_meta)) {
if (is_array($comicpress_meta)) { if (isset($comicpress_meta['zoom_level'])) {
if (isset($comicpress_meta['zoom_level'])) { $zoom_level = floor($comicpress_meta['zoom_level']);
$zoom_level = floor($comicpress_meta['zoom_level']);
}
} }
} }
}
// from wp-admin/includes/media.php O_o // from wp-admin/includes/media.php O_o
$media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID"; $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
$image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&amp;type=image&amp;TB_iframe=true"); $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&amp;type=image&amp;TB_iframe=true");
if ($is_ajax === true) { if ($is_ajax === true) {
include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc'); include(dirname(__FILE__) . '/partials/_comic-image-ordering-sorters.inc');
} else { } else {
include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc'); include(dirname(__FILE__) . '/partials/_comic-image-ordering.inc');
}
} }
} }

View File

@ -0,0 +1,17 @@
<?php
class ComicPressBackend {
function _embed_image($size) {
$extras = array();
if (($dims = $this->dims($size)) !== false) {
$extras = array_merge($extras, $dims);
}
foreach ($extras as $field => $value) {
$extras[] = "${field}=\"${value}\"";
unset($extras[$field]);
}
$output = sprintf('<img src="%s" alt="%s" title="%s" %s />', $this->url(), $this->alt(), $this->title(), implode(" ", $extras));
return apply_filters('comicpress_embed_image', $output, $this);
}
}

View File

@ -8,17 +8,15 @@ class ComicPressComicPost {
if (!is_null($post)) { $this->post = $post; } if (!is_null($post)) { $this->post = $post; }
} }
function get_comic_image_attachments() { function get_attachments() {
if (is_null($this->attachments)) { $comicpress = ComicPress::get_instance();
$this->attachments = get_children(array( $attachments = array();
'post_parent' => $this->post->ID, foreach ($comicpress->backends as $backend) {
'post_type' => 'attachment', $attachments = array_merge($attachments, call_user_func(array($backend, 'generate_from_post'), $this->post));
'post_mime_type' => 'image' }
)); return $attachments;
}
return $this->attachments;
} }
/** /**
* Display all the attached images. * Display all the attached images.
*/ */
@ -33,19 +31,19 @@ class ComicPressComicPost {
if (get_post_meta($attachment_id, "comic_image_type", true) == $type) { if (get_post_meta($attachment_id, "comic_image_type", true) == $type) {
$attachment = get_post($attachment_id); $attachment = get_post($attachment_id);
$title = (!empty($attachment->post_excerpt) ? $attachment->post_excerpt : $attachment->post_title); $title = (!empty($attachment->post_excerpt) ? $attachment->post_excerpt : $attachment->post_title);
$url = wp_get_attachment_url($attachment->ID, ''); $url = wp_get_attachment_url($attachment->ID, '');
$sizes = image_downsize($attachment->ID, $size_type); $sizes = image_downsize($attachment->ID, $size_type);
if ($sizes) { if ($sizes) {
$url = $sizes[0]; $url = $sizes[0];
} }
$output[] = apply_filters('comicpress_attached_image', $output[] = apply_filters('comicpress_attached_image',
sprintf($format, $this->get_comic_img_tag($url, $size_type, array('title' => $title))), sprintf($format, $this->get_comic_img_tag($url, $size_type, array('title' => $title))),
$attachment_id, $attachment_id,
$i++); $i++);
$found = true; $found = true;
if (!is_null($limit)) { if (!is_null($limit)) {
if (--$limit == 0) { break; } if (--$limit == 0) { break; }
} }
@ -58,7 +56,7 @@ class ComicPressComicPost {
return $found; return $found;
} }
function _display_type($types, $format, $single = false) { function _display_type($types, $format, $single = false) {
$target_type = reset($types); $target_type = reset($types);
foreach ($types as $type) { foreach ($types as $type) {
@ -67,7 +65,7 @@ class ComicPressComicPost {
} }
} }
} }
function display_comics($format) { $this->_display_type(array('comic'), $format); } function display_comics($format) { $this->_display_type(array('comic'), $format); }
function display_archive($format) { $this->_display_type(array('archive'. 'comic'), $format, true); } function display_archive($format) { $this->_display_type(array('archive'. 'comic'), $format, true); }
function display_rss($format) { $this->_display_type(array('rss'. 'comic'), $format); } function display_rss($format) { $this->_display_type(array('rss'. 'comic'), $format); }
@ -77,7 +75,7 @@ class ComicPressComicPost {
*/ */
function get_comic_img_tag($url, $type, $additional_parameters = array()) { function get_comic_img_tag($url, $type, $additional_parameters = array()) {
$dimensions = array(); $dimensions = array();
if (isset($this->comicpress->comicpress_options["${type}_dimensions"])) { if (isset($this->comicpress->comicpress_options["${type}_dimensions"])) {
$parts = explode("x", $this->comicpress->comicpress_options["${type}_dimensions"]); $parts = explode("x", $this->comicpress->comicpress_options["${type}_dimensions"]);
switch (count($parts)) { switch (count($parts)) {
@ -86,7 +84,7 @@ class ComicPressComicPost {
} }
$dimensions = compact('width', 'height'); $dimensions = compact('width', 'height');
} }
$output = '<img src="' . $url . '" '; $output = '<img src="' . $url . '" ';
foreach (array('width', 'height') as $field) { foreach (array('width', 'height') as $field) {
if (!empty($dimensions[$field])) { if (!empty($dimensions[$field])) {
@ -95,11 +93,11 @@ class ComicPressComicPost {
} }
if (is_array($additional_parameters)) { if (is_array($additional_parameters)) {
foreach ($additional_parameters as $parameter => $value) { foreach ($additional_parameters as $parameter => $value) {
$output .= $parameter . '="' . $value . '" '; $output .= $parameter . '="' . $value . '" ';
} }
} }
$output .= "/>"; $output .= "/>";
return $output; return $output;
} }
@ -107,62 +105,58 @@ class ComicPressComicPost {
* Normalize the ordering of comic images in this post. * Normalize the ordering of comic images in this post.
* If images have beed added or removed, intelligently update the metadata. * If images have beed added or removed, intelligently update the metadata.
*/ */
function normalize_comic_image_ordering() { function normalize_ordering() {
if (is_array($this->get_comic_image_attachments())) { $attachments = $this->get_attachments();
$ordering_by_type = array(); if (is_array($attachments)) {
$ordering_types = get_post_meta($this->post->ID, 'comic_ordering', true); $new_ordering = array();
$current_ordering = get_post_meta($this->post->ID, 'image-ordering', true);
$comic_image_ordering = array(); if (!is_array($current_ordering)) { $current_ordering = array(); }
$found_post_ids = array();
if (!empty($ordering_types)) { $all_current_ids = array();
foreach ($ordering_types as $type => $post_ids) { foreach ($current_ordering as $key => $children) {
$comic_image_ordering[$type] = array(); $all_current_ids[$key] = true;
foreach ($post_ids as $ordering_post_id) { if (is_array($children)) {
foreach ($this->get_comic_image_attachments() as $attachment) { foreach ($children as $type => $kids) {
if (!isset($found_post_ids[$attachment->ID])) { $all_current_ids = array_merge($all_current_ids, $kids);
if ($attachment->ID == $ordering_post_id) { }
$comic_image_ordering[$type][] = $attachment->ID; }
$found_post_ids[$ordering_post_id] = true;
}
}
}
}
}
} }
$all_current_ids = array_keys($all_current_ids);
$remaining_posts_to_sort = array();
foreach ($this->get_comic_image_attachments() as $attachment) { $attachment_ids = array();
$comic_image_type = get_post_meta($attachment->ID, 'comic_image_type', true); foreach ($attachments as $attachment) { $attachment_ids[] = $attachment->id; }
if (!empty($comic_image_type)) { $new_attachments = array_diff($attachment_ids, $all_current_ids);
if (!isset($found_post_ids[$attachment->ID])) { $missing_attachments = array_diff($all_current_ids, $attachment_ids);
if (!isset($remaining_posts_to_sort[$comic_image_type])) {
$remaining_posts_to_sort[$comic_image_type] = array(); foreach ($new_attachments as $attachment_id) {
} $current_ordering[$attachment_id] = true;
$remaining_posts_to_sort[$comic_image_type][] = $attachment->ID;
}
}
}
foreach ($remaining_posts_to_sort as $type => $posts) {
usort($remaining_posts_to_sort[$type], array(&$this, 'sort_remaining_comic_images'));
}
foreach ($remaining_posts_to_sort as $type => $posts) {
if (!isset($comic_image_ordering[$type])) { $comic_image_ordering[$type] = array(); }
if (is_array($comic_image_ordering[$type])) {
$comic_image_ordering[$type] = array_merge($comic_image_ordering[$type], $posts);
} else {
$comic_image_ordering[$type] = $posts;
}
} }
update_post_meta($this->post->ID, 'comic_ordering', $comic_image_ordering); foreach ($missing_attachments as $attachment_id) {
return $comic_image_ordering; if (isset($current_ordering[$attachment_id])) {
unset($current_ordering[$attachment_id]);
} else {
foreach ($current_ordering as $key => $children) {
foreach ($children as $type => $kids) {
if (isset($kids[$attachment_id])) { unset($kids[$attachment_id]); }
$children[$type] = $kids;
}
if (empty($children[$type])) {
$current_ordering[$key] = true;
} else {
$current_ordering[$key] = $children;
}
}
}
}
update_post_meta($this->post->ID, 'image-ordering', $current_ordering);
return $current_ordering;
} }
return false; return false;
} }
/** /**
* Sort the remaining comic images by file date. * Sort the remaining comic images by file date.
* @param object $a * @param object $a
@ -172,7 +166,7 @@ class ComicPressComicPost {
function sort_remaining_comic_images($a, $b) { function sort_remaining_comic_images($a, $b) {
$a_date = isset($a->post_date) ? $a->post_date : 0; $a_date = isset($a->post_date) ? $a->post_date : 0;
$b_date = isset($b->post_date) ? $b->post_date : 0; $b_date = isset($b->post_date) ? $b->post_date : 0;
return $a_date - $b_date; return $a_date - $b_date;
} }
/** /**
@ -182,7 +176,7 @@ class ComicPressComicPost {
function change_comic_image_ordering($requested_new_order) { function change_comic_image_ordering($requested_new_order) {
$orderings = get_post_meta($this->post->ID, 'comic_ordering', true); $orderings = get_post_meta($this->post->ID, 'comic_ordering', true);
if (!is_array($orderings)) { $orderings = array(); } if (!is_array($orderings)) { $orderings = array(); }
$new_order = array(); $new_order = array();
$requested_new_order = (array)$requested_new_order; $requested_new_order = (array)$requested_new_order;
@ -193,7 +187,7 @@ class ComicPressComicPost {
$position = 0; $position = 0;
foreach ($requested_new_order[$type] as $id) { foreach ($requested_new_order[$type] as $id) {
if (!isset($sort_by_position[$position])) { if (!isset($sort_by_position[$position])) {
$sort_by_position[$position] = array(); $sort_by_position[$position] = array();
} }
$sort_by_position[$position][] = $id; $sort_by_position[$position][] = $id;
$position++; $position++;
@ -213,7 +207,7 @@ class ComicPressComicPost {
} }
} }
} }
update_post_meta($this->post->ID, 'comic_ordering', $new_order); update_post_meta($this->post->ID, 'comic_ordering', $new_order);
} }
@ -228,16 +222,16 @@ class ComicPressComicPost {
$category_parent = $category->parent; $category_parent = $category->parent;
if ($category_parent != 0) { if ($category_parent != 0) {
$post_categories[] = $category_parent; $post_categories[] = $category_parent;
} }
} }
} while ($category_parent != 0); } while ($category_parent != 0);
foreach ($post_categories as $category_id) { foreach ($post_categories as $category_id) {
$category = get_category($category_id); $category = get_category($category_id);
$parents[$category_id] = $category->slug; $parents[$category_id] = $category->slug;
} }
} }
return $parents; return $parents;
} }
} }

View File

@ -1,17 +1,64 @@
<?php <?php
class ComicPressBackendAttachment { require_once(dirname(__FILE__) . '/../ComicPressBackend.inc');
function ComicPressBackendAttachment($attachment_id) {
$this->attachment_id = $attachment_id; class ComicPressBackendAttachment extends ComicPressBackend {
var $root_id = 'attachment';
function generate_from_post($post) {
$result = array();
if (is_object($post)) {
if (isset($post->ID)) {
$children = get_children(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if (!empty($children)) {
foreach ($children as $child) {
$result[] = new ComicPressBackendAttachment($child);
}
}
}
}
return $result;
}
function ComicPressBackendAttachment($attachment) {
$this->attachment = $attachment;
$this->id = sprintf('%s-%d', $this->root_id, $this->attachment->ID);
$this->type = get_post_meta($this->attachment->ID, 'comic_image_type', true);
} }
function get_url() { function dims($size = 'comic') {
return wp_get_attachment_url($this->attachment_id); $metadata = image_downsize($this->attachment->ID, $size);
if (!empty($metadata)) {
if (is_array($metadata)) {
return array_combine(array('width', 'height'), array_slice($metadata, 1, 2));
}
}
return false;
} }
function get_info() { function url($size = 'comic') {
return wp_get_attachment_metadata($this->attachment_id); $metadata = image_downsize($this->attachment->ID, $size);
if (!empty($metadata)) {
if (is_array($metadata)) {
return $metadata[0];
}
}
return false;
} }
function embed($size = 'comic') {
return $this->_embed_image($size);
}
function alt() { return $this->attachment->post_title; }
function title() { return $this->attachment->post_excerpt; }
function get_info() { return array(); }
} }
?> ?>

View File

@ -9,29 +9,31 @@
<?php _e('Click the Refesh button underneath the zoom slider if you\'ve changed the images attached to this post.', 'comicpress') ?> <?php _e('Click the Refesh button underneath the zoom slider if you\'ve changed the images attached to this post.', 'comicpress') ?>
</em></p> </em></p>
<?php foreach ($ordering as $type => $attachment_ids) { ?> <?php if (!empty($ordering)) { ?>
<h3><?php echo $this->comic_image_types[$type] ?></h3> <?php foreach ($ordering as $type => $attachment_ids) { ?>
<div class="comic-ordering" id="comic-ordering-<?php echo $type ?>"> <h3><?php echo $this->comic_image_types[$type] ?></h3>
<?php foreach ($attachment_ids as $attachment_id) { <div class="comic-ordering" id="comic-ordering-<?php echo $type ?>">
$backend = new ComicPressBackendAttachment($attachment_id); <?php foreach ($attachment_ids as $attachment_id) {
$info = $backend->get_info(); ?> $backend = new ComicPressBackendAttachment($attachment_id);
<div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>"> $info = $backend->get_info(); ?>
<img src="<?php echo $backend->get_url() ?>" border="0" height="<?php echo $zoom_level ?>" /> <div class="cp-comic-attachment" id="attachment_<?php echo $attachment_id ?>">
<div> <img src="<?php echo $backend->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
<?php if (isset($info['file'])) { ?> <div>
<p><strong><?php echo basename($info['file']) ?></strong></p> <?php if (isset($info['file'])) { ?>
<?php } ?> <p><strong><?php echo basename($info['file']) ?></strong></p>
<?php if (isset($info['width']) && isset($info['height'])) { ?> <?php } ?>
<p> <?php if (isset($info['width']) && isset($info['height'])) { ?>
<?php _e('Size:', 'comicpress') ?> <p>
<?php printf('%dx%d', $info['width'], $info['height'] ) ?> <?php _e('Size:', 'comicpress') ?>
</p> <?php printf('%dx%d', $info['width'], $info['height'] ) ?>
<?php } ?> </p>
</div> <?php } ?>
<br style="clear: both" /> </div>
</div> <br style="clear: both" />
<?php } ?> </div>
</div> <?php } ?>
</div>
<?php } ?>
<?php } ?> <?php } ?>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -144,6 +144,14 @@ function RT($which, $restrictions = null, $override_post = null) {
return $post; return $post;
} }
function M($override_post = null) {
global $post;
$post_to_use = !is_null($override_post) ? $override_post : $post;
$comic_post = new ComicPressComicPost($post_to_use);
return $comic_post->get_attachments();
}
/** /**
* Display the list of Storyline categories. * Display the list of Storyline categories.
*/ */

View File

@ -11,6 +11,11 @@
RT('previous', array('root_of' => '__post')); the_title(); echo '<br />'; RT('previous', array('root_of' => '__post')); the_title(); echo '<br />';
RT('previous'); the_title(); echo '<br />'; RT('previous'); the_title(); echo '<br />';
Restore(); the_title(); echo '<br />'; Restore(); the_title(); echo '<br />';
foreach (M() as $image) {
echo $image->embed();
}
RT('next'); the_title(); echo '<br />'; RT('next'); the_title(); echo '<br />';
RT('next', array('root_of' => '__post')); the_title(); echo '<br />'; RT('next', array('root_of' => '__post')); the_title(); echo '<br />';
RT('next', 'from_post'); the_title(); echo '<br />'; RT('next', 'from_post'); the_title(); echo '<br />';

View File

@ -20,7 +20,7 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
) )
); );
} }
/** /**
* @dataProvider providerTestGenerateComicTag * @dataProvider providerTestGenerateComicTag
*/ */
@ -30,7 +30,7 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
'comic_dimensions' => $dimensions 'comic_dimensions' => $dimensions
) )
); );
$source = $this->p->get_comic_img_tag("test.gif", "comic"); $source = $this->p->get_comic_img_tag("test.gif", "comic");
if (count($parts = explode("x", $dimensions)) == 2) { if (count($parts = explode("x", $dimensions)) == 2) {
@ -46,83 +46,54 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
} }
} }
} }
function testNormalizeComicImageOrdering() { function providerTestNormalizeOrdering() {
$p = $this->getMock('ComicPressComicPost', array('get_comic_image_attachments')); return array(
array(
$comic_attachments = array( array('attachment-1'),
array( array(),
'ID' => 2, array('attachment-1' => true)
'post_parent' => 1, ),
'post_title' => 'Comic one', array(
'post_meta' => array( array('attachment-1'),
'comic_image_type' => 'comic' array('attachment-1' => true, 'attachment-2' => true),
), array('attachment-1' => true)
'post_date' => 1 ),
), array(
array( array('attachment-1'),
'ID' => 3, array('attachment-1' => array('rss' => array('attachment-2' => true))),
'post_parent' => 1, array('attachment-1' => true)
'post_title' => 'Comic two', ),
'post_meta' => array( array(
'comic_image_type' => 'comic' array('attachment-1', 'attachment-2', 'attachment-3'),
), array('attachment-1' => array('rss' => array('attachment-2' => true))),
'post_date' => 2 array('attachment-1' => array('rss' => array('attachment-2' => true)), 'attachment-3' => true)
), ),
array( );
'ID' => 4,
'post_parent' => 1,
'post_title' => 'Comic three',
'post_meta' => array(
'comic_image_type' => 'rss'
),
'post_date' => 4
),
array(
'ID' => 5,
'post_parent' => 1,
'post_title' => 'Comic four',
'post_meta' => array(
'comic_image_type' => 'rss'
),
'post_date' => 3
),
);
$attachments = array();
foreach ($comic_attachments as $attachment_info) {
$attachment = (object)array();
foreach ($attachment_info as $field => $value) {
switch ($field) {
case "post_meta":
foreach ($value as $meta => $meta_value) {
update_post_meta($attachment_info['ID'], $meta, $meta_value);
}
break;
case "post_date":
$attachment->{$field} = date("r", $value);
break;
default:
$attachment->{$field} = $value;
break;
}
}
$attachments[] = $attachment;
}
$p->expects($this->any())->method('get_comic_image_attachments')->will($this->returnValue($attachments));
wp_insert_post((object)array('ID' => 1));
update_post_meta(1, 'comic_ordering', array('comic' => array(3)));
$p->post = (object)array('ID' => 1);
$result = $p->normalize_comic_image_ordering();
$this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), $result);
$this->assertEquals(array('comic' => array(3,2), 'rss' => array(5,4)), get_post_meta(1, 'comic_ordering', true));
} }
/**
* @dataProvider providerTestNormalizeOrdering
*/
function testNormalizeOrdering($attachments, $current_meta, $expected_result) {
$p = $this->getMock('ComicPressComicPost', array('get_attachments'));
$attachment_objects = array();
foreach ($attachments as $attachment) {
$attachment_objects[] = (object)array('id' => $attachment);
}
$p->expects($this->any())->method('get_attachments')->will($this->returnValue($attachment_objects));
wp_insert_post((object)array('ID' => 1));
update_post_meta(1, 'image-ordering', $current_meta);
$p->post = (object)array('ID' => 1);
$this->assertEquals($expected_result, $p->normalize_ordering());
$this->assertEquals($expected_result, get_post_meta(1, 'image-ordering', true));
}
function providerTestChangeComicImageOrdering() { function providerTestChangeComicImageOrdering() {
return array( return array(
array( array(
@ -148,17 +119,17 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
), ),
); );
} }
/** /**
* @dataProvider providerTestChangeComicImageOrdering * @dataProvider providerTestChangeComicImageOrdering
* @covers ComicPressComicPost::change_comic_image_ordering * @covers ComicPressComicPost::change_comic_image_ordering
*/ */
function testChangeComicImageOrdering($current_ordering, $revised_ordering, $expected_result) { function testChangeComicImageOrdering($current_ordering, $revised_ordering, $expected_result) {
update_post_meta(1, 'comic_ordering', $current_ordering); update_post_meta(1, 'comic_ordering', $current_ordering);
$this->p->post = (object)array('ID' => 1); $this->p->post = (object)array('ID' => 1);
$this->p->change_comic_image_ordering($revised_ordering); $this->p->change_comic_image_ordering($revised_ordering);
$this->assertEquals($expected_result, get_post_meta(1, 'comic_ordering', true)); $this->assertEquals($expected_result, get_post_meta(1, 'comic_ordering', true));
} }
@ -190,7 +161,7 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
), ),
); );
} }
/** /**
* @dataProvider providerTestFindParents * @dataProvider providerTestFindParents
*/ */
@ -199,11 +170,11 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
add_category(2, (object)array('slug' => 'comic', 'parent' => 1)); add_category(2, (object)array('slug' => 'comic', 'parent' => 1));
add_category(3, (object)array('slug' => 'part-1', 'parent' => 2)); add_category(3, (object)array('slug' => 'part-1', 'parent' => 2));
add_category(4, (object)array('slug' => 'blog', 'parent' => 1)); add_category(4, (object)array('slug' => 'blog', 'parent' => 1));
wp_set_post_categories(1, $post_categories); wp_set_post_categories(1, $post_categories);
$this->p->post = (object)array('ID' => 1); $this->p->post = (object)array('ID' => 1);
$this->assertEquals($expected_result, $this->p->find_parents()); $this->assertEquals($expected_result, $this->p->find_parents());
} }
} }

View File

@ -0,0 +1,39 @@
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once('backends/ComicPressBackendAttachment.inc');
class ComicPressBackendAttachmentTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
}
function providerTestGenerateFromPost() {
return array(
array(array(), false),
array(array((object)array('ID' => 1)), array('attachment-1'))
);
}
/**
* @dataProvider providerTestGenerateFromPost
*/
function testGenerateFromPost($get_children_response, $expected_ids) {
_set_get_children(array(
'post_parent' => 1,
'post_type' => 'attachment',
'post_mime_type' => 'image'
), $get_children_response);
$results = ComicPressBackendAttachment::generate_from_post((object)array('ID' => 1));
if ($expected_ids === false) {
$this->assertTrue(empty($results));
} else {
$this->assertEquals(count($expected_ids), count($results));
foreach ($results as $result) {
$this->assertTrue(in_array($result->id, $expected_ids));
}
}
}
}