diff --git a/mailpoet/lib/API/JSON/v1/Settings.php b/mailpoet/lib/API/JSON/v1/Settings.php index 99c06d6ea1..c2cd83a613 100644 --- a/mailpoet/lib/API/JSON/v1/Settings.php +++ b/mailpoet/lib/API/JSON/v1/Settings.php @@ -4,6 +4,7 @@ namespace MailPoet\API\JSON\v1; use MailPoet\API\JSON\Endpoint as APIEndpoint; use MailPoet\API\JSON\Error as APIError; +use MailPoet\API\JSON\Response; use MailPoet\Config\AccessControl; use MailPoet\Config\ServicesChecker; use MailPoet\Cron\Workers\SubscribersEngagementScore; @@ -169,6 +170,32 @@ class Settings extends APIEndpoint { } } + public function delete(string $settingName): Response { + if (empty($settingName)) { + return $this->badRequest( + [ + APIError::BAD_REQUEST => + __('You have not specified any setting to be deleted.', 'mailpoet'), + ] + ); + } + + $setting = $this->settings->get($settingName); + + if (is_null($setting)) { + return $this->badRequest( + [ + APIError::BAD_REQUEST => + __('Setting doesn\'t exist.', 'mailpoet'), + ] + ); + } + + $this->settings->delete($settingName); + + return $this->successResponse(); + } + public function recalculateSubscribersScore() { $this->statisticsOpensRepository->resetSubscribersScoreCalculation(); $this->statisticsOpensRepository->resetSegmentsScoreCalculation(); diff --git a/mailpoet/tests/integration/API/JSON/v1/SettingsTest.php b/mailpoet/tests/integration/API/JSON/v1/SettingsTest.php index b8115348f3..0a795be301 100644 --- a/mailpoet/tests/integration/API/JSON/v1/SettingsTest.php +++ b/mailpoet/tests/integration/API/JSON/v1/SettingsTest.php @@ -5,7 +5,9 @@ namespace MailPoet\Test\API\JSON\v1; use Codeception\Stub\Expected; use Codeception\Util\Fixtures; use MailPoet\API\JSON\Error as APIError; +use MailPoet\API\JSON\ErrorResponse; use MailPoet\API\JSON\Response as APIResponse; +use MailPoet\API\JSON\SuccessResponse; use MailPoet\API\JSON\v1\Settings; use MailPoet\Config\ServicesChecker; use MailPoet\Cron\Workers\InactiveSubscribers; @@ -296,6 +298,21 @@ class SettingsTest extends \MailPoetTest { expect($response->meta['showNotice'])->equals(false); } + public function testItCanDeleteSetting() { + $this->settings->set('setting_to_be_deleted', true); + $response = $this->endpoint->delete('setting_to_be_deleted'); + expect($response)->isInstanceOf(SuccessResponse::class); + expect($this->settings->get('setting_to_be_deleted'))->null(); + } + + public function testDeleteReturnErrorForEmptySettingName() { + expect($this->endpoint->delete(''))->isInstanceOf(ErrorResponse::class); + } + + public function testDeleteReturnErrorIfSettingDoesntExist() { + expect($this->endpoint->delete('unexistent_setting'))->isInstanceOf(ErrorResponse::class); + } + private function createNewsletter(string $type, string $status = NewsletterEntity::STATUS_DRAFT, $parent = null): NewsletterEntity { $newsletter = new NewsletterEntity(); $newsletter->setType($type);