hubblesite-daily-image-word.../test/DailyImageWidgetTest.php

333 lines
8.7 KiB
PHP
Raw Permalink Normal View History

2009-06-02 20:12:47 +00:00
<?php
2009-06-03 21:29:23 +00:00
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../classes/DailyImageWidget.php');
2009-12-21 21:36:57 +00:00
require_once('MockPress/mockpress.php');
2009-06-02 20:12:47 +00:00
class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
2009-06-03 21:29:23 +00:00
function setUp() {
2009-06-09 21:45:59 +00:00
_reset_wp();
2009-06-10 20:44:19 +00:00
wp_create_nonce("hubble");
$_POST = array();
2009-12-21 21:36:57 +00:00
2009-06-10 19:41:27 +00:00
$this->diw = new DailyImageWidget(true);
2009-12-21 21:36:57 +00:00
2009-06-09 21:45:59 +00:00
$this->sample_data = array(
2009-06-04 21:36:51 +00:00
'title' => 'title',
'date' => '12345',
'image_url' => 'image_url',
'gallery_url' => 'gallery_url',
'credits' => 'credits'
);
2009-12-21 21:36:57 +00:00
$this->diw->data = $this->sample_data;
2009-06-09 21:45:59 +00:00
}
function testWidgetRegistered() {
2009-06-10 19:41:27 +00:00
global $wp_test_expectations;
$this->assertEquals("_init", $wp_test_expectations['actions']['init'][1]);
2009-06-03 21:29:23 +00:00
}
2009-06-04 21:36:51 +00:00
function providerTestRetrieveJunkData() {
return array(
array(0),
array(null),
array(false),
array(true),
array(array()),
array((object)array())
2009-12-21 21:36:57 +00:00
);
2009-06-04 21:36:51 +00:00
}
2009-12-21 21:36:57 +00:00
/**
2009-06-04 21:36:51 +00:00
* @dataProvider providerTestRetrieveJunkData
*/
function testRetrieveJunkData($bad_data) {
$this->diw->data = $bad_data;
2009-06-03 21:29:23 +00:00
ob_start();
$this->diw->render();
$result = ob_get_clean();
2009-12-21 21:36:57 +00:00
2009-06-03 21:29:23 +00:00
$this->assertTrue(empty($result));
}
2009-12-21 21:36:57 +00:00
2009-06-04 21:36:51 +00:00
function providerTestTemplateOptions() {
return array(
array(
"image",
array(
'//div[@id="hubblesite-daily-image"]' => false,
2009-07-15 15:53:44 +00:00
'//a[@href="gallery_url&f=wpw" and @title="title"]' => true,
'//a/img[@src="image_url" and @alt="title"]' => true,
2009-06-04 21:36:51 +00:00
)
),
array(
"title",
array(
2009-12-21 21:36:57 +00:00
'//a[@href="gallery_url&f=wpw" and @id="hubblesite-daily-image-title"]' => "title"
2009-06-04 21:36:51 +00:00
)
),
array(
"credits",
array(
'//div[@id="hubblesite-daily-image-credits"]' => 'credits'
2009-06-04 21:36:51 +00:00
)
)
2009-12-21 21:36:57 +00:00
);
2009-06-04 21:36:51 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-04 21:36:51 +00:00
/**
* @dataProvider providerTestTemplateOptions
*/
function testTemplateOptions($option_string, $xpath_tests) {
update_option('hubblesite-daily-image-options', $option_string);
2009-12-21 21:36:57 +00:00
2009-06-04 21:36:51 +00:00
ob_start();
$this->diw->render(array(
'before_widget' => "",
'after_widget' => "",
'before_title' => "",
'after_title' => ""
));
2009-06-04 21:36:51 +00:00
$result = ob_get_clean();
2009-12-21 21:36:57 +00:00
2009-06-04 21:36:51 +00:00
$this->assertTrue(!empty($result));
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
$this->assertTrue(($xml = _to_xml($result, true)) !== false);
2009-06-04 21:36:51 +00:00
foreach ($xpath_tests as $xpath => $result) {
$this->assertTrue(_xpath_test($xml, $xpath, $result), $xpath);
}
}
2009-12-21 21:36:57 +00:00
2009-06-04 21:36:51 +00:00
function providerTestGetDisplayOptions() {
return array(
array("", array("title", "image")),
array("meow", array("title", "image")),
2009-06-04 21:36:51 +00:00
array("title", array("title")),
array("title,image", array("title", "image")),
array("title,meow", array("title"))
2009-12-21 21:36:57 +00:00
);
2009-06-04 21:36:51 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-04 21:36:51 +00:00
/**
* @dataProvider providerTestGetDisplayOptions
2009-12-21 21:36:57 +00:00
*/
2009-06-04 21:36:51 +00:00
function testGetDisplayOptions($options, $result) {
update_option('hubblesite-daily-image-options', $options);
$this->assertEquals($result, $this->diw->get_display_options());
}
2009-12-21 21:36:57 +00:00
2009-06-10 19:41:27 +00:00
function testGetDefaultDisplayOptions() {
_reset_wp();
2009-12-21 21:36:57 +00:00
$this->assertFalse(get_option('hubblesite-daily-image-options'));
2009-06-10 19:41:27 +00:00
$this->diw->get_display_options();
$this->assertTrue(get_option('hubblesite-daily-image-options') !== false);
}
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
function testCheckedOptions() {
$this->diw->display_options = array_keys($this->diw->_valid_options);
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
ob_start();
$this->diw->render_ui();
$result = ob_get_clean();
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
$this->assertTrue(($xml = _to_xml($result, true)) !== false);
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
foreach ($this->diw->display_options as $option) {
$this->assertTrue(_node_exists($xml, '//input[@name="hubblesite[' . $option . ']" and @checked="checked"]'));
}
}
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
function providerTestUpdateOptions() {
$d = new DailyImageWidget(true);
$default_display_options = $d->default_display_options;
return array(
array(
array(),
$default_display_options
),
array(
array(
'save-widgets' => "yes"
),
$default_display_options
),
array(
array(
'hubblesite' => array(
'_wpnonce' => "~*NONCE*~"
)
),
$default_display_options
),
array(
array(
'hubblesite' => array(
'_wpnonce' => "~*NONCE*~",
'credits' => "yes"
)
),
array("credits")
),
);
}
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
/**
* @dataProvider providerTestUpdateOptions
*/
2009-12-21 21:36:57 +00:00
function testUpdateOptions($post, $result) {
2009-06-10 20:44:19 +00:00
$_POST = $post;
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
if (isset($_POST['hubblesite']['_wpnonce'])) {
$_POST['hubblesite']['_wpnonce'] = _get_nonce('hubble');
}
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
$this->diw->handle_post();
$this->diw->get_display_options();
$this->assertEquals($result, $this->diw->display_options);
}
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
function providerTestParseBadXML() {
return array(
array(null),
array(false),
array("<xml"),
array("<xml></yml>")
);
}
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
/**
* @dataProvider providerTestParseBadXML
*/
function testParseBadXML($xml) {
foreach (array(true, false) as $simplexml) {
$this->diw->has_simplexml = $simplexml;
$this->assertFalse($this->diw->parse_xml($xml));
2009-12-21 21:36:57 +00:00
}
2009-06-09 19:55:50 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
function testParseXML() {
foreach (array(true, false) as $simplexml) {
$this->diw->has_simplexml = $simplexml;
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
$result = $this->diw->parse_xml(
"<gallery>" .
"<title>title</title>" .
"<caption>caption</caption>" .
"<date>12345</date>" .
"<image_url>image_url</image_url>" .
"<gallery_url>gallery_url</gallery_url>" .
"<credits>credits</credits>" .
"</gallery>"
);
2009-12-21 21:36:57 +00:00
2009-06-09 19:55:50 +00:00
$this->assertEquals(
2009-06-09 21:45:59 +00:00
$this->sample_data,
2009-06-09 19:55:50 +00:00
$result,
"simplexml? $simplexml"
);
}
}
2009-12-21 21:36:57 +00:00
2009-06-09 21:45:59 +00:00
function testWidgetUI() {
ob_start();
$this->diw->render_ui();
$result = ob_get_clean();
2009-12-21 21:36:57 +00:00
2009-06-09 21:45:59 +00:00
$this->assertTrue(!empty($result));
2009-12-21 21:36:57 +00:00
2009-06-09 21:45:59 +00:00
$this->assertTrue(($xml = _to_xml($result, true)) !== false);
foreach ($this->diw->_valid_options as $option => $label) {
2009-12-21 21:36:57 +00:00
$xpath = "//label[contains(text(), '${label}')]";
2009-06-09 21:45:59 +00:00
$this->assertTrue(_xpath_test($xml, $xpath, true), $xpath);
2009-06-10 20:44:19 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-10 20:44:19 +00:00
foreach (array(
'//input[@type="hidden" and @name="hubblesite[_wpnonce]"]' => true
) as $xpath => $value) {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
}
2009-06-09 21:45:59 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-29 16:50:55 +00:00
function providerTestGetCachedData() {
return array(
array(time() + 86500, true),
2009-12-21 21:46:16 +00:00
array(time() + 86500, array('test' => 'test'), false),
2009-06-29 16:50:55 +00:00
array(time() - 86500, false),
array(null, false)
2009-12-21 21:36:57 +00:00
);
2009-06-29 16:50:55 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-29 16:50:55 +00:00
/**
* @dataProvider providerTestGetCachedData
*/
2009-12-21 21:46:16 +00:00
function testGetCachedData($test_time, $has_sample_data, $expected_return = null) {
2009-06-29 16:50:55 +00:00
if (!is_null($test_time)) {
2009-12-21 21:46:16 +00:00
if ($has_sample_data === true) {
$has_sample_data = $this->sample_data;
}
update_option('hubblesite-daily-image-cache', array($test_time, $has_sample_data));
2009-06-29 16:50:55 +00:00
} else {
2009-12-21 21:36:57 +00:00
update_option('hubblesite-daily-image-cache', null);
2009-06-29 16:50:55 +00:00
}
2009-12-21 21:36:57 +00:00
2009-12-21 21:46:16 +00:00
if (is_null($expected_return)) {
$expected_return = $has_sample_data ? $has_sample_data : false;
}
$this->assertEquals($expected_return, $this->diw->_get_cached_data());
2009-06-09 21:45:59 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-29 15:34:47 +00:00
function providerTestLoadData() {
return array(
array(true, null, null, true),
array(false, false, null, false),
array(false, true, false, false),
array(false, true, true, true)
2009-12-21 21:36:57 +00:00
);
2009-06-29 15:34:47 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-29 15:34:47 +00:00
/**
* @dataProvider providerTestLoadData
*/
2009-12-21 21:36:57 +00:00
function testLoadData($get_cached_data, $get_from_data_source, $parse_xml_result, $expected_return) {
2009-06-10 19:41:27 +00:00
$diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml'));
2009-12-21 21:36:57 +00:00
2009-06-29 15:34:47 +00:00
$diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue($get_cached_data));
if ($get_cached_data == false) {
$diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue($get_from_data_source));
if ($get_from_data_source) {
$diw->expects($this->once())->method('parse_xml')->will($this->returnValue($parse_xml_result));
}
}
2009-12-21 21:36:57 +00:00
2009-06-29 15:34:47 +00:00
$this->assertEquals($expected_return, $diw->_load_data());
2009-12-21 21:36:57 +00:00
2009-06-29 15:34:47 +00:00
$this->assertEquals($parse_xml_result, is_array(get_option('hubblesite-daily-image-cache')));
2009-06-10 19:41:27 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-29 15:42:57 +00:00
function providerTestWidowProtection() {
return array(
array("this is fixed", "this is&nbsp;fixed"),
array("<p>this is fixed</p>" ,"<p>this is&nbsp;fixed</p>"),
array("<a>this is fixed</a>", "<a>this is&nbsp;fixed</a>"),
array("<a href='meow'>word</a>", "<a href='meow'>word</a>"),
array("<p>this is fixed</p><p>Also fixed</p>", '<p>this is&nbsp;fixed</p><p>Also&nbsp;fixed</p>')
2009-12-21 21:36:57 +00:00
);
2009-06-29 15:42:57 +00:00
}
2009-12-21 21:36:57 +00:00
2009-06-29 15:42:57 +00:00
/**
* @dataProvider providerTestWidowProtection
*/
function testWidowProtection($source, $result) {
$this->assertEquals($result, $this->diw->_fix_widows($source));
}
2009-06-02 20:12:47 +00:00
}
2009-12-21 21:36:57 +00:00
?>