Merge pull request #1628 from mailpoet/mp-api-di
Introduce DI to plugin API [MAILPOET-1637]
This commit is contained in:
@@ -2,20 +2,43 @@
|
|||||||
|
|
||||||
namespace MailPoet\API;
|
namespace MailPoet\API;
|
||||||
|
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Container;
|
||||||
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
||||||
|
use MailPoet\DI\ContainerFactory;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class API {
|
class API {
|
||||||
static function JSON(AccessControl $access_control) {
|
|
||||||
return new \MailPoet\API\JSON\API($access_control);
|
/** @var Container */
|
||||||
|
private static $container;
|
||||||
|
|
||||||
|
static function injectContainer(Container $container) {
|
||||||
|
self::$container = $container;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function JSON() {
|
||||||
|
return self::$container->get(JSON\API::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function MP($version) {
|
static function MP($version) {
|
||||||
|
self::ensureContainerIsLoaded();
|
||||||
$api_class = sprintf('%s\MP\%s\API', __NAMESPACE__, $version);
|
$api_class = sprintf('%s\MP\%s\API', __NAMESPACE__, $version);
|
||||||
if(class_exists($api_class)) {
|
try {
|
||||||
return new $api_class();
|
return self::$container->get($api_class);
|
||||||
|
} catch (ServiceNotFoundException $e) {
|
||||||
|
throw new \Exception(__('Invalid API version.', 'mailpoet'));
|
||||||
}
|
}
|
||||||
throw new \Exception(__('Invalid API version.', 'mailpoet'));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* MP API is used by third party plugins so we have to ensure that container is loaded
|
||||||
|
* @see https://kb.mailpoet.com/article/195-add-subscribers-through-your-own-form-or-plugin
|
||||||
|
*/
|
||||||
|
private static function ensureContainerIsLoaded() {
|
||||||
|
if(!self::$container) {
|
||||||
|
$factory = new ContainerFactory();
|
||||||
|
self::$container = $factory->getContainer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
namespace MailPoet\API\JSON;
|
namespace MailPoet\API\JSON;
|
||||||
|
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Container;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\Util\Helpers;
|
use MailPoet\Util\Helpers;
|
||||||
use MailPoet\Util\Security;
|
use MailPoet\Util\Security;
|
||||||
@@ -20,10 +21,16 @@ class API {
|
|||||||
private $_available_api_versions = array(
|
private $_available_api_versions = array(
|
||||||
'v1'
|
'v1'
|
||||||
);
|
);
|
||||||
|
/** @var Container */
|
||||||
|
private $container;
|
||||||
|
|
||||||
|
/** @var AccessControl */
|
||||||
private $access_control;
|
private $access_control;
|
||||||
|
|
||||||
const CURRENT_VERSION = 'v1';
|
const CURRENT_VERSION = 'v1';
|
||||||
|
|
||||||
function __construct(AccessControl $access_control) {
|
function __construct(Container $container, AccessControl $access_control) {
|
||||||
|
$this->container = $container;
|
||||||
$this->access_control = $access_control;
|
$this->access_control = $access_control;
|
||||||
foreach($this->_available_api_versions as $available_api_version) {
|
foreach($this->_available_api_versions as $available_api_version) {
|
||||||
$this->addEndpointNamespace(
|
$this->addEndpointNamespace(
|
||||||
@@ -135,7 +142,7 @@ class API {
|
|||||||
throw new \Exception(__('Invalid API endpoint.', 'mailpoet'));
|
throw new \Exception(__('Invalid API endpoint.', 'mailpoet'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$endpoint = new $this->_request_endpoint_class();
|
$endpoint = $this->container->get($this->_request_endpoint_class);
|
||||||
|
|
||||||
if(!method_exists($endpoint, $this->_request_method)) {
|
if(!method_exists($endpoint, $this->_request_method)) {
|
||||||
throw new \Exception(__('Invalid API endpoint method.', 'mailpoet'));
|
throw new \Exception(__('Invalid API endpoint method.', 'mailpoet'));
|
||||||
|
@@ -10,13 +10,14 @@ use MailPoet\WP\Posts as WPPosts;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class AutomatedLatestContent extends APIEndpoint {
|
class AutomatedLatestContent extends APIEndpoint {
|
||||||
|
/** @var \MailPoet\Newsletter\AutomatedLatestContent */
|
||||||
public $ALC;
|
public $ALC;
|
||||||
public $permissions = array(
|
public $permissions = array(
|
||||||
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
|
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
|
||||||
);
|
);
|
||||||
|
|
||||||
function __construct() {
|
function __construct(\MailPoet\Newsletter\AutomatedLatestContent $alc) {
|
||||||
$this->ALC = new \MailPoet\Newsletter\AutomatedLatestContent();
|
$this->ALC = $alc;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPostTypes() {
|
function getPostTypes() {
|
||||||
@@ -74,14 +75,12 @@ class AutomatedLatestContent extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getBulkTransformedPosts($data = array()) {
|
function getBulkTransformedPosts($data = array()) {
|
||||||
$alc = new \MailPoet\Newsletter\AutomatedLatestContent();
|
|
||||||
|
|
||||||
$used_posts = array();
|
$used_posts = array();
|
||||||
$rendered_posts = array();
|
$rendered_posts = array();
|
||||||
|
|
||||||
foreach($data['blocks'] as $block) {
|
foreach($data['blocks'] as $block) {
|
||||||
$posts = $alc->getPosts($block, $used_posts);
|
$posts = $this->ALC->getPosts($block, $used_posts);
|
||||||
$rendered_posts[] = $alc->transformPosts($block, $posts);
|
$rendered_posts[] = $this->ALC->transformPosts($block, $posts);
|
||||||
|
|
||||||
foreach($posts as $post) {
|
foreach($posts as $post) {
|
||||||
$used_posts[] = $post->ID;
|
$used_posts[] = $post->ID;
|
||||||
@@ -90,4 +89,4 @@ class AutomatedLatestContent extends APIEndpoint {
|
|||||||
|
|
||||||
return $this->successResponse($rendered_posts);
|
return $this->successResponse($rendered_posts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -15,6 +15,26 @@ use MailPoet\Tasks\Sending;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class API {
|
class API {
|
||||||
|
|
||||||
|
/** @var NewSubscriberNotificationMailer */
|
||||||
|
private $new_subscribe_notification_mailer;
|
||||||
|
|
||||||
|
/** @var ConfirmationEmailMailer */
|
||||||
|
private $confirmation_email_mailer;
|
||||||
|
|
||||||
|
/** @var RequiredCustomFieldValidator */
|
||||||
|
private $required_custom_field_validator;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
NewSubscriberNotificationMailer $new_subscribe_notification_mailer,
|
||||||
|
ConfirmationEmailMailer $confirmation_email_mailer,
|
||||||
|
RequiredCustomFieldValidator $required_custom_field_validator
|
||||||
|
) {
|
||||||
|
$this->new_subscribe_notification_mailer = $new_subscribe_notification_mailer;
|
||||||
|
$this->confirmation_email_mailer = $confirmation_email_mailer;
|
||||||
|
$this->required_custom_field_validator = $required_custom_field_validator;
|
||||||
|
}
|
||||||
|
|
||||||
function getSubscriberFields() {
|
function getSubscriberFields() {
|
||||||
$data = array(
|
$data = array(
|
||||||
array(
|
array(
|
||||||
@@ -168,8 +188,7 @@ class API {
|
|||||||
// if some required default fields are missing, set their values
|
// if some required default fields are missing, set their values
|
||||||
$default_fields = Subscriber::setRequiredFieldsDefaultValues($default_fields);
|
$default_fields = Subscriber::setRequiredFieldsDefaultValues($default_fields);
|
||||||
|
|
||||||
$validator = new RequiredCustomFieldValidator();
|
$this->required_custom_field_validator->validate($custom_fields);
|
||||||
$validator->validate($custom_fields);
|
|
||||||
|
|
||||||
// add subscriber
|
// add subscriber
|
||||||
$new_subscriber = Subscriber::create();
|
$new_subscriber = Subscriber::create();
|
||||||
@@ -255,8 +274,7 @@ class API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function _sendConfirmationEmail(Subscriber $subscriber) {
|
protected function _sendConfirmationEmail(Subscriber $subscriber) {
|
||||||
$sender = new ConfirmationEmailMailer();
|
return $this->confirmation_email_mailer->sendConfirmationEmail($subscriber);
|
||||||
return $sender->sendConfirmationEmail($subscriber);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function _scheduleWelcomeNotification(Subscriber $subscriber, array $segments) {
|
protected function _scheduleWelcomeNotification(Subscriber $subscriber, array $segments) {
|
||||||
@@ -274,7 +292,6 @@ class API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function sendSubscriberNotification(Subscriber $subscriber, array $segment_ids) {
|
private function sendSubscriberNotification(Subscriber $subscriber, array $segment_ids) {
|
||||||
$sender = new NewSubscriberNotificationMailer();
|
$this->new_subscribe_notification_mailer->send($subscriber, Segment::whereIn('id', $segment_ids)->findMany());
|
||||||
$sender->send($subscriber, Segment::whereIn('id', $segment_ids)->findMany());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -99,6 +99,7 @@ class Initializer {
|
|||||||
function loadContainer() {
|
function loadContainer() {
|
||||||
$container_factory = new ContainerFactory(WP_DEBUG);
|
$container_factory = new ContainerFactory(WP_DEBUG);
|
||||||
$this->container = $container_factory->getContainer();
|
$this->container = $container_factory->getContainer();
|
||||||
|
API\API::injectContainer($this->container);
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkRequirements() {
|
function checkRequirements() {
|
||||||
@@ -260,7 +261,7 @@ class Initializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setupJSONAPI() {
|
function setupJSONAPI() {
|
||||||
$json_api = API\API::JSON($this->access_control);
|
$json_api = API\API::JSON();
|
||||||
$json_api->init();
|
$json_api->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,6 +4,7 @@ namespace MailPoet\DI;
|
|||||||
|
|
||||||
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\ContainerBuilder;
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Dumper\PhpDumper;
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Dumper\PhpDumper;
|
||||||
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Reference;
|
||||||
|
|
||||||
class ContainerFactory {
|
class ContainerFactory {
|
||||||
|
|
||||||
@@ -46,13 +47,42 @@ class ContainerFactory {
|
|||||||
|
|
||||||
function createContainer() {
|
function createContainer() {
|
||||||
$container = new ContainerBuilder();
|
$container = new ContainerBuilder();
|
||||||
|
// API
|
||||||
|
$container->autowire(\MailPoet\API\MP\v1\API::class)->setPublic(true);
|
||||||
|
$container->register(\MailPoet\API\JSON\API::class)
|
||||||
|
->addArgument(new Reference('service_container'))
|
||||||
|
->setAutowired(true)
|
||||||
|
->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\AutomatedLatestContent::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\CustomFields::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\Forms::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\ImportExport::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\Mailer::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\MP2Migrator::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\Newsletters::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\NewsletterTemplates::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\Segments::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\SendingQueue::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\Services::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\Settings::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\Setup::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\Subscribers::class)->setPublic(true);
|
||||||
|
// Config
|
||||||
$container->autowire(\MailPoet\Config\AccessControl::class)->setPublic(true);
|
$container->autowire(\MailPoet\Config\AccessControl::class)->setPublic(true);
|
||||||
|
// Cron
|
||||||
$container->autowire(\MailPoet\Cron\Daemon::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\Daemon::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true);
|
||||||
|
// Router
|
||||||
$container->autowire(\MailPoet\Router\Endpoints\CronDaemon::class)->setPublic(true);
|
$container->autowire(\MailPoet\Router\Endpoints\CronDaemon::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Router\Endpoints\Subscription::class)->setPublic(true);
|
$container->autowire(\MailPoet\Router\Endpoints\Subscription::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Router\Endpoints\Track::class)->setPublic(true);
|
$container->autowire(\MailPoet\Router\Endpoints\Track::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Router\Endpoints\ViewInBrowser::class)->setPublic(true);
|
$container->autowire(\MailPoet\Router\Endpoints\ViewInBrowser::class)->setPublic(true);
|
||||||
|
// Subscribers
|
||||||
|
$container->autowire(\MailPoet\Subscribers\NewSubscriberNotificationMailer::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Subscribers\ConfirmationEmailMailer::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Subscribers\RequiredCustomFieldValidator::class)->setPublic(true);
|
||||||
|
// Newsletter
|
||||||
|
$container->autowire(\MailPoet\Newsletter\AutomatedLatestContent::class)->setPublic(true);
|
||||||
return $container;
|
return $container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,13 +4,12 @@ namespace MailPoet\Subscription;
|
|||||||
|
|
||||||
use MailPoet\API\API as API;
|
use MailPoet\API\API as API;
|
||||||
use MailPoet\API\JSON\Response as APIResponse;
|
use MailPoet\API\JSON\Response as APIResponse;
|
||||||
use MailPoet\Config\AccessControl;
|
|
||||||
use MailPoet\Util\Url as UrlHelper;
|
use MailPoet\Util\Url as UrlHelper;
|
||||||
|
|
||||||
class Form {
|
class Form {
|
||||||
static function onSubmit($request_data = false) {
|
static function onSubmit($request_data = false) {
|
||||||
$request_data = ($request_data) ? $request_data : $_REQUEST;
|
$request_data = ($request_data) ? $request_data : $_REQUEST;
|
||||||
$api = API::JSON(new AccessControl());
|
$api = API::JSON();
|
||||||
$api->setRequestData($request_data);
|
$api->setRequestData($request_data);
|
||||||
$form_id = (!empty($request_data['data']['form_id'])) ? (int)$request_data['data']['form_id'] : false;
|
$form_id = (!empty($request_data['data']['form_id'])) ? (int)$request_data['data']['form_id'] : false;
|
||||||
$response = $api->processRoute();
|
$response = $api->processRoute();
|
||||||
@@ -32,4 +31,4 @@ class Form {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@ use MailPoet\Config\AccessControl;
|
|||||||
|
|
||||||
class APITest extends \MailPoetTest {
|
class APITest extends \MailPoetTest {
|
||||||
function testItCallsJSONAPI() {
|
function testItCallsJSONAPI() {
|
||||||
expect(API::JSON(new AccessControl()))->isInstanceOf('MailPoet\API\JSON\API');
|
expect(API::JSON())->isInstanceOf('MailPoet\API\JSON\API');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCallsMPAPI() {
|
function testItCallsMPAPI() {
|
||||||
@@ -21,4 +21,4 @@ class APITest extends \MailPoetTest {
|
|||||||
expect($e->getMessage())->equals('Invalid API version.');
|
expect($e->getMessage())->equals('Invalid API version.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,12 +5,15 @@ namespace MailPoet\Test\API\JSON;
|
|||||||
use Codeception\Stub;
|
use Codeception\Stub;
|
||||||
use Codeception\Stub\Expected;
|
use Codeception\Stub\Expected;
|
||||||
use Helper\WordPressHooks as WPHooksHelper;
|
use Helper\WordPressHooks as WPHooksHelper;
|
||||||
use MailPoet\API\API;
|
|
||||||
use MailPoet\API\JSON\API as JSONAPI;
|
use MailPoet\API\JSON\API as JSONAPI;
|
||||||
use MailPoet\API\JSON\Response;
|
use MailPoet\API\JSON\Response;
|
||||||
use MailPoet\API\JSON\Response as APIResponse;
|
use MailPoet\API\JSON\Response as APIResponse;
|
||||||
use MailPoet\API\JSON\SuccessResponse;
|
use MailPoet\API\JSON\SuccessResponse;
|
||||||
|
use MailPoet\API\JSON\v1\APITestNamespacedEndpointStubV1;
|
||||||
|
use MailPoet\API\JSON\v2\APITestNamespacedEndpointStubV2;
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Container;
|
||||||
|
use MailPoet\DI\ContainerFactory;
|
||||||
use MailPoet\WP\Hooks;
|
use MailPoet\WP\Hooks;
|
||||||
|
|
||||||
// required to be able to use wp_delete_user()
|
// required to be able to use wp_delete_user()
|
||||||
@@ -19,6 +22,9 @@ require_once('APITestNamespacedEndpointStubV1.php');
|
|||||||
require_once('APITestNamespacedEndpointStubV2.php');
|
require_once('APITestNamespacedEndpointStubV2.php');
|
||||||
|
|
||||||
class APITest extends \MailPoetTest {
|
class APITest extends \MailPoetTest {
|
||||||
|
/** @var Container */
|
||||||
|
private $container;
|
||||||
|
|
||||||
function _before() {
|
function _before() {
|
||||||
// create WP user
|
// create WP user
|
||||||
$this->wp_user_id = null;
|
$this->wp_user_id = null;
|
||||||
@@ -29,7 +35,12 @@ class APITest extends \MailPoetTest {
|
|||||||
} else {
|
} else {
|
||||||
$this->wp_user_id = $wp_user_id;
|
$this->wp_user_id = $wp_user_id;
|
||||||
}
|
}
|
||||||
$this->api = API::JSON(new AccessControl());
|
$container_factory = new ContainerFactory();
|
||||||
|
$this->container = $container_factory->createContainer();
|
||||||
|
$this->container->autowire(APITestNamespacedEndpointStubV1::class)->setPublic(true);
|
||||||
|
$this->container->autowire(APITestNamespacedEndpointStubV2::class)->setPublic(true);
|
||||||
|
$this->container->compile();
|
||||||
|
$this->api = $this->container->get(\MailPoet\API\JSON\API::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCallsAPISetupAction() {
|
function testItCallsAPISetupAction() {
|
||||||
@@ -143,10 +154,10 @@ class APITest extends \MailPoetTest {
|
|||||||
'api_version' => 'v1',
|
'api_version' => 'v1',
|
||||||
'data' => array('test' => 'data')
|
'data' => array('test' => 'data')
|
||||||
);
|
);
|
||||||
$access_control = new AccessControl();
|
|
||||||
$api = Stub::make(
|
$api = Stub::make(
|
||||||
new \MailPoet\API\JSON\API($access_control),
|
JSONAPI::class,
|
||||||
array(
|
array(
|
||||||
|
'container' => $this->container,
|
||||||
'validatePermissions' => function($method, $permissions) use ($data) {
|
'validatePermissions' => function($method, $permissions) use ($data) {
|
||||||
expect($method)->equals($data['method']);
|
expect($method)->equals($data['method']);
|
||||||
expect($permissions)->equals(
|
expect($permissions)->equals(
|
||||||
@@ -183,7 +194,7 @@ class APITest extends \MailPoetTest {
|
|||||||
new AccessControl(),
|
new AccessControl(),
|
||||||
array('validatePermission' => false)
|
array('validatePermission' => false)
|
||||||
);
|
);
|
||||||
$api = new \MailPoet\API\JSON\API($access_control);
|
$api = new JSONAPI($this->container, $access_control);
|
||||||
$api->addEndpointNamespace($namespace['name'], $namespace['version']);
|
$api->addEndpointNamespace($namespace['name'], $namespace['version']);
|
||||||
$api->setRequestData($data);
|
$api->setRequestData($data);
|
||||||
$response = $api->processRoute();
|
$response = $api->processRoute();
|
||||||
@@ -204,7 +215,7 @@ class APITest extends \MailPoetTest {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$api = new JSONAPI($access_control);
|
$api = new JSONAPI($this->container, $access_control);
|
||||||
expect($api->validatePermissions(null, $permissions))->false();
|
expect($api->validatePermissions(null, $permissions))->false();
|
||||||
|
|
||||||
$access_control = Stub::make(
|
$access_control = Stub::make(
|
||||||
@@ -216,7 +227,7 @@ class APITest extends \MailPoetTest {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$api = new JSONAPI($access_control);
|
$api = new JSONAPI($this->container, $access_control);
|
||||||
expect($api->validatePermissions(null, $permissions))->true();
|
expect($api->validatePermissions(null, $permissions))->true();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +248,7 @@ class APITest extends \MailPoetTest {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$api = new JSONAPI($access_control);
|
$api = new JSONAPI($this->container, $access_control);
|
||||||
expect($api->validatePermissions('test', $permissions))->false();
|
expect($api->validatePermissions('test', $permissions))->false();
|
||||||
|
|
||||||
$access_control = Stub::make(
|
$access_control = Stub::make(
|
||||||
@@ -249,12 +260,11 @@ class APITest extends \MailPoetTest {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$api = new JSONAPI($access_control);
|
$api = new JSONAPI($this->container, $access_control);
|
||||||
expect($api->validatePermissions('test', $permissions))->true();
|
expect($api->validatePermissions('test', $permissions))->true();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItThrowsExceptionWhenInvalidEndpointMethodIsCalled() {
|
function testItThrowsExceptionWhenInvalidEndpointMethodIsCalled() {
|
||||||
$this->api = API::JSON(new AccessControl());
|
|
||||||
$namespace = array(
|
$namespace = array(
|
||||||
'name' => 'MailPoet\API\JSON\v2',
|
'name' => 'MailPoet\API\JSON\v2',
|
||||||
'version' => 'v2'
|
'version' => 'v2'
|
||||||
|
@@ -6,8 +6,8 @@ use MailPoet\API\JSON\v1\AutomatedLatestContent;
|
|||||||
|
|
||||||
class AutomatedLatestContentTest extends \MailPoetTest {
|
class AutomatedLatestContentTest extends \MailPoetTest {
|
||||||
function testItGetsPostTypes() {
|
function testItGetsPostTypes() {
|
||||||
$router = new AutomatedLatestContent();
|
$endpoint = new AutomatedLatestContent(new \MailPoet\Newsletter\AutomatedLatestContent());
|
||||||
$response = $router->getPostTypes();
|
$response = $endpoint->getPostTypes();
|
||||||
expect($response->data)->notEmpty();
|
expect($response->data)->notEmpty();
|
||||||
foreach($response->data as $post_type) {
|
foreach($response->data as $post_type) {
|
||||||
expect($post_type)->count(2);
|
expect($post_type)->count(2);
|
||||||
@@ -17,8 +17,8 @@ class AutomatedLatestContentTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testItDoesNotGetPostTypesExludedFromSearch() {
|
function testItDoesNotGetPostTypesExludedFromSearch() {
|
||||||
$router = new AutomatedLatestContent();
|
$endpoint = new AutomatedLatestContent(new \MailPoet\Newsletter\AutomatedLatestContent());
|
||||||
$response = $router->getPostTypes();
|
$response = $endpoint ->getPostTypes();
|
||||||
// WP's default post type 'revision' is excluded from search
|
// WP's default post type 'revision' is excluded from search
|
||||||
// https://codex.wordpress.org/Post_Types
|
// https://codex.wordpress.org/Post_Types
|
||||||
$revision_post_type = get_post_type_object('revision');
|
$revision_post_type = get_post_type_object('revision');
|
||||||
|
@@ -6,24 +6,34 @@ use AspectMock\Test as Mock;
|
|||||||
use Codeception\Util\Fixtures;
|
use Codeception\Util\Fixtures;
|
||||||
use Codeception\Stub;
|
use Codeception\Stub;
|
||||||
use Codeception\Stub\Expected;
|
use Codeception\Stub\Expected;
|
||||||
use MailPoet\API\API;
|
|
||||||
use MailPoet\Models\CustomField;
|
use MailPoet\Models\CustomField;
|
||||||
use MailPoet\Models\ScheduledTask;
|
use MailPoet\Models\ScheduledTask;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Models\SendingQueue;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Subscribers\ConfirmationEmailMailer;
|
||||||
|
use MailPoet\Subscribers\NewSubscriberNotificationMailer;
|
||||||
|
use MailPoet\Subscribers\RequiredCustomFieldValidator;
|
||||||
use MailPoet\Tasks\Sending;
|
use MailPoet\Tasks\Sending;
|
||||||
|
|
||||||
class APITest extends \MailPoetTest {
|
class APITest extends \MailPoetTest {
|
||||||
const VERSION = 'v1';
|
const VERSION = 'v1';
|
||||||
|
|
||||||
|
private function getApi() {
|
||||||
|
return new \MailPoet\API\MP\v1\API(
|
||||||
|
Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send']),
|
||||||
|
Stub::makeEmpty(ConfirmationEmailMailer::class, ['sendConfirmationEmail']),
|
||||||
|
Stub::makeEmptyExcept(RequiredCustomFieldValidator::class, 'validate')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function testItReturnsSubscriberFields() {
|
function testItReturnsSubscriberFields() {
|
||||||
$custom_field = CustomField::create();
|
$custom_field = CustomField::create();
|
||||||
$custom_field->name = 'test custom field';
|
$custom_field->name = 'test custom field';
|
||||||
$custom_field->type = CustomField::TYPE_TEXT;
|
$custom_field->type = CustomField::TYPE_TEXT;
|
||||||
$custom_field->save();
|
$custom_field->save();
|
||||||
|
|
||||||
$response = API::MP(self::VERSION)->getSubscriberFields();
|
$response = $this->getApi()->getSubscriberFields();
|
||||||
|
|
||||||
expect($response)->equals(
|
expect($response)->equals(
|
||||||
array(
|
array(
|
||||||
@@ -49,7 +59,7 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItDoesNotSubscribeMissingSubscriberToLists() {
|
function testItDoesNotSubscribeMissingSubscriberToLists() {
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->subscribeToLists(false, array(1,2,3));
|
$this->getApi()->subscribeToLists(false, array(1,2,3));
|
||||||
$this->fail('Subscriber does not exist exception should have been thrown.');
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('This subscriber does not exist.');
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
||||||
@@ -62,14 +72,14 @@ class APITest extends \MailPoetTest {
|
|||||||
$subscriber->save();
|
$subscriber->save();
|
||||||
// multiple lists error message
|
// multiple lists error message
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array(1,2,3));
|
$this->getApi()->subscribeToLists($subscriber->id, array(1,2,3));
|
||||||
$this->fail('Missing segments exception should have been thrown.');
|
$this->fail('Missing segments exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('These lists do not exist.');
|
expect($e->getMessage())->equals('These lists do not exist.');
|
||||||
}
|
}
|
||||||
// single list error message
|
// single list error message
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array(1));
|
$this->getApi()->subscribeToLists($subscriber->id, array(1));
|
||||||
$this->fail('Missing segments exception should have been thrown.');
|
$this->fail('Missing segments exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('This list does not exist.');
|
expect($e->getMessage())->equals('This list does not exist.');
|
||||||
@@ -87,7 +97,7 @@ class APITest extends \MailPoetTest {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id));
|
$this->getApi()->subscribeToLists($subscriber->id, array($segment->id));
|
||||||
$this->fail('WP Users segment exception should have been thrown.');
|
$this->fail('WP Users segment exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals("Can't subscribe to a WordPress Users list with ID {$segment->id}.");
|
expect($e->getMessage())->equals("Can't subscribe to a WordPress Users list with ID {$segment->id}.");
|
||||||
@@ -106,14 +116,14 @@ class APITest extends \MailPoetTest {
|
|||||||
);
|
);
|
||||||
// multiple lists error message
|
// multiple lists error message
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id, 90, 100));
|
$this->getApi()->subscribeToLists($subscriber->id, array($segment->id, 90, 100));
|
||||||
$this->fail('Missing segments with IDs exception should have been thrown.');
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('Lists with IDs 90, 100 do not exist.');
|
expect($e->getMessage())->equals('Lists with IDs 90, 100 do not exist.');
|
||||||
}
|
}
|
||||||
// single list error message
|
// single list error message
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id, 90));
|
$this->getApi()->subscribeToLists($subscriber->id, array($segment->id, 90));
|
||||||
$this->fail('Missing segments with IDs exception should have been thrown.');
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('List with ID 90 does not exist.');
|
expect($e->getMessage())->equals('List with ID 90 does not exist.');
|
||||||
@@ -123,7 +133,7 @@ class APITest extends \MailPoetTest {
|
|||||||
function testItUsesMultipleListsSubscribeMethodWhenSubscribingToSingleList() {
|
function testItUsesMultipleListsSubscribeMethodWhenSubscribingToSingleList() {
|
||||||
// subscribing to single list = converting list ID to an array and using
|
// subscribing to single list = converting list ID to an array and using
|
||||||
// multiple lists subscription method
|
// multiple lists subscription method
|
||||||
$API = Stub::make(new \MailPoet\API\MP\v1\API(), array(
|
$API = Stub::make($this->getApi(), array(
|
||||||
'subscribeToLists' => function() {
|
'subscribeToLists' => function() {
|
||||||
return func_get_args();
|
return func_get_args();
|
||||||
}
|
}
|
||||||
@@ -152,13 +162,13 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
// test if segments are specified
|
// test if segments are specified
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array());
|
$this->getApi()->subscribeToLists($subscriber->id, array());
|
||||||
$this->fail('Segments are required exception should have been thrown.');
|
$this->fail('Segments are required exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('At least one segment ID is required.');
|
expect($e->getMessage())->equals('At least one segment ID is required.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id));
|
$result = $this->getApi()->subscribeToLists($subscriber->id, array($segment->id));
|
||||||
expect($result['id'])->equals($subscriber->id);
|
expect($result['id'])->equals($subscriber->id);
|
||||||
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
||||||
}
|
}
|
||||||
@@ -173,7 +183,7 @@ class APITest extends \MailPoetTest {
|
|||||||
'type' => Segment::TYPE_DEFAULT
|
'type' => Segment::TYPE_DEFAULT
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$result = API::MP(self::VERSION)->subscribeToList($subscriber->email, $segment->id);
|
$result = $this->getApi()->subscribeToList($subscriber->email, $segment->id);
|
||||||
expect($result['id'])->equals($subscriber->id);
|
expect($result['id'])->equals($subscriber->id);
|
||||||
expect($result['subscriptions'])->notEmpty();
|
expect($result['subscriptions'])->notEmpty();
|
||||||
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
||||||
@@ -181,7 +191,7 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItSchedulesWelcomeNotificationByDefaultAfterSubscriberSubscriberToLists() {
|
function testItSchedulesWelcomeNotificationByDefaultAfterSubscriberSubscriberToLists() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
\MailPoet\API\MP\v1\API::class,
|
||||||
'subscribeToLists',
|
'subscribeToLists',
|
||||||
array(
|
array(
|
||||||
'_scheduleWelcomeNotification' => Expected::once()
|
'_scheduleWelcomeNotification' => Expected::once()
|
||||||
@@ -201,7 +211,7 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItDoesNotScheduleWelcomeNotificationAfterSubscribingSubscriberToListsIfStatusIsNotSubscribed() {
|
function testItDoesNotScheduleWelcomeNotificationAfterSubscribingSubscriberToListsIfStatusIsNotSubscribed() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
\MailPoet\API\MP\v1\API::class,
|
||||||
'subscribeToLists',
|
'subscribeToLists',
|
||||||
array(
|
array(
|
||||||
'_scheduleWelcomeNotification' => Expected::never()
|
'_scheduleWelcomeNotification' => Expected::never()
|
||||||
@@ -220,7 +230,7 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItDoesNotScheduleWelcomeNotificationAfterSubscribingSubscriberToListsWhenDisabledByOption() {
|
function testItDoesNotScheduleWelcomeNotificationAfterSubscribingSubscriberToListsWhenDisabledByOption() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
\MailPoet\API\MP\v1\API::class,
|
||||||
'subscribeToLists',
|
'subscribeToLists',
|
||||||
array(
|
array(
|
||||||
'_scheduleWelcomeNotification' => Expected::never()
|
'_scheduleWelcomeNotification' => Expected::never()
|
||||||
@@ -246,7 +256,7 @@ class APITest extends \MailPoetTest {
|
|||||||
'type' => Segment::TYPE_DEFAULT
|
'type' => Segment::TYPE_DEFAULT
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$result = API::MP(self::VERSION)->getLists();
|
$result = $this->getApi()->getLists();
|
||||||
expect($result)->count(1);
|
expect($result)->count(1);
|
||||||
expect($result[0]['id'])->equals($segment->id);
|
expect($result[0]['id'])->equals($segment->id);
|
||||||
}
|
}
|
||||||
@@ -264,14 +274,14 @@ class APITest extends \MailPoetTest {
|
|||||||
'type' => Segment::TYPE_WP_USERS
|
'type' => Segment::TYPE_WP_USERS
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$result = API::MP(self::VERSION)->getLists();
|
$result = $this->getApi()->getLists();
|
||||||
expect($result)->count(1);
|
expect($result)->count(1);
|
||||||
expect($result[0]['id'])->equals($default_segment->id);
|
expect($result[0]['id'])->equals($default_segment->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItRequiresEmailAddressToAddSubscriber() {
|
function testItRequiresEmailAddressToAddSubscriber() {
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->addSubscriber(array());
|
$this->getApi()->addSubscriber(array());
|
||||||
$this->fail('Subscriber email address required exception should have been thrown.');
|
$this->fail('Subscriber email address required exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('Subscriber email address is required.');
|
expect($e->getMessage())->equals('Subscriber email address is required.');
|
||||||
@@ -283,7 +293,7 @@ class APITest extends \MailPoetTest {
|
|||||||
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
||||||
$subscriber->save();
|
$subscriber->save();
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->addSubscriber(array('email' => $subscriber->email));
|
$this->getApi()->addSubscriber(array('email' => $subscriber->email));
|
||||||
$this->fail('Subscriber exists exception should have been thrown.');
|
$this->fail('Subscriber exists exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('This subscriber already exists.');
|
expect($e->getMessage())->equals('This subscriber already exists.');
|
||||||
@@ -295,7 +305,7 @@ class APITest extends \MailPoetTest {
|
|||||||
'email' => 'test' // invalid email
|
'email' => 'test' // invalid email
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->addSubscriber($subscriber);
|
$this->getApi()->addSubscriber($subscriber);
|
||||||
$this->fail('Failed to add subscriber exception should have been thrown.');
|
$this->fail('Failed to add subscriber exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->contains('Failed to add subscriber:');
|
expect($e->getMessage())->contains('Failed to add subscriber:');
|
||||||
@@ -315,7 +325,7 @@ class APITest extends \MailPoetTest {
|
|||||||
'cf_' . $custom_field->id => 'test'
|
'cf_' . $custom_field->id => 'test'
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = API::MP(self::VERSION)->addSubscriber($subscriber);
|
$result = $this->getApi()->addSubscriber($subscriber);
|
||||||
expect($result['id'])->greaterThan(0);
|
expect($result['id'])->greaterThan(0);
|
||||||
expect($result['email'])->equals($subscriber['email']);
|
expect($result['email'])->equals($subscriber['email']);
|
||||||
expect($result['cf_' . $custom_field->id])->equals('test');
|
expect($result['cf_' . $custom_field->id])->equals('test');
|
||||||
@@ -334,16 +344,10 @@ class APITest extends \MailPoetTest {
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->setExpectedException('Exception');
|
$this->setExpectedException('Exception');
|
||||||
API::MP(self::VERSION)->addSubscriber($subscriber);
|
$this->getApi()->addSubscriber($subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItSubscribesToSegmentsWhenAddingSubscriber() {
|
function testItSubscribesToSegmentsWhenAddingSubscriber() {
|
||||||
$API = Stub::makeEmptyExcept(
|
|
||||||
new \MailPoet\API\MP\v1\API(),
|
|
||||||
'addSubscriber',
|
|
||||||
array(
|
|
||||||
'_sendConfirmationEmail' => Expected::once()
|
|
||||||
), $this);
|
|
||||||
$segment = Segment::createOrUpdate(
|
$segment = Segment::createOrUpdate(
|
||||||
array(
|
array(
|
||||||
'name' => 'Default',
|
'name' => 'Default',
|
||||||
@@ -354,7 +358,7 @@ class APITest extends \MailPoetTest {
|
|||||||
'email' => 'test@example.com'
|
'email' => 'test@example.com'
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = $API->addSubscriber($subscriber, array($segment->id));
|
$result = $this->getApi()->addSubscriber($subscriber, array($segment->id));
|
||||||
expect($result['id'])->greaterThan(0);
|
expect($result['id'])->greaterThan(0);
|
||||||
expect($result['email'])->equals($subscriber['email']);
|
expect($result['email'])->equals($subscriber['email']);
|
||||||
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
||||||
@@ -362,16 +366,18 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItSchedulesWelcomeNotificationByDefaultAfterAddingSubscriber() {
|
function testItSchedulesWelcomeNotificationByDefaultAfterAddingSubscriber() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
\MailPoet\API\MP\v1\API::class,
|
||||||
'addSubscriber',
|
'addSubscriber',
|
||||||
array(
|
array(
|
||||||
'_scheduleWelcomeNotification' => Expected::once()
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send']),
|
||||||
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate']),
|
||||||
|
'_scheduleWelcomeNotification' => Expected::once(),
|
||||||
), $this);
|
), $this);
|
||||||
$subscriber = array(
|
$subscriber = array(
|
||||||
'email' => 'test@example.com',
|
'email' => 'test@example.com',
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
);
|
);
|
||||||
$segments = array(1);
|
$segments = [1];
|
||||||
$API->addSubscriber($subscriber, $segments);
|
$API->addSubscriber($subscriber, $segments);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -389,7 +395,7 @@ class APITest extends \MailPoetTest {
|
|||||||
'type' => Segment::TYPE_DEFAULT
|
'type' => Segment::TYPE_DEFAULT
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$API = new \MailPoet\API\MP\v1\API();
|
$API = $this->getApi();
|
||||||
$subscriber = array(
|
$subscriber = array(
|
||||||
'email' => 'test@example.com',
|
'email' => 'test@example.com',
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
@@ -401,10 +407,12 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberIfStatusIsNotSubscribed() {
|
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberIfStatusIsNotSubscribed() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
\MailPoet\API\MP\v1\API::class,
|
||||||
'addSubscriber',
|
'addSubscriber',
|
||||||
array(
|
array(
|
||||||
'_scheduleWelcomeNotification' => Expected::never()
|
'_scheduleWelcomeNotification' => Expected::never(),
|
||||||
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send']),
|
||||||
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate']),
|
||||||
), $this);
|
), $this);
|
||||||
$subscriber = array(
|
$subscriber = array(
|
||||||
'email' => 'test@example.com'
|
'email' => 'test@example.com'
|
||||||
@@ -415,10 +423,12 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberWhenDisabledByOption() {
|
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberWhenDisabledByOption() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
\MailPoet\API\MP\v1\API::class,
|
||||||
'addSubscriber',
|
'addSubscriber',
|
||||||
array(
|
array(
|
||||||
'_scheduleWelcomeNotification' => Expected::never()
|
'_scheduleWelcomeNotification' => Expected::never(),
|
||||||
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send']),
|
||||||
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate'])
|
||||||
), $this);
|
), $this);
|
||||||
$subscriber = array(
|
$subscriber = array(
|
||||||
'email' => 'test@example.com',
|
'email' => 'test@example.com',
|
||||||
@@ -431,10 +441,12 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testByDefaultItSendsConfirmationEmailAfterAddingSubscriber() {
|
function testByDefaultItSendsConfirmationEmailAfterAddingSubscriber() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
\MailPoet\API\MP\v1\API::class,
|
||||||
'addSubscriber',
|
'addSubscriber',
|
||||||
array(
|
array(
|
||||||
'_sendConfirmationEmail' => Expected::once()
|
'_sendConfirmationEmail' => Expected::once(),
|
||||||
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate']),
|
||||||
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send'])
|
||||||
), $this);
|
), $this);
|
||||||
$subscriber = array(
|
$subscriber = array(
|
||||||
'email' => 'test@example.com'
|
'email' => 'test@example.com'
|
||||||
@@ -444,14 +456,17 @@ class APITest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testItThrowsWhenConfirmationEmailFailsToSend() {
|
function testItThrowsWhenConfirmationEmailFailsToSend() {
|
||||||
$API = new \MailPoet\API\MP\v1\API();
|
$confirmation_mailer = $this->createMock(ConfirmationEmailMailer::class);
|
||||||
Mock::double($API, array(
|
$confirmation_mailer->expects($this->once())
|
||||||
'_sendConfirmationEmail' => function ($subscriber) {
|
->method('sendConfirmationEmail')
|
||||||
$subscriber->setError('Big Error');
|
->willReturnCallback(function (Subscriber $subscriber) {
|
||||||
return false;
|
$subscriber->setError('Big Error');
|
||||||
}
|
return false;
|
||||||
)
|
});
|
||||||
);
|
|
||||||
|
$API = Stub::copy($this->getApi(), [
|
||||||
|
'confirmation_email_mailer' => $confirmation_mailer,
|
||||||
|
]);
|
||||||
$segment = Segment::createOrUpdate(
|
$segment = Segment::createOrUpdate(
|
||||||
array(
|
array(
|
||||||
'name' => 'Default',
|
'name' => 'Default',
|
||||||
@@ -467,10 +482,12 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItDoesNotSendConfirmationEmailAfterAddingSubscriberWhenOptionIsSet() {
|
function testItDoesNotSendConfirmationEmailAfterAddingSubscriberWhenOptionIsSet() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
\MailPoet\API\MP\v1\API::class,
|
||||||
'addSubscriber',
|
'addSubscriber',
|
||||||
array(
|
array(
|
||||||
'_sendConfirmationEmail' => Expected::never()
|
'__sendConfirmationEmail' => Expected::never(),
|
||||||
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate']),
|
||||||
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send'])
|
||||||
), $this);
|
), $this);
|
||||||
$subscriber = array(
|
$subscriber = array(
|
||||||
'email' => 'test@example.com'
|
'email' => 'test@example.com'
|
||||||
@@ -482,7 +499,7 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
function testItRequiresNameToAddList() {
|
function testItRequiresNameToAddList() {
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->addList(array());
|
$this->getApi()->addList(array());
|
||||||
$this->fail('List name required exception should have been thrown.');
|
$this->fail('List name required exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('List name is required.');
|
expect($e->getMessage())->equals('List name is required.');
|
||||||
@@ -494,7 +511,7 @@ class APITest extends \MailPoetTest {
|
|||||||
$segment->name = 'Test segment';
|
$segment->name = 'Test segment';
|
||||||
$segment->save();
|
$segment->save();
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->addList(array('name' => $segment->name));
|
$this->getApi()->addList(array('name' => $segment->name));
|
||||||
$this->fail('List exists exception should have been thrown.');
|
$this->fail('List exists exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('This list already exists.');
|
expect($e->getMessage())->equals('This list already exists.');
|
||||||
@@ -506,14 +523,14 @@ class APITest extends \MailPoetTest {
|
|||||||
'name' => 'Test segment'
|
'name' => 'Test segment'
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = API::MP(self::VERSION)->addList($segment);
|
$result = $this->getApi()->addList($segment);
|
||||||
expect($result['id'])->greaterThan(0);
|
expect($result['id'])->greaterThan(0);
|
||||||
expect($result['name'])->equals($segment['name']);
|
expect($result['name'])->equals($segment['name']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItDoesNotUnsubscribeMissingSubscriberFromLists() {
|
function testItDoesNotUnsubscribeMissingSubscriberFromLists() {
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->unsubscribeFromLists(false, array(1,2,3));
|
$this->getApi()->unsubscribeFromLists(false, array(1,2,3));
|
||||||
$this->fail('Subscriber does not exist exception should have been thrown.');
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('This subscriber does not exist.');
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
||||||
@@ -526,14 +543,14 @@ class APITest extends \MailPoetTest {
|
|||||||
$subscriber->save();
|
$subscriber->save();
|
||||||
// multiple lists error message
|
// multiple lists error message
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array(1,2,3));
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array(1,2,3));
|
||||||
$this->fail('Missing segments exception should have been thrown.');
|
$this->fail('Missing segments exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('These lists do not exist.');
|
expect($e->getMessage())->equals('These lists do not exist.');
|
||||||
}
|
}
|
||||||
// single list error message
|
// single list error message
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array(1));
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array(1));
|
||||||
$this->fail('Missing segments exception should have been thrown.');
|
$this->fail('Missing segments exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('This list does not exist.');
|
expect($e->getMessage())->equals('This list does not exist.');
|
||||||
@@ -552,14 +569,14 @@ class APITest extends \MailPoetTest {
|
|||||||
);
|
);
|
||||||
// multiple lists error message
|
// multiple lists error message
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id, 90, 100));
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array($segment->id, 90, 100));
|
||||||
$this->fail('Missing segments with IDs exception should have been thrown.');
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('Lists with IDs 90, 100 do not exist.');
|
expect($e->getMessage())->equals('Lists with IDs 90, 100 do not exist.');
|
||||||
}
|
}
|
||||||
// single list error message
|
// single list error message
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id, 90));
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array($segment->id, 90));
|
||||||
$this->fail('Missing segments with IDs exception should have been thrown.');
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('List with ID 90 does not exist.');
|
expect($e->getMessage())->equals('List with ID 90 does not exist.');
|
||||||
@@ -577,7 +594,7 @@ class APITest extends \MailPoetTest {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id));
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array($segment->id));
|
||||||
$this->fail('WP Users segment exception should have been thrown.');
|
$this->fail('WP Users segment exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals("Can't subscribe to a WordPress Users list with ID {$segment->id}.");
|
expect($e->getMessage())->equals("Can't subscribe to a WordPress Users list with ID {$segment->id}.");
|
||||||
@@ -587,7 +604,7 @@ class APITest extends \MailPoetTest {
|
|||||||
function testItUsesMultipleListsUnsubscribeMethodWhenUnsubscribingFromSingleList() {
|
function testItUsesMultipleListsUnsubscribeMethodWhenUnsubscribingFromSingleList() {
|
||||||
// unsubscribing from single list = converting list ID to an array and using
|
// unsubscribing from single list = converting list ID to an array and using
|
||||||
// multiple lists unsubscribe method
|
// multiple lists unsubscribe method
|
||||||
$API = Stub::make(new \MailPoet\API\MP\v1\API(), array(
|
$API = Stub::make(\MailPoet\API\MP\v1\API::class, array(
|
||||||
'unsubscribeFromLists' => function() {
|
'unsubscribeFromLists' => function() {
|
||||||
return func_get_args();
|
return func_get_args();
|
||||||
}
|
}
|
||||||
@@ -615,15 +632,15 @@ class APITest extends \MailPoetTest {
|
|||||||
|
|
||||||
// test if segments are specified
|
// test if segments are specified
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array());
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array());
|
||||||
$this->fail('Segments are required exception should have been thrown.');
|
$this->fail('Segments are required exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('At least one segment ID is required.');
|
expect($e->getMessage())->equals('At least one segment ID is required.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id));
|
$result = $this->getApi()->subscribeToLists($subscriber->id, array($segment->id));
|
||||||
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_SUBSCRIBED);
|
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_SUBSCRIBED);
|
||||||
$result = API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id));
|
$result = $this->getApi()->unsubscribeFromLists($subscriber->id, array($segment->id));
|
||||||
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_UNSUBSCRIBED);
|
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_UNSUBSCRIBED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -637,16 +654,16 @@ class APITest extends \MailPoetTest {
|
|||||||
'type' => Segment::TYPE_DEFAULT
|
'type' => Segment::TYPE_DEFAULT
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
API::MP(self::VERSION)->subscribeToList($subscriber->id, $segment->id);
|
$this->getApi()->subscribeToList($subscriber->id, $segment->id);
|
||||||
|
|
||||||
// successful response
|
// successful response
|
||||||
$result = API::MP(self::VERSION)->getSubscriber($subscriber->email);
|
$result = $this->getApi()->getSubscriber($subscriber->email);
|
||||||
expect($result['email'])->equals($subscriber->email);
|
expect($result['email'])->equals($subscriber->email);
|
||||||
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
||||||
|
|
||||||
// error response
|
// error response
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->getSubscriber('some_fake_email');
|
$this->getApi()->getSubscriber('some_fake_email');
|
||||||
$this->fail('Subscriber does not exist exception should have been thrown.');
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
expect($e->getMessage())->equals('This subscriber does not exist.');
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
||||||
|
Reference in New Issue
Block a user