Refactor MailPoet\API to use new settings

[MAILPOET-1757]
This commit is contained in:
Rostislav Wolny
2019-01-30 13:53:44 +01:00
parent 3aa0926fb9
commit 3b5962d36a
12 changed files with 130 additions and 60 deletions

View File

@@ -13,6 +13,7 @@ use MailPoet\API\JSON\v1\APITestNamespacedEndpointStubV1;
use MailPoet\API\JSON\v2\APITestNamespacedEndpointStubV2;
use MailPoet\Config\AccessControl;
use MailPoet\DI\ContainerConfigurator;
use MailPoet\Settings\SettingsController;
use MailPoetVendor\Symfony\Component\DependencyInjection\Container;
use MailPoet\DI\ContainerFactory;
use MailPoet\WP\Functions as WPFunctions;
@@ -26,6 +27,9 @@ class APITest extends \MailPoetTest {
/** @var Container */
private $container;
/** @var SettingsController */
private $settings;
function _before() {
parent::_before();
// create WP user
@@ -42,7 +46,13 @@ class APITest extends \MailPoetTest {
$this->container->autowire(APITestNamespacedEndpointStubV1::class)->setPublic(true);
$this->container->autowire(APITestNamespacedEndpointStubV2::class)->setPublic(true);
$this->container->compile();
$this->api = new \MailPoet\API\JSON\API($this->container, $this->container->get(AccessControl::class), new WPFunctions);
$this->settings = $this->container->get(SettingsController::class);
$this->api = new \MailPoet\API\JSON\API(
$this->container,
$this->container->get(AccessControl::class),
$this->settings,
new WPFunctions
);
}
function testItCallsAPISetupAction() {
@@ -59,7 +69,8 @@ class APITest extends \MailPoetTest {
'setupAjax',
array(
'wp' => new WPFunctions,
'processRoute' => Stub::makeEmpty(new SuccessResponse)
'processRoute' => Stub::makeEmpty(new SuccessResponse),
'settings' => $this->container->get(SettingsController::class)
)
);
$api->setupAjax();
@@ -197,7 +208,8 @@ class APITest extends \MailPoetTest {
new AccessControl(),
array('validatePermission' => false)
);
$api = new JSONAPI($this->container, $access_control, new WPFunctions);
$api = new JSONAPI($this->container, $access_control, $this->settings, new WPFunctions);
$api->addEndpointNamespace($namespace['name'], $namespace['version']);
$api->setRequestData($data);
$response = $api->processRoute();
@@ -218,7 +230,8 @@ class APITest extends \MailPoetTest {
})
)
);
$api = new JSONAPI($this->container, $access_control, new WPFunctions);
$api = new JSONAPI($this->container, $access_control, $this->settings, new WPFunctions);
expect($api->validatePermissions(null, $permissions))->false();
$access_control = Stub::make(
@@ -230,7 +243,7 @@ class APITest extends \MailPoetTest {
})
)
);
$api = new JSONAPI($this->container, $access_control, new WPFunctions);
$api = new JSONAPI($this->container, $access_control, $this->settings, new WPFunctions);
expect($api->validatePermissions(null, $permissions))->true();
}
@@ -251,7 +264,8 @@ class APITest extends \MailPoetTest {
})
)
);
$api = new JSONAPI($this->container, $access_control, new WPFunctions);
$api = new JSONAPI($this->container, $access_control, $this->settings, new WPFunctions);
expect($api->validatePermissions('test', $permissions))->false();
$access_control = Stub::make(
@@ -263,7 +277,8 @@ class APITest extends \MailPoetTest {
})
)
);
$api = new JSONAPI($this->container, $access_control, new WPFunctions);
$api = new JSONAPI($this->container, $access_control, $this->settings, new WPFunctions);
expect($api->validatePermissions('test', $permissions))->true();
}

View File

@@ -22,6 +22,7 @@ use MailPoet\Models\SendingQueue;
use MailPoet\Newsletter\Scheduler\Scheduler;
use MailPoet\Newsletter\Url;
use MailPoet\Router\Router;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscription\Url as SubscriptionUrl;
use MailPoet\Tasks\Sending as SendingTask;
use MailPoet\WP\Functions as WPFunctions;
@@ -108,7 +109,8 @@ class NewslettersTest extends \MailPoetTest {
$this->endpoint = new Newsletters(
ContainerWrapper::getInstance()->get(BulkActionController::class),
ContainerWrapper::getInstance()->get(Handler::class),
$wp
$wp,
new SettingsController()
);
$response = $this->endpoint->get(array('id' => $this->newsletter->id));
expect($response->status)->equals(APIResponse::STATUS_OK);
@@ -145,7 +147,8 @@ class NewslettersTest extends \MailPoetTest {
$this->endpoint = new Newsletters(
ContainerWrapper::getInstance()->get(BulkActionController::class),
ContainerWrapper::getInstance()->get(Handler::class),
$wp
$wp,
new SettingsController()
);
$response = $this->endpoint->save($valid_data);
@@ -509,7 +512,8 @@ class NewslettersTest extends \MailPoetTest {
$this->endpoint = new Newsletters(
ContainerWrapper::getInstance()->get(BulkActionController::class),
ContainerWrapper::getInstance()->get(Handler::class),
$wp
$wp,
new SettingsController()
);
$response = $this->endpoint->duplicate(array('id' => $this->newsletter->id));

View File

@@ -10,6 +10,7 @@ use MailPoet\Models\NewsletterOptionField;
use MailPoet\Models\ScheduledTask;
use MailPoet\Models\SendingQueue as SendingQueueModel;
use MailPoet\Models\Setting;
use MailPoet\Settings\SettingsController;
use MailPoet\Tasks\Sending;
class SendingQueueTest extends \MailPoetTest {
@@ -22,7 +23,8 @@ class SendingQueueTest extends \MailPoetTest {
'type' => Newsletter::TYPE_STANDARD
)
);
Setting::setValue('sender', array(
$settings = new SettingsController();
$settings->set('sender', array(
'name' => 'John Doe',
'address' => 'john.doe@example.com'
));
@@ -120,4 +122,4 @@ class SendingQueueTest extends \MailPoetTest {
\ORM::raw_execute('TRUNCATE ' . SendingQueueModel::$_table);
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
}
}
}

View File

@@ -6,14 +6,18 @@ use Codeception\Stub\Expected;
use MailPoet\API\JSON\v1\Services;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Config\Installer;
use MailPoet\Models\Setting;
use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController;
class ServicesTest extends \MailPoetTest {
/** @var SettingsController */
private $settings;
function _before() {
parent::_before();
$this->services_endpoint = new Services();
$this->data = array('key' => '1234567890abcdef');
$this->settings = new SettingsController();
}
function testItRespondsWithErrorIfNoMSSKeyIsGiven() {
@@ -268,8 +272,8 @@ class ServicesTest extends \MailPoetTest {
function testItRespondsWithPublicIdForMSS() {
$fake_public_id = 'a-fake-public_id';
Setting::deleteValue('public_id');
Setting::deleteValue('new_public_id');
$this->settings->delete('public_id');
$this->settings->delete('new_public_id');
$this->services_endpoint->bridge = Stub::make(
new Bridge(),
@@ -284,13 +288,13 @@ class ServicesTest extends \MailPoetTest {
);
$response = $this->services_endpoint->checkMSSKey($this->data);
expect(Setting::getValue('public_id'))->equals($fake_public_id);
expect(Setting::getValue('new_public_id'))->equals('true');
expect($this->settings->get('public_id'))->equals($fake_public_id);
expect($this->settings->get('new_public_id'))->equals('true');
}
function testItRespondsWithoutPublicIdForMSS() {
Setting::deleteValue('public_id');
Setting::deleteValue('new_public_id');
$this->settings->delete('public_id');
$this->settings->delete('new_public_id');
$this->services_endpoint->bridge = Stub::make(
new Bridge(),
@@ -302,14 +306,14 @@ class ServicesTest extends \MailPoetTest {
);
$response = $this->services_endpoint->checkMSSKey($this->data);
expect(Setting::getValue('public_id', null))->null();
expect(Setting::getValue('new_public_id', null))->null();
expect($this->settings->get('public_id', null))->null();
expect($this->settings->get('new_public_id', null))->null();
}
function testItRespondsWithPublicIdForPremium() {
$fake_public_id = 'another-fake-public_id';
Setting::deleteValue('public_id');
Setting::deleteValue('new_public_id');
$this->settings->delete('public_id');
$this->settings->delete('new_public_id');
$this->services_endpoint->bridge = Stub::make(
new Bridge(),
@@ -324,13 +328,13 @@ class ServicesTest extends \MailPoetTest {
);
$response = $this->services_endpoint->checkPremiumKey($this->data);
expect(Setting::getValue('public_id'))->equals($fake_public_id);
expect(Setting::getValue('new_public_id'))->equals('true');
expect($this->settings->get('public_id'))->equals($fake_public_id);
expect($this->settings->get('new_public_id'))->equals('true');
}
function testItRespondsWithoutPublicIdForPremium() {
Setting::deleteValue('public_id');
Setting::deleteValue('new_public_id');
$this->settings->delete('public_id');
$this->settings->delete('new_public_id');
$this->services_endpoint->bridge = Stub::make(
new Bridge(),
@@ -342,7 +346,7 @@ class ServicesTest extends \MailPoetTest {
);
$response = $this->services_endpoint->checkPremiumKey($this->data);
expect(Setting::getValue('public_id', null))->null();
expect(Setting::getValue('new_public_id', null))->null();
expect($this->settings->get('public_id', null))->null();
expect($this->settings->get('new_public_id', null))->null();
}
}

View File

@@ -5,24 +5,30 @@ use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\API\JSON\v1\Settings;
use MailPoet\Models\Setting;
use MailPoet\Settings\SettingsController;
class SettingsTest extends \MailPoetTest {
/** @var Settings */
private $endpoint;
function _before() {
parent::_before();
Setting::setValue('some.setting.key', true);
$settings = new SettingsController();
$settings->set('some.setting.key', true);
$this->endpoint = new Settings($settings);
}
function testItCanGetSettings() {
$router = new Settings();
$response = $router->get();
$response = $this->endpoint->get();
expect($response->status)->equals(APIResponse::STATUS_OK);
expect($response->data)->notEmpty();
expect($response->data['some']['setting']['key'])->true();
Setting::deleteMany();
$response = $router->get();
SettingsController::resetCache();
$response = $this->endpoint->get();
expect($response->status)->equals(APIResponse::STATUS_OK);
expect($response->data)->equals(Setting::getDefaults());
}
@@ -37,16 +43,14 @@ class SettingsTest extends \MailPoetTest {
)
);
$router = new Settings();
$response = $router->set(/* missing data */);
$response = $this->endpoint->set(/* missing data */);
expect($response->errors[0]['error'])->equals(APIError::BAD_REQUEST);
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
$response = $router->set($new_settings);
$response = $this->endpoint->set($new_settings);
expect($response->status)->equals(APIResponse::STATUS_OK);
$response = $router->get();
$response = $this->endpoint->get();
expect($response->status)->equals(APIResponse::STATUS_OK);
expect($response->data['some']['setting'])->hasntKey('key');
expect($response->data['some']['setting']['new_key'])->true();
@@ -56,4 +60,4 @@ class SettingsTest extends \MailPoetTest {
function _after() {
\ORM::forTable(Setting::$_table)->deleteMany();
}
}
}

View File

@@ -7,11 +7,13 @@ use MailPoet\API\JSON\v1\Setup;
use MailPoet\WP\Functions as WPFunctions;
use Helper\WordPressHooks as WPHooksHelper;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Settings\SettingsController;
class SetupTest extends \MailPoetTest {
function _before() {
parent::_before();
Setting::setValue('signup_confirmation.enabled', false);
$settings = new SettingsController();
$settings->set('signup_confirmation.enabled', false);
}
function testItCanReinstall() {
@@ -23,7 +25,8 @@ class SetupTest extends \MailPoetTest {
$response = $router->reset();
expect($response->status)->equals(APIResponse::STATUS_OK);
$signup_confirmation = Setting::getValue('signup_confirmation.enabled');
$settings = new SettingsController();
$signup_confirmation = $settings->fetch('signup_confirmation.enabled');
expect($signup_confirmation)->true();
$hook_name = 'mailpoet_setup_reset';

View File

@@ -18,6 +18,7 @@ use MailPoet\Models\SubscriberIP;
use MailPoet\Models\Segment;
use MailPoet\Models\Setting;
use MailPoet\Models\SubscriberSegment;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscribers\Source;
class SubscribersTest extends \MailPoetTest {
@@ -25,6 +26,9 @@ class SubscribersTest extends \MailPoetTest {
/** @var Subscribers */
private $endpoint;
/** @var SettingsController */
private $settings;
function _before() {
parent::_before();
$this->cleanup();
@@ -66,8 +70,9 @@ class SubscribersTest extends \MailPoetTest {
)
));
$this->settings = new SettingsController();
// setup mailer
Setting::setValue('sender', array(
$this->settings->set('sender', array(
'address' => 'sender@mailpoet.com',
'name' => 'Sender'
));
@@ -451,7 +456,7 @@ class SubscribersTest extends \MailPoetTest {
}
function testItCannotSubscribeWithoutCaptchaWhenEnabled() {
Setting::setValue('re_captcha', array('enabled' => true));
$this->settings->set('re_captcha', array('enabled' => true));
$response = $this->endpoint->subscribe(array(
$this->obfuscatedEmail => 'toto@mailpoet.com',
'form_id' => $this->form->id,
@@ -459,7 +464,7 @@ class SubscribersTest extends \MailPoetTest {
));
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
expect($response->errors[0]['message'])->equals('Please check the CAPTCHA.');
Setting::setValue('re_captcha', array());
$this->settings->set('re_captcha', array());
}
function testItCannotSubscribeWithoutMandatoryCustomField() {
@@ -639,5 +644,6 @@ class SubscribersTest extends \MailPoetTest {
\ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
\ORM::raw_execute('TRUNCATE ' . SubscriberIP::$_table);
\ORM::raw_execute('TRUNCATE ' . CustomField::$_table);
\ORM::raw_execute('TRUNCATE ' . Setting::$_table);
}
}