From 202e4b90e15cf71d0c3f92d441475ffa5c08ad18 Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Fri, 21 Oct 2016 13:36:23 +0200 Subject: [PATCH] added unit test for API::checkPermissions --- lib/API/API.php | 1 + tests/unit/API/APITest.php | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/unit/API/APITest.php diff --git a/lib/API/API.php b/lib/API/API.php index 906733ec2e..f52d1f2e2b 100644 --- a/lib/API/API.php +++ b/lib/API/API.php @@ -24,6 +24,7 @@ class API { 'wp_ajax_mailpoet', array($this, 'setupAjax') ); + // ajax (logged out users) add_action( 'wp_ajax_nopriv_mailpoet', diff --git a/tests/unit/API/APITest.php b/tests/unit/API/APITest.php new file mode 100644 index 0000000000..fb07bce600 --- /dev/null +++ b/tests/unit/API/APITest.php @@ -0,0 +1,40 @@ +wp_user_id = null; + $wp_user_id = wp_create_user('WP User', 'pass', 'wp_user@mailpoet.com'); + if(is_wp_error($wp_user_id)) { + // user already exists + $this->wp_user_id = email_exists('user1@mailpoet.com'); + } else { + $this->wp_user_id = $wp_user_id; + } + + $this->api = new API(); + } + + function testItChecksPermissions() { + // logged out user + expect($this->api->checkPermissions())->false(); + + // give administrator role to wp user + $wp_user = get_user_by('id', $this->wp_user_id); + $wp_user->add_role('administrator'); + wp_set_current_user($wp_user->ID, $wp_user->user_login); + + // administrator should have permission + expect($this->api->checkPermissions())->true(); + } + + function _after() { + wp_delete_user($this->wp_user_id); + } +} \ No newline at end of file