comicpress-core/test/ComicPressAdminTest.php

259 lines
6.8 KiB
PHP
Raw Normal View History

2009-07-04 16:08:02 +00:00
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
2009-11-07 17:18:53 +00:00
require_once('ComicPressAdmin.inc');
2009-07-04 16:08:02 +00:00
2009-11-06 02:15:12 +00:00
class ComicPressAdminTest extends PHPUnit_Framework_TestCase {
2009-07-04 16:08:02 +00:00
function setUp() {
_reset_wp();
2009-11-11 12:42:52 +00:00
$_POST = $_REQUEST = array();
2009-11-06 02:15:12 +00:00
$this->admin = new ComicPressAdmin();
2009-07-04 16:08:02 +00:00
}
function testCreateDimensionSelector() {
2009-11-06 02:15:12 +00:00
$source = $this->admin->create_dimension_selector("test", "760x340");
2009-11-08 03:47:28 +00:00
$this->assertTrue(($xml = _to_xml($source, true)) !== false);
2009-11-08 03:47:28 +00:00
foreach (array(
'//input[@name="test[width]" and @value="760"]' => true,
'//input[@name="test[height]" and @value="340"]' => true,
) as $xpath => $value) {
2009-11-08 03:47:28 +00:00
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
}
}
2009-11-08 03:47:28 +00:00
2009-11-11 12:42:52 +00:00
function providerTestHandleUpdateComicPressOptions() {
2009-07-06 03:09:30 +00:00
return array(
2009-11-13 01:25:46 +00:00
array(
array(
'image_types' => array(
'comic' => array('default' => true, 'dimensions' => '500x50')
)
),
array(
'image_types' => array(
'comic' => array('default' => 'yes', 'dimensions' => array('width' => '100', 'height' => '100'))
)
),
array(
'image_types' => array(
'comic' => array('default' => true, 'dimensions' => '100x100')
)
)
),
array(
array(
'image_types' => array(
'comic' => array('default' => true, 'dimensions' => '500x50')
)
),
array(
'image_types' => array(
'comic' => array('dimensions' => array('width' => '500', 'height' => '50')),
'archive' => array('default' => 'yes', 'dimensions' => array('width' => '100', 'height' => '100')),
)
),
array(
'image_types' => array(
'comic' => array('dimensions' => '500x50'),
'archive' => array('default' => true, 'dimensions' => '100x100'),
)
)
),
array(
array(
'image_types' => array(
'comic' => array('default' => true, 'dimensions' => '500x50'),
'archive' => array('dimensions' => '100x100'),
)
),
array(
'image_types' => array(
'archive' => array('default' => 'yes', 'dimensions' => array('width' => '100', 'height' => '100')),
)
),
array(
'image_types' => array(
'archive' => array('default' => true, 'dimensions' => '100x100'),
)
)
),
2009-07-06 03:09:30 +00:00
);
}
/**
2009-11-11 12:42:52 +00:00
* @dataProvider providerTestHandleUpdateComicPressOptions
2009-07-06 03:09:30 +00:00
*/
2009-11-11 12:42:52 +00:00
function testHandleUpdateComicPressOptions($original, $change, $new) {
2009-11-06 02:15:12 +00:00
$this->admin->comicpress = $this->getMock('ComicPress', array('save', 'init'));
$this->admin->comicpress->comicpress_options = array_merge($this->admin->comicpress->comicpress_options, $original);
2009-11-08 03:47:28 +00:00
2009-11-13 01:25:46 +00:00
$this->admin->handle_update_comicpress_options($change);
2009-11-08 03:47:28 +00:00
2009-07-06 03:09:30 +00:00
foreach ($new as $key => $value) {
2009-11-06 02:15:12 +00:00
$this->assertEquals($value, $this->admin->comicpress->comicpress_options[$key]);
2009-07-06 03:09:30 +00:00
}
}
2009-11-08 03:47:28 +00:00
2009-07-21 18:00:23 +00:00
function providerTestUpdateAttachments() {
return array(
array(
array(
'post_meta' => array(),
),
array(
2009-11-11 03:09:53 +00:00
'comicpress_management' => "yes"
2009-07-21 18:00:23 +00:00
),
array(
'post_meta' => array(
2009-11-11 03:12:47 +00:00
'comicpress' => array(
'managed' => true
)
2009-07-21 18:00:23 +00:00
),
),
),
array(
array(
'post' => array(
'post_parent' => 0
),
),
array(
'post_parent' => "2"
),
array(
'post' => array(
'post_parent' => 0
),
),
2009-11-08 03:47:28 +00:00
),
2009-07-21 18:00:23 +00:00
array(
array(
'post' => array(
'post_parent' => 0
),
),
array(
'post_parent' => "2",
2009-11-11 03:09:53 +00:00
'comicpress_management' => "yes"
2009-07-21 18:00:23 +00:00
),
array(
'post' => array(
2009-11-11 03:09:53 +00:00
'post_parent' => 2,
2009-07-21 18:00:23 +00:00
),
2009-11-11 03:09:53 +00:00
'post_meta' => array(
2009-11-11 03:12:47 +00:00
'comicpress' => array(
'managed' => true
)
2009-11-11 03:09:53 +00:00
)
2009-07-21 18:00:23 +00:00
),
)
2009-11-08 03:47:28 +00:00
);
2009-07-21 18:00:23 +00:00
}
2009-11-08 03:47:28 +00:00
2009-07-21 18:00:23 +00:00
/**
* @dataProvider providerTestUpdateAttachments
*/
function testUpdateAttachments($original_settings, $changes, $expected_settings) {
foreach ($original_settings as $settings_type => $settings) {
switch ($settings_type) {
case "post_meta":
foreach ($settings as $key => $value) {
update_post_meta(1, $key, $value);
}
break;
case "post":
wp_insert_post((object)array_merge(array(
'ID' => 1
), $settings));
break;
}
}
2009-11-08 03:47:28 +00:00
2009-07-21 18:00:23 +00:00
$_POST = array(
'attachments' => array('1' => $changes)
);
2009-11-08 03:47:28 +00:00
2009-11-06 02:15:12 +00:00
$this->admin->handle_update_attachments();
2009-11-08 03:47:28 +00:00
2009-07-21 18:00:23 +00:00
foreach ($expected_settings as $settings_type => $settings) {
switch ($settings_type) {
case "post_meta":
foreach ($settings as $key => $value) {
$this->assertEquals($value, get_post_meta(1, $key, true));
}
2009-11-08 03:47:28 +00:00
break;
2009-07-21 18:00:23 +00:00
case "post":
$post = get_post(1);
foreach ($settings as $key => $value) {
$this->assertEquals($value, $post->{$key});
}
}
2009-07-23 00:55:32 +00:00
}
}
2009-11-08 03:47:28 +00:00
function providerTestUpdateZoomSliderMeta() {
2009-08-06 02:29:54 +00:00
return array(
array(false),
array(array()),
array(array('zoom_level' => 50))
2009-08-06 02:29:54 +00:00
);
}
/**
* @dataProvider providerTestUpdateZoomSliderMeta
*/
function testUpdateZoomSliderMeta($initial_usermeta) {
update_usermeta(1, 'comicpress-settings', $initial_usermeta);
$this->admin->_update_zoom_slider_meta(1, 100);
$this->assertEquals(array(
'zoom_level' => 100
), get_usermeta(1, 'comicpress-settings'));
}
2009-11-11 12:42:52 +00:00
function providerTestHandleUpdate() {
return array(
array(array()),
array(array('cp' => true), false),
array(array('cp' => array()), false),
array(array('cp' => array()), true, true, true),
array(array('cp' => array(), 'attachments' => array()), true, true, false),
array(array('cp' => array('action' => 'test')), true, true, false),
array(array('cp' => array('action' => 'comic_ordering')), true, true, false),
);
}
/**
* @dataProvider providerTestHandleUpdate
* @covers ComicPressAdmin::handle_update
*/
function testHandleUpdate($input, $add_nonce = false, $comicpress_load = false, $comicpress_save = false) {
$this->admin->comicpress = $this->getMock('ComicPress', array('save', 'init', 'load'));
if ($comicpress_load) {
$this->admin->comicpress->expects($this->once())->method('load');
}
if ($comicpress_save) {
$this->admin->comicpress->expects($this->once())->method('save');
$this->admin->comicpress->expects($this->once())->method('init');
}
if ($add_nonce) {
if (isset($input['cp'])) {
if (is_array($input['cp'])) {
$input['cp']['_nonce'] = wp_create_nonce('comicpress');
}
}
}
$_POST = $_REQUEST = $input;
$this->admin->handle_update();
}
2009-07-04 16:08:02 +00:00
}
?>