diff --git a/test/ViewMainTest.php b/test/ViewMainTest.php index d5cf620..a56e371 100644 --- a/test/ViewMainTest.php +++ b/test/ViewMainTest.php @@ -7,9 +7,23 @@ require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php'); class ViewMainTest extends PHPUnit_Framework_TestCase { function setUp() { _reset_wp(); + $_GET = array(); + _set_current_option('is_admin', true); $this->m = new PluginWonderfulViewMain(); } + function testRender() { + $m = $this->getMock('PluginWonderfulViewMain', array('_render_memberid_settings', '_render_adbox_information', '_render_instructions', '_create_nonce', '_get_first_adboxid')); + + $m->expects($this->once())->method('_render_memberid_settings'); + $m->expects($this->once())->method('_render_adbox_information'); + $m->expects($this->once())->method('_render_instructions'); + $m->expects($this->once())->method('_create_nonce'); + $m->expects($this->once())->method('_get_first_adboxid'); + + $m->render(); + } + function testCreateNonce() { $this->m->_create_nonce(); $result = _get_nonce('plugin-wonderful'); @@ -36,6 +50,40 @@ class ViewMainTest extends PHPUnit_Framework_TestCase { ); } + function providerTestGetFirstAdboxID() { + return array( + array(false, null), + array( + (object)array('template_tag_id' => 'meow'), + "'meow'" + ), + array( + (object)array('adboxid' => '123'), + "123" + ) + ); + } + + /** + * @dataProvider providerTestGetFirstAdboxID + */ + function testGetFirstAdboxID($publisher_info, $expected_result) { + global $plugin_wonderful; + + $plugin_wonderful = (object)array( + 'publisher_info' => false + ); + + if (is_object($publisher_info)) { + $plugin_wonderful->publisher_info = (object)array( + 'adboxes' => array($publisher_info) + ); + } + + $this->m->_get_first_adboxid(); + $this->assertSame($expected_result, $this->m->first_adboxid); + } + /** * @dataProvider providerTestRenderMemberIDSettings */ @@ -44,6 +92,8 @@ class ViewMainTest extends PHPUnit_Framework_TestCase { update_option($key, $value); } + $this->m->_pw_nonce = "345"; + ob_start(); $this->m->_render_memberid_settings(); $source = ob_get_clean(); @@ -53,12 +103,91 @@ class ViewMainTest extends PHPUnit_Framework_TestCase { foreach (array( '//input[@id="memberid" and @value="123"]' => true, + '//input[@name="_pw_nonce" and @value="345"]' => true, '//input[@name="use-standardcode" and ' . (($options['plugin-wonderful-use-standardcode'] == 1) ? '@checked' : 'not(@checked)') . ']' => true, '//input[@name="enable-body-copy-embedding" and ' . (($options['plugin-wonderful-enable-body-copy-embedding'] == 1) ? '@checked' : 'not(@checked)') . ']' => true, ) as $xpath => $value) { $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); } } + + function testRenderAdboxInformation() { + global $plugin_wonderful; + + $plugin_wonderful = (object)array( + 'publisher_info' => false + ); + + $this->m->_pw_nonce = "345"; + + foreach (array(false, true) as $has_publisher_info) { + if ($has_publisher_info) { + $plugin_wonderful->publisher_info = (object)array( + 'adboxes' => array( + (object)array( + 'url' => 'url', + 'sitename' => 'sitename', + 'description' => 'this is a very long description that is over seventy characters in length and should get an ellipsis', + 'adtype' => 'square', + 'dimensions' => '1x2', + 'adboxid' => '12345', + 'template_tag_id' => 'tag', + 'in_rss_feed' => 1 + ) + ) + ); + } + } + + ob_start(); + $this->m->_render_adbox_information(); + $source = ob_get_clean(); + + if ($has_publisher_info) { + $this->assertTrue(!empty($source)); + $this->assertTrue(($xml = _to_xml($source, true)) !== false); + + foreach (array( + '//input[@name="_pw_nonce" and @value="345"]' => true, + '//table[1]/tr[2]/td[1]/a[@href="url" and contains(@title, "url")]' => 'sitename', + '//table[1]/tr[2]/td[2 and contains(text(), "...")]' => true, + '//table[1]/tr[2]/td[3]' => 'square - 1x2', + '//table[1]/tr[2]/td[4]/input[@name="template_tag_id[12345]" and @value="tag"]' => true, + '//table[1]/tr[2]/td[5]/input[@name="in_rss_feed[12345]" and @checked]' => true, + '//table[1]/tr[2]/td[6]/tt' => "the_project_wonderful_ad('tag')" + ) as $xpath => $value) { + $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); + } + } else { + $this->assertTrue(empty($source)); + } + } + + function testInstructions() { + global $plugin_wonderful; + + $plugin_wonderful->publisher_info = true; + + $this->m->_pw_nonce = "345"; + $this->m->first_adboxid = "123"; + $_GET['allow-destroy'] = 1; + + ob_start(); + $this->m->_render_instructions(); + $source = ob_get_clean(); + + $this->assertTrue(!empty($source)); + $this->assertTrue(($xml = _to_xml($source, true)) !== false); + + foreach (array( + '//tt[contains(text(), "the_project_wonderful_ad(123)")]' => true, + '//tt[contains(text(), "PW(123)")]' => true, + '//tt[contains(text(), "PW\(123\)")]' => true, + '//form[@id="allow-destroy-handler"]/input[@name="_pw_nonce" and @value="345"]' => true + ) as $xpath => $value) { + $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); + } + } } ?> \ No newline at end of file diff --git a/views/PluginWonderfulViewMain.php b/views/PluginWonderfulViewMain.php index 2dbbab2..bf0363a 100644 --- a/views/PluginWonderfulViewMain.php +++ b/views/PluginWonderfulViewMain.php @@ -1,17 +1,53 @@ _create_nonce(); + $this->_get_first_adboxid(); + $this->_render_memberid_settings(); + $this->_render_adbox_information(); + $this->_render_instructions(); + } + function _create_nonce() { $this->_pw_nonce = wp_create_nonce('plugin-wonderful'); } + function _get_first_adboxid() { + global $plugin_wonderful; + + $this->first_adboxid = null; + if ($plugin_wonderful->publisher_info !== false) { + foreach ($plugin_wonderful->publisher_info->adboxes as $adbox) { + if (empty($this->first_adboxid)) { + if (!empty($adbox->template_tag_id)) { + $this->first_adboxid = "'" . $adbox->template_tag_id . "'"; + } else { + $this->first_adboxid = $adbox->adboxid; + } + break; + } + } + } + } + function _partial_path($name) { - return dirname(__FILE__) . '/' . __CLASS__ . '/' . $name . '.php'; + return dirname(__FILE__) . '/' . __CLASS__ . '/' . $name . '.inc'; } function _render_memberid_settings() { include($this->_partial_path('memberid-settings')); } + + function _render_adbox_information() { + global $plugin_wonderful; + include($this->_partial_path('adbox-information')); + } + + function _render_instructions() { + global $plugin_wonderful; + include($this->_partial_path('instructions')); + } } ?> \ No newline at end of file diff --git a/views/PluginWonderfulViewMain/adbox-information.inc b/views/PluginWonderfulViewMain/adbox-information.inc new file mode 100644 index 0000000..72393c8 --- /dev/null +++ b/views/PluginWonderfulViewMain/adbox-information.inc @@ -0,0 +1,51 @@ + + publisher_info !== false) { ?> +
+ + + \ No newline at end of file diff --git a/views/PluginWonderfulViewMain/instructions.inc b/views/PluginWonderfulViewMain/instructions.inc new file mode 100644 index 0000000..180168e --- /dev/null +++ b/views/PluginWonderfulViewMain/instructions.inc @@ -0,0 +1,44 @@ +publisher_info !== false) { ?> + ++ Appearance -> Widgets to quickly add Project Wonderful advertisements to your site. Having multiple widgets on your site only works in WordPress 2.8 and above.', 'plugin-wonderful') ?> +
+ + ++ +
+ + + <?php the_project_wonderful_ad(first_adboxid ?>) ?> + + + ++ +
+ + PW(first_adboxid ?>) + + + + PW\(first_adboxid ?>\) + ++ +
+ + + ++ +
+ + + + diff --git a/views/PluginWonderfulViewMain/memberid-settings.inc b/views/PluginWonderfulViewMain/memberid-settings.inc new file mode 100644 index 0000000..02f09f0 --- /dev/null +++ b/views/PluginWonderfulViewMain/memberid-settings.inc @@ -0,0 +1,44 @@ + + + \ No newline at end of file diff --git a/views/PluginWonderfulViewMain/memberid-settings.php b/views/PluginWonderfulViewMain/memberid-settings.php deleted file mode 100644 index ce0a7fb..0000000 --- a/views/PluginWonderfulViewMain/memberid-settings.php +++ /dev/null @@ -1,42 +0,0 @@ - \ No newline at end of file diff --git a/views/main.php b/views/main.php deleted file mode 100644 index 9dd8de3..0000000 --- a/views/main.php +++ /dev/null @@ -1,146 +0,0 @@ - - -publisher_info !== false) { ?> - - - - -publisher_info !== false) { ?> - -- Appearance -> Widgets to quickly add Project Wonderful advertisements to your site. Widgets only work in WordPress 2.8 and above.', 'plugin-wonderful') ?> -
- - -- -
- - - <?php the_project_wonderful_ad() ?> - - - - -- -
- - PW() - - - - PW\(\) - -- -
- - - -- -
- - - -