save post for admin

This commit is contained in:
John Bintz 2010-02-12 16:55:11 -05:00
parent 195ac26b8a
commit feeceaf1fa
2 changed files with 112 additions and 12 deletions

View File

@ -376,23 +376,29 @@ class ComicPressBackendFilesystemAdmin {
function save_post($post_id) { function save_post($post_id) {
if (isset($_POST['cp'])) { if (isset($_POST['cp'])) {
$info = $_POST['cp']; $info = $_POST['cp'];
if (isset($info['attachments'])) { if (is_array($info)) {
$image_meta_updates = array(); if (isset($info['attachments'])) {
foreach ($info['attachments'] as $id => $properties) { if (is_array($info['attachments'])) {
if ($backend = ComicPressBackend::generate_from_id($id)) { $image_meta_updates = array();
if (is_a($backend, 'ComicPressBackendFilesystem')) { foreach ($info['attachments'] as $id => $properties) {
$image_meta_updates[$backend->root] = array(); if (is_array($properties)) {
foreach (array('alt_text', 'title_text') as $field) { if ($backend = ComicPressBackend::generate_from_id($id)) {
if (isset($properties[$field])) { if (is_a($backend, 'ComicPressBackendFilesystem')) {
$image_meta_updates[$backend->root][$field] = strip_tags($properties[$field]); $image_meta_updates[$backend->root] = array();
} else { foreach (array('alt_text', 'title_text') as $field) {
$image_meta_updates[$backend->root][$field] = ''; if (isset($properties[$field])) {
$image_meta_updates[$backend->root][$field] = strip_tags($properties[$field]);
} else {
$image_meta_updates[$backend->root][$field] = '';
}
}
}
} }
} }
} }
update_post_meta($post_id, 'backend_filesystem_image_meta', $image_meta_updates);
} }
} }
update_post_meta($post_id, 'backend_filesystem_image_meta', $image_meta_updates);
} }
} }
} }

View File

@ -0,0 +1,94 @@
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once('backends/ComicPressBackendFilesystem.inc');
require_once('ComicPress.inc');
class ComicPressBackendFilesystemAdminTest extends PHPUnit_Framework_TestCase {
function providerTestSavePost() {
return array(
array(
array(), array()
),
array(
array('cp' => 'test'), array()
),
array(
array('cp' => array(
'attachments' => 'test'
)), array()
),
array(
array('cp' => array(
'attachments' => array(
'bad' => 'bad'
)
)), array()
),
array(
array('cp' => array(
'attachments' => array(
'filesystem' => 'bad'
)
)), array()
),
array(
array('cp' => array(
'attachments' => array(
'filesystem' => array(
'bad' => 'bad'
)
)
)), array(
'root' => array(
'alt_text' => '',
'title_text' => ''
)
)
),
array(
array('cp' => array(
'attachments' => array(
'filesystem' => array(
'alt_text' => 'alt'
)
)
)), array(
'root' => array(
'alt_text' => 'alt',
'title_text' => ''
)
)
),
);
}
/**
* @dataProvider providerTestSavePost
*/
function testSavePost($post, $expected_post_meta) {
$filesystem = $this->getMock('ComicPressBackendFilesystem');
$filesystem->root = 'root';
$faulty = (object)array('not a' => 'backend');
$factory = $this->getMock('ComicPressSavePostBackend', array('generate_from_id'));
$factory->expects($this->any())
->method('generate_from_id')
->will($this->returnCallback(function($id) use ($filesystem, $faulty) {
return ($id == 'filesystem') ? $filesystem : $faulty;
}));
$comicpress = ComicPress::get_instance(true);
$comicpress->backends = array($factory);
$_POST = $post;
update_post_meta(1, 'backend_filesystem_image_meta', array());
ComicPressBackendFilesystemAdmin::save_post(1);
$this->assertEquals($expected_post_meta, get_post_meta(1, 'backend_filesystem_image_meta', true));
}
}