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