comicpress-core/test/ComicPressAdminTest.php

243 lines
6.3 KiB
PHP

<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once('ComicPressAdmin.inc');
class ComicPressAdminTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$_POST = $_REQUEST = array();
$this->admin = new ComicPressAdmin();
}
function testCreateDimensionSelector() {
$source = $this->admin->create_dimension_selector("test", "760x340");
$this->assertTrue(($xml = _to_xml($source, true)) !== false);
foreach (array(
'//input[@name="test[width]" and @value="760"]' => true,
'//input[@name="test[height]" and @value="340"]' => true,
) as $xpath => $value) {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
}
}
function providerTestHandleUpdateComicPressOptions() {
return array(
array(
array('comic_dimensions' => '150x150'),
array('cp' => array(
'comic_dimensions' => 'test'
)),
array('comic_dimensions' => '150x150')
),
array(
array('comic_dimensions' => '150x150'),
array('cp' => array(
'comic_dimensions' => array(
'width' => '150',
'height' => ''
)
)),
array('comic_dimensions' => '150x')
),
array(
array('comic_dimensions' => '150x150'),
array('cp' => array(
'comic_dimensions' => array(
'width' => '150.1',
'height' => ''
)
)),
array('comic_dimensions' => '150x150')
),
);
}
/**
* @dataProvider providerTestHandleUpdateComicPressOptions
*/
function testHandleUpdateComicPressOptions($original, $change, $new) {
$this->admin->comicpress = $this->getMock('ComicPress', array('save', 'init'));
$this->admin->comicpress->comicpress_options = array(
'comic_dimensions' => '760x',
'rss_dimensions' => '350x',
'archive_dimensions' => '125x'
);
$this->admin->comicpress->comicpress_options = array_merge($this->admin->comicpress->comicpress_options, $original);
add_category(2, (object)array('name' => 'test'));
$_POST = $change;
if (isset($_POST['cp'])) {
$this->admin->handle_update_comicpress_options($_POST['cp']);
}
foreach ($new as $key => $value) {
$this->assertEquals($value, $this->admin->comicpress->comicpress_options[$key]);
}
}
function providerTestUpdateAttachments() {
return array(
array(
array(
'post_meta' => array(),
),
array(
'comicpress_management' => "yes"
),
array(
'post_meta' => array(
'comicpress' => array(
'managed' => true
)
),
),
),
array(
array(
'post' => array(
'post_parent' => 0
),
),
array(
'post_parent' => "2"
),
array(
'post' => array(
'post_parent' => 0
),
),
),
array(
array(
'post' => array(
'post_parent' => 0
),
),
array(
'post_parent' => "2",
'comicpress_management' => "yes"
),
array(
'post' => array(
'post_parent' => 2,
),
'post_meta' => array(
'comicpress' => array(
'managed' => true
)
)
),
)
);
}
/**
* @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;
}
}
$_POST = array(
'attachments' => array('1' => $changes)
);
$this->admin->handle_update_attachments();
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));
}
break;
case "post":
$post = get_post(1);
foreach ($settings as $key => $value) {
$this->assertEquals($value, $post->{$key});
}
}
}
}
function providerTestUpdateZoomSliderMeta() {
return array(
array(false),
array(array()),
array(array('zoom_level' => 50))
);
}
/**
* @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'));
}
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();
}
}
?>