Extracts duplicate code into reusable methods
Updates unit test
This commit is contained in:
@@ -48,22 +48,11 @@ class CronHelper {
|
|||||||
return Security::generateRandomString();
|
return Security::generateRandomString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static function pingDaemon() {
|
static function pingDaemon($timeout = self::DAEMON_REQUEST_TIMEOUT) {
|
||||||
$url = Router::buildRequest(
|
$url = self::getCronUrl(
|
||||||
CronDaemonEndpoint::ENDPOINT,
|
|
||||||
CronDaemonEndpoint::ACTION_PING_RESPONSE
|
CronDaemonEndpoint::ACTION_PING_RESPONSE
|
||||||
);
|
);
|
||||||
$custom_cron_url = WPHooks::applyFilters('mailpoet_cron_request_url', $url);
|
$result = self::queryCronUrl($url, $timeout);
|
||||||
$url = ($custom_cron_url === $url) ?
|
|
||||||
str_replace(home_url(), self::getSiteUrl(), $url) :
|
|
||||||
$custom_cron_url;
|
|
||||||
$args = array(
|
|
||||||
'blocking' => true,
|
|
||||||
'sslverify' => false,
|
|
||||||
'timeout' => self::DAEMON_REQUEST_TIMEOUT,
|
|
||||||
'user-agent' => 'MailPoet Cron'
|
|
||||||
);
|
|
||||||
$result = wp_remote_get($url, $args);
|
|
||||||
return (is_wp_error($result)) ?
|
return (is_wp_error($result)) ?
|
||||||
$result->get_error_message() :
|
$result->get_error_message() :
|
||||||
wp_remote_retrieve_body($result);
|
wp_remote_retrieve_body($result);
|
||||||
@@ -71,23 +60,34 @@ class CronHelper {
|
|||||||
|
|
||||||
static function accessDaemon($token, $timeout = self::DAEMON_REQUEST_TIMEOUT) {
|
static function accessDaemon($token, $timeout = self::DAEMON_REQUEST_TIMEOUT) {
|
||||||
$data = array('token' => $token);
|
$data = array('token' => $token);
|
||||||
$url = Router::buildRequest(
|
$url = self::getCronUrl(
|
||||||
CronDaemonEndpoint::ENDPOINT,
|
|
||||||
CronDaemonEndpoint::ACTION_RUN,
|
CronDaemonEndpoint::ACTION_RUN,
|
||||||
$data
|
$data
|
||||||
);
|
);
|
||||||
$custom_cron_url = WPHooks::applyFilters('mailpoet_cron_request_url', $url);
|
$result = self::queryCronUrl($url, $timeout);
|
||||||
$url = ($custom_cron_url === $url) ?
|
return wp_remote_retrieve_body($result);
|
||||||
str_replace(home_url(), self::getSiteUrl(), $url) :
|
}
|
||||||
$custom_cron_url;
|
|
||||||
|
static function queryCronUrl($url, $timeout) {
|
||||||
$args = array(
|
$args = array(
|
||||||
'blocking' => true,
|
'blocking' => true,
|
||||||
'sslverify' => false,
|
'sslverify' => false,
|
||||||
'timeout' => $timeout,
|
'timeout' => $timeout,
|
||||||
'user-agent' => 'MailPoet Cron'
|
'user-agent' => 'MailPoet Cron'
|
||||||
);
|
);
|
||||||
$result = wp_remote_get($url, $args);
|
return wp_remote_get($url, $args);
|
||||||
return wp_remote_retrieve_body($result);
|
}
|
||||||
|
|
||||||
|
static function getCronUrl($action, $data = false) {
|
||||||
|
$url = Router::buildRequest(
|
||||||
|
CronDaemonEndpoint::ENDPOINT,
|
||||||
|
$action,
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
$custom_cron_url = WPHooks::applyFilters('mailpoet_cron_request_url', $url);
|
||||||
|
return ($custom_cron_url === $url) ?
|
||||||
|
str_replace(home_url(), self::getSiteUrl(), $url) :
|
||||||
|
$custom_cron_url;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function getSiteUrl($site_url = false) {
|
static function getSiteUrl($site_url = false) {
|
||||||
|
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
namespace MailPoet\Test\Cron;
|
namespace MailPoet\Test\Cron;
|
||||||
|
|
||||||
|
use AspectMock\Test as Mock;
|
||||||
use MailPoet\Cron\CronHelper;
|
use MailPoet\Cron\CronHelper;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\WP\Hooks;
|
|
||||||
|
|
||||||
class CronHelperTest extends \MailPoetTest {
|
class CronHelperTest extends \MailPoetTest {
|
||||||
function _before() {
|
function _before() {
|
||||||
@@ -74,38 +74,30 @@ class CronHelperTest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItCreatesRandomToken() {
|
function testItCreatesRandomToken() {
|
||||||
// random token is a string of 5 characters
|
// random token is a string of 5 characters
|
||||||
$token1 = CronHelper::createToken();
|
$token1 = CronHelper::createToken();
|
||||||
$token2 = CronHelper::createToken();
|
$token2 = CronHelper::createToken();
|
||||||
expect($token1)->notEquals($token2);
|
expect($token1)->notEquals($token2);
|
||||||
expect(is_string($token1))->true();
|
expect(is_string($token1))->true();
|
||||||
expect(strlen($token1))->equals(5);
|
expect(strlen($token1))->equals(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItGetsSiteUrl() {
|
function testItGetsSiteUrl() {
|
||||||
// 1. do nothing when the url is manually overridden via a hook
|
// 1. do nothing when the url does not contain port
|
||||||
$filter = function() {
|
|
||||||
return 'custom_url';
|
|
||||||
};
|
|
||||||
add_filter('mailpoet_cron_request_url', $filter);
|
|
||||||
expect(CronHelper::getSiteUrl())->equals('custom_url');
|
|
||||||
remove_filter('mailpoet_cron_request_url', $filter);
|
|
||||||
|
|
||||||
// 2. do nothing when the url does not contain port
|
|
||||||
$site_url = 'http://example.com';
|
$site_url = 'http://example.com';
|
||||||
expect(CronHelper::getSiteUrl($site_url))->equals($site_url);
|
expect(CronHelper::getSiteUrl($site_url))->equals($site_url);
|
||||||
|
|
||||||
if(getenv('WP_TEST_ENABLE_NETWORK_TESTS') !== 'true') return;
|
if(getenv('WP_TEST_ENABLE_NETWORK_TESTS') !== 'true') return;
|
||||||
|
|
||||||
// 3. when url contains valid port, try connecting to it
|
// 2. when url contains valid port, try connecting to it
|
||||||
$site_url = 'http://example.com:80';
|
$site_url = 'http://example.com:80';
|
||||||
expect(CronHelper::getSiteUrl($site_url))->equals($site_url);
|
expect(CronHelper::getSiteUrl($site_url))->equals($site_url);
|
||||||
|
|
||||||
// 4. when url contains invalid port, try connecting to it. when connection fails,
|
// 3. when url contains invalid port, try connecting to it. when connection fails,
|
||||||
// another attempt will be made to connect to the standard port derived from URL schema
|
// another attempt will be made to connect to the standard port derived from URL schema
|
||||||
$site_url = 'http://example.com:8080';
|
$site_url = 'http://example.com:8080';
|
||||||
expect(CronHelper::getSiteUrl($site_url))->equals('http://example.com');
|
expect(CronHelper::getSiteUrl($site_url))->equals('http://example.com');
|
||||||
|
|
||||||
// 5. when connection can't be established, exception should be thrown
|
// 4. when connection can't be established, exception should be thrown
|
||||||
$site_url = 'https://invalid:80';
|
$site_url = 'https://invalid:80';
|
||||||
try {
|
try {
|
||||||
CronHelper::getSiteUrl($site_url);
|
CronHelper::getSiteUrl($site_url);
|
||||||
@@ -126,12 +118,30 @@ class CronHelperTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItAllowsSettingCustomCronUrl() {
|
||||||
|
$filter = function($url) {
|
||||||
|
expect($url)->contains('&endpoint=cron');
|
||||||
|
return 'http://custom_cron_url';
|
||||||
|
};
|
||||||
|
add_filter('mailpoet_cron_request_url', $filter);
|
||||||
|
expect(CronHelper::getCronUrl('sample_action'))->equals('http://custom_cron_url');
|
||||||
|
remove_filter('mailpoet_cron_request_url', $filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItReturnsErrorMessageAsPingResposneWhenCronUrlCannotBeAccessed() {
|
||||||
|
Mock::double('MailPoet\Cron\CronHelper', [
|
||||||
|
'getSiteUrl' => false
|
||||||
|
]);
|
||||||
|
expect(CronHelper::pingDaemon())->equals('A valid URL was not provided.');
|
||||||
|
}
|
||||||
|
|
||||||
function testItPingsDaemon() {
|
function testItPingsDaemon() {
|
||||||
if(getenv('WP_TEST_ENABLE_NETWORK_TESTS') !== 'true') return;
|
if(getenv('WP_TEST_ENABLE_NETWORK_TESTS') !== 'true') return;
|
||||||
expect(CronHelper::pingDaemon())->equals('pong');
|
expect(CronHelper::pingDaemon())->equals('pong');
|
||||||
}
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
|
Mock::clean();
|
||||||
\ORM::raw_execute('TRUNCATE ' . Setting::$_table);
|
\ORM::raw_execute('TRUNCATE ' . Setting::$_table);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user