diff --git a/classes/PostFixtures.inc b/classes/PostFixtures.inc
index 1ddbf10..d6e33d8 100644
--- a/classes/PostFixtures.inc
+++ b/classes/PostFixtures.inc
@@ -9,15 +9,29 @@ class PostFixtures {
/**
* Initialize the plugin.
*/
+ // @codeCoverageIgnoreStart
function init() {
- add_action('admin_menu', array(&$this, 'admin_menu'));
- add_action('admin_notices', array(&$this, 'admin_notices'));
- add_action("admin_enqueue_scripts", array(&$this, 'admin_enqueue_scripts'), 10, 1);
+ foreach (array(
+ 'admin_init' => array(),
+ 'admin_menu' => array(),
+ 'admin_notices' => array(),
+ 'admin_enqueue_scripts' => array(10, 1),
+ ) as $method => $additional_params) {
+ call_user_func_array(
+ 'add_action',
+ array_merge(
+ array($method, array(&$this, $method)),
+ $additional_params
+ )
+ );
+ }
}
+ // @codeCoverageIgnoreEnd
/**
* Print any admin notices.
*/
+ // @codeCoverageIgnoreStart
function admin_notices() {
if (!empty($this->messages)) { ?>
@@ -27,6 +41,7 @@ class PostFixtures {
hook_suffix = add_submenu_page('tools.php', __('Post Fixtures', 'post-fixtures'), __('Post Fixtures', 'post-fixtures'), 'manage_options', 'post-fixtures', array(&$this, 'render_admin'));
+ }
+ // @codeCoverageIgnoreEnd
- if (isset($_POST['pf'])) {
- if (isset($_POST['pf']['_nonce'])) {
- if (wp_verify_nonce($_POST['pf']['_nonce'], 'post-fixtures')) {
- $this->handle_update($_POST['pf']);
- }
+ /**
+ * Handle the admin_init action.
+ * This is where the processing happens.
+ */
+ function admin_init() {
+ if (isset($_POST['pf']['_nonce'])) {
+ if (wp_verify_nonce($_POST['pf']['_nonce'], 'post-fixtures')) {
+ $this->handle_update($_POST['pf']);
}
}
+ unset($_POST['pf']);
}
/**
* Handle the admin_enqueue_scripts action.
* @param string $hook_suffix The hook suffix for the page being loaded.
*/
+ // @codeCoverageIgnoreStart
function admin_enqueue_scripts($hook_suffix) {
if ($this->hook_suffix == $hook_suffix) {
wp_enqueue_script('jquery');
wp_enqueue_style('post-fixtures', plugin_dir_url(dirname(__FILE__)) . 'style.css');
}
}
+ // @codeCoverageIgnoreEnd
/**
* Render the admin page.
diff --git a/test/PostFixturesTest.php b/test/PostFixturesTest.php
index 6d0089b..77fef5f 100644
--- a/test/PostFixturesTest.php
+++ b/test/PostFixturesTest.php
@@ -9,6 +9,11 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
_reset_wp();
$this->pf = new PostFixtures();
+ $_POST = array();
+ }
+
+ function tearDown() {
+ $_POST = array();
}
function providerTestParseJSON() {
@@ -278,4 +283,51 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
}
}
}
+
+ function providerTestHandleUpdate() {
+ return array(
+ array(array(), false),
+ array(array('is_ok' => true, 'data' => '{]'), false),
+ array(array('is_ok' => true, 'data' => '{"test": "test"}'), true),
+ );
+ }
+
+ /**
+ * @dataProvider providerTestHandleUpdate
+ */
+ function testHandleUpdate($info, $should_succeed) {
+ $pf = $this->getMock('PostFixtures', array('process_data', 'remove', 'create'));
+
+ foreach (array('process_data', 'remove', 'create') as $method) {
+ $pf->expects($this->{$should_succeed ? 'once' : 'never'}())->method($method);
+ }
+
+ $pf->handle_update($info);
+ }
+
+ function providerTestAdminInit() {
+ return array(
+ array(array(), false),
+ array(array('pf' => false), false),
+ array(array('pf' => array()), false),
+ array(array('pf' => array('_nonce' => false)), false),
+ array(array('pf' => array('_nonce' => 'post-fixtures')), true),
+ );
+ }
+
+ /**
+ * @dataProvider providerTestAdminInit
+ */
+ function testAdminInit($post, $should_update) {
+ $_POST = $post;
+
+ $pf = $this->getMock('PostFixtures', array('handle_update'));
+ $pf->expects($this->{$should_update ? 'once' : 'never'}())->method('handle_update');
+
+ _set_valid_nonce('post-fixtures', 'post-fixtures');
+
+ $pf->admin_init();
+
+ $this->assertTrue(!isset($_POST['pf']));
+ }
}