diff --git a/classes/PluginWonderful.php b/classes/PluginWonderful.php index b7baaf9..5998195 100644 --- a/classes/PluginWonderful.php +++ b/classes/PluginWonderful.php @@ -164,12 +164,11 @@ class PluginWonderful { } function handle_action() { - $action = "handle_action_" . str_replace("-", "_", preg_replace('#[^a-z\-]#', '', strtolower($_POST['action']))); - if (method_exists($this, $action)) { call_user_func(array($this, $action)); } - - // handle widget updates - if (isset($_POST['pw']['_nonce'])) { - if (wp_verify_nonce($_POST['pw']['_nonce'], "plugin-wonderful")) { $this->handle_action_save_widgets(); } + if (!empty($_POST['_pw_nonce'])) { + if (wp_verify_nonce($_POST['_pw_nonce'], 'plugin-wonderful')) { + $action = "handle_action_" . str_replace("-", "_", preg_replace('#[^a-z\-]#', '', strtolower($_POST['action']))); + if (method_exists($this, $action)) { call_user_func(array($this, $action)); } + } } } diff --git a/test/PluginWonderfulTest.php b/test/PluginWonderfulTest.php index e383e31..ea98335 100644 --- a/test/PluginWonderfulTest.php +++ b/test/PluginWonderfulTest.php @@ -266,8 +266,31 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { } } - function testHandleAction() { - $this->markTestIncomplete(); + function providerTestHandleAction() { + return array( + array(false, false, false), + array(true, false, false), + array(true, true, true) + ); + } + + /** + * @dataProvider providerTestHandleAction + */ + function testHandleAction($has_nonce, $has_verify_nonce, $method_exists) { + if ($has_nonce) { $_POST['_pw_nonce'] = "12345"; } + _set_valid_nonce('plugin-wonderful', $has_verify_nonce ? '12345' : '54321'); + + $pw = $this->getMock('PluginWonderful', $method_exists ? array('handle_action_test') : array('handle_action_invalid')); + $_POST['action'] = 'test'; + + if ($method_exists) { + $pw->expects($this->once())->method('handle_action_test'); + } else { + $pw->expects($this->never())->method('handle_action_invalid'); + } + + $pw->handle_action(); } function testHandleActionSaveWidgets() {