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-06-09 17:24:14 +00:00
|
|
|
require_once(dirname(__FILE__) . '/../../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-03 21:29:23 +00:00
|
|
|
$this->diw = new DailyImageWidget();
|
2009-06-04 21:36:51 +00:00
|
|
|
|
2009-06-09 21:45:59 +00:00
|
|
|
$this->sample_data = array(
|
2009-06-04 21:36:51 +00:00
|
|
|
'title' => 'title',
|
|
|
|
'caption' => 'caption',
|
|
|
|
'date' => '12345',
|
|
|
|
'image_url' => 'image_url',
|
|
|
|
'gallery_url' => 'gallery_url',
|
|
|
|
'credits' => 'credits'
|
|
|
|
);
|
|
|
|
|
2009-06-09 21:45:59 +00:00
|
|
|
$this->diw->data = $this->sample_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function testWidgetRegistered() {
|
|
|
|
global $wp_test_expectations;
|
|
|
|
$this->assertEquals("hubblesite-daily-image", $wp_test_expectations['sidebar_widgets'][0]['id']);
|
|
|
|
$this->assertEquals("hubblesite-daily-image", $wp_test_expectations['widget_controls'][0]['name']);
|
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())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
|
|
|
|
$this->assertTrue(empty($result));
|
|
|
|
}
|
2009-06-04 21:36:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
function providerTestTemplateOptions() {
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
"image",
|
|
|
|
array(
|
|
|
|
'//div[@id="hubblesite-daily-image"]' => true,
|
|
|
|
'//div/a[@href="gallery_url" and @title="title"]' => true,
|
|
|
|
'//div/a/img[@src="image_url" and @alt="title"]' => true,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"title",
|
|
|
|
array(
|
|
|
|
'//div/a[@href="gallery_url" and @id="hubblesite-daily-image-title"]' => "title"
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"styles",
|
|
|
|
array(
|
|
|
|
'//style[@type="text/css"]' => true
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"caption",
|
|
|
|
array(
|
|
|
|
'//div/div[@id="hubblesite-daily-image-caption"]' => 'caption'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"credits",
|
|
|
|
array(
|
|
|
|
'//div/div[@id="hubblesite-daily-image-credits"]' => 'credits'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestTemplateOptions
|
|
|
|
*/
|
|
|
|
function testTemplateOptions($option_string, $xpath_tests) {
|
|
|
|
update_option('hubblesite-daily-image-options', $option_string);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$this->diw->render();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
|
|
|
$this->assertTrue(!empty($result));
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function providerTestGetDisplayOptions() {
|
|
|
|
return array(
|
|
|
|
array("", array("title", "image", "styles")),
|
|
|
|
array("meow", array("title", "image", "styles")),
|
|
|
|
array("title", array("title")),
|
|
|
|
array("title,image", array("title", "image")),
|
|
|
|
array("title,meow", array("title"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestGetDisplayOptions
|
|
|
|
*/
|
|
|
|
function testGetDisplayOptions($options, $result) {
|
|
|
|
update_option('hubblesite-daily-image-options', $options);
|
|
|
|
|
|
|
|
$this->assertEquals($result, $this->diw->get_display_options());
|
|
|
|
}
|
2009-06-09 19:55:50 +00:00
|
|
|
|
|
|
|
function providerTestParseBadXML() {
|
|
|
|
return array(
|
|
|
|
array(null),
|
|
|
|
array(false),
|
|
|
|
array("<xml"),
|
|
|
|
array("<xml></yml>")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestParseBadXML
|
|
|
|
*/
|
|
|
|
function testParseBadXML($xml) {
|
|
|
|
foreach (array(true, false) as $simplexml) {
|
|
|
|
$this->diw->has_simplexml = $simplexml;
|
|
|
|
|
|
|
|
$this->assertFalse($this->diw->parse_xml($xml));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testParseXML() {
|
|
|
|
foreach (array(true, false) as $simplexml) {
|
|
|
|
$this->diw->has_simplexml = $simplexml;
|
|
|
|
|
|
|
|
$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>"
|
|
|
|
);
|
|
|
|
|
|
|
|
$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-06-09 21:45:59 +00:00
|
|
|
|
|
|
|
function testWidgetUI() {
|
|
|
|
ob_start();
|
|
|
|
$this->diw->render_ui();
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
|
|
|
$this->assertTrue(!empty($result));
|
|
|
|
|
|
|
|
$this->assertTrue(($xml = _to_xml($result, true)) !== false);
|
|
|
|
foreach ($this->diw->_valid_options as $option => $label) {
|
|
|
|
$xpath = "//label[contains(text(), '${label}')]";
|
|
|
|
$this->assertTrue(_xpath_test($xml, $xpath, true), $xpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetCachedData() {
|
|
|
|
$test_time = time() + 86500;
|
|
|
|
update_option('hubblesite-daily-image-cache', serialize(array($test_time, $this->sample_data)));
|
2009-06-10 17:32:06 +00:00
|
|
|
$this->assertEquals($this->sample_data, $this->diw->_get_cached_data());
|
2009-06-09 21:45:59 +00:00
|
|
|
|
|
|
|
$test_time = time() - 86500;
|
|
|
|
update_option('hubblesite-daily-image-cache', serialize(array($test_time, $this->sample_data)));
|
|
|
|
$this->assertEquals(false, $this->diw->_get_cached_data());
|
|
|
|
|
|
|
|
update_option('hubblesite-daily-image-cache', null);
|
|
|
|
$this->assertEquals(false, $this->diw->_get_cached_data());
|
|
|
|
}
|
2009-06-02 20:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|